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

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

Introduction

In this page you can find the example usage for com.lowagie.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:com.gtdfree.test.PDFTestFont.java

License:Open Source License

/**
 * Fonts and encoding.//from  w  ww .j  a va2  s .c  om
 * @param args no arguments needed
 */
public static void main(String[] args) {

    System.out.println("Encodings");

    String[] names = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();

    System.out.println(Arrays.toString(names));

    /*System.out.println("---");
    try {
    System.getProperties().store(System.out, "");
    } catch (IOException e1) {
    e1.printStackTrace();
    }
    System.out.println("---");*/
    //System.out.println(System.getenv());
    //System.out.println("---");

    //String font= System.getProperty("java.home")+"/lib/fonts/LucidaBrightRegular.ttf";
    //String font= "fonts/DejaVuSans.ttf";

    //byte[] ttf= ApplicationHelper.loadResource(font);

    try {
        // step 1
        Document document = new Document(PageSize.A4, 50, 50, 50, 50);
        // step 2
        PdfWriter.getInstance(document, new FileOutputStream("encodingfont.pdf"));
        // step 3
        document.open();
        // step 4
        String all[] = { "Symbol", "ZapfDingbats" };
        Font hex = new Font(Font.HELVETICA, 5);
        for (int z = 0; z < all.length; ++z) {
            String file = all[z];
            document.add(new Paragraph(
                    "Unicode equivalence for the font \"" + file + "\" with the encoding \"" + file + "\"\n"));
            /*char tb[];
            if (z == 0)
               tb = SYMBOL_TABLE;
            else
               tb = DINGBATS_TABLE;*/
            BaseFont bf;
            if (z == 2) {
                bf = BaseFont.createFont(file, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, true, null, null);
                ;
            } else {
                bf = BaseFont.createFont(file, file, true);
            }
            Font f = new Font(bf, 12);
            PdfPTable table = new PdfPTable(16);
            table.setWidthPercentage(100);
            table.getDefaultCell().setBorderWidth(1);
            table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
            for (char k = 0; k < Character.MAX_VALUE; ++k) {
                char c = k;
                if (bf.charExists(c)) {
                    Phrase ph = new Phrase(12, new String(new char[] { c }), f);
                    ph.add(new Phrase(12, "\n" + Integer.toString(c) + "\n" + cst(c), hex));
                    table.addCell(ph);
                } /*
                  else {
                     Phrase ph = new Phrase("\u00a0");
                     ph.add(new Phrase(12, "\n\n" + cst(c), hex));
                     table.addCell(ph);
                  }*/
            }
            document.add(table);
            document.newPage();
        }
        // step 5
        document.close();

        FontFactory.registerDirectories();

        Set<?> s = FontFactory.getRegisteredFonts();

        System.out.println("Fonts: " + s);

        s = FontFactory.getRegisteredFamilies();

        System.out.println("Families: " + s);

        ArrayList<Font> f = new ArrayList<Font>(s.size());

        for (Object name : s) {

            try {
                f.add(FontFactory.getFont(name.toString(), "UTF-8", true, 12, Font.NORMAL, Color.BLACK, true));
            } catch (Exception e) {
                f.add(FontFactory.getFont(name.toString(), "UTF-8", false, 12, Font.NORMAL, Color.BLACK, true));
            }

        }

        Collections.sort(f, new Comparator<Font>() {
            @Override
            public int compare(Font o1, Font o2) {
                return o1.getFamilyname().compareTo(o2.getFamilyname());
            }
        });

        for (Font ff : f) {

            if (ff.getBaseFont() == null) {
                continue;
            }
            System.out.println(ff.getFamilyname() + " " + ff.getBaseFont().isEmbedded());

        }

    } catch (Exception de) {
        de.printStackTrace();
    }
}

From source file:org.eclipse.birt.report.engine.layout.pdf.font.FontCacheUtils.java

License:Open Source License

static void createFontIndex(String fontName, String encoding, Writer writer) throws Exception {
    BaseFont font = BaseFont.createFont(fontName, encoding, false);
    ArrayList charSegs = new ArrayList();
    int start = 0;
    int end = 0;//from w ww  .ja  va  2  s.  c o  m
    for (char ch = 0; ch < 0xFFFF; ch++) {
        if (font.charExists(ch)) {
            if (start == -1) {
                start = ch;
            }
            end = ch;
        } else {
            if (start != -1) {
                charSegs.add(new CharSegment(start, end, fontName));
                start = -1;
            }
        }
    }
    if (start != -1) {
        charSegs.add(new CharSegment(start, end, fontName));
    }
    for (int i = 0; i < charSegs.size(); i++) {
        StringBuffer sb = new StringBuffer();
        sb.append("<block region-start=\"").append(start).append("\" region-end=\"").append(end).append("\"/>")
                .append('\n');
        writer.write(sb.toString());
    }
}

