
package com.tom_roush.fontbox.cff;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assume.assumeTrue;

/**
 *
 * @author Petr Slaby
 */
public class CFFParserTest
{
    /**
     * PDFBOX-4038: Test whether BlueValues and other delta encoded lists are read correctly. The
     * test file is from FOP-2432.
     *
     * @throws IOException
     */
    @Test
//    TODO: PdfBox-Android - provide test file
    public void testDeltaLists() throws IOException
    {
        String fontPath = "target/pdfs/SourceSansProBold.otf";
        assumeTrue(new File(fontPath).exists());
        List<CFFFont> fonts = readFont(new FileInputStream(fontPath));
        CFFType1Font font = (CFFType1Font) fonts.get(0);
        @SuppressWarnings("unchecked")
        List<Number> blues = (List<Number>)font.getPrivateDict().get("BlueValues");

        // Expected values found for this font
        assertNumberList("Blue values are different than expected: " + blues.toString(),
            new int[]{-12, 0, 496, 508, 578, 590, 635, 647, 652, 664, 701, 713}, blues);

        @SuppressWarnings("unchecked")
        List<Number> otherBlues = (List<Number>)font.getPrivateDict().get("OtherBlues");
        assertNumberList("Other blues are different than expected: " + otherBlues.toString(),
            new int[]{-196, -184}, otherBlues);

        @SuppressWarnings("unchecked")
        List<Number> familyBlues = (List<Number>)font.getPrivateDict().get("FamilyBlues");
        assertNumberList("Other blues are different than expected: " + familyBlues.toString(),
            new int[]{-12, 0, 486, 498, 574, 586, 638, 650, 656, 668, 712, 724}, familyBlues);

        @SuppressWarnings("unchecked")
        List<Number> familyOtherBlues = (List<Number>)font.getPrivateDict().get("FamilyOtherBlues");
        assertNumberList("Other blues are different than expected: " + familyOtherBlues.toString(),
            new int[]{-217, -205}, familyOtherBlues);

        @SuppressWarnings("unchecked")
        List<Number> stemSnapH = (List<Number>)font.getPrivateDict().get("StemSnapH");
        assertNumberList("StemSnapH values are different than expected: " + stemSnapH.toString(),
            new int[]{115}, stemSnapH);

        @SuppressWarnings("unchecked")
        List<Number> stemSnapV = (List<Number>)font.getPrivateDict().get("StemSnapV");
        assertNumberList("StemSnapV values are different than expected: " + stemSnapV.toString(),
            new int[]{146, 150}, stemSnapV);
    }

    private List<CFFFont> readFont(InputStream in) throws IOException
    {
        ByteArrayOutputStream content = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > -1)
        {
            content.write(buf, 0, len);
        }

        CFFParser parser = new CFFParser();
        return parser.parse(content.toByteArray());
    }

    private void assertNumberList(String message, int[] expected, List<Number> found)
    {
        assertEquals(message, expected.length, found.size());
        for (int i = 0; i < expected.length; i++)
        {
            assertEquals(message, expected[i], found.get(i).intValue());
        }
    }
}
