You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OTFFile.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 org.apache.fontbox.cff.CFFDataInput;
  21. import org.apache.fontbox.cff.CFFFont;
  22. import org.apache.fontbox.cff.CFFFont.Mapping;
  23. import org.apache.fontbox.cff.CFFParser;
  24. public class OTFFile extends OpenFont {
  25. protected CFFFont fileFont;
  26. public OTFFile() throws IOException {
  27. checkForFontbox();
  28. }
  29. private void checkForFontbox() throws IOException {
  30. try {
  31. Class.forName("org.apache.fontbox.cff.CFFFont");
  32. } catch (ClassNotFoundException ex) {
  33. throw new IOException("The Fontbox jar was not found in the classpath. This is "
  34. + "required for OTF CFF ssupport.");
  35. }
  36. }
  37. @Override
  38. protected void updateBBoxAndOffset() throws IOException {
  39. UnicodeMapping[] mappings = unicodeMappings.toArray(new UnicodeMapping[0]);
  40. for (int i = 0; i < mappings.length; i++) {
  41. int glyphIdx = mappings[i].getGlyphIndex();
  42. Mapping m = fileFont.getGIDMappings().get(glyphIdx);
  43. int[] bbox = fileFont.getBoundingBox(m.getSID());
  44. String name = fileFont.getNameOfCharFromCode(m.getSID());
  45. mtxTab[glyphIdx].setBoundingBox(bbox);
  46. mtxTab[glyphIdx].setName(name);
  47. }
  48. }
  49. @Override
  50. protected void initializeFont(FontFileReader in) throws IOException {
  51. fontFile = in;
  52. fontFile.seekSet(0);
  53. CFFParser parser = new CFFParser();
  54. fileFont = parser.parse(in.getAllBytes()).get(0);
  55. }
  56. protected void readName() throws IOException {
  57. Object familyName = fileFont.getProperty("FamilyName");
  58. if (familyName != null && !familyName.equals("")) {
  59. familyNames.add(familyName.toString());
  60. fullName = familyName.toString();
  61. } else {
  62. fullName = fileFont.getName();
  63. familyNames.add(fullName);
  64. }
  65. }
  66. /**
  67. * Reads the CFFData from a given font file
  68. * @param fontFile The font file being read
  69. * @return The byte data found in the CFF table
  70. */
  71. public static byte[] getCFFData(FontFileReader fontFile) throws IOException {
  72. byte[] cff = new byte[0];
  73. CFFDataInput input = new CFFDataInput(fontFile.getAllBytes());
  74. input.readBytes(4); //OTTO
  75. short numTables = input.readShort();
  76. input.readShort(); //searchRange
  77. input.readShort(); //entrySelector
  78. input.readShort(); //rangeShift
  79. for (int q = 0; q < numTables; q++) {
  80. String tagName = new String(input.readBytes(4));
  81. readLong(input); //Checksum
  82. long offset = readLong(input);
  83. long length = readLong(input);
  84. if (tagName.equals("CFF ")) {
  85. cff = new byte[(int)length];
  86. System.arraycopy(fontFile.getAllBytes(), (int)offset, cff, 0, cff.length);
  87. break;
  88. }
  89. }
  90. return cff;
  91. }
  92. private static long readLong(CFFDataInput input) throws IOException {
  93. return (input.readCard16() << 16) | input.readCard16();
  94. }
  95. }