Search in sources :

Example 1 with CharsetString

use of sun.awt.CharsetString in project jdk8u_jdk by JetBrains.

the class PSPrinterJob method textOut.

protected boolean textOut(Graphics g, String str, float x, float y, Font mLastFont, FontRenderContext frc, float width) {
    boolean didText = true;
    if (mFontProps == null) {
        return false;
    } else {
        prepDrawing();
        /* On-screen drawString renders most control chars as the missing
             * glyph and have the non-zero advance of that glyph.
             * Exceptions are \t, \n and \r which are considered zero-width.
             * Postscript handles control chars mostly as a missing glyph.
             * But we use 'ashow' specifying a width for the string which
             * assumes zero-width for those three exceptions, and Postscript
             * tries to squeeze the extra char in, with the result that the
             * glyphs look compressed or even overlap.
             * So exclude those control chars from the string sent to PS.
             */
        str = removeControlChars(str);
        if (str.length() == 0) {
            return true;
        }
        CharsetString[] acs = ((PlatformFont) (mLastFont.getPeer())).makeMultiCharsetString(str, false);
        if (acs == null) {
            /* AWT can't convert all chars so use 2D path */
            return false;
        }
        /* Get an array of indices into our PostScript name
             * table. If all of the runs can not be converted
             * to PostScript fonts then null is returned and
             * we'll want to fall back to printing the text
             * as shapes.
             */
        int[] psFonts = getPSFontIndexArray(mLastFont, acs);
        if (psFonts != null) {
            for (int i = 0; i < acs.length; i++) {
                CharsetString cs = acs[i];
                CharsetEncoder fontCS = cs.fontDescriptor.encoder;
                StringBuffer nativeStr = new StringBuffer();
                byte[] strSeg = new byte[cs.length * 2];
                int len = 0;
                try {
                    ByteBuffer bb = ByteBuffer.wrap(strSeg);
                    fontCS.encode(CharBuffer.wrap(cs.charsetChars, cs.offset, cs.length), bb, true);
                    bb.flip();
                    len = bb.limit();
                } catch (IllegalStateException xx) {
                    continue;
                } catch (CoderMalfunctionError xx) {
                    continue;
                }
                /* The width to fit to may either be specified,
                     * or calculated. Specifying by the caller is only
                     * valid if the text does not need to be decomposed
                     * into multiple calls.
                     */
                float desiredWidth;
                if (acs.length == 1 && width != 0f) {
                    desiredWidth = width;
                } else {
                    Rectangle2D r2d = mLastFont.getStringBounds(cs.charsetChars, cs.offset, cs.offset + cs.length, frc);
                    desiredWidth = (float) r2d.getWidth();
                }
                /* unprintable chars had width of 0, causing a PS error
                     */
                if (desiredWidth == 0) {
                    return didText;
                }
                nativeStr.append('<');
                for (int j = 0; j < len; j++) {
                    byte b = strSeg[j];
                    // to avoid encoding conversion with println()
                    String hexS = Integer.toHexString(b);
                    int length = hexS.length();
                    if (length > 2) {
                        hexS = hexS.substring(length - 2, length);
                    } else if (length == 1) {
                        hexS = "0" + hexS;
                    } else if (length == 0) {
                        hexS = "00";
                    }
                    nativeStr.append(hexS);
                }
                nativeStr.append('>');
                /* This comment costs too much in output file size */
                //                  mPSStream.println("% Font[" + mLastFont.getName() + ", " +
                //                             FontConfiguration.getStyleString(mLastFont.getStyle()) + ", "
                //                             + mLastFont.getSize2D() + "]");
                getGState().emitPSFont(psFonts[i], mLastFont.getSize2D());
                // out String
                mPSStream.println(nativeStr.toString() + " " + desiredWidth + " " + x + " " + y + " " + DrawStringName);
                x += desiredWidth;
            }
        } else {
            didText = false;
        }
    }
    return didText;
}
Also used : PlatformFont(sun.awt.PlatformFont) Rectangle2D(java.awt.geom.Rectangle2D) CharsetString(sun.awt.CharsetString) ByteBuffer(java.nio.ByteBuffer) CharsetString(sun.awt.CharsetString)

Example 2 with CharsetString

use of sun.awt.CharsetString in project jdk8u_jdk by JetBrains.

the class PSPrinterJob method getPSFontIndexArray.

/**
     * Given an array of CharsetStrings that make up a run
     * of text, this routine converts each CharsetString to
     * an index into our PostScript font list. If one or more
     * CharsetStrings can not be represented by a PostScript
     * font, then this routine will return a null array.
     */
private int[] getPSFontIndexArray(Font font, CharsetString[] charSet) {
    int[] psFont = null;
    if (mFontProps != null) {
        psFont = new int[charSet.length];
    }
    for (int i = 0; i < charSet.length && psFont != null; i++) {
        /* Get the encoding of the run of text.
             */
        CharsetString cs = charSet[i];
        CharsetEncoder fontCS = cs.fontDescriptor.encoder;
        String charsetName = cs.fontDescriptor.getFontCharsetName();
        if ("Symbol".equals(charsetName)) {
            charsetName = "symbol";
        } else if ("WingDings".equals(charsetName) || "X11Dingbats".equals(charsetName)) {
            charsetName = "dingbats";
        } else {
            charsetName = makeCharsetName(charsetName, cs.charsetChars);
        }
        int styleMask = font.getStyle() | FontUtilities.getFont2D(font).getStyle();
        String style = FontConfiguration.getStyleString(styleMask);
        /* First we map the font name through the properties file.
             * This mapping provides alias names for fonts, for example,
             * "timesroman" is mapped to "serif".
             */
        String fontName = font.getFamily().toLowerCase(Locale.ENGLISH);
        fontName = fontName.replace(' ', '_');
        String name = mFontProps.getProperty(fontName, "");
        /* Now map the alias name, character set name, and style
             * to a PostScript name.
             */
        String psName = mFontProps.getProperty(name + "." + charsetName + "." + style, null);
        if (psName != null) {
            /* Get the PostScript font index for the PostScript font.
                 */
            try {
                psFont[i] = Integer.parseInt(mFontProps.getProperty(psName));
            /* If there is no PostScript font for this font name,
                 * then we want to termintate the loop and the method
                 * indicating our failure. Setting the array to null
                 * is used to indicate these failures.
                 */
            } catch (NumberFormatException e) {
                psFont = null;
            }
        /* There was no PostScript name for the font, character set,
             * and style so give up.
             */
        } else {
            psFont = null;
        }
    }
    return psFont;
}
Also used : CharsetString(sun.awt.CharsetString) CharsetString(sun.awt.CharsetString)

Aggregations

CharsetString (sun.awt.CharsetString)2 Rectangle2D (java.awt.geom.Rectangle2D)1 ByteBuffer (java.nio.ByteBuffer)1 PlatformFont (sun.awt.PlatformFont)1