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