开发者

Using java2d user space measurements with TextLayout and LineBreakMeasurer

I have a java2d image defined in user space (mm) to print an identity card. The transformation to pixels is by using an AffineTransform for the required DPI (Screen or print).

I want to wrap text across several lines but the the TextLayout does not respect user space co-ordinates. I was using the following to write wrapped text to a region:

private void drawParagraph(Graphics开发者_运维百科2D g2d, Rectangle2D area, String text) {
    LineBreakMeasurer lineMeasurer;
    AttributedString string = new AttributedString(text);
    AttributedCharacterIterator paragraph = string.getIterator();
    int paragraphStart = paragraph.getBeginIndex();
    int paragraphEnd = paragraph.getEndIndex();
    FontRenderContext frc = g2d.getFontRenderContext();
    lineMeasurer = new LineBreakMeasurer(paragraph, frc);

    float breakWidth = (float)area.getWidth();
    float drawPosY = (float)area.getY();
    float drawPosX = (float)area.getX();
    lineMeasurer.setPosition(paragraphStart);
    while (lineMeasurer.getPosition() < paragraphEnd) {
        TextLayout layout = lineMeasurer.nextLayout(breakWidth);

        drawPosY += layout.getAscent();
        layout.draw(g2d, drawPosX, drawPosY);

        drawPosY += layout.getDescent() + layout.getLeading();
    }        
}

The above code determines font metrics using user space sizing of the Font and thus turn out rather large. The font size is calculated as best vertical fit for the number of lines in an area with the calculation as below. E.g.

attr.put(TextAttribute.SIZE, (geTextArea().getHeight() / noOfLines - LINE_SPACING) );

When using g2d.drawString("Some text to display", x, y) the font size appears correct.

Does anyone have a better way of doing text layout in user space co-ords?


A call to g2d.scale() at the beginning should do it (it worked when I did it, and my code is based on the exact same LineBreakMeasurer example).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