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.

FunctionDelegate.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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.shading;
  18. import java.util.List;
  19. public class FunctionDelegate implements Function {
  20. private Function parentFunction;
  21. /**
  22. * Required: The Type of function (0,2,3,4) default is 0.
  23. */
  24. protected int functionType = 0; // Default
  25. /**
  26. * Required: 2 * m Array of Double numbers which are possible inputs to the function
  27. */
  28. protected List<Double> domain = null;
  29. /**
  30. * Required: 2 * n Array of Double numbers which are possible outputs to the function
  31. */
  32. protected List<Double> range = null;
  33. /* ********************TYPE 0***************************** */
  34. // FunctionType 0 specific function guts
  35. /**
  36. * Required: Array containing the Integer size of the Domain and Range, respectively.
  37. * Note: This is really more like two seperate integers, sizeDomain, and sizeRange,
  38. * but since they're expressed as an array in PDF, my implementation reflects that.
  39. */
  40. protected List<Double> size = null;
  41. /**
  42. * Required for Type 0: Number of Bits used to represent each sample value.
  43. * Limited to 1,2,4,8,12,16,24, or 32
  44. */
  45. protected int bitsPerSample = 1;
  46. /**
  47. * Optional for Type 0: order of interpolation between samples.
  48. * Limited to linear (1) or cubic (3). Default is 1
  49. */
  50. protected int order = 1;
  51. /**
  52. * Optional for Type 0: A 2 * m array of Doubles which provides a
  53. * linear mapping of input values to the domain.
  54. *
  55. * Required for Type 3: A 2 * k array of Doubles that, taken
  56. * in pairs, map each subset of the domain defined by Domain
  57. * and the Bounds array to the domain of the corresponding function.
  58. * Should be two values per function, usually (0,1),
  59. * as in [0 1 0 1] for 2 functions.
  60. */
  61. protected List<Double> encode = null;
  62. /**
  63. * Optional for Type 0: A 2 * n array of Doubles which provides
  64. * a linear mapping of sample values to the range. Defaults to Range.
  65. */
  66. protected List<Double> decode = null;
  67. /**
  68. * Optional For Type 0: A stream of sample values
  69. */
  70. /**
  71. * Required For Type 4: Postscript Calculator function
  72. * composed of arithmetic, boolean, and stack operators + boolean constants
  73. */
  74. protected StringBuffer functionDataStream = null;
  75. /**
  76. * Required (possibly) For Type 0: A vector of Strings for the
  77. * various filters to be used to decode the stream.
  78. * These are how the string is compressed. Flate, LZW, etc.
  79. */
  80. protected List<String> filter = null;
  81. /* *************************TYPE 2************************** */
  82. /**
  83. * Required For Type 2: An Array of n Doubles defining
  84. * the function result when x=0. Default is [0].
  85. */
  86. protected List<Double> cZero = null;
  87. /**
  88. * Required For Type 2: An Array of n Doubles defining
  89. * the function result when x=1. Default is [1].
  90. */
  91. protected List<Double> cOne = null;
  92. /**
  93. * Required for Type 2: The interpolation exponent.
  94. * Each value x will return n results.
  95. * Must be greater than 0.
  96. */
  97. protected double interpolationExponentN = 1;
  98. /* *************************TYPE 3************************** */
  99. /**
  100. * Required for Type 3: An vector of PDFFunctions which
  101. * form an array of k single input functions making up
  102. * the stitching function.
  103. */
  104. protected List<Function> functions = null;
  105. /**
  106. * Optional for Type 3: An array of (k-1) Doubles that,
  107. * in combination with Domain, define the intervals to which
  108. * each function from the Functions array apply. Bounds
  109. * elements must be in order of increasing magnitude,
  110. * and each value must be within the value of Domain.
  111. * k is the number of functions.
  112. * If you pass null, it will output (1/k) in an array of k-1 elements.
  113. * This makes each function responsible for an equal amount of the stitching function.
  114. * It makes the gradient even.
  115. */
  116. protected List<Double> bounds = null;
  117. /**
  118. * create an complete Function object of Type 0, A Sampled function.
  119. *
  120. * Use null for an optional object parameter if you choose not to use it.
  121. * For optional int parameters, pass the default.
  122. *
  123. * @param theDomain List objects of Double objects.
  124. * This is the domain of the function.
  125. * See page 264 of the PDF 1.3 Spec.
  126. * @param theRange List objects of Double objects.
  127. * This is the Range of the function.
  128. * See page 264 of the PDF 1.3 Spec.
  129. * @param theSize A List object of Integer objects.
  130. * This is the number of samples in each input dimension.
  131. * I can't imagine there being more or less than two input dimensions,
  132. * so maybe this should be an array of length 2.
  133. *
  134. * See page 265 of the PDF 1.3 Spec.
  135. * @param theBitsPerSample An int specifying the number of bits
  136. used to represent each sample value.
  137. * Limited to 1,2,4,8,12,16,24 or 32.
  138. * See page 265 of the 1.3 PDF Spec.
  139. * @param theOrder The order of interpolation between samples. Default is 1 (one). Limited
  140. * to 1 (one) or 3, which means linear or cubic-spline interpolation.
  141. *
  142. * This attribute is optional.
  143. *
  144. * See page 265 in the PDF 1.3 spec.
  145. * @param theEncode List objects of Double objects.
  146. * This is the linear mapping of input values intop the domain
  147. * of the function's sample table. Default is hard to represent in
  148. * ascii, but basically [0 (Size0 1) 0 (Size1 1)...].
  149. * This attribute is optional.
  150. *
  151. * See page 265 in the PDF 1.3 spec.
  152. * @param theDecode List objects of Double objects.
  153. * This is a linear mapping of sample values into the range.
  154. * The default is just the range.
  155. *
  156. * This attribute is optional.
  157. * Read about it on page 265 of the PDF 1.3 spec.
  158. * @param theFunctionDataStream The sample values that specify
  159. * the function are provided in a stream.
  160. *
  161. * This is optional, but is almost always used.
  162. *
  163. * Page 265 of the PDF 1.3 spec has more.
  164. * @param theFilter This is a vector of String objects which are the various filters that
  165. * have are to be applied to the stream to make sense of it. Order matters,
  166. * so watch out.
  167. *
  168. * This is not documented in the Function section of the PDF 1.3 spec,
  169. * it was deduced from samples that this is sometimes used, even if we may never
  170. * use it in FOP. It is added for completeness sake.
  171. * @param theFunctionType This is the type of function (0,2,3, or 4).
  172. * It should be 0 as this is the constructor for sampled functions.
  173. */
  174. public FunctionDelegate(Function parentFunction, int theFunctionType, List<Double> theDomain,
  175. List<Double> theRange, List<Double> theSize, int theBitsPerSample,
  176. int theOrder, List<Double> theEncode, List<Double> theDecode,
  177. StringBuffer theFunctionDataStream, List<String> theFilter) {
  178. this.parentFunction = parentFunction;
  179. this.functionType = 0; // dang well better be 0;
  180. this.size = theSize;
  181. this.bitsPerSample = theBitsPerSample;
  182. this.order = theOrder; // int
  183. this.encode = theEncode; // vector of int
  184. this.decode = theDecode; // vector of int
  185. this.functionDataStream = theFunctionDataStream;
  186. this.filter = theFilter; // vector of Strings
  187. // the domain and range are actually two dimensional arrays.
  188. // so if there's not an even number of items, bad stuff
  189. // happens.
  190. this.domain = theDomain;
  191. this.range = theRange;
  192. }
  193. /**
  194. * create an complete Function object of Type 2, an Exponential Interpolation function.
  195. *
  196. * Use null for an optional object parameter if you choose not to use it.
  197. * For optional int parameters, pass the default.
  198. *
  199. * @param theDomain List objects of Double objects.
  200. * This is the domain of the function.
  201. * See page 264 of the PDF 1.3 Spec.
  202. * @param theRange List of Doubles that is the Range of the function.
  203. * See page 264 of the PDF 1.3 Spec.
  204. * @param theCZero This is a vector of Double objects which defines the function result
  205. * when x=0.
  206. *
  207. * This attribute is optional.
  208. * It's described on page 268 of the PDF 1.3 spec.
  209. * @param theCOne This is a vector of Double objects which defines the function result
  210. * when x=1.
  211. *
  212. * This attribute is optional.
  213. * It's described on page 268 of the PDF 1.3 spec.
  214. * @param theInterpolationExponentN This is the inerpolation exponent.
  215. *
  216. * This attribute is required.
  217. * PDF Spec page 268
  218. * @param theFunctionType The type of the function, which should be 2.
  219. */
  220. public FunctionDelegate(Function parentFunction, int theFunctionType, List<Double> theDomain,
  221. List<Double> theRange, List<Double> theCZero, List<Double> theCOne,
  222. double theInterpolationExponentN) {
  223. this.parentFunction = parentFunction;
  224. this.functionType = 2; // dang well better be 2;
  225. this.cZero = theCZero;
  226. this.cOne = theCOne;
  227. this.interpolationExponentN = theInterpolationExponentN;
  228. this.domain = theDomain;
  229. this.range = theRange;
  230. }
  231. /**
  232. * create an complete Function object of Type 3, a Stitching function.
  233. *
  234. * Use null for an optional object parameter if you choose not to use it.
  235. * For optional int parameters, pass the default.
  236. *
  237. * @param theDomain List objects of Double objects.
  238. * This is the domain of the function.
  239. * See page 264 of the PDF 1.3 Spec.
  240. * @param theRange List objects of Double objects.
  241. * This is the Range of the function.
  242. * See page 264 of the PDF 1.3 Spec.
  243. * @param theFunctions A List of the PDFFunction objects that the stitching function stitches.
  244. *
  245. * This attributed is required.
  246. * It is described on page 269 of the PDF spec.
  247. * @param theBounds This is a vector of Doubles representing the numbers that,
  248. * in conjunction with Domain define the intervals to which each function from
  249. * the 'functions' object applies. It must be in order of increasing magnitude,
  250. * and each must be within Domain.
  251. *
  252. * It basically sets how much of the gradient each function handles.
  253. *
  254. * This attributed is required.
  255. * It's described on page 269 of the PDF 1.3 spec.
  256. * @param theEncode List objects of Double objects.
  257. * This is the linear mapping of input values intop the domain
  258. * of the function's sample table. Default is hard to represent in
  259. * ascii, but basically [0 (Size0 1) 0 (Size1 1)...].
  260. * This attribute is required.
  261. *
  262. * See page 270 in the PDF 1.3 spec.
  263. * @param theFunctionType This is the function type. It should be 3,
  264. * for a stitching function.
  265. */
  266. public FunctionDelegate(Function parentFunction, int theFunctionType, List<Double> theDomain,
  267. List<Double> theRange, List<Function> theFunctions,
  268. List<Double> theBounds, List<Double> theEncode) {
  269. this.parentFunction = parentFunction;
  270. this.functionType = 3; // dang well better be 3;
  271. this.functions = theFunctions;
  272. this.bounds = theBounds;
  273. this.encode = theEncode;
  274. this.domain = theDomain;
  275. this.range = theRange;
  276. }
  277. /**
  278. * create an complete Function object of Type 4, a postscript calculator function.
  279. *
  280. * Use null for an optional object parameter if you choose not to use it.
  281. * For optional int parameters, pass the default.
  282. *
  283. * @param theDomain List object of Double objects.
  284. * This is the domain of the function.
  285. * See page 264 of the PDF 1.3 Spec.
  286. * @param theRange List object of Double objects.
  287. * This is the Range of the function.
  288. * See page 264 of the PDF 1.3 Spec.
  289. * @param theFunctionDataStream This is a stream of arithmetic,
  290. * boolean, and stack operators and boolean constants.
  291. * I end up enclosing it in the '{' and '}' braces for you, so don't do it
  292. * yourself.
  293. *
  294. * This attribute is required.
  295. * It's described on page 269 of the PDF 1.3 spec.
  296. * @param theFunctionType The type of function which should be 4, as this is
  297. * a Postscript calculator function
  298. */
  299. public FunctionDelegate(Function parentFunction, int theFunctionType, List<Double> theDomain,
  300. List<Double> theRange, StringBuffer theFunctionDataStream) {
  301. this.parentFunction = parentFunction;
  302. this.functionType = 4; // dang well better be 4;
  303. this.functionDataStream = theFunctionDataStream;
  304. this.domain = theDomain;
  305. this.range = theRange;
  306. }
  307. /**
  308. * Gets the function type
  309. */
  310. public int getFunctionType() {
  311. return functionType;
  312. }
  313. /**
  314. * Gets the function bounds
  315. */
  316. public List<Double> getBounds() {
  317. return bounds;
  318. }
  319. /**
  320. * The function domain
  321. */
  322. public List<Double> getDomain() {
  323. return domain;
  324. }
  325. /**
  326. * The function size
  327. */
  328. public List<Double> getSize() {
  329. return size;
  330. }
  331. /**
  332. * Gets the function encoding
  333. */
  334. public List<Double> getEncode() {
  335. return encode;
  336. }
  337. /**
  338. * Gets the sub-functions
  339. */
  340. public List<Function> getFunctions() {
  341. return functions;
  342. }
  343. /**
  344. * Gets the function filter
  345. */
  346. public List<String> getFilter() {
  347. return filter;
  348. }
  349. /**
  350. * Gets the bits per sample of the function
  351. */
  352. public int getBitsPerSample() {
  353. return bitsPerSample;
  354. }
  355. /**
  356. * Gets the interpolation exponent of the function
  357. */
  358. public double getInterpolationExponentN() {
  359. return interpolationExponentN;
  360. }
  361. /**
  362. * Gets the function order
  363. */
  364. public int getOrder() {
  365. return order;
  366. }
  367. /**
  368. * Gets the function range
  369. */
  370. public List<Double> getRange() {
  371. return range;
  372. }
  373. /**
  374. * Gets the function decoding
  375. */
  376. public List<Double> getDecode() {
  377. return decode;
  378. }
  379. /**
  380. * Gets the function data stream
  381. */
  382. public StringBuffer getDataStream() {
  383. return functionDataStream;
  384. }
  385. /**
  386. * Gets the function C0 value (color for gradient)
  387. */
  388. public List<Double> getCZero() {
  389. return cZero;
  390. }
  391. /**
  392. * Gets the function C1 value (color for gradient)
  393. */
  394. public List<Double> getCOne() {
  395. return cOne;
  396. }
  397. public byte[] toByteString() {
  398. return parentFunction.toByteString();
  399. }
  400. }