java类java.awt.font.TextLayout的实例源码

Font.java 文件源码 项目:jdk8u-jdk 阅读 29 收藏 0 点赞 0 评论 0
/**
 * Returns the logical bounds of the specified array of characters
 * in the specified <code>FontRenderContext</code>.  The logical
 * bounds contains the origin, ascent, advance, and height, which
 * includes the leading.  The logical bounds does not always enclose
 * all the text.  For example, in some languages and in some fonts,
 * accent marks can be positioned above the ascent or below the
 * descent.  To obtain a visual bounding box, which encloses all the
 * text, use the {@link TextLayout#getBounds() getBounds} method of
 * <code>TextLayout</code>.
 * <p>Note: The returned bounds is in baseline-relative coordinates
 * (see {@link java.awt.Font class notes}).
 * @param chars an array of characters
 * @param beginIndex the initial offset in the array of
 * characters
 * @param limit the end offset in the array of characters
 * @param frc the specified <code>FontRenderContext</code>
 * @return a <code>Rectangle2D</code> that is the bounding box of the
 * specified array of characters in the specified
 * <code>FontRenderContext</code>.
 * @throws IndexOutOfBoundsException if <code>beginIndex</code> is
 *         less than zero, or <code>limit</code> is greater than the
 *         length of <code>chars</code>, or <code>beginIndex</code>
 *         is greater than <code>limit</code>.
 * @see FontRenderContext
 * @see Font#createGlyphVector
 * @since 1.2
 */
public Rectangle2D getStringBounds(char [] chars,
                                int beginIndex, int limit,
                                   FontRenderContext frc) {
    if (beginIndex < 0) {
        throw new IndexOutOfBoundsException("beginIndex: " + beginIndex);
    }
    if (limit > chars.length) {
        throw new IndexOutOfBoundsException("limit: " + limit);
    }
    if (beginIndex > limit) {
        throw new IndexOutOfBoundsException("range length: " +
                                            (limit - beginIndex));
    }

    // this code should be in textlayout
    // quick check for simple text, assume GV ok to use if simple

    boolean simple = values == null ||
        (values.getKerning() == 0 && values.getLigatures() == 0 &&
          values.getBaselineTransform() == null);
    if (simple) {
        simple = ! FontUtilities.isComplexText(chars, beginIndex, limit);
    }

    if (simple) {
        GlyphVector gv = new StandardGlyphVector(this, chars, beginIndex,
                                                 limit - beginIndex, frc);
        return gv.getLogicalBounds();
    } else {
        // need char array constructor on textlayout
        String str = new String(chars, beginIndex, limit - beginIndex);
        TextLayout tl = new TextLayout(str, this, frc);
        return new Rectangle2D.Float(0, -tl.getAscent(), tl.getAdvance(),
                                     tl.getAscent() + tl.getDescent() +
                                     tl.getLeading());
    }
}
PathGraphics.java 文件源码 项目:openjdk-jdk10 阅读 24 收藏 0 点赞 0 评论 0
protected void drawString(String str, float x, float y,
                          Font font, FontRenderContext frc, float w) {
    TextLayout layout =
        new TextLayout(str, font, frc);
    Shape textShape =
        layout.getOutline(AffineTransform.getTranslateInstance(x, y));
    fill(textShape);
}
SunGraphics2D.java 文件源码 项目:jdk8u-jdk 阅读 29 收藏 0 点赞 0 评论 0
public void drawChars(char data[], int offset, int length, int x, int y) {

        if (data == null) {
            throw new NullPointerException("char data is null");
        }
        if (offset < 0 || length < 0 || offset + length > data.length) {
            throw new ArrayIndexOutOfBoundsException("bad offset/length");
        }
        if (font.hasLayoutAttributes()) {
            if (data.length == 0) {
                return;
            }
            new TextLayout(new String(data, offset, length),
                           font, getFontRenderContext()).draw(this, x, y);
            return;
        }

        try {
            textpipe.drawChars(this, data, offset, length, x, y);
        } catch (InvalidPipeException e) {
            try {
                revalidateAll();
                textpipe.drawChars(this, data, offset, length, x, y);
            } catch (InvalidPipeException e2) {
                // Still catching the exception; we are not yet ready to
                // validate the surfaceData correctly.  Fail for now and
                // try again next time around.
            }
        } finally {
            surfaceData.markDirty();
        }
    }
