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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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) {
  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) {
  96. return new PDFFontType0(fontname, basefont,
  97. encoding);
  98. } else if ((subtype == FontType.TYPE1)
  99. || (subtype == FontType.MMTYPE1)) {
  100. return new PDFFontType1(fontname, basefont,
  101. encoding);
  102. } else if (subtype == FontType.TYPE3) {
  103. //return new PDFFontType3(number, fontname, basefont, encoding);
  104. return null; //NYI
  105. } else if (subtype == FontType.TRUETYPE) {
  106. return new PDFFontTrueType(fontname, basefont,
  107. encoding);
  108. } else {
  109. return null; // should not happen
  110. }
  111. }
  112. /**
  113. * Get the internal name used for this font.
  114. * @return the internal name
  115. */
  116. public String getName() {
  117. return this.fontname;
  118. }
  119. /**
  120. * Returns the name of the BaseFont.
  121. * @return the BaseFont
  122. */
  123. public PDFName getBaseFont() {
  124. return (PDFName)get("BaseFont");
  125. }
  126. /**
  127. * Returns the PDF name for a certain font type.
  128. * @param fontType font type
  129. * @return String corresponding PDF name
  130. */
  131. protected PDFName getPDFNameForFontType(FontType fontType) {
  132. if (fontType == FontType.TYPE0) {
  133. return new PDFName(fontType.getName());
  134. } else if (fontType == FontType.TYPE1) {
  135. return new PDFName(fontType.getName());
  136. } else if (fontType == FontType.MMTYPE1) {
  137. return new PDFName(fontType.getName());
  138. } else if (fontType == FontType.TYPE3) {
  139. return new PDFName(fontType.getName());
  140. } else if (fontType == FontType.TRUETYPE) {
  141. return new PDFName(fontType.getName());
  142. } else {
  143. throw new IllegalArgumentException("Unsupported font type: " + fontType.getName());
  144. }
  145. }
  146. /**
  147. * Validates the PDF object prior to serialization.
  148. */
  149. protected void validate() {
  150. if (getDocumentSafely().getProfile().isFontEmbeddingRequired()) {
  151. if (this.getClass() == PDFFont.class) {
  152. throw new PDFConformanceException("For " + getDocumentSafely().getProfile()
  153. + ", all fonts, even the base 14"
  154. + " fonts, have to be embedded! Offending font: " + getBaseFont());
  155. }
  156. }
  157. }
  158. /** {@inheritDoc} */
  159. public int output(OutputStream stream) throws IOException {
  160. validate();
  161. return super.output(stream);
  162. }
  163. }