|
@@ -1,191 +1,192 @@ |
1 |
|
- |
// This Source Code is in the Public Domain per: http://unlicense.org |
2 |
|
- |
package org.litesoft.GWT.client; |
3 |
|
- |
|
4 |
|
- |
import org.litesoft.GWT.client.widgets.nonpublic.*; |
5 |
|
- |
import org.litesoft.commonfoundation.html.*; |
6 |
|
- |
import org.litesoft.commonfoundation.typeutils.*; |
7 |
|
- |
|
8 |
|
- |
import com.google.gwt.user.client.ui.*; |
9 |
|
- |
|
10 |
|
- |
import java.util.*; |
11 |
|
- |
|
12 |
|
- |
public class FontSizer { |
13 |
|
- |
public static final String ASCII = " ?!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; |
14 |
|
- |
|
15 |
|
- |
public static FontSizer get( String... pStyles ) { |
16 |
|
- |
if ( null != (pStyles = Strings.noEmpty( pStyles )) ) { |
17 |
|
- |
if ( pStyles.length > 1 ) { |
18 |
|
- |
return getComposite( pStyles ); |
19 |
|
- |
} |
20 |
|
- |
String zStyle = Strings.noEmpty( pStyles[0] ); |
21 |
|
- |
if ( zStyle != null ) { |
22 |
|
- |
return getSingle( zStyle ); |
23 |
|
- |
} |
24 |
|
- |
} |
25 |
|
- |
throw new IllegalStateException( "FontSizer requires at least one Style" ); |
26 |
|
- |
} |
27 |
|
- |
|
28 |
|
- |
public Integer EMs( int pChars ) { |
29 |
|
- |
Integer zWidth = getAverageCharacterWidth(); |
30 |
|
- |
return (zWidth != null) ? (pChars * zWidth) : null; |
31 |
|
- |
} |
32 |
|
- |
|
33 |
|
- |
public Integer EMsPlus( int pChars, int pPixels ) { |
34 |
|
- |
Integer zWidth = getAverageCharacterWidth(); |
35 |
|
- |
return (zWidth != null) ? ((pChars * zWidth) + pPixels) : null; |
36 |
|
- |
} |
37 |
|
- |
|
38 |
|
- |
public Integer EMsLess( int pChars, int pPixels ) { |
39 |
|
- |
return EMsPlus( pChars, -pPixels ); |
40 |
|
- |
} |
41 |
|
- |
|
42 |
|
- |
public Integer getAverageCharacterWidth() { |
43 |
|
- |
return mSizer.getAverageCharacterWidth(); |
44 |
|
- |
} |
45 |
|
- |
|
46 |
|
- |
private interface Sizer { |
47 |
|
- |
public Integer getAverageCharacterWidth(); |
48 |
|
- |
} |
49 |
|
- |
|
50 |
|
- |
private static class NullSizer implements Sizer { |
51 |
|
- |
public static final Sizer INSTANCE = new NullSizer(); |
52 |
|
- |
|
53 |
|
- |
@Override |
54 |
|
- |
public Integer getAverageCharacterWidth() { |
55 |
|
- |
return 0; |
56 |
|
- |
} |
57 |
|
- |
} |
58 |
|
- |
|
59 |
|
- |
private static class Composite implements Sizer { |
60 |
|
- |
private Sizer[] mSizers; |
61 |
|
- |
|
62 |
|
- |
private Integer mCachedResults = null; |
63 |
|
- |
|
64 |
|
- |
private Composite( Sizer[] pSizers ) { |
65 |
|
- |
mSizers = pSizers; |
66 |
|
- |
} |
67 |
|
- |
|
68 |
|
- |
@Override |
69 |
|
- |
public Integer getAverageCharacterWidth() { |
70 |
|
- |
if ( mCachedResults == null ) { |
71 |
|
- |
int zMax = 4; |
72 |
|
- |
for ( Sizer zSizer : mSizers ) { |
73 |
|
- |
Integer zWidth = zSizer.getAverageCharacterWidth(); |
74 |
|
- |
if ( zWidth == null ) { |
75 |
|
- |
return null; |
76 |
|
- |
} |
77 |
|
- |
zMax = Math.max( zMax, zWidth ); |
78 |
|
- |
} |
79 |
|
- |
mCachedResults = zMax; |
80 |
|
- |
} |
81 |
|
- |
return mCachedResults; |
82 |
|
- |
} |
83 |
|
- |
} |
84 |
|
- |
|
85 |
|
- |
private static final class MyLabel extends HTML implements Sizer { |
86 |
|
- |
private static final String HTML_ASCII = HTMLize.escapeNoWrap( ASCII ); |
87 |
|
- |
|
88 |
|
- |
private Integer mCachedResults = null; |
89 |
|
- |
|
90 |
|
- |
private MyLabel( String pStyle ) { |
91 |
|
- |
super( createHTML( pStyle ) ); |
92 |
|
- |
|
93 |
|
- |
CommonElementHelper.setOverflowClass( getElement(), true ); |
94 |
|
- |
setWidth( "4" ); |
95 |
|
- |
RootPanel.get().add( this ); |
96 |
|
- |
} |
97 |
|
- |
|
98 |
|
- |
private static String createHTML( String pStyle ) { |
99 |
|
- |
return "<table border='0' cellSpacing='0' cellPadding='0'><tbody><tr><td class='" + pStyle + "' style='white-space:nowrap;'>" + // |
100 |
|
- |
HTML_ASCII + "</td></tr></tbody></table>"; |
101 |
|
- |
} |
102 |
|
- |
|
103 |
|
- |
@Override |
104 |
|
- |
public Integer getAverageCharacterWidth() { |
105 |
|
- |
if ( mCachedResults == null ) { |
106 |
|
- |
if ( isAttached() ) { |
107 |
|
- |
int zWidth = getElement().getFirstChildElement().getOffsetWidth(); |
108 |
|
- |
if ( ASCII.length() <= zWidth ) { |
109 |
|
- |
mCachedResults = zWidth / ASCII.length(); |
110 |
|
- |
} else { |
111 |
|
- |
System.out.println( "FontSizer$MyLabel.getAverageCharacterWidth: " + zWidth ); |
112 |
|
- |
} |
113 |
|
- |
} |
114 |
|
- |
} |
115 |
|
- |
return mCachedResults; |
116 |
|
- |
} |
117 |
|
- |
} |
118 |
|
- |
|
119 |
|
- |
private Sizer mSizer; |
120 |
|
- |
|
121 |
|
- |
private FontSizer( Sizer pSizer ) { |
122 |
|
- |
mSizer = pSizer; |
123 |
|
- |
} |
124 |
|
- |
|
125 |
|
- |
private static class CompositeKey { |
126 |
|
- |
private String[] mStyles; |
127 |
|
- |
|
128 |
|
- |
private CompositeKey( String[] pStyles ) { |
129 |
|
- |
mStyles = pStyles; |
130 |
|
- |
} |
131 |
|
- |
|
132 |
|
- |
@Override |
133 |
|
- |
public int hashCode() { |
134 |
|
- |
return Arrays.hashCode( mStyles ); |
135 |
|
- |
} |
136 |
|
- |
|
137 |
|
- |
@Override |
138 |
|
- |
public boolean equals( Object o ) { |
139 |
|
- |
return (this == o) || // |
140 |
|
- |
((o != null) && (getClass() == o.getClass()) && // |
141 |
|
- |
Arrays.equals( mStyles, ((CompositeKey) o).mStyles )); |
142 |
|
- |
} |
143 |
|
- |
} |
144 |
|
- |
|
145 |
|
- |
private static Map<String, FontSizer> STYLE_INSTANCES = new HashMap<String, FontSizer>(); |
146 |
|
- |
|
147 |
|
- |
private static FontSizer getSingle( String pStyle ) { |
148 |
|
- |
FontSizer zFontSizer = STYLE_INSTANCES.get( pStyle ); |
149 |
|
- |
if ( zFontSizer == null ) { |
150 |
|
- |
STYLE_INSTANCES.put( pStyle, zFontSizer = new FontSizer( getSizer( pStyle ) ) ); |
151 |
|
- |
} |
152 |
|
- |
return zFontSizer; |
153 |
|
- |
} |
154 |
|
- |
|
155 |
|
- |
private static Map<CompositeKey, FontSizer> ARRAY_INSTANCES = new HashMap<CompositeKey, FontSizer>(); |
156 |
|
- |
|
157 |
|
- |
private static FontSizer getComposite( String[] pStyles ) { |
158 |
|
- |
CompositeKey zKey = new CompositeKey( pStyles ); |
159 |
|
- |
FontSizer zFontSizer = ARRAY_INSTANCES.get( zKey ); |
160 |
|
- |
if ( zFontSizer == null ) { |
161 |
|
- |
ARRAY_INSTANCES.put( zKey, zFontSizer = new FontSizer( getSizer( pStyles ) ) ); |
162 |
|
- |
} |
163 |
|
- |
return zFontSizer; |
164 |
|
- |
} |
165 |
|
- |
|
166 |
|
- |
private static Map<Object, Sizer> SIZERS = new HashMap<Object, Sizer>(); |
167 |
|
- |
|
168 |
|
- |
private static Sizer getSizer( String pStyle ) { |
169 |
|
- |
if ( pStyle == null ) { |
170 |
|
- |
return NullSizer.INSTANCE; |
171 |
|
- |
} |
172 |
|
- |
Sizer zSizer = SIZERS.get( pStyle ); |
173 |
|
- |
if ( zSizer == null ) { |
174 |
|
- |
SIZERS.put( pStyle, zSizer = new MyLabel( pStyle ) ); |
175 |
|
- |
} |
176 |
|
- |
return zSizer; |
177 |
|
- |
} |
178 |
|
- |
|
179 |
|
- |
private static Sizer getSizer( String[] pStyles ) // !null & !empty |
180 |
|
- |
{ |
181 |
|
- |
Sizer zSizer = SIZERS.get( pStyles ); |
182 |
|
- |
if ( zSizer == null ) { |
183 |
|
- |
Sizer[] zSizers = new Sizer[pStyles.length]; |
184 |
|
- |
for ( int i = 0; i < pStyles.length; i++ ) { |
185 |
|
- |
zSizers[i] = getSizer( pStyles[i] ); |
186 |
|
- |
} |
187 |
|
- |
SIZERS.put( pStyles, zSizer = new Composite( zSizers ) ); |
188 |
|
- |
} |
189 |
|
- |
return zSizer; |
190 |
|
- |
} |
191 |
|
- |
} |
|
1 |
+ |
// This Source Code is in the Public Domain per: http://unlicense.org |
|
2 |
+ |
package org.litesoft.GWT.client; |
|
3 |
+ |
|
|
4 |
+ |
import org.litesoft.GWT.client.widgets.nonpublic.*; |
|
5 |
+ |
import org.litesoft.commonfoundation.base.*; |
|
6 |
+ |
import org.litesoft.commonfoundation.html.*; |
|
7 |
+ |
import org.litesoft.commonfoundation.typeutils.*; |
|
8 |
+ |
|
|
9 |
+ |
import com.google.gwt.user.client.ui.*; |
|
10 |
+ |
|
|
11 |
+ |
import java.util.*; |
|
12 |
+ |
|
|
13 |
+ |
public class FontSizer { |
|
14 |
+ |
public static final String ASCII = " ?!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; |
|
15 |
+ |
|
|
16 |
+ |
public static FontSizer get( String... pStyles ) { |
|
17 |
+ |
if ( null != (pStyles = Strings.noEmpty( pStyles )) ) { |
|
18 |
+ |
if ( pStyles.length > 1 ) { |
|
19 |
+ |
return getComposite( pStyles ); |
|
20 |
+ |
} |
|
21 |
+ |
String zStyle = ConstrainTo.significantOrNull( pStyles[0] ); |
|
22 |
+ |
if ( zStyle != null ) { |
|
23 |
+ |
return getSingle( zStyle ); |
|
24 |
+ |
} |
|
25 |
+ |
} |
|
26 |
+ |
throw new IllegalStateException( "FontSizer requires at least one Style" ); |
|
27 |
+ |
} |
|
28 |
+ |
|
|
29 |
+ |
public Integer EMs( int pChars ) { |
|
30 |
+ |
Integer zWidth = getAverageCharacterWidth(); |
|
31 |
+ |
return (zWidth != null) ? (pChars * zWidth) : null; |
|
32 |
+ |
} |
|
33 |
+ |
|
|
34 |
+ |
public Integer EMsPlus( int pChars, int pPixels ) { |
|
35 |
+ |
Integer zWidth = getAverageCharacterWidth(); |
|
36 |
+ |
return (zWidth != null) ? ((pChars * zWidth) + pPixels) : null; |
|
37 |
+ |
} |
|
38 |
+ |
|
|
39 |
+ |
public Integer EMsLess( int pChars, int pPixels ) { |
|
40 |
+ |
return EMsPlus( pChars, -pPixels ); |
|
41 |
+ |
} |
|
42 |
+ |
|
|
43 |
+ |
public Integer getAverageCharacterWidth() { |
|
44 |
+ |
return mSizer.getAverageCharacterWidth(); |
|
45 |
+ |
} |
|
46 |
+ |
|
|
47 |
+ |
private interface Sizer { |
|
48 |
+ |
public Integer getAverageCharacterWidth(); |
|
49 |
+ |
} |
|
50 |
+ |
|
|
51 |
+ |
private static class NullSizer implements Sizer { |
|
52 |
+ |
public static final Sizer INSTANCE = new NullSizer(); |
|
53 |
+ |
|
|
54 |
+ |
@Override |
|
55 |
+ |
public Integer getAverageCharacterWidth() { |
|
56 |
+ |
return 0; |
|
57 |
+ |
} |
|
58 |
+ |
} |
|
59 |
+ |
|
|
60 |
+ |
private static class Composite implements Sizer { |
|
61 |
+ |
private Sizer[] mSizers; |
|
62 |
+ |
|
|
63 |
+ |
private Integer mCachedResults = null; |
|
64 |
+ |
|
|
65 |
+ |
private Composite( Sizer[] pSizers ) { |
|
66 |
+ |
mSizers = pSizers; |
|
67 |
+ |
} |
|
68 |
+ |
|
|
69 |
+ |
@Override |
|
70 |
+ |
public Integer getAverageCharacterWidth() { |
|
71 |
+ |
if ( mCachedResults == null ) { |
|
72 |
+ |
int zMax = 4; |
|
73 |
+ |
for ( Sizer zSizer : mSizers ) { |
|
74 |
+ |
Integer zWidth = zSizer.getAverageCharacterWidth(); |
|
75 |
+ |
if ( zWidth == null ) { |
|
76 |
+ |
return null; |
|
77 |
+ |
} |
|
78 |
+ |
zMax = Math.max( zMax, zWidth ); |
|
79 |
+ |
} |
|
80 |
+ |
mCachedResults = zMax; |
|
81 |
+ |
} |
|
82 |
+ |
return mCachedResults; |
|
83 |
+ |
} |
|
84 |
+ |
} |
|
85 |
+ |
|
|
86 |
+ |
private static final class MyLabel extends HTML implements Sizer { |
|
87 |
+ |
private static final String HTML_ASCII = HTMLize.escapeNoWrap( ASCII ); |
|
88 |
+ |
|
|
89 |
+ |
private Integer mCachedResults = null; |
|
90 |
+ |
|
|
91 |
+ |
private MyLabel( String pStyle ) { |
|
92 |
+ |
super( createHTML( pStyle ) ); |
|
93 |
+ |
|
|
94 |
+ |
CommonElementHelper.setOverflowClass( getElement(), true ); |
|
95 |
+ |
setWidth( "4" ); |
|
96 |
+ |
RootPanel.get().add( this ); |
|
97 |
+ |
} |
|
98 |
+ |
|
|
99 |
+ |
private static String createHTML( String pStyle ) { |
|
100 |
+ |
return "<table border='0' cellSpacing='0' cellPadding='0'><tbody><tr><td class='" + pStyle + "' style='white-space:nowrap;'>" + // |
|
101 |
+ |
HTML_ASCII + "</td></tr></tbody></table>"; |
|
102 |
+ |
} |
|
103 |
+ |
|
|
104 |
+ |
@Override |
|
105 |
+ |
public Integer getAverageCharacterWidth() { |
|
106 |
+ |
if ( mCachedResults == null ) { |
|
107 |
+ |
if ( isAttached() ) { |
|
108 |
+ |
int zWidth = getElement().getFirstChildElement().getOffsetWidth(); |
|
109 |
+ |
if ( ASCII.length() <= zWidth ) { |
|
110 |
+ |
mCachedResults = zWidth / ASCII.length(); |
|
111 |
+ |
} else { |
|
112 |
+ |
System.out.println( "FontSizer$MyLabel.getAverageCharacterWidth: " + zWidth ); |
|
113 |
+ |
} |
|
114 |
+ |
} |
|
115 |
+ |
} |
|
116 |
+ |
return mCachedResults; |
|
117 |
+ |
} |
|
118 |
+ |
} |
|
119 |
+ |
|
|
120 |
+ |
private Sizer mSizer; |
|
121 |
+ |
|
|
122 |
+ |
private FontSizer( Sizer pSizer ) { |
|
123 |
+ |
mSizer = pSizer; |
|
124 |
+ |
} |
|
125 |
+ |
|
|
126 |
+ |
private static class CompositeKey { |
|
127 |
+ |
private String[] mStyles; |
|
128 |
+ |
|
|
129 |
+ |
private CompositeKey( String[] pStyles ) { |
|
130 |
+ |
mStyles = pStyles; |
|
131 |
+ |
} |
|
132 |
+ |
|
|
133 |
+ |
@Override |
|
134 |
+ |
public int hashCode() { |
|
135 |
+ |
return Arrays.hashCode( mStyles ); |
|
136 |
+ |
} |
|
137 |
+ |
|
|
138 |
+ |
@Override |
|
139 |
+ |
public boolean equals( Object o ) { |
|
140 |
+ |
return (this == o) || // |
|
141 |
+ |
((o != null) && (getClass() == o.getClass()) && // |
|
142 |
+ |
Arrays.equals( mStyles, ((CompositeKey) o).mStyles )); |
|
143 |
+ |
} |
|
144 |
+ |
} |
|
145 |
+ |
|
|
146 |
+ |
private static Map<String, FontSizer> STYLE_INSTANCES = new HashMap<String, FontSizer>(); |
|
147 |
+ |
|
|
148 |
+ |
private static FontSizer getSingle( String pStyle ) { |
|
149 |
+ |
FontSizer zFontSizer = STYLE_INSTANCES.get( pStyle ); |
|
150 |
+ |
if ( zFontSizer == null ) { |
|
151 |
+ |
STYLE_INSTANCES.put( pStyle, zFontSizer = new FontSizer( getSizer( pStyle ) ) ); |
|
152 |
+ |
} |
|
153 |
+ |
return zFontSizer; |
|
154 |
+ |
} |
|
155 |
+ |
|
|
156 |
+ |
private static Map<CompositeKey, FontSizer> ARRAY_INSTANCES = new HashMap<CompositeKey, FontSizer>(); |
|
157 |
+ |
|
|
158 |
+ |
private static FontSizer getComposite( String[] pStyles ) { |
|
159 |
+ |
CompositeKey zKey = new CompositeKey( pStyles ); |
|
160 |
+ |
FontSizer zFontSizer = ARRAY_INSTANCES.get( zKey ); |
|
161 |
+ |
if ( zFontSizer == null ) { |
|
162 |
+ |
ARRAY_INSTANCES.put( zKey, zFontSizer = new FontSizer( getSizer( pStyles ) ) ); |
|
163 |
+ |
} |
|
164 |
+ |
return zFontSizer; |
|
165 |
+ |
} |
|
166 |
+ |
|
|
167 |
+ |
private static Map<Object, Sizer> SIZERS = new HashMap<Object, Sizer>(); |
|
168 |
+ |
|
|
169 |
+ |
private static Sizer getSizer( String pStyle ) { |
|
170 |
+ |
if ( pStyle == null ) { |
|
171 |
+ |
return NullSizer.INSTANCE; |
|
172 |
+ |
} |
|
173 |
+ |
Sizer zSizer = SIZERS.get( pStyle ); |
|
174 |
+ |
if ( zSizer == null ) { |
|
175 |
+ |
SIZERS.put( pStyle, zSizer = new MyLabel( pStyle ) ); |
|
176 |
+ |
} |
|
177 |
+ |
return zSizer; |
|
178 |
+ |
} |
|
179 |
+ |
|
|
180 |
+ |
private static Sizer getSizer( String[] pStyles ) // !null & !empty |
|
181 |
+ |
{ |
|
182 |
+ |
Sizer zSizer = SIZERS.get( pStyles ); |
|
183 |
+ |
if ( zSizer == null ) { |
|
184 |
+ |
Sizer[] zSizers = new Sizer[pStyles.length]; |
|
185 |
+ |
for ( int i = 0; i < pStyles.length; i++ ) { |
|
186 |
+ |
zSizers[i] = getSizer( pStyles[i] ); |
|
187 |
+ |
} |
|
188 |
+ |
SIZERS.put( pStyles, zSizer = new Composite( zSizers ) ); |
|
189 |
+ |
} |
|
190 |
+ |
return zSizer; |
|
191 |
+ |
} |
|
192 |
+ |
} |