Save This Page
Home » pdfbox-1.1.0-src » org.apache.fontbox.cff » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.fontbox.cff;
   18   
   19   import java.io.IOException;
   20   import java.util.ArrayList;
   21   import java.util.Collection;
   22   import java.util.HashSet;
   23   import java.util.Iterator;
   24   import java.util.LinkedHashMap;
   25   import java.util.List;
   26   import java.util.Map;
   27   import java.util.Set;
   28   
   29   import org.apache.fontbox.cff.charset.CFFCharset;
   30   import org.apache.fontbox.cff.encoding.CFFEncoding;
   31   
   32   /**
   33    * This class represents a CFF/Type2 Font.
   34    * 
   35    * @author Villu Ruusmann
   36    * @version $Revision$
   37    */
   38   public class CFFFont
   39   {
   40   
   41       private String fontname = null;
   42       private Map<String, Object> topDict = new LinkedHashMap<String, Object>();
   43       private Map<String, Object> privateDict = new LinkedHashMap<String, Object>();
   44       private CFFEncoding fontEncoding = null;
   45       private CFFCharset fontCharset = null;
   46       private Map<String, byte[]> charStringsDict = new LinkedHashMap<String, byte[]>();
   47   
   48       /**
   49        * The name of the font.
   50        * @return the name of the font
   51        */
   52       public String getName()
   53       {
   54           return fontname;
   55       }
   56   
   57       /**
   58        * Sets the name of the font.
   59        * @param name the name of the font
   60        */
   61       public void setName(String name)
   62       {
   63           fontname = name;
   64       }
   65   
   66       /**
   67        * Returns the value for the given name from the dictionary.
   68        * @param name the name of the value
   69        * @return the value of the name if available
   70        */
   71       public Object getProperty(String name)
   72       {
   73           Object topDictValue = topDict.get(name);
   74           if (topDictValue != null)
   75           {
   76               return topDictValue;
   77           }
   78           Object privateDictValue = privateDict.get(name);
   79           if (privateDictValue != null)
   80           {
   81               return privateDictValue;
   82           }
   83           return null;
   84       }
   85   
   86       /**
   87        * Adds the given key/value pair to the top dictionary. 
   88        * @param name the given key
   89        * @param value the given value
   90        */
   91       public void addValueToTopDict(String name, Object value)
   92       {
   93           if (value != null)
   94           {
   95               topDict.put(name, value);
   96           }
   97       }
   98       /** 
   99        * Returns the top dictionary.
  100        * @return the dictionary
  101        */
  102       public Map<String, Object> getTopDict()
  103       {
  104           return topDict;
  105       }
  106   
  107       /**
  108        * Adds the given key/value pair to the private dictionary. 
  109        * @param name the given key
  110        * @param value the given value
  111        */
  112       public void addValueToPrivateDict(String name, Object value)
  113       {
  114           if (value != null)
  115           {
  116               privateDict.put(name, value);
  117           }
  118       }
  119       /** 
  120        * Returns the private dictionary.
  121        * @return the dictionary
  122        */
  123       public Map<String, Object> getPrivateDict()
  124       {
  125           return privateDict;
  126       }
  127   
  128       /**
  129        * Get the mapping (code/SID/charname/bytes) for this font.
  130        * @return mappings for codes < 256 and for codes > = 256
  131        */
  132       public Collection<Mapping> getMappings()
  133       {
  134           List<Mapping> mappings = new ArrayList<Mapping>();
  135           Set<String> mappedNames = new HashSet<String>();
  136           for (CFFEncoding.Entry entry : fontEncoding.getEntries())
  137           {
  138               String charName = fontCharset.getName(entry.getSID());
  139               // Predefined encoding
  140               if (charName == null)
  141               {
  142                   continue;
  143               }
  144               byte[] bytes = charStringsDict.get(charName);
  145               if (bytes == null)
  146               {
  147                   continue;
  148               }
  149               Mapping mapping = new Mapping();
  150               mapping.setCode(entry.getCode());
  151               mapping.setSID(entry.getSID());
  152               mapping.setName(charName);
  153               mapping.setBytes(bytes);
  154               mappings.add(mapping);
  155               mappedNames.add(charName);
  156           }
  157           if (fontEncoding instanceof CFFParser.EmbeddedEncoding)
  158           {
  159               CFFParser.EmbeddedEncoding embeddedEncoding = (CFFParser.EmbeddedEncoding)fontEncoding;
  160   
  161               for (CFFParser.EmbeddedEncoding.Supplement supplement : embeddedEncoding.getSupplements())
  162               {
  163                   String charName = fontCharset.getName(supplement.getGlyph());
  164                   if (charName == null)
  165                   {
  166                       continue;
  167                   }
  168                   byte[] bytes = charStringsDict.get(charName);
  169                   if (bytes == null)
  170                   {
  171                       continue;
  172                   }
  173                   Mapping mapping = new Mapping();
  174                   mapping.setCode(supplement.getCode());
  175                   mapping.setSID(supplement.getGlyph());
  176                   mapping.setName(charName);
  177                   mapping.setBytes(bytes);
  178                   mappings.add(mapping);
  179                   mappedNames.add(charName);
  180               }
  181           }
  182           // XXX
  183           int code = 256;
  184           for (CFFCharset.Entry entry : fontCharset.getEntries())
  185           {
  186               String name = entry.getName();
  187               if (mappedNames.contains(name))
  188               {
  189                   continue;
  190               }
  191               byte[] bytes = this.charStringsDict.get(name);
  192               if (bytes == null)
  193               {
  194                   continue;
  195               }
  196               Mapping mapping = new Mapping();
  197               mapping.setCode(code++);
  198               mapping.setSID(entry.getSID());
  199               mapping.setName(name);
  200               mapping.setBytes(bytes);
  201   
  202               mappings.add(mapping);
  203   
  204               mappedNames.add(name);
  205           }
  206           return mappings;
  207       }
  208   
  209       /**
  210        * Returns the CFFEncoding of the font.
  211        * @return the encoding
  212        */
  213       public CFFEncoding getEncoding()
  214       {
  215           return fontEncoding;
  216       }
  217   
  218       /**
  219        * Sets the CFFEncoding of the font.
  220        * @param encoding the given CFFEncoding
  221        */
  222       public void setEncoding(CFFEncoding encoding)
  223       {
  224           fontEncoding = encoding;
  225       }
  226   
  227       /**
  228        * Returns the CFFCharset of the font.
  229        * @return the charset
  230        */
  231       public CFFCharset getCharset()
  232       {
  233           return fontCharset;
  234       }
  235   
  236       /**
  237        * Sets the CFFCharset of the font.
  238        * @param charset the given CFFCharset
  239        */
  240       public void setCharset(CFFCharset charset)
  241       {
  242           fontCharset = charset;
  243       }
  244   
  245       /** 
  246        * Returns the character strings dictionary.
  247        * @return the dictionary
  248        */
  249       public Map<String, byte[]> getCharStringsDict()
  250       {
  251           return charStringsDict;
  252       }
  253   
  254       /**
  255        * Creates a CharStringConverter for this font.
  256        * @return the new CharStringConverter
  257        */
  258       public CharStringConverter createConverter()
  259       {
  260           Number defaultWidthX = (Number) getProperty("defaultWidthX");
  261           Number nominalWidthX = (Number) getProperty("nominalWidthX");
  262           return new CharStringConverter(defaultWidthX.intValue(), nominalWidthX
  263                   .intValue());
  264       }
  265   
  266       /**
  267        * Creates a CharStringRenderer for this font.
  268        * @return the new CharStringRenderer
  269        */
  270       public CharStringRenderer createRenderer()
  271       {
  272           return new CharStringRenderer();
  273       }
  274   
  275       /**
  276        * {@inheritDoc}
  277        */
  278       public String toString()
  279       {
  280           return getClass().getName() + "[name=" + fontname + ", topDict=" + topDict
  281                   + ", privateDict=" + privateDict + ", encoding=" + fontEncoding
  282                   + ", charset=" + fontCharset + ", charStringsDict="
  283                   + charStringsDict + "]";
  284       }
  285   
  286       /**
  287        * This class is used for the font mapping.
  288        *
  289        */
  290       public class Mapping
  291       {
  292           private int mappedCode;
  293           private int mappedSID;
  294           private String mappedName;
  295           private byte[] mappedBytes;
  296   
  297           /**
  298            * Converts the mapping into a Type1-sequence.
  299            * @return the Type1-sequence
  300            * @throws IOException if an error occurs during reading
  301            */
  302           public List<Object> toType1Sequence() throws IOException
  303           {
  304               CharStringConverter converter = createConverter();
  305               return converter.convert(toType2Sequence());
  306           }
  307   
  308           /**
  309            * Converts the mapping into a Type2-sequence.
  310            * @return the Type2-sequence
  311            * @throws IOException if an error occurs during reading
  312            */
  313           public List<Object> toType2Sequence() throws IOException
  314           {
  315               Type2CharStringParser parser = new Type2CharStringParser();
  316               return parser.parse(getBytes());
  317           }
  318   
  319           /**
  320            * Gets the value for the code.
  321            * @return the code
  322            */
  323           public int getCode()
  324           {
  325               return mappedCode;
  326           }
  327   
  328           private void setCode(int code)
  329           {
  330               mappedCode = code;
  331           }
  332   
  333           /**
  334            * Gets the value for the SID.
  335            * @return the SID
  336            */
  337           public int getSID()
  338           {
  339               return mappedSID;
  340           }
  341   
  342           private void setSID(int sid)
  343           {
  344               this.mappedSID = sid;
  345           }
  346   
  347           /**
  348            * Gets the value for the name.
  349            * @return the name
  350            */
  351           public String getName()
  352           {
  353               return mappedName;
  354           }
  355   
  356           private void setName(String name)
  357           {
  358               this.mappedName = name;
  359           }
  360   
  361           /**
  362            * Gets the value for the bytes.
  363            * @return the bytes
  364            */
  365           public byte[] getBytes()
  366           {
  367               return mappedBytes;
  368           }
  369   
  370           private void setBytes(byte[] bytes)
  371           {
  372               this.mappedBytes = bytes;
  373           }
  374       }
  375   }

Save This Page
Home » pdfbox-1.1.0-src » org.apache.fontbox.cff » [javadoc | source]