Skip to content

Commit

Permalink
GH-80 further toUnicode and text selection improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Corless committed Mar 5, 2021
1 parent ab1e916 commit 2acbe50
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public TrueTypeFont(Library library, HashMap entries) {
public synchronized void init() {
super.init();
inited = true;
if (encoding == null) {
encoding = Encoding.standardEncoding;
font = font.deriveFont(encoding, toUnicodeCMap);
}
}


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

Expand All @@ -215,7 +215,7 @@ public FontFile deriveFont(Map<Integer, Float> widths, int firstCh, float missin
font.ascent = ascent;
font.descent = descent;
font.cMap = diff;
font.bbox = calculateBbox(bbox);
font.bbox = bbox;
return font;
}

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

Expand All @@ -181,7 +181,7 @@ public FontFile deriveFont(Map<Integer, Float> widths, int firstCh, float missin
font.ascent = ascent;
font.descent = descent;
font.cMap = diff;
font.bbox = calculateBbox(bbox);
font.bbox = bbox;
return font;
// } else {
// return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public FontFile deriveFont(float[] widths, int firstCh, float missingWidth, floa
font.descent = descent;
font.widths = widths;
font.cMap = diff != null ? diff : font.cMap;
font.bbox = calculateBbox(bbox);
font.bbox = bbox;
return font;
}

Expand All @@ -126,7 +126,7 @@ public FontFile deriveFont(Map<Integer, Float> widths, int firstCh, float missin
font.ascent = ascent;
font.descent = descent;
font.cMap = diff;
font.bbox = calculateBbox(bbox);
font.bbox = bbox;
return font;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,9 @@ public FontFile deriveFont(float[] widths, int firstCh, float missingWidth, floa
font.firstCh = firstCh;
font.ascent = ascent;
font.descent = descent;
font.bbox = calculateBbox(bbox);
font.widths = widths;
font.cMap = diff != null ? diff : font.cMap;
font.bbox = calculateBbox(bbox);
font.bbox = bbox;
return font;
}

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

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

Expand All @@ -166,7 +166,7 @@ public FontFile deriveFont(Map<Integer, Float> widths, int firstCh, float missin
font.ascent = ascent;
font.descent = descent;
font.cMap = diff;
font.bbox = calculateBbox(bbox);
font.bbox = bbox;
return font;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public FontFile deriveFont(float[] widths, int firstCh, float missingWidth, floa
font.missingWidth = missingWidth;
font.ascent = ascent;
font.descent = descent;
font.bbox = calculateBbox(bbox);
font.bbox = bbox;
// todo diff cmap, likely not a type3 thing, see if an example shows up.
return font;
}
Expand All @@ -128,7 +128,7 @@ public FontFile deriveFont(Map<Integer, Float> widths, int firstCh, float missin
font.missingWidth = missingWidth;
font.ascent = ascent;
font.descent = descent;
font.bbox = calculateBbox(bbox);
font.bbox = bbox;
return font;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
Expand Down Expand Up @@ -147,9 +148,33 @@ public void drawEstring(Graphics2D g, String estr, float x, float y, long layout
}
}

@Override
public Shape getEstringOutline(String estr, float x, float y) {
try {
String name = codeToName(estr);
Shape glyph = fontBoxFont.getPath(name);
if (encoding != null && !fontBoxFont.hasGlyph(name)) {
name = encoding.getName(estr.charAt(0));
if (name != null) {
glyph = fontBoxFont.getPath(name);
}
}

Area outline = new Area(glyph);
AffineTransform transform = new AffineTransform();
transform.translate(x, y);
transform.concatenate(fontTransform);
outline = outline.createTransformedArea(transform);
return outline;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

@Override
public Rectangle2D getMaxCharBounds() {
return bbox;
return calculateBbox(bbox);
}

@Override
Expand Down Expand Up @@ -236,11 +261,6 @@ public String getFormat() {
return null;
}

@Override
public Shape getEstringOutline(String estr, float x, float y) {
return null;
}

@Override
public ByteEncoding getByteEncoding() {
return null;
Expand Down Expand Up @@ -284,14 +304,10 @@ protected char getCharDiff(char character) {

protected Rectangle2D calculateBbox(Rectangle2D bbox) {
if (bbox != null) {
double scaleX = 0.001 / fontMatrix.getScaleX();
double scaleY = 0.001 / fontMatrix.getScaleY();

return new Rectangle2D.Double(
bbox.getX() * scaleX,
bbox.getY() * scaleY,
bbox.getWidth() * scaleX,
bbox.getHeight() * scaleY);
AffineTransform af = new AffineTransform();
// af.scale(size, size);
af.concatenate(fontMatrix);
return af.createTransformedShape(bbox).getBounds2D();
}
return this.bbox;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;

Expand Down Expand Up @@ -106,19 +107,11 @@ 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
double descent = font.getDescent();
double ascent = font.getAscent();
float descent = (float) font.getDescent();
float ascent = (float) font.getAscent();
float w = width;//(float)stringBounds.getWidth();
float h = (float) (ascent - descent);
float h = ascent - descent;

Rectangle2D bbox = font.getMaxCharBounds();

if (h <= 0.0f) {
h = (float) bbox.getHeight();
}
if (w <= 0.0f) {
w = (float) bbox.getWidth();
}
// zero height will not intersect with clip rectangle and maybe have visibility issues.
// we generally get here if the font.getAscent is zero and as a result must compensate.
if (h <= 0.0f) {
Expand All @@ -130,16 +123,13 @@ public GlyphText addText(String cid, String unicode, float x, float y, float wid
h = font.getSize();
}
}
// apply fontsize.
// Rectangle2D.Float glyphBounds = new Rectangle2D.Float(x, y - ascent, w, h);
h *= Math.abs(font.getSize());
descent *= Math.abs(font.getSize());

Rectangle2D.Float glyphBounds;
// irregular negative layout of text, need to create the bbox appropriately.
if (w < 0.0f || font.getSize() < 0) {
glyphBounds = new Rectangle2D.Float(x + width, y - (float) descent, -w, h);
glyphBounds = new Rectangle2D.Float(x + width, y - ascent, -w, h);
} else {
glyphBounds = new Rectangle2D.Float(x, y - h - (float) descent, w, h);
glyphBounds = new Rectangle2D.Float(x, y - ascent, w, h);
}

// add bounds to total text bounds.
Expand Down Expand Up @@ -296,7 +286,6 @@ public void setFontSize(int fontSize) {
this.fontSize = fontSize;
}

/*
private void drawBoundBox(Graphics2D gg) {

// draw the characters
Expand All @@ -319,12 +308,11 @@ private void drawBoundBox(Graphics2D gg) {
gg.setColor(oldColor);
gg.setStroke(oldStroke);
}
*/

public void setFont(FontFile font) {
this.font = font;
}
/*

private void drawGyphBox(Graphics2D gg, GlyphText glyphSprite) {

// draw the characters
Expand All @@ -349,9 +337,6 @@ private void drawGyphBox(Graphics2D gg, GlyphText glyphSprite) {

}

*/

/**
* Tests if the interior of the <code>TextSprite</code> bounds intersects the
* interior of a specified <code>shape</code>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1467,7 +1467,6 @@ private static void drawString(
// glyph placement params
float currentX, currentY;
float newAdvanceX, newAdvanceY;
// Iterate through displayText to calculate the the new advanceX value
for (int i = 0; i < textLength; i++) {
currentChar = displayText.charAt(i);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public ContentParser parse(byte[][] streamBytes, Page page)
try {
yBTstart = parseText(lexer, shapes, yBTstart);
} catch (Exception e) {
logger.log(Level.FINEST, "Error parsing text block", e);
logger.log(Level.SEVERE, "Error parsing text block", e);
} finally {
inTextBlock = false;
}
Expand Down

0 comments on commit 2acbe50

Please sign in to comment.