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.

PDFFontDescriptor.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 org.apache.fop.fonts.FontType;
  20. /**
  21. * Class representing a font descriptor (/FontDescriptor object).
  22. * <p>
  23. * Font descriptors are specified on page 222 and onwards of the PDF 1.3 spec.
  24. */
  25. public class PDFFontDescriptor extends PDFDictionary {
  26. /**
  27. * Create the /FontDescriptor object
  28. *
  29. * @param ascent the maximum height above the baseline
  30. * @param descent the maximum depth below the baseline
  31. * @param capHeight height of the capital letters
  32. * @param flags various characteristics of the font
  33. * @param fontBBox the bounding box for the described font
  34. * @param basefont the base font name
  35. * @param italicAngle the angle of the vertical dominant strokes
  36. * @param stemV the width of the dominant vertical stems of glyphs
  37. */
  38. public PDFFontDescriptor(String basefont, int ascent,
  39. int descent, int capHeight, int flags,
  40. PDFRectangle fontBBox, int italicAngle,
  41. int stemV) {
  42. super();
  43. put("Type", new PDFName("FontDescriptor"));
  44. put("FontName", new PDFName(basefont));
  45. put("FontBBox", fontBBox);
  46. put("Flags", flags);
  47. put("CapHeight", capHeight);
  48. put("Ascent", ascent);
  49. put("Descent", descent);
  50. put("ItalicAngle", italicAngle);
  51. put("StemV", stemV);
  52. }
  53. /**
  54. * Set the optional metrics.
  55. * @param avgWidth The average width of characters in this font.
  56. * The default value is 0.
  57. * @param maxWidth The maximum width of characters in this font.
  58. * The default value is 0.
  59. * @param missingWidth missing width
  60. * @param leading the desired spacing between lines of text.
  61. * The default value is 0.
  62. * @param stemH The vertical width of the dominant horizontal stems of
  63. * glyphs in the font. The default value is 0.
  64. * @param xHeight The y-coordinate of the top of flat non-ascending
  65. * lowercase letters, measured from the baseline. The default value is 0.
  66. */
  67. public void setMetrics(int avgWidth, int maxWidth, int missingWidth,
  68. int leading, int stemH, int xHeight) {
  69. if (avgWidth != 0) {
  70. put("AvgWidth", avgWidth);
  71. }
  72. if (maxWidth != 0) {
  73. put("MaxWidth", maxWidth);
  74. }
  75. if (missingWidth != 0) {
  76. put("MissingWidth", missingWidth);
  77. }
  78. if (leading != 0) {
  79. put("Leading", leading);
  80. }
  81. if (stemH != 0) {
  82. put("StemH", stemH);
  83. }
  84. if (xHeight != 0) {
  85. put("XHeight", xHeight);
  86. }
  87. }
  88. /**
  89. * Set the optional font file stream
  90. *
  91. * @param subtype the font type defined in the font stream
  92. * @param fontfile the stream containing an embedded font
  93. */
  94. public void setFontFile(FontType subtype, AbstractPDFStream fontfile) {
  95. if (subtype == FontType.TYPE1) {
  96. put("FontFile", fontfile);
  97. } else if (fontfile instanceof PDFCFFStreamType0C || subtype == FontType.TYPE1C) {
  98. put("FontFile3", fontfile);
  99. } else {
  100. put("FontFile2", fontfile);
  101. }
  102. }
  103. /** @return the FontFile or null if the font is not embedded */
  104. public AbstractPDFStream getFontFile() {
  105. AbstractPDFStream stream;
  106. stream = (AbstractPDFStream)get("FontFile");
  107. if (stream == null) {
  108. stream = (AbstractPDFStream)get("FontFile2");
  109. }
  110. if (stream == null) {
  111. stream = (AbstractPDFStream)get("FontFile3");
  112. }
  113. return stream;
  114. }
  115. /**
  116. * Sets the CIDSet stream for this font descriptor. (Optional)
  117. * @param cidSet the CIDSet stream
  118. */
  119. public void setCIDSet(AbstractPDFStream cidSet) {
  120. put("CIDSet", cidSet);
  121. }
  122. /** @return the CIDSet stream or null if not applicable */
  123. public AbstractPDFStream getCIDSet() {
  124. return (AbstractPDFStream)get("CIDSet");
  125. }
  126. /**
  127. * {@inheritDoc}
  128. */
  129. /*
  130. public String toPDFString() {
  131. StringBuffer p = new StringBuffer(128);
  132. p.append(getObjectID()
  133. + "<< /Type /FontDescriptor"
  134. + "\n/FontName /" + this.basefont);
  135. p.append("\n/FontBBox ");
  136. p.append(fontBBox.toPDFString());
  137. p.append("\n/Flags ");
  138. p.append(flags);
  139. p.append("\n/CapHeight ");
  140. p.append(capHeight);
  141. p.append("\n/Ascent ");
  142. p.append(ascent);
  143. p.append("\n/Descent ");
  144. p.append(descent);
  145. p.append("\n/ItalicAngle ");
  146. p.append(italicAngle);
  147. p.append("\n/StemV ");
  148. p.append(stemV);
  149. // optional fields
  150. if (stemH != 0) {
  151. p.append("\n/StemH ");
  152. p.append(stemH);
  153. }
  154. if (xHeight != 0) {
  155. p.append("\n/XHeight ");
  156. p.append(xHeight);
  157. }
  158. if (avgWidth != 0) {
  159. p.append("\n/AvgWidth ");
  160. p.append(avgWidth);
  161. }
  162. if (maxWidth != 0) {
  163. p.append("\n/MaxWidth ");
  164. p.append(maxWidth);
  165. }
  166. if (missingWidth != 0) {
  167. p.append("\n/MissingWidth ");
  168. p.append(missingWidth);
  169. }
  170. if (leading != 0) {
  171. p.append("\n/Leading ");
  172. p.append(leading);
  173. }
  174. if (fontfile != null) {
  175. if (subtype == FontType.TYPE1) {
  176. p.append("\n/FontFile ");
  177. } else {
  178. p.append("\n/FontFile2 ");
  179. }
  180. p.append(fontfile.referencePDF());
  181. }
  182. if (getCIDSet() != null) {
  183. p.append("\n/CIDSet ");
  184. p.append(getCIDSet().referencePDF());
  185. }
  186. // charSet for subset fonts // not yet implemented
  187. // CID optional field
  188. fillInPDF(p);
  189. p.append(" >>\nendobj\n");
  190. return p.toString();
  191. }*/
  192. /**
  193. * Fill in the specifics for the font's descriptor.
  194. * <p>
  195. * The given buffer already contains the fields common to all descriptors.
  196. *
  197. * @param begin the buffer to be completed with the specific fields
  198. *//*
  199. protected void fillInPDF(StringBuffer begin) {
  200. //nop
  201. }*/
  202. }