Example usage for com.itextpdf.text Font getSize

List of usage examples for com.itextpdf.text Font getSize

Introduction

In this page you can find the example usage for com.itextpdf.text Font getSize.

Prototype

public float getSize() 

Source Link

Document

Gets the size of this font.

Usage

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

License:Apache License

public static boolean isTextTooWide(String text, Font f, float width) {
    return f.getBaseFont().getWidthPoint(text, f.getSize()) > width;
}

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

License:Apache License

public static float renderConstrainedText(PdfContentByte content, String text, Font f, float x, float y,
        int width) {
    float offset = 0.0f;
    if (text == null || text.isEmpty()) {
        return offset;
    }/*from w  w w .j a  v  a  2s .  c o  m*/
    float w = PdfUtil.convertFromMillimeters(width) * .95f;

    String prevStr = "";
    text = text.replace("\\n", "\n");
    for (String v : text.split("\n")) {
        String str = "";
        for (String s : v.split(" ")) {
            str += ((!str.isEmpty()) ? " " : "") + s;
            if (isTextTooWide(str, f, w)) {
                improveRenderText(content, prevStr, f, x, y + offset);
                offset -= f.getSize() + 1;
                prevStr = s;
                str = s;
                continue;
            }
            prevStr = str;
        }
        if (!str.isEmpty()) {
            improveRenderText(content, prevStr, f, x, y + offset);
            offset -= f.getSize() + 1;
        }
    }

    return offset;
}

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();
    }//  w w  w  .j a 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();

}

From source file:org.javad.stamp.pdf.ColumnSet.java

License:Apache License

@Override
public OutputBounds generate(PdfContentByte content) {
    if (isSkipped()) {
        return new OutputBounds(getX(), getY(), 0, 0);
    }// w  w w . ja  va  2  s.  c om
    float totalWidth = 0.0f;
    float maxHeight = 0.0f;
    float cur_x = getX();
    float top = getY();
    if (getIssue() != null && !getIssue().isEmpty()) {
        top -= PdfUtil.convertFromMillimeters(5.0f);
        Font f = FontRegistry.getInstance().getFont(PdfFontDefinition.SetIssue);

        content.setFontAndSize(f.getBaseFont(), f.getSize());
        String is = getIssue().replace("\\n", "\n");
        StringTokenizer tokenizer = new StringTokenizer(is, "\n", true);
        float x = PdfUtil
                .convertFromMillimeters(configuration.getUsableWidth() / 2 + configuration.getMarginLeft());
        while (tokenizer.hasMoreTokens()) {
            is = tokenizer.nextToken();
            if (is.equals("\n")) {
                top -= f.getCalculatedSize() + 2;
            } else {
                PdfUtil.renderConstrainedText(content, is, f, x, top,
                        (int) PdfUtil.convertFromMillimeters(configuration.getUsableWidth()));
                if (tokenizer.hasMoreTokens()) {
                    top -= f.getCalculatedSize() + 2;
                }
            }
        }

        top -= PdfUtil.convertFromMillimeters(3.0f);
    }
    float spacing = PdfUtil.convertFromMillimeters(getSpacingMode().getSpacing(box_spacing));
    int count = 0;
    for (int i = 0; i < columns.size(); i++) {
        StampSet set = columns.get(i);
        if (set.isSkipped()) {
            continue;
        }
        float s_w = PdfUtil.findMaximumWidth(set, content);
        set.setX(cur_x + s_w / 2);
        set.setY(top);
        OutputBounds rect = set.generate(content);

        totalWidth += rect.width + ((count > 0) ? spacing : 0.0f);
        cur_x += rect.width + spacing;
        maxHeight = Math.max(maxHeight, rect.height);
        count++;
    }
    return new OutputBounds(getX() - (totalWidth / 2.0f), getY(), totalWidth, maxHeight + (getY() - top));
}

From source file:org.javad.stamp.pdf.CompositeRow.java

License:Apache License

