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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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.Collections;
  19. import java.util.List;
  20. import org.apache.fop.pdf.PDFNumber;
  21. public class Function {
  22. /**
  23. * Required: The Type of function (0,2,3,4) default is 0.
  24. */
  25. private int functionType;
  26. /**
  27. * Required: 2 * m Array of Double numbers which are possible inputs to the function
  28. */
  29. private List<Double> domain;
  30. /**
  31. * Required: 2 * n Array of Double numbers which are possible outputs to the function
  32. */
  33. private List<Double> range;
  34. /* ********************TYPE 0***************************** */
  35. // FunctionType 0 specific function guts
  36. /**
  37. * Required: Array containing the Integer size of the Domain and Range, respectively.
  38. * Note: This is really more like two seperate integers, sizeDomain, and sizeRange,
  39. * but since they're expressed as an array in PDF, my implementation reflects that.
  40. */
  41. protected List<Double> size;
  42. /**
  43. * Required for Type 0: Number of Bits used to represent each sample value.
  44. * Limited to 1,2,4,8,12,16,24, or 32
  45. */
  46. private int bitsPerSample = 1;
  47. /**
  48. * Optional for Type 0: order of interpolation between samples.
  49. * Limited to linear (1) or cubic (3). Default is 1
  50. */
  51. private int order = 1;
  52. /**
  53. * Optional for Type 0: A 2 * m array of Doubles which provides a
  54. * linear mapping of input values to the domain.
  55. *
  56. * Required for Type 3: A 2 * k array of Doubles that, taken
  57. * in pairs, map each subset of the domain defined by Domain
  58. * and the Bounds array to the domain of the corresponding function.
  59. * Should be two values per function, usually (0,1),
  60. * as in [0 1 0 1] for 2 functions.
  61. */
  62. private List<Double> encode;
  63. /**
  64. * Optional for Type 0: A 2 * n array of Doubles which provides
  65. * a linear mapping of sample values to the range. Defaults to Range.
  66. */
  67. private List<Double> decode;
  68. /**
  69. * Optional For Type 0: A stream of sample values
  70. */
  71. /**
  72. * Required For Type 4: Postscript Calculator function
  73. * composed of arithmetic, boolean, and stack operators + boolean constants
  74. */
  75. private StringBuffer functionDataStream;
  76. /**
  77. * Required (possibly) For Type 0: A vector of Strings for the
  78. * various filters to be used to decode the stream.
  79. * These are how the string is compressed. Flate, LZW, etc.
  80. */
  81. private List<String> filter;
  82. /* *************************TYPE 2************************** */
  83. /**
  84. * Required For Type 2: An Array of n Doubles defining
  85. * the function result when x=0. Default is [0].
  86. */
  87. private float[] cZero;
  88. /**
  89. * Required For Type 2: An Array of n Doubles defining
  90. * the function result when x=1. Default is [1].
  91. */
  92. private float[] cOne;
  93. /**
  94. * Required for Type 2: The interpolation exponent.
  95. * Each value x will return n results.
  96. * Must be greater than 0.
  97. */
  98. private double interpolationExponentN = 1;
  99. /* *************************TYPE 3************************** */
  100. /**
  101. * Required for Type 3: An vector of PDFFunctions which
  102. * form an array of k single input functions making up
  103. * the stitching function.
  104. */
  105. private List<Function> functions;
  106. /**
  107. * Optional for Type 3: An array of (k-1) Doubles that,
  108. * in combination with Domain, define the intervals to which
  109. * each function from the Functions array apply. Bounds
  110. * elements must be in order of increasing magnitude,
  111. * and each value must be within the value of Domain.
  112. * k is the number of functions.
  113. * If you pass null, it will output (1/k) in an array of k-1 elements.
  114. * This makes each function responsible for an equal amount of the stitching function.
  115. * It makes the gradient even.
  116. */
  117. private List<Double> bounds;
  118. /**
  119. * create an complete Function object of Type 2, an Exponential Interpolation function.
  120. *
  121. * Use null for an optional object parameter if you choose not to use it.
  122. * For optional int parameters, pass the default.
  123. * @param functionType The type of the function, which should be 2.
  124. * @param domain List objects of Double objects.
  125. * This is the domain of the function.
  126. * See page 264 of the PDF 1.3 Spec.
  127. * @param range List of Doubles that is the Range of the function.
  128. * See page 264 of the PDF 1.3 Spec.
  129. * @param cZero This is a vector of Double objects which defines the function result
  130. * when x=0.
  131. *
  132. * This attribute is optional.
  133. * It's described on page 268 of the PDF 1.3 spec.
  134. * @param cOne This is a vector of Double objects which defines the function result
  135. * when x=1.
  136. *
  137. * This attribute is optional.
  138. * It's described on page 268 of the PDF 1.3 spec.
  139. * @param interpolationExponentN This is the inerpolation exponent.
  140. *
  141. * This attribute is required.
  142. * PDF Spec page 268
  143. */
  144. public Function(int functionType, List<Double> domain, List<Double> range,
  145. float[] cZero, float[] cOne, double interpolationExponentN) {
  146. this.functionType = 2; // dang well better be 2;
  147. this.cZero = cZero;
  148. this.cOne = cOne;
  149. this.interpolationExponentN = interpolationExponentN;
  150. this.domain = domain;
  151. this.range = range;
  152. }
  153. /**
  154. * create an complete Function object of Type 3, a Stitching function.
  155. *
  156. * Use null for an optional object parameter if you choose not to use it.
  157. * For optional int parameters, pass the default.
  158. * @param functionType This is the function type. It should be 3,
  159. * for a stitching function.
  160. * @param domain List objects of Double objects.
  161. * This is the domain of the function.
  162. * See page 264 of the PDF 1.3 Spec.
  163. * @param range List objects of Double objects.
  164. * This is the Range of the function.
  165. * See page 264 of the PDF 1.3 Spec.
  166. * @param functions A List of the PDFFunction objects that the stitching function stitches.
  167. *
  168. * This attributed is required.
  169. * It is described on page 269 of the PDF spec.
  170. * @param bounds This is a vector of Doubles representing the numbers that,
  171. * in conjunction with Domain define the intervals to which each function from
  172. * the 'functions' object applies. It must be in order of increasing magnitude,
  173. * and each must be within Domain.
  174. *
  175. * It basically sets how much of the gradient each function handles.
  176. *
  177. * This attributed is required.
  178. * It's described on page 269 of the PDF 1.3 spec.
  179. * @param encode List objects of Double objects.
  180. * This is the linear mapping of input values intop the domain
  181. * of the function's sample table. Default is hard to represent in
  182. * ascii, but basically [0 (Size0 1) 0 (Size1 1)...].
  183. * This attribute is required.
  184. *
  185. * See page 270 in the PDF 1.3 spec.
  186. */
  187. public Function(int functionType, List<Double> domain, List<Double> range,
  188. List<Function> functions, List<Double> bounds,
  189. List<Double> encode) {
  190. this.functionType = 3; // dang well better be 3;
  191. this.functions = functions;
  192. this.bounds = bounds;
  193. this.encode = encode;
  194. this.domain = domain;
  195. this.range = range;
  196. }
  197. /**
  198. * Gets the function type
  199. */
  200. public int getFunctionType() {
  201. return functionType;
  202. }
  203. /**
  204. * Gets the function bounds
  205. */
  206. public List<Double> getBounds() {
  207. return bounds;
  208. }
  209. /**
  210. * The function domain
  211. */
  212. public List<Double> getDomain() {
  213. return domain;
  214. }
  215. /**
  216. * The function size
  217. */
  218. public List<Double> getSize() {
  219. return size;
  220. }
  221. /**
  222. * Gets the function encoding
  223. */
  224. public List<Double> getEncode() {
  225. return encode;
  226. }
  227. /**
  228. * Gets the sub-functions
  229. */
  230. public List<Function> getFunctions() {
  231. if (functions == null) {
  232. return Collections.emptyList();
  233. } else {
  234. return functions;
  235. }
  236. }
  237. /**
  238. * Gets the function filter
  239. */
  240. public List<String> getFilter() {
  241. return filter;
  242. }
  243. /**
  244. * Gets the bits per sample of the function
  245. */
  246. public int getBitsPerSample() {
  247. return bitsPerSample;
  248. }
  249. /**
  250. * Gets the interpolation exponent of the function
  251. */
  252. public double getInterpolationExponentN() {
  253. return interpolationExponentN;
  254. }
  255. /**
  256. * Gets the function order
  257. */
  258. public int getOrder() {
  259. return order;
  260. }
  261. /**
  262. * Gets the function range
  263. */
  264. public List<Double> getRange() {
  265. return range;
  266. }
  267. /**
  268. * Gets the function decoding
  269. */
  270. public List<Double> getDecode() {
  271. return decode;
  272. }
  273. /**
  274. * Gets the function data stream
  275. */
  276. public StringBuffer getDataStream() {
  277. return functionDataStream;
  278. }
  279. /**
  280. * Gets the function C0 value (color for gradient)
  281. */
  282. public float[] getCZero() {
  283. return cZero;
  284. }
  285. /**
  286. * Gets the function C1 value (color for gradient)
  287. */
  288. public float[] getCOne() {
  289. return cOne;
  290. }
  291. public String toWriteableString(List<String> functionsStrings) {
  292. StringBuilder out = new StringBuilder(256);
  293. out.append("<<\n/FunctionType " + functionType + "\n");
  294. outputDomain(out);
  295. if (this.functionType == 0) {
  296. outputSize(out);
  297. outputEncode(out);
  298. outputBitsPerSample(out);
  299. outputOrder(out);
  300. outputRange(out);
  301. outputDecode(out);
  302. if (functionDataStream != null) {
  303. out.append("/Length " + (functionDataStream.length() + 1) + "\n");
  304. }
  305. outputFilter(out);
  306. out.append(">>");
  307. if (functionDataStream != null) {
  308. out.append("\nstream\n" + functionDataStream + "\nendstream");
  309. }
  310. } else if (functionType == 2) {
  311. outputRange(out);
  312. outputCZero(out);
  313. outputCOne(out);
  314. outputInterpolationExponentN(out);
  315. out.append(">>");
  316. } else if (functionType == 3) {
  317. outputRange(out);
  318. if (!functions.isEmpty()) {
  319. out.append("/Functions [ ");
  320. for (String f : functionsStrings) {
  321. out.append(f);
  322. out.append(' ');
  323. }
  324. out.append("]\n");
  325. }
  326. outputEncode(out);
  327. out.append("/Bounds ");
  328. if (bounds != null) {
  329. outputDoubles(out, bounds);
  330. } else if (!functions.isEmpty()) {
  331. // if there are n functions,
  332. // there must be n-1 bounds.
  333. // so let each function handle an equal portion
  334. // of the whole. e.g. if there are 4, then [ 0.25 0.25 0.25 ]
  335. int numberOfFunctions = functions.size();
  336. String functionsFraction = PDFNumber.doubleOut(Double.valueOf(1.0 / (numberOfFunctions)));
  337. out.append("[ ");
  338. for (int i = 0; i + 1 < numberOfFunctions; i++) {
  339. out.append(functionsFraction);
  340. out.append(" ");
  341. }
  342. out.append("]");
  343. }
  344. out.append("\n>>");
  345. } else if (functionType == 4) {
  346. outputRange(out);
  347. if (functionDataStream != null) {
  348. out.append("/Length " + (functionDataStream.length() + 1) + "\n");
  349. }
  350. out.append(">>");
  351. if (functionDataStream != null) {
  352. out.append("\nstream\n{ " + functionDataStream + " }\nendstream");
  353. }
  354. }
  355. return out.toString();
  356. }
  357. private void outputDomain(StringBuilder p) {
  358. if (domain != null) {
  359. p.append("/Domain ");
  360. outputDoubles(p, domain);
  361. p.append("\n");
  362. } else {
  363. p.append("/Domain [ 0 1 ]\n");
  364. }
  365. }
  366. private void outputSize(StringBuilder out) {
  367. if (size != null) {
  368. out.append("/Size ");
  369. outputDoubles(out, size);
  370. out.append("\n");
  371. }
  372. }
  373. private void outputBitsPerSample(StringBuilder out) {
  374. out.append("/BitsPerSample " + bitsPerSample + "\n");
  375. }
  376. private void outputOrder(StringBuilder out) {
  377. if (order == 1 || order == 3) {
  378. out.append("\n/Order " + order + "\n");
  379. }
  380. }
  381. private void outputRange(StringBuilder out) {
  382. if (range != null) {
  383. out.append("/Range ");
  384. outputDoubles(out, range);
  385. out.append("\n");
  386. }
  387. }
  388. private void outputEncode(StringBuilder out) {
  389. if (encode != null) {
  390. out.append("/Encode ");
  391. outputDoubles(out, encode);
  392. out.append("\n");
  393. } else {
  394. out.append("/Encode [ ");
  395. int size = functions.size();
  396. for (int i = 0; i < size; i++) {
  397. out.append("0 1 ");
  398. }
  399. out.append("]\n");
  400. }
  401. }
  402. private void outputDecode(StringBuilder out) {
  403. if (decode != null) {
  404. out.append("/Decode ");
  405. outputDoubles(out, decode);
  406. out.append("\n");
  407. }
  408. }
  409. private void outputFilter(StringBuilder out) {
  410. if (filter != null) {
  411. int size = filter.size();
  412. out.append("/Filter ");
  413. if (size == 1) {
  414. out.append("/" + filter.get(0) + "\n");
  415. } else {
  416. out.append("[ ");
  417. for (int i = 0; i < size; i++) {
  418. out.append("/" + filter.get(0) + " ");
  419. }
  420. out.append("]\n");
  421. }
  422. }
  423. }
  424. private void outputCZero(StringBuilder out) {
  425. if (cZero != null) {
  426. out.append("/C0 [ ");
  427. for (float c : cZero) {
  428. out.append(PDFNumber.doubleOut(c));
  429. out.append(" ");
  430. }
  431. out.append("]\n");
  432. }
  433. }
  434. private void outputCOne(StringBuilder out) {
  435. if (cOne != null) {
  436. out.append("/C1 [ ");
  437. for (float c : cOne) {
  438. out.append(PDFNumber.doubleOut(c));
  439. out.append(" ");
  440. }
  441. out.append("]\n");
  442. }
  443. }
  444. private void outputInterpolationExponentN(StringBuilder out) {
  445. out.append("/N ");
  446. out.append(PDFNumber.doubleOut(Double.valueOf(interpolationExponentN)));
  447. out.append("\n");
  448. }
  449. private void outputDoubles(StringBuilder out, List<Double> doubles) {
  450. out.append("[ ");
  451. for (Double d : doubles) {
  452. out.append(PDFNumber.doubleOut(d));
  453. out.append(" ");
  454. }
  455. out.append("]");
  456. }
  457. }