Package org.jfree.fonts.afm

Source Code of org.jfree.fonts.afm.AfmFontRegistry$FontPathFilter

/**
* ===========================================
* LibFonts : a free Java font reading library
* ===========================================
*
* Project Info:  http://reporting.pentaho.org/libfonts/
*
* (C) Copyright 2006-2007, by Pentaho Corporation and Contributors.
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ------------
* $Id: AfmFontRegistry.java 3523 2007-10-16 11:03:09Z tmorgner $
* ------------
* (C) Copyright 2006-2007, by Pentaho Corporation.
*/

package org.jfree.fonts.afm;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.HashMap;

import org.jfree.fonts.registry.AbstractFontFileRegistry;
import org.jfree.fonts.registry.DefaultFontFamily;
import org.jfree.fonts.registry.FontFamily;
import org.jfree.fonts.registry.FontMetricsFactory;
import org.jfree.util.Log;
import org.jfree.util.StringUtils;

/**
* Creation-Date: 21.07.2007, 20:14:05
*
* @author Thomas Morgner
*/
public class AfmFontRegistry extends AbstractFontFileRegistry
{
  /**
   * The font path filter is used to collect font files and directories during
   * the font path registration.
   */
  private static class FontPathFilter implements FileFilter
  {
    /** Default Constructor. */
    protected FontPathFilter()
    {
    }

    /**
     * Tests whether or not the specified abstract pathname should be included
     * in a pathname list.
     *
     * @param pathname The abstract pathname to be tested
     * @return <code>true</code> if and only if <code>pathname</code> should be
     *         included
     */
    public boolean accept(final File pathname)
    {
      if (pathname.canRead() == false)
      {
        return false;
      }
      if (pathname.isDirectory())
      {
        return true;
      }
      final String name = pathname.getName();
      return StringUtils.endsWithIgnoreCase(name, ".afm");
    }

  }

  /** The singleton instance of the font path filter. */
  private static final FontPathFilter FONTPATHFILTER = new FontPathFilter();
  /** Fonts stored by name. */

  private HashMap fontFamilies;
  private HashMap alternateFamilyNames;
  private HashMap fullFontNames;
  private HashMap seenFiles;

  public AfmFontRegistry()
  {
    this.fontFamilies = new HashMap();
    this.alternateFamilyNames = new HashMap();
    this.fullFontNames = new HashMap();
    this.seenFiles = new HashMap();
  }

  protected FileFilter getFileFilter()
  {
    return FONTPATHFILTER;
  }

  public FontMetricsFactory createMetricsFactory()
  {
    // this is a todo - for now we rely on itext
    throw new UnsupportedOperationException();
  }

  protected boolean isCached(final File file)
  {
    return super.isCached(file);
  }

  /**
   * 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 file name.
   * @param encoding the encoding.
   * @throws java.io.IOException       if the base font file could not be read.
   */
  public void addFont(final File font, final String encoding) throws IOException
  {
    if (seenFiles.containsKey(font))
    {
      return; // already in there
    }

    final String fileName = font.getCanonicalPath();
    final String filePfbName = fileName.substring(0, fileName.length() - 3) + "pfb";
    final File filePfb = new File(filePfbName);
    boolean embedded = true;
    if (filePfb.exists() == false ||
        filePfb.isFile() == false ||
        filePfb.canRead() == false)
    {
      Log.warn("Cannot embedd font: " + filePfb + " is missing for " + font);
      embedded = false;
    }

    final AfmFont pfmFont = new AfmFont(font, embedded);
    registerFont (pfmFont);
    pfmFont.dispose();
  }

  private void registerFont(final AfmFont font) throws IOException
  {
    final String windowsName = font.getFamilyName();
    final String postscriptName = font.getFontName();

    final DefaultFontFamily fontFamily = createFamily(windowsName);
    this.alternateFamilyNames.put(windowsName, fontFamily);
    this.alternateFamilyNames.put(postscriptName, fontFamily);

    this.fullFontNames.put(windowsName, fontFamily);
    this.fullFontNames.put(postscriptName, fontFamily);

    final AfmFontRecord record = new AfmFontRecord(font, fontFamily);
    fontFamily.addFontRecord(record);

  }

  private DefaultFontFamily createFamily(final String name)
  {
    final DefaultFontFamily fontFamily = (DefaultFontFamily)
            this.fontFamilies.get(name);
    if (fontFamily != null)
    {
      return fontFamily;
    }

    final DefaultFontFamily createdFamily = new DefaultFontFamily(name);
    this.fontFamilies.put(name, createdFamily);
    return createdFamily;
  }

  public String[] getRegisteredFamilies()
  {
    return (String[]) fontFamilies.keySet().toArray
            (new String[fontFamilies.size()]);
  }

  public String[] getAllRegisteredFamilies()
  {
    return (String[]) alternateFamilyNames.keySet().toArray
            (new String[alternateFamilyNames.size()]);
  }

  public FontFamily getFontFamily(final String name)
  {
    final FontFamily primary = (FontFamily) this.fontFamilies.get(name);
    if (primary != null)
    {
      return primary;
    }
    final FontFamily secondary = (FontFamily)
            this.alternateFamilyNames.get(name);
    if (secondary != null)
    {
      return secondary;
    }
    return (FontFamily) this.fullFontNames.get(name);
  }
}
TOP

Related Classes of org.jfree.fonts.afm.AfmFontRegistry$FontPathFilter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.