Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

PDFResources.java 8.1KB

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