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: FontInfoFinder.java 746664 2009-02-22 12:40:44Z jeremias $ */
 
 package org.docx4j.fonts.fop.fonts.autodetect;
 
 import java.net.URL;
 import java.util.List;
 import java.util.Set;
 
 
Attempts to determine correct FontInfo
 
 public class FontInfoFinder {

    
logging instance
 
     private Logger log = LoggerFactory.getLogger(FontInfoFinder.class);
 
     private FontEventListener eventListener;

    
Sets the font event listener that can be used to receive events about particular events in this class.

Parameters:
listener the font event listener
 
     public void setEventListener(FontEventListener listener) {
         this. = listener;
     }

    
Attempts to determine FontTriplets from a given CustomFont. It seems to be fairly accurate but will probably require some tweaking over time

Parameters:
customFont CustomFont
triplet Collection that will take the generated triplets
 
     private void generateTripletsFromFont(CustomFont customFontCollection triplets) {
         if (.isTraceEnabled()) {
             .trace("Font: " + customFont.getFullName()
                     + ", family: " + customFont.getFamilyNames()
                     + ", PS: " + customFont.getFontName()
                     + ", EmbedName: " + customFont.getEmbedFontName());
         }
 
         // default style and weight triplet vales (fallback)
         String strippedName = stripQuotes(customFont.getStrippedFontName());
         //String subName = customFont.getFontSubName();
         String fullName = stripQuotes(customFont.getFullName());
         String searchName = fullName.toLowerCase();
 
         String style = guessStyle(customFontsearchName);
         int weight//= customFont.getWeight();
         int guessedWeight = FontUtil.guessWeight(searchName);
         //We always take the guessed weight for now since it yield much better results.
         //OpenType's OS/2 usWeightClass value proves to be unreliable.
        weight = guessedWeight;
        //Full Name usually includes style/weight info so don't use these traits
        //If we still want to use these traits, we have to make FontInfo.fontLookup() smarter
        
        //System.out.println("fuulName:" + fullName);
        // Full name contains spaces, and is typically Title Case
        triplets.add(new FontTriplet(fullName..));
        if (!fullName.equals(strippedName)) {
            triplets.add(new FontTriplet(strippedName..));
        }
        Set familyNames = customFont.getFamilyNames();
        Iterator iter = familyNames.iterator();
        while (iter.hasNext()) {
            String familyName = stripQuotes((String)iter.next());
            if (!fullName.equals(familyName)) {
                /* Heuristic:
                 *   The more similar the family name to the full font name,
                 *   the higher the priority of its triplet.
                 * (Lower values indicate higher priorities.) */
                int priority = fullName.startsWith(familyName)
                    ? fullName.length() - familyName.length()
                    : fullName.length();
                triplets.add(new FontTriplet(familyNamestyleweightpriority));
            }
        }
    }
    private final Pattern quotePattern = Pattern.compile("'");
    private String stripQuotes(String name) {
        return .matcher(name).replaceAll("");
    }
    private String guessStyle(CustomFont customFontString fontName) {
        // style
        String style = .;
        if (customFont.getItalicAngle() > 0) {
            style = .;
        } else {
            style = FontUtil.guessStyle(fontName);
        }
        return style;
    }

    
Attempts to determine FontInfo from a given custom font

Parameters:
fontUrl the font URL
customFont the custom font
fontCache font cache (may be null)
Returns:
            URL fontUrlCustomFont customFontFontCache fontCache) {
        List fontTripletList = new java.util.ArrayList();
        generateTripletsFromFont(customFontfontTripletList);
        String embedUrl;
        embedUrl = fontUrl.toExternalForm();
        String subFontName = null;
        if (customFont instanceof MultiByteFont) {
            subFontName = ((MultiByteFont)customFont).getTTCName();
        }
        EmbedFontInfo fontInfo = new EmbedFontInfo(nullcustomFont.isKerningEnabled(),
                fontTripletListembedUrlsubFontName);
        fontInfo.setFamilyNames(customFont.getFamilyNames());
        fontInfo.setPanose(customFont.getPanose());
        fontInfo.setPostScriptName(customFont.getFontName());
        fontInfo.setEmbeddable(customFont.isEmbeddable());
        
        if (fontCache != null) {
            fontCache.addFont(fontInfo);
        }
        return fontInfo;
    }

    
