Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 9 additions & 122 deletions fontbox/src/main/java/org/apache/fontbox/cff/CFFCharset.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,140 +16,59 @@
*/
package org.apache.fontbox.cff;

import java.util.HashMap;
import java.util.Map;

/**
* A CFF charset. A charset is an array of SIDs/CIDs for all glyphs in the font.
*
* todo: split this into two? CFFCharsetType1 and CFFCharsetCID ?
*
* @author John Hewson
*/
public abstract class CFFCharset
{
private final boolean isCIDFont;
private final Map<Integer, Integer> sidOrCidToGid = new HashMap<>(250);
private final Map<Integer, Integer> gidToSid = new HashMap<>(250);
private final Map<String, Integer> nameToSid = new HashMap<>(250);

// inverse
private final Map<Integer, Integer> gidToCid = new HashMap<>();
private final Map<Integer, String> gidToName = new HashMap<>(250);

/**
* Package-private constructor for use by subclasses.
*
* @param isCIDFont true if the parent font is a CIDFont
*/
CFFCharset(boolean isCIDFont)
{
this.isCIDFont = isCIDFont;
}

/**
* Indicates if the charset belongs to a CID font.
*
* @return true for CID fonts
*/
public boolean isCIDFont()
{
return isCIDFont;
}
public abstract boolean isCIDFont();

/**
* Adds a new GID/SID/name combination to the charset.
*
* @param gid GID
* @param sid SID
*/
public void addSID(int gid, int sid, String name)
{
if (isCIDFont)
{
throw new IllegalStateException("Not a Type 1-equivalent font");
}
sidOrCidToGid.put(sid, gid);
gidToSid.put(gid, sid);
nameToSid.put(name, sid);
gidToName.put(gid, name);
}
public abstract void addSID(int gid, int sid, String name);

/**
* Adds a new GID/CID combination to the charset.
*
* @param gid GID
* @param cid CID
*/
public void addCID(int gid, int cid)
{
if (!isCIDFont)
{
throw new IllegalStateException("Not a CIDFont");
}
sidOrCidToGid.put(cid, gid);
gidToCid.put(gid, cid);
}
public abstract void addCID(int gid, int cid);

/**
* Returns the SID for a given GID. SIDs are internal to the font and are not public.
*
* @param sid SID
* @return GID
*/
int getSIDForGID(int sid)
{
if (isCIDFont)
{
throw new IllegalStateException("Not a Type 1-equivalent font");
}
Integer gid = gidToSid.get(sid);
if (gid == null)
{
return 0;
}
return gid;
}
abstract int getSIDForGID(int sid);

/**
* Returns the GID for the given SID. SIDs are internal to the font and are not public.
*
* @param sid SID
* @return GID
*/
int getGIDForSID(int sid)
{
if (isCIDFont)
{
throw new IllegalStateException("Not a Type 1-equivalent font");
}
Integer gid = sidOrCidToGid.get(sid);
if (gid == null)
{
return 0;
}
return gid;
}
abstract int getGIDForSID(int sid);

/**
* Returns the GID for a given CID. Returns 0 if the CID is missing.
*
* @param cid CID
* @return GID
*/
public int getGIDForCID(int cid)
{
if (!isCIDFont)
{
throw new IllegalStateException("Not a CIDFont");
}
Integer gid = sidOrCidToGid.get(cid);
if (gid == null)
{
return 0;
}
return gid;
}
public abstract int getGIDForCID(int cid);

/**
* Returns the SID for a given PostScript name, you would think this is not needed,
Expand All @@ -158,53 +77,21 @@ public int getGIDForCID(int cid)
* @param name PostScript glyph name
* @return SID
*/
int getSID(String name)
{
if (isCIDFont)
{
throw new IllegalStateException("Not a Type 1-equivalent font");
}
Integer sid = nameToSid.get(name);
if (sid == null)
{
return 0;
}
return sid;
}
abstract int getSID(String name);

/**
* Returns the PostScript glyph name for the given GID.
*
* @param gid GID
* @return PostScript glyph name
*/
public String getNameForGID(int gid)
{
if (isCIDFont)
{
throw new IllegalStateException("Not a Type 1-equivalent font");
}
return gidToName.get(gid);
}
public abstract String getNameForGID(int gid);

/**
* Returns the CID for the given GID.
*
* @param gid GID
* @return CID
*/
public int getCIDForGID(int gid)
{
if (!isCIDFont)
{
throw new IllegalStateException("Not a CIDFont");
}

Integer cid = gidToCid.get(gid);
if (cid != null)
{
return cid;
}
return 0;
}
public abstract int getCIDForGID(int gid);
}
136 changes: 136 additions & 0 deletions fontbox/src/main/java/org/apache/fontbox/cff/CFFCharsetCID.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package org.apache.fontbox.cff;

import java.util.HashMap;
import java.util.Map;

/**
* A CFF charset. A charset is an array of CIDs for all glyphs in the font.
*
* @author Valery Bokov
*/
class CFFCharsetCID extends CFFCharset
{
private final Map<Integer, Integer> sidOrCidToGid = new HashMap<>(250);

// inverse
private final Map<Integer, Integer> gidToCid = new HashMap<>();

/**
* Indicates if the charset belongs to a CID font.
*
* @return true for CID fonts
*/
@Override
public boolean isCIDFont()
{
return true;
}

/**
* Adds a new GID/SID/name combination to the charset.
*
* @param gid GID
* @param sid SID
*/
@Override
public void addSID(int gid, int sid, String name)
{
throw new IllegalStateException("Not a Type 1-equivalent font");
}

/**
* Adds a new GID/CID combination to the charset.
*
* @param gid GID
* @param cid CID
*/
@Override
public void addCID(int gid, int cid)
{
sidOrCidToGid.put(cid, gid);
gidToCid.put(gid, cid);
}

/**
* Returns the SID for a given GID. SIDs are internal to the font and are not public.
*
* @param sid SID
* @return GID
*/
@Override
int getSIDForGID(int sid)
{
throw new IllegalStateException("Not a Type 1-equivalent font");
}

/**
* Returns the GID for the given SID. SIDs are internal to the font and are not public.
*
* @param sid SID
* @return GID
*/
@Override
int getGIDForSID(int sid)
{
throw new IllegalStateException("Not a Type 1-equivalent font");
}

/**
* Returns the GID for a given CID. Returns 0 if the CID is missing.
*
* @param cid CID
* @return GID
*/
@Override
public int getGIDForCID(int cid)
{
Integer gid = sidOrCidToGid.get(cid);
if (gid == null)
{
return 0;
}
return gid;
}

/**
* Returns the SID for a given PostScript name, you would think this is not needed,
* but some fonts have glyphs beyond their encoding with charset SID names.
*
* @param name PostScript glyph name
* @return SID
*/
@Override
int getSID(String name)
{
throw new IllegalStateException("Not a Type 1-equivalent font");
}

/**
* Returns the PostScript glyph name for the given GID.
*
* @param gid GID
* @return PostScript glyph name
*/
@Override
public String getNameForGID(int gid)
{
throw new IllegalStateException("Not a Type 1-equivalent font");
}

/**
* Returns the CID for the given GID.
*
* @param gid GID
* @return CID
*/
@Override
public int getCIDForGID(int gid)
{
Integer cid = gidToCid.get(gid);
if (cid != null)
{
return cid;
}
return 0;
}
}
Loading