]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
FOP-3017: Add option to disable positioning per char
authorSimon Steiner <ssteiner@apache.org>
Sat, 26 Jun 2021 06:22:24 +0000 (06:22 +0000)
committerSimon Steiner <ssteiner@apache.org>
Sat, 26 Jun 2021 06:22:24 +0000 (06:22 +0000)
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1891051 13f79535-47bb-0310-9956-ffa450edef68

fop-core/src/main/java/org/apache/fop/render/afp/AFPFontConfig.java
fop-core/src/main/java/org/apache/fop/render/afp/AFPPainter.java
fop-core/src/test/java/org/apache/fop/render/afp/AFPTrueTypeTestCase.java

index eb6c4a77f0c2515bd5d49c1722407def35a180fe..a9f222bf17880fb0b1d6dca237034c082d93c44e 100644 (file)
@@ -266,8 +266,9 @@ public final class AFPFontConfig implements FontConfig {
                 return null;
             }
             String subfont = cfg.getAttribute("sub-font", null);
+            boolean positionByChar = cfg.getAttributeAsBoolean("position-by-char", true);
             return new TrueTypeFontConfig(fontTriplets, type, codepage, encoding, "",
-                    name, subfont, isEmbbedable(fontTriplets), uri);
+                    name, subfont, isEmbbedable(fontTriplets), uri, positionByChar);
         }
 
         private RasterFontConfig getRasterFont(List<FontTriplet> triplets, String type,
@@ -368,14 +369,16 @@ public final class AFPFontConfig implements FontConfig {
         private String characterset;
         private String subfont;
         private String fontUri;
+        private boolean positionByChar;
 
         private TrueTypeFontConfig(List<FontTriplet> triplets, String type, String codePage,
                                    String encoding, String characterset, String name, String subfont,
-                                   boolean embeddable, String uri) {
+                                   boolean embeddable, String uri, boolean positionByChar) {
             super(triplets, type, codePage, encoding, name, embeddable, null);
             this.characterset = characterset;
             this.subfont = subfont;
             this.fontUri = uri;
+            this.positionByChar = positionByChar;
         }
 
         @Override
@@ -390,7 +393,7 @@ public final class AFPFontConfig implements FontConfig {
                 CharacterSet characterSet = CharacterSetBuilder.getDoubleByteInstance().build(characterset,
                         super.codePage, super.encoding, tf, accessor, eventProducer);
                 OutlineFont font = new AFPTrueTypeFont(super.name, super.embeddable, characterSet,
-                            eventProducer, subfont, new URI(fontUri));
+                            eventProducer, subfont, new URI(fontUri), positionByChar);
                 return getFontInfo(font, this);
             } catch (URISyntaxException e) {
                 throw new IOException(e);
@@ -401,11 +404,13 @@ public final class AFPFontConfig implements FontConfig {
     public static class AFPTrueTypeFont extends OutlineFont {
         private String ttc;
         private URI uri;
+        private boolean positionByChar;
         public AFPTrueTypeFont(String name, boolean embeddable, CharacterSet charSet, AFPEventProducer eventProducer,
-                               String ttc, URI uri) {
+                               String ttc, URI uri, boolean positionByChar) {
             super(name, embeddable, charSet, eventProducer);
             this.ttc = ttc;
             this.uri = uri;
+            this.positionByChar = positionByChar;
         }
 
         public FontType getFontType() {
@@ -419,6 +424,10 @@ public final class AFPFontConfig implements FontConfig {
         public URI getUri() {
             return uri;
         }
+
+        public boolean isPositionByChar() {
+            return positionByChar;
+        }
     }
 
     static final class OutlineFontConfig extends AFPFontConfigData {
index 35235668aa1145a3c7bbebe4edb510be0cca8b60..9a4a7b93e3245940daeb9b3bd65db38d4bba8eae 100644 (file)
@@ -70,7 +70,6 @@ import org.apache.fop.afp.ptoca.PtocaProducer;
 import org.apache.fop.afp.util.AFPResourceAccessor;
 import org.apache.fop.fonts.Font;
 import org.apache.fop.fonts.FontTriplet;
-import org.apache.fop.fonts.FontType;
 import org.apache.fop.fonts.Typeface;
 import org.apache.fop.render.ImageHandlerUtil;
 import org.apache.fop.render.RenderingContext;
@@ -1026,11 +1025,12 @@ public class AFPPainter extends AbstractIFPainter<AFPDocumentHandler> {
 
                 boolean fixedSpaceMode = false;
                 double ttPos = p.x;
-
+                boolean positionByChar = afpFont instanceof AFPFontConfig.AFPTrueTypeFont
+                        && ((AFPFontConfig.AFPTrueTypeFont) afpFont).isPositionByChar();
                 for (int i = 0; i < l; i++) {
                     char orgChar = text.charAt(i);
                     float glyphAdjust = 0;
-                    if (afpFont.getFontType() == FontType.TRUETYPE) {
+                    if (positionByChar) {
                         flushText(builder, sb, charSet);
                         fixedSpaceMode = true;
                         int charWidth = font.getCharWidth(orgChar);
@@ -1064,7 +1064,7 @@ public class AFPPainter extends AbstractIFPainter<AFPDocumentHandler> {
                         glyphAdjust += dx[i + 1];
                     }
 
-                    if (afpFont.getFontType() == FontType.TRUETYPE) {
+                    if (positionByChar) {
                         flushText(builder, sb, charSet);
                         ttPos += unitConv.mpt2units(glyphAdjust);
                         builder.absoluteMoveInline((int) Math.round(ttPos));
index 0dfbd1a46a07b278333bd22e6eaa1e57ca03e2de..9c6b51873ac58baaa0c4a4b2a983659f417b7bac 100644 (file)
@@ -224,7 +224,7 @@ public class AFPTrueTypeTestCase {
         PageObject po = new PageObject(new Factory(), "PAGE0001", 0, 0, 0, 0, 0);
         when(ds.getCurrentPage()).thenReturn(po);
 
-        AFPPainter afpPainter = new MyAFPPainter(afpDocumentHandler);
+        AFPPainter afpPainter = new MyAFPPainter(afpDocumentHandler, true);
         afpPainter.setFont("any", "normal", 400, null, null, Color.BLACK);
         afpPainter.drawText(0, 0, 0, 0, null, "test");
 
@@ -240,6 +240,19 @@ public class AFPTrueTypeTestCase {
 
     @Test
     public void testAFPPainterWidths() throws IFException, IOException {
+        String s = getAFPPainterWidths(true);
+        Assert.assertTrue(s, s.contains("DATA PRESENTATION_TEXT AMB AMI 0 SCFL SVI TRN a AMI"
+                + " 9 TRN b AMI 29 TRN c AMI 59 TRN d AMI 99 TRN e AMI 149 TRN f AMI 209 TRN g AMI 24 TRN h AMI 105 TRN"
+                + " i AMI 196 TRN j AMI 42 TRN k AMI 153 TRN l AMI 19 TRN m AMI 151 TRN n AMI 38 TRN o AMI 190"));
+    }
+
+    @Test
+    public void testAFPPainterWidthsNoPositionByChar() throws IFException, IOException {
+        String s = getAFPPainterWidths(false);
+        Assert.assertTrue(s, s.contains("DATA PRESENTATION_TEXT AMB AMI 0 SCFL SVI TRN abcdefghijklmno"));
+    }
+
+    private String getAFPPainterWidths(boolean positionByChar) throws IFException, IOException {
         AFPDocumentHandler afpDocumentHandler = mock(AFPDocumentHandler.class);
         when(afpDocumentHandler.getPaintingState()).thenReturn(new AFPPaintingState());
         when(afpDocumentHandler.getResourceManager()).thenReturn(new AFPResourceManager(null));
@@ -249,7 +262,7 @@ public class AFPTrueTypeTestCase {
         PageObject po = new PageObject(new Factory(), "PAGE0001", 0, 0, 0, 0, 0);
         when(ds.getCurrentPage()).thenReturn(po);
 
-        AFPPainter afpPainter = new MyAFPPainter(afpDocumentHandler);
+        AFPPainter afpPainter = new MyAFPPainter(afpDocumentHandler, positionByChar);
         afpPainter.setFont("any", "normal", 400, null, 12000, Color.BLACK);
         afpPainter.drawText(0, 0, 0, 0, null, "abcdefghijklmno");
 
@@ -260,14 +273,15 @@ public class AFPTrueTypeTestCase {
         AFPParser afpParser = new AFPParser(true);
         afpParser.readWidths = true;
         afpParser.read(bis, sb);
-        Assert.assertTrue(sb.toString(), sb.toString().contains("DATA PRESENTATION_TEXT AMB AMI 0 SCFL SVI TRN a AMI"
-                + " 9 TRN b AMI 29 TRN c AMI 59 TRN d AMI 99 TRN e AMI 149 TRN f AMI 209 TRN g AMI 24 TRN h AMI 105 TRN"
-                + " i AMI 196 TRN j AMI 42 TRN k AMI 153 TRN l AMI 19 TRN m AMI 151 TRN n AMI 38 TRN o AMI 190"));
+        return sb.toString();
     }
 
-    class MyAFPPainter extends AFPPainter {
-        MyAFPPainter(AFPDocumentHandler documentHandler) {
+    static class MyAFPPainter extends AFPPainter {
+        boolean positionByChar;
+
+        MyAFPPainter(AFPDocumentHandler documentHandler, boolean positionByChar) {
             super(documentHandler);
+            this.positionByChar = positionByChar;
         }
 
         protected FOUserAgent getUserAgent() {
@@ -292,7 +306,7 @@ public class AFPTrueTypeTestCase {
             }
             font.setWidthArray(widths);
             f.addMetrics("any", new AFPFontConfig.AFPTrueTypeFont("", true,
-                    new FopCharacterSet("", "UTF-16BE", "", font, null, null), null, null, null));
+                    new FopCharacterSet("", "UTF-16BE", "", font, null, null), null, null, null, positionByChar));
             return f;
         }
     }