Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

OTFFile.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.fonts.truetype;
  19. import java.io.IOException;
  20. import java.util.List;
  21. import org.apache.fontbox.cff.CFFDataInput;
  22. import org.apache.fontbox.cff.CFFFont;
  23. import org.apache.fontbox.cff.CFFParser;
  24. import org.apache.fontbox.cff.CFFType1Font;
  25. public class OTFFile extends OpenFont {
  26. protected CFFFont fileFont;
  27. public OTFFile() throws IOException {
  28. this(true, false);
  29. }
  30. public OTFFile(boolean useKerning, boolean useAdvanced) throws IOException {
  31. super(useKerning, useAdvanced);
  32. checkForFontbox();
  33. }
  34. private void checkForFontbox() throws IOException {
  35. try {
  36. Class.forName("org.apache.fontbox.cff.CFFFont");
  37. } catch (ClassNotFoundException ex) {
  38. throw new IOException("The Fontbox jar was not found in the classpath. This is "
  39. + "required for OTF CFF ssupport.");
  40. }
  41. }
  42. @Override
  43. protected void updateBBoxAndOffset() throws IOException {
  44. Object bbox = fileFont.getTopDict().get("FontBBox");
  45. if (bbox != null) {
  46. List bboxList = (List) bbox;
  47. int[] bboxInt = new int[4];
  48. for (int i = 0; i < bboxInt.length; i++) {
  49. bboxInt[i] = (Integer) bboxList.get(i);
  50. }
  51. for (OFMtxEntry o : mtxTab) {
  52. o.setBoundingBox(bboxInt);
  53. }
  54. }
  55. }
  56. private static class Mapping {
  57. private int sid;
  58. private String name;
  59. private byte[] bytes;
  60. public void setSID(int sid) {
  61. this.sid = sid;
  62. }
  63. public int getSID() {
  64. return sid;
  65. }
  66. public void setName(String name) {
  67. this.name = name;
  68. }
  69. public String getName() {
  70. return name;
  71. }
  72. public void setBytes(byte[] bytes) {
  73. this.bytes = bytes;
  74. }
  75. public byte[] getBytes() {
  76. return bytes;
  77. }
  78. }
  79. @Override
  80. protected void initializeFont(FontFileReader in) throws IOException {
  81. fontFile = in;
  82. fontFile.seekSet(0);
  83. CFFParser parser = new CFFParser();
  84. fileFont = parser.parse(in.getAllBytes()).get(0);
  85. embedFontName = fileFont.getName();
  86. }
  87. protected void readName() throws IOException {
  88. Object familyName = fileFont.getTopDict().get("FamilyName");
  89. if (familyName != null && !familyName.equals("")) {
  90. familyNames.add(familyName.toString());
  91. fullName = familyName.toString();
  92. } else {
  93. fullName = fileFont.getName();
  94. familyNames.add(fullName);
  95. }
  96. }
  97. /**
  98. * Reads the CFFData from a given font file
  99. * @param fontFile The font file being read
  100. * @return The byte data found in the CFF table
  101. */
  102. public static byte[] getCFFData(FontFileReader fontFile) throws IOException {
  103. byte[] cff = fontFile.getAllBytes();
  104. CFFDataInput input = new CFFDataInput(fontFile.getAllBytes());
  105. input.readBytes(4); //OTTO
  106. short numTables = input.readShort();
  107. input.readShort(); //searchRange
  108. input.readShort(); //entrySelector
  109. input.readShort(); //rangeShift
  110. for (int q = 0; q < numTables; q++) {
  111. String tagName = new String(input.readBytes(4));
  112. readLong(input); //Checksum
  113. long offset = readLong(input);
  114. long length = readLong(input);
  115. if (tagName.equals("CFF ")) {
  116. cff = new byte[(int)length];
  117. System.arraycopy(fontFile.getAllBytes(), (int)offset, cff, 0, cff.length);
  118. break;
  119. }
  120. }
  121. return cff;
  122. }
  123. private static long readLong(CFFDataInput input) throws IOException {
  124. return (input.readCard16() << 16) | input.readCard16();
  125. }
  126. public boolean isType1() {
  127. return fileFont instanceof CFFType1Font;
  128. }
  129. }