Search in sources :

Example 1 with PrintColorUIResource

use of sun.swing.PrintColorUIResource in project jdk8u_jdk by JetBrains.

the class SwingUtilities2 method drawString.

/*
     * see documentation for drawChars
     * returns the advance
     */
public static float drawString(JComponent c, Graphics g, AttributedCharacterIterator iterator, int x, int y) {
    float retVal;
    boolean isPrinting = isPrinting(g);
    Color col = g.getColor();
    if (isPrinting) {
        /* Use alternate print color if specified */
        if (col instanceof PrintColorUIResource) {
            g.setColor(((PrintColorUIResource) col).getPrintColor());
        }
    }
    Graphics2D g2d = getGraphics2D(g);
    if (g2d == null) {
        //for the cases where advance
        g.drawString(iterator, x, y);
        //matters it should not happen
        retVal = x;
    } else {
        FontRenderContext frc;
        if (isPrinting) {
            frc = getFontRenderContext(c);
            if (frc.isAntiAliased() || frc.usesFractionalMetrics()) {
                frc = new FontRenderContext(frc.getTransform(), false, false);
            }
        } else if ((frc = getFRCProperty(c)) != null) {
        /* frc = frc; ! */
        } else {
            frc = g2d.getFontRenderContext();
        }
        TextLayout layout;
        if (isPrinting) {
            FontRenderContext deviceFRC = g2d.getFontRenderContext();
            if (!isFontRenderContextPrintCompatible(frc, deviceFRC)) {
                layout = new TextLayout(iterator, deviceFRC);
                AttributedCharacterIterator trimmedIt = getTrimmedTrailingSpacesIterator(iterator);
                if (trimmedIt != null) {
                    float screenWidth = new TextLayout(trimmedIt, frc).getAdvance();
                    layout = layout.getJustifiedLayout(screenWidth);
                }
            } else {
                layout = new TextLayout(iterator, frc);
            }
        } else {
            layout = new TextLayout(iterator, frc);
        }
        layout.draw(g2d, x, y);
        retVal = layout.getAdvance();
    }
    if (isPrinting) {
        g.setColor(col);
    }
    return retVal;
}
Also used : PrintColorUIResource(sun.swing.PrintColorUIResource) AttributedCharacterIterator(java.text.AttributedCharacterIterator)

Example 2 with PrintColorUIResource

use of sun.swing.PrintColorUIResource in project jdk8u_jdk by JetBrains.

the class SwingUtilities2 method drawChars.

/**
     * The following draw functions have the same semantic as the
     * Graphics methods with the same names.
     *
     * this is used for printing
     */
public static int drawChars(JComponent c, Graphics g, char[] data, int offset, int length, int x, int y) {
    if (length <= 0) {
        //no need to paint empty strings
        return x;
    }
    int nextX = x + getFontMetrics(c, g).charsWidth(data, offset, length);
    if (isPrinting(g)) {
        Graphics2D g2d = getGraphics2D(g);
        if (g2d != null) {
            FontRenderContext deviceFontRenderContext = g2d.getFontRenderContext();
            FontRenderContext frc = getFontRenderContext(c);
            if (frc != null && !isFontRenderContextPrintCompatible(deviceFontRenderContext, frc)) {
                String text = new String(data, offset, length);
                TextLayout layout = new TextLayout(text, g2d.getFont(), deviceFontRenderContext);
                String trimmedText = trimTrailingSpaces(text);
                if (!trimmedText.isEmpty()) {
                    float screenWidth = (float) g2d.getFont().getStringBounds(trimmedText, frc).getWidth();
                    layout = layout.getJustifiedLayout(screenWidth);
                    /* Use alternate print color if specified */
                    Color col = g2d.getColor();
                    if (col instanceof PrintColorUIResource) {
                        g2d.setColor(((PrintColorUIResource) col).getPrintColor());
                    }
                    layout.draw(g2d, x, y);
                    g2d.setColor(col);
                }
                return nextX;
            }
        }
    }
    // Assume we're not printing if we get here, or that we are invoked
    // via Swing text printing which is laid out for the printer.
    AATextInfo info = drawTextAntialiased(c);
    if (info != null && (g instanceof Graphics2D)) {
        Graphics2D g2 = (Graphics2D) g;
        Object oldContrast = null;
        Object oldAAValue = g2.getRenderingHint(KEY_TEXT_ANTIALIASING);
        if (info.aaHint != null && info.aaHint != oldAAValue) {
            g2.setRenderingHint(KEY_TEXT_ANTIALIASING, info.aaHint);
        } else {
            oldAAValue = null;
        }
        if (info.lcdContrastHint != null) {
            oldContrast = g2.getRenderingHint(KEY_TEXT_LCD_CONTRAST);
            if (info.lcdContrastHint.equals(oldContrast)) {
                oldContrast = null;
            } else {
                g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST, info.lcdContrastHint);
            }
        }
        g.drawChars(data, offset, length, x, y);
        if (oldAAValue != null) {
            g2.setRenderingHint(KEY_TEXT_ANTIALIASING, oldAAValue);
        }
        if (oldContrast != null) {
            g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST, oldContrast);
        }
    } else {
        g.drawChars(data, offset, length, x, y);
    }
    return nextX;
}
Also used : AttributedString(java.text.AttributedString) PrintColorUIResource(sun.swing.PrintColorUIResource)

