| 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.Color; |
| 22 |
|
import java.awt.Dimension; |
| 23 |
|
import java.awt.Font; |
| 24 |
|
import java.awt.Insets; |
| 25 |
|
import java.awt.Point; |
| 26 |
|
|
| 27 |
|
import java.awt.font.FontRenderContext; |
| 28 |
|
|
| 29 |
|
import java.io.Serializable; |
| 30 |
|
|
| 31 |
|
import java.util.List; |
| 32 |
|
|
| 33 |
|
import tsukuba_bunko.peko.Logger; |
| 34 |
|
import tsukuba_bunko.peko.PekoSystem; |
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
public class Page implements Cloneable, Serializable { |
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
private static final long serialVersionUID = 2116822706503877518L; |
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
0 |
transient private TextCanvas _canvas = null; |
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
0 |
private List _committedLines = null; |
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
0 |
private float _commitedPageHeight = 0f; |
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
0 |
transient private List _lines = null; |
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
0 |
transient private float _pageHeight = 0f; |
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 81 |
0 |
transient private Font _defaultFont = null; |
| 82 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
0 |
private Dimension _size = new Dimension(); |
| 87 |
|
|
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 |
0 |
private Point _location = new Point( 0, 0 ); |
| 92 |
|
|
| 93 |
|
|
| 94 |
|
|
| 95 |
|
|
| 96 |
0 |
private Insets _padding = new Insets( 10, 10, 10, 10 ); |
| 97 |
|
|
| 98 |
|
|
| 99 |
|
|
| 100 |
|
|
| 101 |
0 |
private Color _foreground = Color.white; |
| 102 |
|
|
| 103 |
|
|
| 104 |
|
|
| 105 |
|
|
| 106 |
0 |
private Color _shadow = Color.black; |
| 107 |
|
|
| 108 |
|
|
| 109 |
|
|
| 110 |
|
|
| 111 |
0 |
private Color _background = Color.black; |
| 112 |
|
|
| 113 |
|
|
| 114 |
|
|
| 115 |
|
|
| 116 |
0 |
private float _transparency = 0.5f; |
| 117 |
|
|
| 118 |
|
|
| 119 |
|
|
| 120 |
|
|
| 121 |
|
|
| 122 |
|
public Page() |
| 123 |
|
{ |
| 124 |
0 |
super(); |
| 125 |
0 |
} |
| 126 |
|
|
| 127 |
|
|
| 128 |
|
|
| 129 |
|
|
| 130 |
|
|
| 131 |
|
|
| 132 |
|
void setTextCanvas( TextCanvas canvas ) |
| 133 |
|
{ |
| 134 |
0 |
_canvas = canvas; |
| 135 |
0 |
} |
| 136 |
|
|
| 137 |
|
|
| 138 |
|
|
| 139 |
|
|
| 140 |
|
|
| 141 |
|
public TextCanvas getTextCanvas() |
| 142 |
|
{ |
| 143 |
0 |
return _canvas; |
| 144 |
|
} |
| 145 |
|
|
| 146 |
|
|
| 147 |
|
|
| 148 |
|
|
| 149 |
|
|
| 150 |
|
|
| 151 |
|
public void setDefaultFont( Font font ) |
| 152 |
|
{ |
| 153 |
0 |
_defaultFont = font; |
| 154 |
0 |
} |
| 155 |
|
|
| 156 |
|
|
| 157 |
|
|
| 158 |
|
|
| 159 |
|
|
| 160 |
|
public Font getDefaultFont() |
| 161 |
|
{ |
| 162 |
0 |
return _defaultFont; |
| 163 |
|
} |
| 164 |
|
|
| 165 |
|
|
| 166 |
|
|
| 167 |
|
|
| 168 |
|
|
| 169 |
|
public FontRenderContext getFontRenderContext() |
| 170 |
|
{ |
| 171 |
0 |
if( _canvas == null ) { |
| 172 |
0 |
return null; |
| 173 |
|
} |
| 174 |
|
else { |
| 175 |
0 |
return _canvas.getFontRenderContext(); |
| 176 |
|
} |
| 177 |
|
} |
| 178 |
|
|
| 179 |
|
|
| 180 |
|
|
| 181 |
|
|
| 182 |
|
|
| 183 |
|
public void setSize( Dimension size ) |
| 184 |
|
{ |
| 185 |
0 |
_size.setSize( size ); |
| 186 |
0 |
} |
| 187 |
|
|
| 188 |
|
|
| 189 |
|
|
| 190 |
|
|
| 191 |
|
|
| 192 |
|
public Dimension getSize() |
| 193 |
|
{ |
| 194 |
0 |
return getSize( new Dimension() ); |
| 195 |
|
} |
| 196 |
|
|
| 197 |
|
|
| 198 |
|
|
| 199 |
|
|
| 200 |
|
|
| 201 |
|
|
| 202 |
|
public Dimension getSize( Dimension d ) |
| 203 |
|
{ |
| 204 |
0 |
if( d == null ) { |
| 205 |
0 |
d = new Dimension(); |
| 206 |
|
} |
| 207 |
0 |
d.setSize( _size ); |
| 208 |
0 |
return d; |
| 209 |
|
} |
| 210 |
|
|
| 211 |
|
|
| 212 |
|
|
| 213 |
|
|
| 214 |
|
|
| 215 |
|
|
| 216 |
|
public void setLocation( int x, class="keyword">int y ) |
| 217 |
|
{ |
| 218 |
0 |
_location.x = x; |
| 219 |
0 |
_location.y = y; |
| 220 |
0 |
} |
| 221 |
|
|
| 222 |
|
|
| 223 |
|
|
| 224 |
|
|
| 225 |
|
|
| 226 |
|
public Point getMargin() |
| 227 |
|
{ |
| 228 |
0 |
return getLocation( null ); |
| 229 |
|
} |
| 230 |
|
|
| 231 |
|
|
| 232 |
|
|
| 233 |
|
|
| 234 |
|
|
| 235 |
|
|
| 236 |
|
public Point getLocation( Point location ) |
| 237 |
|
{ |
| 238 |
0 |
if( location == null ) { |
| 239 |
0 |
location = (Point)_location.clone(); |
| 240 |
0 |
} |
| 241 |
|
else { |
| 242 |
0 |
location.x = _location.x; |
| 243 |
0 |
location.y = _location.y; |
| 244 |
|
} |
| 245 |
0 |
return location; |
| 246 |
|
} |
| 247 |
|
|
| 248 |
|
|
| 249 |
|
|
| 250 |
|
|
| 251 |
|
|
| 252 |
|
|
| 253 |
|
|
| 254 |
|
|
| 255 |
|
public void setPadding( int top, class="keyword">int left, class="keyword">int bottom, class="keyword">int right ) |
| 256 |
|
{ |
| 257 |
0 |
_padding.top = top; |
| 258 |
0 |
_padding.left = left; |
| 259 |
0 |
_padding.bottom = bottom; |
| 260 |
0 |
_padding.right = right; |
| 261 |
0 |
} |
| 262 |
|
|
| 263 |
|
|
| 264 |
|
|
| 265 |
|
|
| 266 |
|
|
| 267 |
|
public Insets getPadding() |
| 268 |
|
{ |
| 269 |
0 |
return getPadding( null ); |
| 270 |
|
} |
| 271 |
|
|
| 272 |
|
|
| 273 |
|
|
| 274 |
|
|
| 275 |
|
|
| 276 |
|
|
| 277 |
|
public Insets getPadding( Insets padding ) |
| 278 |
|
{ |
| 279 |
0 |
if( padding == null ) { |
| 280 |
0 |
padding = (Insets)_padding.clone(); |
| 281 |
0 |
} |
| 282 |
|
else { |
| 283 |
0 |
padding.top = _padding.top; |
| 284 |
0 |
padding.left = _padding.left; |
| 285 |
0 |
padding.bottom = _padding.bottom; |
| 286 |
0 |
padding.right = _padding.right; |
| 287 |
|
} |
| 288 |
0 |
return padding; |
| 289 |
|
} |
| 290 |
|
|
| 291 |
|
|
| 292 |
|
|
| 293 |
|
|
| 294 |
|
|
| 295 |
|
public void setForeground( Color foreground ) |
| 296 |
|
{ |
| 297 |
0 |
_foreground = foreground; |
| 298 |
0 |
} |
| 299 |
|
|
| 300 |
|
|
| 301 |
|
|
| 302 |
|
|
| 303 |
|
|
| 304 |
|
public Color getForeground() |
| 305 |
|
{ |
| 306 |
0 |
return _foreground; |
| 307 |
|
} |
| 308 |
|
|
| 309 |
|
|
| 310 |
|
|
| 311 |
|
|
| 312 |
|
|
| 313 |
|
public void setShadow( Color shadow ) |
| 314 |
|
{ |
| 315 |
0 |
_shadow = shadow; |
| 316 |
0 |
} |
| 317 |
|
|
| 318 |
|
|
| 319 |
|
|
| 320 |
|
|
| 321 |
|
|
| 322 |
|
public Color getShadow() |
| 323 |
|
{ |
| 324 |
0 |
return _shadow; |
| 325 |
|
} |
| 326 |
|
|
| 327 |
|
|
| 328 |
|
|
| 329 |
|
|
| 330 |
|
|
| 331 |
|
public void setBackground( Color background ) |
| 332 |
|
{ |
| 333 |
0 |
_background = background; |
| 334 |
0 |
} |
| 335 |
|
|
| 336 |
|
|
| 337 |
|
|
| 338 |
|
|
| 339 |
|
|
| 340 |
|
public Color getBackground() |
| 341 |
|
{ |
| 342 |
0 |
return _background; |
| 343 |
|
} |
| 344 |
|
|
| 345 |
|
|
| 346 |
|
|
| 347 |
|
|
| 348 |
|
|
| 349 |
|
public void setTransparency( float trans ) |
| 350 |
|
{ |
| 351 |
0 |
_transparency = trans; |
| 352 |
0 |
} |
| 353 |
|
|
| 354 |
|
|
| 355 |
|
|
| 356 |
|
|
| 357 |
|
|
| 358 |
|
public float getTransparency() |
| 359 |
|
{ |
| 360 |
0 |
return _transparency; |
| 361 |
|
} |
| 362 |
|
|
| 363 |
|
|
| 364 |
|
|
| 365 |
|
|
| 366 |
|
|
| 367 |
|
|
| 368 |
|
public float getMaxLineWidth() |
| 369 |
|
{ |
| 370 |
0 |
int width = _size.width; |
| 371 |
0 |
width -= _padding.left; |
| 372 |
0 |
width -= _padding.right; |
| 373 |
|
|
| 374 |
0 |
return (float)width; |
| 375 |
|
} |
| 376 |
|
|
| 377 |
|
|
| 378 |
|
|
| 379 |
|
|
| 380 |
|
|
| 381 |
|
public synchronized void addLine( Line line ) |
| 382 |
|
{ |
| 383 |
0 |
if( _lines == null ) { |
| 384 |
0 |
_lines = new java.util.ArrayList(); |
| 385 |
|
} |
| 386 |
0 |
_lines.add( line ); |
| 387 |
0 |
_pageHeight += line.getAscent(); |
| 388 |
0 |
_pageHeight += line.getDescent(); |
| 389 |
0 |
} |
| 390 |
|
|
| 391 |
|
|
| 392 |
|
|
| 393 |
|
|
| 394 |
|
|
| 395 |
|
public int getLineCount() |
| 396 |
|
{ |
| 397 |
0 |
if( _lines != null ) { |
| 398 |
0 |
return _lines.size(); |
| 399 |
|
} |
| 400 |
|
else { |
| 401 |
0 |
return 0; |
| 402 |
|
} |
| 403 |
|
} |
| 404 |
|
|
| 405 |
|
|
| 406 |
|
|
| 407 |
|
|
| 408 |
|
|
| 409 |
|
|
| 410 |
|
public synchronized void setLine( int index, Line line ) |
| 411 |
|
{ |
| 412 |
|
try { |
| 413 |
0 |
if( (_lines == null) || (index < 0) || (index >= _lines.size()) ) { |
| 414 |
0 |
Logger.debug( "[canvas.text] invalid length! index:" + index + " size:" + ((_lines != null)?_lines.size():0) ); |
| 415 |
|
} |
| 416 |
|
|
| 417 |
0 |
synchronized( this ) { |
| 418 |
0 |
Line old = (Line)_lines.get( index ); |
| 419 |
0 |
_pageHeight -= (old.getAscent() + old.getDescent()); |
| 420 |
0 |
_lines.set( index, line ); |
| 421 |
0 |
_pageHeight += (line.getAscent() + line.getDescent()); |
| 422 |
0 |
} |
| 423 |
|
} |
| 424 |
0 |
catch( IndexOutOfBoundsException ioobe ) { |
| 425 |
0 |
Logger.error( "[canvas] invalid line number.", ioobe ); |
| 426 |
0 |
throw ioobe; |
| 427 |
0 |
} |
| 428 |
0 |
} |
| 429 |
|
|
| 430 |
|
|
| 431 |
|
|
| 432 |
|
|
| 433 |
|
|
| 434 |
|
|
| 435 |
|
|
| 436 |
|
public void removeLine( int index ) |
| 437 |
|
{ |
| 438 |
|
try { |
| 439 |
0 |
if( _lines == null ) { |
| 440 |
0 |
throw new IndexOutOfBoundsException( "there is still no line." ); |
| 441 |
|
} |
| 442 |
|
|
| 443 |
0 |
synchronized( this ) { |
| 444 |
0 |
Line line = (Line)_lines.remove( index ); |
| 445 |
0 |
_pageHeight -= (line.getAscent() + line.getDescent()); |
| 446 |
0 |
} |
| 447 |
|
} |
| 448 |
0 |
catch( IndexOutOfBoundsException ioobe ) { |
| 449 |
0 |
Logger.error( "[canvas] invalid line number.", ioobe ); |
| 450 |
0 |
throw ioobe; |
| 451 |
0 |
} |
| 452 |
0 |
} |
| 453 |
|
|
| 454 |
|
|
| 455 |
|
|
| 456 |
|
|
| 457 |
|
public List getLines() |
| 458 |
|
{ |
| 459 |
0 |
if( _lines != null ) { |
| 460 |
0 |
return (List)((java.util.ArrayList)_lines).clone(); |
| 461 |
|
} |
| 462 |
|
else { |
| 463 |
0 |
return null; |
| 464 |
|
} |
| 465 |
|
} |
| 466 |
|
|
| 467 |
|
|
| 468 |
|
|
| 469 |
|
|
| 470 |
|
public void clearLines() |
| 471 |
|
{ |
| 472 |
0 |
synchronized( this ) { |
| 473 |
0 |
if( _lines != null ) { |
| 474 |
0 |
_lines.clear(); |
| 475 |
0 |
_pageHeight = 0f; |
| 476 |
|
} |
| 477 |
0 |
} |
| 478 |
0 |
} |
| 479 |
|
|
| 480 |
|
|
| 481 |
|
|
| 482 |
|
|
| 483 |
|
|
| 484 |
|
|
| 485 |
|
public boolean isAdaptive( Line line ) |
| 486 |
|
{ |
| 487 |
0 |
if( line == null ) { |
| 488 |
0 |
throw new NullPointerException( "てめぇ、なめてんじゃねぇぞぐらぁ!" ); |
| 489 |
|
} |
| 490 |
0 |
float pageHeight = _pageHeight + _padding.top + _padding.bottom; |
| 491 |
0 |
return ((pageHeight + line.getAscent() + line.getDescent()) < (float)_size.height); |
| 492 |
|
} |
| 493 |
|
|
| 494 |
|
|
| 495 |
|
|
| 496 |
|
|
| 497 |
|
|
| 498 |
|
public void updateCanvas() |
| 499 |
|
{ |
| 500 |
0 |
if( _canvas != null ) { |
| 501 |
0 |
_canvas.updateCanvas(); |
| 502 |
|
} |
| 503 |
0 |
} |
| 504 |
|
|
| 505 |
|
|
| 506 |
|
|
| 507 |
|
|
| 508 |
|
public void updateContents() |
| 509 |
|
{ |
| 510 |
0 |
if( _canvas != null ) { |
| 511 |
0 |
if( !_canvas.isVisible() ) { |
| 512 |
|
|
| 513 |
0 |
PekoSystem.getInstance().getActionControler().stop( 300L ); |
| 514 |
|
} |
| 515 |
0 |
_canvas.updateText(); |
| 516 |
|
} |
| 517 |
0 |
} |
| 518 |
|
|
| 519 |
|
|
| 520 |
|
|
| 521 |
|
|
| 522 |
|
public void commit() |
| 523 |
|
{ |
| 524 |
0 |
Logger.debug( "[canvas.text] commit." ); |
| 525 |
0 |
if( _lines != null ) { |
| 526 |
0 |
if( _committedLines != null ) { |
| 527 |
0 |
_committedLines.clear(); |
| 528 |
0 |
_committedLines.addAll( _lines ); |
| 529 |
0 |
} |
| 530 |
|
else { |
| 531 |
0 |
_committedLines = (List)((java.util.ArrayList)_lines).clone(); |
| 532 |
|
} |
| 533 |
0 |
Logger.debug( "[canvas.text] committed lines :" + _committedLines.size() ); |
| 534 |
|
} |
| 535 |
0 |
_commitedPageHeight = _pageHeight; |
| 536 |
0 |
} |
| 537 |
|
|
| 538 |
|
|
| 539 |
|
|
| 540 |
|
|
| 541 |
|
public void rollback() |
| 542 |
|
{ |
| 543 |
0 |
Logger.debug( "[canvas.text] rollback." ); |
| 544 |
0 |
if( _lines != null ) { |
| 545 |
0 |
_lines.clear(); |
| 546 |
|
} |
| 547 |
0 |
_lines = _committedLines; |
| 548 |
0 |
_committedLines = null; |
| 549 |
0 |
_pageHeight = _commitedPageHeight; |
| 550 |
0 |
} |
| 551 |
|
|
| 552 |
|
|
| 553 |
|
|
| 554 |
|
|
| 555 |
|
|
| 556 |
|
public Object clone() |
| 557 |
|
{ |
| 558 |
0 |
return clone( new Page() ); |
| 559 |
|
} |
| 560 |
|
|
| 561 |
|
|
| 562 |
|
|
| 563 |
|
|
| 564 |
|
|
| 565 |
|
|
| 566 |
|
public Object clone( Page copy ) |
| 567 |
|
{ |
| 568 |
0 |
synchronized( this ) { |
| 569 |
0 |
if( _lines != null ) { |
| 570 |
0 |
copy._lines = (List)((java.util.ArrayList)_lines).clone(); |
| 571 |
|
} |
| 572 |
0 |
} |
| 573 |
0 |
copy._defaultFont = _defaultFont; |
| 574 |
0 |
copy._size = (Dimension)_size.clone(); |
| 575 |
0 |
copy._location = (Point)_location.clone(); |
| 576 |
0 |
copy._padding = (Insets)_padding.clone(); |
| 577 |
0 |
copy._foreground = _foreground; |
| 578 |
0 |
copy._background = _background; |
| 579 |
0 |
copy._transparency = _transparency; |
| 580 |
|
|
| 581 |
0 |
return copy; |
| 582 |
|
} |
| 583 |
|
} |