388 } else { |
388 } else { |
389 nextX += metrics.charWidth(txt[i]); |
389 nextX += metrics.charWidth(txt[i]); |
390 } |
390 } |
391 if ((x >= currX) && (x < nextX)) { |
391 if ((x >= currX) && (x < nextX)) { |
392 // found the hit position... return the appropriate side |
392 // found the hit position... return the appropriate side |
393 if ((round == false) || ((x - currX) < (nextX - x))) { |
393 int offset = ((round == false) || ((x - currX) < (nextX - x))) ? |
394 return i - txtOffset; |
394 (i - txtOffset) : (i + 1 - txtOffset); |
395 } else { |
395 // the length of the string measured as a whole may differ from |
396 return i + 1 - txtOffset; |
396 // the sum of individual character lengths, for example if |
|
397 // fractional metrics are enabled; and we must guard from this. |
|
398 while (metrics.charsWidth(txt, txtOffset, offset + 1) > (x - x0)) { |
|
399 offset--; |
397 } |
400 } |
|
401 return (offset < 0 ? 0 : offset); |
398 } |
402 } |
399 currX = nextX; |
403 currX = nextX; |
400 } |
404 } |
401 |
405 |
402 // didn't find, return end offset |
406 // didn't find, return end offset |
403 return txtCount; |
407 return txtCount; |
404 } |
|
405 |
|
406 /** |
|
407 * Adjust text offset so that the length of a resulting string as a whole |
|
408 * fits into the specified width. |
|
409 */ |
|
410 static int adjustOffsetForFractionalMetrics( |
|
411 Segment s, FontMetrics fm, int offset, int width) { |
|
412 // Sometimes the offset returned by getTabbedTextOffset is beyond the |
|
413 // available area, when fractional metrics are enabled. We should |
|
414 // guard against this. |
|
415 if (offset < s.count) { |
|
416 while (offset > 0 && |
|
417 fm.charsWidth(s.array, s.offset, offset + 1) > width) { |
|
418 offset--; |
|
419 } |
|
420 } |
|
421 return offset; |
|
422 } |
408 } |
423 |
409 |
424 /** |
410 /** |
425 * Determine where to break the given text to fit |
411 * Determine where to break the given text to fit |
426 * within the given span. This tries to find a word boundary. |
412 * within the given span. This tries to find a word boundary. |
441 char[] txt = s.array; |
427 char[] txt = s.array; |
442 int txtOffset = s.offset; |
428 int txtOffset = s.offset; |
443 int txtCount = s.count; |
429 int txtCount = s.count; |
444 int index = Utilities.getTabbedTextOffset(s, metrics, x0, x, |
430 int index = Utilities.getTabbedTextOffset(s, metrics, x0, x, |
445 e, startOffset, false); |
431 e, startOffset, false); |
446 index = adjustOffsetForFractionalMetrics(s, metrics, index, x - x0); |
|
447 |
432 |
448 if (index >= txtCount - 1) { |
433 if (index >= txtCount - 1) { |
449 return txtCount; |
434 return txtCount; |
450 } |
435 } |
451 |
436 |