FOP-3017: Add option to disable positioning per char

git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1891051 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/fop-core/src/main/java/org/apache/fop/render/afp/AFPFontConfig.java b/fop-core/src/main/java/org/apache/fop/render/afp/AFPFontConfig.java
index eb6c4a7..a9f222b 100644
--- a/fop-core/src/main/java/org/apache/fop/render/afp/AFPFontConfig.java
+++ b/fop-core/src/main/java/org/apache/fop/render/afp/AFPFontConfig.java
@@ -266,8 +266,9 @@
                 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 @@
         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 @@
                 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 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 URI getUri() {
             return uri;
         }
+
+        public boolean isPositionByChar() {
+            return positionByChar;
+        }
     }
 
     static final class OutlineFontConfig extends AFPFontConfigData {
diff --git a/fop-core/src/main/java/org/apache/fop/render/afp/AFPPainter.java b/fop-core/src/main/java/org/apache/fop/render/afp/AFPPainter.java
index 3523566..9a4a7b9 100644
--- a/fop-core/src/main/java/org/apache/fop/render/afp/AFPPainter.java
+++ b/fop-core/src/main/java/org/apache/fop/render/afp/AFPPainter.java
@@ -70,7 +70,6 @@
 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 @@
 
                 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 @@
                         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));
diff --git a/fop-core/src/test/java/org/apache/fop/render/afp/AFPTrueTypeTestCase.java b/fop-core/src/test/java/org/apache/fop/render/afp/AFPTrueTypeTestCase.java
index 0dfbd1a..9c6b518 100644
--- a/fop-core/src/test/java/org/apache/fop/render/afp/AFPTrueTypeTestCase.java
+++ b/fop-core/src/test/java/org/apache/fop/render/afp/AFPTrueTypeTestCase.java
@@ -224,7 +224,7 @@
         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 @@
 
     @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 @@
         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 @@
         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 @@
             }
             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;
         }
     }