@Override
public OutputBounds generate(PdfContentByte content) {
    if (isSkipped()) {
        return new OutputBounds(getX(), getY(), 0, 0);
    }/* w w  w  .ja  va  2 s . c o  m*/
    float maxHeight = 0.0f;
    float maxWidth = 0.0f;
    float top = getY();

    if (getDescription() != null && !getDescription().isEmpty()) {
        content.setColorStroke(BaseColor.BLACK);
        Font descFont = FontRegistry.getInstance().getFont(PdfFontDefinition.CompositeSetDescription);
        content.setFontAndSize(descFont.getBaseFont(), descFont.getSize());

        top -= descFont.getCalculatedSize();
        int count = 0;
        int tc = getDescription().split("\n").length;
        for (String desc : getDescription().split("\n")) {
            maxWidth = Math.max(maxWidth, content.getEffectiveStringWidth(desc, false));
            PdfUtil.renderConstrainedText(content, desc, descFont, getX(), top, (int) maxWidth);
            count++;
            top -= descFont.getCalculatedSize() + ((count < tc) ? 2 : 4);
        }

    }

    float totalWidth = 0.0f;
    float totalHeight = getY() - top;
    float spacing = PdfUtil.convertFromMillimeters(getSpacingMode().getSpacing(box_spacing));
    int count = 0;
    for (int i = 0; i < rows.size(); i++) {
        StampRow set = rows.get(i);
        if (set.isSkipped()) {
            continue;
        }
        totalWidth += PdfUtil.findMaximumWidth(set, content) + ((count > 0) ? spacing : 0.0f);
        count++;
    }
    float cur_x = getX() - totalWidth / 2.0f;
    count = 0;
    for (StampRow set : rows) {
        if (set.isSkipped()) {
            continue;
        }
        set.setX(cur_x + PdfUtil.findMaximumWidth(set, content) / 2.0f);
        set.setY(top);
        OutputBounds rect = set.generate(content);
        count++;
        cur_x += rect.width + ((count > 0) ? spacing : 0);
        maxHeight = Math.max(maxHeight, rect.height);

    }
    maxWidth = Math.max(maxWidth, totalWidth);
    totalHeight += maxHeight;
    return new OutputBounds(getX() - (maxWidth / 2.0f), getY(), maxWidth, totalHeight);
}

From source file:org.javad.stamp.pdf.StampBox.java

License:Apache License

@Override
public OutputBounds generate(PdfContentByte content) {
    if (isSkipped()) {
        return new OutputBounds(getX(), getY(), 0, 0);
    }/*from   w w w .j  av  a2  s .c  o  m*/
    OutputBounds rect = new OutputBounds(getX(), getY(),
            PdfUtil.convertFromMillimeters(getWidth() + getPadding()),
            PdfUtil.convertFromMillimeters(getHeight() + getVerticalPadding()));
    if (bisect != Bisect.none && isBorder()) {
        drawBisect(content, rect);
    }
    if (getSetTenantPosition() == null) {
        drawShape(content, rect);
    }

    float verticalPadding = PdfUtil.convertFromMillimeters(getVerticalPadding());
    float w = rect.width;
    Font f = FontRegistry.getInstance().getFont(PdfFontDefinition.Stampbox);
    float center = getX() + w / 2;
    float top = getY() + verticalPadding + f.getCalculatedSize() * 6
            + PdfUtil.convertFromMillimeters(getTextPadding());

    if (image != null) {
        try {
            com.itextpdf.text.Image img = determineScaledImage(rect, f.getCalculatedSize(), top, isImageOnly());
            if (img != null) {
                content.addImage(img);
            }
        } catch (Exception e) {
            logger.log(Level.FINER, "An error occured scaling the image. ", e);
        }
    }
    if (!isImageOnly()) {
        if (shape == Shape.diamond || shape == Shape.triangleInverted) {
            top = getY() + rect.height / 2.0f + f.getCalculatedSize() * 1.5f;
        }
        top += PdfUtil.renderConstrainedText(content, getDenomination(), f, center, top, getWidth());
        top -= f.getCalculatedSize() + 1;
        float d_delta = PdfUtil.renderConstrainedText(content, getDescription(), f, center, top, getWidth());
        float rows = (float) Math.ceil(d_delta / f.getCalculatedSize());
        top += d_delta;
        Font f2 = FontRegistry.getInstance().getFont(PdfFontDefinition.StampboxOther);
        float delta = PdfUtil.renderConstrainedText(content, getDescriptionSecondary(), f2, center, top,
                getWidth());
        if (delta < 1.0f && rows > -3.00) {
            delta = -1 * (f.getSize() + 1);
        }
        if (d_delta >= -1 * (f.getSize() + 1)) { // handle one line descriptions
            top -= f.getSize() + 1;
        }
        top += delta;
        PdfUtil.renderConstrainedText(content, getCatalogueNumber(), f, center, top, getWidth());
    }
    return rect;
}

