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.

PDFFont.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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.pdf;
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import org.apache.fop.fonts.FontType;
  22. /**
  23. * Class representing a /Font object.
  24. * <p>
  25. * A more complete object expressing the base font name and encoding of a
  26. * font along with an internal name for the font used within
  27. * streams of content.
  28. * <p>
  29. * Fonts are specified on page 198 and onwards of the PDF 1.3 spec.
  30. */
  31. public class PDFFont extends PDFDictionary {
  32. /** Internal F-number for each font (it is not written to the font dict) */
  33. private String fontname;
  34. /**
  35. * create the /Font object
  36. *
  37. * @param fontname the internal name for the font
  38. * @param subtype the font's subtype
  39. * @param basefont the base font name
  40. * @param encoding the character encoding schema used by the font
  41. */
  42. public PDFFont(String fontname, FontType subtype,
  43. String basefont,
  44. Object encoding) {
  45. /* generic creation of PDF object */
  46. super();
  47. this.fontname = fontname;
  48. put("Type", new PDFName("Font"));
  49. put("Subtype", getPDFNameForFontType(subtype));
  50. //put("Name", new PDFName(fontname));
  51. put("BaseFont", new PDFName(basefont));
  52. if (encoding instanceof PDFEncoding) {
  53. setEncoding((PDFEncoding)encoding);
  54. } else if (encoding instanceof String) {
  55. setEncoding((String)encoding);
  56. }
  57. }
  58. /**
  59. * Sets the Encoding value of the font.
  60. * @param encoding the encoding
  61. */
  62. public void setEncoding(String encoding) {
  63. if (encoding != null && !PDFEncoding.hasStandardEncoding(encoding)) {
  64. put("Encoding", new PDFName(encoding));
  65. }
  66. }
  67. /**
  68. * Sets the Encoding value of the font.
  69. * @param encoding the encoding
  70. */
  71. public void setEncoding(PDFEncoding encoding) {
  72. if (encoding != null) {
  73. put("Encoding", encoding);
  74. }
  75. }
  76. /**
  77. * Sets a ToUnicode CMap.
  78. * @param cmap the ToUnicode character map
  79. */
  80. public void setToUnicode(PDFCMap cmap) {
  81. put("ToUnicode", cmap);
  82. }
  83. /**
  84. * factory method with the basic parameters
  85. *
  86. * @param fontname the internal name for the font
  87. * @param subtype the font's subtype
  88. * @param basefont the base font name
  89. * @param encoding the character encoding schema used by the font
  90. * @return the generated PDFFont object
  91. */
  92. public static PDFFont createFont(String fontname,
  93. FontType subtype, String basefont,
  94. Object encoding) {
  95. if (subtype == FontType.TYPE0 || subtype == FontType.CIDTYPE0) {
  96. return new PDFFontType0(fontname, basefont,
  97. encoding);
  98. } else if ((subtype == FontType.TYPE1)
  99. || (subtype == FontType.TYPE1C)
  100. || (subtype == FontType.MMTYPE1)) {
  101. return new PDFFontType1(fontname, basefont,
  102. encoding);
  103. } else if (subtype == FontType.TYPE3) {
  104. //return new PDFFontType3(number, fontname, basefont, encoding);
  105. return null; //NYI
  106. } else if (subtype == FontType.TRUETYPE) {
  107. return new PDFFontTrueType(fontname, basefont,
  108. encoding);
  109. } else {
  110. return null; // should not happen
  111. }
  112. }
  113. /**
  114. * Get the internal name used for this font.
  115. * @return the internal name
  116. */
  117. public String getName() {
  118. return this.fontname;
  119. }
  120. /**
  121. * Returns the name of the BaseFont.
  122. * @return the BaseFont
  123. */
  124. public PDFName getBaseFont() {
  125. return (PDFName)get("BaseFont");
  126. }
  127. /**
  128. * Returns the PDF name for a certain font type.
  129. * @param fontType font type
  130. * @return String corresponding PDF name
  131. */
  132. protected PDFName getPDFNameForFontType(FontType fontType) {
  133. if (fontType == FontType.TYPE0) {
  134. return new PDFName(fontType.getName());
  135. } else if (fontType == FontType.TYPE1) {
  136. return new PDFName(fontType.getName());
  137. } else if (fontType == FontType.MMTYPE1) {
  138. return new PDFName(fontType.getName());
  139. } else if (fontType == FontType.TYPE3) {
  140. return new PDFName(fontType.getName());
  141. } else if (fontType == FontType.TRUETYPE) {
  142. return new PDFName(fontType.getName());
  143. } else {
  144. throw new IllegalArgumentException("Unsupported font type: " + fontType.getName());
  145. }
  146. }
  147. /**
  148. * Validates the PDF object prior to serialization.
  149. */
  150. protected void validate() {
  151. if (getDocumentSafely().getProfile().isFontEmbeddingRequired()) {
  152. if (this.getClass() == PDFFont.class) {
  153. throw new PDFConformanceException("For " + getDocumentSafely().getProfile()
  154. + ", all fonts, even the base 14"
  155. + " fonts, have to be embedded! Offending font: " + getBaseFont());
  156. }
  157. }
  158. }
  159. /** {@inheritDoc} */
  160. public int output(OutputStream stream) throws IOException {
  161. validate();
  162. return super.output(stream);
  163. }
  164. }