File: CSSValueResolverUtility.java

package info (click to toggle)
liblayout 0.2.10-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 5,332 kB
  • sloc: java: 51,125; xml: 138; makefile: 17
file content (459 lines) | stat: -rw-r--r-- 13,001 bytes parent folder | download | duplicates (4)
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
/**
 * ===========================================
 * LibLayout : a free Java layouting library
 * ===========================================
 *
 * Project Info:  http://reporting.pentaho.org/liblayout/
 *
 * (C) Copyright 2006-2007, by Pentaho Corporation and Contributors.
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
 * in the United States and other countries.]
 *
 * ------------
 * $Id: CSSValueResolverUtility.java 6489 2008-11-28 14:53:40Z tmorgner $
 * ------------
 * (C) Copyright 2006-2007, by Pentaho Corporation.
 */
package org.jfree.layouting.layouter.style;

import org.jfree.layouting.input.style.values.CSSNumericType;
import org.jfree.layouting.input.style.values.CSSNumericValue;
import org.jfree.layouting.input.style.values.CSSStringType;
import org.jfree.layouting.input.style.values.CSSStringValue;
import org.jfree.layouting.input.style.values.CSSValue;
import org.jfree.layouting.layouter.context.FontSpecification;
import org.jfree.layouting.layouter.context.LayoutContext;
import org.jfree.layouting.output.OutputProcessorFeature;
import org.jfree.layouting.output.OutputProcessorMetaData;
import org.jfree.layouting.util.geom.StrictGeomUtility;
import org.pentaho.reporting.libraries.fonts.registry.FontMetrics;

/**
 * Creation-Date: 15.12.2005, 11:29:22
 *
 * @author Thomas Morgner
 */
public class CSSValueResolverUtility
{
  public static final double DEFAULT_X_HEIGHT_FACTOR = 0.58;

  public static boolean isAbsoluteValue(final CSSNumericValue value)
  {
    if (CSSNumericType.PT.equals(value.getType()))
    {
      return true;
    }
    if (CSSNumericType.PC.equals(value.getType()))
    {
      return true;
    }
    if (CSSNumericType.INCH.equals(value.getType()))
    {
      return true;
    }
    if (CSSNumericType.CM.equals(value.getType()))
    {
      return true;
    }
    if (CSSNumericType.MM.equals(value.getType()))
    {
      return true;
    }
    // PX is relative to the device, so during a layouting process, it can
    // be considered absolute
    return CSSNumericType.PX.equals(value.getType());
  }

  public static boolean isLengthValue(final CSSNumericValue value)
  {
    if (CSSNumericType.PT.equals(value.getType()))
    {
      return true;
    }
    if (CSSNumericType.PC.equals(value.getType()))
    {
      return true;
    }
    if (CSSNumericType.INCH.equals(value.getType()))
    {
      return true;
    }
    if (CSSNumericType.CM.equals(value.getType()))
    {
      return true;
    }
    if (CSSNumericType.MM.equals(value.getType()))
    {
      return true;
    }
    if (CSSNumericType.EM.equals(value.getType()))
    {
      return true;
    }
    if (CSSNumericType.EX.equals(value.getType()))
    {
      return true;
    }
    // PX is relative to the device, so during a layouting process, it can
    // be considered absolute
    return CSSNumericType.PX.equals(value.getType());
  }

  public static double convertLengthToDouble(final CSSValue rawValue)
  {
    return convertLengthToDouble(rawValue, null, null);
  }

