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

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