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

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