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

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