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.

FunctionPattern.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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.render.shading;
  19. import java.util.List;
  20. import org.apache.fop.pdf.PDFNumber;
  21. /**
  22. * A class for writing function objects for different output formats
  23. */
  24. public class FunctionPattern {
  25. private Function function;
  26. /**
  27. * Constructor
  28. * @param function The function from which to write the output
  29. */
  30. public FunctionPattern(Function function) {
  31. this.function = function;
  32. }
  33. /**
  34. * Outputs the function to a byte array
  35. */
  36. public String toWriteableString(List<String> functionsStrings) {
  37. int vectorSize = 0;
  38. int numberOfFunctions = 0;
  39. int tempInt = 0;
  40. StringBuffer p = new StringBuffer(256);
  41. p.append("<< \n/FunctionType " + function.getFunctionType() + " \n");
  42. // FunctionType 0
  43. if (this.function.getFunctionType() == 0) {
  44. if (function.getDomain() != null) {
  45. // DOMAIN
  46. p.append("/Domain [ ");
  47. vectorSize = function.getDomain().size();
  48. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  49. p.append(PDFNumber.doubleOut(function.getDomain().get(tempInt))
  50. + " ");
  51. }
  52. p.append("] \n");
  53. } else {
  54. p.append("/Domain [ 0 1 ] \n");
  55. }
  56. // SIZE
  57. if (function.getSize() != null) {
  58. p.append("/Size [ ");
  59. vectorSize = function.getSize().size();
  60. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  61. p.append(PDFNumber.doubleOut(function.getSize().get(tempInt))
  62. + " ");
  63. }
  64. p.append("] \n");
  65. }
  66. // ENCODE
  67. if (function.getEncode() != null) {
  68. p.append("/Encode [ ");
  69. vectorSize = function.getEncode().size();
  70. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  71. p.append(PDFNumber.doubleOut(function.getEncode().get(tempInt))
  72. + " ");
  73. }
  74. p.append("] \n");
  75. } else {
  76. p.append("/Encode [ ");
  77. vectorSize = function.getFunctions().size();
  78. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  79. p.append("0 1 ");
  80. }
  81. p.append("] \n");
  82. }
  83. // BITSPERSAMPLE
  84. p.append("/BitsPerSample " + function.getBitsPerSample());
  85. // ORDER (optional)
  86. if (function.getOrder() == 1 || function.getOrder() == 3) {
  87. p.append(" \n/Order " + function.getOrder() + " \n");
  88. }
  89. // RANGE
  90. if (function.getRange() != null) {
  91. p.append("/Range [ ");
  92. vectorSize = function.getRange().size();
  93. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  94. p.append(PDFNumber.doubleOut(function.getRange().get(tempInt))
  95. + " ");
  96. }
  97. p.append("] \n");
  98. }
  99. // DECODE
  100. if (function.getDecode() != null) {
  101. p.append("/Decode [ ");
  102. vectorSize = function.getDecode().size();
  103. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  104. p.append(PDFNumber.doubleOut(function.getDecode().get(tempInt))
  105. + " ");
  106. }
  107. p.append("] \n");
  108. }
  109. // LENGTH
  110. if (function.getDataStream() != null) {
  111. p.append("/Length " + (function.getDataStream().length() + 1)
  112. + " \n");
  113. }
  114. // FILTER?
  115. if (function.getFilter() != null) { // if there's a filter
  116. vectorSize = function.getFilter().size();
  117. p.append("/Filter ");
  118. if (vectorSize == 1) {
  119. p.append("/" + (function.getFilter().get(0))
  120. + " \n");
  121. } else {
  122. p.append("[ ");
  123. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  124. p.append("/" + (function.getFilter().get(0))
  125. + " ");
  126. }
  127. p.append("] \n");
  128. }
  129. }
  130. p.append(">>");
  131. // stream representing the function
  132. if (function.getDataStream() != null) {
  133. p.append("\nstream\n" + function.getDataStream()
  134. + "\nendstream");
  135. }
  136. // end of if FunctionType 0
  137. } else if (function.getFunctionType() == 2) {
  138. // DOMAIN
  139. if (function.getDomain() != null) {
  140. p.append("/Domain [ ");
  141. vectorSize = function.getDomain().size();
  142. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  143. p.append(PDFNumber.doubleOut(function.getDomain().get(tempInt))
  144. + " ");
  145. }
  146. p.append("] \n");
  147. } else {
  148. p.append("/Domain [ 0 1 ] \n");
  149. }
  150. // RANGE
  151. if (function.getRange() != null) {
  152. p.append("/Range [ ");
  153. vectorSize = function.getRange().size();
  154. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  155. p.append(PDFNumber.doubleOut(function.getRange().get(tempInt))
  156. + " ");
  157. }
  158. p.append("] \n");
  159. }
  160. // FunctionType, C0, C1, N are required in PDF
  161. // C0
  162. if (function.getCZero() != null) {
  163. p.append("/C0 [ ");
  164. vectorSize = function.getCZero().size();
  165. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  166. p.append(PDFNumber.doubleOut(function.getCZero().get(tempInt))
  167. + " ");
  168. }
  169. p.append("] \n");
  170. }
  171. // C1
  172. if (function.getCOne() != null) {
  173. p.append("/C1 [ ");
  174. vectorSize = function.getCOne().size();
  175. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  176. p.append(PDFNumber.doubleOut(function.getCOne().get(tempInt))
  177. + " ");
  178. }
  179. p.append("] \n");
  180. }
  181. // N: The interpolation Exponent
  182. p.append("/N "
  183. + PDFNumber.doubleOut(Double.valueOf(function.getInterpolationExponentN()))
  184. + " \n");
  185. p.append(">>");
  186. } else if (function.getFunctionType()
  187. == 3) { // fix this up when my eyes uncross
  188. // DOMAIN
  189. if (function.getDomain() != null) {
  190. p.append("/Domain [ ");
  191. vectorSize = function.getDomain().size();
  192. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  193. p.append(PDFNumber.doubleOut(function.getDomain().get(tempInt))
  194. + " ");
  195. }
  196. p.append("] \n");
  197. } else {
  198. p.append("/Domain [ 0 1 ] \n");
  199. }
  200. // RANGE
  201. if (function.getRange() != null) {
  202. p.append("/Range [ ");
  203. vectorSize = function.getRange().size();
  204. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  205. p.append(PDFNumber.doubleOut(function.getRange().get(tempInt))
  206. + " ");
  207. }
  208. p.append("] \n");
  209. }
  210. // FUNCTIONS
  211. if (!function.getFunctions().isEmpty()) {
  212. p.append("/Functions [ ");
  213. numberOfFunctions = function.getFunctions().size();
  214. for (String f : functionsStrings) {
  215. p.append(f);
  216. p.append(' ');
  217. }
  218. p.append("] \n");
  219. }
  220. // ENCODE
  221. if (function.getEncode() != null) {
  222. p.append("/Encode [ ");
  223. vectorSize = function.getEncode().size();
  224. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  225. p.append(PDFNumber.doubleOut(function.getEncode().get(tempInt))
  226. + " ");
  227. }
  228. p.append("] \n");
  229. } else {
  230. p.append("/Encode [ ");
  231. vectorSize = function.getFunctions().size();
  232. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  233. p.append("0 1 ");
  234. }
  235. p.append("] \n");
  236. }
  237. // BOUNDS, required, but can be empty
  238. p.append("/Bounds [ ");
  239. if (function.getBounds() != null) {
  240. vectorSize = function.getBounds().size();
  241. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  242. p.append(PDFNumber.doubleOut(function.getBounds().get(tempInt))
  243. + " ");
  244. }
  245. } else {
  246. if (!function.getFunctions().isEmpty()) {
  247. // if there are n functions,
  248. // there must be n-1 bounds.
  249. // so let each function handle an equal portion
  250. // of the whole. e.g. if there are 4, then [ 0.25 0.25 0.25 ]
  251. String functionsFraction = PDFNumber.doubleOut(Double.valueOf(1.0
  252. / (numberOfFunctions)));
  253. for (tempInt = 0; tempInt + 1 < numberOfFunctions;
  254. tempInt++) {
  255. p.append(functionsFraction + " ");
  256. }
  257. }
  258. }
  259. p.append("]\n>>");
  260. } else if (function.getFunctionType()
  261. == 4) { // fix this up when my eyes uncross
  262. // DOMAIN
  263. if (function.getDomain() != null) {
  264. p.append("/Domain [ ");
  265. vectorSize = function.getDomain().size();
  266. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  267. p.append(PDFNumber.doubleOut(function.getDomain().get(tempInt))
  268. + " ");
  269. }
  270. p.append("] \n");
  271. } else {
  272. p.append("/Domain [ 0 1 ] \n");
  273. }
  274. // RANGE
  275. if (function.getRange() != null) {
  276. p.append("/Range [ ");
  277. vectorSize = function.getRange().size();
  278. for (tempInt = 0; tempInt < vectorSize; tempInt++) {
  279. p.append(PDFNumber.doubleOut(function.getRange().get(tempInt))
  280. + " ");
  281. }
  282. p.append("] \n");
  283. }
  284. // LENGTH
  285. if (function.getDataStream() != null) {
  286. p.append("/Length " + (function.getDataStream().length() + 1)
  287. + " \n");
  288. }
  289. p.append(">>");
  290. // stream representing the function
  291. if (function.getDataStream() != null) {
  292. p.append("\nstream\n{ " + function.getDataStream()
  293. + " }\nendstream");
  294. }
  295. }
  296. return p.toString();
  297. }
  298. }