  /**
   * Returns the length in point as a double primitive value.
   * Be aware that using double-values is not very accurate.
   *
   * @param rawValue
   * @param context
   * @param metaData
   * @return
   */
  public static strictfp double convertLengthToDouble(final CSSValue rawValue,
                                                      final LayoutContext context,
                                                      final OutputProcessorMetaData metaData)
  {
    if (rawValue instanceof CSSNumericValue == false)
    {
      return 0;
    }

    final CSSNumericValue value = (CSSNumericValue) rawValue;
    if (CSSNumericType.PT.equals(value.getType()))
    {
      return value.getValue();
    }
    if (CSSNumericType.PC.equals(value.getType()))
    {
      return (value.getValue() / 12.0d);
    }
    if (CSSNumericType.INCH.equals(value.getType()))
    {
      return (value.getValue() / 72.0d);
    }
    if (CSSNumericType.CM.equals(value.getType()))
    {
      return ((value.getValue() * 100 * 72.0d) / 254.0d);
    }
    if (CSSNumericType.MM.equals(value.getType()))
    {
      return ((value.getValue() * 10 * 72.0d) / 254.0d);
    }

    if (CSSNumericType.PX.equals(value.getType()))
    {
      final int pixelPerInch;
      if (metaData != null)
      {
        pixelPerInch = (int) metaData.getNumericFeatureValue(OutputProcessorFeature.DEVICE_RESOLUTION);
      }
      else
      {
        pixelPerInch = 96;
      }
      if (pixelPerInch <= 0)
      {
        // we assume 72 pixel per inch ...
        return value.getValue();
      }
      return value.getValue() * 72d / pixelPerInch;
    }

    if (metaData == null)
    {
      return 0;
    }

    if (context != null)
    {
      if (CSSNumericType.EM.equals(value.getType()))
      {
        final FontSpecification fspec =
            context.getFontSpecification();
        final double fontSize = fspec.getFontSize();
        return (fontSize * value.getValue());
      }
      if (CSSNumericType.EX.equals(value.getType()))
      {
        final FontSpecification fspec =
            context.getFontSpecification();
        final FontMetrics fontMetrics = metaData.getFontMetrics(fspec);
        if (fontMetrics == null)
        {
          final long fontSize = (long) (fspec.getFontSize() * DEFAULT_X_HEIGHT_FACTOR);
          return StrictGeomUtility.toExternalValue((long) (value.getValue() * fontSize));
        }
        else
        {
          return StrictGeomUtility.toExternalValue((long) (value.getValue() * fontMetrics.getXHeight()));
        }
      }
    }
    return 0;
  }

  /**
   * Returns the length in point as a double primitive value.
   *
   * @param rawValue
   * @param context
   * @param metaData
   * @return
   */
  public static strictfp long convertLengthToLong(final CSSValue rawValue,
                                                  final LayoutContext context,
                                                  final OutputProcessorMetaData metaData)
  {
    if (rawValue instanceof CSSNumericValue == false)
    {
      return 0;
    }

    final CSSNumericValue value = (CSSNumericValue) rawValue;
    final long internal = StrictGeomUtility.toInternalValue(value.getValue());
    if (CSSNumericType.PT.equals(value.getType()))
    {
      return internal;
    }
    if (CSSNumericType.PC.equals(value.getType()))
    {
      return (internal / 12);
    }
    if (CSSNumericType.INCH.equals(value.getType()))
    {
      return (internal / 72);
    }
    if (CSSNumericType.CM.equals(value.getType()))
    {
      return (internal * 100 * 72 / 254);
    }
    if (CSSNumericType.MM.equals(value.getType()))
    {
      return (internal * 10 * 72 / 254);
    }

    if (CSSNumericType.PX.equals(value.getType()))
    {
      final int pixelPerInch;
      if (metaData != null)
      {
        pixelPerInch = (int) metaData.getNumericFeatureValue(OutputProcessorFeature.DEVICE_RESOLUTION);
      }
      else
      {
        pixelPerInch = 96;
      }
      if (pixelPerInch <= 0)
      {
        // we assume 72 pixel per inch ...
        return internal;
      }
      return internal * 72 / pixelPerInch;
    }

    if (metaData == null)
    {
      return 0;
    }

    if (context != null)
    {
      if (CSSNumericType.EM.equals(value.getType()))
      {
        final FontSpecification fspec =
            context.getFontSpecification();
        final double fontSize = fspec.getFontSize();
        return (long) (fontSize * internal);
      }
      if (CSSNumericType.EX.equals(value.getType()))
      {
        final FontSpecification fspec =
            context.getFontSpecification();
        final FontMetrics fontMetrics = metaData.getFontMetrics(fspec);
        if (fontMetrics == null)
        {
          final long fontSize = (long) (fspec.getFontSize() * DEFAULT_X_HEIGHT_FACTOR);
          return StrictGeomUtility.multiply (internal, fontSize);
        }
        else
        {
          return StrictGeomUtility.multiply (internal, fontMetrics.getXHeight());
        }
      }
    }
    return 0;
  }

