Java Code Examples for java.awt.font.TextLayout#getVisualHighlightShape()

The following examples show how to use java.awt.font.TextLayout#getVisualHighlightShape() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TextMeasureTests.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public void runTest(Object ctx, int numReps) {
    TLExContext tlctx = (TLExContext)ctx;
    TextLayout tl = tlctx.tl;
    TextHitInfo[] hits = tlctx.hits;
    Rectangle2D lb = tlctx.lb;
    Shape s;
    if (hits.length < 3) {
        do {
            s = tl.getVisualHighlightShape(hits[0], hits[hits.length - 1], lb);
        } while (--numReps >= 0);
    } else {
        do {
            for (int i = 3; i < hits.length; ++i) {
                s = tl.getVisualHighlightShape(hits[i-3], hits[i], lb);
            }
        } while (--numReps >= 0);
    }
}
 
Example 2
Source File: TextMeasureTests.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public void runTest(Object ctx, int numReps) {
    TLExContext tlctx = (TLExContext)ctx;
    TextLayout tl = tlctx.tl;
    TextHitInfo[] hits = tlctx.hits;
    Rectangle2D lb = tlctx.lb;
    Shape s;
    if (hits.length < 3) {
        do {
            s = tl.getVisualHighlightShape(hits[0], hits[hits.length - 1], lb);
        } while (--numReps >= 0);
    } else {
        do {
            for (int i = 3; i < hits.length; ++i) {
                s = tl.getVisualHighlightShape(hits[i-3], hits[i], lb);
            }
        } while (--numReps >= 0);
    }
}
 
Example 3
Source File: TextMeasureTests.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public void runTest(Object ctx, int numReps) {
    TLExContext tlctx = (TLExContext)ctx;
    TextLayout tl = tlctx.tl;
    TextHitInfo[] hits = tlctx.hits;
    Rectangle2D lb = tlctx.lb;
    Shape s;
    if (hits.length < 3) {
        do {
            s = tl.getVisualHighlightShape(hits[0], hits[hits.length - 1], lb);
        } while (--numReps >= 0);
    } else {
        do {
            for (int i = 3; i < hits.length; ++i) {
                s = tl.getVisualHighlightShape(hits[i-3], hits[i], lb);
            }
        } while (--numReps >= 0);
    }
}
 
Example 4
Source File: TextMeasureTests.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public void runTest(Object ctx, int numReps) {
    TLExContext tlctx = (TLExContext)ctx;
    TextLayout tl = tlctx.tl;
    TextHitInfo[] hits = tlctx.hits;
    Rectangle2D lb = tlctx.lb;
    Shape s;
    if (hits.length < 3) {
        do {
            s = tl.getVisualHighlightShape(hits[0], hits[hits.length - 1], lb);
        } while (--numReps >= 0);
    } else {
        do {
            for (int i = 3; i < hits.length; ++i) {
                s = tl.getVisualHighlightShape(hits[i-3], hits[i], lb);
            }
        } while (--numReps >= 0);
    }
}
 
Example 5
Source File: TextLayoutUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
     * Get real allocation (possibly not rectangular) of a part of layout.
     * <br>
     * It's used when rendering the text layout for filling background highlights of the view.
     *
     * @param length Total number of characters for which the allocation is computed.
     * @param alloc Allocation given by a parent view.
     * @return
     */
    public static Shape getRealAlloc(TextLayout textLayout, Rectangle2D textLayoutRect,
            TextHitInfo startHit, TextHitInfo endHit)
    {
        // Quick-fix to eliminate missing line in rendering italic "d" - more elaborate fix is needed
        textLayoutRect = new Rectangle2D.Double(textLayoutRect.getX(), textLayoutRect.getY(),
                textLayoutRect.getWidth() + 2, textLayoutRect.getHeight());
        Rectangle2D.Double zeroBasedRect = ViewUtils.shape2Bounds(textLayoutRect);
        zeroBasedRect.x = 0;
        zeroBasedRect.y = 0;
        Shape ret = textLayout.getVisualHighlightShape(startHit, endHit, zeroBasedRect);
        AffineTransform transform = AffineTransform.getTranslateInstance(
                textLayoutRect.getX(),
                textLayoutRect.getY()
        );
        ret = transform.createTransformedShape(ret);
        // The following gives bad result for some reason (works for layout but not for caret modelToView())
//        Shape ret2 = textLayout.getVisualHighlightShape(startHit.getCharIndex(), endHit.getCharIndex(), textLayoutRect);
        return ret;
    }
 
