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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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.io.IOException;
  20. import java.io.OutputStream;
  21. import java.util.HashMap;
  22. import java.util.HashSet;
  23. import java.util.Iterator;
  24. import java.util.Map;
  25. import java.util.Set;
  26. import org.apache.xmlgraphics.java2d.color.profile.ColorProfileUtil;
  27. import org.apache.fop.fonts.FontDescriptor;
  28. import org.apache.fop.fonts.FontInfo;
  29. import org.apache.fop.fonts.Typeface;
  30. import org.apache.fop.fonts.base14.Symbol;
  31. import org.apache.fop.fonts.base14.ZapfDingbats;
  32. /**
  33. * Class representing a /Resources object.
  34. *
  35. * /Resources object contain a list of references to the fonts for the
  36. * document
  37. */
  38. public class PDFResources extends PDFDictionary {
  39. /**
  40. * /Font objects keyed by their internal name
  41. */
  42. protected Map fonts = new HashMap();
  43. /**
  44. * Set of XObjects
  45. */
  46. protected Set xObjects = new HashSet();
  47. /**
  48. * Set of patterns
  49. */
  50. protected Set patterns = new HashSet();
  51. /**
  52. * Set of shadings
  53. */
  54. protected Set shadings = new HashSet();
  55. /**
  56. * Set of ExtGStates
  57. */
  58. protected Set gstates = new HashSet();
  59. /** Map of color spaces (key: color space name) */
  60. protected Map colorSpaces = new HashMap();
  61. /** Map of ICC color spaces (key: ICC profile description) */
  62. protected Map iccColorSpaces = new HashMap();
  63. /**
  64. * create a /Resources object.
  65. *
  66. * @param objnum the object's number
  67. */
  68. public PDFResources(int objnum) {
  69. /* generic creation of object */
  70. super();
  71. setObjectNumber(objnum);
  72. }
  73. /**
  74. * add font object to resources list.
  75. *
  76. * @param font the PDFFont to add
  77. */
  78. public void addFont(PDFFont font) {
  79. this.fonts.put(font.getName(), font);
  80. }
  81. /**
  82. * Add the fonts in the font info to this PDF document's Font Resources.
  83. *
  84. * @param doc PDF document to add fonts to
  85. * @param fontInfo font info object to get font information from
  86. */
  87. public void addFonts(PDFDocument doc, FontInfo fontInfo) {
  88. Map<String, Typeface> usedFonts = fontInfo.getUsedFonts();
  89. for (String f : usedFonts.keySet()) {
  90. Typeface font = 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(PDFColorSpace colorSpace) {
  144. this.colorSpaces.put(new PDFName(colorSpace.getName()), colorSpace);
  145. if (colorSpace instanceof PDFICCBasedColorSpace) {
  146. PDFICCBasedColorSpace icc = (PDFICCBasedColorSpace)colorSpace;
  147. String desc = ColorProfileUtil.getICCProfileDescription(
  148. icc.getICCStream().getICCProfile());
  149. this.iccColorSpaces.put(desc, colorSpace);
  150. }
  151. }
  152. /**
  153. * Returns a ICCBased color space by profile name.
  154. * @param desc the name of the color space
  155. * @return the requested color space or null if it wasn't found
  156. */
  157. public PDFICCBasedColorSpace getICCColorSpaceByProfileName(String desc) {
  158. PDFICCBasedColorSpace cs = (PDFICCBasedColorSpace)this.iccColorSpaces.get(desc);
  159. return cs;
  160. }
  161. /**
  162. * Returns a color space by name.
  163. * @param name the name of the color space
  164. * @return the requested color space or null if it wasn't found
  165. */
  166. public PDFColorSpace getColorSpace(PDFName name) {
  167. PDFColorSpace cs = (PDFColorSpace)this.colorSpaces.get(name);
  168. return cs;
  169. }
  170. @Override
  171. public int output(OutputStream stream) throws IOException {
  172. populateDictionary();
  173. return super.output(stream);
  174. }
  175. private void populateDictionary() {
  176. if (!this.fonts.isEmpty()) {
  177. PDFDictionary dict = new PDFDictionary(this);
  178. /* construct PDF dictionary of font object references */
  179. Iterator fontIterator = this.fonts.keySet().iterator();
  180. while (fontIterator.hasNext()) {
  181. String fontName = (String)fontIterator.next();
  182. dict.put(fontName, (PDFFont)this.fonts.get(fontName));
  183. }
  184. put("Font", dict);
  185. }
  186. if (!this.shadings.isEmpty()) {
  187. PDFDictionary dict = new PDFDictionary(this);
  188. for (Iterator iter = shadings.iterator(); iter.hasNext();) {
  189. PDFShading currentShading = (PDFShading)iter.next();
  190. dict.put(currentShading.getName(), currentShading);
  191. }
  192. put("Shading", dict);
  193. }
  194. if (!this.patterns.isEmpty()) {
  195. PDFDictionary dict = new PDFDictionary(this);
  196. for (Iterator iter = patterns.iterator(); iter.hasNext();) {
  197. PDFPattern currentPattern = (PDFPattern)iter.next();
  198. dict.put(currentPattern.getName(), currentPattern);
  199. }
  200. put("Pattern", dict);
  201. }
  202. PDFArray procset = new PDFArray(this);
  203. procset.add(new PDFName("PDF"));
  204. procset.add(new PDFName("ImageB"));
  205. procset.add(new PDFName("ImageC"));
  206. procset.add(new PDFName("Text"));
  207. put("ProcSet", procset);
  208. if (this.xObjects != null && !this.xObjects.isEmpty()) {
  209. PDFDictionary dict = new PDFDictionary(this);
  210. for (Iterator iter = xObjects.iterator(); iter.hasNext();) {
  211. PDFXObject xobj = (PDFXObject)iter.next();
  212. dict.put(xobj.getName().toString(), xobj);
  213. }
  214. put("XObject", dict);
  215. }
  216. if (!this.gstates.isEmpty()) {
  217. PDFDictionary dict = new PDFDictionary(this);
  218. for (Iterator iter = gstates.iterator(); iter.hasNext();) {
  219. PDFGState gs = (PDFGState)iter.next();
  220. dict.put(gs.getName(), gs);
  221. }
  222. put("ExtGState", dict);
  223. }
  224. if (!this.colorSpaces.isEmpty()) {
  225. PDFDictionary dict = new PDFDictionary(this);
  226. for (Iterator iter = colorSpaces.values().iterator(); iter.hasNext();) {
  227. PDFColorSpace colorSpace = (PDFColorSpace)iter.next();
  228. dict.put(colorSpace.getName(), colorSpace);
  229. }
  230. put("ColorSpace", dict);
  231. }
  232. }
  233. }