6760148: Certain fonts are not correctly soft wrapped when using JTextComponent.print()

Reviewed-by: peterz
This commit is contained in:
Pavel Porvatov 2011-03-05 18:27:51 +03:00
parent 1c0c4dc366
commit 1af9167ee7
2 changed files with 11 additions and 28 deletions

View File

@ -390,11 +390,15 @@ public class Utilities {
}
if ((x >= currX) && (x < nextX)) {
// found the hit position... return the appropriate side
if ((round == false) || ((x - currX) < (nextX - x))) {
return i - txtOffset;
} else {
return i + 1 - txtOffset;
int offset = ((round == false) || ((x - currX) < (nextX - x))) ?
(i - txtOffset) : (i + 1 - txtOffset);
// the length of the string measured as a whole may differ from
// the sum of individual character lengths, for example if
// fractional metrics are enabled; and we must guard from this.
while (metrics.charsWidth(txt, txtOffset, offset + 1) > (x - x0)) {
offset--;
}
return (offset < 0 ? 0 : offset);
}
currX = nextX;
}
@ -403,24 +407,6 @@ public class Utilities {
return txtCount;
}
/**
* Adjust text offset so that the length of a resulting string as a whole
* fits into the specified width.
*/
static int adjustOffsetForFractionalMetrics(
Segment s, FontMetrics fm, int offset, int width) {
// Sometimes the offset returned by getTabbedTextOffset is beyond the
// available area, when fractional metrics are enabled. We should
// guard against this.
if (offset < s.count) {
while (offset > 0 &&
fm.charsWidth(s.array, s.offset, offset + 1) > width) {
offset--;
}
}
return offset;
}
/**
* Determine where to break the given text to fit
* within the given span. This tries to find a word boundary.
@ -443,7 +429,6 @@ public class Utilities {
int txtCount = s.count;
int index = Utilities.getTabbedTextOffset(s, metrics, x0, x,
e, startOffset, false);
index = adjustOffsetForFractionalMetrics(s, metrics, index, x - x0);
if (index >= txtCount - 1) {
return txtCount;

View File

@ -239,11 +239,9 @@ public class WrappedPlainView extends BoxView implements TabExpander {
tabBase, tabBase + currentWidth,
this, p0);
} else {
int offset = Utilities.getTabbedTextOffset(segment, metrics,
tabBase, tabBase + currentWidth, this, p0, false);
offset = Utilities.adjustOffsetForFractionalMetrics(
segment, metrics, offset, currentWidth);
p = p0 + offset;
p = p0 + Utilities.getTabbedTextOffset(segment, metrics,
tabBase, tabBase + currentWidth,
this, p0, false);
}
SegmentCache.releaseSharedSegment(segment);
return p;