Example 6
Source File: TextMeasureTests.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public void runTest(Object ctx, int numReps) {
    TLExContext tlctx = (TLExContext)ctx;
    TextLayout tl = tlctx.tl;
    TextHitInfo[] hits = tlctx.hits;
    Rectangle2D lb = tlctx.lb;
    Shape s;
    if (hits.length < 3) {
        do {
            s = tl.getVisualHighlightShape(hits[0], hits[hits.length - 1], lb);
        } while (--numReps >= 0);
    } else {
        do {
            for (int i = 3; i < hits.length; ++i) {
                s = tl.getVisualHighlightShape(hits[i-3], hits[i], lb);
            }
        } while (--numReps >= 0);
    }
}
 
Example 7
Source File: TextMeasureTests.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public void runTest(Object ctx, int numReps) {
    TLExContext tlctx = (TLExContext)ctx;
    TextLayout tl = tlctx.tl;
    TextHitInfo[] hits = tlctx.hits;
    Rectangle2D lb = tlctx.lb;
    Shape s;
    if (hits.length < 3) {
        do {
            s = tl.getVisualHighlightShape(hits[0], hits[hits.length - 1], lb);
        } while (--numReps >= 0);
    } else {
        do {
            for (int i = 3; i < hits.length; ++i) {
                s = tl.getVisualHighlightShape(hits[i-3], hits[i], lb);
            }
        } while (--numReps >= 0);
    }
}
 
Example 8
Source File: TextMeasureTests.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public void runTest(Object ctx, int numReps) {
    TLExContext tlctx = (TLExContext)ctx;
    TextLayout tl = tlctx.tl;
    TextHitInfo[] hits = tlctx.hits;
    Rectangle2D lb = tlctx.lb;
    Shape s;
    if (hits.length < 3) {
        do {
            s = tl.getVisualHighlightShape(hits[0], hits[hits.length - 1], lb);
        } while (--numReps >= 0);
    } else {
        do {
            for (int i = 3; i < hits.length; ++i) {
                s = tl.getVisualHighlightShape(hits[i-3], hits[i], lb);
            }
        } while (--numReps >= 0);
    }
}
 
Example 9
Source File: TextMeasureTests.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public void runTest(Object ctx, int numReps) {
    TLExContext tlctx = (TLExContext)ctx;
    TextLayout tl = tlctx.tl;
    TextHitInfo[] hits = tlctx.hits;
    Rectangle2D lb = tlctx.lb;
    Shape s;
    if (hits.length < 3) {
        do {
            s = tl.getVisualHighlightShape(hits[0], hits[hits.length - 1], lb);
        } while (--numReps >= 0);
    } else {
        do {
            for (int i = 3; i < hits.length; ++i) {
                s = tl.getVisualHighlightShape(hits[i-3], hits[i], lb);
            }
        } while (--numReps >= 0);
    }
}
 
Example 10
Source File: TextMeasureTests.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public void runTest(Object ctx, int numReps) {
    TLExContext tlctx = (TLExContext)ctx;
    TextLayout tl = tlctx.tl;
    TextHitInfo[] hits = tlctx.hits;
    Rectangle2D lb = tlctx.lb;
    Shape s;
    if (hits.length < 3) {
        do {
            s = tl.getVisualHighlightShape(hits[0], hits[hits.length - 1], lb);
        } while (--numReps >= 0);
    } else {
        do {
            for (int i = 3; i < hits.length; ++i) {
                s = tl.getVisualHighlightShape(hits[i-3], hits[i], lb);
            }
        } while (--numReps >= 0);
    }
}
 
Example 11
Source File: TextMeasureTests.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public void runTest(Object ctx, int numReps) {
    TLExContext tlctx = (TLExContext)ctx;
    TextLayout tl = tlctx.tl;
    TextHitInfo[] hits = tlctx.hits;
    Rectangle2D lb = tlctx.lb;
    Shape s;
    if (hits.length < 3) {
        do {
            s = tl.getVisualHighlightShape(hits[0], hits[hits.length - 1], lb);
        } while (--numReps >= 0);
    } else {
        do {
            for (int i = 3; i < hits.length; ++i) {
                s = tl.getVisualHighlightShape(hits[i-3], hits[i], lb);
            }
        } while (--numReps >= 0);
    }
}
 
