01: /*
02: * (C) Copyright IBM Corp. 1998-2004. All Rights Reserved.
03: *
04: * The program is provided "as is" without any warranty express or
05: * implied, including the warranty of non-infringement and the implied
06: * warranties of merchantibility and fitness for a particular purpose.
07: * IBM will not be liable for any damages suffered by you as a result
08: * of using the Program. In no event will IBM be liable for any
09: * special, indirect or consequential damages or lost profits even if
10: * IBM has been advised of the possibility of their occurrence. IBM
11: * will not be liable for any third party claims against you.
12: */
13: // Requires Java2
14: package com.ibm.richtext.textformat;
15:
16: import java.util.Hashtable;
17:
18: import com.ibm.richtext.textlayout.attributes.AttributeMap;
19:
20: ///*JDK12IMPORTS
21: import java.awt.font.FontRenderContext;
22: import java.awt.font.TextLayout;
23:
24: //JDK12IMPORTS*/
25:
26: /*JDK11IMPORTS
27: import com.ibm.richtext.textlayout.FontRenderContext;
28: import com.ibm.richtext.textlayout.TextLayout;
29: JDK11IMPORTS*/
30:
31: /**
32: * This class is used by the Formatter to estimate the height
33: * of characters in a particular style.
34: */
35: final class DefaultCharacterMetric {
36:
37: static final String COPYRIGHT = "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
38:
39: final class Metric {
40:
41: private float fAscent;
42: private float fDescent;
43: private float fLeading;
44:
45: private Metric(float ascent, float descent, float leading) {
46:
47: fAscent = ascent;
48: fDescent = descent;
49: fLeading = leading;
50: }
51:
52: public int getAscent() {
53: return (int) Math.ceil(fAscent);
54: }
55:
56: public int getDescent() {
57: return (int) Math.ceil(fDescent);
58: }
59:
60: public int getLeading() {
61: return (int) Math.ceil(fLeading);
62: }
63: }
64:
65: private final Hashtable fCache = new Hashtable();
66: private/*final*/FontResolver fResolver;
67: private/*final*/FontRenderContext fFrc;
68:
69: public DefaultCharacterMetric(FontResolver resolver,
70: FontRenderContext frc) {
71:
72: fResolver = resolver;
73: fFrc = frc;
74: }
75:
76: /**
77: * Get a DefaultCharacterMetric instance for the given style. The
78: * style is first resolved with FontResolver.
79: */
80: public Metric getMetricForStyle(AttributeMap style) {
81:
82: style = fResolver.applyFont(style);
83: Metric metric = (Metric) fCache.get(style);
84: if (metric == null) {
85: TextLayout layout = new TextLayout(" ", style, fFrc);
86: metric = new Metric(layout.getAscent(),
87: layout.getDescent(), layout.getLeading());
88: fCache.put(style, metric);
89: }
90: return metric;
91: }
92: }
|