Subversion Repository Public Repository

litesoft

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
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt
package org.litesoft.uispecification;

import org.litesoft.commonfoundation.typeutils.*;
import org.litesoft.core.hierarchicaldata.*;

public class UiFont
{
    /**
     * Family takes a String of comma seperated Font Family 'names', if the 'name' has spaces in it, it
     * must be quoted by single quotes (').<p>
     * <p/>
     * Parsing rules:
     * Leading & trailing spaces will be removed,
     * Single Quotes (') must be paired, &
     * The following characters are NOT allowed:
     * ';', ':', '"'
     */
    public static final String FAMILY = "Family";

    public static final String COLOR = "Color";

    public static final String SIZE = "Size";
    public static final String STYLE = "Style";
    public static final String WEIGHT = "Weight";
    public static final String DECORATION = "Decoration";

    public static final String SIZE_XX_SMALL = "xx-small";
    public static final String SIZE_X_SMALL = "x-small";
    public static final String SIZE_SMALL = "small";
    public static final String SIZE_MEDIUM = "medium";
    public static final String SIZE_LARGE = "large";
    public static final String SIZE_X_LARGE = "x-large";
    public static final String SIZE_XX_LARGE = "xx-large";
    public static final String SIZE_SMALLER = "smaller";
    public static final String SIZE_LARGER = "larger";

    public static final String STYLE_NORMAL = "normal";
    public static final String STYLE_ITALIC = "italic";

    public static final String WEIGHT_NORMAL = "normal";
    public static final String WEIGHT_BOLD = "bold";

    public static final String DECORATION_NONE = "none";
    public static final String DECORATION_UNDERLINE = "underline";
    public static final String DECORATION_LINE_THROUGH = "line-through";

    private static final String[] SIZE_OPTIONS = new String[] //
            { //
              SIZE_XX_SMALL, //
              SIZE_X_SMALL, //
              SIZE_SMALL, //
              SIZE_MEDIUM, //
              SIZE_LARGE, //
              SIZE_X_LARGE, //
              SIZE_XX_LARGE, //
              SIZE_SMALLER, //
              SIZE_LARGER, //
            };

    private static final String[] STYLE_OPTIONS = new String[] //
            { //
              STYLE_NORMAL, //
              STYLE_ITALIC, //
            };

    private static final String[] WEIGHT_OPTIONS = new String[] //
            { //
              WEIGHT_NORMAL, //
              WEIGHT_BOLD, //
            };

    private static final String[] DECORATION_OPTIONS = new String[] //
            { //
              DECORATION_NONE, //
              DECORATION_UNDERLINE, //
              DECORATION_LINE_THROUGH, //
            };

    private String mFamily = null;
    private String mColor = null;
    private String mSize = null;
    private String mStyle = null;
    private String mWeight = null;
    private String mDecoration = null;

    private UiFont( String pFamily, String pColor, String pSize, String pStyle, String pWeight, String pDecoration )
    {
        mFamily = pFamily;
        mColor = pColor;
        mSize = pSize;
        mStyle = pStyle;
        mWeight = pWeight;
        mDecoration = pDecoration;
    }

    public String getFamily()
    {
        return mFamily;
    }

    /**
     * Family takes a String of comma seperated Font Family 'names', if the 'name' has spaces in it, it
     * must be quoted by single quotes (').<p>
     * <p/>
     * Parsing rules:
     * Leading & trailing spaces will be removed,
     * Single Quotes (') must be paired, &
     * The following characters are NOT allowed:
     * ';', ':', '"'
     */
    public UiFont family( String pFamily )
    {
        pFamily = parseFamily( pFamily );
        return Objects.areNonArraysEqual( mFamily, pFamily ) ? this : new UiFont( pFamily, mColor, mSize, mStyle, mWeight, mDecoration );
    }

    private String parseFamily( String pStr )
    {
        if ( null != (pStr = Strings.noEmpty( pStr )) )
        {
            checkFamilyBadChar( pStr, ';' );
            checkFamilyBadChar( pStr, ':' );
            checkFamilyBadChar( pStr, '"' );
            checkFamilyPairedSingleQuotes( pStr );
        }
        return pStr;
    }

    private void checkFamilyBadChar( String pStr, char c )
    {
        if ( pStr.indexOf( c ) != -1 )
        {
            throw new IllegalArgumentException( "Font Family may not have a '" + c + "' character in it: " + pStr );
        }
    }

