/*
 * 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 org.apache.pdfbox.cos.COSArray;
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSInteger;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.font.encoding.DictionaryEncoding;
import org.apache.pdfbox.pdmodel.font.encoding.MacRomanEncoding;
import org.apache.pdfbox.pdmodel.font.encoding.WinAnsiEncoding;

import junit.framework.TestCase;

/**
 * Tests font encoding.
 *
 */
public class TestFontEncoding extends TestCase
{
    /**
     * Test the add method of a font encoding.
     */
    public void testAdd() throws Exception
    {
        // see PDFDBOX-3332
        int codeForSpace = WinAnsiEncoding.INSTANCE.getNameToCodeMap().get("space");
        assertEquals(32, codeForSpace);

        codeForSpace = MacRomanEncoding.INSTANCE.getNameToCodeMap().get("space");
        assertEquals(32, codeForSpace);
    }

    public void testOverwrite() throws Exception
    {
        // see PDFDBOX-3332
        COSDictionary dictEncodingDict = new COSDictionary();
        dictEncodingDict.setItem(COSName.TYPE, COSName.ENCODING);
        dictEncodingDict.setItem(COSName.BASE_ENCODING, COSName.WIN_ANSI_ENCODING);
        COSArray differences = new COSArray();
        differences.add(COSInteger.get(32));
        differences.add(COSName.getPDFName("a"));
        dictEncodingDict.setItem(COSName.DIFFERENCES, differences);
        DictionaryEncoding dictEncoding = new DictionaryEncoding(dictEncodingDict, false, null);
        assertNull(dictEncoding.getNameToCodeMap().get("space"));
        assertEquals(32, dictEncoding.getNameToCodeMap().get("a").intValue());
    }
}

@@ -838,16 +838,117 @@
 .font;%0A%0A
+import java.io.ByteArrayOutputStream;%0Aimport java.io.IOException;%0A%0Aimport junit.framework.TestCase;%0A%0A
 import o
@@ -1101,16 +1101,156 @@
 OSName;%0A
+import org.apache.pdfbox.pdmodel.PDDocument;%0Aimport org.apache.pdfbox.pdmodel.PDPage;%0Aimport org.apache.pdfbox.pdmodel.PDPageContentStream;%0A
 import o
@@ -1441,40 +1441,53 @@
 ng;%0A
-%0A
 import 
-junit.framework.TestCase
+org.apache.pdfbox.text.PDFTextStripper
 ;%0A%0A/
@@ -2716,11 +2716,1087 @@
 );%0A    %7D
+%0A%0A    /**%0A     * PDFBOX-3826: Some unicodes are reached by several names in glyphlist.txt, e.g. tilde and%0A     * ilde.%0A     *%0A     * @throws IOException%0A     */%0A    public void testPDFBox3884() throws IOException%0A    %7B%0A        PDDocument doc = new PDDocument();%0A        PDPage page = new PDPage();%0A        doc.addPage(page);%0A        PDPageContentStream cs = new PDPageContentStream(doc, page);%0A        cs.setFont(PDType1Font.HELVETICA, 20);%0A        cs.beginText();%0A        cs.newLineAtOffset(100, 700);%0A        // first tilde is %22asciitilde%22 (from the keyboard), 2nd tilde is %22tilde%22%0A        // using %CB%9C would bring IllegalArgumentException prior to bugfix%0A        cs.showText(%22~%CB%9C%22);%0A        cs.endText();%0A        cs.close();%0A        ByteArrayOutputStream baos = new ByteArrayOutputStream();%0A        doc.save(baos);%0A        doc.close();%0A%0A        // verify%0A        doc = PDDocument.load(baos.toByteArray());%0A        PDFTextStripper stripper = new PDFTextStripper();%0A        String text = stripper.getText(doc);%0A        assertEquals(%22~%CB%9C%22, text.trim());%0A        doc.close();%0A    %7D
 %0A%7D%0A

add test