Skip to content

Commit

Permalink
Merge pull request #240 from syjer/open-dev-v1
Browse files Browse the repository at this point in the history
cherry-picked commit from pr #143: bold and italic emulation
  • Loading branch information
danfickle authored Jul 3, 2018
2 parents b7e1c6b + 1060bae commit 3491b7e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.openhtmltopdf.extend.OutputDevice;
import com.openhtmltopdf.extend.OutputDeviceGraphicsDrawer;
import com.openhtmltopdf.layout.SharedContext;
import com.openhtmltopdf.outputdevice.helper.FontResolverHelper;
import com.openhtmltopdf.pdfboxout.PdfBoxFontResolver.FontDescription;
import com.openhtmltopdf.pdfboxout.PdfBoxForm.CheckboxStyle;
import com.openhtmltopdf.render.*;
Expand All @@ -51,6 +52,7 @@
import org.apache.pdfbox.pdmodel.graphics.image.JPEGFactory;
import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.pdmodel.graphics.state.RenderingMode;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDDestination;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageFitHeightDestination;
Expand Down Expand Up @@ -523,8 +525,6 @@ public void drawString(String s, float x, float y, JustificationInfo info) {
}

public void drawStringFast(String s, float x, float y, JustificationInfo info, FontDescription desc, float fontSize) {
// TODO: Emulate bold and italic.

if (s.length() == 0)
return;

Expand All @@ -543,8 +543,28 @@ public void drawStringFast(String s, float x, float y, JustificationInfo info, F

fontSize = fontSize / _dotsPerPoint;

boolean resetMode = false;
FontSpecification fontSpec = getFontSpecification();
if (fontSpec != null) {
int need = FontResolverHelper.convertWeightToInt(fontSpec.fontWeight);
int have = desc.getWeight();
if (need > have) {
_cp.setRenderingMode(RenderingMode.FILL_STROKE);
float lineWidth = fontSize * 0.04f; // 4% of font size
_cp.setLineWidth(lineWidth);
resetMode = true;
ensureStrokeColor();
}
if ((fontSpec.fontStyle == IdentValue.ITALIC) && (desc.getStyle() != IdentValue.ITALIC)) {
b = 0f;
c = 0.21256f;
}
}

_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 ) {
Expand All @@ -558,6 +578,11 @@ public void drawStringFast(String s, float x, float y, JustificationInfo info, F

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

if (resetMode) {
_cp.setRenderingMode(RenderingMode.FILL);
_cp.setLineWidth(1);
}
}

public static class FontRun {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState;
import org.apache.pdfbox.pdmodel.graphics.state.RenderingMode;
import org.apache.pdfbox.util.Matrix;

import java.awt.geom.AffineTransform;
Expand Down Expand Up @@ -270,6 +271,14 @@ public void setTextMatrix(float a, float b, float c, float d, float e,
}
}

public void setRenderingMode(RenderingMode rm) {
try {
cs.setRenderingMode(rm);
} catch (IOException e) {
logAndThrow("setRenderingMode", e);
}
}

public void drawString(String s) {
try {
cs.showText(s);
Expand Down

0 comments on commit 3491b7e

Please sign in to comment.