Search in sources :

Example 1 with FontInfo

use of com.intellij.openapi.editor.impl.FontInfo 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 2 with FontInfo

use of com.intellij.openapi.editor.impl.FontInfo in project intellij-community by JetBrains.

the class EditorUtil method textWidth.

/**
   * Allows to answer what width in pixels is required to draw fragment of the given char array from <code>[start; end)</code> interval
   * at the given editor.
   * <p/>
   * Tabulation symbols is processed specially, i.e. it's ta
   * <p/>
   * <b>Note:</b> it's assumed that target text fragment remains to the single line, i.e. line feed symbols within it are not
   * treated specially.
   *
   * @param editor    editor that will be used for target text representation
   * @param text      target text holder
   * @param start     offset within the given char array that points to target text start (inclusive)
   * @param end       offset within the given char array that points to target text end (exclusive)
   * @param fontType  font type to use for target text representation
   * @param x         <code>'x'</code> coordinate that should be used as a starting point for target text representation.
   *                  It's necessity is implied by the fact that IDEA editor may represent tabulation symbols in any range
   *                  from <code>[1; tab size]</code> (check {@link #nextTabStop(int, Editor)} for more details)
   * @return          width in pixels required for target text representation
   */
public static int textWidth(@NotNull Editor editor, @NotNull CharSequence text, int start, int end, @JdkConstants.FontStyle int fontType, int x) {
    int result = 0;
    for (int i = start; i < end; i++) {
        char c = text.charAt(i);
        if (c != '\t') {
            FontInfo font = fontForChar(c, fontType, editor);
            result += font.charWidth(c);
            continue;
        }
        result += nextTabStop(x + result, editor) - result - x;
    }
    return result;
}
Also used : FontInfo(com.intellij.openapi.editor.impl.FontInfo)

Example 3 with FontInfo

use of com.intellij.openapi.editor.impl.FontInfo in project intellij-community by JetBrains.

the class TextBasedSoftWrapPainter method paint.

@Override
public int paint(@NotNull Graphics g, @NotNull SoftWrapDrawingType drawingType, int x, int y, int lineHeight) {
    FontInfo fontInfo = myFonts.get(drawingType);
    if (fontInfo != null) {
        char[] buffer = mySymbols.get(drawingType);
        int vGap = myVGaps.get(drawingType);
        myDrawingCallback.drawChars(g, buffer, 0, buffer.length, x, y + lineHeight - vGap, myColorHolder.getColor(), fontInfo);
    }
    return getMinDrawingWidth(drawingType);
}
Also used : FontInfo(com.intellij.openapi.editor.impl.FontInfo)

Example 4 with FontInfo

use of com.intellij.openapi.editor.impl.FontInfo in project intellij-community by JetBrains.

the class TextBasedSoftWrapPainter method reinit.

/**
   * Tries to find fonts that are capable to display all unicode symbols used by the current painter.
   */
@Override
public void reinit() {
    // We use dummy component here in order to being able to work with font metrics.
    JLabel component = new JLabel();
    myCanUse = true;
    for (Map.Entry<SoftWrapDrawingType, char[]> entry : mySymbols.entrySet()) {
        SoftWrapDrawingType type = entry.getKey();
        char c = entry.getValue()[0];
        FontInfo fontInfo = EditorUtil.fontForChar(c, Font.PLAIN, myEditor);
        if (!fontInfo.canDisplay(c)) {
            myCanUse = false;
            myFonts.put(type, null);
            myVGaps.put(type, null);
            myWidths[type.ordinal()] = 0;
        } else {
            myFonts.put(type, fontInfo);
            FontMetrics metrics = component.getFontMetrics(fontInfo.getFont());
            myWidths[type.ordinal()] = metrics.charWidth(c);
            int vGap = metrics.getDescent();
            myVGaps.put(type, vGap);
        }
    }
}
Also used : EnumMap(java.util.EnumMap) Map(java.util.Map) FontInfo(com.intellij.openapi.editor.impl.FontInfo)

Example 5 with FontInfo

use of com.intellij.openapi.editor.impl.FontInfo in project intellij-community by JetBrains.

the class LineLayout method addFragments.

@SuppressWarnings("AssignmentToForLoopParameter")
private static void addFragments(BidiRun run, Chunk chunk, char[] text, int start, int end, int fontStyle, FontPreferences fontPreferences, FontRenderContext fontRenderContext, @Nullable TabFragment tabFragment) {
    assert start < end;
    FontInfo currentFontInfo = null;
    int currentIndex = start;
    for (int i = start; i < end; i++) {
        char c = text[i];
        if (c == '\t' && tabFragment != null) {
            assert run.level == 0;
            addTextFragmentIfNeeded(chunk, text, currentIndex, i, currentFontInfo, false);
            chunk.fragments.add(tabFragment);
            currentFontInfo = null;
            currentIndex = i + 1;
        } else {
            boolean surrogatePair = false;
            int codePoint = c;
            if (Character.isHighSurrogate(c) && (i + 1 < end)) {
                char nextChar = text[i + 1];
                if (Character.isLowSurrogate(nextChar)) {
                    codePoint = Character.toCodePoint(c, nextChar);
                    surrogatePair = true;
                }
            }
            FontInfo fontInfo = ComplementaryFontsRegistry.getFontAbleToDisplay(codePoint, fontStyle, fontPreferences, fontRenderContext);
            if (!fontInfo.equals(currentFontInfo)) {
                addTextFragmentIfNeeded(chunk, text, currentIndex, i, currentFontInfo, run.isRtl());
                currentFontInfo = fontInfo;
                currentIndex = i;
            }
            if (surrogatePair)
                i++;
        }
    }
    addTextFragmentIfNeeded(chunk, text, currentIndex, end, currentFontInfo, run.isRtl());
    assert !chunk.fragments.isEmpty();
}
Also used : FontInfo(com.intellij.openapi.editor.impl.FontInfo)

Aggregations

FontInfo (com.intellij.openapi.editor.impl.FontInfo)5 AbstractMockGlyphVector (com.intellij.testFramework.AbstractMockGlyphVector)1 MockFontLayoutService (com.intellij.testFramework.MockFontLayoutService)1 FontRenderContext (java.awt.font.FontRenderContext)1 GlyphVector (java.awt.font.GlyphVector)1 EnumMap (java.util.EnumMap)1 Map (java.util.Map)1 NotNull (org.jetbrains.annotations.NotNull)1