Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Function.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. private byte[] datasource;
  97. private List<Integer> size;
  98. /**
  99. * create an complete Function object of Type 2, an Exponential Interpolation function.
  100. *
  101. * Use null for an optional object parameter if you choose not to use it.
  102. * For optional int parameters, pass the default.
  103. * @param domain List objects of Double objects.
  104. * This is the domain of the function.
  105. * See page 264 of the PDF 1.3 Spec.
  106. * @param range List of Doubles that is the Range of the function.
  107. * See page 264 of the PDF 1.3 Spec.
  108. * @param cZero This is a vector of Double objects which defines the function result
  109. * when x=0.
  110. *
  111. * This attribute is optional.
  112. * It's described on page 268 of the PDF 1.3 spec.
  113. * @param cOne This is a vector of Double objects which defines the function result
  114. * when x=1.
  115. *
  116. * This attribute is optional.
  117. * It's described on page 268 of the PDF 1.3 spec.
  118. * @param interpolationExponentN This is the inerpolation exponent.
  119. *
  120. * This attribute is required.
  121. * PDF Spec page 268
  122. */
  123. public Function(List<Double> domain, List<Double> range, float[] cZero, float[] cOne,
  124. double interpolationExponentN) {
  125. this(2, domain, range);
  126. this.cZero = cZero;
  127. this.cOne = cOne;
  128. this.interpolationExponentN = interpolationExponentN;
  129. }
  130. /**
  131. * create an complete Function object of Type 3, a Stitching function.
  132. *
  133. * Use null for an optional object parameter if you choose not to use it.
  134. * For optional int parameters, pass the default.
  135. * @param domain List objects of Double objects.
  136. * This is the domain of the function.
  137. * See page 264 of the PDF 1.3 Spec.
  138. * @param range List objects of Double objects.
  139. * This is the Range of the function.
  140. * See page 264 of the PDF 1.3 Spec.
  141. * @param functions A List of the PDFFunction objects that the stitching function stitches.
  142. *
  143. * This attributed is required.
  144. * It is described on page 269 of the PDF spec.
  145. * @param bounds This is a vector of Doubles representing the numbers that,
  146. * in conjunction with Domain define the intervals to which each function from
  147. * the 'functions' object applies. It must be in order of increasing magnitude,
  148. * and each must be within Domain.
  149. *
  150. * It basically sets how much of the gradient each function handles.
  151. *
  152. * This attributed is required.
  153. * It's described on page 269 of the PDF 1.3 spec.
  154. * @param encode List objects of Double objects.
  155. * This is the linear mapping of input values intop the domain
  156. * of the function's sample table. Default is hard to represent in
  157. * ascii, but basically [0 (Size0 1) 0 (Size1 1)...].
  158. * This attribute is required.
  159. *
  160. * See page 270 in the PDF 1.3 spec.
  161. */
  162. public Function(List<Double> domain, List<Double> range, List<Function> functions,
  163. List<Float> bounds, List<Double> encode) {
  164. this(3, domain, range);
  165. this.functions = functions;
  166. this.bounds = bounds;
  167. this.encode = makeEncode(encode);
  168. }
  169. public void setCZero(float[] cZero) {
  170. this.cZero = cZero;
  171. }
  172. public void setCOne(float[] cOne) {
  173. this.cOne = cOne;
  174. }
  175. private List<Double> makeEncode(List<Double> encode) {
  176. if (encode != null) {
  177. return encode;
  178. } else {
  179. encode = new ArrayList<Double>(functions.size() * 2);
  180. for (int i = 0; i < functions.size(); i++) {
  181. encode.add(0.0);
  182. encode.add(1.0);
  183. }
  184. return encode;
  185. }
  186. }
  187. private Function(int functionType, List<Double> domain, List<Double> range) {
  188. this.functionType = functionType;
  189. this.domain = (domain == null) ? Arrays.asList(0.0, 1.0) : domain;
  190. this.range = range;
  191. }
  192. public Function(List<Double> domain, List<Double> range, List<Double> encode, byte[] datasource, int bitsPerSample,
  193. List<Integer> size) {
  194. this(0, domain, range);
  195. this.encode = encode;
  196. this.datasource = datasource;
  197. this.bitsPerSample = bitsPerSample;
  198. this.size = size;
  199. }
  200. /**
  201. * Gets the function type
  202. */
  203. public int getFunctionType() {
  204. return functionType;
  205. }
  206. /**
  207. * Gets the function bounds
  208. */
  209. public List<Float> getBounds() {
  210. return bounds;
  211. }
  212. /**
  213. * The function domain
  214. */
  215. public List<Double> getDomain() {
  216. return domain;
  217. }
  218. /**
  219. * Gets the function encoding
  220. */
  221. public List<Double> getEncode() {
  222. return encode;
  223. }
  224. /**
  225. * Gets the sub-functions
  226. */
  227. public List<Function> getFunctions() {
  228. if (functions == null) {
  229. return Collections.emptyList();
  230. } else {
  231. return functions;
  232. }
  233. }
  234. /**
  235. * Gets the bits per sample of the function
  236. */
  237. public int getBitsPerSample() {
  238. return bitsPerSample;
  239. }
  240. /**
  241. * Gets the interpolation exponent of the function
  242. */
  243. public double getInterpolationExponentN() {
  244. return interpolationExponentN;
  245. }
  246. /**
  247. * Gets the function order
  248. */
  249. public int getOrder() {
  250. return order;
  251. }
  252. /**
  253. * Gets the function range
  254. */
  255. public List<Double> getRange() {
  256. return range;
  257. }
  258. /**
  259. * Gets the function C0 value (color for gradient)
  260. */
  261. public float[] getCZero() {
  262. return cZero;
  263. }
  264. /**
  265. * Gets the function C1 value (color for gradient)
  266. */
  267. public float[] getCOne() {
  268. return cOne;
  269. }
  270. public String output(StringBuilder out, DoubleFormatter doubleFormatter,
  271. SubFunctionRenderer subFunctionRenderer) {
  272. out.append("<<\n/FunctionType " + functionType + "\n");
  273. outputDomain(out, doubleFormatter);
  274. if (this.functionType == 0) {
  275. outputEncode(out, doubleFormatter);
  276. outputBitsPerSample(out);
  277. outputOrder(out);
  278. outputRange(out, doubleFormatter);
  279. out.append("\n/DataSource <");
  280. for (byte b : datasource) {
  281. out.append(String.format("%02x", b & 0xff));
  282. }
  283. out.append(">\n");
  284. out.append("/Size [");
  285. for (Integer i : size) {
  286. out.append(i);
  287. out.append(" ");
  288. }
  289. out.append("]\n");
  290. out.append(">>");
  291. } else if (functionType == 2) {
  292. outputRange(out, doubleFormatter);
  293. outputCZero(out, doubleFormatter);
  294. outputCOne(out, doubleFormatter);
  295. outputInterpolationExponentN(out, doubleFormatter);
  296. out.append(">>");
  297. } else if (functionType == 3) {
  298. outputRange(out, doubleFormatter);
  299. if (!functions.isEmpty()) {
  300. out.append("/Functions [ ");
  301. for (int i = 0; i < functions.size(); i++) {
  302. subFunctionRenderer.outputFunction(out, i);
  303. out.append(' ');
  304. }
  305. out.append("]\n");
  306. }
  307. outputEncode(out, doubleFormatter);
  308. out.append("/Bounds ");
  309. if (bounds != null) {
  310. GradientMaker.outputDoubles(out, doubleFormatter, bounds);
  311. } else if (!functions.isEmpty()) {
  312. // if there are n functions,
  313. // there must be n-1 bounds.
  314. // so let each function handle an equal portion
  315. // of the whole. e.g. if there are 4, then [ 0.25 0.25 0.25 ]
  316. int numberOfFunctions = functions.size();
  317. String functionsFraction = doubleFormatter.formatDouble(1.0 / numberOfFunctions);
  318. out.append("[ ");
  319. for (int i = 0; i + 1 < numberOfFunctions; i++) {
  320. out.append(functionsFraction);
  321. out.append(" ");
  322. }
  323. out.append("]");
  324. }
  325. out.append("\n>>");
  326. } else if (functionType == 4) {
  327. outputRange(out, doubleFormatter);
  328. out.append(">>");
  329. }
  330. return out.toString();
  331. }
  332. private void outputDomain(StringBuilder p, DoubleFormatter doubleFormatter) {
  333. p.append("/Domain ");
  334. GradientMaker.outputDoubles(p, doubleFormatter, domain);
  335. p.append("\n");
  336. }
  337. private void outputBitsPerSample(StringBuilder out) {
  338. out.append("/BitsPerSample " + bitsPerSample + "\n");
  339. }
  340. private void outputOrder(StringBuilder out) {
  341. if (order == 1 || order == 3) {
  342. out.append("\n/Order " + order + "\n");
  343. }
  344. }
  345. private void outputRange(StringBuilder out, DoubleFormatter doubleFormatter) {
  346. if (range != null) {
  347. out.append("/Range ");
  348. GradientMaker.outputDoubles(out, doubleFormatter, range);
  349. out.append("\n");
  350. }
  351. }
  352. private void outputEncode(StringBuilder out, DoubleFormatter doubleFormatter) {
  353. out.append("/Encode ");
  354. GradientMaker.outputDoubles(out, doubleFormatter, encode);
  355. out.append("\n");
  356. }
  357. private void outputCZero(StringBuilder out, DoubleFormatter doubleFormatter) {
  358. if (cZero != null) {
  359. out.append("/C0 [ ");
  360. for (float c : cZero) {
  361. out.append(doubleFormatter.formatDouble(c));
  362. out.append(" ");
  363. }
  364. out.append("]\n");
  365. }
  366. }
  367. private void outputCOne(StringBuilder out, DoubleFormatter doubleFormatter) {
  368. if (cOne != null) {
  369. out.append("/C1 [ ");
  370. for (float c : cOne) {
  371. out.append(doubleFormatter.formatDouble(c));
  372. out.append(" ");
  373. }
  374. out.append("]\n");
  375. }
  376. }
  377. private void outputInterpolationExponentN(StringBuilder out, DoubleFormatter doubleFormatter) {
  378. out.append("/N ");
  379. out.append(doubleFormatter.formatDouble(interpolationExponentN));
  380. out.append("\n");
  381. }
  382. }