Example #1
0
  protected void findFonts(String path) {
    File file = new File(path);

    if (!file.exists()) {
      throw new RuntimeException("Font or dir not found: " + file);
    }

    if (file.isDirectory()) {
      File[] children = file.listFiles();
      if (children != null) {
        for (int i = 0; i < children.length; i++) {
          File child = children[i];
          if (child.isDirectory() || child.toString().toLowerCase().endsWith(".ttf")) {
            findFonts(child.toString());
          }
        }
      }
    } else {
      Font font = null;
      String err = null;
      try {
        font = Font.create(file.toString());
      } catch (Exception e) {
        err = e.toString();
      }

      if (font == null || font.getOS2Table() == null || font.getNameTable() == null) {
        System.err.println("Error reading " + file + ": " + err);
      } else {
        Os2Table os2Table = font.getOS2Table();
        int fsType = os2Table.getLicenseType();

        NameTable name = font.getNameTable();
        String copyright = name.getRecord(Table.nameCopyrightNotice);
        String trademark = name.getRecord(Table.nameTrademark);

        String postScriptName = name.getRecord(Table.namePostscriptName);
        LocalFont localFont = new LocalFont(postScriptName, path, fsType, copyright, trademark);
        LocalFont cachedFont = fonts.get(postScriptName);

        if (cachedFont != null) {
          if (!localFont.equals(cachedFont)) {
            // FIXME: localize if we're keeping this class
            System.out.println(
                "Found different fonts with the same postscript name. Keeping font "
                    + postScriptName
                    + " found at  "
                    + cachedFont.path
                    + " and ignoring "
                    + localFont.path);
          }
        } else {
          fonts.put(postScriptName, localFont);

          if (printForMap) {
            System.out.println(
                "defaultLocalFonts.put(\""
                    + postScriptName
                    + "\", new LocalFont(\""
                    + postScriptName
                    + "\", null, "
                    + fsType
                    + ", \""
                    + copyright
                    + "\", \""
                    + trademark
                    + "\"));");
          }
        }
      }
    }
  }