Skip to content

Commit

Permalink
GH-80 text selection bounds more or less working again.
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Corless committed Jan 3, 2021
1 parent 7db5854 commit 8085313
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.icepdf.core.pobjects.*;
import org.icepdf.core.util.Library;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
Expand Down Expand Up @@ -224,6 +225,13 @@ public PRectangle getFontBBox() {
if (value instanceof List) {
List rectangle = (List) value;
return new PRectangle(rectangle);
} else if (value instanceof int[]) {
int[] ints = (int[]) value;
List<Integer> intList = new ArrayList<Integer>(ints.length);
for (int i : ints) {
intList.add(i);
}
return new PRectangle(intList);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.icepdf.core.util.Library;

import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -156,6 +157,16 @@ protected void parseWidths() {
((Number) library.getObject(entries, DW_KEY)).floatValue();
}

if (fontDescriptor != null) {
float missingWidth = fontDescriptor.getMissingWidth() / 1000f;
float ascent = fontDescriptor.getAscent() / 1000f;
float descent = fontDescriptor.getDescent() / 1000f;
Rectangle2D bbox = fontDescriptor.getFontBBox();
float[] widths = null;
font = font.deriveFont(widths, firstchar, missingWidth, ascent, descent, bbox, null);
}


}

private int calculateWidthLength(ArrayList widths) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.icepdf.core.io.SeekableInput;
import org.icepdf.core.pobjects.Name;
import org.icepdf.core.pobjects.Stream;
import org.icepdf.core.pobjects.fonts.AFM;
import org.icepdf.core.pobjects.fonts.FontManager;
import org.icepdf.core.pobjects.fonts.ofont.OFont;
import org.icepdf.core.util.FontUtil;
Expand Down Expand Up @@ -36,14 +37,15 @@ public class SimpleFont extends org.icepdf.core.pobjects.fonts.Font {
public static final Name FIRST_CHAR_KEY = new Name("FirstChar");
public static final Name DIFFERENCES_KEY = new Name("Differences");

// todo simple font props

// An array of (LastChar ? FirstChar + 1) widths, each element being the
// glyph width for the character code that equals FirstChar plus the array index.
// For character codes outside the range FirstChar to LastChar, the value
// of MissingWidth from the FontDescriptor entry for this font is used.
protected List widths;

// Base 14 AFM fonts
protected AFM afm;

// Base character mapping of 256 chars
protected String[] cMap;

Expand Down Expand Up @@ -138,10 +140,9 @@ protected void parseWidth() {
}
}
font = font.deriveFont(newWidth, firstchar, missingWidth, ascent, descent, bbox, null);
} else if (afm != null) {
font = font.deriveFont(afm.getWidths(), firstchar, missingWidth, ascent, descent, bbox, null);
}
// else if (afm != null && isAFMFont) {
// font = font.deriveFont(afm.getWidths(), firstchar, missingWidth, ascent, descent, cMap);
// }
}

protected void setBaseEncoding(Name baseEncoding) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.icepdf.core.pobjects.fonts.zfont;

import org.icepdf.core.pobjects.fonts.AFM;
import org.icepdf.core.util.Library;

import java.util.HashMap;
Expand All @@ -18,6 +19,11 @@ public Type1Font(Library library, HashMap entries) {

@Override
public void init() {
// handle afm
AFM a = AFM.AFMs.get(basefont.toLowerCase());
if (a != null && a.getFontName() != null) {
afm = a;
}
super.init();
inited = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ public Point2D echarAdvance(char ech) {
return super.echarAdvance(ech);
} else if (widths != null) {
float advance = widths[ech] * 0.001f;
advance = advance * size;// * (float) fontMatrix.getScaleX();
return new Point2D.Float(advance, 0);
} else {
return new Point2D.Float(0.001f, 0);
Expand Down Expand Up @@ -157,8 +156,7 @@ public FontFile deriveFont(float pointsize) {
ZFontTrueType font = new ZFontTrueType(this);
font.fontMatrix = convertFontMatrix(trueTypeFont);
font.fontMatrix.scale(pointsize, -pointsize);
// todo clean up size usage vs fontMatrix.
// font.size = pointsize;
font.size = pointsize;
// font.maxCharBounds = this.maxCharBounds;
return font;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ private ZFontType1C(ZFontType1C font) {
this.bbox = font.bbox;
this.widths = font.widths;
this.cMap = font.cMap;
this.size = font.size;
// this.maxCharBounds = font.maxCharBounds;
}

Expand Down Expand Up @@ -107,7 +108,6 @@ public FontFile deriveFont(Map<Integer, Float> widths, int firstCh, float missin
font.firstCh = firstCh;
font.ascent = ascent;
font.descent = descent;
font.bbox = calculateBbox(bbox);
font.cMap = diff;
font.bbox = calculateBbox(bbox);
return font;
Expand Down Expand Up @@ -138,6 +138,7 @@ public FontFile deriveFont(float pointsize) {
ZFontType1C font = new ZFontType1C(this);
font.fontMatrix = convertFontMatrix(cffType1Font);
font.fontMatrix.scale(pointsize, -pointsize);
font.size = pointsize;
// font.maxCharBounds = this.maxCharBounds;
return font;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ else if (missingWidth > 0) {
AffineTransform fontMatrix = convertFontMatrix(fontBoxFont);
advance = missingWidth / (float) fontMatrix.getScaleX();
}
advance = advance * size * (float) fontMatrix.getScaleX();
advance = advance * (float) fontMatrix.getScaleX();

return new Point2D.Float(advance, 0);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ 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 w = width;//(float)stringBounds.getWidth();
float h = (float) (font.getAscent() - font.getDescent());
Rectangle2D bbox = font.getMaxCharBounds();

double descent = font.getDescent();
double ascent = font.getAscent();
float w = width;//(float)stringBounds.getWidth();
float h = (float) (ascent - descent);

Rectangle2D bbox = font.getMaxCharBounds();

if (h <= 0.0f) {
h = (float) bbox.getHeight();
Expand All @@ -129,10 +129,10 @@ public GlyphText addText(String cid, String unicode, float x, float y, float wid
// match the width, as it will make text selection work a bit better.
h = font.getSize();
}
if (ascent == 0) {
ascent = h;
}
}
// apply fontsize.
h *= font.getSize();
descent *= font.getSize();

Rectangle2D.Float glyphBounds;
// irregular negative layout of text, need to create the bbox appropriately.
Expand Down

0 comments on commit 8085313

Please sign in to comment.