Search in sources :

Example 26 with FontRenderContext

use of java.awt.font.FontRenderContext in project intellij-community by JetBrains.

the class LineLayout method createFragments.

private static List<BidiRun> createFragments(@NotNull EditorView view, @NotNull CharSequence text, @JdkConstants.FontStyle int fontStyle) {
    if (text.length() == 0)
        return Collections.emptyList();
    FontRenderContext fontRenderContext = view.getFontRenderContext();
    FontPreferences fontPreferences = view.getEditor().getColorsScheme().getFontPreferences();
    char[] chars = CharArrayUtil.fromSequence(text);
    List<BidiRun> runs = createRuns(view, chars, -1);
    for (BidiRun run : runs) {
        for (Chunk chunk : run.getChunks(text, 0)) {
            chunk.fragments = new ArrayList<>();
            addFragments(run, chunk, chars, chunk.startOffset, chunk.endOffset, fontStyle, fontPreferences, fontRenderContext, null);
        }
    }
    return runs;
}
Also used : FontPreferences(com.intellij.openapi.editor.colors.FontPreferences) FontRenderContext(java.awt.font.FontRenderContext)

Example 27 with FontRenderContext

use of java.awt.font.FontRenderContext in project intellij-community by JetBrains.

the class ComplexTextFragmentTest method assertCaretPositionsForGlyphVector.

private static void assertCaretPositionsForGlyphVector(MyGlyphVector gv, int... expectedPositions) {
    FontLayoutService.setInstance(new MockFontLayoutService(TEST_CHAR_WIDTH, TEST_LINE_HEIGHT, TEST_DESCENT) {

        @NotNull
        @Override
        public GlyphVector layoutGlyphVector(@NotNull Font font, @NotNull FontRenderContext fontRenderContext, @NotNull char[] chars, int start, int end, boolean isRtl) {
            return gv;
        }
    });
    try {
        int length = gv.getNumChars();
        char[] text = new char[length];
        FontInfo fontInfo = new FontInfo(Font.MONOSPACED, 1, Font.PLAIN, false, new FontRenderContext(null, false, false));
        ComplexTextFragment fragment = new ComplexTextFragment(text, 0, length, (gv.getLayoutFlags() & GlyphVector.FLAG_RUN_RTL) != 0, fontInfo);
        int[] charPositions = new int[length];
        for (int i = 0; i < length; i++) {
            charPositions[i] = (int) fragment.visualColumnToX(0, i + 1);
        }
        assertArrayEquals(expectedPositions, charPositions);
    } finally {
        FontLayoutService.setInstance(null);
    }
}
Also used : GlyphVector(java.awt.font.GlyphVector) AbstractMockGlyphVector(com.intellij.testFramework.AbstractMockGlyphVector) MockFontLayoutService(com.intellij.testFramework.MockFontLayoutService) FontRenderContext(java.awt.font.FontRenderContext) NotNull(org.jetbrains.annotations.NotNull) FontInfo(com.intellij.openapi.editor.impl.FontInfo)

Example 28 with FontRenderContext

use of java.awt.font.FontRenderContext in project intellij-community by JetBrains.

the class UIUtil method getHighestGlyphHeight.

/**
   * @param string {@code String} to examine
   * @param font {@code Font} that is used to render the string
   * @param graphics {@link Graphics} that should be used to render the string
   * @return height of the tallest glyph in a string. If string is empty, returns 0
   */
public static int getHighestGlyphHeight(@NotNull String string, @NotNull Font font, @NotNull Graphics graphics) {
    FontRenderContext frc = ((Graphics2D) graphics).getFontRenderContext();
    GlyphVector gv = font.createGlyphVector(frc, string);
    int maxHeight = 0;
    for (int i = 0; i < string.length(); i++) {
        maxHeight = Math.max(maxHeight, (int) gv.getGlyphMetrics(i).getBounds2D().getHeight());
    }
    return maxHeight;
}
Also used : GlyphVector(java.awt.font.GlyphVector) FontRenderContext(java.awt.font.FontRenderContext)

Example 29 with FontRenderContext

use of java.awt.font.FontRenderContext in project android by JetBrains.

the class BlueprintLayer method drawComponent.

/**
   * Draw the given component and its children
   *
   * @param gc                 the graphics context
   * @param component          the component we want to draw
   * @param viewHandlerManager the view handler
   */
