Example usage for com.lowagie.text.pdf FontMapper pdfToAwt

List of usage examples for com.lowagie.text.pdf FontMapper pdfToAwt

Introduction

In this page you can find the example usage for com.lowagie.text.pdf FontMapper pdfToAwt.

Prototype


public Font pdfToAwt(BaseFont font, int size);

Source Link

Document

Returns an AWT Font which can be used to represent the given BaseFont

Usage

From source file:com.jaspersoft.ireport.designer.fonts.TTFFontsLoader.java

License:Open Source License

public static void addFontNames(List<IRFont> fonts, String path_str, TTFFontsLoaderMonitor monitor) {
    java.text.MessageFormat formatter = new java.text.MessageFormat("Loaded font {0}");

    java.io.File file = new java.io.File(path_str);
    if (!file.exists()) {

        return;//from w  w w  . j  a v  a2  s  . c  o  m
    }
    if (file.isDirectory()) {
        String[] files = file.list(new java.io.FilenameFilter() {
            public boolean accept(java.io.File dir, String filename) {
                // modified                             
                //                            return filename.toUpperCase().endsWith(".TTF") ;
                return filename.toUpperCase().endsWith(".TTF") || filename.toUpperCase().endsWith(".TTC");
            }
        });

        //added begin 
        com.lowagie.text.pdf.FontMapper fontMapper = new com.lowagie.text.pdf.DefaultFontMapper();

        if (files == null) {
            System.out.println(Misc.formatString("Unable to list files in: {0}", new Object[] { "" + file }));
            return;
        }
        for (int i = 0; i < files.length; ++i) {
            try {
                if (files[i].toUpperCase().endsWith(".TTC")) {
                    try {
                        String[] names = com.lowagie.text.pdf.BaseFont
                                .enumerateTTCNames(file.getPath() + File.separator + files[i]);
                        for (int a = 0; a < names.length; a++) {

                            java.awt.Font f = fontMapper.pdfToAwt(
                                    com.lowagie.text.FontFactory
                                            .getFont(file.getPath() + File.separator + files[i]).getBaseFont(),
                                    10);
                            if (f != null) {
                                fonts.add(new IRFont(f, files[i]));
                            } else {
                                System.out.println(Misc.formatString("Failed to load font {0}",
                                        new Object[] { "" + file.getPath() + File.separator + files[i] }));
                            }

                            if (monitor != null) {
                                monitor.fontsLoadingStatusUpdated(formatter
                                        .format(new Object[] { file.getPath() + File.separator + files[i] }));
                            }
                        }
                    } catch (com.lowagie.text.DocumentException de) {
                        System.out.println(de);
                    } catch (java.io.IOException ioe) {
                        System.out.println(ioe);
                    }
                } else {
                    //added end

                    java.awt.Font f = loadFont(file.getPath() + File.separator + files[i]);
                    if (f != null) {
                        fonts.add(new IRFont(f, files[i]));
                        //System.out.println(""+ f.getFamily() + " " + f.getFontName() +" ("+file.getPath() + file.separator +files[i]+")");
                    } else {
                        System.out.println(Misc.formatString("Failed to load font {0}",
                                new Object[] { "" + file.getPath() + File.separator + files[i] }));
                    }

                    if (monitor != null) {
                        monitor.fontsLoadingStatusUpdated(
                                formatter.format(new Object[] { file.getPath() + File.separator + files[i] }));
                    }
                }

            } catch (Exception ex) {

                System.out.println(
                        Misc.formatString("Invalid font found: {0}. Font skipped.", new Object[] { files[i] })); //"fontListLoader.invalidFont"
            }

        }
    } else if (path_str.toUpperCase().endsWith(".TTF")) {
        // Try to load this file...
        //System.out.println(""+ path_str);
    } else if (path_str.toUpperCase().endsWith(".TTC")) {
        // Try to load this file...
        //System.out.println("TTC: "+ path_str);
    }
}

From source file:it.businesslogic.ireport.FontListLoader.java

License:Open Source License

public static void addFont(Vector fonts, String path_str, FontsLoaderMonitor monitor) {
    java.text.MessageFormat formatter = new java.text.MessageFormat(
            I18n.getString("fontsLoader.loadedFont", "Loaded font {0}"), I18n.getCurrentLocale());

    java.io.File file = new java.io.File(path_str);
    if (!file.exists()) {

        return;/*from w ww .ja  v  a2  s  .c  o  m*/
    }
    if (file.isDirectory()) {
        String[] files = file.list(new java.io.FilenameFilter() {
            public boolean accept(java.io.File dir, String filename) {
                // modified                             
                //                            return filename.toUpperCase().endsWith(".TTF") ;
                return filename.toUpperCase().endsWith(".TTF") || filename.toUpperCase().endsWith(".TTC");
            }
        });

        //added begin 
        com.lowagie.text.pdf.FontMapper fontMapper = new com.lowagie.text.pdf.DefaultFontMapper();

        if (files == null) {
            MainFrame.getMainInstance().logOnConsole(I18n.getFormattedString("fontListLoader.unableToListFiles",
                    "Unable to list files in: {0}", new Object[] { "" + file }));
            return;
        }
        for (int i = 0; i < files.length; ++i) {
            if (files[i].toUpperCase().endsWith(".TTC")) {
                try {
                    String[] names = com.lowagie.text.pdf.BaseFont
                            .enumerateTTCNames(file.getPath() + file.separator + files[i]);
                    for (int a = 0; a < names.length; a++) {

                        java.awt.Font f = fontMapper.pdfToAwt(com.lowagie.text.FontFactory
                                .getFont(file.getPath() + file.separator + files[i]).getBaseFont(), 10);
                        if (f != null) {
                            fonts.addElement(new IRFont(f, files[i]));
                        } else {
                            MainFrame.getMainInstance()
                                    .logOnConsole(I18n.getFormattedString("fontListLoader.failedLoadingFont",
                                            "Failed to load font {0}",
                                            new Object[] { "" + file.getPath() + file.separator + files[i] }));
                        }

                        if (monitor != null) {
                            monitor.fontsLoadingStatusUpdated(formatter
                                    .format(new Object[] { file.getPath() + file.separator + files[i] }));
                        }
                    }
                } catch (com.lowagie.text.DocumentException de) {
                    System.out.println(de);
                } catch (java.io.IOException ioe) {
                    System.out.println(ioe);
                }
            } else {
                //added end

                java.awt.Font f = loadFont(file.getPath() + file.separator + files[i]);
                if (f != null) {
                    fonts.addElement(new IRFont(f, files[i]));
                    //System.out.println(""+ f.getFamily() + " " + f.getFontName() +" ("+file.getPath() + file.separator +files[i]+")");
                } else {
                    MainFrame.getMainInstance()
                            .logOnConsole(I18n.getFormattedString("fontListLoader.failedLoadingFont",
                                    "Failed to load font {0}",
                                    new Object[] { "" + file.getPath() + file.separator + files[i] }));
                }

                if (monitor != null) {
                    monitor.fontsLoadingStatusUpdated(
                            formatter.format(new Object[] { file.getPath() + file.separator + files[i] }));
                }
            }

        }
    } else if (path_str.toUpperCase().endsWith(".TTF")) {
        // Try to load this file...
        System.out.println("m" + path_str);
    } else if (path_str.toUpperCase().endsWith(".TTC")) {
        // Try to load this file...
        System.out.println("TTC: " + path_str);
    }
}