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.

PDFFunction.java 7.0KB

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