1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package tsukuba_bunko.peko.canvas.text;
20
21 import java.io.Serializable;
22
23 import java.awt.Color;
24 import java.awt.Graphics2D;
25 import java.awt.Shape;
26
27 import java.awt.font.TextAttribute;
28 import java.awt.font.TextLayout;
29
30 import java.text.AttributedString;
31
32
33 /***
34 * ページに描画される行です。
35 * @author $Author: ppoi $
36 * @version $Revision: 1.2 $
37 */
38 public class Line implements Serializable {
39
40 /***
41 * serial version UID
42 */
43 private static final long serialVersionUID = 6658248129363617887L;
44
45 /***
46 * 描画する TextLayout
47 */
48 transient private TextLayout _textLayout = null;
49
50 /***
51 * shadow
52 */
53 transient private Shape _shape = null;
54
55 /***
56 * line span
57 */
58 private float _lineSpan = 0f;
59
60 /***
61 */
62 private Color _foreground = null;
63
64 /***
65 */
66 private Color _shadowColor = Color.black;
67
68 /***
69 * この行に描画される文字列
70 */
71 private String _text = null;
72
73
74 /***
75 * <code>Line</code> のインスタンスを作成します。
76 */
77 public Line()
78 {
79 super();
80 }
81
82 /***
83 * 描画に使用する前景色を指定します。
84 * @param color 前景色
85 */
86 public void setForeground( Color color )
87 {
88 _foreground = color;
89 }
90
91 /***
92 * 描画に使用する前景色を取得します。
93 * @return 前景色
94 */
95 public Color getForeground()
96 {
97 return _foreground;
98 }
99
100 /***
101 * 描画に使用する影の色を指定します。
102 * @param color 影の色
103 */
104 public void setShadowColor( Color color )
105 {
106 _shadowColor = color;
107 }
108
109 /***
110 * 描画に使用する影の色を取得します。
111 * @return 描画に使用する影の色
112 */
113 public Color getShadowColor()
114 {
115 return _shadowColor;
116 }
117
118 /***
119 * 描画する文字列を設定します。
120 * @param text 描画する文字列
121 */
122 public void setText( String text )
123 {
124 _text = text;
125 }
126
127 /***
128 * 描画する文字列を取得します。
129 * @return 描画する文字列
130 */
131 public String getText()
132 {
133 return _text;
134 }
135
136 /***
137 * 描画する TextLayout を設定します。
138 * @param layout 描画する TextLayout
139 */
140 public void setTextLayout( TextLayout layout )
141 {
142 _textLayout = layout;
143 _shape = layout.getOutline( null );
144 }
145
146 /***
147 * 描画する TextLayout を取得します。
148 * @return 描画する TextLayout
149 */
150 public TextLayout getTextLayout()
151 {
152 return _textLayout;
153 }
154
155 /***
156 * 行間隔を設定します。
157 * @param lineSpan 行間隔
158 */
159 public void setLineSpan( float lineSpan )
160 {
161 _lineSpan = lineSpan;
162 }
163
164 /***
165 * 行間隔を取得します。
166 * @return 行間隔
167 */
168 public float getLineSpan()
169 {
170 return _lineSpan;
171 }
172
173 /***
174 * ベースラインより上の部分の長さ(ascent)を取得します。ascent には、行間隔部分も含まれます。
175 * @return ascent
176 */
177 public float getAscent()
178 {
179 return _textLayout.getAscent() + _lineSpan;
180 }
181
182 /***
183 * ベースラインより下の部分の長さ(descent)を取得します。
184 * @return descent
185 */
186 public float getDescent()
187 {
188 return _textLayout.getDescent();
189 }
190
191 /***
192 * 行の長さ(advance)を取得します。
193 * @return advance
194 */
195 public float getAdavance()
196 {
197 return _textLayout.getAdvance();
198 }
199
200 /***
201 * 行を描画します。
202 * @param g2 グラフィックコンテクスト
203 * @param x 描画開始位置の x 座標
204 * @param y ベースラインの y 座標
205 */
206 public void draw( Graphics2D g2, float x, float y )
207 {
208 Graphics2D g = (Graphics2D)g2.create();
209 g.translate( (int)x + 2, (int)y + 2 );
210 if( _shadowColor == null ) {
211 g.setColor( Color.black );
212 }
213 else {
214 g.setColor( _shadowColor );
215 }
216 g.setRenderingHint( java.awt.RenderingHints.KEY_ANTIALIASING, java.awt.RenderingHints.VALUE_ANTIALIAS_ON );
217 g.fill( _shape );
218
219 Color original = g2.getColor();
220 if( _foreground != null ) {
221 g2.setColor( _foreground );
222 }
223 _textLayout.draw( g2, x, y );
224
225 g2.setColor( original );
226 }
227
228
229 /***
230 * 描画の準備を行います。
231 */
232 public void prepare( Page page )
233 {
234 if( _textLayout == null ) {
235 AttributedString astring = new AttributedString( _text );
236 astring.addAttribute( TextAttribute.FONT, page.getDefaultFont() );
237 astring.addAttribute( TextAttribute.FOREGROUND, page.getForeground() );
238
239 setTextLayout( new TextLayout(astring.getIterator(), page.getFontRenderContext()) );
240 }
241 }
242 }