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.

PDFCIDFont.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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.CIDFontType;
  20. // based on work by Takayuki Takeuchi
  21. /**
  22. * Class representing a "character identifier" font (p 210 and onwards).
  23. */
  24. public class PDFCIDFont extends PDFObject {
  25. private String basefont;
  26. private CIDFontType cidtype;
  27. private Integer dw;
  28. private PDFWArray w;
  29. private int[] dw2;
  30. private PDFWArray w2;
  31. private PDFCIDSystemInfo systemInfo;
  32. private PDFCIDFontDescriptor descriptor;
  33. private PDFCMap cmap;
  34. /**
  35. * /CIDToGIDMap (only for CIDFontType2, see p 212)
  36. * can be either "Identity" (default) or a PDFStream
  37. */
  38. private PDFStream cidMap;
  39. /**
  40. * Create the /Font object
  41. * @param basefont Name of the basefont
  42. * @param cidtype CID type
  43. * @param dw default width
  44. * @param w array of character widths
  45. * @param registry name of the issuer
  46. * @param ordering Unique name of the font
  47. * @param supplement Supplement number
  48. * @param descriptor CID font descriptor
  49. */
  50. public PDFCIDFont( // CSOK: ParameterNumber
  51. String basefont, CIDFontType cidtype, int dw,
  52. int[] w, String registry, String ordering,
  53. int supplement, PDFCIDFontDescriptor descriptor) {
  54. this(basefont, cidtype, dw,
  55. new PDFWArray(w),
  56. new PDFCIDSystemInfo(registry, ordering, supplement),
  57. descriptor);
  58. }
  59. /**
  60. * Create the /Font object
  61. * @param basefont Name of the basefont
  62. * @param cidtype CID type
  63. * @param dw default width
  64. * @param w array of character widths
  65. * @param systemInfo CID system info
  66. * @param descriptor CID font descriptor
  67. */
  68. public PDFCIDFont(String basefont, CIDFontType cidtype, int dw,
  69. int[] w, PDFCIDSystemInfo systemInfo,
  70. PDFCIDFontDescriptor descriptor) {
  71. this(basefont, cidtype, dw,
  72. new PDFWArray(w),
  73. systemInfo,
  74. descriptor);
  75. }
  76. /**
  77. * Create the /Font object
  78. * @param basefont Name of the basefont
  79. * @param cidtype CID type
  80. * @param dw default width
  81. * @param w array of character widths
  82. * @param systemInfo CID system info
  83. * @param descriptor CID font descriptor
  84. */
  85. public PDFCIDFont(String basefont, CIDFontType cidtype, int dw,
  86. PDFWArray w, PDFCIDSystemInfo systemInfo,
  87. PDFCIDFontDescriptor descriptor) {
  88. super();
  89. this.basefont = basefont;
  90. this.cidtype = cidtype;
  91. this.dw = new Integer(dw);
  92. this.w = w;
  93. this.dw2 = null;
  94. this.w2 = null;
  95. this.systemInfo = systemInfo;
  96. this.descriptor = descriptor;
  97. this.cidMap = null;
  98. this.cmap = null;
  99. }
  100. /**
  101. * Set the /DW attribute
  102. * @param dw the default width
  103. */
  104. public void setDW(int dw) {
  105. this.dw = new Integer(dw);
  106. }
  107. /**
  108. * Set the /W array
  109. * @param w the width array
  110. */
  111. public void setW(PDFWArray w) {
  112. this.w = w;
  113. }
  114. /**
  115. * Set the (two elements) /DW2 array
  116. * @param dw2 the default metrics for vertical writing
  117. */
  118. public void setDW2(int[] dw2) {
  119. this.dw2 = dw2;
  120. }
  121. /**
  122. * Set the two elements of the /DW2 array
  123. * @param posY position vector
  124. * @param displacementY displacement vector
  125. */
  126. public void setDW2(int posY, int displacementY) {
  127. this.dw2 = new int[] {
  128. posY, displacementY
  129. };
  130. }
  131. /**
  132. * Set the CMap used as /ToUnicode cmap
  133. * @param cmap character map
  134. */
  135. public void setCMAP(PDFCMap cmap) {
  136. this.cmap = cmap;
  137. }
  138. /**
  139. * Set the /W2 array
  140. * @param w2 array of metrics for vertical writing
  141. */
  142. public void setW2(PDFWArray w2) {
  143. this.w2 = w2;
  144. }
  145. /**
  146. * Set the /CIDToGIDMap (to be used only for CIDFontType2)
  147. * @param map mapping information
  148. */
  149. public void setCIDMap(PDFStream map) {
  150. this.cidMap = map;
  151. }
  152. /**
  153. * Set the /CIDToGIDMap (to be used only for CIDFontType2) to "Identity"
  154. */
  155. public void setCIDMapIdentity() {
  156. this.cidMap = null; // not an error here, simply use the default
  157. }
  158. /**
  159. * Returns the PDF name for a certain CID font type.
  160. * @param cidFontType CID font type
  161. * @return corresponding PDF name
  162. */
  163. protected String getPDFNameForCIDFontType(CIDFontType cidFontType) {
  164. if (cidFontType == CIDFontType.CIDTYPE0) {
  165. return cidFontType.getName();
  166. } else if (cidFontType == CIDFontType.CIDTYPE2) {
  167. return cidFontType.getName();
  168. } else {
  169. throw new IllegalArgumentException("Unsupported CID font type: "
  170. + cidFontType.getName());
  171. }
  172. }
  173. /**
  174. * {@inheritDoc}
  175. */
  176. public String toPDFString() {
  177. StringBuffer p = new StringBuffer(128);
  178. p.append("<< /Type /Font");
  179. p.append("\n/BaseFont /");
  180. p.append(this.basefont);
  181. p.append(" \n/CIDToGIDMap ");
  182. if (cidMap != null) {
  183. p.append(cidMap.referencePDF());
  184. } else {
  185. p.append("/Identity");
  186. //This is the default. We still write it because PDF/A requires it.
  187. }
  188. p.append(" \n/Subtype /");
  189. p.append(getPDFNameForCIDFontType(this.cidtype));
  190. p.append("\n");
  191. p.append(systemInfo.toPDFString());
  192. p.append("\n/FontDescriptor ");
  193. p.append(this.descriptor.referencePDF());
  194. if (cmap != null) {
  195. p.append("\n/ToUnicode ");
  196. p.append(cmap.referencePDF());
  197. }
  198. if (dw != null) {
  199. p.append("\n/DW ");
  200. p.append(this.dw);
  201. }
  202. if (w != null) {
  203. p.append("\n/W ");
  204. p.append(w.toPDFString());
  205. }
  206. if (dw2 != null) {
  207. p.append("\n/DW2 ["); // always two values, see p 211
  208. p.append(this.dw2[0]);
  209. p.append(this.dw2[1]);
  210. p.append("]");
  211. }
  212. if (w2 != null) {
  213. p.append("\n/W2 ");
  214. p.append(w2.toPDFString());
  215. }
  216. p.append("\n>>");
  217. return p.toString();
  218. }
  219. }