1 51 package org.apache.fop.pdf; 52 53 import java.io.UnsupportedEncodingException ; 55 import java.util.Iterator ; 56 import java.util.List ; 57 import java.util.Map ; 58 59 65 public class PDFResources extends PDFObject { 66 67 70 protected Map fonts = new java.util.HashMap (); 71 72 protected List xObjects = null; 73 protected List patterns = new java.util.ArrayList (); 74 protected List shadings = new java.util.ArrayList (); 75 76 81 public PDFResources(int number) { 82 83 84 super(number); 85 86 } 87 88 93 public void addFont(PDFFont font) { 94 this.fonts.put(font.getName(), font); 95 } 96 97 public void addShading(PDFShading theShading) { 98 this.shadings.add(theShading); 99 } 100 101 public void addPattern(PDFPattern thePattern) { 102 this.patterns.add(thePattern); 103 } 104 105 public void setXObjects(List xObjects) { 106 this.xObjects = xObjects; 107 } 108 109 114 public byte[] toPDF() { 115 StringBuffer p = new StringBuffer (this.number + " " + this.generation 116 + " obj\n<< \n"); 117 if (!this.fonts.isEmpty()) { 118 p.append("/Font << "); 119 120 121 Iterator fontIterator = this.fonts.keySet().iterator(); 122 while (fontIterator.hasNext()) { 123 String fontName = (String )fontIterator.next(); 124 p.append("/" + fontName + " " 125 + ((PDFFont)this.fonts.get(fontName)).referencePDF() 126 + " "); 127 } 128 129 p.append(">> \n"); 130 } 131 132 PDFShading currentShading = null; 133 if (!this.shadings.isEmpty()) { 134 p.append("/Shading << "); 135 136 for (int currentShadingNumber = 0; 137 currentShadingNumber < this.shadings.size(); 138 currentShadingNumber++) { 139 currentShading = 140 ((PDFShading)this.shadings.get(currentShadingNumber)); 141 142 p.append("/" + currentShading.getName() + " " 143 + currentShading.referencePDF() + " "); } 145 146 p.append(">> \n"); 147 } 148 currentShading = null; 150 151 PDFPattern currentPattern = null; 152 if (!this.patterns.isEmpty()) { 153 p.append("/Pattern << "); 154 155 for (int currentPatternNumber = 0; 156 currentPatternNumber < this.patterns.size(); 157 currentPatternNumber++) { 158 currentPattern = 159 ((PDFPattern)this.patterns.get(currentPatternNumber)); 160 161 p.append("/" + currentPattern.getName() + " " 162 + currentPattern.referencePDF() + " "); 163 } 164 165 p.append(">> \n"); 166 } 167 currentPattern = null; 169 170 p.append("/ProcSet [ /PDF /ImageC /Text ] "); 171 172 if (!this.xObjects.isEmpty()) { 173 p = p.append("/XObject <<"); 174 for (int i = 1; i <= this.xObjects.size(); i++) { 175 p = p.append("/Im" + i + " " 176 + ((PDFXObject)this.xObjects.get(i - 1)).referencePDF() 177 + " \n"); 178 } 179 p = p.append(" >>\n"); 180 } 181 182 p = p.append(">> \nendobj\n"); 183 184 try { 185 return p.toString().getBytes(PDFDocument.ENCODING); 186 } catch (UnsupportedEncodingException ue) { 187 return p.toString().getBytes(); 188 } 189 } 190 191 } 192 | Popular Tags |