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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*-- $Id$ --
  2. ============================================================================
  3. The Apache Software License, Version 1.1
  4. ============================================================================
  5. Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modifica-
  7. tion, are permitted provided that the following conditions are met:
  8. 1. Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright notice,
  11. this list of conditions and the following disclaimer in the documentation
  12. and/or other materials provided with the distribution.
  13. 3. The end-user documentation included with the redistribution, if any, must
  14. include the following acknowledgment: "This product includes software
  15. developed by the Apache Software Foundation (http://www.apache.org/)."
  16. Alternately, this acknowledgment may appear in the software itself, if
  17. and wherever such third-party acknowledgments normally appear.
  18. 4. The names "Fop" and "Apache Software Foundation" must not be used to
  19. endorse or promote products derived from this software without prior
  20. written permission. For written permission, please contact
  21. apache@apache.org.
  22. 5. Products derived from this software may not be called "Apache", nor may
  23. "Apache" appear in their name, without prior written permission of the
  24. Apache Software Foundation.
  25. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  26. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  27. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  29. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  30. DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  31. OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  32. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  34. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. This software consists of voluntary contributions made by many individuals
  36. on behalf of the Apache Software Foundation and was originally created by
  37. James Tauber <jtauber@jtauber.com>. For more information on the Apache
  38. Software Foundation, please see <http://www.apache.org/>.
  39. */
  40. package org.apache.fop.pdf;
  41. /**
  42. * class representing a font descriptor.
  43. *
  44. * Font descriptors are specified on page 222 and onwards of the PDF 1.3 spec.
  45. */
  46. public class PDFFontDescriptor extends PDFObject {
  47. // Required fields
  48. protected int ascent;
  49. protected int capHeight;
  50. protected int descent;
  51. protected int flags;
  52. protected PDFRectangle fontBBox;
  53. protected String basefont; //PDF-spec: FontName
  54. protected int italicAngle;
  55. protected int stemV;
  56. // Optional fields
  57. protected int stemH = 0;
  58. protected int xHeight = 0;
  59. protected int leading = 0;
  60. protected int avgWidth = 0;
  61. protected int maxWidth = 0;
  62. protected int missingWidth = 0;
  63. protected PDFStream fontfile;
  64. //protected String charSet = null;
  65. protected byte subtype;
  66. /**
  67. * create the /FontDescriptor object
  68. *
  69. * @param number the object's number
  70. * @param ascent the maximum height above the baseline
  71. * @param descent the maximum depth below the baseline
  72. * @param capHeight height of the capital letters
  73. * @param flags various characteristics of the font
  74. * @param fontBBox the bounding box for the described font
  75. * @param basefont the base font name
  76. * @param italicAngle the angle of the vertical dominant strokes
  77. * @param stemV the width of the dominant vertical stems of glyphs
  78. */
  79. public PDFFontDescriptor(int number,
  80. String basefont,
  81. int ascent, int descent,
  82. int capHeight, int flags,
  83. PDFRectangle fontBBox,
  84. int italicAngle, int stemV) {
  85. /* generic creation of PDF object */
  86. super(number);
  87. /* set fields using paramaters */
  88. this.basefont = basefont;
  89. this.ascent = ascent;
  90. this.descent = descent;
  91. this.capHeight = capHeight;
  92. this.flags = flags;
  93. this.fontBBox = fontBBox;
  94. this.italicAngle = italicAngle;
  95. this.stemV = stemV;
  96. }
  97. /**
  98. * set the optional metrics
  99. */
  100. public void setMetrics(int avgWidth, int maxWidth, int missingWidth, int leading, int stemH, int xHeight) {
  101. this.avgWidth = avgWidth;
  102. this.maxWidth = maxWidth;
  103. this.missingWidth = missingWidth;
  104. this.leading = leading;
  105. this.stemH = stemH;
  106. this.xHeight = xHeight;
  107. }
  108. /**
  109. * set the optional font file stream
  110. *
  111. * @param subtype the font type defined in the font stream
  112. * @param fontfile the stream containing an embedded font
  113. */
  114. public void setFontFile(byte subtype, PDFStream fontfile) {
  115. this.subtype = subtype;
  116. this.fontfile = fontfile;
  117. }
  118. //public void setCharSet(){}//for subset fonts
  119. /**
  120. * produce the PDF representation for the object
  121. *
  122. * @return the PDF
  123. */
  124. public byte[] toPDF() {
  125. StringBuffer p = new StringBuffer(
  126. this.number + " " + this.generation
  127. + " obj\n<< /Type /FontDescriptor"
  128. + "\n/FontName /" + this.basefont);
  129. p.append("\n/FontBBox "); p.append(fontBBox.toPDFString());
  130. p.append("\n/Flags "); p.append(flags);
  131. p.append("\n/CapHeight "); p.append(capHeight);
  132. p.append("\n/Ascent "); p.append(ascent);
  133. p.append("\n/Descent "); p.append(descent);
  134. p.append("\n/ItalicAngle "); p.append(italicAngle);
  135. p.append("\n/StemV "); p.append(stemV);
  136. // optional fields
  137. if (stemH != 0) {
  138. p.append("\n/StemH "); p.append(stemH);
  139. }
  140. if (xHeight != 0) {
  141. p.append("\n/XHeight "); p.append(xHeight);
  142. }
  143. if (avgWidth != 0) {
  144. p.append("\n/AvgWidth "); p.append(avgWidth);
  145. }
  146. if (maxWidth != 0) {
  147. p.append("\n/MaxWidth "); p.append(maxWidth);
  148. }
  149. if (missingWidth != 0) {
  150. p.append("\n/MissingWidth "); p.append(missingWidth);
  151. }
  152. if (leading != 0) {
  153. p.append("\n/Leading "); p.append(leading);
  154. }
  155. if (fontfile != null) {
  156. switch (subtype) {
  157. case PDFFont.TYPE1:
  158. p.append("\n/FontFile ");
  159. break;
  160. case PDFFont.TRUETYPE:
  161. p.append("\n/FontFile2 ");
  162. break;
  163. case PDFFont.TYPE0:
  164. p.append("\n/FontFile2 ");
  165. break;
  166. default:
  167. p.append("\n/FontFile2 ");
  168. }
  169. p.append(fontfile.referencePDF());
  170. }
  171. // charSet for subset fonts // not yet implemented
  172. // CID optional field
  173. fillInPDF(p);
  174. p.append("\n >>\nendobj\n");
  175. return p.toString().getBytes();
  176. }
  177. /**
  178. * fill in the specifics for the font's descriptor.
  179. *
  180. * the given buffer already contains the fields common to all descriptors.
  181. *
  182. * @param begin the buffer to be completed with the specific fields
  183. */
  184. protected void fillInPDF(StringBuffer begin) {
  185. }
  186. }