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: FontReader.java 679326 2008-07-24 09:35:34Z vhennebert $ */
 
 package org.docx4j.fonts.fop.fonts;
 
 //Java
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 
 
Class for reading a metric.xml file and creating a font object. Typical usage:
 FontReader reader = new FontReader(<path til metrics.xml>);
 reader.setFontEmbedPath(<path to a .ttf or .pfb file or null to diable embedding>);
 reader.useKerning(true);
 Font f = reader.getFont();
 
 
 public class FontReader extends DefaultHandler {
 
     private Locator locator = null;
     private boolean isCID = false;
     private CustomFont returnFont = null;
     private MultiByteFont multiFont = null;
     private SingleByteFont singleFont = null;
     private StringBuffer text = new StringBuffer();
 
     private List cidWidths = null;
     private int cidWidthIndex = 0;
 
     private Map currentKerning = null;
 
     private List bfranges = null;
 
     private void createFont(InputSource sourcethrows FOPException {
         XMLReader parser = null;
 
         try {
             final SAXParserFactory factory = javax.xml.parsers.SAXParserFactory.newInstance();
             factory.setNamespaceAware(true);
             parser = factory.newSAXParser().getXMLReader();
         } catch (Exception e) {
             throw new FOPException(e);
         }
         if (parser == null) {
             throw new FOPException("Unable to create SAX parser");
         }
 
         try {
             parser.setFeature("http://xml.org/sax/features/namespace-prefixes",
                               false);
         } catch (SAXException e) {
             throw new FOPException("You need a SAX parser which supports SAX version 2",
                                    e);
         }
 
         parser.setContentHandler(this);
 
         try {
             parser.parse(source);
         } catch (SAXException e) {
             throw new FOPException(e);
        } catch (IOException e) {
            throw new FOPException(e);
        }
    }

    
Sets the path to embed a font. A null value disables font embedding.

Parameters:
path URI for the embeddable file
    public void setFontEmbedPath(String path) {
        .setEmbedFileName(path);
    }

    
Enable/disable use of kerning for the font

Parameters:
enabled true to enable kerning, false to disable
    public void setKerningEnabled(boolean enabled) {
        .setKerningEnabled(enabled);
    }

    
Sets the font resolver. Needed for URI resolution.

Parameters:
resolver the font resolver
    public void setResolver(FontResolver resolver) {
        .setResolver(resolver);
    }


    
Get the generated font object

Returns:
the font
    public Typeface getFont() {
        return ;
    }

    
Construct a FontReader object from a path to a metric.xml file and read metric data

Parameters:
source Source of the font metric file
Throws:
org.docx4j.fonts.fop.apps.FOPException if loading the font fails
    public FontReader(InputSource sourcethrows FOPException {
        createFont(source);
    }

    
    public void startDocument() {
    }

    
    public void setDocumentLocator(Locator locator) {
        this. = locator;
    }

    
    public void startElement(String uriString localNameString qName,
                             Attributes attributesthrows SAXException {
        if (localName.equals("font-metrics")) {
            if ("TYPE0".equals(attributes.getValue("type"))) {
                 = new MultiByteFont();
                 = ;
                 = true;
                TTFReader.checkMetricsVersion(attributes);
            } else if ("TRUETYPE".equals(attributes.getValue("type"))) {
                 = new SingleByteFont();
                .setFontType(.);
                 = ;
                 = false;
                TTFReader.checkMetricsVersion(attributes);
            } else {
                 = new SingleByteFont();
                .setFontType(.);
                 = ;
                 = false;
            }
        } else if ("embed".equals(localName)) {
            .setEmbedFileName(attributes.getValue("file"));
            .setEmbedResourceName(attributes.getValue("class"));
        } else if ("cid-widths".equals(localName)) {
             = getInt(attributes.getValue("start-index"));
             = new java.util.ArrayList();
        } else if ("kerning".equals(localName)) {
             = new java.util.HashMap();
            .putKerningEntry(new Integer(attributes.getValue("kpx1")),
                                        );
        } else if ("bfranges".equals(localName)) {
             = new java.util.ArrayList();
        } else if ("bf".equals(localName)) {
            BFEntry entry = new BFEntry(getInt(attributes.getValue("us")),
                                        getInt(attributes.getValue("ue")),
                                        getInt(attributes.getValue("gi")));
            .add(entry);
        } else if ("wx".equals(localName)) {
            .add(new Integer(attributes.getValue("w")));
        } else if ("widths".equals(localName)) {
            //singleFont.width = new int[256];
        } else if ("char".equals(localName)) {
            try {
                .setWidth(Integer.parseInt(attributes.getValue("idx")),
                        Integer.parseInt(attributes.getValue("wdt")));
            } catch (NumberFormatException ne) {
                throw new SAXException("Malformed width in metric file: "
                                   + ne.getMessage(), ne);
            }
        } else if ("pair".equals(localName)) {
            .put(new Integer(attributes.getValue("kpx2")),
                               new Integer(attributes.getValue("kern")));
        }
    }
    private int getInt(String strthrows SAXException {
        int ret = 0;
        try {
            ret = Integer.parseInt(str);
        } catch (Exception e) {
            throw new SAXException("Error while parsing integer value: " + stre);
        }
        return ret;
    }

    
    public void endElement(String uriString localNameString qNamethrows SAXException {
        String content = .toString().trim();
        if ("font-name".equals(localName)) {
            .setFontName(content);
        } else if ("full-name".equals(localName)) {
            .setFullName(content);
        } else if ("family-name".equals(localName)) {
            Set s = new java.util.HashSet();
            s.add(content);
            .setFamilyNames(s);
        } else if ("ttc-name".equals(localName) && ) {
            .setTTCName(content);
        } else if ("encoding".equals(localName)) {
            if ( != null && .getFontType() == .) {
                .setEncoding(content);
            }
        } else if ("cap-height".equals(localName)) {
            .setCapHeight(getInt(content));
        } else if ("x-height".equals(localName)) {
            .setXHeight(getInt(content));
        } else if ("ascender".equals(localName)) {
            .setAscender(getInt(content));
        } else if ("descender".equals(localName)) {
            .setDescender(getInt(content));
        } else if ("left".equals(localName)) {
            int[] bbox = .getFontBBox();
            bbox[0] = getInt(content);
            .setFontBBox(bbox);
        } else if ("bottom".equals(localName)) {
            int[] bbox = .getFontBBox();
            bbox[1] = getInt(content);
            .setFontBBox(bbox);
        } else if ("right".equals(localName)) {
            int[] bbox = .getFontBBox();
            bbox[2] = getInt(content);
            .setFontBBox(bbox);
        } else if ("top".equals(localName)) {
            int[] bbox = .getFontBBox();
            bbox[3] = getInt(content);
            .setFontBBox(bbox);
        } else if ("first-char".equals(localName)) {
            .setFirstChar(getInt(content));
        } else if ("last-char".equals(localName)) {
            .setLastChar(getInt(content));
        } else if ("flags".equals(localName)) {
            .setFlags(getInt(content));
        } else if ("stemv".equals(localName)) {
            .setStemV(getInt(content));
        } else if ("italic-angle".equals(localName)) {
            .setItalicAngle(getInt(content));
        } else if ("missing-width".equals(localName)) {
            .setMissingWidth(getInt(content));
        } else if ("cid-type".equals(localName)) {
            .setCIDType(CIDFontType.byName(content));
        } else if ("default-width".equals(localName)) {
            .setDefaultWidth(getInt(content));
        } else if ("cid-widths".equals(localName)) {
            int[] wds = new int[.size()];
            int j = 0;
            for (int count = 0; count < .size(); count++) {
                Integer i = (Integer).get(count);
                wds[j++] = i.intValue();
            }
            //multiFont.addCIDWidthEntry(cidWidthIndex, wds);
            .setWidthArray(wds);
        } else if ("bfranges".equals(localName)) {
            .setBFEntries((BFEntry[]).toArray(new BFEntry[0]));
        }
        .setLength(0); //Reset text buffer (see characters())
    }

    
    public void characters(char[] chint startint length) {
        .append(chstartlength);
    }
New to GrepCode? Check out our FAQ X