KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > html > style > HtmlValues


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21
22 package org.lobobrowser.html.style;
23
24 import java.awt.Color JavaDoc;
25 import java.awt.Font JavaDoc;
26 import java.awt.FontMetrics JavaDoc;
27 import java.awt.Insets JavaDoc;
28 import java.awt.Toolkit JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.StringTokenizer JavaDoc;
33
34 import org.lobobrowser.util.Strings;
35 import org.lobobrowser.util.gui.ColorFactory;
36 import org.w3c.dom.css.CSS2Properties;
37
38 public class HtmlValues {
39     private static final Map JavaDoc SYSTEM_FONTS = new HashMap JavaDoc();
40     public static final float DEFAULT_FONT_SIZE = 14.0f;
41     public static final int DEFAULT_FONT_SIZE_INT = 14;
42     public static final Float JavaDoc DEFAULT_FONT_SIZE_BOX = new Float JavaDoc(14.0f);
43     
44     static {
45         FontInfo systemFont = new FontInfo();
46         SYSTEM_FONTS.put("caption", systemFont);
47         SYSTEM_FONTS.put("icon", systemFont);
48         SYSTEM_FONTS.put("menu", systemFont);
49         SYSTEM_FONTS.put("message-box", systemFont);
50         SYSTEM_FONTS.put("small-caption", systemFont);
51         SYSTEM_FONTS.put("status-bar", systemFont);
52     }
53     
54     private HtmlValues() {
55     }
56     
57     public static boolean isBorderStyle(String JavaDoc token) {
58         return token.equalsIgnoreCase("solid") ||
59             token.equalsIgnoreCase("double") ||
60             token.equalsIgnoreCase("none") ||
61             token.equalsIgnoreCase("dashed") ||
62             token.equalsIgnoreCase("dotted") ||
63             token.equalsIgnoreCase("hidden") ||
64             token.equalsIgnoreCase("groove") ||
65             token.equalsIgnoreCase("ridge") ||
66             token.equalsIgnoreCase("inset") ||
67             token.equalsIgnoreCase("outset");
68     }
69     
70     public static Insets JavaDoc getMarginInsets(CSS2Properties cssProperties, RenderState renderState) {
71         String JavaDoc shortcutText = cssProperties.getMargin();
72         Insets JavaDoc insets = null;
73         if(shortcutText != null && !"".equals(shortcutText)) {
74             insets = getInsets(shortcutText, renderState, true);
75         }
76         String JavaDoc topText = cssProperties.getMarginTop();
77         insets = updateTopInset(insets, topText, renderState);
78         String JavaDoc leftText = cssProperties.getMarginLeft();
79         insets = updateLeftInset(insets, leftText, renderState);
80         String JavaDoc bottomText = cssProperties.getMarginBottom();
81         insets = updateBottomInset(insets, bottomText, renderState);
82         String JavaDoc rightText = cssProperties.getMarginRight();
83         insets = updateRightInset(insets, rightText, renderState);
84         return insets;
85     }
86     
87     public static Insets JavaDoc getPaddingInsets(CSS2Properties cssProperties, RenderState renderState) {
88         String JavaDoc shortcutText = cssProperties.getPadding();
89         Insets JavaDoc insets = null;
90         if(shortcutText != null && !"".equals(shortcutText)) {
91             insets = getInsets(shortcutText, renderState, false);
92         }
93         String JavaDoc topText = cssProperties.getPaddingTop();
94         insets = updateTopInset(insets, topText, renderState);
95         String JavaDoc leftText = cssProperties.getPaddingLeft();
96         insets = updateLeftInset(insets, leftText, renderState);
97         String JavaDoc bottomText = cssProperties.getPaddingBottom();
98         insets = updateBottomInset(insets, bottomText, renderState);
99         String JavaDoc rightText = cssProperties.getPaddingRight();
100         insets = updateRightInset(insets, rightText, renderState);
101         return insets;
102     }
103
104     public static Insets JavaDoc getBorderInsets(Insets JavaDoc insets, CSS2Properties cssProperties, RenderState renderState) {
105         String JavaDoc shortcutText = cssProperties.getBorderWidth();
106         if(shortcutText != null && !"".equals(shortcutText)) {
107             insets = getInsets(shortcutText, renderState, false);
108         }
109         String JavaDoc topText = cssProperties.getBorderTopWidth();
110         insets = updateTopInset(insets, topText, renderState);
111         String JavaDoc leftText = cssProperties.getBorderLeftWidth();
112         insets = updateLeftInset(insets, leftText, renderState);
113         String JavaDoc bottomText = cssProperties.getBorderBottomWidth();
114         insets = updateBottomInset(insets, bottomText, renderState);
115         String JavaDoc rightText = cssProperties.getBorderRightWidth();
116         insets = updateRightInset(insets, rightText, renderState);
117         return insets;
118     }
119
120     private static Insets JavaDoc updateTopInset(Insets JavaDoc insets, String JavaDoc sizeText, RenderState renderState) {
121         if(sizeText == null || "".equals(sizeText)) {
122             return insets;
123         }
124         if(insets == null) {
125             insets = new Insets JavaDoc(0, 0, 0, 0);
126         }
127         insets.top = getPixelSize(sizeText, renderState, 0);
128         return insets;
129     }
130
131     private static Insets JavaDoc updateLeftInset(Insets JavaDoc insets, String JavaDoc sizeText, RenderState renderState) {
132         if(sizeText == null || "".equals(sizeText)) {
133             return insets;
134         }
135         if(insets == null) {
136             insets = new Insets JavaDoc(0, 0, 0, 0);
137         }
138         insets.left = getPixelSize(sizeText, renderState, 0);
139         return insets;
140     }
141
142     private static Insets JavaDoc updateBottomInset(Insets JavaDoc insets, String JavaDoc sizeText, RenderState renderState) {
143         if(sizeText == null || "".equals(sizeText)) {
144             return insets;
145         }
146         if(insets == null) {
147             insets = new Insets JavaDoc(0, 0, 0, 0);
148         }
149         insets.bottom = getPixelSize(sizeText, renderState, 0);
150         return insets;
151     }
152
153     private static Insets JavaDoc updateRightInset(Insets JavaDoc insets, String JavaDoc sizeText, RenderState renderState) {
154         if(sizeText == null || "".equals(sizeText)) {
155             return insets;
156         }
157         if(insets == null) {
158             insets = new Insets JavaDoc(0, 0, 0, 0);
159         }
160         insets.right = getPixelSize(sizeText, renderState, 0);
161         return insets;
162     }
163
164     public static Insets JavaDoc getInsets(String JavaDoc insetsSpec, RenderState renderState, boolean negativeOK) {
165         int[] insetsArray = new int[4];
166         int size = 0;
167         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(insetsSpec);
168         if(tok.hasMoreTokens()) {
169             String JavaDoc token = tok.nextToken();
170             insetsArray[0] = getPixelSize(token, renderState, 0);
171             if(negativeOK || insetsArray[0] >= 0) {
172                 size = 1;
173                 if(tok.hasMoreTokens()) {
174                     token = tok.nextToken();
175                     insetsArray[1] = getPixelSize(token, renderState, 0);
176                     if(negativeOK || insetsArray[1] >= 0) {
177                         size = 2;
178                         if(tok.hasMoreTokens()) {
179                             token = tok.nextToken();
180                             insetsArray[2] = getPixelSize(token, renderState, 0);
181                             if(negativeOK || insetsArray[2] >= 0) {
182                                 size = 3;
183                                 if(tok.hasMoreTokens()) {
184                                     token = tok.nextToken();
185                                     insetsArray[3] = getPixelSize(token, renderState, 0);
186                                     size = 4;
187                                     if(negativeOK || insetsArray[3] >= 0) {
188                                         // nop
189
}
190                                     else {
191                                         insetsArray[3] = 0;
192                                     }
193                                 }
194                             }
195                             else {
196                                 size = 4;
197                                 insetsArray[2] = 0;
198                             }
199                         }
200                     }
201                     else {
202                         size = 4;
203                         insetsArray[1] = 0;
204                     }
205                 }
206             }
207             else {
208                 size = 1;
209                 insetsArray[0] = 0;
210             }
211         }
212         if(size == 4) {
213             return new Insets JavaDoc(insetsArray[0], insetsArray[3], insetsArray[2], insetsArray[1]);
214         }
215         else if (size == 1) {
216             int val = insetsArray[0];
217             return new Insets JavaDoc(val, val, val, val);
218         }
219         else if(size == 2) {
220             return new Insets JavaDoc(insetsArray[0], insetsArray[1], insetsArray[0], insetsArray[1]);
221         }
222         else if(size == 3) {
223             return new Insets JavaDoc(insetsArray[0], insetsArray[1], insetsArray[2], insetsArray[1]);
224         }
225         else {
226             return null;
227         }
228     }
229
230     /**
231      * Gets an array of top-left-bottom-right colors
232      * given a spec.
233      */

234     public static Color JavaDoc[] getColors(String JavaDoc colorsSpec) {
235         Color JavaDoc[] colorsArray = new Color JavaDoc[4];
236         String JavaDoc[] colorStrings = HtmlValues.splitCssValue(colorsSpec);
237         int size = colorStrings.length;
238         if(size > 4) {
239             size = 4;
240         }
241         for(int i = 0; i < size; i++) {
242             colorsArray[i] = ColorFactory.getInstance().getColor(colorStrings[i]);
243         }
244         if(size == 4) {
245             return colorsArray;
246         }
247         else if (size == 1) {
248             Color JavaDoc color = colorsArray[0];
249             colorsArray[1] = color;
250             colorsArray[2] = color;
251             colorsArray[3] = color;
252             return colorsArray;
253         }
254         else if(size == 2) {
255             colorsArray[2] = colorsArray[0];
256             colorsArray[3] = colorsArray[1];
257             return colorsArray;
258         }
259         else if(size == 3) {
260             colorsArray[3] = colorsArray[1];
261             return colorsArray;
262         }
263         else {
264             throw new IllegalStateException JavaDoc("size=" + size);
265         }
266     }
267
268     /**
269      * Gets a number for 1 to 7.
270      * @param oldHtmlSpec A number from 1 to 7 or +1, etc.
271      */

272     public static final int getFontNumberOldStyle(String JavaDoc oldHtmlSpec, RenderState renderState) {
273         oldHtmlSpec = oldHtmlSpec.trim();
274         int tentative;
275         try {
276             if(oldHtmlSpec.startsWith("+")) {
277                 tentative = renderState.getFontBase() + Integer.parseInt(oldHtmlSpec.substring(1));
278             }
279             else if(oldHtmlSpec.startsWith("-")) {
280                 tentative = renderState.getFontBase() + Integer.parseInt(oldHtmlSpec);
281             }
282             else {
283                 tentative = Integer.parseInt(oldHtmlSpec);
284             }
285             if(tentative < 1) {
286                 tentative = 1;
287             }
288             else if (tentative > 7) {
289                 tentative = 7;
290             }
291         } catch(NumberFormatException JavaDoc nfe) {
292             // ignore
293
tentative = 3;
294         }
295         return tentative;
296     }
297     
298     public static final float getFontSize(int fontNumber) {
299         switch(fontNumber) {
300         case 1:
301             return 10.0f;
302         case 2:
303             return 11.0f;
304         case 3:
305             return 13.0f;
306         case 4:
307             return 16.0f;
308         case 5:
309             return 21.0f;
310         case 6:
311             return 29.0f;
312         case 7:
313             return 42.0f;
314         default:
315             return 63.0f;
316         }
317     }
318
319     public static final float getFontSize(String JavaDoc spec, RenderState parentRenderState) {
320         String JavaDoc specTL = spec.toLowerCase();
321         if(specTL.endsWith("pt") || specTL.endsWith("px") || specTL.endsWith("cm") || specTL.endsWith("pc") || specTL.endsWith("cm") || specTL.endsWith("mm") || specTL.endsWith("em") || specTL.endsWith("ex")) {
322             return getPixelSize(spec, parentRenderState, DEFAULT_FONT_SIZE_INT);
323         }
324         else if(specTL.endsWith("%")) {
325             String JavaDoc value = specTL.substring(0, specTL.length() - 1);
326             try {
327                 double valued = Double.parseDouble(value);
328                 double parentFontSize = parentRenderState == null ? 14.0 : parentRenderState.getFont().getSize();
329                 return (float) (parentFontSize * valued / 100.0);
330             } catch(NumberFormatException JavaDoc nfe) {
331                 return DEFAULT_FONT_SIZE;
332             }
333         }
334         else if("small".equals(specTL)) {
335             return 12.0f;
336         }
337         else if("medium".equals(specTL)) {
338             return 14.0f;
339         }
340         else if("large".equals(specTL)) {
341             return 20.0f;
342         }
343         else if("x-small".equals(specTL)) {
344             return 11.0f;
345         }
346         else if("xx-small".equals(specTL)) {
347             return 10.0f;
348         }
349         else if("x-large".equals(specTL)) {
350             return 26.0f;
351         }
352         else if("xx-large".equals(specTL)) {
353             return 40.0f;
354         }
355         else if("larger".equals(specTL)) {
356             int parentFontSize = parentRenderState == null ? DEFAULT_FONT_SIZE_INT : parentRenderState.getFont().getSize();
357             return parentFontSize * 1.2f;
358         }
359         else if("smaller".equals(specTL)) {
360             int parentFontSize = parentRenderState == null ? DEFAULT_FONT_SIZE_INT : parentRenderState.getFont().getSize();
361             return parentFontSize * 1.2f;
362         }
363         else {
364             return getPixelSize(spec, parentRenderState, DEFAULT_FONT_SIZE_INT);
365         }
366     }
367     
368     public static final int getPixelSize(String JavaDoc spec, RenderState renderState, int errorValue, int availSize) {
369         if(spec.endsWith("%")) {
370             String JavaDoc perText = spec.substring(0, spec.length() - 1);
371             try {
372                 double val = Double.parseDouble(perText);
373                 return (int) Math.round(availSize * val / 100.0);
374             } catch(NumberFormatException JavaDoc nfe) {
375                 return errorValue;
376             }
377         }
378         else {
379             return getPixelSize(spec, renderState, errorValue);
380         }
381     }
382
383     public static final int getPixelSize(String JavaDoc spec, RenderState renderState, int errorValue) {
384         String JavaDoc lcSpec = spec.toLowerCase();
385         if(lcSpec.endsWith("pt")) {
386             String JavaDoc valText = lcSpec.substring(0, lcSpec.length() - "pt".length());
387             double val;
388             try {
389                 val = Double.parseDouble(valText);
390             } catch(NumberFormatException JavaDoc nfe) {
391                 return errorValue;
392             }
393             int dpi = Toolkit.getDefaultToolkit().getScreenResolution();
394             double inches = val / 72;
395             return (int) Math.round(dpi * inches);
396         }
397         else if(lcSpec.endsWith("px")) {
398             String JavaDoc pxText = lcSpec.substring(0, lcSpec.length() - "px".length());
399             try {
400                 return (int) Math.round(Double.parseDouble(pxText));
401             } catch(NumberFormatException JavaDoc nfe) {
402                 return errorValue;
403             }
404         }
405         else if(lcSpec.endsWith("pc")) {
406             String JavaDoc valText = lcSpec.substring(0, lcSpec.length() - "pc".length());
407             double val;
408             try {
409                 val = Double.parseDouble(valText);
410             } catch(NumberFormatException JavaDoc nfe) {
411                 return errorValue;
412             }
413             int dpi = Toolkit.getDefaultToolkit().getScreenResolution();
414             double inches = val / 6;
415             return (int) Math.round(dpi * inches);
416         }
417         else if(lcSpec.endsWith("cm")) {
418             String JavaDoc valText = lcSpec.substring(0, lcSpec.length() - "cm".length());
419             double val;
420             try {
421                 val = Double.parseDouble(valText);
422             } catch(NumberFormatException JavaDoc nfe) {
423                 return errorValue;
424             }
425             int dpi = Toolkit.getDefaultToolkit().getScreenResolution();
426             double inches = val / 2.54;
427             return (int) Math.round(dpi * inches);
428         }
429         else if(lcSpec.endsWith("mm")) {
430             String JavaDoc valText = lcSpec.substring(0, lcSpec.length() - "mm".length());
431             double val;
432             try {
433                 val = Double.parseDouble(valText);
434             } catch(NumberFormatException JavaDoc nfe) {
435                 return errorValue;
436             }
437             int dpi = Toolkit.getDefaultToolkit().getScreenResolution();
438             double inches = val / 25.4;
439             return (int) Math.round(dpi * inches);
440         }
441         else if(lcSpec.endsWith("ex") && renderState != null) {
442             FontMetrics JavaDoc fm = renderState.getFontMetrics();
443             String JavaDoc valText = lcSpec.substring(0, lcSpec.length() - "ex".length());
444             double val;
445             try {
446                 val = Double.parseDouble(valText);
447             } catch(NumberFormatException JavaDoc nfe) {
448                 return errorValue;
449             }
450             return (int) Math.round(fm.getAscent() * val);
451         }
452         else if(lcSpec.endsWith("em") && renderState != null) {
453             Font JavaDoc f = renderState.getFont();
454             String JavaDoc valText = lcSpec.substring(0, lcSpec.length() - "ex".length());
455             double val;
456             try {
457                 val = Double.parseDouble(valText);
458             } catch(NumberFormatException JavaDoc nfe) {
459                 return errorValue;
460             }
461             return (int) Math.round(f.getSize() * val);
462         }
463         else {
464             String JavaDoc pxText = lcSpec;
465             try {
466                 return (int) Math.round(Double.parseDouble(pxText));
467             } catch(NumberFormatException JavaDoc nfe) {
468                 return errorValue;
469             }
470         }
471     }
472     
473     public static int getOldSyntaxPixelSize(String JavaDoc spec, int availSize, int errorValue) {
474         if(spec == null) {
475             return errorValue;
476         }
477         spec = spec.trim();
478         try {
479             if(spec.endsWith("%")) {
480                 return availSize * Integer.parseInt(spec.substring(0, spec.length() - 1)) / 100;
481             }
482             else {
483                 return Integer.parseInt(spec);
484             }
485         } catch(NumberFormatException JavaDoc nfe) {
486             return errorValue;
487         }
488     }
489     
490     public static String JavaDoc getURIFromStyleValue(String JavaDoc styleValue) {
491         String JavaDoc start = "url(";
492         if(!styleValue.toLowerCase().startsWith(start)) {
493             return null;
494         }
495         int startIdx = start.length();
496         int closingIdx = styleValue.lastIndexOf(')');
497         if(closingIdx == -1) {
498             return null;
499         }
500         String JavaDoc quotedUrl = styleValue.substring(startIdx, closingIdx);
501         return unquoteAndUnescape(quotedUrl);
502     }
503     
504     public static String JavaDoc unquoteAndUnescape(String JavaDoc text) {
505         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
506         int index = 0;
507         int length = text.length();
508         boolean escape = false;
509         boolean single = false;
510         if(index < length) {
511             char ch = text.charAt(index);
512             switch(ch) {
513             case '\'':
514                 single = true;
515                 break;
516             case '"':
517                 break;
518             case '\\':
519                 escape = true;
520                 break;
521             default:
522                 result.append(ch);
523             }
524             index++;
525         }
526         OUTER:
527         for(; index < length; index++) {
528             char ch = text.charAt(index);
529             switch(ch) {
530             case '\'':
531                 if(escape || !single) {
532                     escape = false;
533                     result.append(ch);
534                 }
535                 else {
536                     break OUTER;
537                 }
538                 break;
539             case '"':
540                 if(escape || single) {
541                     escape = false;
542                     result.append(ch);
543                 }
544                 else {
545                     break OUTER;
546                 }
547                 break;
548             case '\\':
549                 if(escape) {
550                     escape = false;
551                     result.append(ch);
552                 }
553                 else {
554                     escape = true;
555                 }
556                 break;
557             default:
558                 if(escape) {
559                     escape = false;
560                     result.append('\\');
561                 }
562                 result.append(ch);
563             }
564         }
565         return result.toString();
566     }
567     
568     public static String JavaDoc getColorFromBackground(String JavaDoc background) {
569         String JavaDoc[] backgroundParts = HtmlValues.splitCssValue(background);
570         for(int i = 0; i < backgroundParts.length; i++) {
571             String JavaDoc token = backgroundParts[i];
572             if(ColorFactory.getInstance().isColor(token)) {
573                 return token;
574             }
575         }
576         return null;
577     }
578     
579     public static boolean isLength(String JavaDoc token) {
580         if(token.endsWith("px") ||
581             token.endsWith("pt") ||
582             token.endsWith("pc") ||
583             token.endsWith("cm") ||
584             token.endsWith("mm") ||
585             token.endsWith("ex") ||
586             token.endsWith("em")) {
587             return true;
588         }
589         try {
590             Double.parseDouble(token);
591             return true;
592         } catch(NumberFormatException JavaDoc nfe) {
593             return false;
594         }
595     }
596     
597     public static String JavaDoc[] splitCssValue(String JavaDoc cssValue) {
598         ArrayList JavaDoc tokens = new ArrayList JavaDoc();
599         int len = cssValue.length();
600         int parenCount = 0;
601         StringBuffer JavaDoc currentWord = null;
602         for(int i = 0; i < len; i++) {
603             char ch = cssValue.charAt(i);
604             switch(ch) {
605             case '(':
606                 parenCount++;
607                 if(currentWord == null) {
608                     currentWord = new StringBuffer JavaDoc();
609                 }
610                 currentWord.append(ch);
611                 break;
612             case ')':
613                 parenCount--;
614                 if(currentWord == null) {
615                     currentWord = new StringBuffer JavaDoc();
616                 }
617                 currentWord.append(ch);
618                 break;
619             case ' ':
620             case '\t':
621             case '\n':
622             case '\r':
623                 if(parenCount == 0) {
624                     tokens.add(currentWord.toString());
625                     currentWord = null;
626                     break;
627                 }
628                 else {
629                     // Fall through - no break
630
}
631             default:
632                 if(currentWord == null) {
633                     currentWord = new StringBuffer JavaDoc();
634                 }
635                 currentWord.append(ch);
636                 break;
637             }
638         }
639         if(currentWord != null) {
640             tokens.add(currentWord.toString());
641         }
642         return (String JavaDoc[]) tokens.toArray(new String JavaDoc[tokens.size()]);
643     }
644     
645     public static boolean isUrl(String JavaDoc token) {
646         return token.toLowerCase().startsWith("url(");
647     }
648     
649     public static int getListStyleType(String JavaDoc token) {
650         String JavaDoc tokenTL = token.toLowerCase();
651         if("disc".equals(tokenTL)) {
652             return ListStyle.TYPE_DISC;
653         }
654         else if("circle".equals(tokenTL)) {
655             return ListStyle.TYPE_CIRCLE;
656         }
657         else if("square".equals(tokenTL)) {
658             return ListStyle.TYPE_SQUARE;
659         }
660         else if("decimal".equals(tokenTL)) {
661             return ListStyle.TYPE_DECIMAL;
662         }
663         else if("lower-alpha".equals(tokenTL) || "lower-latin".equals(tokenTL)) {
664             return ListStyle.TYPE_LOWER_ALPHA;
665         }
666         else if("upper-alpha".equals(tokenTL) || "upper-latin".equals(tokenTL)) {
667             return ListStyle.TYPE_UPPER_ALPHA;
668         }
669         else {
670             //TODO: Many types missing here
671
return ListStyle.TYPE_UNSET;
672         }
673     }
674     
675     public static int getListStyleTypeDeprecated(String JavaDoc token) {
676         String JavaDoc tokenTL = token.toLowerCase();
677         if("disc".equals(tokenTL)) {
678             return ListStyle.TYPE_DISC;
679         }
680         else if("circle".equals(tokenTL)) {
681             return ListStyle.TYPE_CIRCLE;
682         }
683         else if("square".equals(tokenTL)) {
684             return ListStyle.TYPE_SQUARE;
685         }
686         else if("1".equals(tokenTL)) {
687             return ListStyle.TYPE_DECIMAL;
688         }
689         else if("a".equals(tokenTL)) {
690             return ListStyle.TYPE_LOWER_ALPHA;
691         }
692         else if("A".equals(tokenTL)) {
693             return ListStyle.TYPE_UPPER_ALPHA;
694         }
695         else {
696             //TODO: Missing i, I.
697
return ListStyle.TYPE_UNSET;
698         }
699     }
700
701     public static int getListStylePosition(String JavaDoc token) {
702         String JavaDoc tokenTL = token.toLowerCase();
703         if("inside".equals(tokenTL)) {
704             return ListStyle.POSITION_INSIDE;
705         }
706         else if("outside".equals(tokenTL)) {
707             return ListStyle.POSITION_OUTSIDE;
708         }
709         else {
710             return ListStyle.POSITION_UNSET;
711         }
712     }
713
714     public static ListStyle getListStyle(String JavaDoc listStyleText) {
715         ListStyle listStyle = new ListStyle();
716         String JavaDoc[] tokens = HtmlValues.splitCssValue(listStyleText);
717         for(int i = 0; i < tokens.length; i++) {
718             String JavaDoc token = tokens[i];
719             int listStyleType = HtmlValues.getListStyleType(token);
720             if(listStyleType != ListStyle.TYPE_UNSET) {
721                 listStyle.type = listStyleType;
722             }
723             else if(HtmlValues.isUrl(token)) {
724                 //TODO: listStyle.image
725
}
726             else {
727                 int listStylePosition = HtmlValues.getListStylePosition(token);
728                 if(listStylePosition != ListStyle.POSITION_UNSET) {
729                     listStyle.position = listStylePosition;
730                 }
731             }
732         }
733         return listStyle;
734     }
735     
736     private static boolean isFontStyle(String JavaDoc token) {
737         return "italic".equals(token) || "normal".equals(token) || "oblique".equals(token);
738     }
739     
740     private static boolean isFontVariant(String JavaDoc token) {
741         return "small-caps".equals(token) || "normal".equals(token);
742     }
743     private static boolean isFontWeight(String JavaDoc token) {
744         if("bold".equals(token) || "bolder".equals(token) || "lighter".equals(token)) {
745             return true;
746         }
747         try {
748             int value = Integer.parseInt(token);
749             return (value % 100) == 0 && value >= 100 && value <= 900;
750         } catch(NumberFormatException JavaDoc nfe) {
751             return false;
752         }
753     }
754
755     public static FontInfo createFontInfo(String JavaDoc fontSpec, RenderState parentRenderState) {
756         String JavaDoc fontSpecTL = fontSpec.toLowerCase();
757         FontInfo fontInfo = (FontInfo) SYSTEM_FONTS.get(fontSpecTL);
758         if(fontInfo != null) {
759             return fontInfo;
760         }
761         fontInfo = new FontInfo();
762         String JavaDoc[] tokens = HtmlValues.splitCssValue(fontSpecTL);
763         String JavaDoc token = null;
764         int length = tokens.length;
765         int i;
766         for(i = 0; i < length; i++) {
767             token = tokens[i];
768             if(isFontStyle(token)) {
769                 fontInfo.fontStyle = token;
770                 continue;
771             }
772             if(isFontVariant(token)) {
773                 fontInfo.fontVariant = token;
774                 continue;
775             }
776             if(isFontWeight(token)) {
777                 fontInfo.fontWeight = token;
778                 continue;
779             }
780             // Otherwise exit loop
781
break;
782         }
783         if(token != null) {
784             int slashIdx = token.indexOf('/');
785             String JavaDoc fontSizeText = slashIdx == -1 ? token : token.substring(0, slashIdx);
786             fontInfo.fontSize = new java.lang.Float JavaDoc(HtmlValues.getFontSize(fontSizeText, parentRenderState));
787             //TODO: Line height
788
if(++i < length) {
789                 StringBuffer JavaDoc fontFamilyBuff = new StringBuffer JavaDoc();
790                 do {
791                     token = tokens[i];
792                     fontFamilyBuff.append(token);
793                     fontFamilyBuff.append(' ');
794                 } while(++i < length);
795                 fontInfo.fontFamily = fontFamilyBuff.toString();
796             }
797         }
798         return fontInfo;
799     }
800
801     
802 // if(token != null) {
803
// int slashIdx = token.indexOf('/');
804
// String fontSizeText = slashIdx == -1 ? token : token.substring(0, slashIdx);
805
// try {
806
// fontSizeText = fontSizeText.toLowerCase();
807
// todo();
808
// } catch(Exception err) {
809
// // ignore
810
// }
811
//
812
// // The rest is the font family
813
// if(hasMoreTokens) {
814
// StringBuffer fontFamilyBuf = new StringBuffer();
815
// while(tok.hasMoreTokens()) {
816
// fontFamilyBuf.append(tok.nextToken());
817
// fontFamilyBuf.append(' ');
818
// }
819
// fontInfo.fontFamily = fontFamilyBuf.toString();
820
// }
821
// }
822

823 }
824
Popular Tags