Example usage for com.lowagie.text.pdf BaseFont IDENTITY_V

List of usage examples for com.lowagie.text.pdf BaseFont IDENTITY_V

Introduction

In this page you can find the example usage for com.lowagie.text.pdf BaseFont IDENTITY_V.

Prototype

String IDENTITY_V

To view the source code for com.lowagie.text.pdf BaseFont IDENTITY_V.

Click Source Link

Document

The Unicode encoding with vertical writing.

Usage

From source file:org.pentaho.reporting.libraries.fonts.itext.BaseFontSupport.java

License:Open Source License

/**
 * Creates a BaseFontRecord for an font.  If no basefont could be created, an BaseFontCreateException is thrown.
 *
 * @param logicalName the name of the font (null not permitted).
 * @param bold        a flag indicating whether the font is rendered as bold font.
 * @param italic      a flag indicating whether the font is rendered as italic or cursive font.
 * @param encoding    the encoding.//from   w w w. j  a v  a2s  . c o m
 * @param embedded    a flag indicating whether to embed the font glyphs in the generated documents.
 * @return the base font record.
 * @throws BaseFontCreateException if there was a problem setting the font for the target.
 */
public BaseFontRecord createBaseFontRecord(final String logicalName, final boolean bold, final boolean italic,
        String encoding, final boolean embedded) throws BaseFontCreateException {
    if (logicalName == null) {
        throw new NullPointerException("Font definition is null.");
    }
    if (encoding == null) {
        encoding = getDefaultEncoding();
    }

    // use the Java logical font name to map to a predefined iText font.

    final String fontKey;
    if (FontMappingUtility.isCourier(logicalName)) {
        fontKey = "Courier";
    } else if (FontMappingUtility.isSymbol(logicalName)) {
        fontKey = "Symbol";
    } else if (FontMappingUtility.isSerif(logicalName)) {
        fontKey = "Times";
    } else if (FontMappingUtility.isSansSerif(logicalName)) {
        // default, this catches Dialog and SansSerif
        fontKey = "Helvetica";
    } else {
        fontKey = logicalName;
    }

    // iText uses some weird mapping between IDENTY-H/V and java supported encoding, IDENTITY-H/V is
    // used to recognize TrueType fonts, but the real JavaEncoding is used to encode Type1 fonts
    final String stringEncoding;
    if ("utf-8".equalsIgnoreCase(encoding)) {
        stringEncoding = "utf-8";
        encoding = BaseFont.IDENTITY_H;
    } else if ("utf-16".equalsIgnoreCase(encoding)) {
        stringEncoding = "utf-16";
        encoding = BaseFont.IDENTITY_H;
    } else {
        // Correct the encoding for truetype fonts
        // iText will crash if IDENTITY_H is used to create a base font ...
        if (encoding.equalsIgnoreCase(BaseFont.IDENTITY_H) || encoding.equalsIgnoreCase(BaseFont.IDENTITY_V)) {
            //changed to UTF to support all unicode characters ..
            stringEncoding = "utf-8";
        } else {
            stringEncoding = encoding;
        }
    }

    try {
        final FontFamily registryFontFamily = registry.getFontFamily(fontKey);
        FontRecord registryFontRecord = null;
        if (registryFontFamily != null) {
            registryFontRecord = registryFontFamily.getFontRecord(bold, italic);

            if (registryFontRecord instanceof CompoundFontRecord) {
                final CompoundFontRecord cfr = (CompoundFontRecord) registryFontRecord;
                registryFontRecord = cfr.getBase();
            }
        }

        if (registryFontRecord != null) {
            // Check, whether this is an built-in font. If not, then the record points to a file.
            if ((registryFontRecord instanceof ITextBuiltInFontRecord) == false) {

                boolean embeddedOverride = embedded;
                if (embedded == true && registryFontRecord instanceof FontSource) {
                    final FontSource source = (FontSource) registryFontRecord;
                    if (source.isEmbeddable() == false) {
                        logger.warn("License of font forbids embedded usage for font: " + fontKey);
                        // strict mode here?
                        embeddedOverride = false;
                    }
                }

                final BaseFontRecord fontRecord = createFontFromTTF(registryFontRecord, bold, italic, encoding,
                        stringEncoding, embeddedOverride);
                if (fontRecord != null) {
                    return fontRecord;
                }
            } else {
                final ITextBuiltInFontRecord buildInFontRecord = (ITextBuiltInFontRecord) registryFontRecord;
                // So this is one of the built-in records.
                final String fontName = buildInFontRecord.getFullName();

                // Alternative: No Registered TrueType font was found. OK; don't panic,
                // we try to create a font anyway..

                BaseFontRecord fontRecord = getFromCache(fontName, encoding, embedded);
                if (fontRecord != null) {
                    return fontRecord;
                }
                fontRecord = getFromCache(fontName, stringEncoding, embedded);
                if (fontRecord != null) {
                    return fontRecord;
                }

                // filename is null, so no ttf file registered for the fontname, maybe this is
                // one of the internal fonts ...
                final BaseFont f = BaseFont.createFont(fontName, stringEncoding, embedded, useGlobalCache, null,
                        null);
                if (f != null) {
                    fontRecord = new BaseFontRecord(fontName, false, embedded, f, bold, italic);
                    putToCache(fontRecord);
                    return fontRecord;
                }
            }
        }

        // If we got to this point, then the font was not recognized as any known font. We will fall back
        // to Helvetica instead ..
    } catch (Exception e) {
        if (logger.isDebugEnabled()) {
            logger.debug("BaseFont.createFont failed. Key = " + fontKey + ": " + e.getMessage(), e);
        } else if (logger.isWarnEnabled()) {
            logger.warn("BaseFont.createFont failed. Key = " + fontKey + ": " + e.getMessage(), e);
        }
    }
    // fallback .. use BaseFont.HELVETICA as default
    try {
        // check, whether HELVETICA is already created - yes, then return cached instance instead
        BaseFontRecord fontRecord = getFromCache(BaseFont.HELVETICA, stringEncoding, embedded);
        if (fontRecord != null) {
            // map all font references of the invalid font to the default font..
            // this might be not very nice, but at least the report can go on..
            putToCache(new BaseFontRecordKey(fontKey, encoding, embedded), fontRecord);
            return fontRecord;
        }

        // no helvetica created, so do this now ...
        final BaseFont f = BaseFont.createFont(BaseFont.HELVETICA, stringEncoding, embedded, useGlobalCache,
                null, null);
        if (f != null) {
            fontRecord = new BaseFontRecord(BaseFont.HELVETICA, false, embedded, f, bold, italic);
            putToCache(fontRecord);
            putToCache(new BaseFontRecordKey(fontKey, encoding, embedded), fontRecord);
            return fontRecord;
        }
    } catch (Exception e) {
        logger.warn("BaseFont.createFont for FALLBACK failed.", e);
        throw new BaseFontCreateException("Null font = " + fontKey);
    }
    throw new BaseFontCreateException("BaseFont creation failed, null font: " + fontKey);
}