/*
 * 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.
 */
package org.apache.pdfbox.pdmodel.font;

import java.awt.Font;
import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.pdfbox.cos.COSArray;
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.common.PDRectangle;

/**
 * This is implementation of the Type0 Font. 
 * See <a href="https://issues.apache.org/jira/browse/PDFBOX-605">PDFBOX-605</a>
 * for the related improvement issue.
 *
 * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
 * @version $Revision: 1.9 $
 */
public class PDType0Font extends PDSimpleFont
{

    /**
     * Log instance.
     */
    private static final Log LOG = LogFactory.getLog(PDType0Font.class);

    private COSArray descendantFontArray;
    private PDFont descendantFont;
    private COSDictionary descendantFontDictionary;
    private Font awtFont;
    /**
     * Constructor.
     */
    public PDType0Font()
    {
        super();
        font.setItem( COSName.SUBTYPE, COSName.TYPE0 );
    }

    /**
     * Constructor.
     *
     * @param fontDictionary The font dictionary according to the PDF specification.
     */
    public PDType0Font( COSDictionary fontDictionary )
    {
        super( fontDictionary );
        descendantFontDictionary = (COSDictionary)getDescendantFonts().getObject( 0 );
        if (descendantFontDictionary != null)
        {
            try 
            {
                descendantFont = PDFontFactory.createFont( descendantFontDictionary );
            }
            catch (IOException exception)
            {
                LOG.error("Error while creating the descendant font!");
            }
        }
    }

    /**
     * {@inheritDoc}
     */
    public Font getawtFont() throws IOException
    {
        if (awtFont == null)
        {
            if (descendantFont != null) 
            {
                awtFont = ((PDSimpleFont)descendantFont).getawtFont();
            }
            if (awtFont == null)
            {
                awtFont = FontManager.getStandardFont();
                LOG.info("Using font "+awtFont.getName()
                        + " instead of "+descendantFont.getFontDescriptor().getFontName());
                setIsFontSubstituted(true);
            }
        }
        return awtFont;
    }
    
    /**
     * This will get the fonts bounding box.
     *
     * @return The fonts bounding box.
     *
     * @throws IOException If there is an error getting the bounding box.
     */
    public PDRectangle getFontBoundingBox() throws IOException
    {
        throw new RuntimeException( "Not yet implemented" );
    }

    /**
     * This will get the font width for a character.
     *
     * @param c The character code to get the width for.
     * @param offset The offset into the array.
     * @param length The length of the data.
     *
     * @return The width is in 1000 unit of text space, ie 333 or 777
     *
     * @throws IOException If an error occurs while parsing.
     */
    public float getFontWidth( byte[] c, int offset, int length ) throws IOException
    {
        return descendantFont.getFontWidth( c, offset, length );
    }

    /**
     * This will get the font height for a character.
     *
     * @param c The character code to get the height for.
     * @param offset The offset into the array.
     * @param length The length of the data.
     *
     * @return The width is in 1000 unit of text space, ie 333 or 777
     *
     * @throws IOException If an error occurs while parsing.
     */
    public float getFontHeight( byte[] c, int offset, int length ) throws IOException
    {
        return descendantFont.getFontHeight( c, offset, length );
    }

    /**
     * This will get the average font width for all characters.
     *
     * @return The width is in 1000 unit of text space, ie 333 or 777
     *
     * @throws IOException If an error occurs while parsing.
     */
    public float getAverageFontWidth() throws IOException
    {
        return descendantFont.getAverageFontWidth();
    }
 
    private COSArray getDescendantFonts()
    {
        if (descendantFontArray == null)
        {
            descendantFontArray = (COSArray)font.getDictionaryObject( COSName.DESCENDANT_FONTS );
        }
        return descendantFontArray;
    }
    
    /**
     * {@inheritDoc}
     */
    public float getFontWidth( int charCode ) 
    {
        return descendantFont.getFontWidth(charCode);
    }

    @Override
    public String encode(byte[] c, int offset, int length) throws IOException
    {
    	String retval = null;
        if (hasToUnicode())
        {
            retval = super.encode(c, offset, length);
        }
        
        if (retval == null)
        {
            int result = cmap.lookupCID(c, offset, length);
            if (result != -1) 
            {
                retval = descendantFont.cmapEncoding(result, 2, true, cmap);
            }
        }
        return retval;
    }

    /**
     * 
     * Provides the descendant font.
     * @return the descendant font.
     * 
     */
    protected PDFont getDescendantFont() 
    {
        return descendantFont;
    }
}

@@ -1196,17 +1196,16 @@
 e0 Font.
- 
 %0A * See 
@@ -2263,17 +2263,16 @@
      try
- 
 %0A       
@@ -2707,17 +2707,16 @@
 != null)
- 
 %0A       
@@ -2823,33 +2823,33 @@
     if (awtFont 
-=
+!
 = null)%0A        
@@ -2846,32 +2846,167 @@
 )%0A            %7B%0A
+                setIsFontSubstituted(((PDSimpleFont)descendantFont).isFontSubstituted());%0A            %7D%0A            else%0A            %7B%0A
                 
@@ -3285,36 +3285,32 @@
  awtFont;%0A    %7D%0A
-    
 %0A    /**%0A     * 
@@ -5039,17 +5039,16 @@
 ;%0A    %7D%0A
- 
 %0A    pri
@@ -5289,20 +5289,16 @@
 ;%0A    %7D%0A
-    
 %0A    /**
@@ -5372,17 +5372,16 @@
 arCode )
- 
 %0A    %7B%0A 
@@ -5667,24 +5667,16 @@
       %7D%0A
-        
 %0A       
@@ -5795,17 +5795,16 @@
 t != -1)
- 
 %0A       
@@ -5951,25 +5951,24 @@
   /**%0A     *
- 
 %0A     * Prov
@@ -6035,17 +6035,16 @@
 .%0A     *
- 
 %0A     */
@@ -6084,17 +6084,16 @@
 ntFont()
- 
 %0A    %7B%0A 

Fix for wrong glyphs being painted when a CIDFontType2 for a subset TTF is substituted.