Skip to content

Commit

Permalink
GH-182 potential fix to derive ascent/descent from bbox if not set fr…
Browse files Browse the repository at this point in the history
…om font descriptor.
  • Loading branch information
Patrick Corless committed Apr 21, 2021
1 parent 2036db1 commit b9acbd2
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ enum ByteEncoding {

long LAYOUT_NONE = 0;

// todo do some refacotring of method name 'e' awlays seemed strange to me.

Point2D getAdvance(char ech);

FontFile deriveFont(AffineTransform at);
Expand All @@ -54,12 +52,12 @@ FontFile deriveFont(float[] widths, int firstCh, float missingWidth,
FontFile deriveFont(Map<Integer, Float> widths, int firstCh, float missingWidth,
float ascent, float descent, Rectangle2D bbox, char[] diff);

FontFile deriveFont(float pointSize);

boolean canDisplay(char ech);

void setIsCid();

FontFile deriveFont(float pointsize);

CMap getToUnicode();

String toUnicode(String displayText);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ protected void parseWidth() {
font = font.deriveFont(newWidth, firstchar, missingWidth, ascent, descent, bbox, null);
}
// currently not using afm, instead using font's width table, seems more reliable
else if (afm != null && !isFontSubstitution) {
else if (afm != null) {
font = font.deriveFont(afm.getWidths(), firstchar, missingWidth, ascent, descent, bbox, null);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ public FontFile deriveFont(float[] widths, int firstCh, float missingWidth, floa
font.widths = widths;
font.cMap = diff;
font.bbox = bbox;
font.maxCharBounds = null;
return font;
}

Expand All @@ -198,6 +199,7 @@ public FontFile deriveFont(Map<Integer, Float> widths, int firstCh, float missin
font.descent = descent;
font.cMap = diff;
font.bbox = bbox;
font.maxCharBounds = null;
return font;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ public FontFile deriveFont(float[] widths, int firstCh, float missingWidth, floa
font.widths = widths;
font.cMap = diff != null ? diff : font.cMap;
font.bbox = bbox;
font.maxCharBounds = null;
return font;
}

Expand All @@ -181,6 +182,7 @@ public FontFile deriveFont(Map<Integer, Float> widths, int firstCh, float missin
font.descent = descent;
font.cMap = diff;
font.bbox = bbox;
font.maxCharBounds = null;
return font;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public FontFile deriveFont(Map<Integer, Float> widths, int firstCh, float missin
font.descent = descent;
font.cMap = diff;
font.bbox = bbox;
font.maxCharBounds = null;
return font;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public FontFile deriveFont(float[] widths, int firstCh, float missingWidth, floa
font.widths = widths;
font.cMap = diff != null ? diff : font.cMap;
font.bbox = bbox;
font.maxCharBounds = null;
return font;
}

Expand All @@ -110,6 +111,7 @@ public FontFile deriveFont(Map<Integer, Float> widths, int firstCh, float missin
font.descent = descent;
font.cMap = diff;
font.bbox = bbox;
font.maxCharBounds = null;
return font;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public FontFile deriveFont(float[] widths, int firstCh, float missingWidth, floa
font.descent = descent;
font.cMap = diff;
font.bbox = bbox;
font.maxCharBounds = null;
return font;
}

Expand All @@ -164,6 +165,7 @@ public FontFile deriveFont(Map<Integer, Float> widths, int firstCh, float missin
font.descent = descent;
font.cMap = diff;
font.bbox = bbox;
font.maxCharBounds = null;
return font;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public FontFile deriveFont(float[] widths, int firstCh, float missingWidth, floa
font.ascent = ascent;
font.descent = descent;
font.bbox = bbox;
font.maxCharBounds = null;
return font;
}

Expand All @@ -123,6 +124,7 @@ public FontFile deriveFont(Map<Integer, Float> widths, int firstCh, float missin
font.ascent = ascent;
font.descent = descent;
font.bbox = bbox;
font.maxCharBounds = null;
return font;
}

Expand Down Expand Up @@ -221,27 +223,6 @@ public Rectangle2D getMaxCharBounds() {
return af.createTransformedShape(bBox.toJava2dCoordinates()).getBounds2D();
}

public Rectangle2D getCharBounds(char displayChar) {
Rectangle2D r = getMaxCharBounds();

String charName = encoding.getName(displayChar);
float width = 0f;
if (widths != null && displayChar - firstCh >= 0 && displayChar - firstCh < widths.length) {
width = widths[displayChar - firstCh];

}

if (width == 0.0f) {
width = charWidths.get(charName).x;
}

PRectangle charRect = charBBoxes.get(charName);
r.setRect(0.0, r.getY(),
width * size,
charRect.getHeight() * size);
return r;
}

/**
* <p>Sets the horizontal displacement of this Type3 font for the specified
* name. Like BBox this value must be set from inside the content parser as
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public abstract class ZSimpleFont implements FontFile {
protected float ascent;
protected float descent;
protected Rectangle2D bbox = new Rectangle2D.Double(0.0, 0.0, 1.0, 1.0);
protected Rectangle2D maxCharBounds;

// cid specific, todo new subclass if we get a few more?
protected float defaultWidth;
Expand Down Expand Up @@ -186,7 +187,10 @@ public Shape getOutline(String estr, float x, float y) {

@Override
public Rectangle2D getMaxCharBounds() {
return calculateBbox(bbox);
AffineTransform af = new AffineTransform();
af.scale(size, -size);
af.concatenate(fontMatrix);
return af.createTransformedShape(bbox).getBounds2D();
}

@Override
Expand Down Expand Up @@ -225,12 +229,34 @@ public float getSize() {

@Override
public double getAscent() {
return ascent;
if (ascent != 0) {
return ascent * size;
} else {
if (maxCharBounds == null) {
maxCharBounds = getMaxCharBounds();
}
return -maxCharBounds.getY();
}

}

public double getHeight() {
if (maxCharBounds == null) {
maxCharBounds = getMaxCharBounds();
}
return maxCharBounds.getHeight();
}


@Override
public double getDescent() {
return descent;
if (descent != 0) {
return descent * size;
} else {
double height = getHeight();
double ascent = getAscent();
return height - ascent;
}
}

@Override
Expand Down Expand Up @@ -309,6 +335,7 @@ protected void setPointSize(float pointSize) {
fontTransform.concatenate(gsTransform);
fontTransform.scale(pointSize, -pointSize);
size = pointSize;
maxCharBounds = null;
}

protected char getCharDiff(char character) {
Expand All @@ -319,16 +346,6 @@ protected char getCharDiff(char character) {
}
}

protected Rectangle2D calculateBbox(Rectangle2D bbox) {
if (bbox != null) {
AffineTransform af = new AffineTransform();
// af.scale(size, size);
af.concatenate(fontMatrix);
return af.createTransformedShape(bbox).getBounds2D();
}
return this.bbox;
}

protected AffineTransform convertFontMatrix(FontBoxFont fontBoxFont) {
try {
java.util.List<Number> matrix = fontBoxFont.getFontMatrix();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ public GlyphText addText(String cid, String unicode, float x, float y, float wid
// we can change the bounds of glyphBounds as this is what needs to be normalized
// to page space
// IMPORTANT: where working in Java Coordinates with any of the Font bounds
float descent = (float) font.getDescent() * font.getSize();
float ascent = (float) font.getAscent() * font.getSize();
float descent = (float) font.getDescent();
float ascent = (float) font.getAscent();
float w = width;
float h = ascent - descent;
// width/height are kept unscaled for coords, w/h are scaled to get correct bounds w/h
Expand Down

0 comments on commit b9acbd2

Please sign in to comment.