Skip to content

Commit

Permalink
#143 #3 Text justification for embedded unicode fonts.
Browse files Browse the repository at this point in the history
Manual merge from PR.
  • Loading branch information
danfickle committed Jul 28, 2018
1 parent 4227ad2 commit 90aa945
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -436,21 +436,19 @@ public void drawStringFast(String s, float x, float y, JustificationInfo info, F
}

_cp.beginText();

_cp.setFont(desc.getFont(), fontSize);


_cp.setTextMatrix((float) mx[0], b, c, (float) mx[3], (float) mx[4], (float) mx[5]);

if (info != null ) {
// The JustificationInfo numbers need to be normalized using the current document DPI
_cp.setTextSpacing(info.getNonSpaceAdjust() / _dotsPerPoint);
_cp.setSpaceSpacing(info.getSpaceAdjust() / _dotsPerPoint);
// Justification must be done through TJ rendering
// because Tw param does not work for UNICODE fonts
Object[] array = makeJustificationArray(s, info);
_cp.drawStringWithPositioning(array);
} else {
_cp.setTextSpacing(0.0f);
_cp.setSpaceSpacing(0.0f);
_cp.drawString(s);
}

_cp.drawString(s);
_cp.endText();

if (resetMode) {
Expand All @@ -464,85 +462,26 @@ public static class FontRun {
FontDescription des;
}

private Object[] makeJustificationArray(String s, JustificationInfo info) {
List<Object> data = new ArrayList<Object>(s.length() * 2);


/*
public void drawString(String s, float x, float y, JustificationInfo info) {
if (Configuration.isTrue("xr.renderer.replace-missing-characters", false)) {
s = replaceMissingCharacters(s);
}
if (s.length() == 0)
return;
ensureFillColor();
AffineTransform at = (AffineTransform) getTransform().clone();
at.translate(x, y);
AffineTransform inverse = normalizeMatrix(at);
AffineTransform flipper = AffineTransform.getScaleInstance(1, -1);
inverse.concatenate(flipper);
inverse.scale(_dotsPerPoint, _dotsPerPoint);
double[] mx = new double[6];
inverse.getMatrix(mx);
_cp.beginText();
// Check if bold or italic need to be emulated
boolean resetMode = false;
FontDescription desc = _font.getFontDescription();
float fontSize = _font.getSize2D() / _dotsPerPoint;
cb.setFontAndSize(desc.getFont(), fontSize);
float b = (float) mx[1];
float c = (float) mx[2];
FontSpecification fontSpec = getFontSpecification();
if (fontSpec != null) {
int need = ITextFontResolver.convertWeightToInt(fontSpec.fontWeight);
int have = desc.getWeight();
if (need > have) {
cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE);
float lineWidth = fontSize * 0.04f; // 4% of font size
cb.setLineWidth(lineWidth);
resetMode = true;
ensureStrokeColor();
}
if ((fontSpec.fontStyle == IdentValue.ITALIC) && (desc.getStyle() != IdentValue.ITALIC)) {
b = 0f;
c = 0.21256f;
}
}
cb.setTextMatrix((float) mx[0], b, c, (float) mx[3], (float) mx[4], (float) mx[5]);
if (info == null) {
_cp.drawString(s);
} else {
PdfTextArray array = makeJustificationArray(s, info);
cb.showText(array);
}
if (resetMode) {
cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL);
cb.setLineWidth(1);
}
_cp.endText();
}
*/
/*
private PdfTextArray makeJustificationArray(String s, JustificationInfo info) {
PdfTextArray array = new PdfTextArray();
int len = s.length();
for (int i = 0; i < len; i++) {
char c = s.charAt(i);
array.add(Character.toString(c));
data.add(Character.toString(c));
if (i != len - 1) {
float offset;
if (c == ' ' || c == '\u00a0' || c == '\u3000') {
offset = info.getSpaceAdjust();
} else {
offset = info.getNonSpaceAdjust();
}
array.add((-offset / _dotsPerPoint) * 1000 / (_font.getSize2D() / _dotsPerPoint));
data.add(Float.valueOf((-offset / _dotsPerPoint) * 1000 / (_font.getSize2D() / _dotsPerPoint)));
}
}
return array;
return data.toArray();
}
*/

private AffineTransform getTransform() {
return _transform;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import java.awt.geom.AffineTransform;
import java.io.IOException;
import java.util.Locale;

public class PdfContentStreamAdapter {
private final PDPageContentStream cs;
Expand Down Expand Up @@ -308,20 +307,17 @@ public void setMiterLimit(float miterLimit) {
logAndThrow("setMiterLimit", e);
}
}

public void setTextSpacing(float nonSpaceAdjust) {
try {
cs.appendRawCommands(String.format(Locale.US, "%.4f Tc\n", nonSpaceAdjust));
} catch (IOException e) {
logAndThrow("setSpaceSpacing", e);
}
}

public void setSpaceSpacing(float spaceAdjust) {

/**
*
* @param str MUST consist of a array of strings optionally interspersed with
* Float values specifying additional spacing.
*/
public void drawStringWithPositioning(Object[] str) {
try {
cs.appendRawCommands(String.format(Locale.US, "%.4f Tw\n", spaceAdjust));
cs.showTextWithPositioning(str);
} catch (IOException e) {
logAndThrow("setSpaceSpacing", e);
logAndThrow("drawStringWithPositioning", e);
}
}

Expand Down

0 comments on commit 90aa945

Please sign in to comment.