Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

PDFFont.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.pdf;
  18. import org.apache.fop.fonts.FontType;
  19. /**
  20. * Class representing a /Font object.
  21. * <p>
  22. * A more complete object expressing the base font name and encoding of a
  23. * font along with an internal name for the font used within
  24. * streams of content.
  25. * <p>
  26. * Fonts are specified on page 198 and onwards of the PDF 1.3 spec.
  27. */
  28. public class PDFFont extends PDFObject {
  29. /**
  30. * the internal name for the font (eg "F1")
  31. */
  32. protected String fontname;
  33. /**
  34. * the font's subtype
  35. * (as defined by the constants FontType: TYPE0, TYPE1, MMTYPE1, TYPE3, TRUETYPE)
  36. */
  37. protected FontType subtype;
  38. /**
  39. * the base font name (eg "Helvetica")
  40. */
  41. protected String basefont;
  42. /**
  43. * the character encoding scheme used by the font.
  44. * It can be a String for standard encodings, or
  45. * a PDFEncoding for a more complex scheme, or
  46. * a PDFStream containing a CMap in a Type0 font.
  47. * If <code>null</code> then not written out in the PDF.
  48. */
  49. protected Object encoding;
  50. /**
  51. * the Unicode mapping mechanism
  52. */
  53. // protected PDFToUnicode mapping;
  54. /**
  55. * create the /Font object
  56. *
  57. * @param fontname the internal name for the font
  58. * @param subtype the font's subtype
  59. * @param basefont the base font name
  60. * @param encoding the character encoding schema used by the font
  61. */
  62. public PDFFont(String fontname, FontType subtype,
  63. String basefont,
  64. Object encoding /* , PDFToUnicode mapping */) {
  65. /* generic creation of PDF object */
  66. super();
  67. /* set fields using paramaters */
  68. this.fontname = fontname;
  69. this.subtype = subtype;
  70. this.basefont = basefont;
  71. this.encoding = encoding;
  72. // this.mapping = mapping;
  73. }
  74. /**
  75. * factory method with the basic parameters
  76. *
  77. * @param fontname the internal name for the font
  78. * @param subtype the font's subtype
  79. * @param basefont the base font name
  80. * @param encoding the character encoding schema used by the font
  81. * @return the generated PDFFont object
  82. */
  83. public static PDFFont createFont(String fontname,
  84. FontType subtype, String basefont,
  85. Object encoding) {
  86. if (subtype == FontType.TYPE0) {
  87. return new PDFFontType0(fontname, basefont,
  88. encoding);
  89. } else if ((subtype == FontType.TYPE1)
  90. || (subtype == FontType.MMTYPE1)) {
  91. return new PDFFontType1(fontname, basefont,
  92. encoding);
  93. } else if (subtype == FontType.TYPE3) {
  94. //return new PDFFontType3(number, fontname, basefont, encoding);
  95. return null; //NYI
  96. } else if (subtype == FontType.TRUETYPE) {
  97. return new PDFFontTrueType(fontname, basefont,
  98. encoding);
  99. } else {
  100. return null; // should not happend
  101. }
  102. }
  103. /**
  104. * factory method with the extended parameters
  105. * for Type1, MMType1 and TrueType
  106. *
  107. * @param fontname the internal name for the font
  108. * @param subtype the font's subtype
  109. * @param basefont the base font name
  110. * @param encoding the character encoding schema used by the font
  111. * @param firstChar the first character code in the font
  112. * @param lastChar the last character code in the font
  113. * @param widths an array of size (lastChar - firstChar +1)
  114. * @param descriptor the descriptor for other font's metrics
  115. * @return the generated PDFFont object
  116. */
  117. public static PDFFont createFont(String fontname,
  118. FontType subtype, String basefont,
  119. Object encoding, int firstChar,
  120. int lastChar, PDFArray widths,
  121. PDFFontDescriptor descriptor) {
  122. PDFFontNonBase14 font;
  123. if (subtype == FontType.TYPE0) {
  124. font = new PDFFontType0(fontname, basefont,
  125. encoding);
  126. font.setDescriptor(descriptor);
  127. return font;
  128. } else if ((subtype == FontType.TYPE1)
  129. || (subtype == FontType.MMTYPE1)) {
  130. font = new PDFFontType1(fontname, basefont,
  131. encoding);
  132. font.setWidthMetrics(firstChar, lastChar, widths);
  133. font.setDescriptor(descriptor);
  134. return font;
  135. } else if (subtype == FontType.TYPE3) {
  136. return null; //NYI, should not happend
  137. } else if (subtype == FontType.TRUETYPE) {
  138. font = new PDFFontTrueType(fontname, basefont,
  139. encoding);
  140. font.setWidthMetrics(firstChar, lastChar, widths);
  141. font.setDescriptor(descriptor);
  142. return font;
  143. } else {
  144. return null; // should not happend
  145. }
  146. }
  147. /**
  148. * get the internal name used for this font
  149. * @return the internal name
  150. */
  151. public String getName() {
  152. return this.fontname;
  153. }
  154. /**
  155. * Returns the PDF name for a certain font type.
  156. * @param fontType font type
  157. * @return String corresponding PDF name
  158. */
  159. protected String getPDFNameForFontType(FontType fontType) {
  160. if (fontType == FontType.TYPE0) {
  161. return fontType.getName();
  162. } else if (fontType == FontType.TYPE1) {
  163. return fontType.getName();
  164. } else if (fontType == FontType.MMTYPE1) {
  165. return fontType.getName();
  166. } else if (fontType == FontType.TYPE3) {
  167. return fontType.getName();
  168. } else if (fontType == FontType.TRUETYPE) {
  169. return fontType.getName();
  170. } else {
  171. throw new IllegalArgumentException("Unsupported font type: " + fontType.getName());
  172. }
  173. }
  174. /**
  175. * @see org.apache.fop.pdf.PDFObject#toPDFString()
  176. */
  177. public String toPDFString() {
  178. StringBuffer p = new StringBuffer(128);
  179. p.append(getObjectID());
  180. p.append("<< /Type /Font\n/Subtype /"
  181. + getPDFNameForFontType(this.subtype)
  182. + "\n/Name /" + this.fontname
  183. + "\n/BaseFont /" + this.basefont);
  184. if (encoding != null) {
  185. p.append("\n/Encoding ");
  186. if (encoding instanceof PDFEncoding) {
  187. p.append(((PDFEncoding)this.encoding).referencePDF());
  188. } else if (encoding instanceof PDFStream) {
  189. p.append(((PDFStream)this.encoding).referencePDF());
  190. } else {
  191. p.append("/").append((String)encoding);
  192. }
  193. }
  194. fillInPDF(p);
  195. p.append(" >>\nendobj\n");
  196. return p.toString();
  197. }
  198. /**
  199. * This method is called to receive the specifics for the font's subtype.
  200. * <p>
  201. * The given buffer already contains the fields common to all font types.
  202. *
  203. * @param target the buffer to be completed with the type specific fields
  204. */
  205. protected void fillInPDF(StringBuffer target) {
  206. //nop
  207. }
  208. }