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.List;
   22   
   23   /**
   24    * This class represents a converter for a mapping into a Type1-sequence.
   25    * @author Villu Ruusmann
   26    * @version $Revision: 1.0 $
   27    */
   28   public class Type1CharStringParser
   29   {
   30   
   31       private DataInput input = null;
   32       private List<Object> sequence = null;
   33   
   34       /**
   35        * The given byte array will be parsed and converted to a Type1 sequence.
   36        * @param bytes the given mapping as byte array
   37        * @return the Type1 sequence
   38        * @throws IOException if an error occurs during reading
   39        */
   40       public List<Object> parse(byte[] bytes) throws IOException
   41       {
   42           input = new DataInput(bytes);
   43           sequence = new ArrayList<Object>();
   44           while (input.hasRemaining())
   45           {
   46               int b0 = input.readUnsignedByte();
   47   
   48               if (b0 >= 0 && b0 <= 31)
   49               {
   50                   sequence.add(readCommand(b0));
   51               } 
   52               else if (b0 >= 32 && b0 <= 255)
   53               {
   54                   sequence.add(readNumber(b0));
   55               } 
   56               else
   57               {
   58                   throw new IllegalArgumentException();
   59               }
   60           }
   61           return sequence;
   62       }
   63   
   64       private CharStringCommand readCommand(int b0) throws IOException
   65       {
   66           if (b0 == 12)
   67           {
   68               int b1 = input.readUnsignedByte();
   69               return new CharStringCommand(b0, b1);
   70           }
   71           return new CharStringCommand(b0);
   72       }
   73   
   74       private Integer readNumber(int b0) throws IOException
   75       {
   76           if (b0 >= 32 && b0 <= 246)
   77           {
   78               return Integer.valueOf(b0 - 139);
   79           } 
   80           else if (b0 >= 247 && b0 <= 250)
   81           {
   82               int b1 = input.readUnsignedByte();
   83               return Integer.valueOf((b0 - 247) * 256 + b1 + 108);
   84           } 
   85           else if (b0 >= 251 && b0 <= 254)
   86           {
   87               int b1 = input.readUnsignedByte();
   88               return Integer.valueOf(-(b0 - 251) * 256 - b1 - 108);
   89           } 
   90           else if (b0 == 255)
   91           {
   92               int b1 = input.readUnsignedByte();
   93               int b2 = input.readUnsignedByte();
   94               int b3 = input.readUnsignedByte();
   95               int b4 = input.readUnsignedByte();
   96   
   97               return Integer.valueOf(b1 << 24 | b2 << 16 | b3 << 8 | b4);
   98           } 
   99           else
  100           {
  101               throw new IllegalArgumentException();
  102           }
  103       }
  104   }

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