J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1998-2005 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | /* |
| 26 | * |
| 27 | * (C) Copyright IBM Corp. 1998-2003 - All Rights Reserved |
| 28 | */ |
| 29 | |
| 30 | package sun.font; |
| 31 | |
| 32 | import java.awt.Font; |
| 33 | import java.awt.Graphics2D; |
| 34 | import java.awt.Rectangle; |
| 35 | import java.awt.Shape; |
| 36 | |
| 37 | import java.awt.font.FontRenderContext; |
| 38 | import java.awt.font.GlyphJustificationInfo; |
| 39 | import java.awt.font.GlyphMetrics; |
| 40 | import java.awt.font.LineMetrics; |
| 41 | import java.awt.font.TextAttribute; |
| 42 | |
| 43 | import java.awt.geom.AffineTransform; |
| 44 | import java.awt.geom.Point2D; |
| 45 | import java.awt.geom.Rectangle2D; |
| 46 | |
| 47 | import java.util.Map; |
| 48 | |
| 49 | /** |
| 50 | * Default implementation of ExtendedTextLabel. |
| 51 | */ |
| 52 | |
| 53 | // {jbr} I made this class package-private to keep the |
| 54 | // Decoration.Label API package-private. |
| 55 | |
| 56 | /* public */ |
| 57 | class ExtendedTextSourceLabel extends ExtendedTextLabel implements Decoration.Label { |
| 58 | |
| 59 | TextSource source; |
| 60 | private Decoration decorator; |
| 61 | |
| 62 | // caches |
| 63 | private Font font; |
| 64 | private AffineTransform baseTX; |
| 65 | private CoreMetrics cm; |
| 66 | |
| 67 | Rectangle2D lb; |
| 68 | Rectangle2D ab; |
| 69 | Rectangle2D vb; |
| 70 | Rectangle2D ib; |
| 71 | StandardGlyphVector gv; |
| 72 | float[] charinfo; |
| 73 | |
| 74 | /** |
| 75 | * Create from a TextSource. |
| 76 | */ |
| 77 | public ExtendedTextSourceLabel(TextSource source, Decoration decorator) { |
| 78 | this.source = source; |
| 79 | this.decorator = decorator; |
| 80 | finishInit(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Create from a TextSource, optionally using cached data from oldLabel starting at the offset. |
| 85 | * If present oldLabel must have been created from a run of text that includes the text used in |
| 86 | * the new label. Start in source corresponds to logical character offset in oldLabel. |
| 87 | */ |
| 88 | public ExtendedTextSourceLabel(TextSource source, ExtendedTextSourceLabel oldLabel, int offset) { |
| 89 | // currently no optimization. |
| 90 | this.source = source; |
| 91 | this.decorator = oldLabel.decorator; |
| 92 | finishInit(); |
| 93 | } |
| 94 | |
| 95 | private void finishInit() { |
| 96 | font = source.getFont(); |
| 97 | |
| 98 | Map<TextAttribute, ?> atts = font.getAttributes(); |
| 99 | baseTX = AttributeValues.getBaselineTransform(atts); |
| 100 | if (baseTX == null){ |
| 101 | cm = source.getCoreMetrics(); |
| 102 | } else { |
| 103 | AffineTransform charTX = AttributeValues.getCharTransform(atts); |
| 104 | if (charTX == null) { |
| 105 | charTX = new AffineTransform(); |
| 106 | } |
| 107 | font = font.deriveFont(charTX); |
| 108 | |
| 109 | LineMetrics lm = font.getLineMetrics(source.getChars(), source.getStart(), |
| 110 | source.getStart() + source.getLength(), source.getFRC()); |
| 111 | cm = CoreMetrics.get(lm); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | |
| 116 | // TextLabel API |
| 117 | |
| 118 | public Rectangle2D getLogicalBounds() { |
| 119 | return getLogicalBounds(0, 0); |
| 120 | } |
| 121 | |
| 122 | public Rectangle2D getLogicalBounds(float x, float y) { |
| 123 | if (lb == null) { |
| 124 | lb = createLogicalBounds(); |
| 125 | } |
| 126 | return new Rectangle2D.Float((float)(lb.getX() + x), |
| 127 | (float)(lb.getY() + y), |
| 128 | (float)lb.getWidth(), |
| 129 | (float)lb.getHeight()); |
| 130 | } |
| 131 | |
| 132 | public float getAdvance() { |
| 133 | if (lb == null) { |
| 134 | lb = createLogicalBounds(); |
| 135 | } |
| 136 | return (float)lb.getWidth(); |
| 137 | } |
| 138 | |
| 139 | public Rectangle2D getVisualBounds(float x, float y) { |
| 140 | if (vb == null) { |
| 141 | vb = decorator.getVisualBounds(this); |
| 142 | } |
| 143 | return new Rectangle2D.Float((float)(vb.getX() + x), |
| 144 | (float)(vb.getY() + y), |
| 145 | (float)vb.getWidth(), |
| 146 | (float)vb.getHeight()); |
| 147 | } |
| 148 | |
| 149 | public Rectangle2D getAlignBounds(float x, float y) { |
| 150 | if (ab == null) { |
| 151 | ab = createAlignBounds(); |
| 152 | } |
| 153 | return new Rectangle2D.Float((float)(ab.getX() + x), |
| 154 | (float)(ab.getY() + y), |
| 155 | (float)ab.getWidth(), |
| 156 | (float)ab.getHeight()); |
| 157 | |
| 158 | } |
| 159 | |
| 160 | public Rectangle2D getItalicBounds(float x, float y) { |
| 161 | if (ib == null) { |
| 162 | ib = createItalicBounds(); |
| 163 | } |
| 164 | return new Rectangle2D.Float((float)(ib.getX() + x), |
| 165 | (float)(ib.getY() + y), |
| 166 | (float)ib.getWidth(), |
| 167 | (float)ib.getHeight()); |
| 168 | |
| 169 | } |
| 170 | |
| 171 | public Rectangle getPixelBounds(FontRenderContext frc, float x, float y) { |
| 172 | return getGV().getPixelBounds(frc, x, y); |
| 173 | } |
| 174 | |
| 175 | public boolean isSimple() { |
| 176 | return decorator == Decoration.getPlainDecoration() && |
| 177 | baseTX == null; |
| 178 | } |
| 179 | |
| 180 | public AffineTransform getBaselineTransform() { |
| 181 | return baseTX; // passing internal object, caller must not modify! |
| 182 | } |
| 183 | |
| 184 | public Shape handleGetOutline(float x, float y) { |
| 185 | return getGV().getOutline(x, y); |
| 186 | } |
| 187 | |
| 188 | public Shape getOutline(float x, float y) { |
| 189 | return decorator.getOutline(this, x, y); |
| 190 | } |
| 191 | |
| 192 | public void handleDraw(Graphics2D g, float x, float y) { |
| 193 | g.drawGlyphVector(getGV(), x, y); |
| 194 | } |
| 195 | |
| 196 | public void draw(Graphics2D g, float x, float y) { |
| 197 | decorator.drawTextAndDecorations(this, g, x, y); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * The logical bounds extends from the origin of the glyphvector to the |
| 202 | * position at which a following glyphvector's origin should be placed. |
| 203 | * We always assume glyph vectors are rendered from left to right, so |
| 204 | * the origin is always to the left. |
| 205 | * <p> On a left-to-right run, combining marks and 'ligatured away' |
| 206 | * characters are to the right of their base characters. The charinfo |
| 207 | * array will record the character positions for these 'missing' characters |
| 208 | * as being at the origin+advance of the base glyph, with zero advance. |
| 209 | * (This is not necessarily the same as the glyph position, for example, |
| 210 | * an umlaut glyph may have a position to the left of this point, it depends |
| 211 | * on whether the font was designed so that such glyphs overhang to the left |
| 212 | * of their origin, or whether it presumes some kind of kerning to position |
| 213 | * the glyphs). Anyway, the left of the bounds is the origin of the first |
| 214 | * logical (leftmost) character, and the right is the origin + advance of the |
| 215 | * last logical (rightmost) character. |
| 216 | * <p> On a right-to-left run, these special characters are to the left |
| 217 | * of their base characters. Again, since 'glyph position' has been abstracted |
| 218 | * away, we can use the origin of the leftmost character, and the origin + |
| 219 | * advance of the rightmost character. |
| 220 | * <p> On a mixed run (hindi) we can't rely on the first logical character |
| 221 | * being the leftmost character. However we can again rely on the leftmost |
| 222 | * character origin and the rightmost character + advance. |
| 223 | */ |
| 224 | protected Rectangle2D createLogicalBounds() { |
| 225 | return getGV().getLogicalBounds(); |
| 226 | } |
| 227 | |
| 228 | public Rectangle2D handleGetVisualBounds() { |
| 229 | return getGV().getVisualBounds(); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Like createLogicalBounds except ignore leading and logically trailing white space. |
| 234 | * this assumes logically trailing whitespace is also visually trailing. |
| 235 | * Whitespace is anything that has a zero visual width, regardless of its advance. |
| 236 | * <p> We make the same simplifying assumptions as in createLogicalBounds, namely |
| 237 | * that we can rely on the charinfo to shield us from any glyph positioning oddities |
| 238 | * in the font that place the glyph for a character at other than the pos + advance |
| 239 | * of the character to its left. So we no longer need to skip chars with zero |
| 240 | * advance, as their bounds (right and left) are already correct. |
| 241 | */ |
| 242 | protected Rectangle2D createAlignBounds() { |
| 243 | float[] info = getCharinfo(); |
| 244 | |
| 245 | float al = 0f; |
| 246 | float at = -cm.ascent; |
| 247 | float aw = 0f; |
| 248 | float ah = cm.ascent + cm.descent; |
| 249 | |
| 250 | boolean lineIsLTR = (source.getLayoutFlags() & 0x8) == 0; |
| 251 | int rn = info.length - numvals; |
| 252 | if (lineIsLTR) { |
| 253 | while (rn > 0 && info[rn+visw] == 0) { |
| 254 | rn -= numvals; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | if (rn >= 0) { |
| 259 | int ln = 0; |
| 260 | while (ln < rn && ((info[ln+advx] == 0) || (!lineIsLTR && info[ln+visw] == 0))) { |
| 261 | ln += numvals; |
| 262 | } |
| 263 | |
| 264 | al = Math.max(0f, info[ln+posx]); |
| 265 | aw = info[rn+posx] + info[rn+advx] - al; |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | boolean lineIsLTR = source.lineIsLTR(); |
| 270 | int rn = info.length - numvals; |
| 271 | while (rn > 0 && ((info[rn+advx] == 0) || (lineIsLTR && info[rn+visw] == 0))) { |
| 272 | rn -= numvals; |
| 273 | } |
| 274 | |
| 275 | if (rn >= 0) { |
| 276 | int ln = 0; |
| 277 | while (ln < rn && ((info[ln+advx] == 0) || (!lineIsLTR && info[ln+visw] == 0))) { |
| 278 | ln += numvals; |
| 279 | } |
| 280 | |
| 281 | al = Math.max(0f, info[ln+posx]); |
| 282 | aw = info[rn+posx] + info[rn+advx] - al; |
| 283 | } |
| 284 | */ |
| 285 | |
| 286 | return new Rectangle2D.Float(al, at, aw, ah); |
| 287 | } |
| 288 | |
| 289 | public Rectangle2D createItalicBounds() { |
| 290 | float ia = cm.italicAngle; |
| 291 | |
| 292 | Rectangle2D lb = getLogicalBounds(); |
| 293 | float l = (float)lb.getMinX(); |
| 294 | float t = -cm.ascent; |
| 295 | float r = (float)lb.getMaxX(); |
| 296 | float b = cm.descent; |
| 297 | if (ia != 0) { |
| 298 | if (ia > 0) { |
| 299 | l -= ia * (b - cm.ssOffset); |
| 300 | r -= ia * (t - cm.ssOffset); |
| 301 | } else { |
| 302 | l -= ia * (t - cm.ssOffset); |
| 303 | r -= ia * (b - cm.ssOffset); |
| 304 | } |
| 305 | } |
| 306 | return new Rectangle2D.Float(l, t, r - l, b - t); |
| 307 | } |
| 308 | |
| 309 | private final StandardGlyphVector getGV() { |
| 310 | if (gv == null) { |
| 311 | gv = createGV(); |
| 312 | } |
| 313 | |
| 314 | return gv; |
| 315 | } |
| 316 | |
| 317 | protected StandardGlyphVector createGV() { |
| 318 | FontRenderContext frc = source.getFRC(); |
| 319 | int flags = source.getLayoutFlags(); |
| 320 | char[] context = source.getChars(); |
| 321 | int start = source.getStart(); |
| 322 | int length = source.getLength(); |
| 323 | |
| 324 | GlyphLayout gl = GlyphLayout.get(null); // !!! no custom layout engines |
| 325 | gv = gl.layout(font, frc, context, start, length, flags, null); // ??? use textsource |
| 326 | GlyphLayout.done(gl); |
| 327 | |
| 328 | return gv; |
| 329 | } |
| 330 | |
| 331 | // ExtendedTextLabel API |
| 332 | |
| 333 | private static final int posx = 0, |
| 334 | posy = 1, |
| 335 | advx = 2, |
| 336 | advy = 3, |
| 337 | visx = 4, |
| 338 | visy = 5, |
| 339 | visw = 6, |
| 340 | vish = 7; |
| 341 | private static final int numvals = 8; |
| 342 | |
| 343 | public int getNumCharacters() { |
| 344 | return source.getLength(); |
| 345 | } |
| 346 | |
| 347 | public CoreMetrics getCoreMetrics() { |
| 348 | return cm; |
| 349 | } |
| 350 | |
| 351 | public float getCharX(int index) { |
| 352 | validate(index); |
| 353 | return getCharinfo()[l2v(index) * numvals + posx]; |
| 354 | } |
| 355 | |
| 356 | public float getCharY(int index) { |
| 357 | validate(index); |
| 358 | return getCharinfo()[l2v(index) * numvals + posy]; |
| 359 | } |
| 360 | |
| 361 | public float getCharAdvance(int index) { |
| 362 | validate(index); |
| 363 | return getCharinfo()[l2v(index) * numvals + advx]; |
| 364 | } |
| 365 | |
| 366 | public Rectangle2D handleGetCharVisualBounds(int index) { |
| 367 | validate(index); |
| 368 | float[] charinfo = getCharinfo(); |
| 369 | index = l2v(index) * numvals; |
| 370 | return new Rectangle2D.Float( |
| 371 | charinfo[index + visx], |
| 372 | charinfo[index + visy], |
| 373 | charinfo[index + visw], |
| 374 | charinfo[index + vish]); |
| 375 | } |
| 376 | |
| 377 | public Rectangle2D getCharVisualBounds(int index, float x, float y) { |
| 378 | |
| 379 | Rectangle2D bounds = decorator.getCharVisualBounds(this, index); |
| 380 | if (x != 0 || y != 0) { |
| 381 | bounds.setRect(bounds.getX()+x, |
| 382 | bounds.getY()+y, |
| 383 | bounds.getWidth(), |
| 384 | bounds.getHeight()); |
| 385 | } |
| 386 | return bounds; |
| 387 | } |
| 388 | |
| 389 | private void validate(int index) { |
| 390 | if (index < 0) { |
| 391 | throw new IllegalArgumentException("index " + index + " < 0"); |
| 392 | } else if (index >= source.getLength()) { |
| 393 | throw new IllegalArgumentException("index " + index + " < " + source.getLength()); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | /* |
| 398 | public int hitTestChar(float x, float y) { |
| 399 | // !!! return index of char hit, for swing |
| 400 | // result is negative for trailing-edge hits |
| 401 | // no italics so no problem at margins. |
| 402 | // for now, ignore y since we assume horizontal text |
| 403 | |
| 404 | // find non-combining char origin to right of x |
| 405 | float[] charinfo = getCharinfo(); |
| 406 | |
| 407 | int n = 0; |
| 408 | int e = source.getLength(); |
| 409 | while (n < e && charinfo[n + advx] != 0 && charinfo[n + posx] > x) { |
| 410 | n += numvals; |
| 411 | } |
| 412 | float rightx = n < e ? charinfo[n+posx] : charinfo[e - numvals + posx] + charinfo[e - numvals + advx]; |
| 413 | |
| 414 | // find non-combining char to left of that char |
| 415 | n -= numvals; |
| 416 | while (n >= 0 && charinfo[n+advx] == 0) { |
| 417 | n -= numvals; |
| 418 | } |
| 419 | float leftx = n >= 0 ? charinfo[n+posx] : 0; |
| 420 | float lefta = n >= 0 ? charinfo[n+advx] : 0; |
| 421 | |
| 422 | n /= numvals; |
| 423 | |
| 424 | boolean left = true; |
| 425 | if (x < leftx + lefta / 2f) { |
| 426 | // left of prev char |
| 427 | } else if (x < (leftx + lefta + rightx) / 2f) { |
| 428 | // right of prev char |
| 429 | left = false; |
| 430 | } else { |
| 431 | // left of follow char |
| 432 | n += 1; |
| 433 | } |
| 434 | |
| 435 | if ((source.getLayoutFlags() & 0x1) != 0) { |
| 436 | n = getNumCharacters() - 1 - n; |
| 437 | left = !left; |
| 438 | } |
| 439 | |
| 440 | return left ? n : -n; |
| 441 | } |
| 442 | */ |
| 443 | |
| 444 | public int logicalToVisual(int logicalIndex) { |
| 445 | validate(logicalIndex); |
| 446 | return l2v(logicalIndex); |
| 447 | } |
| 448 | |
| 449 | public int visualToLogical(int visualIndex) { |
| 450 | validate(visualIndex); |
| 451 | return v2l(visualIndex); |
| 452 | } |
| 453 | |
| 454 | public int getLineBreakIndex(int start, float width) { |
| 455 | float[] charinfo = getCharinfo(); |
| 456 | int length = source.getLength(); |
| 457 | --start; |
| 458 | while (width >= 0 && ++start < length) { |
| 459 | float adv = charinfo[l2v(start) * numvals + advx]; |
| 460 | width -= adv; |
| 461 | } |
| 462 | |
| 463 | return start; |
| 464 | } |
| 465 | |
| 466 | public float getAdvanceBetween(int start, int limit) { |
| 467 | float a = 0f; |
| 468 | |
| 469 | float[] charinfo = getCharinfo(); |
| 470 | --start; |
| 471 | while (++start < limit) { |
| 472 | a += charinfo[l2v(start) * numvals + advx]; |
| 473 | } |
| 474 | |
| 475 | return a; |
| 476 | } |
| 477 | |
| 478 | public boolean caretAtOffsetIsValid(int offset) { |
| 479 | // REMIND: improve this implementation |
| 480 | |
| 481 | // Ligature formation can either be done in logical order, |
| 482 | // with the ligature glyph logically preceding the null |
| 483 | // chars; or in visual order, with the ligature glyph to |
| 484 | // the left of the null chars. This method's implementation |
| 485 | // must reflect which strategy is used. |
| 486 | |
| 487 | if (offset == 0 || offset == source.getLength()) { |
| 488 | return true; |
| 489 | } |
| 490 | char c = source.getChars()[source.getStart() + offset]; |
| 491 | if (c == '\t' || c == '\n' || c == '\r') { // hack |
| 492 | return true; |
| 493 | } |
| 494 | int v = l2v(offset); |
| 495 | |
| 496 | // If ligatures are always to the left, do this stuff: |
| 497 | //if (!(source.getLayoutFlags() & 0x1) == 0) { |
| 498 | // v += 1; |
| 499 | // if (v == source.getLength()) { |
| 500 | // return true; |
| 501 | // } |
| 502 | //} |
| 503 | |
| 504 | return getCharinfo()[v * numvals + advx] != 0; |
| 505 | } |
| 506 | |
| 507 | private final float[] getCharinfo() { |
| 508 | if (charinfo == null) { |
| 509 | charinfo = createCharinfo(); |
| 510 | } |
| 511 | return charinfo; |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | * This takes the glyph info record obtained from the glyph vector and converts it into a similar record |
| 516 | * adjusted to represent character data instead. For economy we don't use glyph info records in this processing. |
| 517 | * |
| 518 | * Here are some constraints: |
| 519 | * - there can be more glyphs than characters (glyph insertion, perhaps based on normalization, has taken place) |
| 520 | * - there can not be fewer glyphs than characters (0xffff glyphs are inserted for characters ligaturized away) |
| 521 | * - each glyph maps to a single character, when multiple glyphs exist for a character they all map to it, but |
| 522 | * no two characters map to the same glyph |
| 523 | * - multiple glyphs mapping to the same character need not be in sequence (thai, tamil have split characters) |
| 524 | * - glyphs may be arbitrarily reordered (Indic reorders glyphs) |
| 525 | * - all glyphs share the same bidi level |
| 526 | * - all glyphs share the same horizontal (or vertical) baseline |
| 527 | * - combining marks visually follow their base character in the glyph array-- i.e. in an rtl gv they are |
| 528 | * to the left of their base character-- and have zero advance. |
| 529 | * |
| 530 | * The output maps this to character positions, and therefore caret positions, via the following assumptions: |
| 531 | * - zero-advance glyphs do not contribute to the advance of their character (i.e. position is ignored), conversely |
| 532 | * if a glyph is to contribute to the advance of its character it must have a non-zero (float) advance |
| 533 | * - no carets can appear between a zero width character and its preceeding character, where 'preceeding' is |
| 534 | * defined logically. |
| 535 | * - no carets can appear within a split character |
| 536 | * - no carets can appear within a local reordering (i.e. Indic reordering, or non-adjacent split characters) |
| 537 | * - all characters lie on the same baseline, and it is either horizontal or vertical |
| 538 | * - the charinfo is in uniform ltr or rtl order (visual order), since local reorderings and split characters are removed |
| 539 | * |
| 540 | * The algorithm works in the following way: |
| 541 | * 1) we scan the glyphs ltr or rtl based on the bidi run direction |
| 542 | * 2) we can work in place, since we always consume a glyph for each char we write |
| 543 | * a) if the line is ltr, we start writing at position 0 until we finish, there may be leftver space |
| 544 | * b) if the line is rtl and 1-1, we start writing at position numChars/glyphs - 1 until we finish at 0 |
| 545 | * c) otherwise if we don't finish at 0, we have to copy the data down |
| 546 | * 3) we consume clusters in the following way: |
| 547 | * a) the first element is always consumed |
| 548 | * b) subsequent elements are consumed if: |
| 549 | * i) their advance is zero |
| 550 | * ii) their character index <= the character index of any character seen in this cluster |
| 551 | * iii) the minimum character index seen in this cluster isn't adjacent to the previous cluster |
| 552 | * c) character data is written as follows for horizontal lines (x/y and w/h are exchanged on vertical lines) |
| 553 | * i) the x position is the position of the leftmost glyph whose advance is not zero |
| 554 | * ii)the y position is the baseline |
| 555 | * iii) the x advance is the distance to the maximum x + adv of all glyphs whose advance is not zero |
| 556 | * iv) the y advance is the baseline |
| 557 | * v) vis x,y,w,h tightly encloses the vis x,y,w,h of all the glyphs with nonzero w and h |
| 558 | * 4) we can make some simple optimizations if we know some things: |
| 559 | * a) if the mapping is 1-1, unidirectional, and there are no zero-adv glyphs, we just return the glyphinfo |
| 560 | * b) if the mapping is 1-1, unidirectional, we just adjust the remaining glyphs to originate at right/left of the base |
| 561 | * c) if the mapping is 1-1, we compute the base position and advance as we go, then go back to adjust the remaining glyphs |
| 562 | * d) otherwise we keep separate track of the write position as we do (c) since no glyph in the cluster may be in the |
| 563 | * position we are writing. |
| 564 | * e) most clusters are simply the single base glyph in the same position as its character, so we try to avoid |
| 565 | * copying its data unnecessarily. |
| 566 | * 5) the glyph vector ought to provide access to these 'global' attributes to enable these optimizations. A single |
| 567 | * int with flags set is probably ok, we could also provide accessors for each attribute. This doesn't map to |
| 568 | * the GlyphMetrics flags very well, so I won't attempt to keep them similar. It might be useful to add those |
| 569 | * in addition to these. |
| 570 | * int FLAG_HAS_ZERO_ADVANCE_GLYPHS = 1; // set if there are zero-advance glyphs |
| 571 | * int FLAG_HAS_NONUNIFORM_ORDER = 2; // set if some glyphs are rearranged out of character visual order |
| 572 | * int FLAG_HAS_SPLIT_CHARACTERS = 4; // set if multiple glyphs per character |
| 573 | * int getDescriptionFlags(); // return an int containing the above flags |
| 574 | * boolean hasZeroAdvanceGlyphs(); |
| 575 | * boolean hasNonuniformOrder(); |
| 576 | * boolean hasSplitCharacters(); |
| 577 | * The optimized cases in (4) correspond to values 0, 1, 3, and 7 returned by getDescriptionFlags(). |
| 578 | */ |
| 579 | protected float[] createCharinfo() { |
| 580 | StandardGlyphVector gv = getGV(); |
| 581 | float[] glyphinfo = null; |
| 582 | try { |
| 583 | glyphinfo = gv.getGlyphInfo(); |
| 584 | } |
| 585 | catch (Exception e) { |
| 586 | System.out.println(source); |
| 587 | } |
| 588 | |
| 589 | /* |
| 590 | if ((gv.getDescriptionFlags() & 0x7) == 0) { |
| 591 | return glyphinfo; |
| 592 | } |
| 593 | */ |
| 594 | |
| 595 | int numGlyphs = gv.getNumGlyphs(); |
| 596 | int[] indices = gv.getGlyphCharIndices(0, numGlyphs, null); |
| 597 | |
| 598 | boolean DEBUG = false; |
| 599 | if (DEBUG) { |
| 600 | System.err.println("number of glyphs: " + numGlyphs); |
| 601 | for (int i = 0; i < numGlyphs; ++i) { |
| 602 | System.err.println("g: " + i + |
| 603 | ", x: " + glyphinfo[i*numvals+posx] + |
| 604 | ", a: " + glyphinfo[i*numvals+advx] + |
| 605 | ", n: " + indices[i]); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | int minIndex = indices[0]; // smallest index seen this cluster |
| 610 | int maxIndex = minIndex; // largest index seen this cluster |
| 611 | int nextMin = 0; // expected smallest index for this cluster |
| 612 | int cp = 0; // character position |
| 613 | int cx = 0; // character index (logical) |
| 614 | int gp = 0; // glyph position |
| 615 | int gx = 0; // glyph index (visual) |
| 616 | int gxlimit = numGlyphs; // limit of gx, when we reach this we're done |
| 617 | int pdelta = numvals; // delta for incrementing positions |
| 618 | int xdelta = 1; // delta for incrementing indices |
| 619 | |
| 620 | boolean ltr = (source.getLayoutFlags() & 0x1) == 0; |
| 621 | if (!ltr) { |
| 622 | minIndex = indices[numGlyphs - 1]; |
| 623 | maxIndex = minIndex; |
| 624 | nextMin = 0; // still logical |
| 625 | cp = glyphinfo.length - numvals; |
| 626 | cx = 0; // still logical |
| 627 | gp = glyphinfo.length - numvals; |
| 628 | gx = numGlyphs - 1; |
| 629 | gxlimit = -1; |
| 630 | pdelta = -numvals; |
| 631 | xdelta = -1; |
| 632 | } |
| 633 | |
| 634 | /* |
| 635 | // to support vertical, use 'ixxxx' indices and swap horiz and vertical components |
| 636 | if (source.isVertical()) { |
| 637 | iposx = posy; |
| 638 | iposy = posx; |
| 639 | iadvx = advy; |
| 640 | iadvy = advx; |
| 641 | ivisx = visy; |
| 642 | ivisy = visx; |
| 643 | ivish = visw; |
| 644 | ivisw = vish; |
| 645 | } else { |
| 646 | // use standard values |
| 647 | } |
| 648 | */ |
| 649 | |
| 650 | // use intermediates to reduce array access when we need to |
| 651 | float cposl = 0, cposr = 0, cvisl = 0, cvist = 0, cvisr = 0, cvisb = 0; |
| 652 | float baseline = 0; |
| 653 | |
| 654 | // record if we have to copy data even when no cluster |
| 655 | boolean mustCopy = false; |
| 656 | |
| 657 | while (gx != gxlimit) { |
| 658 | // start of new cluster |
| 659 | boolean haveCopy = false; |
| 660 | int clusterExtraGlyphs = 0; |
| 661 | |
| 662 | minIndex = indices[gx]; |
| 663 | maxIndex = minIndex; |
| 664 | |
| 665 | // advance to next glyph |
| 666 | gx += xdelta; |
| 667 | gp += pdelta; |
| 668 | |
| 669 | /* |
| 670 | while (gx != gxlimit && (glyphinfo[gp + advx] == 0 || |
| 671 | minIndex != nextMin || indices[gx] <= maxIndex)) { |
| 672 | */ |
| 673 | while (gx != gxlimit && |
| 674 | ((glyphinfo[gp + advx] == 0) || |
| 675 | (minIndex != nextMin) || |
| 676 | (indices[gx] <= maxIndex) || |
| 677 | (maxIndex - minIndex > clusterExtraGlyphs))) { |
| 678 | // initialize base data first time through, using base glyph |
| 679 | if (!haveCopy) { |
| 680 | int gps = gp - pdelta; |
| 681 | |
| 682 | cposl = glyphinfo[gps + posx]; |
| 683 | cposr = cposl + glyphinfo[gps + advx]; |
| 684 | cvisl = glyphinfo[gps + visx]; |
| 685 | cvist = glyphinfo[gps + visy]; |
| 686 | cvisr = cvisl + glyphinfo[gps + visw]; |
| 687 | cvisb = cvist + glyphinfo[gps + vish]; |
| 688 | |
| 689 | haveCopy = true; |
| 690 | } |
| 691 | |
| 692 | // have an extra glyph in this cluster |
| 693 | ++clusterExtraGlyphs; |
| 694 | |
| 695 | // adjust advance only if new glyph has non-zero advance |
| 696 | float radvx = glyphinfo[gp + advx]; |
| 697 | if (radvx != 0) { |
| 698 | float rposx = glyphinfo[gp + posx]; |
| 699 | cposl = Math.min(cposl, rposx); |
| 700 | cposr = Math.max(cposr, rposx + radvx); |
| 701 | } |
| 702 | |
| 703 | // adjust visible bounds only if new glyph has non-empty bounds |
| 704 | float rvisw = glyphinfo[gp + visw]; |
| 705 | if (rvisw != 0) { |
| 706 | float rvisx = glyphinfo[gp + visx]; |
| 707 | float rvisy = glyphinfo[gp + visy]; |
| 708 | cvisl = Math.min(cvisl, rvisx); |
| 709 | cvist = Math.min(cvist, rvisy); |
| 710 | cvisr = Math.max(cvisr, rvisx + rvisw); |
| 711 | cvisb = Math.max(cvisb, rvisy + glyphinfo[gp + vish]); |
| 712 | } |
| 713 | |
| 714 | // adjust min, max index |
| 715 | minIndex = Math.min(minIndex, indices[gx]); |
| 716 | maxIndex = Math.max(maxIndex, indices[gx]); |
| 717 | |
| 718 | // get ready to examine next glyph |
| 719 | gx += xdelta; |
| 720 | gp += pdelta; |
| 721 | } |
| 722 | // done with cluster, gx and gp are set for next glyph |
| 723 | |
| 724 | if (DEBUG) { |
| 725 | System.out.println("minIndex = " + minIndex + ", maxIndex = " + maxIndex); |
| 726 | } |
| 727 | |
| 728 | nextMin = maxIndex + 1; |
| 729 | |
| 730 | // do common character adjustments |
| 731 | glyphinfo[cp + posy] = baseline; |
| 732 | glyphinfo[cp + advy] = 0; |
| 733 | |
| 734 | if (haveCopy) { |
| 735 | // save adjustments to the base character |
| 736 | glyphinfo[cp + posx] = cposl; |
| 737 | glyphinfo[cp + advx] = cposr - cposl; |
| 738 | glyphinfo[cp + visx] = cvisl; |
| 739 | glyphinfo[cp + visy] = cvist; |
| 740 | glyphinfo[cp + visw] = cvisr - cvisl; |
| 741 | glyphinfo[cp + vish] = cvisb - cvist; |
| 742 | |
| 743 | // compare number of chars read with number of glyphs read. |
| 744 | // if more glyphs than chars, set mustCopy to true, as we'll always have |
| 745 | // to copy the data from here on out. |
| 746 | if (maxIndex - minIndex < clusterExtraGlyphs) { |
| 747 | mustCopy = true; |
| 748 | } |
| 749 | |
| 750 | // Fix the characters that follow the base character. |
| 751 | // New values are all the same. Note we fix the number of characters |
| 752 | // we saw, not the number of glyphs we saw. |
| 753 | if (minIndex < maxIndex) { |
| 754 | if (!ltr) { |
| 755 | // if rtl, characters to left of base, else to right. reuse cposr. |
| 756 | cposr = cposl; |
| 757 | } |
| 758 | cvisr -= cvisl; // reuse, convert to deltas. |
| 759 | cvisb -= cvist; |
| 760 | |
| 761 | int iMinIndex = minIndex, icp = cp / 8; |
| 762 | |
| 763 | while (minIndex < maxIndex) { |
| 764 | ++minIndex; |
| 765 | cx += xdelta; |
| 766 | cp += pdelta; |
| 767 | |
| 768 | if (cp < 0 || cp >= glyphinfo.length) { |
| 769 | if (DEBUG) System.out.println("minIndex = " + iMinIndex + ", maxIndex = " + maxIndex + ", cp = " + icp); |
| 770 | } |
| 771 | |
| 772 | glyphinfo[cp + posx] = cposr; |
| 773 | glyphinfo[cp + posy] = baseline; |
| 774 | glyphinfo[cp + advx] = 0; |
| 775 | glyphinfo[cp + advy] = 0; |
| 776 | glyphinfo[cp + visx] = cvisl; |
| 777 | glyphinfo[cp + visy] = cvist; |
| 778 | glyphinfo[cp + visw] = cvisr; |
| 779 | glyphinfo[cp + vish] = cvisb; |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | // no longer using this copy |
| 784 | haveCopy = false; |
| 785 | } else if (mustCopy) { |
| 786 | // out of synch, so we have to copy all the time now |
| 787 | int gpr = gp - pdelta; |
| 788 | |
| 789 | glyphinfo[cp + posx] = glyphinfo[gpr + posx]; |
| 790 | glyphinfo[cp + advx] = glyphinfo[gpr + advx]; |
| 791 | glyphinfo[cp + visx] = glyphinfo[gpr + visx]; |
| 792 | glyphinfo[cp + visy] = glyphinfo[gpr + visy]; |
| 793 | glyphinfo[cp + visw] = glyphinfo[gpr + visw]; |
| 794 | glyphinfo[cp + vish] = glyphinfo[gpr + vish]; |
| 795 | } |
| 796 | // else glyphinfo is already at the correct character position, and is unchanged, so just leave it |
| 797 | |
| 798 | // reset for new cluster |
| 799 | cp += pdelta; |
| 800 | cx += xdelta; |
| 801 | } |
| 802 | |
| 803 | if (mustCopy && !ltr) { |
| 804 | // data written to wrong end of array, need to shift down |
| 805 | |
| 806 | cp -= pdelta; // undo last increment, get start of valid character data in array |
| 807 | System.arraycopy(glyphinfo, cp, glyphinfo, 0, glyphinfo.length - cp); |
| 808 | } |
| 809 | |
| 810 | if (DEBUG) { |
| 811 | char[] chars = source.getChars(); |
| 812 | int start = source.getStart(); |
| 813 | int length = source.getLength(); |
| 814 | System.out.println("char info for " + length + " characters"); |
| 815 | for(int i = 0; i < length * numvals;) { |
| 816 | System.out.println(" ch: " + Integer.toHexString(chars[start + v2l(i / numvals)]) + |
| 817 | " x: " + glyphinfo[i++] + |
| 818 | " y: " + glyphinfo[i++] + |
| 819 | " xa: " + glyphinfo[i++] + |
| 820 | " ya: " + glyphinfo[i++] + |
| 821 | " l: " + glyphinfo[i++] + |
| 822 | " t: " + glyphinfo[i++] + |
| 823 | " w: " + glyphinfo[i++] + |
| 824 | " h: " + glyphinfo[i++]); |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | return glyphinfo; |
| 829 | } |
| 830 | |
| 831 | /** |
| 832 | * Map logical character index to visual character index. |
| 833 | * <p> |
| 834 | * This ignores hindi reordering. @see createCharinfo |
| 835 | */ |
| 836 | protected int l2v(int index) { |
| 837 | return (source.getLayoutFlags() & 0x1) == 0 ? index : source.getLength() - 1 - index; |
| 838 | } |
| 839 | |
| 840 | /** |
| 841 | * Map visual character index to logical character index. |
| 842 | * <p> |
| 843 | * This ignores hindi reordering. @see createCharinfo |
| 844 | */ |
| 845 | protected int v2l(int index) { |
| 846 | return (source.getLayoutFlags() & 0x1) == 0 ? index : source.getLength() - 1 - index; |
| 847 | } |
| 848 | |
| 849 | public TextLineComponent getSubset(int start, int limit, int dir) { |
| 850 | return new ExtendedTextSourceLabel(source.getSubSource(start, limit-start, dir), decorator); |
| 851 | } |
| 852 | |
| 853 | public String toString() { |
| 854 | if (true) { |
| 855 | return source.toString(source.WITHOUT_CONTEXT); |
| 856 | } |
| 857 | StringBuffer buf = new StringBuffer(); |
| 858 | buf.append(super.toString()); |
| 859 | buf.append("[source:"); |
| 860 | buf.append(source.toString(source.WITHOUT_CONTEXT)); |
| 861 | buf.append(", lb:"); |
| 862 | buf.append(lb); |
| 863 | buf.append(", ab:"); |
| 864 | buf.append(ab); |
| 865 | buf.append(", vb:"); |
| 866 | buf.append(vb); |
| 867 | buf.append(", gv:"); |
| 868 | buf.append(gv); |
| 869 | buf.append(", ci: "); |
| 870 | if (charinfo == null) { |
| 871 | buf.append("null"); |
| 872 | } else { |
| 873 | buf.append(charinfo[0]); |
| 874 | for (int i = 1; i < charinfo.length;) { |
| 875 | buf.append(i % numvals == 0 ? "; " : ", "); |
| 876 | buf.append(charinfo[i]); |
| 877 | } |
| 878 | } |
| 879 | buf.append("]"); |
| 880 | |
| 881 | return buf.toString(); |
| 882 | } |
| 883 | |
| 884 | //public static ExtendedTextLabel create(TextSource source) { |
| 885 | // return new ExtendedTextSourceLabel(source); |
| 886 | //} |
| 887 | |
| 888 | public int getNumJustificationInfos() { |
| 889 | return getGV().getNumGlyphs(); |
| 890 | } |
| 891 | |
| 892 | |
| 893 | public void getJustificationInfos(GlyphJustificationInfo[] infos, int infoStart, int charStart, int charLimit) { |
| 894 | // This simple implementation only uses spaces for justification. |
| 895 | // Since regular characters aren't justified, we don't need to deal with |
| 896 | // special infos for combining marks or ligature substitution glyphs. |
| 897 | // added character justification for kanjii only 2/22/98 |
| 898 | |
| 899 | StandardGlyphVector gv = getGV(); |
| 900 | |
| 901 | float[] charinfo = getCharinfo(); |
| 902 | |
| 903 | float size = gv.getFont().getSize2D(); |
| 904 | |
| 905 | GlyphJustificationInfo nullInfo = |
| 906 | new GlyphJustificationInfo(0, |
| 907 | false, GlyphJustificationInfo.PRIORITY_NONE, 0, 0, |
| 908 | false, GlyphJustificationInfo.PRIORITY_NONE, 0, 0); |
| 909 | |
| 910 | GlyphJustificationInfo spaceInfo = |
| 911 | new GlyphJustificationInfo(size, |
| 912 | true, GlyphJustificationInfo.PRIORITY_WHITESPACE, 0, size, |
| 913 | true, GlyphJustificationInfo.PRIORITY_WHITESPACE, 0, size / 4f); |
| 914 | |
| 915 | GlyphJustificationInfo kanjiInfo = |
| 916 | new GlyphJustificationInfo(size, |
| 917 | true, GlyphJustificationInfo.PRIORITY_INTERCHAR, size, size, |
| 918 | false, GlyphJustificationInfo.PRIORITY_NONE, 0, 0); |
| 919 | |
| 920 | char[] chars = source.getChars(); |
| 921 | int offset = source.getStart(); |
| 922 | |
| 923 | // assume data is 1-1 and either all rtl or all ltr, for now |
| 924 | |
| 925 | int numGlyphs = gv.getNumGlyphs(); |
| 926 | int minGlyph = 0; |
| 927 | int maxGlyph = numGlyphs; |
| 928 | boolean ltr = (source.getLayoutFlags() & 0x1) == 0; |
| 929 | if (charStart != 0 || charLimit != source.getLength()) { |
| 930 | if (ltr) { |
| 931 | minGlyph = charStart; |
| 932 | maxGlyph = charLimit; |
| 933 | } else { |
| 934 | minGlyph = numGlyphs - charLimit; |
| 935 | maxGlyph = numGlyphs - charStart; |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | for (int i = 0; i < numGlyphs; ++i) { |
| 940 | GlyphJustificationInfo info = null; |
| 941 | if (i >= minGlyph && i < maxGlyph) { |
| 942 | if (charinfo[i * numvals + advx] == 0) { // combining marks don't justify |
| 943 | info = nullInfo; |
| 944 | } else { |
| 945 | int ci = v2l(i); // 1-1 assumption again |
| 946 | char c = chars[offset + ci]; |
| 947 | if (Character.isWhitespace(c)) { |
| 948 | info = spaceInfo; |
| 949 | // CJK, Hangul, CJK Compatibility areas |
| 950 | } else if (c >= 0x4e00 && |
| 951 | (c < 0xa000) || |
| 952 | (c >= 0xac00 && c < 0xd7b0) || |
| 953 | (c >= 0xf900 && c < 0xfb00)) { |
| 954 | info = kanjiInfo; |
| 955 | } else { |
| 956 | info = nullInfo; |
| 957 | } |
| 958 | } |
| 959 | } |
| 960 | infos[infoStart + i] = info; |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | public TextLineComponent applyJustificationDeltas(float[] deltas, int deltaStart, boolean[] flags) { |
| 965 | |
| 966 | // when we justify, we need to adjust the charinfo since spaces |
| 967 | // change their advances. preserve the existing charinfo. |
| 968 | |
| 969 | float[] newCharinfo = (float[])getCharinfo().clone(); |
| 970 | |
| 971 | // we only push spaces, so never need to rejustify |
| 972 | flags[0] = false; |
| 973 | |
| 974 | // preserve the existing gv. |
| 975 | |
| 976 | StandardGlyphVector newgv = (StandardGlyphVector)getGV().clone(); |
| 977 | float[] newPositions = newgv.getGlyphPositions(null); |
| 978 | int numGlyphs = newgv.getNumGlyphs(); |
| 979 | |
| 980 | /* |
| 981 | System.out.println("oldgv: " + getGV() + ", newgv: " + newgv); |
| 982 | System.out.println("newpositions: " + newPositions); |
| 983 | for (int i = 0; i < newPositions.length; i += 2) { |
| 984 | System.out.println("[" + (i/2) + "] " + newPositions[i] + ", " + newPositions[i+1]); |
| 985 | } |
| 986 | |
| 987 | System.out.println("deltas: " + deltas + " start: " + deltaStart); |
| 988 | for (int i = deltaStart; i < deltaStart + numGlyphs; i += 2) { |
| 989 | System.out.println("[" + (i/2) + "] " + deltas[i] + ", " + deltas[i+1]); |
| 990 | } |
| 991 | */ |
| 992 | |
| 993 | char[] chars = source.getChars(); |
| 994 | int offset = source.getStart(); |
| 995 | |
| 996 | // accumulate the deltas to adjust positions and advances. |
| 997 | // handle whitespace by modifying advance, |
| 998 | // handle everything else by modifying position before and after |
| 999 | |
| 1000 | float deltaPos = 0; |
| 1001 | for (int i = 0; i < numGlyphs; ++i) { |
| 1002 | if (Character.isWhitespace(chars[offset + v2l(i)])) { |
| 1003 | newPositions[i*2] += deltaPos; |
| 1004 | |
| 1005 | float deltaAdv = deltas[deltaStart + i*2] + deltas[deltaStart + i*2 + 1]; |
| 1006 | |
| 1007 | newCharinfo[i * numvals + posx] += deltaPos; |
| 1008 | newCharinfo[i * numvals + visx] += deltaPos; |
| 1009 | newCharinfo[i * numvals + advx] += deltaAdv; |
| 1010 | |
| 1011 | deltaPos += deltaAdv; |
| 1012 | } else { |
| 1013 | deltaPos += deltas[deltaStart + i*2]; |
| 1014 | |
| 1015 | newPositions[i*2] += deltaPos; |
| 1016 | newCharinfo[i * numvals + posx] += deltaPos; |
| 1017 | newCharinfo[i * numvals + visx] += deltaPos; |
| 1018 | |
| 1019 | deltaPos += deltas[deltaStart + i*2 + 1]; |
| 1020 | } |
| 1021 | } |
| 1022 | newPositions[numGlyphs * 2] += deltaPos; |
| 1023 | |
| 1024 | newgv.setGlyphPositions(newPositions); |
| 1025 | |
| 1026 | /* |
| 1027 | newPositions = newgv.getGlyphPositions(null); |
| 1028 | System.out.println(">> newpositions: " + newPositions); |
| 1029 | for (int i = 0; i < newPositions.length; i += 2) { |
| 1030 | System.out.println("[" + (i/2) + "] " + newPositions[i] + ", " + newPositions[i+1]); |
| 1031 | } |
| 1032 | */ |
| 1033 | |
| 1034 | ExtendedTextSourceLabel result = new ExtendedTextSourceLabel(source, decorator); |
| 1035 | result.gv = newgv; |
| 1036 | result.charinfo = newCharinfo; |
| 1037 | |
| 1038 | return result; |
| 1039 | } |
| 1040 | } |