Example usage for com.itextpdf.text.pdf BaseFont charExists

List of usage examples for com.itextpdf.text.pdf BaseFont charExists

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf BaseFont charExists.

Prototype

public boolean charExists(int c) 

Source Link

Document

Checks if a character exists in this font.

Usage

From source file:org.javad.pdf.util.PdfUtil.java

License:Apache License

protected static void improveRenderText(PdfContentByte content, String str, Font f, float x, float y) {

    BaseFont bf = f.getCalculatedBaseFont(false);
    if (i18Font == null && (System.currentTimeMillis() - LAST_CHECK_TIME > 10000)) {
        i18Font = FontRegistry.getInstance().getFont(PdfFontDefinition.ExtendedCharacters)
                .getCalculatedBaseFont(false);
        LAST_CHECK_TIME = System.currentTimeMillis();
    }//from www  .ja v a  2 s. c o  m
    StringBuilder buf = new StringBuilder(str.length());
    boolean extendedCodePoint = false;
    float effWidth = 0.0f;
    for (int i = 0; i < str.length(); i++) {
        char c = str.charAt(i);
        int codePoint = Character.codePointAt("" + c, 0);
        boolean curCodePoint = (!bf.charExists(codePoint)) ? true : false;
        if (curCodePoint != extendedCodePoint) {
            content.setFontAndSize((extendedCodePoint) ? i18Font : bf, f.getSize());
            effWidth += content.getEffectiveStringWidth(buf.toString(), false);
            buf = new StringBuilder();
            extendedCodePoint = curCodePoint;
        }
        buf.append(c);
    }
    content.setFontAndSize(bf, f.getSize());
    effWidth += content.getEffectiveStringWidth(buf.toString(), false);
    float x_pos = x - (effWidth / 2.0f); // eq. to showTextAligned

    content.beginText();
    content.setTextMatrix(x_pos, y);
    BaseFont lastFont = bf;
    @SuppressWarnings("UnusedAssignment")
    BaseFont currentFont = lastFont;
    buf = new StringBuilder();
    for (int i = 0; i < str.length(); i++) {
        char c = str.charAt(i);
        int codePoint = Character.codePointAt("" + c, 0);
        if (!bf.charExists(codePoint)) {
            currentFont = i18Font;
        } else {
            currentFont = bf;
        }
        if (currentFont != lastFont) {
            content.showText(buf.toString());
            buf = new StringBuilder();
            content.setFontAndSize(currentFont, f.getSize());
            lastFont = currentFont;
        }
        buf.append(c);
    }
    if (buf.length() > 0) {
        content.showText(buf.toString());
    }
    content.endText();

}