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.ByteArrayOutputStream;
   20   import java.util.List;
   21   
   22   /**
   23    * This class represents a formatter for CharString commands of a Type1 font.
   24    * @author Villu Ruusmann
   25    * @version $Revision: 1.0 $
   26    */
   27   public class Type1CharStringFormatter
   28   {
   29   
   30       private ByteArrayOutputStream output = null;
   31   
   32       /**
   33        * Formats the given command sequence to a byte array.
   34        * @param sequence the given command sequence
   35        * @return the formatted seuqence as byte array
   36        */
   37       public byte[] format(List<Object> sequence)
   38       {
   39           output = new ByteArrayOutputStream();
   40   
   41           for (Object object : sequence)
   42           {
   43               if (object instanceof CharStringCommand)
   44               {
   45                   writeCommand((CharStringCommand) object);
   46               } 
   47               else if (object instanceof Integer)
   48               {
   49                   writeNumber((Integer) object);
   50               } 
   51               else
   52               {
   53                   throw new IllegalArgumentException();
   54               }
   55           }
   56           return output.toByteArray();
   57       }
   58   
   59       private void writeCommand(CharStringCommand command)
   60       {
   61           int[] value = command.getKey().getValue();
   62           for (int i = 0; i < value.length; i++)
   63           {
   64               output.write(value[i]);
   65           }
   66       }
   67   
   68       private void writeNumber(Integer number)
   69       {
   70           int value = number.intValue();
   71           if (value >= -107 && value <= 107)
   72           {
   73               output.write(value + 139);
   74           } 
   75           else if (value >= 108 && value <= 1131)
   76           {
   77               int b1 = (value - 108) % 256;
   78               int b0 = (value - 108 - b1) / 256 + 247;
   79               output.write(b0);
   80               output.write(b1);
   81           } 
   82           else if (value >= -1131 && value <= -108)
   83           {
   84               int b1 = -((value + 108) % 256);
   85               int b0 = -((value + 108 + b1) / 256 - 251);
   86               output.write(b0);
   87               output.write(b1);
   88           } 
   89           else
   90           {
   91               int b1 = value >>> 24 & 0xff;
   92               int b2 = value >>> 16 & 0xff;
   93               int b3 = value >>> 8 & 0xff;
   94               int b4 = value >>> 0 & 0xff;
   95               output.write(255);
   96               output.write(b1);
   97               output.write(b2);
   98               output.write(b3);
   99               output.write(b4);
  100           }
  101       }
  102   }

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