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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 {
  98. put("FontFile2", fontfile);
  99. }
  100. }
  101. /** @return the FontFile or null if the font is not embedded */
  102. public AbstractPDFStream getFontFile() {
  103. AbstractPDFStream stream;
  104. stream = (AbstractPDFStream)get("FontFile");
  105. if (stream == null) {
  106. stream = (AbstractPDFStream)get("FontFile2");
  107. }
  108. if (stream == null) {
  109. stream = (AbstractPDFStream)get("FontFile3");
  110. }
  111. return stream;
  112. }
  113. /**
  114. * Sets the CIDSet stream for this font descriptor. (Optional)
  115. * @param cidSet the CIDSet stream
  116. */
  117. public void setCIDSet(AbstractPDFStream cidSet) {
  118. put("CIDSet", cidSet);
  119. }
  120. /** @return the CIDSet stream or null if not applicable */
  121. public AbstractPDFStream getCIDSet() {
  122. return (AbstractPDFStream)get("CIDSet");
  123. }
  124. /**
  125. * {@inheritDoc}
  126. */
  127. /*
  128. public String toPDFString() {
  129. StringBuffer p = new StringBuffer(128);
  130. p.append(getObjectID()
  131. + "<< /Type /FontDescriptor"
  132. + "\n/FontName /" + this.basefont);
  133. p.append("\n/FontBBox ");
  134. p.append(fontBBox.toPDFString());
  135. p.append("\n/Flags ");
  136. p.append(flags);
  137. p.append("\n/CapHeight ");
  138. p.append(capHeight);
  139. p.append("\n/Ascent ");
  140. p.append(ascent);
  141. p.append("\n/Descent ");
  142. p.append(descent);
  143. p.append("\n/ItalicAngle ");
  144. p.append(italicAngle);
  145. p.append("\n/StemV ");
  146. p.append(stemV);
  147. // optional fields
  148. if (stemH != 0) {
  149. p.append("\n/StemH ");
  150. p.append(stemH);
  151. }
  152. if (xHeight != 0) {
  153. p.append("\n/XHeight ");
  154. p.append(xHeight);
  155. }
  156. if (avgWidth != 0) {
  157. p.append("\n/AvgWidth ");
  158. p.append(avgWidth);
  159. }
  160. if (maxWidth != 0) {
  161. p.append("\n/MaxWidth ");
  162. p.append(maxWidth);
  163. }
  164. if (missingWidth != 0) {
  165. p.append("\n/MissingWidth ");
  166. p.append(missingWidth);
  167. }
  168. if (leading != 0) {
  169. p.append("\n/Leading ");
  170. p.append(leading);
  171. }
  172. if (fontfile != null) {
  173. if (subtype == FontType.TYPE1) {
  174. p.append("\n/FontFile ");
  175. } else {
  176. p.append("\n/FontFile2 ");
  177. }
  178. p.append(fontfile.referencePDF());
  179. }
  180. if (getCIDSet() != null) {
  181. p.append("\n/CIDSet ");
  182. p.append(getCIDSet().referencePDF());
  183. }
  184. // charSet for subset fonts // not yet implemented
  185. // CID optional field
  186. fillInPDF(p);
  187. p.append(" >>\nendobj\n");
  188. return p.toString();
  189. }*/
  190. /**
  191. * Fill in the specifics for the font's descriptor.
  192. * <p>
  193. * The given buffer already contains the fields common to all descriptors.
  194. *
  195. * @param begin the buffer to be completed with the specific fields
  196. *//*
  197. protected void fillInPDF(StringBuffer begin) {
  198. //nop
  199. }*/
  200. }