From source file:org.javad.stamp.pdf.StampRow.java

License:Apache License

@Override
public OutputBounds generate(PdfContentByte content) {
    if (isSkipped()) {
        return new OutputBounds(getX(), getY(), 0, 0);
    }/*from  w  ww.ja v a  2  s.  co m*/
    float maxWidth = 0;
    float top = getY();
    if (getDescription() != null && !getDescription().isEmpty()) {
        content.setColorStroke(BaseColor.BLACK);
        Font descFont = FontRegistry.getInstance().getFont(PdfFontDefinition.RowDescription);
        content.setFontAndSize(descFont.getBaseFont(), descFont.getSize());
        top -= descFont.getCalculatedSize();
        int count = 0;
        int tc = getDescription().split("\n").length;
        for (String desc : getDescription().split("\n")) {
            maxWidth = Math.max(maxWidth, (int) descFont.getBaseFont().getWidthPoint(desc, descFont.getSize()));
            PdfUtil.renderConstrainedText(content, desc, descFont, getX(), top, (int) (maxWidth * 1.10));
            count++;
            top -= descFont.getCalculatedSize() + ((count < tc) ? 2 : 4);

        }
    }
    float totalWidth = 0;
    int maxHeight = 0;
    int count = 0;
    for (int i = 0; i < stampContents.size(); i++) {
        if (stampContents.get(i).isSkipped()) {
            continue;
        }

        totalWidth += stampContents.get(i).getWidth() + stampContents.get(i).getPadding();
        if (count > 0) {
            totalWidth += padding;
        }
        count++;
        maxHeight = Math.max(maxHeight, stampContents.get(i).getHeight());
    }
    totalWidth = PdfUtil.convertFromMillimeters(totalWidth);
    float start_x = getX() - (totalWidth / 2.0f);
    float cur_x = start_x;
    float totalHeight = getY() - top;
    float deltaHeight = totalHeight;
    count = 0;
    for (IStampContent s : stampContents) {
        if (s.isSkipped()) {
            continue;
        }
        if (count > 0) {
            cur_x += PdfUtil.convertFromMillimeters(padding);
        }
        s.setX(cur_x);
        int delta_y = getVerticalAlignmentOffset(s, maxHeight);
        s.setY(top - PdfUtil.convertFromMillimeters(delta_y));
        OutputBounds r = s.generate(content);
        totalHeight = Math.max(r.height + deltaHeight, totalHeight);
        cur_x += r.width;
        count++;
    }
    maxWidth = (int) Math.max(maxWidth, totalWidth);
    return new OutputBounds(start_x, getY(), maxWidth, totalHeight);
}

From source file:org.javad.stamp.pdf.StampSet.java

License:Apache License

