Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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