  public static CSSNumericValue convertLength(final CSSValue rawValue,
                                              final LayoutContext context,
                                              final OutputProcessorMetaData metaData)
  {
    return CSSNumericValue.createValue(CSSNumericType.PT,
        convertLengthToDouble(rawValue, context, metaData));
  }

  public static CSSNumericValue getLength(final CSSValue value)
  {
    if (value instanceof CSSNumericValue == false)
    {
      return null;
    }

    final CSSNumericValue nval = (CSSNumericValue) value;
    if (isNumericType(CSSNumericType.PERCENTAGE, nval))
    {
      return null;
    }

    return nval;
  }

  private static boolean isNumericType(final CSSNumericType type,
                                       final CSSValue value)
  {
    if (value instanceof CSSNumericValue == false)
    {
      return false;
    }
    final CSSNumericValue nval = (CSSNumericValue) value;
    return nval.getType().equals(type);
  }


  public static CSSNumericValue getLength(final CSSValue value,
                                          final CSSNumericValue percentageBase)
  {
    if (value instanceof CSSNumericValue == false)
    {
      return null;
    }

    final CSSNumericValue nval = (CSSNumericValue) value;
    if (isNumericType(CSSNumericType.PERCENTAGE, nval))
    {
      if (percentageBase == null)
      {
        return null;
      }

      final double percentage = nval.getValue();
      return CSSNumericValue.createValue(percentageBase.getType(),
          percentageBase.getValue() * percentage / 100.0d);
    }

    return nval;
  }

//
//  public static CSSNumericValue getParentFontSize(final LayoutElement node)
//  {
//    final LayoutElement parent = node.getParent();
//    if (parent == null)
//    {
//      return null;
//    }
//    final long fs =
//        parent.getLayoutContext().getFontSpecification().getFontSize();
//    return CSSNumericValue.createInternalValue(CSSNumericType.PT, fs);
//  }
//

  public static boolean isURI(final CSSValue value)
  {
    if (value instanceof CSSStringValue == false)
    {
      return false;
    }
    final CSSStringValue sval = (CSSStringValue) value;
    return sval.getType().equals(CSSStringType.URI);
  }

  public static double getNumericValue(final CSSValue value,
                                       final double defaultValue)
  {
    if (value instanceof CSSNumericValue)
    {
      final CSSNumericValue nval = (CSSNumericValue) value;
      if (CSSNumericType.NUMBER.equals(nval.getType()))
      {
        return nval.getValue();
      }
    }
    return defaultValue;
  }


  public static CSSNumericValue convertLength(final CSSNumericValue value, final CSSNumericType type)
  {
    if (type == CSSNumericType.NUMBER || type == CSSNumericType.PERCENTAGE || type == CSSNumericType.DEG)
    {
      throw new IllegalArgumentException();
    }
    final CSSNumericType valueType = value.getType();
    if (valueType == CSSNumericType.NUMBER || valueType == CSSNumericType.PERCENTAGE || valueType == CSSNumericType.DEG)
    {
      throw new IllegalArgumentException();
    }
    if (valueType == type)
    {
      return value;
    }

    final double targetFactor = getFactor(type);
    final double sourceFactor = getFactor(valueType);
    final double unitvalue = value.getValue() * targetFactor / sourceFactor;
    return CSSNumericValue.createValue(type, unitvalue);
  }

  private static double getFactor (final CSSNumericType type)
  {
    for (int i = 0; i < types.length; i++)
    {
      final CSSNumericType numericType = types[i];
      if (type == numericType)
      {
        return vals[i];
      }
    }
    throw new IllegalArgumentException();
  }

  private static CSSNumericType[] types = new CSSNumericType[]{
      CSSNumericType.CM,
      CSSNumericType.MM,
      CSSNumericType.PX,
      CSSNumericType.PT,
      CSSNumericType.PC,
      CSSNumericType.INCH
  };

  private static double[] vals = new double[]{
      254, // CM
      2540, //MM
      9600, // PX
      7200, // PT
      600, // PC
      100 // Inch
  };
  
  private CSSValueResolverUtility()
  {
  }
}