@Override
public OutputBounds generate(PdfContentByte content) {
    if (isSkipped()) {
        return new OutputBounds(getX(), getY(), 0, 0);
    }/*from w ww.j  a  v a 2  s  .  com*/
    float maxWidth = 0;
    content.setColorStroke(BaseColor.BLACK);

    float top = getY();
    if (getIssue() != null && !getIssue().isEmpty()) {
        top -= PdfUtil.convertFromMillimeters(5.0f);
        Font f = FontRegistry.getInstance().getFont(PdfFontDefinition.SetIssue);
        content.setFontAndSize(f.getBaseFont(), f.getSize());
        String is = getIssue().replace("\\n", "\n");
        StringTokenizer tokenizer = new StringTokenizer(is, "\n", true);
        while (tokenizer.hasMoreTokens()) {
            is = tokenizer.nextToken();
            maxWidth = Math.max(maxWidth, content.getEffectiveStringWidth(is, false));
            if (is.equals("\n")) {
                top -= f.getCalculatedSize() + 2;
            } else {
                PdfUtil.renderConstrainedText(content, is, f, getX(), top, (int) maxWidth);
                if (tokenizer.hasMoreTokens()) {
                    top -= f.getCalculatedSize() + 2;
                }
            }

        }
        top -= PdfUtil.convertFromMillimeters(3.0f);
    }
    if (getDescription() != null && !getDescription().isEmpty()) {
        Font descFont = FontRegistry.getInstance().getFont(PdfFontDefinition.SetDescription);
        content.setFontAndSize(descFont.getBaseFont(), descFont.getSize());
        top -= descFont.getCalculatedSize();
        int count = 0;
        int tc = getDescription().split("\n").length;
        for (String desc : getDescription().split("\n")) {
            maxWidth = Math.max(maxWidth, content.getEffectiveStringWidth(desc, false));
            PdfUtil.renderConstrainedText(content, desc, descFont, getX(), top, (int) maxWidth);
            count++;
            if (count < tc) {
                top -= descFont.getCalculatedSize() + 2;
            }

        }
    }
    if (getDescriptionSecondary() != null && !getDescriptionSecondary().isEmpty()) {
        Font secFont = FontRegistry.getInstance().getFont(PdfFontDefinition.SetDescriptionSecondary);
        content.setFontAndSize(secFont.getBaseFont(), secFont.getSize());
        top -= secFont.getCalculatedSize() + PdfUtil.convertFromMillimeters(3.0f);
        int count = 0;
        int tc = getDescriptionSecondary().split("\n").length;
        for (String desc : getDescriptionSecondary().split("\n")) {
            maxWidth = Math.max(maxWidth, content.getEffectiveStringWidth(desc, false));
            PdfUtil.renderConstrainedText(content, desc, secFont, getX(), top, (int) maxWidth);
            count++;
            if (count < tc) {
                top -= secFont.getCalculatedSize() + 2;
            }

        }
    }

    if (!rows.isEmpty()) {
        top -= ((top != getY()) ? PdfUtil.convertFromMillimeters(3.0f) : 0);
        int count = 0;
        for (int i = 0; i < rows.size(); i++) {
            ISetContent row = rows.get(i);
            if (row.isSkipped()) {
                continue;
            }
            top -= ((count > 0) ? (PdfUtil.convertFromMillimeters(verticalPadding)) : 0.0f);
            row.setX(getX());
            row.setY(top);
            OutputBounds bounds = row.generate(content);
            count++;
            top -= bounds.height;
            maxWidth = Math.max(maxWidth, bounds.width);
        }
    }
    if (getComment() != null && !getComment().isEmpty()) {

        top -= PdfUtil.convertFromMillimeters((int) (0.75 * verticalPadding));
        Font cFont = FontRegistry.getInstance().getFont(PdfFontDefinition.SetComment);
        content.setFontAndSize(cFont.getBaseFont(), cFont.getSize());
        for (String s : getComment().split("\n")) {
            top -= cFont.getCalculatedSize();
            maxWidth = Math.max(maxWidth, content.getEffectiveStringWidth(s, false));
            PdfUtil.renderConstrainedText(content, s, cFont, getX(), top, (int) maxWidth);
        }

    }

    OutputBounds rect = new OutputBounds(getX() - maxWidth / 2, getY(), maxWidth, getY() - top);
    return rect;
}