From source file:org.eclipse.birt.report.engine.layout.pdf.font.FontHandler.java

License:Open Source License

/**
 * Selects a proper font for a character.
 * //ww  w  .  j  a  va  2s.  c o m
 * @return true: we find a font which can be used to display the character.
 *         false: no font can display the character.
 */
public boolean selectFont(char character) {
    assert (fontManager != null);
    // FIXME: code review : return null when no mapped font defined the
    // character so that charExist only need to be invoked once.
    BaseFont candidateFont = getMappedFont(character);
    assert (candidateFont != null);
    if (bf == candidateFont) {
        isFontChanged = false;
    } else {
        isFontChanged = true;
        bf = candidateFont;
        simulation = needSimulate(bf);
    }
    return candidateFont.charExists(character);
}

From source file:org.eclipse.birt.report.engine.layout.pdf.font.FontHandler.java

License:Open Source License

/**
 * Gets the BaseFont object to display the given character.
 * //from  ww w . ja v a  2  s .c  om
 * The search sequence is:
 * <li> try the font family defined in the families to see if one can be
 * used to display the character. </li>
 * <li> try to use the default font to display the character. </li>
 * <li> if none of the above success, return NULL for the character.</li>
 * 
 * @param c
 *            the given character.
 * @return the BaseFont. it always return a font.
 */
public BaseFont getMappedFont(char c) {
    if (WEAK_FONT_CHARS.indexOf(c) != -1) {
        if (bf != null && bf.charExists(c)) {
            return bf;
        }
    }
    // search in the font family to find one to display the character
    for (int i = 0; i < fontFamilies.length; i++) {
        // Translate the font alias to font family
        String fontFamily = fontManager.getAliasedFont(fontFamilies[i]);

        // test if it is a composite font
        CompositeFont cf = fontManager.getCompositeFont(fontFamily);
        if (cf != null) {
            // once it is created by the composite font, we needn't test if
            // the character can be displayed by the returned font as the
            // returned font is
            // either defined by the user or test through the base font.
            String usedFont = cf.getUsedFont(c);
            if (usedFont != null) {
                BaseFont bf = createBaseFont(usedFont);
                if (bf != null) {
                    return bf;
                }
            }
        } else {
            BaseFont bf = createBaseFont(fontFamily);
            if (bf != null && bf.charExists(c)) {
                return bf;
            }
        }
    }
    // Use the default font to display this character
    CompositeFont df = fontManager.getCompositeFont(FontMappingManager.FONT_NAME_ALL_FONTS);
    if (df != null) {
        String usedFont = df.getUsedFont(c);
        if (usedFont != null) {
            BaseFont bf = createBaseFont(usedFont);
            if (bf != null) {
                return bf;
            }
        }
    }
    // it's the last choice to use the default fonts
    BaseFont bf = createBaseFont(FontMappingManager.DEFAULT_FONT);
    if (bf == null) {
        throw new NullPointerException(
                "Failed to create " + FontMappingManager.DEFAULT_FONT + " which is not allow");
    }
    return bf;
}

From source file:textdisplay.TagFilter.java

License:Educational Community License

/**Apply a style (font) to a chunk of pdf text*/
private void styleChunk(Chunk c, styles s) {
    try {/* ww  w  .  ja  v a  2 s.  co  m*/

        BaseFont bf = BaseFont.createFont("/usr/Junicode.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        BaseFont ita = BaseFont.createFont("/usr/Junicode-Italic.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        BaseFont bol = BaseFont.createFont("/usr/Junicode-Italic.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        if (bf.charExists(540)) {
            System.out.print("font cant do 540\n");
        }
        Font italic = new Font(ita, 12, Font.ITALIC);
        Font bold = new Font(bol, 12, Font.BOLD);
        Font underlined = new Font(bf, 12, Font.UNDERLINE);
        Font superscript = new Font(bf, 9, Font.NORMAL);

        if (s == styles.bold) {
            c.setFont(bold);
        }
        if (s == styles.italic) {
            c.setFont(italic);
        }
        if (s == styles.underlined) {
            c.setFont(underlined);
        }
        if (s == styles.superscript) {
            c.setTextRise(7.0f);
            c.setFont(superscript);

        }

        //wipe out that content

    } catch (DocumentException ex) {
        Logger.getLogger(TagFilter.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(TagFilter.class.getName()).log(Level.SEVERE, null, ex);
    }
}