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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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.Arrays;
  20. import java.util.Collections;
  21. import java.util.List;
  22. import org.apache.fop.render.gradient.Function;
  23. import org.apache.fop.render.gradient.Function.SubFunctionRenderer;
  24. import org.apache.fop.render.gradient.GradientMaker;
  25. import org.apache.fop.render.gradient.GradientMaker.DoubleFormatter;
  26. /**
  27. * class representing a PDF Function.
  28. *
  29. * PDF Functions represent parameterized mathematical formulas and
  30. * sampled representations with
  31. * arbitrary resolution. Functions are used in two areas: device-dependent
  32. * rasterization information for halftoning and transfer
  33. * functions, and color specification for smooth shading (a PDF 1.3 feature).
  34. *
  35. * All PDF Functions have a FunctionType (0,2,3, or 4), a Domain, and a Range.
  36. */
  37. public class PDFFunction extends PDFObject {
  38. private final Function function;
  39. private final List<PDFFunction> pdfFunctions;
  40. /**
  41. * create an complete Function object of Type 2, an Exponential Interpolation function.
  42. *
  43. * Use null for an optional object parameter if you choose not to use it.
  44. * For optional int parameters, pass the default.
  45. *
  46. * @param domain List objects of Double objects.
  47. * This is the domain of the function.
  48. * See page 264 of the PDF 1.3 Spec.
  49. * @param range List of Doubles that is the Range of the function.
  50. * See page 264 of the PDF 1.3 Spec.
  51. * @param cZero This is a vector of Double objects which defines the function result
  52. * when x=0.
  53. *
  54. * This attribute is optional.
  55. * It's described on page 268 of the PDF 1.3 spec.
  56. * @param cOne This is a vector of Double objects which defines the function result
  57. * when x=1.
  58. *
  59. * This attribute is optional.
  60. * It's described on page 268 of the PDF 1.3 spec.
  61. * @param interpolationExponentN This is the inerpolation exponent.
  62. *
  63. * This attribute is required.
  64. * PDF Spec page 268
  65. */
  66. public PDFFunction(List<Double> domain, List<Double> range, float[] cZero, float[] cOne,
  67. double interpolationExponentN) {
  68. this(new Function(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. SubFunctionRenderer subFunctionRenderer = new SubFunctionRenderer() {
  97. public void outputFunction(StringBuilder out, int functionIndex) {
  98. out.append(pdfFunctions.get(functionIndex).referencePDF());
  99. }
  100. };
  101. StringBuilder out = new StringBuilder();
  102. GradientMaker.DoubleFormatter doubleFormatter = new DoubleFormatter() {
  103. public String formatDouble(double d) {
  104. return PDFNumber.doubleOut(d);
  105. }
  106. };
  107. function.output(out, doubleFormatter, subFunctionRenderer);
  108. return encode(out.toString());
  109. }
  110. /** {@inheritDoc} */
  111. protected boolean contentEquals(PDFObject obj) {
  112. if (obj == null) {
  113. return false;
  114. }
  115. if (obj == this) {
  116. return true;
  117. }
  118. if (!(obj instanceof PDFFunction)) {
  119. return false;
  120. }
  121. Function func = ((PDFFunction) obj).function;
  122. if (function.getFunctionType() != func.getFunctionType()) {
  123. return false;
  124. }
  125. if (function.getBitsPerSample() != func.getBitsPerSample()) {
  126. return false;
  127. }
  128. if (function.getOrder() != func.getOrder()) {
  129. return false;
  130. }
  131. if (function.getInterpolationExponentN() != func.getInterpolationExponentN()) {
  132. return false;
  133. }
  134. if (function.getDomain() != null) {
  135. if (!function.getDomain().equals(func.getDomain())) {
  136. return false;
  137. }
  138. } else if (func.getDomain() != null) {
  139. return false;
  140. }
  141. if (function.getRange() != null) {
  142. if (!function.getRange().equals(func.getRange())) {
  143. return false;
  144. }
  145. } else if (func.getRange() != null) {
  146. return false;
  147. }
  148. if (function.getEncode() != null) {
  149. if (!function.getEncode().equals(func.getEncode())) {
  150. return false;
  151. }
  152. } else if (func.getEncode() != null) {
  153. return false;
  154. }
  155. if (!Arrays.equals(function.getCZero(), func.getCZero())) {
  156. return false;
  157. }
  158. if (!Arrays.equals(function.getCOne(), func.getCOne())) {
  159. return false;
  160. }
  161. if (!pdfFunctions.equals(((PDFFunction) obj).pdfFunctions)) {
  162. return false;
  163. }
  164. if (function.getBounds() != null) {
  165. if (!function.getBounds().equals(func.getBounds())) {
  166. return false;
  167. }
  168. } else if (func.getBounds() != null) {
  169. return false;
  170. }
  171. return true;
  172. }
  173. }