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.

PDFResources.java 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 java.util.HashMap;
  20. import java.util.HashSet;
  21. import java.util.Iterator;
  22. import java.util.Map;
  23. import java.util.Set;
  24. import org.apache.fop.fonts.FontDescriptor;
  25. import org.apache.fop.fonts.FontInfo;
  26. import org.apache.fop.fonts.Typeface;
  27. import org.apache.fop.fonts.base14.Symbol;
  28. import org.apache.fop.fonts.base14.ZapfDingbats;
  29. import org.apache.fop.util.ColorProfileUtil;
  30. /**
  31. * class representing a /Resources object.
  32. *
  33. * /Resources object contain a list of references to the fonts for the
  34. * document
  35. */
  36. public class PDFResources extends PDFObject {
  37. /**
  38. * /Font objects keyed by their internal name
  39. */
  40. protected Map fonts = new HashMap();
  41. /**
  42. * Set of XObjects
  43. */
  44. protected Set xObjects = new HashSet();
  45. /**
  46. * Set of patterns
  47. */
  48. protected Set patterns = new HashSet();
  49. /**
  50. * Set of shadings
  51. */
  52. protected Set shadings = new HashSet();
  53. /**
  54. * Set of ExtGStates
  55. */
  56. protected Set gstates = new HashSet();
  57. /** Map of color spaces (key: color space name) */
  58. protected Map colorSpaces = new HashMap();
  59. /** Map of ICC color spaces (key: ICC profile description) */
  60. protected Map iccColorSpaces = new HashMap();
  61. /**
  62. * create a /Resources object.
  63. *
  64. * @param objnum the object's number
  65. */
  66. public PDFResources(int objnum) {
  67. /* generic creation of object */
  68. super();
  69. setObjectNumber(objnum);
  70. }
  71. /**
  72. * add font object to resources list.
  73. *
  74. * @param font the PDFFont to add
  75. */
  76. public void addFont(PDFFont font) {
  77. this.fonts.put(font.getName(), font);
  78. }
  79. /**
  80. * Add the fonts in the font info to this PDF document's Font Resources.
  81. *
  82. * @param doc PDF document to add fonts to
  83. * @param fontInfo font info object to get font information from
  84. */
  85. public void addFonts(PDFDocument doc, FontInfo fontInfo) {
  86. Map usedFonts = fontInfo.getUsedFonts();
  87. Iterator e = usedFonts.keySet().iterator();
  88. while (e.hasNext()) {
  89. String f = (String)e.next();
  90. Typeface font = (Typeface)usedFonts.get(f);
  91. //Check if the font actually had any mapping operations. If not, it is an indication
  92. //that it has never actually been used and therefore doesn't have to be embedded.
  93. if (font.hadMappingOperations()) {
  94. FontDescriptor desc = null;
  95. if (font instanceof FontDescriptor) {
  96. desc = (FontDescriptor)font;
  97. }
  98. String encoding = font.getEncodingName();
  99. if (font instanceof Symbol || font instanceof ZapfDingbats) {
  100. encoding = null; //Symbolic fonts shouldn't specify an encoding value in PDF
  101. }
  102. addFont(doc.getFactory().makeFont(
  103. f, font.getEmbedFontName(), encoding, font, desc));
  104. }
  105. }
  106. }
  107. /**
  108. * Add a PDFGState to the resources.
  109. *
  110. * @param gs the PDFGState to add
  111. */
  112. public void addGState(PDFGState gs) {
  113. this.gstates.add(gs);
  114. }
  115. /**
  116. * Add a Shading to the resources.
  117. *
  118. * @param theShading the shading to add
  119. */
  120. public void addShading(PDFShading theShading) {
  121. this.shadings.add(theShading);
  122. }
  123. /**
  124. * Add the pattern to the resources.
  125. *
  126. * @param thePattern the pattern to add
  127. */
  128. public void addPattern(PDFPattern thePattern) {
  129. this.patterns.add(thePattern);
  130. }
  131. /**
  132. * Add an XObject to the resources.
  133. *
  134. * @param xObject the XObject to add
  135. */
  136. public void addXObject(PDFXObject xObject) {
  137. this.xObjects.add(xObject);
  138. }
  139. /**
  140. * Add a ColorSpace dictionary to the resources.
  141. * @param colorSpace the color space
  142. */
  143. public void addColorSpace(PDFICCBasedColorSpace colorSpace) {
  144. this.colorSpaces.put(colorSpace.getName(), colorSpace);
  145. String desc = ColorProfileUtil.getICCProfileDescription(
  146. colorSpace.getICCStream().getICCProfile());
  147. this.iccColorSpaces.put(desc, colorSpace);
  148. }
  149. /**
  150. * Returns a ICCBased color space by profile name.
  151. * @param desc the name of the color space
  152. * @return the requested color space or null if it wasn't found
  153. */
  154. public PDFICCBasedColorSpace getICCColorSpaceByProfileName(String desc) {
  155. PDFICCBasedColorSpace cs = (PDFICCBasedColorSpace)this.iccColorSpaces.get(desc);
  156. return cs;
  157. }
  158. /**
  159. * Returns a color space by name.
  160. * @param name the name of the color space
  161. * @return the requested color space or null if it wasn't found
  162. */
  163. public PDFICCBasedColorSpace getColorSpace(String name) {
  164. PDFICCBasedColorSpace cs = (PDFICCBasedColorSpace)this.colorSpaces.get(name);
  165. return cs;
  166. }
  167. /**
  168. * represent the object in PDF
  169. * This adds the references to all the objects in the current
  170. * resource context.
  171. *
  172. * @return the PDF
  173. * {@inheritDoc}
  174. */
  175. public String toPDFString() {
  176. StringBuffer p = new StringBuffer(128);
  177. p.append(getObjectID() + "<<\n");
  178. if (!this.fonts.isEmpty()) {
  179. p.append("/Font <<\n");
  180. /* construct PDF dictionary of font object references */
  181. Iterator fontIterator = this.fonts.keySet().iterator();
  182. while (fontIterator.hasNext()) {
  183. String fontName = (String)fontIterator.next();
  184. p.append(" /" + fontName + " "
  185. + ((PDFFont)this.fonts.get(fontName)).referencePDF()
  186. + "\n");
  187. }
  188. p.append(">>\n");
  189. }
  190. PDFShading currentShading = null;
  191. if (!this.shadings.isEmpty()) {
  192. p.append("/Shading <<\n");
  193. for (Iterator iter = shadings.iterator(); iter.hasNext();) {
  194. currentShading = (PDFShading)iter.next();
  195. p.append(" /" + currentShading.getName() + " "
  196. + currentShading.referencePDF() + " "); // \n ??????
  197. }
  198. p.append(">>\n");
  199. }
  200. // "free" the memory. Sorta.
  201. currentShading = null;
  202. PDFPattern currentPattern = null;
  203. if (!this.patterns.isEmpty()) {
  204. p.append("/Pattern <<\n");
  205. for (Iterator iter = patterns.iterator(); iter.hasNext();) {
  206. currentPattern = (PDFPattern)iter.next();
  207. p.append(" /" + currentPattern.getName() + " "
  208. + currentPattern.referencePDF() + " ");
  209. }
  210. p.append(">>\n");
  211. }
  212. // "free" the memory. Sorta.
  213. currentPattern = null;
  214. p.append("/ProcSet [ /PDF /ImageB /ImageC /Text ]\n");
  215. if (this.xObjects != null && !this.xObjects.isEmpty()) {
  216. p = p.append("/XObject <<\n");
  217. for (Iterator iter = xObjects.iterator(); iter.hasNext();) {
  218. PDFXObject xobj = (PDFXObject)iter.next();
  219. p = p.append(" " + xobj.getName() + " "
  220. + xobj.referencePDF()
  221. + "\n");
  222. }
  223. p = p.append(">>\n");
  224. }
  225. if (!this.gstates.isEmpty()) {
  226. p = p.append("/ExtGState <<\n");
  227. for (Iterator iter = gstates.iterator(); iter.hasNext();) {
  228. PDFGState gs = (PDFGState)iter.next();
  229. p = p.append(" /" + gs.getName() + " "
  230. + gs.referencePDF()
  231. + "\n");
  232. }
  233. p = p.append(">>\n");
  234. }
  235. if (!this.colorSpaces.isEmpty()) {
  236. p = p.append("/ColorSpace <<\n");
  237. for (Iterator iter = colorSpaces.values().iterator(); iter.hasNext();) {
  238. PDFICCBasedColorSpace colorSpace = (PDFICCBasedColorSpace)iter.next();
  239. p = p.append(" /" + colorSpace.getName() + " "
  240. + colorSpace.referencePDF()
  241. + "\n");
  242. }
  243. p = p.append(">>\n");
  244. }
  245. p = p.append(">>\nendobj\n");
  246. return p.toString();
  247. }
  248. }