HighlightsView.java 文件源码 项目:incubator-netbeans 阅读 20 收藏 0 点赞 0 评论 0
@Override
protected StringBuilder appendViewInfo(StringBuilder sb, int indent, String xyInfo, int importantChildIndex) {
    super.appendViewInfo(sb, indent, xyInfo, importantChildIndex);
    sb.append(" TL=");
    if (textLayout == null) {
        sb.append("<NULL>");
    } else {
        sb.append(TextLayoutUtils.toStringShort((TextLayout) textLayout));
    }
    return sb;
}
HighlightsViewUtils.java 文件源码 项目:incubator-netbeans 阅读 29 收藏 0 点赞 0 评论 0
static int viewToIndex(TextLayout textLayout, double x, Shape alloc, Position.Bias[] biasReturn) {
    Rectangle2D bounds = ViewUtils.shapeAsRect(alloc);
    TextHitInfo hitInfo = x2Index(textLayout, (float)(x - bounds.getX()));
    if (biasReturn != null) {
        biasReturn[0] = hitInfo.isLeadingEdge() ? Position.Bias.Forward : Position.Bias.Backward;
    }
    return hitInfo.getInsertionIndex();
}
TextMeasureTests.java 文件源码 项目:jdk8u-jdk 阅读 28 收藏 0 点赞 0 评论 0
public void runTest(Object ctx, int numReps) {
    TLExContext tlctx = (TLExContext)ctx;
    TextLayout tl = tlctx.tl;
    int numhits = tlctx.hits.length;
    Rectangle2D lb = tlctx.lb;
    TextHitInfo hit;
    for (int i = 0; i <= numhits; ++i) {
        float x = (float)(lb.getMinX() + lb.getWidth() * i / numhits);
        float y = (float)(lb.getMinY() + lb.getHeight() * i / numhits);
        hit = tl.hitTestChar(x, y, lb);
    }
}
GlyphVectorOutline.java 文件源码 项目:openjdk-jdk10 阅读 23 收藏 0 点赞 0 评论 0
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);
}
SkipLinesViewFactory.java 文件源码 项目:incubator-netbeans 阅读 23 收藏 0 点赞 0 评论 0
@Override
public void paint(Graphics2D g, Shape alloc, Rectangle clipBounds) {
    Rectangle2D.Double allocBounds = ViewUtils.shape2Bounds(alloc);
    TextLayout textLayout = getTextLayout();
    if (textLayout != null) {
        g.setColor(component.getForeground());
        EditorView.Parent parent = (EditorView.Parent) getParent();
        float ascent = parent.getViewRenderContext().getDefaultAscent();
        float x = (float) allocBounds.getX();
        float y = (float) allocBounds.getY();
        textLayout.draw(g, x, y + ascent);
    }
}
SkipLinesViewFactory.java 文件源码 项目:incubator-netbeans 阅读 25 收藏 0 点赞 0 评论 0
@Override
public float getPreferredSpan(int axis) {
    EditorView.Parent parent = (EditorView.Parent) getParent();
    if (axis == View.X_AXIS) {
        float advance = 0;
        TextLayout textLayout = getTextLayout();
        if (textLayout == null) {
            return 0f;
        }
        return textLayout.getAdvance();
    } else {
        return (parent != null) ? parent.getViewRenderContext().getDefaultRowHeight() : 0f;
    }
}
CompositionArea.java 文件源码 项目:jdk8u-jdk 阅读 26 收藏 0 点赞 0 评论 0
public void paint(Graphics g) {
    super.paint(g);
    g.setColor(getForeground());
    TextLayout layout = composedTextLayout;
    if (layout != null) {
        layout.draw((Graphics2D) g, TEXT_ORIGIN_X, TEXT_ORIGIN_Y);
    }
    if (caret != null) {
        Rectangle rectangle = getCaretRectangle(caret);
        g.setXORMode(getBackground());
        g.fillRect(rectangle.x, rectangle.y, 1, rectangle.height);
        g.setPaintMode();
    }
}


问题


面经


文章

微信
公众号

扫码关注公众号