下面列出了怎么用java.awt.font.LineBreakMeasurer的API类实例代码及写法,或者点击链接到github查看源代码。
private LineBreakMeasurer[] getLineBreakMeasurers(Graphics2D g) {
if (lbmTexto == null && (Texto != null && !Texto.equals(""))) {
lbmTexto = new LineBreakMeasurer[Textos.length];
for (int i = 0; i < lbmTexto.length; i++) {
String tmp = Textos[i].isEmpty()? " " : Textos[i];
AttributedString attribString = new AttributedString(tmp);
attribString.addAttribute(TextAttribute.FONT, getFont());
//attribString.addAttribute(TextAttribute.FONT, getFont());
AttributedCharacterIterator attribCharIterator = attribString.getIterator();
//FontRenderContext frc = new FontRenderContext(null, true, false);
FontRenderContext frc = g.getFontRenderContext();
lbmTexto[i] = new LineBreakMeasurer(attribCharIterator, frc);
}
}
return lbmTexto;
}
protected int measureExactLineBreakIndex(float width, int endLimit, boolean requireWord)
{
//FIXME would it be faster to create and cache a LineBreakMeasurer for the whole paragraph?
Map<Attribute, Object> attributes = new HashMap<Attribute, Object>();
// we only need the font as it includes the size and style
attributes.put(TextAttribute.FONT, fontInfo.fontInfo.font);
String textLine = paragraphText.substring(paragraphPosition, endLimit);
AttributedString attributedLine = new AttributedString(textLine, attributes);
// we need a fresh iterator for the line
BreakIterator breakIterator = paragraphTruncateAtChar ? BreakIterator.getCharacterInstance()
: BreakIterator.getLineInstance();
LineBreakMeasurer breakMeasurer = new LineBreakMeasurer(attributedLine.getIterator(),
breakIterator, context.getFontRenderContext());
int breakIndex = breakMeasurer.nextOffset(width, endLimit - paragraphPosition, requireWord)
+ paragraphPosition;
if (logTrace)
{
log.trace("exact line break index measured at " + (paragraphOffset + breakIndex));
}
return breakIndex;
}
private final static void pixelDataFromString(int width, int height, String str, java.awt.Font font, int[] pixels, LineBreakMeasurer measurer) {
measurer.setPosition(0);
g2d.clearRect(0, 0, width, height);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
float wrapping_width = width;
float y = 0;
int length = str.length();
while (measurer.getPosition() < length) {
TextLayout layout = measurer.nextLayout(wrapping_width);
y += (layout.getAscent());
float x = layout.isLeftToRight() ? 0 : (wrapping_width - layout.getAdvance());
layout.draw(g2d, x, y);
y += layout.getDescent() + layout.getLeading();
}
image.getRaster().getDataElements(0, 0, image.getWidth(), image.getHeight(), pixels);
}
@Override protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setPaint(getForeground());
Insets i = getInsets();
float x = i.left;
float y = i.top;
int w = getWidth() - i.left - i.right;
AttributedString as = new AttributedString(getText());
as.addAttribute(TextAttribute.FONT, getFont());
AttributedCharacterIterator aci = as.getIterator();
FontRenderContext frc = g2.getFontRenderContext();
LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc);
while (lbm.getPosition() < aci.getEndIndex()) {
TextLayout tl = lbm.nextLayout(w);
tl.draw(g2, x, y + tl.getAscent());
y += tl.getDescent() + tl.getLeading() + tl.getAscent();
}
g2.dispose();
}
@Override protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(getForeground());
g2.setFont(getFont());
SwingUtilities.calculateInnerArea(this, RECT);
float x = RECT.x;
float y = RECT.y;
int w = RECT.width;
AttributedString as = new AttributedString(getText());
as.addAttribute(TextAttribute.FONT, getFont()); // TEST: .deriveFont(at));
// TEST: as.addAttribute(TextAttribute.TRANSFORM, at);
AttributedCharacterIterator aci = as.getIterator();
FontRenderContext frc = g2.getFontRenderContext();
LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc);
while (lbm.getPosition() < aci.getEndIndex()) {
TextLayout tl = lbm.nextLayout(w);
tl.draw(g2, x, y + tl.getAscent());
y += tl.getDescent() + tl.getLeading() + tl.getAscent();
}
g2.dispose();
}
private TextLayout performWordBreak( final int start, final TextLayout textLayout,
final LineBreakMeasurer lineBreakMeasurer, final int end ) {
final TextLayout layout;
if ( wordInstance.isBoundary( end ) != false ) {
return textLayout;
}
int preceding = wordInstance.preceding( end );
if ( preceding == start ) {
// single word does not fit on the line, so print full word
lineBreakMeasurer.setPosition( start );
return lineBreakMeasurer.nextLayout( INFINITY, wordInstance.following( end ), false );
} else {
lineBreakMeasurer.setPosition( start );
return lineBreakMeasurer.nextLayout( INFINITY, preceding, false );
}
}
private Dimension paintOrGetSize(Graphics2D g, int width) {
Insets insets = getInsets();
width -= insets.left + insets.right + margin.left + margin.right;
float w = insets.left + insets.right + margin.left + margin.right;
float x = insets.left + margin.left, y = insets.top + margin.top;
if (width > 0 && text != null && text.length() > 0) {
AttributedString as = new AttributedString(getText());
as.addAttribute(TextAttribute.FONT, getFont());
AttributedCharacterIterator aci = as.getIterator();
LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc);
float max = 0;
while (lbm.getPosition() < aci.getEndIndex()) {
TextLayout textLayout = lbm.nextLayout(width);
if (g != null && isJustified() && textLayout.getVisibleAdvance() > 0.80 * width)
textLayout = textLayout.getJustifiedLayout(width);
if (g != null)
textLayout.draw(g, x, y + textLayout.getAscent());
y += textLayout.getDescent() + textLayout.getLeading() + textLayout.getAscent();
max = Math.max(max, textLayout.getVisibleAdvance());
}
w += max;
}
return new Dimension((int)Math.ceil(w), (int)Math.ceil(y) + insets.bottom + margin.bottom);
}
private static void drawString(Graphics2D g, Font font, String value, float x, float y) {
AttributedString str = new AttributedString(value);
str.addAttribute(TextAttribute.FOREGROUND, Color.BLACK);
str.addAttribute(TextAttribute.FONT, font);
FontRenderContext frc = new FontRenderContext(null, true, true);
TextLayout layout = new LineBreakMeasurer(str.getIterator(), frc).nextLayout(Integer.MAX_VALUE);
layout.draw(g, x, y);
}
private static void drawString(Graphics2D g, Font font, String value, float x, float y) {
AttributedString str = new AttributedString(value);
str.addAttribute(TextAttribute.FOREGROUND, Color.BLACK);
str.addAttribute(TextAttribute.FONT, font);
FontRenderContext frc = new FontRenderContext(null, true, true);
TextLayout layout = new LineBreakMeasurer(str.getIterator(), frc).nextLayout(Integer.MAX_VALUE);
layout.draw(g, x, y);
}
public void drawLabel(String text, GraphicInfo graphicInfo, boolean centered){
float interline = 1.0f;
// text
if (text != null && text.length()>0) {
Paint originalPaint = g.getPaint();
Font originalFont = g.getFont();
g.setPaint(LABEL_COLOR);
g.setFont(LABEL_FONT);
int wrapWidth = 100;
int textY = (int) graphicInfo.getY();
// TODO: use drawMultilineText()
AttributedString as = new AttributedString(text);
as.addAttribute(TextAttribute.FOREGROUND, g.getPaint());
as.addAttribute(TextAttribute.FONT, g.getFont());
AttributedCharacterIterator aci = as.getIterator();
FontRenderContext frc = new FontRenderContext(null, true, false);
LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc);
while (lbm.getPosition() < text.length()) {
TextLayout tl = lbm.nextLayout(wrapWidth);
textY += tl.getAscent();
Rectangle2D bb = tl.getBounds();
double tX = graphicInfo.getX();
if (centered) {
tX += (int) (graphicInfo.getWidth() / 2 - bb.getWidth() / 2);
}
tl.draw(g, (float) tX, textY);
textY += tl.getDescent() + tl.getLeading() + (interline - 1.0f) * tl.getAscent();
}
// restore originals
g.setFont(originalFont);
g.setPaint(originalPaint);
}
}
private static void drawString(Graphics2D g, Font font, String value, float x, float y) {
AttributedString str = new AttributedString(value);
str.addAttribute(TextAttribute.FOREGROUND, Color.BLACK);
str.addAttribute(TextAttribute.FONT, font);
FontRenderContext frc = new FontRenderContext(null, true, true);
TextLayout layout = new LineBreakMeasurer(str.getIterator(), frc).nextLayout(Integer.MAX_VALUE);
layout.draw(g, x, y);
}
@Override
public void paint(Graphics g, JComponent c) {
if (c.isOpaque()) {
ImageLibrary.drawTiledImage("image.background.FreeColToolTip",
g, c, null);
}
g.setColor(Color.BLACK); // FIXME: find out why this is necessary
Graphics2D graphics = (Graphics2D)g;
float x = margin;
float y = margin;
for (String line : lineBreak.split(((JToolTip) c).getTipText())) {
if (line.isEmpty()) {
y += LEADING;
continue;
}
AttributedCharacterIterator styledText =
new AttributedString(line).getIterator();
LineBreakMeasurer measurer = new LineBreakMeasurer(styledText, frc);
while (measurer.getPosition() < styledText.getEndIndex()) {
TextLayout layout = measurer.nextLayout(maximumWidth);
y += (layout.getAscent());
float dx = layout.isLeftToRight() ?
0 : (maximumWidth - layout.getAdvance());
layout.draw(graphics, x + dx, y);
y += layout.getDescent() + layout.getLeading();
}
}
}
@Override
public Dimension getPreferredSize(JComponent c) {
String tipText = ((JToolTip)c).getTipText();
if (tipText == null || tipText.isEmpty()) {
return new Dimension(0, 0);
}
float x = 0f;
float y = 0f;
for (String line : lineBreak.split(tipText)) {
if (line.isEmpty()) {
y += LEADING;
continue;
}
AttributedCharacterIterator styledText
= new AttributedString(line).getIterator();
LineBreakMeasurer measurer = new LineBreakMeasurer(styledText, frc);
while (measurer.getPosition() < styledText.getEndIndex()) {
TextLayout layout = measurer.nextLayout(maximumWidth);
x = Math.max(x, layout.getVisibleAdvance());
y += layout.getAscent() + layout.getDescent() + layout.getLeading();
}
}
return new Dimension((int) (x + 2 * margin),
(int) (y + 2 * margin));
}
public void test() {
// construct a paragraph as follows: MIXED + [SPACING + WORD] + ...
StringBuffer text = new StringBuffer(MIXED);
for (int i=0; i < NUM_WORDS; i++) {
text.append(SPACING);
text.append(WORD);
}
AttributedString attrString = new AttributedString(text.toString());
attrString.addAttribute(TextAttribute.SIZE, new Float(24.0));
LineBreakMeasurer measurer = new LineBreakMeasurer(attrString.getIterator(),
DEFAULT_FRC);
// get width of a space-word sequence, in context
int sequenceLength = WORD.length()+SPACING.length();
measurer.setPosition(text.length() - sequenceLength);
TextLayout layout = measurer.nextLayout(10000.0f);
if (layout.getCharacterCount() != sequenceLength) {
throw new Error("layout length is incorrect!");
}
final float sequenceAdvance = layout.getVisibleAdvance();
float wrappingWidth = sequenceAdvance * 2;
// now run test with a variety of widths
while (wrappingWidth < (sequenceAdvance*NUM_WORDS)) {
measurer.setPosition(0);
checkMeasurer(measurer,
wrappingWidth,
sequenceAdvance,
text.length());
wrappingWidth += sequenceAdvance / 5;
}
}
/**
* Iterate through measurer and check that every line is
* not too long and not too short, but just right.
*/
private void checkMeasurer(LineBreakMeasurer measurer,
float wrappingWidth,
float sequenceAdvance,
int endPosition) {
do {
TextLayout layout = measurer.nextLayout(wrappingWidth);
float visAdvance = layout.getVisibleAdvance();
// Check that wrappingWidth-sequenceAdvance < visAdvance
// Also, if we're not at the end of the paragraph,
// check that visAdvance <= wrappingWidth
if (visAdvance > wrappingWidth) {
// line is too long for given wrapping width
throw new Error("layout is too long");
}
if (measurer.getPosition() < endPosition) {
if (visAdvance <= wrappingWidth - sequenceAdvance) {
// line is too short for given wrapping width;
// another word would have fit
throw new Error("room for more words on line. diff=" +
(wrappingWidth - sequenceAdvance - visAdvance));
}
}
} while (measurer.getPosition() != endPosition);
}
private static void drawString(Graphics2D g, Font font, String value, float x, float y) {
AttributedString str = new AttributedString(value);
str.addAttribute(TextAttribute.FOREGROUND, Color.BLACK);
str.addAttribute(TextAttribute.FONT, font);
FontRenderContext frc = new FontRenderContext(null, true, true);
TextLayout layout = new LineBreakMeasurer(str.getIterator(), frc).nextLayout(Integer.MAX_VALUE);
layout.draw(g, x, y);
}
protected void startParagraph(AttributedCharacterIterator paragraph, boolean truncateAtChar)
{
this.paragraph = paragraph;
BreakIterator breakIt = truncateAtChar ? BreakIterator.getCharacterInstance()
: BreakIterator.getLineInstance();
lineMeasurer = new LineBreakMeasurer(paragraph, breakIt, context.getFontRenderContext());
}
@Override
public TextLine baseTextLine(int index)
{
AttributedString tmpText = new AttributedString(paragraph, index, index + 1);
LineBreakMeasurer lbm = new LineBreakMeasurer(tmpText.getIterator(), context.getFontRenderContext());
TextLayout tlyt = lbm.nextLayout(100);//FIXME what is this? why 100?
return new TextLayoutLine(tlyt);
}
public TextBoxPaintable(String string, Font font, float maxWidth,
float insets, Paint paint) {
if (paint == null)
paint = SystemColor.textText;
this.insets = insets;
this.text = string;
this.paint = paint;
List<String> lines = new ArrayList<String>();
Map<TextAttribute, Object> attributes = new HashMap<TextAttribute, Object>();
attributes.put(TextAttribute.FONT, font);
AttributedString attrString = new AttributedString(string, attributes);
LineBreakMeasurer lbm = new LineBreakMeasurer(attrString.getIterator(),
frc);
TextLayout tl = lbm.nextLayout(maxWidth);
float dy = 0;
GeneralPath path = new GeneralPath();
int startIndex = 0;
while (tl != null) {
path.append(
tl.getOutline(AffineTransform.getTranslateInstance(0, dy)),
false);
dy += tl.getAscent() + tl.getDescent() + tl.getLeading();
int charCount = tl.getCharacterCount();
lines.add(text.substring(startIndex, startIndex + charCount));
startIndex += charCount;
tl = lbm.nextLayout(maxWidth);
}
this.lines = lines.toArray(new String[lines.size()]);
untransformedShape = path;
Rectangle2D b = ShapeBounds.getBounds(untransformedShape);
b.setFrame(b.getX(), b.getY(), b.getWidth() + 2 * insets, b.getHeight()
+ 2 * insets);
untransformedBounds = b.getBounds();
}
public void test() {
// construct a paragraph as follows: MIXED + [SPACING + WORD] + ...
StringBuffer text = new StringBuffer(MIXED);
for (int i=0; i < NUM_WORDS; i++) {
text.append(SPACING);
text.append(WORD);
}
AttributedString attrString = new AttributedString(text.toString());
attrString.addAttribute(TextAttribute.SIZE, new Float(24.0));
LineBreakMeasurer measurer = new LineBreakMeasurer(attrString.getIterator(),
DEFAULT_FRC);
// get width of a space-word sequence, in context
int sequenceLength = WORD.length()+SPACING.length();
measurer.setPosition(text.length() - sequenceLength);
TextLayout layout = measurer.nextLayout(10000.0f);
if (layout.getCharacterCount() != sequenceLength) {
throw new Error("layout length is incorrect!");
}
final float sequenceAdvance = layout.getVisibleAdvance();
float wrappingWidth = sequenceAdvance * 2;
// now run test with a variety of widths
while (wrappingWidth < (sequenceAdvance*NUM_WORDS)) {
measurer.setPosition(0);
checkMeasurer(measurer,
wrappingWidth,
sequenceAdvance,
text.length());
wrappingWidth += sequenceAdvance / 5;
}
}
/**
* Iterate through measurer and check that every line is
* not too long and not too short, but just right.
*/
private void checkMeasurer(LineBreakMeasurer measurer,
float wrappingWidth,
float sequenceAdvance,
int endPosition) {
do {
TextLayout layout = measurer.nextLayout(wrappingWidth);
float visAdvance = layout.getVisibleAdvance();
// Check that wrappingWidth-sequenceAdvance < visAdvance
// Also, if we're not at the end of the paragraph,
// check that visAdvance <= wrappingWidth
if (visAdvance > wrappingWidth) {
// line is too long for given wrapping width
throw new Error("layout is too long");
}
if (measurer.getPosition() < endPosition) {
if (visAdvance <= wrappingWidth - sequenceAdvance) {
// line is too short for given wrapping width;
// another word would have fit
throw new Error("room for more words on line. diff=" +
(wrappingWidth - sequenceAdvance - visAdvance));
}
}
} while (measurer.getPosition() != endPosition);
}
private static void drawString(Graphics2D g, Font font, String value, float x, float y) {
AttributedString str = new AttributedString(value);
str.addAttribute(TextAttribute.FOREGROUND, Color.BLACK);
str.addAttribute(TextAttribute.FONT, font);
FontRenderContext frc = new FontRenderContext(null, true, true);
TextLayout layout = new LineBreakMeasurer(str.getIterator(), frc).nextLayout(Integer.MAX_VALUE);
layout.draw(g, x, y);
}
public void drawLabel(String text, GraphicInfo graphicInfo, boolean centered) {
float interline = 1.0f;
// text
if (text != null && text.length() > 0) {
Paint originalPaint = g.getPaint();
Font originalFont = g.getFont();
g.setPaint(LABEL_COLOR);
g.setFont(LABEL_FONT);
int wrapWidth = 100;
double textY = graphicInfo.getY();
// TODO: use drawMultilineText()
AttributedString as = new AttributedString(text);
as.addAttribute(TextAttribute.FOREGROUND, g.getPaint());
as.addAttribute(TextAttribute.FONT, g.getFont());
AttributedCharacterIterator aci = as.getIterator();
FontRenderContext frc = new FontRenderContext(null, true, false);
LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc);
while (lbm.getPosition() < text.length()) {
TextLayout tl = lbm.nextLayout(wrapWidth);
textY += tl.getAscent();
Rectangle2D bb = tl.getBounds();
double tX = graphicInfo.getX();
if (centered) {
tX += (int) (graphicInfo.getWidth() / 2 - bb.getWidth() / 2);
}
tl.draw(g, (float) tX, (float) textY);
textY += tl.getDescent() + tl.getLeading() + (interline - 1.0f) * tl.getAscent();
}
// restore originals
g.setFont(originalFont);
g.setPaint(originalPaint);
}
}
public LineBreakIterator( ParagraphRenderBox box, FontRenderContext fontRenderContext, AttributedCharacterIterator ci ) {
this.wrappingWidth = (float) StrictGeomUtility.toExternalValue( box.getCachedWidth() );
this.justifiedLayout =
ElementAlignment.JUSTIFY.equals( box.getStyleSheet().getStyleProperty( ElementStyleKeys.ALIGNMENT ) );
this.ci = ci;
this.lineBreakMeasurer = new LineBreakMeasurer( ci, fontRenderContext );
this.lineBreakMeasurer.setPosition( ci.getBeginIndex() );
}
protected TextLayout postProcess( final int start, final TextLayout textLayout,
final LineBreakMeasurer lineBreakMeasurer ) {
if ( justifiedLayout ) {
return textLayout.getJustifiedLayout( wrappingWidth );
} else {
return textLayout;
}
}
void rescale(double scale) {
Rectangle bounds = getBounds(scale);
HashMap settings = new HashMap();
settings.put(TextAttribute.FONT, new Font(style.getFontAttributes(scale)));
AttributedCharacterIterator par = (new AttributedString(element.getAttribute("text"), settings)).getIterator();
LineBreakMeasurer lbm = new LineBreakMeasurer(par, new FontRenderContext(null, false, false));
ArrayList drawList = new ArrayList();
int parEnd = par.getEndIndex();
int positionX;
int positionY = bounds.y;
lbm.setPosition(par.getBeginIndex());
while (lbm.getPosition() < parEnd) {
TextLayout layout = lbm.nextLayout(bounds.width);
positionX = bounds.x;
if (!layout.isLeftToRight()) {
positionX += bounds.width - (int) layout.getAdvance();
}
positionY += layout.getAscent();
if (positionY > bounds.y+bounds.height) break;
drawList.add(new Point(positionX, positionY));
drawList.add(layout);
positionY += layout.getDescent() + layout.getLeading();
}
textPositions = new Point[drawList.size()/2];
textLines = new TextLayout[drawList.size()/2];
textScale = scale;
for (int i = 0; i < textPositions.length; i++) {
textPositions[i] = (Point) drawList.get(i*2);
textLines[i] = (TextLayout) drawList.get(i*2+1);
}
}
void rescale(double scale) {
Rectangle bounds = getBounds(scale);
HashMap settings = new HashMap();
settings.put(TextAttribute.FONT, new Font(style.getFontAttributes(scale * Integer.parseInt(element.getAttribute("size")))));
AttributedCharacterIterator par = (new AttributedString(element.getAttribute("text"), settings)).getIterator();
LineBreakMeasurer lbm = new LineBreakMeasurer(par, new FontRenderContext(null, false, false));
ArrayList drawList = new ArrayList();
int parEnd = par.getEndIndex();
int positionX;
int positionY = bounds.y;
lbm.setPosition(par.getBeginIndex());
while (lbm.getPosition() < parEnd) {
TextLayout layout = lbm.nextLayout(bounds.width);
positionX = bounds.x;
if (!layout.isLeftToRight()) {
positionX += bounds.width - (int) layout.getAdvance();
}
positionY += layout.getAscent();
if (positionY > bounds.y+bounds.height) break;
drawList.add(new Point(positionX, positionY));
drawList.add(layout);
positionY += layout.getDescent() + layout.getLeading();
}
textPositions = new Point[drawList.size()/2];
textLines = new TextLayout[drawList.size()/2];
textScale = scale;
for (int i = 0; i < textPositions.length; i++) {
textPositions[i] = (Point) drawList.get(i*2);
textLines[i] = (TextLayout) drawList.get(i*2+1);
}
}
private void calcFontMetrics( Graphics2D g2d, int w, int h ) {
FontMetrics fm;
Graphics2D g2 = (Graphics2D)g2d.create();
/// ABP
if ( g2Transform != NONE && textToUse != FILE_TEXT ) {
g2.setFont( g2.getFont().deriveFont( getAffineTransform( g2Transform )) );
fm = g2.getFontMetrics();
}
else {
fm = g2.getFontMetrics();
}
maxAscent = fm.getMaxAscent();
maxDescent = fm.getMaxDescent();
if (maxAscent == 0) maxAscent = 10;
if (maxDescent == 0) maxDescent = 5;
if ( textToUse == RANGE_TEXT || textToUse == ALL_GLYPHS ) {
/// Give slight extra room for each character
maxAscent += 3;
maxDescent += 3;
gridWidth = fm.getMaxAdvance() + 6;
gridHeight = maxAscent + maxDescent;
if ( force16Cols )
numCharAcross = 16;
else
numCharAcross = ( w - 10 ) / gridWidth;
numCharDown = ( h - 10 ) / gridHeight;
canvasInset_X = ( w - numCharAcross * gridWidth ) / 2;
canvasInset_Y = ( h - numCharDown * gridHeight ) / 2;
if ( numCharDown == 0 || numCharAcross == 0 )
throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW );
if ( !isPrinting )
resetScrollbar( verticalBar.getValue() * numCharAcross );
}
else {
maxDescent += fm.getLeading();
canvasInset_X = 5;
canvasInset_Y = 5;
/// gridWidth and numCharAcross will not be used in this mode...
gridHeight = maxAscent + maxDescent;
numCharDown = ( h - canvasInset_Y * 2 ) / gridHeight;
if ( numCharDown == 0 )
throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW );
/// If this is text loaded from file, prepares the LineBreak'ed
/// text layout at this point
if ( textToUse == FILE_TEXT ) {
if ( !isPrinting )
f2dt.fireChangeStatus( "LineBreaking Text... Please Wait", false );
lineBreakTLs = new Vector();
for ( int i = 0; i < fileText.length; i++ ) {
AttributedString as =
new AttributedString( fileText[i], g2.getFont().getAttributes() );
LineBreakMeasurer lbm =
new LineBreakMeasurer( as.getIterator(), g2.getFontRenderContext() );
while ( lbm.getPosition() < fileText[i].length() )
lineBreakTLs.add( lbm.nextLayout( (float) w ));
}
}
if ( !isPrinting )
resetScrollbar( verticalBar.getValue() );
}
}
private void calcFontMetrics( Graphics2D g2d, int w, int h ) {
FontMetrics fm;
Graphics2D g2 = (Graphics2D)g2d.create();
/// ABP
if ( g2Transform != NONE && textToUse != FILE_TEXT ) {
g2.setFont( g2.getFont().deriveFont( getAffineTransform( g2Transform )) );
fm = g2.getFontMetrics();
}
else {
fm = g2.getFontMetrics();
}
maxAscent = fm.getMaxAscent();
maxDescent = fm.getMaxDescent();
if (maxAscent == 0) maxAscent = 10;
if (maxDescent == 0) maxDescent = 5;
if ( textToUse == RANGE_TEXT || textToUse == ALL_GLYPHS ) {
/// Give slight extra room for each character
maxAscent += 3;
maxDescent += 3;
gridWidth = fm.getMaxAdvance() + 6;
gridHeight = maxAscent + maxDescent;
if ( force16Cols )
numCharAcross = 16;
else
numCharAcross = ( w - 10 ) / gridWidth;
numCharDown = ( h - 10 ) / gridHeight;
canvasInset_X = ( w - numCharAcross * gridWidth ) / 2;
canvasInset_Y = ( h - numCharDown * gridHeight ) / 2;
if ( numCharDown == 0 || numCharAcross == 0 )
throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW );
if ( !isPrinting )
resetScrollbar( verticalBar.getValue() * numCharAcross );
}
else {
maxDescent += fm.getLeading();
canvasInset_X = 5;
canvasInset_Y = 5;
/// gridWidth and numCharAcross will not be used in this mode...
gridHeight = maxAscent + maxDescent;
numCharDown = ( h - canvasInset_Y * 2 ) / gridHeight;
if ( numCharDown == 0 )
throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW );
/// If this is text loaded from file, prepares the LineBreak'ed
/// text layout at this point
if ( textToUse == FILE_TEXT ) {
if ( !isPrinting )
f2dt.fireChangeStatus( "LineBreaking Text... Please Wait", false );
lineBreakTLs = new Vector();
for ( int i = 0; i < fileText.length; i++ ) {
AttributedString as =
new AttributedString( fileText[i], g2.getFont().getAttributes() );
LineBreakMeasurer lbm =
new LineBreakMeasurer( as.getIterator(), g2.getFontRenderContext() );
while ( lbm.getPosition() < fileText[i].length() )
lineBreakTLs.add( lbm.nextLayout( (float) w ));
}
}
if ( !isPrinting )
resetScrollbar( verticalBar.getValue() );
}
}
private void calcFontMetrics( Graphics2D g2d, int w, int h ) {
FontMetrics fm;
Graphics2D g2 = (Graphics2D)g2d.create();
/// ABP
if ( g2Transform != NONE && textToUse != FILE_TEXT ) {
g2.setFont( g2.getFont().deriveFont( getAffineTransform( g2Transform )) );
fm = g2.getFontMetrics();
}
else {
fm = g2.getFontMetrics();
}
maxAscent = fm.getMaxAscent();
maxDescent = fm.getMaxDescent();
if (maxAscent == 0) maxAscent = 10;
if (maxDescent == 0) maxDescent = 5;
if ( textToUse == RANGE_TEXT || textToUse == ALL_GLYPHS ) {
/// Give slight extra room for each character
maxAscent += 3;
maxDescent += 3;
gridWidth = fm.getMaxAdvance() + 6;
gridHeight = maxAscent + maxDescent;
if ( force16Cols )
numCharAcross = 16;
else
numCharAcross = ( w - 10 ) / gridWidth;
numCharDown = ( h - 10 ) / gridHeight;
canvasInset_X = ( w - numCharAcross * gridWidth ) / 2;
canvasInset_Y = ( h - numCharDown * gridHeight ) / 2;
if ( numCharDown == 0 || numCharAcross == 0 )
throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW );
if ( !isPrinting )
resetScrollbar( verticalBar.getValue() * numCharAcross );
}
else {
maxDescent += fm.getLeading();
canvasInset_X = 5;
canvasInset_Y = 5;
/// gridWidth and numCharAcross will not be used in this mode...
gridHeight = maxAscent + maxDescent;
numCharDown = ( h - canvasInset_Y * 2 ) / gridHeight;
if ( numCharDown == 0 )
throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW );
/// If this is text loaded from file, prepares the LineBreak'ed
/// text layout at this point
if ( textToUse == FILE_TEXT ) {
if ( !isPrinting )
f2dt.fireChangeStatus( "LineBreaking Text... Please Wait", false );
lineBreakTLs = new Vector();
for ( int i = 0; i < fileText.length; i++ ) {
AttributedString as =
new AttributedString( fileText[i], g2.getFont().getAttributes() );
LineBreakMeasurer lbm =
new LineBreakMeasurer( as.getIterator(), g2.getFontRenderContext() );
while ( lbm.getPosition() < fileText[i].length() )
lineBreakTLs.add( lbm.nextLayout( (float) w ));
}
}
if ( !isPrinting )
resetScrollbar( verticalBar.getValue() );
}
}