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

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