Example 12
Source File: TextMeasureTests.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public void runTest(Object ctx, int numReps) {
    TLExContext tlctx = (TLExContext)ctx;
    TextLayout tl = tlctx.tl;
    TextHitInfo[] hits = tlctx.hits;
    Rectangle2D lb = tlctx.lb;
    Shape s;
    if (hits.length < 3) {
        do {
            s = tl.getVisualHighlightShape(hits[0], hits[hits.length - 1], lb);
        } while (--numReps >= 0);
    } else {
        do {
            for (int i = 3; i < hits.length; ++i) {
                s = tl.getVisualHighlightShape(hits[i-3], hits[i], lb);
            }
        } while (--numReps >= 0);
    }
}
 
Example 13
Source File: TextMeasureTests.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public void runTest(Object ctx, int numReps) {
    TLExContext tlctx = (TLExContext)ctx;
    TextLayout tl = tlctx.tl;
    TextHitInfo[] hits = tlctx.hits;
    Rectangle2D lb = tlctx.lb;
    Shape s;
    if (hits.length < 3) {
        do {
            s = tl.getVisualHighlightShape(hits[0], hits[hits.length - 1], lb);
        } while (--numReps >= 0);
    } else {
        do {
            for (int i = 3; i < hits.length; ++i) {
                s = tl.getVisualHighlightShape(hits[i-3], hits[i], lb);
            }
        } while (--numReps >= 0);
    }
}
 
Example 14
Source File: TextMeasureTests.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
public void runTest(Object ctx, int numReps) {
    TLExContext tlctx = (TLExContext)ctx;
    TextLayout tl = tlctx.tl;
    TextHitInfo[] hits = tlctx.hits;
    Rectangle2D lb = tlctx.lb;
    Shape s;
    if (hits.length < 3) {
        do {
            s = tl.getVisualHighlightShape(hits[0], hits[hits.length - 1], lb);
        } while (--numReps >= 0);
    } else {
        do {
            for (int i = 3; i < hits.length; ++i) {
                s = tl.getVisualHighlightShape(hits[i-3], hits[i], lb);
            }
        } while (--numReps >= 0);
    }
}
 
Example 15
Source File: SwingUtilities2.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Draws the string at the specified location underlining the specified
 * character.
 *
 * @param c JComponent that will display the string, may be null
 * @param g Graphics to draw the text to
 * @param text String to display
 * @param underlinedIndex Index of a character in the string to underline
 * @param x X coordinate to draw the text at
 * @param y Y coordinate to draw the text at
 * @param useFPAPI use floating point API
 */
public static void drawStringUnderlineCharAt(JComponent c, Graphics g,
                                             String text, int underlinedIndex,
                                             float x, float y,
                                             boolean useFPAPI) {
    if (text == null || text.length() <= 0) {
        return;
    }
    SwingUtilities2.drawString(c, g, text, x, y, useFPAPI);
    int textLength = text.length();
    if (underlinedIndex >= 0 && underlinedIndex < textLength ) {
        float underlineRectY = y;
        int underlineRectHeight = 1;
        float underlineRectX = 0;
        int underlineRectWidth = 0;
        boolean isPrinting = isPrinting(g);
        boolean needsTextLayout = isPrinting;
        if (!needsTextLayout) {
            synchronized (charsBufferLock) {
                syncCharsBuffer(text);
                needsTextLayout =
                    isComplexLayout(charsBuffer, 0, textLength);
            }
        }
        if (!needsTextLayout) {
            FontMetrics fm = g.getFontMetrics();
            underlineRectX = x +
                SwingUtilities2.stringWidth(c,fm,
                                    text.substring(0,underlinedIndex));
            underlineRectWidth = fm.charWidth(text.
                                              charAt(underlinedIndex));
        } else {
            Graphics2D g2d = getGraphics2D(g);
            if (g2d != null) {
                TextLayout layout =
                    createTextLayout(c, text, g2d.getFont(),
                                   g2d.getFontRenderContext());
                if (isPrinting) {
                    float screenWidth = (float)g2d.getFont().
                        getStringBounds(text, getFontRenderContext(c)).getWidth();
                    // If text fits the screenWidth, then do not need to justify
                    if (SwingUtilities2.stringWidth(c, g2d.getFontMetrics(),
                                                    text) > screenWidth) {
                        layout = layout.getJustifiedLayout(screenWidth);
                    }
                }
                TextHitInfo leading =
                    TextHitInfo.leading(underlinedIndex);
                TextHitInfo trailing =
                    TextHitInfo.trailing(underlinedIndex);
                Shape shape =
                    layout.getVisualHighlightShape(leading, trailing);
                Rectangle rect = shape.getBounds();
                underlineRectX = x + rect.x;
                underlineRectWidth = rect.width;
            }
        }
        g.fillRect((int) underlineRectX, (int) underlineRectY + 1,
                   underlineRectWidth, underlineRectHeight);
    }
}