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: TTFFontLoader.java 746664 2009-02-22 12:40:44Z jeremias $ */
 
 package org.docx4j.fonts.fop.fonts.truetype;
 
 import java.util.List;
 import java.util.Map;
 
 
 
Loads a TrueType font into memory directly from the original font file.
 
 public class TTFFontLoader extends FontLoader {
 
     private MultiByteFont multiFont;
     private SingleByteFont singleFont;
     private String subFontName;
     private EncodingMode encodingMode;

    
Default constructor

Parameters:
fontFileURI the URI representing the font file
resolver the FontResolver for font URI resolution
 
     public TTFFontLoader(String fontFileURIFontResolver resolver) {
         this(fontFileURInulltrue.trueresolver);
     }

    
Additional constructor for TrueType Collections.

Parameters:
fontFileURI the URI representing the font file
subFontName the sub-fontname of a font in a TrueType Collection (or null for normal TrueType fonts)
embedded indicates whether the font is embedded or referenced
encodingMode the requested encoding mode
useKerning true to enable loading kerning info if available, false to disable
resolver the FontResolver for font URI resolution
 
     public TTFFontLoader(String fontFileURIString subFontName,
                 boolean embeddedEncodingMode encodingModeboolean useKerning,
                 FontResolver resolver) {
         super(fontFileURIembeddedtrueresolver);
         this. = subFontName;
         this. = encodingMode;
         if (this. == .) {
             this. = .//Default to CID mode for TrueType
         }
     }

    
 
     protected void read() throws IOException {
         read(this.);
     }

    
Reads a TrueType font.

Parameters:
ttcFontName the TrueType sub-font name of TrueType Collection (may be null for normal TrueType fonts)
Throws:
java.io.IOException if an I/O error occurs
 
     private void read(String ttcFontNamethrows IOException {
        InputStream in = openFontUri(this.);
        try {
            TTFFile ttf = new TTFFile();
            FontFileReader reader = new FontFileReader(in);
            boolean supported = ttf.readFont(readerttcFontName);
            if (!supported) {
                throw new IOException("TrueType font is not supported: " + );
            }
            buildFont(ttfttcFontName);
             = true;
        } finally {
            IOUtils.closeQuietly(in);
        }
    }
    private void buildFont(TTFFile ttfString ttcFontName) {
        if (ttf.isCFF()) {
            throw new UnsupportedOperationException(
                    "OpenType fonts with CFF data are not supported, yet");
        }
        boolean isCid = this.;
        if (this. == .) {
            isCid = false;
        }
        if (isCid) {
             = new MultiByteFont();
             = ;
            .setTTCName(ttcFontName);
        } else {
             = new SingleByteFont();
             = ;
        }
        .setFullName(ttf.getFullName());
        .setCapHeight(ttf.getCapHeight());
        .setXHeight(ttf.getXHeight());
        .setFontBBox(ttf.getFontBBox());
        .setFlags(ttf.getFlags());
        .setStemV(Integer.parseInt(ttf.getStemV())); //not used for TTF
        .setItalicAngle(Integer.parseInt(ttf.getItalicAngle()));
        .setMissingWidth(0);
        .setWeight(ttf.getWeightClass());
        
        .setPanose(ttf.getPanose() );
        .setEmbeddable(ttf.isEmbeddable() );
        if (isCid) {
            .setCIDType(.);
            int[] wx = ttf.getWidths();
            .setWidthArray(wx);
            List entries = ttf.getCMaps();
            BFEntry[] bfentries = new BFEntry[entries.size()];
            int pos = 0;
            Iterator iter = ttf.getCMaps().listIterator();
            while (iter.hasNext()) {
                TTFCmapEntry ce = (TTFCmapEntry)iter.next();
                bfentries[pos] = new BFEntry(ce.getUnicodeStart(), ce.getUnicodeEnd(),
                        ce.getGlyphStartIndex());
                pos++;
            }
            .setBFEntries(bfentries);
        } else {
            .setFontType(.);
            .setEncoding(ttf.getCharSetName());
            .setFirstChar(ttf.getFirstChar());
            .setLastChar(ttf.getLastChar());
            copyWidthsSingleByte(ttf);
        }
        if () {
            copyKerning(ttfisCid);
        }
        if (this. && ttf.isEmbeddable()) {
            .setEmbedFileName(this.);
        }
    }
    private void copyWidthsSingleByte(TTFFile ttf) {
        int[] wx = ttf.getWidths();
        for (int i = .getFirstChar(); i <= .getLastChar(); i++) {
            .setWidth(ittf.getCharWidth(i));
        }
        Iterator iter = ttf.getCMaps().listIterator();
        while (iter.hasNext()) {
            TTFCmapEntry ce = (TTFCmapEntry)iter.next();
            if (ce.getUnicodeStart() < 0xFFFE) {
                for (char u = (char)ce.getUnicodeStart(); u <= ce.getUnicodeEnd(); u++) {
                    int codePoint = .getEncoding().mapChar(u);
                    if (codePoint <= 0) {
                        String unicode = Character.toString(u);
                        String charName = Glyphs.stringToGlyph(unicode);
                        if (charName.length() > 0) {
                            NamedCharacter nc = new NamedCharacter(charNameunicode);
                            int glyphIndex = ce.getGlyphStartIndex() + u - ce.getUnicodeStart();
                            .addUnencodedCharacter(ncwx[glyphIndex]);
                        }
                    }
                }
            }
        }
    }

    
Copy kerning information.
    private void copyKerning(TTFFile ttfboolean isCid) {
        // Get kerning
        Iterator iter;
        if (isCid) {
            iter = ttf.getKerning().keySet().iterator();
        } else {
            iter = ttf.getAnsiKerning().keySet().iterator();
        }
        while (iter.hasNext()) {
            Integer kpx1 = (Integer)iter.next();
            Map h2;
            if (isCid) {
                h2 = (Map)ttf.getKerning().get(kpx1);
            } else {
                h2 = (Map)ttf.getAnsiKerning().get(kpx1);
            }
            .putKerningEntry(kpx1h2);
        }
    }
New to GrepCode? Check out our FAQ X