]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
FOP-3015: Set text color for simulate-style
authorSimon Steiner <ssteiner@apache.org>
Fri, 21 Jan 2022 16:07:55 +0000 (16:07 +0000)
committerSimon Steiner <ssteiner@apache.org>
Fri, 21 Jan 2022 16:07:55 +0000 (16:07 +0000)
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1897301 13f79535-47bb-0310-9956-ffa450edef68

fop-core/src/main/java/org/apache/fop/render/pdf/PDFPainter.java
fop-core/src/test/java/org/apache/fop/render/pdf/PDFPainterTestCase.java

index b2c7a0d179571a17ffdc07b1b30949856e125878..114cb7ae72c4b864b7119c1abf3f7c2dbed516d7 100644 (file)
@@ -469,16 +469,7 @@ public class PDFPainter extends AbstractIFPainter<PDFDocumentHandler> {
         PDFTextUtil textutil = generator.getTextUtil();
         textutil.updateTf(fontKey, fontSize, tf.isMultiByte(), tf.isCID());
 
-        double shear = 0;
-        boolean simulateStyle = tf instanceof CustomFont && ((CustomFont) tf).getSimulateStyle();
-        if (simulateStyle) {
-            if (triplet.getWeight() == 700) {
-                generator.add("2 Tr 0.31543 w\n");
-            }
-            if (triplet.getStyle().equals("italic")) {
-                shear = 0.3333;
-            }
-        }
+        double shear = startSimulateStyle(tf, triplet);
 
         generator.updateCharacterSpacing(letterSpacing / 1000f);
 
@@ -531,6 +522,26 @@ public class PDFPainter extends AbstractIFPainter<PDFDocumentHandler> {
 
         }
         textutil.writeTJ();
+        endSimulateStyle(tf, triplet);
+    }
+
+    private double startSimulateStyle(Typeface tf, FontTriplet triplet) {
+        double shear = 0;
+        boolean simulateStyle = tf instanceof CustomFont && ((CustomFont) tf).getSimulateStyle();
+        if (simulateStyle) {
+            if (triplet.getWeight() == 700) {
+                generator.updateColor(state.getTextColor(), false, null);
+                generator.add("2 Tr 0.31543 w\n");
+            }
+            if (triplet.getStyle().equals("italic")) {
+                shear = 0.3333;
+            }
+        }
+        return shear;
+    }
+
+    private void endSimulateStyle(Typeface tf, FontTriplet triplet) {
+        boolean simulateStyle = tf instanceof CustomFont && ((CustomFont) tf).getSimulateStyle();
         if (simulateStyle && triplet.getWeight() == 700) {
             generator.add("0 Tr\n");
         }
@@ -556,19 +567,7 @@ public class PDFPainter extends AbstractIFPainter<PDFDocumentHandler> {
             double      yoLast          = 0f;
             double      wox             = wordSpacing;
 
-            // FOP-2810
-            boolean simulateStyle = tf instanceof CustomFont && ((CustomFont) tf).getSimulateStyle();
-            double shear = 0;
-
-            if (simulateStyle) {
-                if (triplet.getWeight() == 700) {
-                    generator.add("q\n");
-                    generator.add("2 Tr 0.31543 w\n");
-                }
-                if (triplet.getStyle().equals("italic")) {
-                    shear = 0.3333;
-                }
-            }
+            double shear = startSimulateStyle(tf, triplet);
 
             tu.writeTextMatrix(new AffineTransform(1, 0, shear, -1, x / 1000f, y / 1000f));
             tu.updateTf(fk, fsPoints, tf.isMultiByte(), true);
@@ -589,6 +588,7 @@ public class PDFPainter extends AbstractIFPainter<PDFDocumentHandler> {
                 xoLast = xo;
                 yoLast = yo;
             }
+            endSimulateStyle(tf, triplet);
         }
     }
 
index f72e227db4770c767c89f75dace6a7a359073598..387afef9f018422ea1e380e7d92f1ea400290f77 100644 (file)
@@ -171,6 +171,45 @@ public class PDFPainterTestCase {
         verify(pdfContentGenerator).add("0 Tr\n");
     }
 
+    @Test
+    public void testSimulateStyleColor() throws Exception {
+        FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
+        foUserAgent = fopFactory.newFOUserAgent();
+        PDFDocumentHandler pdfDocumentHandler = new PDFDocumentHandler(new IFContext(foUserAgent));
+
+        pdfDocumentHandler.setResult(new StreamResult(new ByteArrayOutputStream()));
+        pdfDocumentHandler.startDocument();
+        pdfDocumentHandler.startPage(0, "", "", new Dimension());
+
+        FontInfo fi = new FontInfo();
+        fi.addFontProperties("f1", new FontTriplet("a", "italic", 700));
+        MultiByteFont font = new MultiByteFont(null, null);
+        font.setSimulateStyle(true);
+        fi.addMetrics("f1", font);
+        pdfDocumentHandler.setFontInfo(fi);
+        PDFPainter pdfPainter = new PDFPainter(pdfDocumentHandler, null);
+        pdfPainter.setFont("a", "italic", 700, null, 12, Color.red);
+        pdfPainter.drawText(0, 0, 0, 0, null, "test");
+
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        PDFFilterList filters = pdfPainter.generator.getStream().getFilterList();
+        filters.setDisableAllFilters(true);
+        pdfPainter.generator.getStream().output(bos);
+        Assert.assertEquals(bos.toString(), "<< /Length 1 0 R >>\n"
+                + "stream\n"
+                + "q\n"
+                + "1 0 0 -1 0 0 cm\n"
+                + "1 0 0 rg\n"
+                + "BT\n"
+                + "/f1 0.012 Tf\n"
+                + "1 0 0 RG\n"
+                + "2 Tr 0.31543 w\n"
+                + "1 0 0.3333 -1 0 0 Tm [<0000000000000000>] TJ\n"
+                + "0 Tr\n"
+                + "\n"
+                + "endstream");
+    }
+
     @Test
     public void testDrawTextWithMultiByteFont() throws IFException {
         StringBuilder output = new StringBuilder();