1 47 package com.lowagie.text.pdf; 48 49 import java.awt.Color ; 50 import java.io.IOException ; 51 55 public class PdfShading { 56 57 protected PdfDictionary shading; 58 59 protected PdfWriter writer; 60 61 protected int shadingType; 62 63 protected ColorDetails colorDetails; 64 65 protected PdfName shadingName; 66 67 protected PdfIndirectReference shadingReference; 68 69 private Color cspace; 70 71 72 protected float[] bBox; 73 74 75 protected boolean antiAlias = false; 76 77 78 protected PdfShading(PdfWriter writer) { 79 this.writer = writer; 80 } 81 82 protected void setColorSpace(Color color) { 83 cspace = color; 84 int type = ExtendedColor.getType(color); 85 PdfObject colorSpace = null; 86 switch (type) { 87 case ExtendedColor.TYPE_GRAY: { 88 colorSpace = PdfName.DEVICEGRAY; 89 break; 90 } 91 case ExtendedColor.TYPE_CMYK: { 92 colorSpace = PdfName.DEVICECMYK; 93 break; 94 } 95 case ExtendedColor.TYPE_SEPARATION: { 96 SpotColor spot = (SpotColor)color; 97 colorDetails = writer.addSimple(spot.getPdfSpotColor()); 98 colorSpace = colorDetails.getIndirectReference(); 99 break; 100 } 101 case ExtendedColor.TYPE_PATTERN: 102 case ExtendedColor.TYPE_SHADING: { 103 throwColorSpaceError(); 104 } 105 default: 106 colorSpace = PdfName.DEVICERGB; 107 break; 108 } 109 shading.put(PdfName.COLORSPACE, colorSpace); 110 } 111 112 public Color getColorSpace() { 113 return cspace; 114 } 115 116 public static void throwColorSpaceError() { 117 throw new IllegalArgumentException ("A tiling or shading pattern cannot be used as a color space in a shading pattern"); 118 } 119 120 public static void checkCompatibleColors(Color c1, Color c2) { 121 int type1 = ExtendedColor.getType(c1); 122 int type2 = ExtendedColor.getType(c2); 123 if (type1 != type2) 124 throw new IllegalArgumentException ("Both colors must be of the same type."); 125 if (type1 == ExtendedColor.TYPE_SEPARATION && ((SpotColor)c1).getPdfSpotColor() != ((SpotColor)c2).getPdfSpotColor()) 126 throw new IllegalArgumentException ("The spot color must be the same, only the tint can vary."); 127 if (type1 == ExtendedColor.TYPE_PATTERN || type1 == ExtendedColor.TYPE_SHADING) 128 throwColorSpaceError(); 129 } 130 131 public static float[] getColorArray(Color color) { 132 int type = ExtendedColor.getType(color); 133 switch (type) { 134 case ExtendedColor.TYPE_GRAY: { 135 return new float[]{((GrayColor)color).getGray()}; 136 } 137 case ExtendedColor.TYPE_CMYK: { 138 CMYKColor cmyk = (CMYKColor)color; 139 return new float[]{cmyk.getCyan(), cmyk.getMagenta(), cmyk.getYellow(), cmyk.getBlack()}; 140 } 141 case ExtendedColor.TYPE_SEPARATION: { 142 return new float[]{((SpotColor)color).getTint()}; 143 } 144 case ExtendedColor.TYPE_RGB: { 145 return new float[]{color.getRed() / 255f, color.getGreen() / 255f, color.getBlue() / 255f}; 146 } 147 } 148 throwColorSpaceError(); 149 return null; 150 } 151 152 public static PdfShading type1(PdfWriter writer, Color colorSpace, float domain[], float tMatrix[], PdfFunction function) { 153 PdfShading sp = new PdfShading(writer); 154 sp.shading = new PdfDictionary(); 155 sp.shadingType = 1; 156 sp.shading.put(PdfName.SHADINGTYPE, new PdfNumber(sp.shadingType)); 157 sp.setColorSpace(colorSpace); 158 if (domain != null) 159 sp.shading.put(PdfName.DOMAIN, new PdfArray(domain)); 160 if (tMatrix != null) 161 sp.shading.put(PdfName.MATRIX, new PdfArray(tMatrix)); 162 sp.shading.put(PdfName.FUNCTION, function.getReference()); 163 return sp; 164 } 165 166 public static PdfShading type2(PdfWriter writer, Color colorSpace, float coords[], float domain[], PdfFunction function, boolean extend[]) { 167 PdfShading sp = new PdfShading(writer); 168 sp.shading = new PdfDictionary(); 169 sp.shadingType = 2; 170 sp.shading.put(PdfName.SHADINGTYPE, new PdfNumber(sp.shadingType)); 171 sp.setColorSpace(colorSpace); 172 sp.shading.put(PdfName.COORDS, new PdfArray(coords)); 173 if (domain != null) 174 sp.shading.put(PdfName.DOMAIN, new PdfArray(domain)); 175 sp.shading.put(PdfName.FUNCTION, function.getReference()); 176 if (extend != null && (extend[0] || extend[1])) { 177 PdfArray array = new PdfArray(extend[0] ? PdfBoolean.PDFTRUE : PdfBoolean.PDFFALSE); 178 array.add(extend[1] ? PdfBoolean.PDFTRUE : PdfBoolean.PDFFALSE); 179 sp.shading.put(PdfName.EXTEND, array); 180 } 181 return sp; 182 } 183 184 public static PdfShading type3(PdfWriter writer, Color colorSpace, float coords[], float domain[], PdfFunction function, boolean extend[]) { 185 PdfShading sp = type2(writer, colorSpace, coords, domain, function, extend); 186 sp.shadingType = 3; 187 sp.shading.put(PdfName.SHADINGTYPE, new PdfNumber(sp.shadingType)); 188 return sp; 189 } 190 191 public static PdfShading simpleAxial(PdfWriter writer, float x0, float y0, float x1, float y1, Color startColor, Color endColor, boolean extendStart, boolean extendEnd) { 192 checkCompatibleColors(startColor, endColor); 193 PdfFunction function = PdfFunction.type2(writer, new float[]{0, 1}, null, getColorArray(startColor), 194 getColorArray(endColor), 1); 195 return type2(writer, startColor, new float[]{x0, y0, x1, y1}, null, function, new boolean[]{extendStart, extendEnd}); 196 } 197 198 public static PdfShading simpleAxial(PdfWriter writer, float x0, float y0, float x1, float y1, Color startColor, Color endColor) { 199 return simpleAxial(writer, x0, y0, x1, y1, startColor, endColor, true, true); 200 } 201 202 public static PdfShading simpleRadial(PdfWriter writer, float x0, float y0, float r0, float x1, float y1, float r1, Color startColor, Color endColor, boolean extendStart, boolean extendEnd) { 203 checkCompatibleColors(startColor, endColor); 204 PdfFunction function = PdfFunction.type2(writer, new float[]{0, 1}, null, getColorArray(startColor), 205 getColorArray(endColor), 1); 206 return type3(writer, startColor, new float[]{x0, y0, r0, x1, y1, r1}, null, function, new boolean[]{extendStart, extendEnd}); 207 } 208 209 public static PdfShading simpleRadial(PdfWriter writer, float x0, float y0, float r0, float x1, float y1, float r1, Color startColor, Color endColor) { 210 return simpleRadial(writer, x0, y0, r0, x1, y1, r1, startColor, endColor, true, true); 211 } 212 213 PdfName getShadingName() { 214 return shadingName; 215 } 216 217 PdfIndirectReference getShadingReference() { 218 if (shadingReference == null) 219 shadingReference = writer.getPdfIndirectReference(); 220 return shadingReference; 221 } 222 223 void setName(int number) { 224 shadingName = new PdfName("Sh" + number); 225 } 226 227 void addToBody() throws IOException { 228 if (bBox != null) 229 shading.put(PdfName.BBOX, new PdfArray(bBox)); 230 if (antiAlias) 231 shading.put(PdfName.ANTIALIAS, PdfBoolean.PDFTRUE); 232 writer.addToBody(shading, getShadingReference()); 233 } 234 235 PdfWriter getWriter() { 236 return writer; 237 } 238 239 ColorDetails getColorDetails() { 240 return colorDetails; 241 } 242 243 public float[] getBBox() { 244 return bBox; 245 } 246 247 public void setBBox(float[] bBox) { 248 if (bBox.length != 4) 249 throw new IllegalArgumentException ("BBox must be a 4 element array."); 250 this.bBox = bBox; 251 } 252 253 public boolean isAntiAlias() { 254 return antiAlias; 255 } 256 257 public void setAntiAlias(boolean antiAlias) { 258 this.antiAlias = antiAlias; 259 } 260 261 } 262 | Popular Tags |