org.apache.fontbox.type1.Type1Font 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.type1;
import java.awt.geom.GeneralPath;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.fontbox.FontBoxFont;
import org.apache.fontbox.EncodedFont;
import org.apache.fontbox.cff.Type1CharString;
import org.apache.fontbox.cff.Type1CharStringParser;
import org.apache.fontbox.encoding.Encoding;
import org.apache.fontbox.pfb.PfbParser;
import org.apache.fontbox.util.BoundingBox;
/**
* Represents an Adobe Type 1 (.pfb) font. Thread safe.
*
* @author John Hewson
*/
public final class Type1Font implements Type1CharStringReader, EncodedFont, FontBoxFont
{
/**
* Constructs a new Type1Font object from a .pfb stream.
*
* @param pfbStream .pfb input stream, including headers
* @return a type1 font
*
* @throws IOException if something went wrong
*/
public static Type1Font createWithPFB(InputStream pfbStream) throws IOException
{
PfbParser pfb = new PfbParser(pfbStream);
Type1Parser parser = new Type1Parser();
return parser.parse(pfb.getSegment1(), pfb.getSegment2());
}
/**
* Constructs a new Type1Font object from a .pfb stream.
*
* @param pfbBytes .pfb data, including headers
* @return a type1 font
*
* @throws IOException if something went wrong
*/
public static Type1Font createWithPFB(byte[] pfbBytes) throws IOException
{
PfbParser pfb = new PfbParser(pfbBytes);
Type1Parser parser = new Type1Parser();
return parser.parse(pfb.getSegment1(), pfb.getSegment2());
}
/**
* Constructs a new Type1Font object from two header-less .pfb segments.
*
* @param segment1 The first segment, without header
* @param segment2 The second segment, without header
* @return A new Type1Font instance
* @throws IOException if something went wrong
*/
public static Type1Font createWithSegments(byte[] segment1, byte[] segment2) throws IOException
{
Type1Parser parser = new Type1Parser();
return parser.parse(segment1, segment2);
}
// font dictionary
String fontName = "";
Encoding encoding = null;
int paintType;
int fontType;
List fontMatrix = new ArrayList();
List fontBBox = new ArrayList();
int uniqueID;
float strokeWidth;
String fontID = "";
// FontInfo dictionary
String version = "";
String notice = "";
String fullName = "";
String familyName = "";
String weight = "";
float italicAngle;
boolean isFixedPitch;
float underlinePosition;
float underlineThickness;
// Private dictionary
List blueValues = new ArrayList();
List otherBlues = new ArrayList();
List familyBlues = new ArrayList();
List familyOtherBlues = new ArrayList();
float blueScale;
int blueShift, blueFuzz;
List stdHW = new ArrayList();
List stdVW = new ArrayList();
List stemSnapH = new ArrayList();
List stemSnapV = new ArrayList();
boolean forceBold;
int languageGroup;
// Subrs array, and CharStrings dictionary
final List subrs = new ArrayList();
final Map charstrings = new LinkedHashMap();
// private caches
private final Map charStringCache =
new ConcurrentHashMap();
// raw data
private final byte[] segment1, segment2;
/**
* Constructs a new Type1Font, called by Type1Parser.
*/
Type1Font(byte[] segment1, byte[] segment2)
{
this.segment1 = segment1;
this.segment2 = segment2;
}
/**
* Returns the /Subrs array as raw bytes.
*
* @return Type 1 char string bytes
*/
public List getSubrsArray()
{
return Collections.unmodifiableList(subrs);
}
/**
* Returns the /CharStrings dictionary as raw bytes.
*
* @return Type 1 char string bytes
*/
public Map getCharStringsDict()
{
return Collections.unmodifiableMap(charstrings);
}
@Override
public String getName()
{
return fontName;
}
@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)
{
return charstrings.get(name) != null;
}
@Override
public Type1CharString getType1CharString(String name) throws IOException
{
Type1CharString type1 = charStringCache.get(name);
if (type1 == null)
{
byte[] bytes = charstrings.get(name);
if (bytes == null)
{
bytes = charstrings.get(".notdef");
}
Type1CharStringParser parser = new Type1CharStringParser(fontName, name);
List