Search in sources :

Example 1 with PlatformFont

use of sun.awt.PlatformFont 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)

Aggregations

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