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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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(getObjectID()
  311. + "<< \n/ShadingType " + this.shadingType + " \n");
  312. if (this.colorSpace != null) {
  313. p.append("/ColorSpace /"
  314. + this.colorSpace.getName() + " \n");
  315. }
  316. if (this.background != null) {
  317. p.append("/Background [ ");
  318. vectorSize = this.background.size();
  319. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  320. p.append(PDFNumber.doubleOut((Double)this.background.get(tempInt))
  321. + " ");
  322. }
  323. p.append("] \n");
  324. }
  325. if (this.bBox
  326. != null) { // I've never seen an example, so I guess this is right.
  327. p.append("/BBox [ ");
  328. vectorSize = this.bBox.size();
  329. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  330. p.append(PDFNumber.doubleOut((Double)this.bBox.get(tempInt))
  331. + " ");
  332. }
  333. p.append("] \n");
  334. }
  335. if (this.antiAlias) {
  336. p.append("/AntiAlias " + this.antiAlias + " \n");
  337. }
  338. // Here's where we differentiate based on what type it is.
  339. if (this.shadingType == 1) { // function based shading
  340. if (this.domain != null) {
  341. p.append("/Domain [ ");
  342. vectorSize = this.domain.size();
  343. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  344. p.append(PDFNumber.doubleOut((Double)this.domain.get(tempInt))
  345. + " ");
  346. }
  347. p.append("] \n");
  348. } else {
  349. p.append("/Domain [ 0 1 ] \n");
  350. }
  351. if (this.matrix != null) {
  352. p.append("/Matrix [ ");
  353. vectorSize = this.matrix.size();
  354. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  355. p.append(PDFNumber.doubleOut((Double)this.matrix.get(tempInt))
  356. + " ");
  357. }
  358. p.append("] \n");
  359. }
  360. if (this.function != null) {
  361. p.append("/Function ");
  362. p.append(this.function.referencePDF() + " \n");
  363. }
  364. } else if ((this.shadingType == 2)
  365. || (this.shadingType
  366. == 3)) { // 2 is axial shading (linear gradient)
  367. // 3 is radial shading (circular gradient)
  368. if (this.coords != null) {
  369. p.append("/Coords [ ");
  370. vectorSize = this.coords.size();
  371. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  372. p.append(PDFNumber.doubleOut((Double)this.coords.get(tempInt))
  373. + " ");
  374. }
  375. p.append("] \n");
  376. }
  377. // DOMAIN
  378. if (this.domain != null) {
  379. p.append("/Domain [ ");
  380. vectorSize = this.domain.size();
  381. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  382. p.append(PDFNumber.doubleOut((Double)this.domain.get(tempInt))
  383. + " ");
  384. }
  385. p.append("] \n");
  386. } else {
  387. p.append("/Domain [ 0 1 ] \n");
  388. }
  389. if (this.extend != null) {
  390. p.append("/Extend [ ");
  391. vectorSize = this.extend.size();
  392. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  393. p.append(((Boolean)this.extend.get(tempInt)) + " ");
  394. }
  395. p.append("] \n");
  396. } else {
  397. p.append("/Extend [ true true ] \n");
  398. }
  399. if (this.function != null) {
  400. p.append("/Function ");
  401. p.append(this.function.referencePDF() + " \n");
  402. }
  403. } else if ((this.shadingType == 4) || (this.shadingType == 6)
  404. || (this.shadingType
  405. == 7)) { // 4:Free-form Gouraud-shaded triangle meshes
  406. // 6:coons patch meshes
  407. // 7://tensor product patch meshes (which no one ever uses)
  408. if (this.bitsPerCoordinate > 0) {
  409. p.append("/BitsPerCoordinate " + this.bitsPerCoordinate
  410. + " \n");
  411. } else {
  412. p.append("/BitsPerCoordinate 1 \n");
  413. }
  414. if (this.bitsPerComponent > 0) {
  415. p.append("/BitsPerComponent " + this.bitsPerComponent
  416. + " \n");
  417. } else {
  418. p.append("/BitsPerComponent 1 \n");
  419. }
  420. if (this.bitsPerFlag > 0) {
  421. p.append("/BitsPerFlag " + this.bitsPerFlag + " \n");
  422. } else {
  423. p.append("/BitsPerFlag 2 \n");
  424. }
  425. if (this.decode != null) {
  426. p.append("/Decode [ ");
  427. vectorSize = this.decode.size();
  428. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  429. p.append(((Boolean)this.decode.get(tempInt)) + " ");
  430. }
  431. p.append("] \n");
  432. }
  433. if (this.function != null) {
  434. p.append("/Function ");
  435. p.append(this.function.referencePDF() + " \n");
  436. }
  437. } else if (this.shadingType
  438. == 5) { // Lattice Free form gouraud-shaded triangle mesh
  439. if (this.bitsPerCoordinate > 0) {
  440. p.append("/BitsPerCoordinate " + this.bitsPerCoordinate
  441. + " \n");
  442. } else {
  443. p.append("/BitsPerCoordinate 1 \n");
  444. }
  445. if (this.bitsPerComponent > 0) {
  446. p.append("/BitsPerComponent " + this.bitsPerComponent
  447. + " \n");
  448. } else {
  449. p.append("/BitsPerComponent 1 \n");
  450. }
  451. if (this.decode != null) {
  452. p.append("/Decode [ ");
  453. vectorSize = this.decode.size();
  454. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  455. p.append(((Boolean)this.decode.get(tempInt)) + " ");
  456. }
  457. p.append("] \n");
  458. }
  459. if (this.function != null) {
  460. p.append("/Function ");
  461. p.append(this.function.referencePDF() + " \n");
  462. }
  463. if (this.verticesPerRow > 0) {
  464. p.append("/VerticesPerRow " + this.verticesPerRow + " \n");
  465. } else {
  466. p.append("/VerticesPerRow 2 \n");
  467. }
  468. }
  469. p.append(">> \nendobj\n");
  470. return (p.toString());
  471. }
  472. /** {@inheritDoc} */
  473. protected boolean contentEquals(PDFObject obj) {
  474. if (obj == null) {
  475. return false;
  476. }
  477. if (obj == this) {
  478. return true;
  479. }
  480. if (!(obj instanceof PDFShading)) {
  481. return false;
  482. }
  483. PDFShading shad = (PDFShading)obj;
  484. if (shadingType != shad.shadingType) {
  485. return false;
  486. }
  487. if (antiAlias != shad.antiAlias) {
  488. return false;
  489. }
  490. if (bitsPerCoordinate != shad.bitsPerCoordinate) {
  491. return false;
  492. }
  493. if (bitsPerFlag != shad.bitsPerFlag) {
  494. return false;
  495. }
  496. if (bitsPerComponent != shad.bitsPerComponent) {
  497. return false;
  498. }
  499. if (verticesPerRow != shad.verticesPerRow) {
  500. return false;
  501. }
  502. if (colorSpace != null) {
  503. if (!colorSpace.equals(shad.colorSpace)) {
  504. return false;
  505. }
  506. } else if (shad.colorSpace != null) {
  507. return false;
  508. }
  509. if (background != null) {
  510. if (!background.equals(shad.background)) {
  511. return false;
  512. }
  513. } else if (shad.background != null) {
  514. return false;
  515. }
  516. if (bBox != null) {
  517. if (!bBox.equals(shad.bBox)) {
  518. return false;
  519. }
  520. } else if (shad.bBox != null) {
  521. return false;
  522. }
  523. if (domain != null) {
  524. if (!domain.equals(shad.domain)) {
  525. return false;
  526. }
  527. } else if (shad.domain != null) {
  528. return false;
  529. }
  530. if (matrix != null) {
  531. if (!matrix.equals(shad.matrix)) {
  532. return false;
  533. }
  534. } else if (shad.matrix != null) {
  535. return false;
  536. }
  537. if (coords != null) {
  538. if (!coords.equals(shad.coords)) {
  539. return false;
  540. }
  541. } else if (shad.coords != null) {
  542. return false;
  543. }
  544. if (extend != null) {
  545. if (!extend.equals(shad.extend)) {
  546. return false;
  547. }
  548. } else if (shad.extend != null) {
  549. return false;
  550. }
  551. if (decode != null) {
  552. if (!decode.equals(shad.decode)) {
  553. return false;
  554. }
  555. } else if (shad.decode != null) {
  556. return false;
  557. }
  558. if (function != null) {
  559. if (!function.equals(shad.function)) {
  560. return false;
  561. }
  562. } else if (shad.function != null) {
  563. return false;
  564. }
  565. return true;
  566. }
  567. }