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.awt.AlphaComposite; |
22 |
|
import java.awt.Dimension; |
23 |
|
import java.awt.Graphics; |
24 |
|
import java.awt.Graphics2D; |
25 |
|
import java.awt.Insets; |
26 |
|
import java.awt.Point; |
27 |
|
import java.awt.RenderingHints; |
28 |
|
|
29 |
|
import java.awt.font.FontRenderContext; |
30 |
|
|
31 |
|
import java.util.List; |
32 |
|
|
33 |
|
import javax.swing.BorderFactory; |
34 |
|
import javax.swing.JComponent; |
35 |
|
|
36 |
|
import javax.swing.border.Border; |
37 |
|
|
38 |
|
import tsukuba_bunko.peko.Logger; |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
public class TextCanvas extends JComponent { |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
private static final long serialVersionUID = 2600492357487531048L; |
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
0 |
private Page _page = null; |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
0 |
private List _lines = null; |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
0 |
private AlphaComposite _alphaComposite = AlphaComposite.getInstance( AlphaComposite.DST_OVER, 0.5f ); |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
0 |
private Dimension _size = new Dimension(); |
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
0 |
private Point _location = new Point( 0, 0 ); |
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
0 |
private Insets _padding = new Insets( 0, 0, 0, 0 ); |
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
0 |
private FontRenderContext _frc = null; |
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
0 |
private Marker _marker = null; |
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
public TextCanvas() |
102 |
|
{ |
103 |
0 |
super(); |
104 |
0 |
initialize(); |
105 |
0 |
} |
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
public FontRenderContext getFontRenderContext() |
113 |
|
{ |
114 |
0 |
if( _frc == null ) { |
115 |
0 |
synchronized( this ) { |
116 |
0 |
if( _frc == null ) { |
117 |
|
try { |
118 |
0 |
Logger.debug( "[canvas.text] waiting for create cached FontRenderContext." ); |
119 |
0 |
wait(); |
120 |
0 |
Logger.debug( "[canvas.text] creating cached FontRenderContext done." ); |
121 |
|
} |
122 |
0 |
catch( InterruptedException ie ) { |
123 |
0 |
Logger.error( "[canvas.text] interrupted." ); |
124 |
0 |
} |
125 |
|
} |
126 |
0 |
} |
127 |
|
} |
128 |
0 |
return _frc; |
129 |
|
} |
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
public AlphaComposite getAlphaComposite() |
136 |
|
{ |
137 |
0 |
return _alphaComposite; |
138 |
|
} |
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
|
143 |
|
public void updateCanvas() |
144 |
|
{ |
145 |
0 |
if( !isEnabled() ) { |
146 |
0 |
return; |
147 |
|
} |
148 |
|
|
149 |
0 |
Logger.debug( "[canvas.text] update TextCanvas view" ); |
150 |
0 |
_lines = null; |
151 |
0 |
_page.getSize( _size ); |
152 |
0 |
_page.getLocation( _location ); |
153 |
0 |
_page.getPadding( _padding ); |
154 |
0 |
setForeground( _page.getForeground() ); |
155 |
0 |
setBackground( _page.getBackground() ); |
156 |
0 |
float trans = _page.getTransparency(); |
157 |
0 |
if( (_alphaComposite == null) || (_alphaComposite.getAlpha() != trans) ) { |
158 |
0 |
_alphaComposite = AlphaComposite.getInstance( AlphaComposite.SRC_OVER, trans ); |
159 |
|
} |
160 |
0 |
_marker.setText( "▼", _page ); |
161 |
0 |
} |
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
|
166 |
|
public void updateText() |
167 |
|
{ |
168 |
0 |
if( !isEnabled() ) { |
169 |
0 |
return; |
170 |
|
} |
171 |
|
|
172 |
0 |
Logger.debug( "[canvas.text] update texts." ); |
173 |
0 |
_marker.setVisible( false ); |
174 |
|
|
175 |
0 |
List lines = _page.getLines(); |
176 |
0 |
int size = lines.size(); |
177 |
0 |
for( int i = 0; i < size; ++i ) { |
178 |
0 |
((Line)lines.get(i)).prepare( _page ); |
179 |
|
} |
180 |
0 |
_lines = lines; |
181 |
|
|
182 |
|
|
183 |
0 |
tsukuba_bunko.peko.PekoSystem.getInstance().getCanvasManager().showTextCanvas(); |
184 |
0 |
repaint(); |
185 |
|
|
186 |
|
try { |
187 |
0 |
synchronized( this ) { |
188 |
0 |
wait( 100 ); |
189 |
0 |
} |
190 |
|
} |
191 |
0 |
catch( Exception e ) { |
192 |
0 |
} |
193 |
0 |
_marker.setVisible( true ); |
194 |
0 |
} |
195 |
|
|
196 |
|
|
197 |
|
|
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
public void setPage( Page page ) |
202 |
|
{ |
203 |
0 |
_page = page; |
204 |
0 |
_page.setTextCanvas( this ); |
205 |
0 |
} |
206 |
|
|
207 |
|
|
208 |
|
|
209 |
|
|
210 |
|
|
211 |
|
public Page getPage() |
212 |
|
{ |
213 |
0 |
return _page; |
214 |
|
} |
215 |
|
|
216 |
|
public void paintPageBackground( Graphics g ) |
217 |
|
{ |
218 |
0 |
Dimension size = _size; |
219 |
0 |
Point location = _location; |
220 |
|
|
221 |
0 |
Graphics2D g2 = (Graphics2D)g.create( location.x, location.y, size.width, size.height ); |
222 |
0 |
g2.setComposite( _alphaComposite ); |
223 |
0 |
g2.setColor( getBackground() ); |
224 |
0 |
g2.fillRect( 0, 0, size.width, size.height ); |
225 |
0 |
g2.dispose(); |
226 |
0 |
} |
227 |
|
|
228 |
|
|
229 |
|
|
230 |
|
|
231 |
|
private void initialize() |
232 |
|
{ |
233 |
0 |
setLayout( null ); |
234 |
0 |
setDoubleBuffered( false ); |
235 |
0 |
setBorder( BorderFactory.createEtchedBorder() ); |
236 |
|
|
237 |
0 |
_marker = new Marker(); |
238 |
0 |
add( _marker ); |
239 |
0 |
_marker.setLocation( 0, 0 ); |
240 |
0 |
_marker.setVisible( false ); |
241 |
0 |
} |
242 |
|
|
243 |
|
|
244 |
|
|
245 |
|
|
246 |
|
|
247 |
|
public void setVisible( boolean visibility ) |
248 |
|
{ |
249 |
0 |
Logger.debug( "[canvas.text] set visibility :" + visibility ); |
250 |
0 |
if( visibility ) { |
251 |
0 |
_marker.start(); |
252 |
|
} |
253 |
0 |
super.setVisible( visibility ); |
254 |
0 |
} |
255 |
|
|
256 |
|
public void addNotify() |
257 |
|
{ |
258 |
0 |
super.addNotify(); |
259 |
0 |
Logger.debug( "[canvas.text] added notify to TextCavas" ); |
260 |
0 |
if( _frc == null ) { |
261 |
0 |
Logger.debug( "[canvas.text] try create new font render context." ); |
262 |
0 |
synchronized( this ) { |
263 |
0 |
if( _frc == null ) { |
264 |
0 |
Graphics2D g2 = (Graphics2D)getGraphics(); |
265 |
0 |
g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ); |
266 |
0 |
_frc = g2.getFontRenderContext(); |
267 |
0 |
Logger.debug( "[canvas] notify all thread waiting at TextCanvas" ); |
268 |
0 |
notifyAll(); |
269 |
|
} |
270 |
0 |
} |
271 |
|
} |
272 |
0 |
} |
273 |
|
|
274 |
|
public void paint( Graphics g ) |
275 |
|
{ |
276 |
0 |
List lines = _lines; |
277 |
0 |
if( (lines == null) || lines.isEmpty() ) { |
278 |
0 |
return; |
279 |
|
} |
280 |
|
else { |
281 |
0 |
super.paint( g ); |
282 |
|
} |
283 |
0 |
} |
284 |
|
|
285 |
|
public void paintBorder( Graphics g ) |
286 |
|
{ |
287 |
0 |
Border border = getBorder(); |
288 |
0 |
if( border != null ) { |
289 |
0 |
border.paintBorder( this, g, _location.x, _location.y, _size.width, _size.height ); |
290 |
|
} |
291 |
0 |
} |
292 |
|
|
293 |
|
public void paintComponent( Graphics g ) |
294 |
|
{ |
295 |
0 |
List lines = _lines; |
296 |
|
|
297 |
0 |
Point location = _location; |
298 |
0 |
Insets padding = _padding; |
299 |
|
|
300 |
|
|
301 |
|
|
302 |
0 |
Graphics2D g2 = (Graphics2D)g; |
303 |
0 |
float x = (class="keyword">float)(location.x + padding.left); |
304 |
0 |
float y = (class="keyword">float)(location.y + padding.top); |
305 |
0 |
float tail = 0f; |
306 |
0 |
int length = lines.size(); |
307 |
0 |
Line line = null; |
308 |
0 |
for( int i = 0; i < length; ++i ) { |
309 |
0 |
line = (Line)lines.get( i ); |
310 |
0 |
y += line.getAscent(); |
311 |
0 |
line.draw( g2, x, y ); |
312 |
0 |
y += line.getDescent(); |
313 |
0 |
tail = line.getAdavance(); |
314 |
|
} |
315 |
0 |
_marker.setPosition( (int)(tail + 5f + x), (class="keyword">int)y ); |
316 |
0 |
} |
317 |
|
} |