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 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.CFFParser;
  23. import org.apache.fontbox.cff.CFFType1Font;
  24. public class OTFFile extends OpenFont {
  25. protected CFFFont fileFont;
  26. public OTFFile() throws IOException {
  27. this(true, false);
  28. }
  29. public OTFFile(boolean useKerning, boolean useAdvanced) throws IOException {
  30. super(useKerning, useAdvanced);
  31. checkForFontbox();
  32. }
  33. private void checkForFontbox() throws IOException {
  34. try {
  35. Class.forName("org.apache.fontbox.cff.CFFFont");
  36. } catch (ClassNotFoundException ex) {
  37. throw new IOException("The Fontbox jar was not found in the classpath. This is "
  38. + "required for OTF CFF ssupport.");
  39. }
  40. }
  41. @Override
  42. protected void updateBBoxAndOffset() throws IOException {
  43. }
  44. private static class Mapping {
  45. private int sid;
  46. private String name;
  47. private byte[] bytes;
  48. public void setSID(int sid) {
  49. this.sid = sid;
  50. }
  51. public int getSID() {
  52. return sid;
  53. }
  54. public void setName(String name) {
  55. this.name = name;
  56. }
  57. public String getName() {
  58. return name;
  59. }
  60. public void setBytes(byte[] bytes) {
  61. this.bytes = bytes;
  62. }
  63. public byte[] getBytes() {
  64. return bytes;
  65. }
  66. }
  67. @Override
  68. protected void initializeFont(FontFileReader in) throws IOException {
  69. fontFile = in;
  70. fontFile.seekSet(0);
  71. CFFParser parser = new CFFParser();
  72. fileFont = parser.parse(in.getAllBytes()).get(0);
  73. embedFontName = fileFont.getName();
  74. }
  75. protected void readName() throws IOException {
  76. Object familyName = fileFont.getTopDict().get("FamilyName");
  77. if (familyName != null && !familyName.equals("")) {
  78. familyNames.add(familyName.toString());
  79. fullName = familyName.toString();
  80. } else {
  81. fullName = fileFont.getName();
  82. familyNames.add(fullName);
  83. }
  84. }
  85. /**
  86. * Reads the CFFData from a given font file
  87. * @param fontFile The font file being read
  88. * @return The byte data found in the CFF table
  89. */
  90. public static byte[] getCFFData(FontFileReader fontFile) throws IOException {
  91. byte[] cff = fontFile.getAllBytes();
  92. CFFDataInput input = new CFFDataInput(fontFile.getAllBytes());
  93. input.readBytes(4); //OTTO
  94. short numTables = input.readShort();
  95. input.readShort(); //searchRange
  96. input.readShort(); //entrySelector
  97. input.readShort(); //rangeShift
  98. for (int q = 0; q < numTables; q++) {
  99. String tagName = new String(input.readBytes(4));
  100. readLong(input); //Checksum
  101. long offset = readLong(input);
  102. long length = readLong(input);
  103. if (tagName.equals("CFF ")) {
  104. cff = new byte[(int)length];
  105. System.arraycopy(fontFile.getAllBytes(), (int)offset, cff, 0, cff.length);
  106. break;
  107. }
  108. }
  109. return cff;
  110. }
  111. private static long readLong(CFFDataInput input) throws IOException {
  112. return (input.readCard16() << 16) | input.readCard16();
  113. }
  114. public boolean isType1() {
  115. return fileFont instanceof CFFType1Font;
  116. }
  117. }