private boolean drawComponent(@NotNull Graphics2D gc, @NotNull NlComponent component, @NotNull ViewHandlerManager viewHandlerManager, boolean parentHandlesPainting) {
    boolean needsRepaint = false;
    boolean handlesPainting = false;
    if (component.viewInfo != null) {
        ViewHandler handler = component.getViewHandler();
        // Check if the view handler handles the painting
        if (handler != null && handler instanceof ViewGroupHandler) {
            ViewGroupHandler viewGroupHandler = (ViewGroupHandler) handler;
            if (viewGroupHandler.handlesPainting()) {
                if (handler.paintConstraints(myScreenView, gc, component)) {
                    return needsRepaint;
                }
                needsRepaint |= viewGroupHandler.drawGroup(gc, myScreenView, component);
                handlesPainting = true;
            }
        }
        if (!handlesPainting && !parentHandlesPainting) {
            // If not, layout the component ourselves
            Graphics2D g = (Graphics2D) gc.create();
            int x = getSwingX(myScreenView, component.x);
            int y = getSwingY(myScreenView, component.y);
            int w = getSwingDimension(myScreenView, component.w);
            int h = getSwingDimension(myScreenView, component.h);
            drawComponentBackground(g, component);
            String name = component.getTagName();
            name = name.substring(name.lastIndexOf('.') + 1);
            Font font = BLUEPRINT_TEXT_FONT;
            g.setFont(font);
            g.setColor(BLUEPRINT_FG_COLOR);
            String id = component.getId();
            int lineHeight = g.getFontMetrics().getHeight();
            FontRenderContext fontRenderContext = g.getFontRenderContext();
            if (id != null && h > lineHeight * 2) {
                // Can fit both
                Rectangle2D classBounds = font.getStringBounds(name, fontRenderContext);
                Rectangle2D idBounds = font.getStringBounds(id, fontRenderContext);
                int textY = y + h / 2;
                int textX = x + w / 2 - ((int) classBounds.getWidth()) / 2;
                if (component.isRoot()) {
                    textX = x + lineHeight;
                    textY = y - (int) (classBounds.getHeight() + idBounds.getHeight());
                }
                g.drawString(name, textX, textY);
                if (component.isRoot()) {
                    textX = x + lineHeight;
                    textY = y - (int) (idBounds.getHeight());
                } else {
                    textX = x + w / 2 - ((int) idBounds.getWidth()) / 2;
                    textY += (int) (idBounds.getHeight());
                }
                g.drawString(id, textX, textY);
            } else {
                // Only room for a single line: prioritize the id if it's available, otherwise the class name
                String text = id != null ? id : name;
                Rectangle2D stringBounds = font.getStringBounds(text, fontRenderContext);
                int textX = x + w / 2 - ((int) stringBounds.getWidth()) / 2;
                int textY = y + h / 2 + ((int) stringBounds.getHeight()) / 2;
                g.drawString(text, textX, textY);
            }
            g.dispose();
        }
    }
    // Draw the children of the component...
    for (NlComponent child : component.getChildren()) {
        needsRepaint |= drawComponent(gc, child, viewHandlerManager, handlesPainting);
    }
    return needsRepaint;
}
Also used : NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) ViewHandler(com.android.tools.idea.uibuilder.api.ViewHandler) Rectangle2D(java.awt.geom.Rectangle2D) ViewGroupHandler(com.android.tools.idea.uibuilder.api.ViewGroupHandler) FontRenderContext(java.awt.font.FontRenderContext)

Example 30 with FontRenderContext

use of java.awt.font.FontRenderContext in project jdk8u_jdk by JetBrains.

the class FontDesignMetrics method readObject.

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    in.defaultReadObject();
    if (serVersion != CURRENT_VERSION) {
        frc = getDefaultFrc();
        isAntiAliased = frc.isAntiAliased();
        usesFractionalMetrics = frc.usesFractionalMetrics();
        frcTx = frc.getTransform();
    } else {
        frc = new FontRenderContext(frcTx, isAntiAliased, usesFractionalMetrics);
    }
    // when deserialized, members are set to their default values for their type--
    // not to the values assigned during initialization before the constructor
    // body!
    height = -1;
    cache = null;
    initMatrixAndMetrics();
    initAdvCache();
}
Also used : FontRenderContext(java.awt.font.FontRenderContext)

Aggregations

FontRenderContext (java.awt.font.FontRenderContext)75 Font (java.awt.Font)25 TextLayout (java.awt.font.TextLayout)21 GlyphVector (java.awt.font.GlyphVector)18 Graphics2D (java.awt.Graphics2D)16 Rectangle2D (java.awt.geom.Rectangle2D)15 AttributedString (java.text.AttributedString)14 AffineTransform (java.awt.geom.AffineTransform)13 Paint (java.awt.Paint)8 LineBreakMeasurer (java.awt.font.LineBreakMeasurer)7 AttributedCharacterIterator (java.text.AttributedCharacterIterator)7 LineMetrics (java.awt.font.LineMetrics)5 BufferedImage (java.awt.image.BufferedImage)5 ArrayList (java.util.ArrayList)5 Point (java.awt.Point)4 Shape (java.awt.Shape)4 BasicStroke (java.awt.BasicStroke)3 Color (java.awt.Color)3 FontMetrics (java.awt.FontMetrics)3 RoundRectangle2D (java.awt.geom.RoundRectangle2D)3