⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basefontfactory.java

📁 swing编写的库存管理程序。毕业设计类
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    Log.debug("Found windows in os name, assuming DOS/Win32 structures");
    // Assume windows
    // If you are not using windows, ignore this. This just checks if a windows system
    // directory exist and includes a font dir.

    String fontPath = null;
    final String windirs = System.getProperty("java.library.path");
    final String fs = System.getProperty("file.separator");

    if (windirs != null)
    {
      final StringTokenizer strtok
          = new StringTokenizer(windirs, System.getProperty("path.separator"));
      while (strtok.hasMoreTokens())
      {
        final String token = strtok.nextToken();

        if (token.endsWith("System32"))
        {
          // found windows folder ;-)
          final int lastBackslash = token.lastIndexOf(fs);
          fontPath = token.substring(0, lastBackslash) + fs + "Fonts";

          break;
        }
      }
    }
    Log.debug("Fonts located in \"" + fontPath + "\"");
    if (fontPath != null)
    {
      final File file = new File(fontPath);
      registerFontPath(file, encoding, knownFonts, seenFiles);
    }
  }

  /**
   * Register all fonts (*.ttf files) in the given path.
   *
   * @param file  the directory that contains the font files.
   * @param encoding  the encoding for the given font.
   */
  public synchronized void registerFontPath
      (final File file, final String encoding)
  {
    registerFontPath(file, encoding, new HashNMap(), new Properties());
  }

  /**
   * Register all fonts (*.ttf files) in the given path.
   *
   * @param file  the directory that contains the font files.
   * @param encoding  the encoding for the given font.
   * @param knownFonts a map containing all known fonts
   * @param seenFiles a map containing all known font files.
   */
  private synchronized void registerFontPath
      (final File file, final String encoding,
       final HashNMap knownFonts, final Properties seenFiles)
  {
    if (file.exists() && file.isDirectory() && file.canRead())
    {
      final File[] files = file.listFiles(FONTPATHFILTER);
      for (int i = 0; i < files.length; i++)
      {
        final File currentFile = files[i];
        if (currentFile.isDirectory())
        {
          registerFontPath(currentFile, encoding, knownFonts, seenFiles);
        }
        else
        {
          final String fileName = currentFile.toString();
          final String cachedAccessTime = seenFiles.getProperty(fileName);
          final String newAccessTime =
              String.valueOf (currentFile.lastModified() + "," + currentFile.length());

          // the font file is not known ... or has changed.
          // or is not contained in the list of embedable fonts
          // then register the font
          if (newAccessTime.equals(cachedAccessTime) == false ||
              notEmbeddedFonts.containsKey(fileName) == false)
          {
            registerFontFile(fileName, encoding);
          }
          else
          {
            final Iterator it = knownFonts.getAll(fileName);
            while (it.hasNext())
            {
              final String fontName = (String) it.next();
              fontsByName.put(fontName, fileName);
            }
            confirmedFiles.put(fileName, newAccessTime);
          }
        }
      }
    }
    if (ReportConfiguration.getGlobalConfig().getConfigProperty
        (GC_AFTER_REGISTER, "true").equals("true"))
    {
      // clean up after the registering ...
      System.gc();
    }
  }

  /**
   * Register the font (must end this *.ttf) to the FontFactory.
   *
   * @param filename  the filename.
   * @param encoding  the encoding.
   */
  public synchronized void registerFontFile(final String filename, final String encoding)
  {
    if (!filename.toLowerCase().endsWith(".ttf") &&
        !filename.toLowerCase().endsWith(".afm") &&
        !filename.toLowerCase().endsWith(".pfb"))
    {
      return;
    }
    final File file = new File(filename);
    if (file.exists() && file.isFile() && file.canRead())
    {
      final String newAccessTime =
          String.valueOf (file.lastModified() + "," + file.length());
      confirmedFiles.put(filename, newAccessTime);
      try
      {
        addFont(filename, encoding);
      }
      catch (Exception e)
      {
        Log.warn(new Log.SimpleMessage("Font ", filename, " is invalid. Message:", e.getMessage()));
        notEmbeddedFonts.setProperty (filename, "false");
      }
    }
  }

  /**
   * Adds the fontname by creating the basefont object. This method tries to
   * load the fonts as embeddable fonts, if this fails, it repeats the loading
   * with the embedded-flag set to false.
   *
   * @param font  the font name.
   * @param encoding  the encoding.
   *
   * @throws DocumentException if the base font could not be created
   * @throws IOException if the base font file could not be read.
   */
  private void addFont(final String font, final String encoding)
      throws DocumentException, IOException
  {
    if (fontsByName.containsValue(font))
    {
      return; // already in there
    }

    BaseFont bfont;
    String embedded;
    try
    {
      bfont = BaseFont.createFont(font, encoding, true, false, null, null);
      embedded = "true";
    }
    catch (DocumentException de)
    {
      // failed to load the font as embedded font, try not-embedded.
      bfont = BaseFont.createFont(font, encoding, false, false, null, null);
      Log.info (new Log.SimpleMessage
        ("Font ", font, "  cannot be used as embedded font " +
          "due to licensing restrictions."));
      embedded = "false";
    }

    final String[][] fi = bfont.getFullFontName();
    for (int i = 0; i < fi.length; i++)
    {
      final String[] ffi = fi[i];
      final String knownFontEmbeddedState = notEmbeddedFonts.getProperty(font, "false");

      // if unknown or the known font is a restricted version and this one is none,
      // then register the new font
      final String logicalFontname = ffi[3];
      notEmbeddedFonts.setProperty (font, embedded);
      if ((fontsByName.containsKey(logicalFontname) == false) ||
          ((knownFontEmbeddedState.equals("true") == false) &&
           embedded.equals(knownFontEmbeddedState) == false))
      {
        fontsByName.setProperty(logicalFontname, font);
        Log.debug(new Log.SimpleMessage
          ("Registered truetype font ", logicalFontname, "; Embedded=", 
            embedded, new Log.SimpleMessage("File=", font)));
      }
    }
  }

  /**
   * Returns all registered fonts as enumeration.
   *
   * @return an enumeration of the registered fonts.
   */
  public Iterator getRegisteredFonts()
  {
    return fontsByName.keySet().iterator();
  }

  /**
   * Returns the name of the font file by looking up the name.
   *
   * @param font  the font name
   *
   * @return the font file name.
   */
  public String getFontfileForName(final String font)
  {
    if (isInitialized() == false)
    {
      if (getPDFTargetAutoInit().equals(ITEXT_FONT_AUTOINIT_LAZY))
      {
        registerDefaultFontPath();
      }
    }
    return fontsByName.getProperty(font);
  }

  /**
   * Checks, whether the font has known license restrictions.
   * Returns true, if the font can be embedded, and false otherwise.
   *
   * @param fontFileName the filename of the font (not the logical name!)
   * @return true, if the font can be embedded, false otherwise.
   */
  public boolean isEmbeddable (final String fontFileName)
  {
    if (isInitialized() == false)
    {
      if (getPDFTargetAutoInit().equals(ITEXT_FONT_AUTOINIT_LAZY))
      {
        registerDefaultFontPath();
      }
    }
    return notEmbeddedFonts.getProperty(fontFileName, "false").equals("true");
  }

  /**
   * Checks, whether the factory is initialized.
   *
   * @return true, if the factory is initalized, false otherwise.
   */
  public boolean isInitialized()
  {
    return initialized;
  }

  /**
   * Returns the BaseFont encoding property value.
   *
   * @return the BaseFont encoding property value.
   */
  public static final String getDefaultFontEncoding()
  {
    return ReportConfiguration.getGlobalConfig().getConfigProperty
        (ITEXT_FONT_ENCODING, ITEXT_FONT_ENCODING_DEFAULT);
  }

  /**
   * Sets the BaseFont encoding property value.
   *
   * @param encoding the new encoding.
   */
  public static final void setDefaultFontEncoding(final String encoding)
  {
    ReportConfiguration.getGlobalConfig().setConfigProperty
        (ITEXT_FONT_ENCODING, encoding);
  }


  /**
   * Returns whether to search for ttf-fonts when the PDFOutputTarget is loaded.
   *
   * @return the PDFOutputTarget autoinitialisation value.
   */
  public String getPDFTargetAutoInit()
  {
    return ReportConfiguration.getGlobalConfig().getConfigProperty
        (ITEXT_FONT_AUTOINIT, ITEXT_FONT_AUTOINIT_DEFAULT);
  }

  /**
   * Sets the PDF target auto init status.
   *
   * @param autoInit  the new status.
   */
  public void setPDFTargetAutoInit(final String autoInit)
  {
    if (autoInit != null)
    {
      if ((autoInit.equals(ITEXT_FONT_AUTOINIT_LAZY) == false) &&
          (autoInit.equals(ITEXT_FONT_AUTOINIT_NEVER) == false) &&
          (autoInit.equals(ITEXT_FONT_AUTOINIT_ONINIT) == false))
      {
        throw new IllegalArgumentException("Invalid autoinit value.");
      }
    }
    ReportConfiguration.getGlobalConfig().setConfigProperty
        (ITEXT_FONT_AUTOINIT, String.valueOf(autoInit));
  }

  /**
   * Returns/creates the singleton font factory.
   *
   * @return the font factory.
   */
  public static BaseFontFactory getFontFactory()
  {
    if (fontFactory == null)
    {
      fontFactory = new BaseFontFactory();
    }
    return fontFactory;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -