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.

PDFShading.java 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001-2002 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.pdf;
  8. // Java...
  9. import java.util.ArrayList;
  10. /**
  11. * class representing a PDF Smooth Shading object.
  12. *
  13. * PDF Functions represent parameterized mathematical formulas and sampled representations with
  14. * arbitrary resolution. Functions are used in two areas: device-dependent
  15. * rasterization information for halftoning and transfer
  16. * functions, and color specification for smooth shading (a PDF 1.3 feature).
  17. *
  18. * All PDF Functions have a shadingType (0,2,3, or 4), a Domain, and a Range.
  19. */
  20. public class PDFShading extends PDFObject {
  21. // Guts common to all function types
  22. /**
  23. * The name of the Shading e.g. "Shading1"
  24. */
  25. protected String shadingName = null;
  26. /**
  27. * Required: The Type of shading (1,2,3,4,5,6,7)
  28. */
  29. protected int shadingType = 3; // Default
  30. /**
  31. * A ColorSpace representing the colorspace. "DeviceRGB" is an example.
  32. */
  33. // protected StringBuffer colorSpace = null;
  34. protected PDFColorSpace colorSpace = null;
  35. /**
  36. * The background color. Since shading is opaque,
  37. * this is very rarely used.
  38. */
  39. protected ArrayList background = null;
  40. /**
  41. * Optional: A ArrayList specifying the clipping rectangle
  42. */
  43. protected ArrayList bBox = null;
  44. /**
  45. * Optional: A flag whether or not to filter the shading function
  46. * to prevent aliasing artifacts. Default is false.
  47. */
  48. protected boolean antiAlias = false;
  49. /**
  50. * Optional for Type 1: Array of four numbers, xmin, xmax, ymin, ymax. Default is [0 1 0 1]
  51. * Optional for Type 2: An array of two numbers between which the blend varies between start and end points. Default is 0, 1.
  52. * Optional for Type 3: An array of two numbers between which the blend varies between start and end points. Default is 0, 1.
  53. */
  54. protected ArrayList domain = null;
  55. /**
  56. * Optional for Type 1: A transformation matrix
  57. */
  58. protected ArrayList matrix = null;
  59. /**
  60. * Required for Type 1, 2, and 3:
  61. * The object of the color mapping function (usually type 2 or 3).
  62. * Optional for Type 4,5,6, and 7: When it's nearly the same thing.
  63. */
  64. protected PDFFunction function = null;
  65. /**
  66. * Required for Type 2: An Array of four numbers specifying the starting and ending coordinate pairs
  67. * Required for Type 3: An Array of six numbers [x0,y0,r0,x1,y1,r1] specifying the centers and radii of
  68. * the starting and ending circles.
  69. */
  70. protected ArrayList coords = null;
  71. /**
  72. * Required for Type 2+3: An Array of two boolean values specifying whether to extend the
  73. * start and end colors past the start and end points,
  74. * respectively. Default is false, false.
  75. */
  76. protected ArrayList extend = null;
  77. /**
  78. * Required for Type 4,5,6, and 7: Specifies the number of bits used to represent each vertex coordinate.
  79. * Allowed to be 1,2,4,8,12,16,24, or 32.
  80. */
  81. protected int bitsPerCoordinate = 0;
  82. /**
  83. * Required for Type 4,5,6, and 7: Specifies the number of bits used to represent the edge flag for each vertex.
  84. * Allowed to be 2,4,or 8, while the Edge flag itself is allowed to be 0,1 or 2.
  85. */
  86. protected int bitsPerFlag = 0;
  87. /**
  88. * Required for Type 4,5,6, and 7: Array of Doubles which specifies how to decode coordinate and color component values.
  89. * Each type has a differing number of decode array members, so check the spec.
  90. * Page 303 in PDF Spec 1.3
  91. */
  92. protected ArrayList decode = null;
  93. /**
  94. * Required for Type 4,5,6, and 7: Specifies the number of bits used to represent each color coordinate.
  95. * Allowed to be 1,2,4,8,12, or 16
  96. */
  97. protected int bitsPerComponent = 0;
  98. /**
  99. * Required for Type 5:The number of vertices in each "row" of the lattice; it must be greater than or equal to 2.
  100. */
  101. protected int verticesPerRow = 0;
  102. /**
  103. * Constructor for type function based shading
  104. *
  105. * @param theNumber The object number of this PDF object
  106. * @param theShadingName The name of the shading pattern. Can be anything
  107. * without spaces. "Shading1" or "Sh1" are good examples.
  108. * @param theShadingType The type of shading object, which should be 1 for function
  109. * based shading.
  110. * @param theColorSpace The colorspace is 'DeviceRGB' or something similar.
  111. * @param theBackground An array of color components appropriate to the
  112. * colorspace key specifying a single color value.
  113. * This key is used by the f operator buy ignored by the sh operator.
  114. * @param theBBox ArrayList of double's representing a rectangle
  115. * in the coordinate space that is current at the
  116. * time of shading is imaged. Temporary clipping
  117. * boundary.
  118. * @param theAntiAlias Whether or not to anti-alias.
  119. * @param theDomain Optional vector of Doubles specifying the domain.
  120. * @param theMatrix ArrayList of Doubles specifying the matrix.
  121. * If it's a pattern, then the matrix maps it to pattern space.
  122. * If it's a shading, then it maps it to current user space.
  123. * It's optional, the default is the identity matrix
  124. * @param theFunction The PDF Function that maps an (x,y) location to a color
  125. */
  126. public PDFShading(int theNumber, String theShadingName,
  127. int theShadingType, PDFColorSpace theColorSpace,
  128. ArrayList theBackground, ArrayList theBBox,
  129. boolean theAntiAlias, ArrayList theDomain,
  130. ArrayList theMatrix, PDFFunction theFunction) {
  131. super(theNumber);
  132. this.shadingName = theShadingName;
  133. this.shadingType = theShadingType; // 1
  134. this.colorSpace = theColorSpace;
  135. this.background = theBackground;
  136. this.bBox = theBBox;
  137. this.antiAlias = theAntiAlias;
  138. this.domain = theDomain;
  139. this.matrix = theMatrix;
  140. this.function = theFunction;
  141. }
  142. /**
  143. * Constructor for Type 2 and 3
  144. *
  145. * @param theNumber The object number of this PDF object.
  146. * @param theShadingName The name of the shading pattern. Can be anything
  147. * without spaces. "Shading1" or "Sh1" are good examples.
  148. * @param theShadingType 2 or 3 for axial or radial shading
  149. * @param theColorSpace "DeviceRGB" or similar.
  150. * @param theBackground theBackground An array of color components appropriate to the
  151. * colorspace key specifying a single color value.
  152. * This key is used by the f operator buy ignored by the sh operator.
  153. * @param theBBox ArrayList of double's representing a rectangle
  154. * in the coordinate space that is current at the
  155. * time of shading is imaged. Temporary clipping
  156. * boundary.
  157. * @param theAntiAlias Default is false
  158. * @param theCoords ArrayList of four (type 2) or 6 (type 3) Double
  159. * @param theDomain ArrayList of Doubles specifying the domain
  160. * @param theFunction the Stitching (PDFfunction type 3) function, even if it's stitching a single function
  161. * @param theExtend ArrayList of Booleans of whether to extend teh start and end colors past the start and end points
  162. * The default is [false, false]
  163. */
  164. public PDFShading(int theNumber, String theShadingName,
  165. int theShadingType, PDFColorSpace theColorSpace,
  166. ArrayList theBackground, ArrayList theBBox,
  167. boolean theAntiAlias, ArrayList theCoords,
  168. ArrayList theDomain, PDFFunction theFunction,
  169. ArrayList theExtend) {
  170. super(theNumber);
  171. this.shadingName = theShadingName;
  172. this.shadingType = theShadingType; // 2 or 3
  173. this.colorSpace = theColorSpace;
  174. this.background = theBackground;
  175. this.bBox = theBBox;
  176. this.antiAlias = theAntiAlias;
  177. this.coords = theCoords;
  178. this.domain = theDomain;
  179. this.function = theFunction;
  180. this.extend = theExtend;
  181. }
  182. /**
  183. * Constructor for Type 4,6, or 7
  184. *
  185. * @param theNumber The object number of this PDF object.
  186. * @param theShadingType 4, 6, or 7 depending on whether it's
  187. * Free-form gouraud-shaded triangle meshes, coons patch meshes,
  188. * or tensor product patch meshes, respectively.
  189. * @param theShadingName The name of the shading pattern. Can be anything
  190. * without spaces. "Shading1" or "Sh1" are good examples.
  191. * @param theColorSpace "DeviceRGB" or similar.
  192. * @param theBackground theBackground An array of color components appropriate to the
  193. * colorspace key specifying a single color value.
  194. * This key is used by the f operator buy ignored by the sh operator.
  195. * @param theBBox ArrayList of double's representing a rectangle
  196. * in the coordinate space that is current at the
  197. * time of shading is imaged. Temporary clipping
  198. * boundary.
  199. * @param theAntiAlias Default is false
  200. * @param theBitsPerCoordinate 1,2,4,8,12,16,24 or 32.
  201. * @param theBitsPerComponent 1,2,4,8,12, and 16
  202. * @param theBitsPerFlag 2,4,8.
  203. * @param theDecode ArrayList of Doubles see PDF 1.3 spec pages 303 to 312.
  204. * @param theFunction the PDFFunction
  205. */
  206. public PDFShading(int theNumber, String theShadingName,
  207. int theShadingType, PDFColorSpace theColorSpace,
  208. ArrayList theBackground, ArrayList theBBox,
  209. boolean theAntiAlias, int theBitsPerCoordinate,
  210. int theBitsPerComponent, int theBitsPerFlag,
  211. ArrayList theDecode, PDFFunction theFunction) {
  212. super(theNumber);
  213. this.shadingType = theShadingType; // 4,6 or 7
  214. this.colorSpace = theColorSpace;
  215. this.background = theBackground;
  216. this.bBox = theBBox;
  217. this.antiAlias = theAntiAlias;
  218. this.bitsPerCoordinate = theBitsPerCoordinate;
  219. this.bitsPerComponent = theBitsPerComponent;
  220. this.bitsPerFlag = theBitsPerFlag;
  221. this.decode = theDecode;
  222. this.function = theFunction;
  223. }
  224. /**
  225. * Constructor for type 5
  226. *
  227. * @param theShadingType 5 for lattice-Form Gouraud shaded-triangle mesh
  228. * @param theShadingName The name of the shading pattern. Can be anything
  229. * without spaces. "Shading1" or "Sh1" are good examples.
  230. * @param theColorSpace "DeviceRGB" or similar.
  231. * @param theBackground theBackground An array of color components appropriate to the
  232. * colorspace key specifying a single color value.
  233. * This key is used by the f operator buy ignored by the sh operator.
  234. * @param theBBox ArrayList of double's representing a rectangle
  235. * in the coordinate space that is current at the
  236. * time of shading is imaged. Temporary clipping
  237. * boundary.
  238. * @param theAntiAlias Default is false
  239. * @param theBitsPerCoordinate 1,2,4,8,12,16, 24, or 32
  240. * @param theBitsPerComponent 1,2,4,8,12,24,32
  241. * @param theDecode ArrayList of Doubles. See page 305 in PDF 1.3 spec.
  242. * @param theVerticesPerRow number of vertices in each "row" of the lattice.
  243. * @param theFunction The PDFFunction that's mapped on to this shape
  244. * @param theNumber the object number of this PDF object.
  245. */
  246. public PDFShading(int theNumber, String theShadingName,
  247. int theShadingType, PDFColorSpace theColorSpace,
  248. ArrayList theBackground, ArrayList theBBox,
  249. boolean theAntiAlias, int theBitsPerCoordinate,
  250. int theBitsPerComponent, ArrayList theDecode,
  251. int theVerticesPerRow, PDFFunction theFunction) {
  252. super(theNumber);
  253. this.shadingName = theShadingName;
  254. this.shadingType = theShadingType; // 5
  255. this.colorSpace = theColorSpace;
  256. this.background = theBackground;
  257. this.bBox = theBBox;
  258. this.antiAlias = theAntiAlias;
  259. this.bitsPerCoordinate = theBitsPerCoordinate;
  260. this.bitsPerComponent = theBitsPerComponent;
  261. this.decode = theDecode;
  262. this.verticesPerRow = theVerticesPerRow;
  263. this.function = theFunction;
  264. }
  265. public String getName() {
  266. return (this.shadingName);
  267. }
  268. /**
  269. * represent as PDF. Whatever the shadingType is, the correct
  270. * representation spits out. The sets of required and optional
  271. * attributes are different for each type, but if a required
  272. * attribute's object was constructed as null, then no error
  273. * is raised. Instead, the malformed PDF that was requested
  274. * by the construction is dutifully output.
  275. * This policy should be reviewed.
  276. *
  277. * @return the PDF string.
  278. */
  279. public byte[] toPDF() {
  280. int vectorSize;
  281. int tempInt;
  282. StringBuffer p = new StringBuffer();
  283. p.append(this.number + " " + this.generation
  284. + " obj\n<< \n/ShadingType " + this.shadingType + " \n");
  285. if (this.colorSpace != null) {
  286. p.append("/ColorSpace /"
  287. + this.colorSpace.getColorSpacePDFString() + " \n");
  288. }
  289. if (this.background != null) {
  290. p.append("/Background [ ");
  291. vectorSize = this.background.size();
  292. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  293. p.append(PDFNumber.doubleOut((Double)this.background.get(tempInt))
  294. + " ");
  295. }
  296. p.append("] \n");
  297. }
  298. if (this.bBox
  299. != null) { // I've never seen an example, so I guess this is right.
  300. p.append("/BBox [ ");
  301. vectorSize = this.bBox.size();
  302. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  303. p.append(PDFNumber.doubleOut((Double)this.bBox.get(tempInt))
  304. + " ");
  305. }
  306. p.append("] \n");
  307. }
  308. if (this.antiAlias) {
  309. p.append("/AntiAlias " + this.antiAlias + " \n");
  310. }
  311. // Here's where we differentiate based on what type it is.
  312. if (this.shadingType == 1) { // function based shading
  313. if (this.domain != null) {
  314. p.append("/Domain [ ");
  315. vectorSize = this.domain.size();
  316. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  317. p.append(PDFNumber.doubleOut((Double)this.domain.get(tempInt))
  318. + " ");
  319. }
  320. p.append("] \n");
  321. } else {
  322. p.append("/Domain [ 0 1 ] \n");
  323. }
  324. if (this.matrix != null) {
  325. p.append("/Matrix [ ");
  326. vectorSize = this.matrix.size();
  327. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  328. p.append(PDFNumber.doubleOut((Double)this.matrix.get(tempInt))
  329. + " ");
  330. }
  331. p.append("] \n");
  332. }
  333. if (this.function != null) {
  334. p.append("/Function ");
  335. p.append(this.function.referencePDF() + " \n");
  336. }
  337. } else if ((this.shadingType == 2)
  338. || (this.shadingType
  339. == 3)) { // 2 is axial shading (linear gradient)
  340. // 3 is radial shading (circular gradient)
  341. if (this.coords != null) {
  342. p.append("/Coords [ ");
  343. vectorSize = this.coords.size();
  344. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  345. p.append(PDFNumber.doubleOut((Double)this.coords.get(tempInt))
  346. + " ");
  347. }
  348. p.append("] \n");
  349. }
  350. // DOMAIN
  351. if (this.domain != null) {
  352. p.append("/Domain [ ");
  353. vectorSize = this.domain.size();
  354. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  355. p.append(PDFNumber.doubleOut((Double)this.domain.get(tempInt))
  356. + " ");
  357. }
  358. p.append("] \n");
  359. } else {
  360. p.append("/Domain [ 0 1 ] \n");
  361. }
  362. if (this.extend != null) {
  363. p.append("/Extend [ ");
  364. vectorSize = this.extend.size();
  365. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  366. p.append(((Boolean)this.extend.get(tempInt)) + " ");
  367. }
  368. p.append("] \n");
  369. } else {
  370. p.append("/Extend [ true true ] \n");
  371. }
  372. if (this.function != null) {
  373. p.append("/Function ");
  374. p.append(this.function.referencePDF() + " \n");
  375. }
  376. } else if ((this.shadingType == 4) || (this.shadingType == 6)
  377. || (this.shadingType
  378. == 7)) { // 4:Free-form Gouraud-shaded triangle meshes
  379. // 6:coons patch meshes
  380. // 7://tensor product patch meshes (which no one ever uses)
  381. if (this.bitsPerCoordinate > 0) {
  382. p.append("/BitsPerCoordinate " + this.bitsPerCoordinate
  383. + " \n");
  384. } else {
  385. p.append("/BitsPerCoordinate 1 \n");
  386. }
  387. if (this.bitsPerComponent > 0) {
  388. p.append("/BitsPerComponent " + this.bitsPerComponent
  389. + " \n");
  390. } else {
  391. p.append("/BitsPerComponent 1 \n");
  392. }
  393. if (this.bitsPerFlag > 0) {
  394. p.append("/BitsPerFlag " + this.bitsPerFlag + " \n");
  395. } else {
  396. p.append("/BitsPerFlag 2 \n");
  397. }
  398. if (this.decode != null) {
  399. p.append("/Decode [ ");
  400. vectorSize = this.decode.size();
  401. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  402. p.append(((Boolean)this.decode.get(tempInt)) + " ");
  403. }
  404. p.append("] \n");
  405. }
  406. if (this.function != null) {
  407. p.append("/Function ");
  408. p.append(this.function.referencePDF() + " \n");
  409. }
  410. } else if (this.shadingType
  411. == 5) { // Lattice Free form gouraud-shaded triangle mesh
  412. if (this.bitsPerCoordinate > 0) {
  413. p.append("/BitsPerCoordinate " + this.bitsPerCoordinate
  414. + " \n");
  415. } else {
  416. p.append("/BitsPerCoordinate 1 \n");
  417. }
  418. if (this.bitsPerComponent > 0) {
  419. p.append("/BitsPerComponent " + this.bitsPerComponent
  420. + " \n");
  421. } else {
  422. p.append("/BitsPerComponent 1 \n");
  423. }
  424. if (this.decode != null) {
  425. p.append("/Decode [ ");
  426. vectorSize = this.decode.size();
  427. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  428. p.append(((Boolean)this.decode.get(tempInt)) + " ");
  429. }
  430. p.append("] \n");
  431. }
  432. if (this.function != null) {
  433. p.append("/Function ");
  434. p.append(this.function.referencePDF() + " \n");
  435. }
  436. if (this.verticesPerRow > 0) {
  437. p.append("/VerticesPerRow " + this.verticesPerRow + " \n");
  438. } else {
  439. p.append("/VerticesPerRow 2 \n");
  440. }
  441. }
  442. p.append(">> \nendobj\n");
  443. return (p.toString().getBytes());
  444. }
  445. public boolean equals(Object obj) {
  446. if (obj == null) {
  447. return false;
  448. }
  449. if (obj == this) {
  450. return true;
  451. }
  452. if (!(obj instanceof PDFShading)) {
  453. return false;
  454. }
  455. PDFShading shad = (PDFShading)obj;
  456. if (shadingType != shad.shadingType) {
  457. return false;
  458. }
  459. if (antiAlias != shad.antiAlias) {
  460. return false;
  461. }
  462. if (bitsPerCoordinate != shad.bitsPerCoordinate) {
  463. return false;
  464. }
  465. if (bitsPerFlag != shad.bitsPerFlag) {
  466. return false;
  467. }
  468. if (bitsPerComponent != shad.bitsPerComponent) {
  469. return false;
  470. }
  471. if (verticesPerRow != shad.verticesPerRow) {
  472. return false;
  473. }
  474. if (colorSpace != null) {
  475. if (!colorSpace.equals(shad.colorSpace)) {
  476. return false;
  477. }
  478. } else if (shad.colorSpace != null) {
  479. return false;
  480. }
  481. if (background != null) {
  482. if (!background.equals(shad.background)) {
  483. return false;
  484. }
  485. } else if (shad.background != null) {
  486. return false;
  487. }
  488. if (bBox != null) {
  489. if (!bBox.equals(shad.bBox)) {
  490. return false;
  491. }
  492. } else if (shad.bBox != null) {
  493. return false;
  494. }
  495. if (domain != null) {
  496. if (!domain.equals(shad.domain)) {
  497. return false;
  498. }
  499. } else if (shad.domain != null) {
  500. return false;
  501. }
  502. if (matrix != null) {
  503. if (!matrix.equals(shad.matrix)) {
  504. return false;
  505. }
  506. } else if (shad.matrix != null) {
  507. return false;
  508. }
  509. if (coords != null) {
  510. if (!coords.equals(shad.coords)) {
  511. return false;
  512. }
  513. } else if (shad.coords != null) {
  514. return false;
  515. }
  516. if (extend != null) {
  517. if (!extend.equals(shad.extend)) {
  518. return false;
  519. }
  520. } else if (shad.extend != null) {
  521. return false;
  522. }
  523. if (decode != null) {
  524. if (!decode.equals(shad.decode)) {
  525. return false;
  526. }
  527. } else if (shad.decode != null) {
  528. return false;
  529. }
  530. if (function != null) {
  531. if (!function.equals(shad.function)) {
  532. return false;
  533. }
  534. } else if (shad.function != null) {
  535. return false;
  536. }
  537. return true;
  538. }
  539. }