Example 3 with PrintColorUIResource

use of sun.swing.PrintColorUIResource in project jdk8u_jdk by JetBrains.

the class SwingUtilities2 method drawString.

/**
     * Draws the string at the specified location.
     *
     * @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 x X coordinate to draw the text at
     * @param y Y coordinate to draw the text at
     */
public static void drawString(JComponent c, Graphics g, String text, int x, int y) {
    // but NOT JTextComponents.
    if (text == null || text.length() <= 0) {
        //no need to paint empty strings
        return;
    }
    if (isPrinting(g)) {
        Graphics2D g2d = getGraphics2D(g);
        if (g2d != null) {
            /* The printed text must scale linearly with the UI.
                 * Calculate the width on screen, obtain a TextLayout with
                 * advances for the printer graphics FRC, and then justify
                 * it to fit in the screen width. This distributes the spacing
                 * more evenly than directly laying out to the screen advances.
                 */
            String trimmedText = trimTrailingSpaces(text);
            if (!trimmedText.isEmpty()) {
                float screenWidth = (float) g2d.getFont().getStringBounds(trimmedText, DEFAULT_FRC).getWidth();
                TextLayout layout = createTextLayout(c, text, g2d.getFont(), g2d.getFontRenderContext());
                layout = layout.getJustifiedLayout(screenWidth);
                /* Use alternate print color if specified */
                Color col = g2d.getColor();
                if (col instanceof PrintColorUIResource) {
                    g2d.setColor(((PrintColorUIResource) col).getPrintColor());
                }
                layout.draw(g2d, x, y);
                g2d.setColor(col);
            }
            return;
        }
    }
    // If we get here we're not printing
    if (g instanceof Graphics2D) {
        AATextInfo info = drawTextAntialiased(c);
        Graphics2D g2 = (Graphics2D) g;
        boolean needsTextLayout = ((c != null) && (c.getClientProperty(TextAttribute.NUMERIC_SHAPING) != null));
        if (needsTextLayout) {
            synchronized (charsBufferLock) {
                int length = syncCharsBuffer(text);
                needsTextLayout = isComplexLayout(charsBuffer, 0, length);
            }
        }
        if (info != null) {
            Object oldContrast = null;
            Object oldAAValue = g2.getRenderingHint(KEY_TEXT_ANTIALIASING);
            if (info.aaHint != oldAAValue) {
                g2.setRenderingHint(KEY_TEXT_ANTIALIASING, info.aaHint);
            } else {
                oldAAValue = null;
            }
            if (info.lcdContrastHint != null) {
                oldContrast = g2.getRenderingHint(KEY_TEXT_LCD_CONTRAST);
                if (info.lcdContrastHint.equals(oldContrast)) {
                    oldContrast = null;
                } else {
                    g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST, info.lcdContrastHint);
                }
            }
            if (needsTextLayout) {
                TextLayout layout = createTextLayout(c, text, g2.getFont(), g2.getFontRenderContext());
                layout.draw(g2, x, y);
            } else {
                g.drawString(text, x, y);
            }
            if (oldAAValue != null) {
                g2.setRenderingHint(KEY_TEXT_ANTIALIASING, oldAAValue);
            }
            if (oldContrast != null) {
                g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST, oldContrast);
            }
            return;
        }
        if (needsTextLayout) {
            TextLayout layout = createTextLayout(c, text, g2.getFont(), g2.getFontRenderContext());
            layout.draw(g2, x, y);
            return;
        }
    }
    g.drawString(text, x, y);
}
Also used : AttributedString(java.text.AttributedString) PrintColorUIResource(sun.swing.PrintColorUIResource)

Aggregations

PrintColorUIResource (sun.swing.PrintColorUIResource)3 AttributedString (java.text.AttributedString)2 AttributedCharacterIterator (java.text.AttributedCharacterIterator)1