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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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, patterns,
  35. * shadings, etc., for the document.
  36. */
  37. public class PDFResources extends PDFDictionary {
  38. /**
  39. * /Font objects keyed by their internal name
  40. */
  41. protected Map<String, PDFDictionary> fonts = new LinkedHashMap<String, PDFDictionary>();
  42. /**
  43. * Set of XObjects
  44. */
  45. protected Set<PDFXObject> xObjects = new LinkedHashSet<PDFXObject>();
  46. /** Map of color spaces (key: color space name) */
  47. protected Map<LazyName, PDFColorSpace> colorSpaces = new LinkedHashMap<LazyName, PDFColorSpace>();
  48. /** Map of ICC color spaces (key: ICC profile description) */
  49. protected Map<String, PDFICCBasedColorSpace> iccColorSpaces = new LinkedHashMap<String, PDFICCBasedColorSpace>();
  50. private PDFResources parent;
  51. private PDFDictionary fontsObj;
  52. private Map<String, PDFDictionary> fontsObjDict = new LinkedHashMap<String, PDFDictionary>();
  53. /** Named properties */
  54. protected Map<String, PDFReference> properties = new LinkedHashMap<String, PDFReference>();
  55. protected Set<PDFResourceContext> contexts = new LinkedHashSet<PDFResourceContext>();
  56. /**
  57. * create a /Resources object.
  58. */
  59. public PDFResources(PDFDocument doc) {
  60. /* generic creation of object */
  61. super();
  62. setObjectNumber(doc);
  63. }
  64. public void addContext(PDFResourceContext c) {
  65. contexts.add(c);
  66. }
  67. public void setParentResources(PDFResources p) {
  68. parent = p;
  69. }
  70. public PDFResources getParentResources() {
  71. return parent;
  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. addFont(font.getName(), font);
  80. }
  81. public void addFont(String name, PDFDictionary font) {
  82. if (fontsObj != null) {
  83. fontsObj.put(name, font);
  84. fontsObjDict.put(name, font);
  85. } else {
  86. fonts.put(name, font);
  87. }
  88. }
  89. public void createFontsAsObj() {
  90. fontsObj = new PDFDictionary();
  91. getDocument().registerTrailerObject(fontsObj);
  92. put("Font", fontsObj);
  93. }
  94. /**
  95. * Add the fonts in the font info to this PDF document's Font Resources.
  96. *
  97. * @param doc PDF document to add fonts to
  98. * @param fontInfo font info object to get font information from
  99. */
  100. public void addFonts(PDFDocument doc, FontInfo fontInfo) {
  101. Map<String, Typeface> usedFonts = fontInfo.getUsedFonts();
  102. for (Map.Entry<String, Typeface> e : usedFonts.entrySet()) {
  103. String f = e.getKey();
  104. Typeface font = e.getValue();
  105. //Check if the font actually had any mapping operations. If not, it is an indication
  106. //that it has never actually been used and therefore doesn't have to be embedded.
  107. if (font.hadMappingOperations()) {
  108. FontDescriptor desc = null;
  109. if (font instanceof FontDescriptor) {
  110. desc = (FontDescriptor)font;
  111. }
  112. String encoding = font.getEncodingName();
  113. if (font instanceof Symbol || font instanceof ZapfDingbats) {
  114. encoding = null; //Symbolic fonts shouldn't specify an encoding value in PDF
  115. }
  116. addFont(doc.getFactory().makeFont(
  117. f, font.getEmbedFontName(), encoding, font, desc));
  118. }
  119. }
  120. }
  121. /**
  122. * Add an XObject to the resources.
  123. *
  124. * @param xObject the XObject to add
  125. */
  126. public void addXObject(PDFXObject xObject) {
  127. this.xObjects.add(xObject);
  128. }
  129. /**
  130. * Add a ColorSpace dictionary to the resources.
  131. * @param colorSpace the color space
  132. */
  133. public void addColorSpace(PDFColorSpace colorSpace) {
  134. this.colorSpaces.put(new LazyName(colorSpace), colorSpace);
  135. if (colorSpace instanceof PDFICCBasedColorSpace) {
  136. PDFICCBasedColorSpace icc = (PDFICCBasedColorSpace)colorSpace;
  137. String desc = ColorProfileUtil.getICCProfileDescription(
  138. icc.getICCStream().getICCProfile());
  139. this.iccColorSpaces.put(desc, icc);
  140. }
  141. }
  142. static class LazyName {
  143. private PDFColorSpace colorSpace;
  144. public LazyName(PDFColorSpace colorSpace) {
  145. this.colorSpace = colorSpace;
  146. }
  147. public PDFName getName() {
  148. return new PDFName(colorSpace.getName());
  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. for (Map.Entry<LazyName, PDFColorSpace> x : colorSpaces.entrySet()) {
  167. if (x.getKey().getName().equals(name)) {
  168. return x.getValue();
  169. }
  170. }
  171. return null;
  172. }
  173. /**
  174. * Add a named property.
  175. *
  176. * @param name name of property
  177. * @param property reference to property value
  178. */
  179. public void addProperty(String name, PDFReference property) {
  180. this.properties.put(name, property);
  181. }
  182. /**
  183. * Get a named property.
  184. *
  185. * @param name name of property
  186. */
  187. public PDFReference getProperty(String name) {
  188. return this.properties.get(name);
  189. }
  190. @Override
  191. public int output(OutputStream stream) throws IOException {
  192. populateDictionary();
  193. return super.output(stream);
  194. }
  195. private void populateDictionary() {
  196. if (parent != null && parent.fontsObj != null) {
  197. put("Font", parent.fontsObj);
  198. }
  199. if (!this.fonts.isEmpty() || (parent != null && !parent.fonts.isEmpty())) {
  200. PDFDictionary dict = new PDFDictionary(this);
  201. /* construct PDF dictionary of font object references */
  202. for (Map.Entry<String, PDFDictionary> entry : fonts.entrySet()) {
  203. dict.put(entry.getKey(), entry.getValue());
  204. }
  205. if (parent != null) {
  206. for (Map.Entry<String, PDFDictionary> entry : parent.fonts.entrySet()) {
  207. dict.put(entry.getKey(), entry.getValue());
  208. }
  209. for (Map.Entry<String, PDFDictionary> entry : parent.fontsObjDict.entrySet()) {
  210. dict.put(entry.getKey(), entry.getValue());
  211. }
  212. }
  213. put("Font", dict);
  214. }
  215. Set<PDFPattern> patterns = new LinkedHashSet<PDFPattern>();
  216. Set<PDFShading> shadings = new LinkedHashSet<PDFShading>();
  217. Set<PDFGState> gstates = new LinkedHashSet<PDFGState>();
  218. for (PDFResourceContext c : contexts) {
  219. xObjects.addAll(c.getXObjects());
  220. patterns.addAll(c.getPatterns());
  221. shadings.addAll(c.getShadings());
  222. gstates.addAll(c.getGStates());
  223. }
  224. if (parent != null) {
  225. xObjects.addAll(parent.xObjects);
  226. for (PDFResourceContext c : parent.contexts) {
  227. patterns.addAll(c.getPatterns());
  228. shadings.addAll(c.getShadings());
  229. gstates.addAll(c.getGStates());
  230. }
  231. }
  232. if (!shadings.isEmpty()) {
  233. PDFDictionary dict = (PDFDictionary) get("Shading");
  234. if (dict == null) {
  235. dict = new PDFDictionary(this);
  236. }
  237. for (PDFShading shading : shadings) {
  238. dict.put(shading.getName(), shading);
  239. }
  240. put("Shading", dict);
  241. }
  242. if (!patterns.isEmpty()) {
  243. PDFDictionary dict = (PDFDictionary) get("Pattern");
  244. if (dict == null) {
  245. dict = new PDFDictionary(this);
  246. }
  247. for (PDFPattern pattern : patterns) {
  248. dict.put(pattern.getName(), pattern);
  249. }
  250. put("Pattern", dict);
  251. }
  252. PDFArray procset = new PDFArray(this);
  253. procset.add(new PDFName("PDF"));
  254. procset.add(new PDFName("ImageB"));
  255. procset.add(new PDFName("ImageC"));
  256. procset.add(new PDFName("Text"));
  257. put("ProcSet", procset);
  258. if (!xObjects.isEmpty()) {
  259. PDFDictionary dict = (PDFDictionary) get("XObject");
  260. if (dict == null) {
  261. dict = new PDFDictionary(this);
  262. }
  263. for (PDFXObject xObject : xObjects) {
  264. dict.put(xObject.getName().toString(), xObject);
  265. }
  266. put("XObject", dict);
  267. }
  268. if (!gstates.isEmpty()) {
  269. PDFDictionary dict = (PDFDictionary) get("ExtGState");
  270. if (dict == null) {
  271. dict = new PDFDictionary(this);
  272. }
  273. for (PDFGState gstate : gstates) {
  274. dict.put(gstate.getName(), gstate);
  275. }
  276. put("ExtGState", dict);
  277. }
  278. if (!this.colorSpaces.isEmpty() || (parent != null && !parent.colorSpaces.isEmpty())) {
  279. PDFDictionary dict = (PDFDictionary)this.get("ColorSpace");
  280. if (dict == null) {
  281. dict = new PDFDictionary(this);
  282. }
  283. if (parent != null) {
  284. for (PDFColorSpace colorSpace : parent.colorSpaces.values()) {
  285. dict.put(colorSpace.getName(), colorSpace);
  286. }
  287. }
  288. for (PDFColorSpace colorSpace : colorSpaces.values()) {
  289. dict.put(colorSpace.getName(), colorSpace);
  290. }
  291. put("ColorSpace", dict);
  292. }
  293. if (!properties.isEmpty()) {
  294. PDFDictionary dict = new PDFDictionary(this);
  295. for (Map.Entry<String, PDFReference> stringPDFReferenceEntry : properties.entrySet()) {
  296. dict.put(stringPDFReferenceEntry.getKey(), stringPDFReferenceEntry.getValue());
  297. }
  298. put("Properties", dict);
  299. }
  300. }
  301. @Override
  302. public void getChildren(Set<PDFObject> children) {
  303. getChildren(children, false);
  304. }
  305. private void getChildren(Set<PDFObject> children, boolean isParent) {
  306. super.getChildren(children);
  307. for (PDFDictionary f : fonts.values()) {
  308. children.add(f);
  309. f.getChildren(children);
  310. }
  311. for (PDFResourceContext c : contexts) {
  312. for (PDFXObject x : c.getXObjects()) {
  313. children.add(x);
  314. x.getChildren(children);
  315. }
  316. for (PDFPattern x : c.getPatterns()) {
  317. children.add(x);
  318. x.getChildren(children);
  319. }
  320. for (PDFShading x : c.getShadings()) {
  321. children.add(x);
  322. x.getChildren(children);
  323. }
  324. for (PDFGState x : c.getGStates()) {
  325. children.add(x);
  326. x.getChildren(children);
  327. }
  328. }
  329. if (!isParent) {
  330. for (PDFColorSpace x : colorSpaces.values()) {
  331. children.add((PDFObject)x);
  332. ((PDFObject)x).getChildren(children);
  333. }
  334. }
  335. if (parent != null) {
  336. parent.getChildren(children, true);
  337. }
  338. }
  339. }