Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

PDFResources.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * $Id: PDFResources.java,v 1.17 2003/03/07 08:25:47 jeremias Exp $
  3. * ============================================================================
  4. * The Apache Software License, Version 1.1
  5. * ============================================================================
  6. *
  7. * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modifica-
  10. * tion, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if any, must
  20. * include the following acknowledgment: "This product includes software
  21. * developed by the Apache Software Foundation (http://www.apache.org/)."
  22. * Alternately, this acknowledgment may appear in the software itself, if
  23. * and wherever such third-party acknowledgments normally appear.
  24. *
  25. * 4. The names "FOP" and "Apache Software Foundation" must not be used to
  26. * endorse or promote products derived from this software without prior
  27. * written permission. For written permission, please contact
  28. * apache@apache.org.
  29. *
  30. * 5. Products derived from this software may not be called "Apache", nor may
  31. * "Apache" appear in their name, without prior written permission of the
  32. * Apache Software Foundation.
  33. *
  34. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  36. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  37. * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  38. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  39. * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  40. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  41. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  43. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. * ============================================================================
  45. *
  46. * This software consists of voluntary contributions made by many individuals
  47. * on behalf of the Apache Software Foundation and was originally created by
  48. * James Tauber <jtauber@jtauber.com>. For more information on the Apache
  49. * Software Foundation, please see <http://www.apache.org/>.
  50. */
  51. package org.apache.fop.pdf;
  52. import org.apache.fop.apps.Document;
  53. import org.apache.fop.fonts.Typeface;
  54. import org.apache.fop.fonts.FontDescriptor;
  55. // Java
  56. import java.util.Iterator;
  57. import java.util.Map;
  58. import java.util.Set;
  59. import java.util.HashMap;
  60. import java.util.HashSet;
  61. /**
  62. * class representing a /Resources object.
  63. *
  64. * /Resources object contain a list of references to the fonts for the
  65. * document
  66. */
  67. public class PDFResources extends PDFObject {
  68. /**
  69. * /Font objects keyed by their internal name
  70. */
  71. protected Map fonts = new HashMap();
  72. /**
  73. * Set of XObjects
  74. */
  75. protected Set xObjects = new HashSet();
  76. /**
  77. * Set of patterns
  78. */
  79. protected Set patterns = new HashSet();
  80. /**
  81. * Set of shadings
  82. */
  83. protected Set shadings = new HashSet();
  84. /**
  85. * Set of ExtGStates
  86. */
  87. protected Set gstates = new HashSet();
  88. /**
  89. * create a /Resources object.
  90. *
  91. * @param objnum the object's number
  92. */
  93. public PDFResources(int objnum) {
  94. /* generic creation of object */
  95. super();
  96. setObjectNumber(objnum);
  97. }
  98. /**
  99. * add font object to resources list.
  100. *
  101. * @param font the PDFFont to add
  102. */
  103. public void addFont(PDFFont font) {
  104. this.fonts.put(font.getName(), font);
  105. }
  106. /**
  107. * Add the fonts in the font info to this PDF document's Font Resources.
  108. *
  109. * @param doc PDF document to add fonts to
  110. * @param fontInfo font info object to get font information from
  111. */
  112. public void addFonts(PDFDocument doc, Document fontInfo) {
  113. Map fonts = fontInfo.getUsedFonts();
  114. Iterator e = fonts.keySet().iterator();
  115. while (e.hasNext()) {
  116. String f = (String)e.next();
  117. Typeface font = (Typeface)fonts.get(f);
  118. FontDescriptor desc = null;
  119. if (font instanceof FontDescriptor) {
  120. desc = (FontDescriptor)font;
  121. }
  122. addFont(doc.getFactory().makeFont(
  123. f, font.getFontName(), font.getEncoding(), font, desc));
  124. }
  125. }
  126. /**
  127. * Add a PDFGState to the resources.
  128. *
  129. * @param gs the PDFGState to add
  130. */
  131. public void addGState(PDFGState gs) {
  132. this.gstates.add(gs);
  133. }
  134. /**
  135. * Add a Shading to the resources.
  136. *
  137. * @param theShading the shading to add
  138. */
  139. public void addShading(PDFShading theShading) {
  140. this.shadings.add(theShading);
  141. }
  142. /**
  143. * Add the pattern to the resources.
  144. *
  145. * @param thePattern the pattern to add
  146. */
  147. public void addPattern(PDFPattern thePattern) {
  148. this.patterns.add(thePattern);
  149. }
  150. /**
  151. * Add an XObject to the resources.
  152. *
  153. * @param xObject the XObject to add
  154. */
  155. public void addXObject(PDFXObject xObject) {
  156. this.xObjects.add(xObject);
  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. * @see org.apache.fop.pdf.PDFObject#toPDFString()
  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(" /Im" + xobj.getXNumber() + " "
  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. p = p.append(">>\nendobj\n");
  227. return p.toString();
  228. }
  229. }