Skip to content

Commit

Permalink
GH-80 round the board improvements to glyph selection and widths.
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Corless committed Mar 19, 2021
1 parent b491e80 commit 44fb386
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ public interface Encoding {

String getName(int code);

char getChar(String name);
Character getChar(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public String getName(int code) {
}

@Override
public char getChar(String name) {
public Character getChar(String name) {
return 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ protected void parseWidths() {
if (currentNext instanceof ArrayList) {
ArrayList widths2 = (ArrayList) currentNext;
for (int j = 0, max2 = widths2.size(); j < max2; j++) {
widths[current + j] = (float) (((Number) widths2.get(j)).intValue());
widths[current + j] = (float) (((Number) widths2.get(j)).intValue()) * 0.001f;
}
i++;
} else if (currentNext instanceof Number) {
int currentEnd = ((Number) currentNext).intValue();
float width2 = (float) (((Number) individualWidths.get(i + 2)).intValue());
for (; current <= currentEnd; current++) {
widths[current] = width2;
widths[current] = width2 * 0.001f;
}
i += 2;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,15 @@ public class Encoding implements org.icepdf.core.pobjects.fonts.Encoding {
public static Encoding symbolEncoding;
public static Encoding zapfDingBats;

public static Encoding identity;

static {
initializeLatinEncodings();
initializeOddBallEncodings();
}

private String name;
private String[] encodingMap;
private final String name;
private final String[] encodingMap;

private Encoding(String name, String[] encodingMap) {
this.name = name;
Expand All @@ -95,7 +97,6 @@ private Encoding(String name, String[] encodingMap) {
public Encoding(Encoding base, String[] diff) {
this.name = "diff";
String[] cMap = base.encodingMap.clone();
String name;
for (int code = 0, max = diff.length; code < max; code++) {
cMap[code] = diff[code];
}
Expand All @@ -107,7 +108,6 @@ public Encoding(org.apache.fontbox.encoding.Encoding encoding) {
this.name = "embedded-font";
// this is just a guess, will need to see an example if a simple font really only has 256 max.
String[] cMap = new String[256];
String name;
for (int code = 0, max = cMap.length; code < max; code++) {
cMap[code] = fontBoxEncoding.get(code);
}
Expand All @@ -125,13 +125,13 @@ public String getName(int code) {
return ".notdef";
}

public char getChar(String name) {
char ch = 0;
public Character getChar(String name) {
Character ch = null;
// simple check to see if we have a /Euro or /euro
if (name.equalsIgnoreCase("euro")) {
name = "Euro";
}
boolean isEuro = name.equalsIgnoreCase("euro");
// boolean isEuro = name.equalsIgnoreCase("euro");
for (int i = 0, max = encodingMap.length; i < max; i++) {
if (name.equals(encodingMap[i])) {
ch = (char) i;
Expand Down Expand Up @@ -164,8 +164,7 @@ public static Encoding getInstance(String name) {
} else if (SYMBOL_ENCODING_NAME.equals(name)) {
return symbolEncoding;
} else {
// todo return identity
return null;
return identity;
}
}

Expand All @@ -176,6 +175,7 @@ private static void initializeLatinEncodings() {
macRomanEncoding = new Encoding(MAC_ROMAN_ENCODING_NAME, mappings[1]);
winAnsiEncoding = new Encoding(WIN_ANSI_ENCODING_NAME, mappings[2]);
pdfDocEncoding = new Encoding(PDF_DOC_ENCODING_NAME, mappings[3]);
identity = new Encoding("Identity", new String[256]);
}

private static void initializeOddBallEncodings() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ protected void parseEncoding() {
encoding = new Encoding(encoding, cMap);
} else if (encodingValue instanceof Name) {
setBaseEncoding((Name) encodingValue);
} else {
encoding = Encoding.standardEncoding;
}
font = font.deriveFont(encoding, toUnicodeCMap);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public void init() {
}

protected void parseEncoding() {
Object encoding = library.getName(entries, ENCODING_KEY);
if (encoding instanceof Name) {
cMap = CMap.getInstance((Name) encoding);
// todo clean up encoding and fix font substitution
font = font.deriveFont(null, cMap);
Name name = library.getName(entries, ENCODING_KEY);
if (name != null) {
cMap = CMap.getInstance(name);
Encoding encoding = Encoding.getInstance((name).getName());
font = font.deriveFont(encoding, cMap);
}
if (cMap != null) {
isCMapPredefined = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ public void init() {
}
}
super.init();

if (encoding == null) {
encoding = Encoding.standardEncoding;
font = font.deriveFont(encoding, toUnicodeCMap);
}

inited = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ public Point2D echarAdvance(char ech) {
int glyphId = ech - firstCh;
if (trueTypeFont == null || gid > trueTypeFont.getNumberOfGlyphs()) {
return new Point2D.Float(advance, 0);
} else if (widths != null && glyphId >= 0 && glyphId < widths.length && widths[glyphId] > 1) {
advance = widths[glyphId] * 0.001f;
} else if (widths != null && glyphId >= 0 && glyphId < widths.length && widths[glyphId] <= 1) {
advance = widths[glyphId];
}
Expand All @@ -111,23 +109,31 @@ public void drawEstring(Graphics2D g, String estr, float x, float y, long layout
char echar = estr.charAt(0);

Shape outline;
int gid;
if (trueTypeFont instanceof OpenTypeFont) {
int cid = codeToGID(echar);
Type2CharString charstring = ((OpenTypeFont) trueTypeFont).getCFF().getFont().getType2CharString(cid);
outline = charstring.getPath();
} else {
int gid = getCharToGid(echar);
gid = getCharToGid(echar);
GlyphData glyphData = trueTypeFont.getGlyph().getGlyph(gid);
if (glyphData == null) {
outline = new GeneralPath();
} else {
// must scaled by caller using FontMatrix
outline = glyphData.getPath();
}
}
g.translate(x, y);
g.transform(this.fontTransform);

// apply font scaling experiment
// int glyphId = echar - firstCh;
// int metricsWidth = horizontalMetricsTable.getAdvanceWidth(
// Math.min(gid, (int) horizontalMetricsTable.getLength() - 1));
// double width = widths != null && 0 <= glyphId && glyphId < widths.length && widths[glyphId] > 1 ? widths[glyphId] * 0.001f / fontTransform.getScaleX() : 0.0;
// double scale = metricsWidth / width;
// System.out.println(scale);

if (TextState.MODE_FILL == mode || TextState.MODE_FILL_STROKE == mode ||
TextState.MODE_FILL_ADD == mode || TextState.MODE_FILL_STROKE_ADD == mode) {
g.fill(outline);
Expand Down Expand Up @@ -175,8 +181,16 @@ public FontFile deriveFont(float pointSize) {
@Override
public FontFile deriveFont(Encoding encoding, CMap toUnicode) {
ZFontTrueType font = new ZFontTrueType(this);
font.encoding = encoding;
font.toUnicode = toUnicode;
if (encoding != null) {
font.encoding = encoding;
}
if (toUnicode != null) {
font.toUnicode = toUnicode;
}
if (font.toUnicode == null) {
font.toUnicode = org.icepdf.core.pobjects.fonts.ofont.CMap.IDENTITY;
}

return font;
}

Expand Down Expand Up @@ -255,31 +269,29 @@ public int codeToGID(int code) {
int gid = 0;
try {
if (cmapWinSymbol == null) {
String name = encoding != null ? encoding.getName(code) : ".notdef2"; // todo fix hack
if (".notdef".equals(name)) {
return cmapWinUnicode.getGlyphId(code); // null
} else {
// (3, 1) - (Windows, Unicode)
if (cmapWinUnicode != null) {
String unicode = GlyphList.getAdobeGlyphList().toUnicode(name);
if (unicode != null) {
int uni = unicode.codePointAt(0);
gid = cmapWinUnicode.getGlyphId(uni);
}
}
// (1, 0) - (Macintosh, Roman)
if (gid == 0 && cmapMacRoman != null) {
String macCode = GlyphList.getAdobeGlyphList().toUnicode(name);// INVERTED_MACOS_ROMAN.get(name);
if (macCode != null) {
int uni = macCode.codePointAt(0);
gid = cmapMacRoman.getGlyphId(uni);
}
String name = encoding != null ? encoding.getName(code) : ".notdef";
if (encoding == null) {
return cmapMacRoman.getGlyphId(code);
}
// (3, 1) - (Windows, Unicode)
if (cmapWinUnicode != null && name != null) {
String unicode = GlyphList.getAdobeGlyphList().toUnicode(name);
if (unicode != null) {
int uni = unicode.codePointAt(0);
gid = cmapWinUnicode.getGlyphId(uni);
}
// 'post' table
if (gid == 0) {
gid = code;//trueTypeFont.nameToGID(name);
}
// (1, 0) - (Macintosh, Roman)
if (gid == 0 && cmapMacRoman != null && name != null) {
Character macCode = org.icepdf.core.pobjects.fonts.zfont.Encoding.macRomanEncoding.getChar(name);
if (macCode != null) {
gid = cmapMacRoman.getGlyphId(macCode);
}
}
// 'post' table
if (gid == 0) {
gid = code;
}
}
// symbolic
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,12 @@ private ZFontType0(ZFontType0 font) {
@Override
public Point2D echarAdvance(char ech) {
float advance = defaultWidth;
if (encoding != null) {
return super.echarAdvance(ech);
} else if (widths != null && ech < widths.length) {
float width = widths[ech];
if (width <= 1) {
advance = width;
} else {
advance = width * 0.001f;
}
if (widths != null && ech < widths.length) {
advance = widths[ech];
}
if (advance == 0) {
advance = 1.0f;
}

float x = advance * size;//* (float) gsTransform.getScaleX();
float y = advance * size;//* (float) gsTransform.getShearY();
return new Point2D.Float(x, y);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,13 @@ private ZFontType2(ZFontType2 font) {

@Override
public Point2D echarAdvance(char ech) {
// todo, needs som more work.
float advance = defaultWidth;
if (encoding != null) {
return super.echarAdvance(ech);
} else if (widths != null && ech < widths.length) {
// int gid = getCharToGid(ech);
float width = widths[ech];
if (width <= 1) {
advance = width;
} else {
advance = width * 0.001f;
}
if (widths != null && ech < widths.length) {
advance = widths[ech];
}
if (advance == 0) {
advance = 1.0f;
}

float x = advance * size;//* (float) gsTransform.getScaleX();
float y = advance * size;//* (float) gsTransform.getShearY();
return new Point2D.Float(x, y);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,9 @@ apple - 360 - -
# additions necessary in practice
shy - - 255 -
# white space mappings, octal values. for 9,10,12 dec. OCT values.
space 011 011 011 011
space 012 012 012 012
space 013 013 013 013
space 014 014 014 014
space 015 015 015 015
space 240 240 240 240
# space 011 011 011 011
# space 012 012 012 012
# space 013 013 013 013
# space 014 014 014 014
# space 015 015 015 015
# space 240 240 240 240

0 comments on commit 44fb386

Please sign in to comment.