Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* NOTICE: This file has been changed by Plutext Pty Ltd for use in docx4j.
   * The package name has been changed; there may also be other changes.
   * 
   * This notice is included to meet the condition in clause 4(b) of the License. 
   */
  
   /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
  * this work for additional information regarding copyright ownership.
  * The ASF licenses this file to You under the Apache License, Version 2.0
  * (the "License"); you may not use this file except in compliance with
  * the License.  You may obtain a copy of the License at
  *
  *      http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 /* $Id: FontLoader.java 746664 2009-02-22 12:40:44Z jeremias $ */
 
 package org.docx4j.fonts.fop.fonts;
 
 import java.io.File;
 import java.net.URL;
 
 
 
Base class for font loaders.
 
 public abstract class FontLoader {

    
logging instance
 
     protected static Logger log = LoggerFactory.getLogger(FontLoader.class);

    
URI representing the font file
 
     protected String fontFileURI = null;
    
the FontResolver to use for font URI resolution
 
     protected FontResolver resolver = null;
    
the loaded font
 
     protected CustomFont returnFont = null;

    
true if the font has been loaded
 
     protected boolean loaded = false;
    
true if the font will be embedded, false if it will be referenced only.
 
     protected boolean embedded = true;
    
true if kerning information shall be loaded if available.
 
     protected boolean useKerning = true;

    
Default constructor.

Parameters:
fontFileURI the URI to the PFB file of a Type 1 font
embedded indicates whether the font is embedded or referenced
useKerning indicates whether kerning information shall be loaded if available
resolver the font resolver used to resolve URIs
 
     public FontLoader(String fontFileURIboolean embeddedboolean useKerning,
             FontResolver resolver) {
         this. = fontFileURI;
         this. = embedded;
         this. = useKerning;
         this. = resolver;
     }
 
     private static boolean isType1(String fontURI) {
         return fontURI.toLowerCase().endsWith(".pfb");
     }

    
Loads a custom font from a File. In the case of Type 1 fonts, the PFB file must be specified.

Parameters:
fontFile the File representation of the font
subFontName the sub-fontname of a font (for TrueType Collections, null otherwise)
embedded indicates whether the font is embedded or referenced
encodingMode the requested encoding mode
resolver the font resolver to use when resolving URIs
Returns:
the newly loaded font
Throws:
java.io.IOException In case of an I/O error
 
     public static CustomFont loadFont(File fontFileString subFontName,
             boolean embeddedEncodingMode encodingModeFontResolver resolverthrows IOException {
         return loadFont(fontFile.getAbsolutePath(), subFontName,
                 embeddedencodingModetrueresolver);
     }

    
Loads a custom font from an URL. In the case of Type 1 fonts, the PFB file must be specified.

Parameters:
fontUrl the URL representation of the font
subFontName the sub-fontname of a font (for TrueType Collections, null otherwise)
embedded indicates whether the font is embedded or referenced
encodingMode the requested encoding mode
resolver the font resolver to use when resolving URIs
Returns:
the newly loaded font
Throws:
java.io.IOException In case of an I/O error
    public static CustomFont loadFont(URL fontUrlString subFontName,
            boolean embeddedEncodingMode encodingMode,
            FontResolver resolverthrows IOException {
        return loadFont(fontUrl.toExternalForm(), subFontName,
                embeddedencodingModetrue,
                resolver);
    }

    
Loads a custom font from a URI. In the case of Type 1 fonts, the PFB file must be specified.

Parameters:
fontFileURI the URI to the font
subFontName the sub-fontname of a font (for TrueType Collections, null otherwise)
embedded indicates whether the font is embedded or referenced
encodingMode the requested encoding mode
useKerning indicates whether kerning information should be loaded if available
resolver the font resolver to use when resolving URIs
Returns:
the newly loaded font
Throws:
java.io.IOException In case of an I/O error
    public static CustomFont loadFont(String fontFileURIString subFontName,
            boolean embeddedEncodingMode encodingModeboolean useKerning,
            FontResolver resolverthrows IOException {
        fontFileURI = fontFileURI.trim();
        boolean type1 = isType1(fontFileURI);
        FontLoader loader;
        if (type1) {
            if (encodingMode == .) {
                throw new IllegalArgumentException(
                        "CID encoding mode not supported for Type 1 fonts");
            }
            loader = new Type1FontLoader(fontFileURIembeddeduseKerningresolver);
        } else {
            loader = new TTFFontLoader(fontFileURIsubFontName,
                    embeddedencodingModeuseKerningresolver);
        }
        return loader.getFont();
    }

    
Opens a font URI and returns an input stream.

Parameters:
resolver the FontResolver to use for font URI resolution
uri the URI representing the font
Returns:
the InputStream to read the font from.
Throws:
java.io.IOException In case of an I/O error
java.net.MalformedURLException If an invalid URL is built
    public static InputStream openFontUri(FontResolver resolverString uri)
                    throws IOExceptionMalformedURLException {
        InputStream in = null;
        if (resolver != null) {
            Source source = resolver.resolve(uri);
            if (source == null) {
                String err = "Cannot load font: failed to create Source for font file "
                    + uri;
                throw new IOException(err);
            }
            if (source instanceof StreamSource) {
                in = ((StreamSourcesource).getInputStream();
            }
            if (in == null && source.getSystemId() != null) {
                in = new java.net.URL(source.getSystemId()).openStream();
            }
            if (in == null) {
                String err = "Cannot load font: failed to create InputStream from"
                    + " Source for font file " + uri;
                throw new IOException(err);
            }
        } else {
            in = new URL(uri).openStream();
        }
        return in;
    }

    
Reads/parses the font data.

Throws:
java.io.IOException In case of an I/O error
    protected abstract void read() throws IOException;

    
Returns the custom font that was read using this instance of FontLoader.

Returns:
the newly loaded font
Throws:
java.io.IOException if an I/O error occurs
    public CustomFont getFont() throws IOException {
        if (!) {
            read();
        }
        return this.;
    }
New to GrepCode? Check out our FAQ X