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.awt.geom.GeneralPath;
   20   import java.awt.geom.Point2D;
   21   import java.awt.geom.Rectangle2D;
   22   import java.util.List;
   23   
   24   /**
   25    * This class represents a renderer for a charstring.
   26    * @author Villu Ruusmann
   27    * @version $Revision: 1.0 $
   28    */
   29   public class CharStringRenderer extends CharStringHandler
   30   {
   31   
   32       private GeneralPath path = null;
   33       private Point2D sidebearingPoint = null;
   34       private Point2D referencePoint = null;
   35       private int width = 0;
   36   
   37       /**
   38        * Renders the given sequence and returns the result as a GeneralPath.
   39        * @param sequence the given charstring sequence
   40        * @return the rendered GeneralPath 
   41        */
   42       public GeneralPath render(List<Object> sequence)
   43       {
   44           path = new GeneralPath();
   45           sidebearingPoint = new Point2D.Float(0, 0);
   46           referencePoint = null;
   47           setWidth(0);
   48           handleSequence(sequence);
   49           return path;
   50       }
   51   
   52       /**
   53        * {@inheritDoc}
   54        */
   55       public void handleCommand(List<Integer> numbers, CharStringCommand command)
   56       {
   57           String name = CharStringCommand.TYPE1_VOCABULARY.get(command.getKey());
   58   
   59           if ("vmoveto".equals(name))
   60           {
   61               rmoveTo(Integer.valueOf(0), numbers.get(0));
   62           } 
   63           else if ("rlineto".equals(name))
   64           {
   65               rlineTo(numbers.get(0), numbers.get(1));
   66           } 
   67           else if ("hlineto".equals(name))
   68           {
   69               rlineTo(numbers.get(0), Integer.valueOf(0));
   70           } 
   71           else if ("vlineto".equals(name))
   72           {
   73               rlineTo(Integer.valueOf(0), numbers.get(0));
   74           } 
   75           else if ("rrcurveto".equals(name))
   76           {
   77               rrcurveTo(numbers.get(0), numbers.get(1), numbers.get(2), numbers
   78                       .get(3), numbers.get(4), numbers.get(5));
   79           } 
   80           else if ("closepath".equals(name))
   81           {
   82               closePath();
   83           } 
   84           else if ("sbw".equals(name))
   85           {
   86               pointSb(numbers.get(0), numbers.get(1));
   87               setWidth(numbers.get(2).intValue());
   88           } 
   89           else if ("hsbw".equals(name))
   90           {
   91               pointSb(numbers.get(0), Integer.valueOf(0));
   92               setWidth(numbers.get(1).intValue());
   93           } 
   94           else if ("rmoveto".equals(name))
   95           {
   96               rmoveTo(numbers.get(0), numbers.get(1));
   97           } 
   98           else if ("hmoveto".equals(name))
   99           {
  100               rmoveTo(numbers.get(0), Integer.valueOf(0));
  101           } 
  102           else if ("vhcurveto".equals(name))
  103           {
  104               rrcurveTo(Integer.valueOf(0), numbers.get(0), numbers.get(1),
  105                       numbers.get(2), numbers.get(3), Integer.valueOf(0));
  106           } 
  107           else if ("hvcurveto".equals(name))
  108           {
  109               rrcurveTo(numbers.get(0), Integer.valueOf(0), numbers.get(1),
  110                       numbers.get(2), Integer.valueOf(0), numbers.get(3));
  111           }
  112       }
  113   
  114       private void rmoveTo(Number dx, Number dy)
  115       {
  116           Point2D point = referencePoint;
  117           if (point == null)
  118           {
  119               point = sidebearingPoint;
  120           }
  121           referencePoint = null;
  122           path.moveTo((float)(point.getX() + dx.doubleValue()),
  123                       (float)(point.getY() + dy.doubleValue()));
  124       }
  125   
  126       private void rlineTo(Number dx, Number dy)
  127       {
  128           Point2D point = path.getCurrentPoint();
  129           path.lineTo((float)(point.getX() + dx.doubleValue()),
  130                       (float)(point.getY() + dy.doubleValue()));
  131       }
  132   
  133       private void rrcurveTo(Number dx1, Number dy1, Number dx2, Number dy2,
  134               Number dx3, Number dy3)
  135       {
  136           Point2D point = path.getCurrentPoint();
  137           float x1 = (float) point.getX() + dx1.floatValue();
  138           float y1 = (float) point.getY() + dy1.floatValue();
  139           float x2 = x1 + dx2.floatValue();
  140           float y2 = y1 + dy2.floatValue();
  141           float x3 = x2 + dx3.floatValue();
  142           float y3 = y2 + dy3.floatValue();
  143           path.curveTo(x1, y1, x2, y2, x3, y3);
  144       }
  145   
  146       private void closePath()
  147       {
  148           referencePoint = path.getCurrentPoint();
  149           path.closePath();
  150       }
  151   
  152       private void pointSb(Number x, Number y)
  153       {
  154           sidebearingPoint = new Point2D.Float(x.floatValue(), y.floatValue());
  155       }
  156   
  157       /**
  158        * Returns the bounds of the renderer path.
  159        * @return the bounds as Rectangle2D
  160        */
  161       public Rectangle2D getBounds()
  162       {
  163           return path.getBounds2D();
  164       }
  165   
  166       /**
  167        * Returns the width of the current command.
  168        * @return the width
  169        */
  170       public int getWidth()
  171       {
  172           return width;
  173       }
  174   
  175       private void setWidth(int width)
  176       {
  177           this.width = width;
  178       }
  179   }

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