1
2
3
4
5
6
7
8
9 package tsukuba_bunko.peko.resource;
10
11 import java.awt.font.TextAttribute;
12
13 import java.util.Map;
14
15 import org.xml.sax.Attributes;
16 import org.xml.sax.SAXException;
17
18 import tsukuba_bunko.peko.Logger;
19
20 import tsukuba_bunko.peko.resource.FontManager;
21
22 import tsukuba_bunko.resource.BasicDeserializer;
23
24
25 /***
26 * {@link java.awt.Font} 型のリソースに対する {@link tsukuba_bunko.resource.ResourceDeserializer} 実装です。
27 * @author $Author: ppoi $
28 * @version $Revision: 1.1 $
29 * @see <a href="http://softlab.tsukuba-bunko.org/peko/userguide/resource.html#type-peko:font">peko:font 型のリソース</a>
30 */
31 public class FontDeserializer extends BasicDeserializer {
32
33 /***
34 * フォント属性マップ
35 */
36 protected Map _fontAttributes = null;
37
38 /***
39 */
40 protected StringBuffer _text = null;
41
42 /***
43 * <code>FontDeserializer</code> のインスタンスを生成します。
44 */
45 public FontDeserializer()
46 {
47 super();
48 }
49
50
51
52
53
54 public void startDocument()
55 {
56 _fontAttributes = new java.util.HashMap();
57 _fontAttributes.put( TextAttribute.FAMILY, "Serif" );
58 _fontAttributes.put( TextAttribute.SIZE, new Float(20f) );
59 _fontAttributes.put( TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR );
60 _fontAttributes.put( TextAttribute.WEIGHT, TextAttribute.WEIGHT_REGULAR );
61 }
62
63 public void endDocument()
64 {
65 FontManager fonts = FontManager.getInstance();
66 setValue( fonts.getFont(_fontAttributes) );
67 }
68
69 public void startElement( String namespaceURI, String localName, String qName, Attributes attrs )
70 {
71 _text = new StringBuffer();
72 }
73
74 public void endElement( String namespaceURI, String localName, String qName )
75 throws SAXException
76 {
77 if( qName.equals("family") ) {
78 _fontAttributes.put( TextAttribute.FAMILY, new String(_text) );
79 }
80 else if( qName.equals("size") ) {
81 try {
82 _fontAttributes.put( TextAttribute.SIZE, Float.valueOf(new String(_text)) );
83 }
84 catch( Exception e ) {
85 Logger.warn( "[resource.font] invalid font size is specified :" + _text, e );
86 }
87 }
88 else if( qName.equals("style") ) {
89 String style = new String( _text );
90 if( "italic".equals(style) ) {
91 _fontAttributes.put( TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE );
92 }
93 }
94 else if( qName.equals("weight") ) {
95 String weight = new String( _text );
96 if( "bold".equals(weight) ) {
97 _fontAttributes.put( TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD );
98 }
99 }
100 _text = null;
101 }
102
103 public void characters( char[] ch, int begin, int length )
104 {
105 if( _text != null ) {
106 _text.append( ch, begin, length );
107 }
108 }
109 }