Attempts to determine EmbedFontInfo from a given font file.

Parameters:
fontUrl font URL. Assumed to be local.
resolver font resolver used to resolve font
fontCache font cache (may be null)
Returns:
an array of newly created embed font info. Generally, this array will have only one entry, unless the fontUrl is a TrueType Collection
    public EmbedFontInfo[] find(URL fontUrlFontResolver resolverFontCache fontCache) {
        String embedUrl = null;
        embedUrl = fontUrl.toExternalForm();
        long fileLastModified = -1;
        if (fontCache != null) {
            fileLastModified = FontCache.getLastModified(fontUrl);
            // firstly try and fetch it from cache before loading/parsing the font file
            if (fontCache.containsFont(embedUrl)) {
                EmbedFontInfo[] fontInfos = fontCache.getFontInfos(embedUrlfileLastModified);
                if (fontInfos != null) {
                    return fontInfos;
                }
            // is this a previously failed parsed font?
            } else if (fontCache.isFailedFont(embedUrlfileLastModified)) {
                if (.isDebugEnabled()) {
                    .debug("Skipping font file that failed to load previously: " + embedUrl);
                }
                return null;
            }
        }
        // try to determine triplet information from font file
        CustomFont customFont = null;
        if (fontUrl.toExternalForm().endsWith(".ttc")) {
            // Get a list of the TTC Font names
            List ttcNames = null//List<String>
            String fontFileURI = fontUrl.toExternalForm().trim();
            InputStream in = null;
            try {
                in = FontLoader.openFontUri(resolverfontFileURI);
                TTFFile ttf = new TTFFile();
                FontFileReader reader = new FontFileReader(in);
                ttcNames = ttf.getTTCnames(reader);
            } catch (Exception e) {
                if (this. != null) {
                    this..fontLoadingErrorAtAutoDetection(thisfontFileURIe);
                }
                return null;
            } finally {
                IOUtils.closeQuietly(in);
            }
            List embedFontInfoList = new java.util.ArrayList(); //List<EmbedFontInfo>
            // For each font name ...
            //for (String fontName : ttcNames) {
            Iterator ttcNamesIterator = ttcNames.iterator();
            while (ttcNamesIterator.hasNext()) {
                String fontName = (String)ttcNamesIterator.next();
                if (.isDebugEnabled()) {
                    .debug("Loading " + fontName);
                }
                try {
                    TTFFontLoader ttfLoader = new TTFFontLoader(
                            fontFileURIfontNametrue.trueresolver);
                    customFont = ttfLoader.getFont();
                    if (this. != null) {
                        customFont.setEventListener(this.);
                    }
                } catch (Exception e) {
                    if (fontCache != null) {
                        fontCache.registerFailedFont(embedUrlfileLastModified);
                    }
                    if (this. != null) {
                        this..fontLoadingErrorAtAutoDetection(thisembedUrle);
                    }
                    continue;
                }
                EmbedFontInfo fi = getFontInfoFromCustomFont(fontUrlcustomFontfontCache);
                if (fi != null) {
                    embedFontInfoList.add(fi);
                }
            }
            return (EmbedFontInfo[])embedFontInfoList.toArray(
                    new EmbedFontInfo[embedFontInfoList.size()]);
        } else {
            // The normal case
            try {
                customFont = FontLoader.loadFont(fontUrlnulltrue.resolver);
                if (this. != null) {
                    customFont.setEventListener(this.);
                }
            } catch (Exception e) {
                if (fontCache != null) {
                    fontCache.registerFailedFont(embedUrlfileLastModified);
                }
                if (this. != null) {
                    this..fontLoadingErrorAtAutoDetection(thisembedUrle);
                }
                return null;
            }
            EmbedFontInfo fi = getFontInfoFromCustomFont(fontUrlcustomFontfontCache);
            if (fi != null) {
                return new EmbedFontInfo[] {fi};
            } else {
                return null;
            }
        }
    }
New to GrepCode? Check out our FAQ X