    private void checkFamilyPairedSingleQuotes( String pStr )
    {
        int zQuotes = 0;
        for ( int at, from = 0; -1 != (at = pStr.indexOf( '\'', from )); from = at + 1 )
        {
            zQuotes++;
        }
        if ( (zQuotes & 1) != 0 ) // Odd
        {
            throw new IllegalArgumentException( "Font Family has single quotes (') that are NOT paired: " + pStr );
        }
    }

    public String getColor()
    {
        return mColor;
    }

    public UiFont color( String pColor )
    {
        pColor = parseColor( pColor );
        return Objects.areNonArraysEqual( mColor, pColor ) ? this : new UiFont( mFamily, pColor, mSize, mStyle, mWeight, mDecoration );
    }

    private String parseColor( String pStr )
    {
        if ( null != (pStr = Strings.noEmpty( pStr )) )
        {
            if ( pStr.startsWith( "#" ) )
            {
                return "#" + parseColorCode( pStr.substring( 1 ).trim().toUpperCase() );
            }
            if ( !isAllAlpha( pStr ) )
            {
                throw new IllegalArgumentException( "A Color must either be a 'code' (eg #FF0000) or a color name, but provided: " + pStr );
            }
        }
        return pStr;
    }

    private boolean isAllAlpha( String pStr )
    {
        for ( int i = pStr.length(); --i >= 0; )
        {
            if ( !Character.isLetter( pStr.charAt( i ) ) )
            {
                return false;
            }
        }
        return true;
    }

    private String parseColorCode( String pStr )
    {
        if ( (pStr.length() == 6) //
             && isHex( pStr.charAt( 0 ) ) //
             && isHex( pStr.charAt( 1 ) ) //
             && isHex( pStr.charAt( 2 ) ) //
             && isHex( pStr.charAt( 3 ) ) //
             && isHex( pStr.charAt( 4 ) ) //
             && isHex( pStr.charAt( 5 ) ) //
                )
        {
            return pStr;
        }
        throw new IllegalArgumentException( "A Color 'code' must have 6 hex digits eg #FF0000, but provided: #" + pStr );
    }

    private boolean isHex( char pChar )
    {
        return (('0' <= pChar) && (pChar <= '9')) || (('A' <= pChar) && (pChar <= 'F'));
    }

    public String getSize()
    {
        return mSize;
    }

    public UiFont size( String pSize )
    {
        pSize = parseSize( pSize );
        return Objects.areNonArraysEqual( mSize, pSize ) ? this : new UiFont( mFamily, mColor, pSize, mStyle, mWeight, mDecoration );
    }

    private String parseSize( String pStr )
    {
        if ( null != (pStr = Strings.noEmpty( pStr )) )
        {
            if ( (pStr.length() > 1) && pStr.endsWith( "%" ) )
            {
                String num = pStr.substring( 0, pStr.length() - 1 ).trim();
                int i = Integer.parseInt( num );
                if ( 1 < i )
                {
                    return i + "%";
                }
            }
        }
        return nullOrOptions( SIZE, "##%", pStr, SIZE_OPTIONS );
    }

    public String getStyle()
    {
        return mStyle;
    }

    public UiFont style( String pStyle )
    {
        pStyle = parseStyle( pStyle );
        return Objects.areNonArraysEqual( mStyle, pStyle ) ? this : new UiFont( mFamily, mColor, mSize, pStyle, mWeight, mDecoration );
    }

    private String parseStyle( String pStr )
    {
        return nullOrOptions( STYLE, "", Strings.noEmpty( pStr ), STYLE_OPTIONS );
    }

    public UiFont sizeXXsmall()
    {
        return size( SIZE_XX_SMALL );
    }

    public UiFont sizeXsmall()
    {
        return size( SIZE_X_SMALL );
    }

    public UiFont sizeSmall()
    {
        return size( SIZE_SMALL );
    }

    public UiFont sizeMedium()
    {
        return size( SIZE_MEDIUM );
    }

    public UiFont sizeLarge()
    {
        return size( SIZE_LARGE );
    }

    public UiFont sizeXlarge()
    {
        return size( SIZE_X_LARGE );
    }

    public UiFont sizeXXlarge()
    {
        return size( SIZE_XX_LARGE );
    }

    public UiFont sizeSmaller()
    {
        return size( SIZE_SMALLER );
    }

    public UiFont sizeLarger()
    {
        return size( SIZE_LARGER );
    }

    public UiFont styleNormal()
    {
        return style( STYLE_NORMAL );
    }

    public UiFont styleItalic()
    {
        return style( STYLE_ITALIC );
    }

