org.apache.fontbox.cff.CFFType1Font Maven / Gradle / Ivy
/*
* 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.fontbox.cff;
import java.awt.geom.GeneralPath;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.fontbox.EncodedFont;
import org.apache.fontbox.type1.Type1CharStringReader;
/**
* A Type 1-equivalent font program represented in a CFF file. Thread safe.
*
* @author Villu Ruusmann
* @author John Hewson
*/
public class CFFType1Font extends CFFFont implements EncodedFont
{
private final Map privateDict = new LinkedHashMap();
private CFFEncoding encoding;
private final Map charStringCache =
new ConcurrentHashMap();
private final PrivateType1CharStringReader reader = new PrivateType1CharStringReader();
/**
* Private implementation of Type1CharStringReader, because only CFFType1Font can
* expose this publicly, as CIDFonts only support this for legacy 'seac' commands.
*/
private class PrivateType1CharStringReader implements Type1CharStringReader
{
@Override
public Type1CharString getType1CharString(String name) throws IOException
{
return CFFType1Font.this.getType1CharString(name);
}
}
@Override
public GeneralPath getPath(String name) throws IOException
{
return getType1CharString(name).getPath();
}
@Override
public float getWidth(String name) throws IOException
{
return getType1CharString(name).getWidth();
}
@Override
public boolean hasGlyph(String name)
{
int sid = charset.getSID(name);
int gid = charset.getGIDForSID(sid);
return gid != 0;
}
@Override
public List getFontMatrix()
{
return (List)topDict.get("FontMatrix");
}
/**
* Returns the Type 1 charstring for the given PostScript glyph name.
*
* @param name PostScript glyph name
* @throws IOException if the charstring could not be read
*/
public Type1CharString getType1CharString(String name) throws IOException
{
// lookup via charset
int gid = nameToGID(name);
// lookup in CharStrings INDEX
return getType2CharString(gid, name);
}
/**
* Returns the GID for the given PostScript glyph name.
*
* @param name a PostScript glyph name.
* @return GID
*/
public int nameToGID(String name)
{
// some fonts have glyphs beyond their encoding, so we look up by charset SID
int sid = charset.getSID(name);
return charset.getGIDForSID(sid);
}
/**
* Returns the Type 1 charstring for the given GID.
*
* @param gid GID
* @throws IOException if the charstring could not be read
*/
@Override
public Type2CharString getType2CharString(int gid) throws IOException
{
String name = "GID+" + gid; // for debugging only
return getType2CharString(gid, name);
}
// Returns the Type 2 charstring for the given GID, with name for debugging
private Type2CharString getType2CharString(int gid, String name) throws IOException
{
Type2CharString type2 = charStringCache.get(gid);
if (type2 == null)
{
byte[] bytes = null;
if (gid < charStrings.length)
{
bytes = charStrings[gid];
}
if (bytes == null)
{
// .notdef
bytes = charStrings[0];
}
Type2CharStringParser parser = new Type2CharStringParser(fontName, name);
List