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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. import java.util.List;
  20. import org.apache.fop.render.gradient.GradientMaker;
  21. import org.apache.fop.render.gradient.GradientMaker.DoubleFormatter;
  22. import org.apache.fop.render.gradient.Shading;
  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 {
  34. // Guts common to all function types
  35. /**
  36. * The name of the Shading e.g. "Shading1"
  37. */
  38. protected String shadingName;
  39. private final Shading shading;
  40. private final PDFFunction pdfFunction;
  41. /**
  42. * Constructor for Type 2 and 3
  43. *
  44. * @param shadingType 2 or 3 for axial or radial shading
  45. * @param colorSpace "DeviceRGB" or similar.
  46. * @param coords List of four (type 2) or 6 (type 3) Double
  47. * @param pdfFunction the Stitching (PDFfunction type 3) function,
  48. * even if it's stitching a single function
  49. */
  50. public PDFShading(int shadingType, PDFDeviceColorSpace colorSpace,
  51. List coords, PDFFunction pdfFunction) {
  52. shading = new Shading(shadingType, colorSpace, coords, pdfFunction.getFunction());
  53. this.pdfFunction = pdfFunction;
  54. }
  55. /**
  56. * Get the name of this shading.
  57. *
  58. * @return the name of the shading
  59. */
  60. public String getName() {
  61. return (this.shadingName);
  62. }
  63. /**
  64. * Sets the name of the shading
  65. * @param name the name of the shading pattern. Can be anything
  66. * without spaces. "Shading1" or "Sh1" are good examples.
  67. */
  68. public void setName(String name) {
  69. if (name.indexOf(" ") >= 0) {
  70. throw new IllegalArgumentException(
  71. "Shading name must not contain any spaces");
  72. }
  73. this.shadingName = name;
  74. }
  75. /**
  76. * represent as PDF. Whatever the shadingType is, the correct
  77. * representation spits out. The sets of required and optional
  78. * attributes are different for each type, but if a required
  79. * attribute's object was constructed as null, then no error
  80. * is raised. Instead, the malformed PDF that was requested
  81. * by the construction is dutifully output.
  82. * This policy should be reviewed.
  83. *
  84. * @return the PDF string.
  85. */
  86. public String toPDFString() {
  87. Shading.FunctionRenderer functionRenderer = new Shading.FunctionRenderer() {
  88. public void outputFunction(StringBuilder out) {
  89. out.append(pdfFunction.referencePDF());
  90. }
  91. };
  92. StringBuilder out = new StringBuilder();
  93. GradientMaker.DoubleFormatter doubleFormatter = new DoubleFormatter() {
  94. public String formatDouble(double d) {
  95. return PDFNumber.doubleOut(d);
  96. }
  97. };
  98. shading.output(out, doubleFormatter, functionRenderer);
  99. return out.toString();
  100. }
  101. /** {@inheritDoc} */
  102. protected boolean contentEquals(PDFObject obj) {
  103. if (obj == null) {
  104. return false;
  105. }
  106. if (obj == this) {
  107. return true;
  108. }
  109. if (!(obj instanceof PDFShading)) {
  110. return false;
  111. }
  112. Shading other = ((PDFShading) obj).shading;
  113. if (shading.getShadingType() != other.getShadingType()) {
  114. return false;
  115. }
  116. if (shading.isAntiAlias() != other.isAntiAlias()) {
  117. return false;
  118. }
  119. if (shading.getBitsPerCoordinate() != other.getBitsPerCoordinate()) {
  120. return false;
  121. }
  122. if (shading.getBitsPerFlag() != other.getBitsPerFlag()) {
  123. return false;
  124. }
  125. if (shading.getBitsPerComponent() != other.getBitsPerComponent()) {
  126. return false;
  127. }
  128. if (shading.getVerticesPerRow() != other.getVerticesPerRow()) {
  129. return false;
  130. }
  131. if (shading.getColorSpace() != null) {
  132. if (!shading.getColorSpace().equals(other.getColorSpace())) {
  133. return false;
  134. }
  135. } else if (other.getColorSpace() != null) {
  136. return false;
  137. }
  138. if (shading.getCoords() != null) {
  139. if (!shading.getCoords().equals(other.getCoords())) {
  140. return false;
  141. }
  142. } else if (other.getCoords() != null) {
  143. return false;
  144. }
  145. if (shading.getExtend() != null) {
  146. if (!shading.getExtend().equals(other.getExtend())) {
  147. return false;
  148. }
  149. } else if (other.getExtend() != null) {
  150. return false;
  151. }
  152. if (shading.getFunction() != null) {
  153. if (!shading.getFunction().equals(other.getFunction())) {
  154. return false;
  155. }
  156. } else if (other.getFunction() != null) {
  157. return false;
  158. }
  159. return true;
  160. }
  161. }