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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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.ArrayList;
  20. import java.util.Arrays;
  21. import java.util.Collections;
  22. import java.util.List;
  23. import org.apache.fop.render.gradient.Function;
  24. import org.apache.fop.render.gradient.Function.SubFunctionRenderer;
  25. /**
  26. * class representing a PDF Function.
  27. *
  28. * PDF Functions represent parameterized mathematical formulas and
  29. * sampled representations with
  30. * arbitrary resolution. Functions are used in two areas: device-dependent
  31. * rasterization information for halftoning and transfer
  32. * functions, and color specification for smooth shading (a PDF 1.3 feature).
  33. *
  34. * All PDF Functions have a FunctionType (0,2,3, or 4), a Domain, and a Range.
  35. */
  36. public class PDFFunction extends PDFObject {
  37. private final Function function;
  38. private final List<PDFFunction> pdfFunctions;
  39. /**
  40. * create an complete Function object of Type 2, an Exponential Interpolation function.
  41. *
  42. * Use null for an optional object parameter if you choose not to use it.
  43. * For optional int parameters, pass the default.
  44. *
  45. * @param domain List objects of Double objects.
  46. * This is the domain of the function.
  47. * See page 264 of the PDF 1.3 Spec.
  48. * @param range List of Doubles that is the Range of the function.
  49. * See page 264 of the PDF 1.3 Spec.
  50. * @param cZero This is a vector of Double objects which defines the function result
  51. * when x=0.
  52. *
  53. * This attribute is optional.
  54. * It's described on page 268 of the PDF 1.3 spec.
  55. * @param cOne This is a vector of Double objects which defines the function result
  56. * when x=1.
  57. *
  58. * This attribute is optional.
  59. * It's described on page 268 of the PDF 1.3 spec.
  60. * @param interpolationExponentN This is the inerpolation exponent.
  61. *
  62. * This attribute is required.
  63. * PDF Spec page 268
  64. * @param functionType The type of the function, which should be 2.
  65. */
  66. public PDFFunction(int functionType, List<Double> domain, List<Double> range, float[] cZero, float[] cOne,
  67. double interpolationExponentN) {
  68. this(new Function(functionType, domain, range, cZero, cOne, interpolationExponentN));
  69. }
  70. @SuppressWarnings("unchecked")
  71. public PDFFunction(Function function) {
  72. this(function, Collections.EMPTY_LIST);
  73. }
  74. public PDFFunction(Function function, List<PDFFunction> pdfFunctions) {
  75. this.function = function;
  76. this.pdfFunctions = pdfFunctions;
  77. }
  78. public Function getFunction() {
  79. return function;
  80. }
  81. /**
  82. * represent as PDF. Whatever the FunctionType is, the correct
  83. * representation spits out. The sets of required and optional
  84. * attributes are different for each type, but if a required
  85. * attribute's object was constructed as null, then no error
  86. * is raised. Instead, the malformed PDF that was requested
  87. * by the construction is dutifully output.
  88. * This policy should be reviewed.
  89. *
  90. * @return the PDF string.
  91. */
  92. public byte[] toPDF() {
  93. return toByteString();
  94. }
  95. public byte[] toByteString() {
  96. List<String> functionsStrings = new ArrayList<String>(function.getFunctions().size());
  97. for (PDFFunction f : pdfFunctions) {
  98. functionsStrings.add(f.referencePDF());
  99. }
  100. SubFunctionRenderer subFunctionRenderer = new SubFunctionRenderer() {
  101. public void outputFunction(StringBuilder out, int functionIndex) {
  102. out.append(pdfFunctions.get(functionIndex).referencePDF());
  103. }
  104. };
  105. return encode(function.toWriteableString(subFunctionRenderer));
  106. }
  107. /** {@inheritDoc} */
  108. protected boolean contentEquals(PDFObject obj) {
  109. if (obj == null) {
  110. return false;
  111. }
  112. if (obj == this) {
  113. return true;
  114. }
  115. if (!(obj instanceof PDFFunction)) {
  116. return false;
  117. }
  118. Function func = ((PDFFunction) obj).function;
  119. if (function.getFunctionType() != func.getFunctionType()) {
  120. return false;
  121. }
  122. if (function.getBitsPerSample() != func.getBitsPerSample()) {
  123. return false;
  124. }
  125. if (function.getOrder() != func.getOrder()) {
  126. return false;
  127. }
  128. if (function.getInterpolationExponentN() != func.getInterpolationExponentN()) {
  129. return false;
  130. }
  131. if (function.getDomain() != null) {
  132. if (!function.getDomain().equals(func.getDomain())) {
  133. return false;
  134. }
  135. } else if (func.getDomain() != null) {
  136. return false;
  137. }
  138. if (function.getRange() != null) {
  139. if (!function.getRange().equals(func.getRange())) {
  140. return false;
  141. }
  142. } else if (func.getRange() != null) {
  143. return false;
  144. }
  145. if (function.getSize() != null) {
  146. if (!function.getSize().equals(func.getSize())) {
  147. return false;
  148. }
  149. } else if (func.getSize() != null) {
  150. return false;
  151. }
  152. if (function.getEncode() != null) {
  153. if (!function.getEncode().equals(func.getEncode())) {
  154. return false;
  155. }
  156. } else if (func.getEncode() != null) {
  157. return false;
  158. }
  159. if (function.getDecode() != null) {
  160. if (!function.getDecode().equals(func.getDecode())) {
  161. return false;
  162. }
  163. } else if (func.getDecode() != null) {
  164. return false;
  165. }
  166. if (function.getDataStream() != null) {
  167. if (!function.getDataStream().equals(func.getDataStream())) {
  168. return false;
  169. }
  170. } else if (func.getDataStream() != null) {
  171. return false;
  172. }
  173. if (function.getFilter() != null) {
  174. if (!function.getFilter().equals(func.getFilter())) {
  175. return false;
  176. }
  177. } else if (func.getFilter() != null) {
  178. return false;
  179. }
  180. if (!Arrays.equals(function.getCZero(), func.getCZero())) {
  181. return false;
  182. }
  183. if (!Arrays.equals(function.getCOne(), func.getCOne())) {
  184. return false;
  185. }
  186. if (!pdfFunctions.equals(((PDFFunction) obj).pdfFunctions)) {
  187. return false;
  188. }
  189. if (function.getBounds() != null) {
  190. if (!function.getBounds().equals(func.getBounds())) {
  191. return false;
  192. }
  193. } else if (func.getBounds() != null) {
  194. return false;
  195. }
  196. return true;
  197. }
  198. }