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.

Function.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. package org.apache.fop.render.gradient;
  18. import java.util.ArrayList;
  19. import java.util.Arrays;
  20. import java.util.Collections;
  21. import java.util.List;
  22. import org.apache.fop.render.gradient.GradientMaker.DoubleFormatter;
  23. public class Function {
  24. public interface SubFunctionRenderer {
  25. void outputFunction(StringBuilder out, int functionIndex);
  26. }
  27. /**
  28. * Required: The Type of function (0,2,3,4) default is 0.
  29. */
  30. private int functionType;
  31. /**
  32. * Required: 2 * m Array of Double numbers which are possible inputs to the function
  33. */
  34. private List<Double> domain;
  35. /**
  36. * Required: 2 * n Array of Double numbers which are possible outputs to the function
  37. */
  38. private List<Double> range;
  39. /**
  40. * Required for Type 0: Number of Bits used to represent each sample value.
  41. * Limited to 1,2,4,8,12,16,24, or 32
  42. */
  43. private int bitsPerSample = 1;
  44. /**
  45. * Optional for Type 0: order of interpolation between samples.
  46. * Limited to linear (1) or cubic (3). Default is 1
  47. */
  48. private int order = 1;
  49. /**
  50. * Optional for Type 0: A 2 * m array of Doubles which provides a
  51. * linear mapping of input values to the domain.
  52. *
  53. * Required for Type 3: A 2 * k array of Doubles that, taken
  54. * in pairs, map each subset of the domain defined by Domain
  55. * and the Bounds array to the domain of the corresponding function.
  56. * Should be two values per function, usually (0,1),
  57. * as in [0 1 0 1] for 2 functions.
  58. */
  59. private List<Double> encode;
  60. /* *************************TYPE 2************************** */
  61. /**
  62. * Required For Type 2: An Array of n Doubles defining
  63. * the function result when x=0. Default is [0].
  64. */
  65. private float[] cZero;
  66. /**
  67. * Required For Type 2: An Array of n Doubles defining
  68. * the function result when x=1. Default is [1].
  69. */
  70. private float[] cOne;
  71. /**
  72. * Required for Type 2: The interpolation exponent.
  73. * Each value x will return n results.
  74. * Must be greater than 0.
  75. */
  76. private double interpolationExponentN = 1;
  77. /* *************************TYPE 3************************** */
  78. /**
  79. * Required for Type 3: An vector of PDFFunctions which
  80. * form an array of k single input functions making up
  81. * the stitching function.
  82. */
  83. private List<Function> functions;
  84. /**
  85. * Optional for Type 3: An array of (k-1) Doubles that,
  86. * in combination with Domain, define the intervals to which
  87. * each function from the Functions array apply. Bounds
  88. * elements must be in order of increasing magnitude,
  89. * and each value must be within the value of Domain.
  90. * k is the number of functions.
  91. * If you pass null, it will output (1/k) in an array of k-1 elements.
  92. * This makes each function responsible for an equal amount of the stitching function.
  93. * It makes the gradient even.
  94. */
  95. private List<Float> bounds;
  96. /**
  97. * create an complete Function object of Type 2, an Exponential Interpolation function.
  98. *
  99. * Use null for an optional object parameter if you choose not to use it.
  100. * For optional int parameters, pass the default.
  101. * @param domain List objects of Double objects.
  102. * This is the domain of the function.
  103. * See page 264 of the PDF 1.3 Spec.
  104. * @param range List of Doubles that is the Range of the function.
  105. * See page 264 of the PDF 1.3 Spec.
  106. * @param cZero This is a vector of Double objects which defines the function result
  107. * when x=0.
  108. *
  109. * This attribute is optional.
  110. * It's described on page 268 of the PDF 1.3 spec.
  111. * @param cOne This is a vector of Double objects which defines the function result
  112. * when x=1.
  113. *
  114. * This attribute is optional.
  115. * It's described on page 268 of the PDF 1.3 spec.
  116. * @param interpolationExponentN This is the inerpolation exponent.
  117. *
  118. * This attribute is required.
  119. * PDF Spec page 268
  120. */
  121. public Function(List<Double> domain, List<Double> range, float[] cZero, float[] cOne,
  122. double interpolationExponentN) {
  123. this(2, domain, range);
  124. this.cZero = cZero;
  125. this.cOne = cOne;
  126. this.interpolationExponentN = interpolationExponentN;
  127. }
  128. /**
  129. * create an complete Function object of Type 3, a Stitching function.
  130. *
  131. * Use null for an optional object parameter if you choose not to use it.
  132. * For optional int parameters, pass the default.
  133. * @param domain List objects of Double objects.
  134. * This is the domain of the function.
  135. * See page 264 of the PDF 1.3 Spec.
  136. * @param range List objects of Double objects.
  137. * This is the Range of the function.
  138. * See page 264 of the PDF 1.3 Spec.
  139. * @param functions A List of the PDFFunction objects that the stitching function stitches.
  140. *
  141. * This attributed is required.
  142. * It is described on page 269 of the PDF spec.
  143. * @param bounds This is a vector of Doubles representing the numbers that,
  144. * in conjunction with Domain define the intervals to which each function from
  145. * the 'functions' object applies. It must be in order of increasing magnitude,
  146. * and each must be within Domain.
  147. *
  148. * It basically sets how much of the gradient each function handles.
  149. *
  150. * This attributed is required.
  151. * It's described on page 269 of the PDF 1.3 spec.
  152. * @param encode List objects of Double objects.
  153. * This is the linear mapping of input values intop the domain
  154. * of the function's sample table. Default is hard to represent in
  155. * ascii, but basically [0 (Size0 1) 0 (Size1 1)...].
  156. * This attribute is required.
  157. *
  158. * See page 270 in the PDF 1.3 spec.
  159. */
  160. public Function(List<Double> domain, List<Double> range, List<Function> functions,
  161. List<Float> bounds, List<Double> encode) {
  162. this(3, domain, range);
  163. this.functions = functions;
  164. this.bounds = bounds;
  165. this.encode = makeEncode(encode);
  166. }
  167. private List<Double> makeEncode(List<Double> encode) {
  168. if (encode != null) {
  169. return encode;
  170. } else {
  171. encode = new ArrayList<Double>(functions.size() * 2);
  172. for (int i = 0; i < functions.size(); i++) {
  173. encode.add(0.0);
  174. encode.add(1.0);
  175. }
  176. return encode;
  177. }
  178. }
  179. private Function(int functionType, List<Double> domain, List<Double> range) {
  180. this.functionType = functionType;
  181. this.domain = (domain == null) ? Arrays.asList(0.0, 1.0) : domain;
  182. this.range = range;
  183. }
  184. /**
  185. * Gets the function type
  186. */
  187. public int getFunctionType() {
  188. return functionType;
  189. }
  190. /**
  191. * Gets the function bounds
  192. */
  193. public List<Float> getBounds() {
  194. return bounds;
  195. }
  196. /**
  197. * The function domain
  198. */
  199. public List<Double> getDomain() {
  200. return domain;
  201. }
  202. /**
  203. * Gets the function encoding
  204. */
  205. public List<Double> getEncode() {
  206. return encode;
  207. }
  208. /**
  209. * Gets the sub-functions
  210. */
  211. public List<Function> getFunctions() {
  212. if (functions == null) {
  213. return Collections.emptyList();
  214. } else {
  215. return functions;
  216. }
  217. }
  218. /**
  219. * Gets the bits per sample of the function
  220. */
  221. public int getBitsPerSample() {
  222. return bitsPerSample;
  223. }
  224. /**
  225. * Gets the interpolation exponent of the function
  226. */
  227. public double getInterpolationExponentN() {
  228. return interpolationExponentN;
  229. }
  230. /**
  231. * Gets the function order
  232. */
  233. public int getOrder() {
  234. return order;
  235. }
  236. /**
  237. * Gets the function range
  238. */
  239. public List<Double> getRange() {
  240. return range;
  241. }
  242. /**
  243. * Gets the function C0 value (color for gradient)
  244. */
  245. public float[] getCZero() {
  246. return cZero;
  247. }
  248. /**
  249. * Gets the function C1 value (color for gradient)
  250. */
  251. public float[] getCOne() {
  252. return cOne;
  253. }
  254. public String output(StringBuilder out, DoubleFormatter doubleFormatter,
  255. SubFunctionRenderer subFunctionRenderer) {
  256. out.append("<<\n/FunctionType " + functionType + "\n");
  257. outputDomain(out, doubleFormatter);
  258. if (this.functionType == 0) {
  259. outputEncode(out, doubleFormatter);
  260. outputBitsPerSample(out);
  261. outputOrder(out);
  262. outputRange(out, doubleFormatter);
  263. out.append(">>");
  264. } else if (functionType == 2) {
  265. outputRange(out, doubleFormatter);
  266. outputCZero(out, doubleFormatter);
  267. outputCOne(out, doubleFormatter);
  268. outputInterpolationExponentN(out, doubleFormatter);
  269. out.append(">>");
  270. } else if (functionType == 3) {
  271. outputRange(out, doubleFormatter);
  272. if (!functions.isEmpty()) {
  273. out.append("/Functions [ ");
  274. for (int i = 0; i < functions.size(); i++) {
  275. subFunctionRenderer.outputFunction(out, i);
  276. out.append(' ');
  277. }
  278. out.append("]\n");
  279. }
  280. outputEncode(out, doubleFormatter);
  281. out.append("/Bounds ");
  282. if (bounds != null) {
  283. GradientMaker.outputDoubles(out, doubleFormatter, bounds);
  284. } else if (!functions.isEmpty()) {
  285. // if there are n functions,
  286. // there must be n-1 bounds.
  287. // so let each function handle an equal portion
  288. // of the whole. e.g. if there are 4, then [ 0.25 0.25 0.25 ]
  289. int numberOfFunctions = functions.size();
  290. String functionsFraction = doubleFormatter.formatDouble(1.0 / numberOfFunctions);
  291. out.append("[ ");
  292. for (int i = 0; i + 1 < numberOfFunctions; i++) {
  293. out.append(functionsFraction);
  294. out.append(" ");
  295. }
  296. out.append("]");
  297. }
  298. out.append("\n>>");
  299. } else if (functionType == 4) {
  300. outputRange(out, doubleFormatter);
  301. out.append(">>");
  302. }
  303. return out.toString();
  304. }
  305. private void outputDomain(StringBuilder p, DoubleFormatter doubleFormatter) {
  306. p.append("/Domain ");
  307. GradientMaker.outputDoubles(p, doubleFormatter, domain);
  308. p.append("\n");
  309. }
  310. private void outputBitsPerSample(StringBuilder out) {
  311. out.append("/BitsPerSample " + bitsPerSample + "\n");
  312. }
  313. private void outputOrder(StringBuilder out) {
  314. if (order == 1 || order == 3) {
  315. out.append("\n/Order " + order + "\n");
  316. }
  317. }
  318. private void outputRange(StringBuilder out, DoubleFormatter doubleFormatter) {
  319. if (range != null) {
  320. out.append("/Range ");
  321. GradientMaker.outputDoubles(out, doubleFormatter, range);
  322. out.append("\n");
  323. }
  324. }
  325. private void outputEncode(StringBuilder out, DoubleFormatter doubleFormatter) {
  326. out.append("/Encode ");
  327. GradientMaker.outputDoubles(out, doubleFormatter, encode);
  328. out.append("\n");
  329. }
  330. private void outputCZero(StringBuilder out, DoubleFormatter doubleFormatter) {
  331. if (cZero != null) {
  332. out.append("/C0 [ ");
  333. for (float c : cZero) {
  334. out.append(doubleFormatter.formatDouble(c));
  335. out.append(" ");
  336. }
  337. out.append("]\n");
  338. }
  339. }
  340. private void outputCOne(StringBuilder out, DoubleFormatter doubleFormatter) {
  341. if (cOne != null) {
  342. out.append("/C1 [ ");
  343. for (float c : cOne) {
  344. out.append(doubleFormatter.formatDouble(c));
  345. out.append(" ");
  346. }
  347. out.append("]\n");
  348. }
  349. }
  350. private void outputInterpolationExponentN(StringBuilder out, DoubleFormatter doubleFormatter) {
  351. out.append("/N ");
  352. out.append(doubleFormatter.formatDouble(interpolationExponentN));
  353. out.append("\n");
  354. }
  355. }