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 21KB

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