    public String getWeight()
    {
        return mWeight;
    }

    public UiFont weight( String pWeight )
    {
        pWeight = parseWeight( pWeight );
        return Objects.areNonArraysEqual( mWeight, pWeight ) ? this : new UiFont( mFamily, mColor, mSize, mStyle, pWeight, mDecoration );
    }

    private String parseWeight( String pStr )
    {
        return nullOrOptions( WEIGHT, "", Strings.noEmpty( pStr ), WEIGHT_OPTIONS );
    }

    public UiFont weightNormal()
    {
        return weight( WEIGHT_NORMAL );
    }

    public UiFont weightBold()
    {
        return weight( WEIGHT_BOLD );
    }

    public String getDecoration()
    {
        return mDecoration;
    }

    public UiFont decoration( String pDecoration )
    {
        pDecoration = parseDecoration( pDecoration );
        return Objects.areNonArraysEqual( mDecoration, pDecoration ) ? this : new UiFont( mFamily, mColor, mSize, mStyle, mWeight, pDecoration );
    }

    private String parseDecoration( String pStr )
    {
        return nullOrOptions( DECORATION, "", Strings.noEmpty( pStr ), DECORATION_OPTIONS );
    }

    public UiFont decorationNone()
    {
        return decoration( DECORATION_NONE );
    }

    public UiFont decorationUnderline()
    {
        return decoration( DECORATION_UNDERLINE );
    }

    public UiFont decorationLineThrough()
    {
        return decoration( DECORATION_LINE_THROUGH );
    }

    /*
       font-family:
           arial, sans-serif;
           tahoma, sans-serif;
           courier, serif;
           times, serif;

       color:
           red;
           green;
           #FFFFFF;

       font-size:
           xx-small;
           x-small;
           small;
           medium;
           large;
           x-large;
           xx-large;
           smaller;
           larger;
           80%;

       font-style:
           normal;
           italic;

       font-weight:
           normal;
           bold;

       text-decoration:
           none;
           underline;
           line-through;
    */

    public void desiccate( AttributesNameValueMutator pMutator )
    {
        desiccate( pMutator, FAMILY, mFamily );
        desiccate( pMutator, COLOR, mColor );
        desiccate( pMutator, SIZE, mSize );
        desiccate( pMutator, STYLE, mStyle );
        desiccate( pMutator, WEIGHT, mWeight );
        desiccate( pMutator, DECORATION, mDecoration );
    }

    public static UiFont rehydrate( AttributesNameValueAccessor pAccessor )
    {
        UiFont rv = NULL;
        rv = rv.family( rehydrate( pAccessor, FAMILY ) );
        rv = rv.color( rehydrate( pAccessor, COLOR ) );
        rv = rv.size( rehydrate( pAccessor, SIZE ) );
        rv = rv.style( rehydrate( pAccessor, STYLE ) );
        rv = rv.weight( rehydrate( pAccessor, WEIGHT ) );
        rv = rv.decoration( rehydrate( pAccessor, DECORATION ) );
        return rv;
    }

    public static final UiFont NULL = new UiFont( null, null, null, null, null, null );

    private static void desiccate( AttributesNameValueMutator pMutator, String pWhat, String pValue )
    {
        if ( pValue != null )
        {
            pMutator.setAttribute( pWhat, pValue );
        }
    }

    private static String rehydrate( AttributesNameValueAccessor pAccessor, String pAttibuteName )
    {
        return pAccessor.getAttributeValue( pAttibuteName );
    }

    private String nullOrOptions( String pWhat, String pXtraOption, String pValue, String[] pOptions )
    {
        if ( pValue == null )
        {
            return null;
        }
        for ( String option : pOptions )
        {
            if ( option.equalsIgnoreCase( pValue ) )
            {
                return option;
            }
        }
        throw new IllegalArgumentException( "Invalid " + pWhat + " '', only accepting: " + pXtraOption + Objects.optionsToString( pOptions ) );
    }
}

Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/uispecification/UiFont.java

Diff revisions: vs.
Revision Author Commited Message
939 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 21:30:31 +0000

Extracting commonfoundation

822 Diff Diff GeorgeS picture GeorgeS Sun 19 Aug, 2012 01:03:51 +0000
804 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 12:48:51 +0000
801 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 03:59:02 +0000
151 Diff Diff GeorgeS picture GeorgeS Thu 17 Mar, 2011 04:16:22 +0000
49 Diff Diff GeorgeS picture GeorgeS Mon 12 Apr, 2010 02:59:10 +0000

License Text

2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000