You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-dev@xmlgraphics.apache.org by Chris Bowditch <bo...@hotmail.com> on 2009/01/15 12:21:02 UTC

[Fwd: svn commit: r734105 - in /xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop: afp/fonts/ afp/modca/ render/afp/]

Hi Jeremias,

Am I right in thinking supporting fixed width spaces in AFP is the same 
as bug 46278? If so then you've made me a happy man!

Thanks,

Chris

-------- Original Message --------
Subject: svn commit: r734105 - in 
/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop: 
afp/fonts/ afp/modca/ render/afp/
Date: Tue, 13 Jan 2009 11:29:37 -0000
From: jeremias@apache.org
Reply-To: fop-dev@xmlgraphics.apache.org
To: fop-commits@xmlgraphics.apache.org

Author: jeremias
Date: Tue Jan 13 03:28:56 2009
New Revision: 734105

URL: http://svn.apache.org/viewvc?rev=734105&view=rev
Log:
Added support for fixed-width spaces.
Better handling of unmappable characters.

Modified:
 
xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/afp/fonts/AFPFont.java
 
xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/afp/fonts/CharacterSet.java
 
xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/afp/fonts/CharacterSetOrientation.java
 
xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/afp/fonts/OutlineFont.java
 
xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/afp/fonts/RasterFont.java
 
xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/afp/modca/PresentationTextObject.java
 
xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/afp/AFPPainter.java

Modified: 
xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/afp/fonts/AFPFont.java
URL: 
http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/afp/fonts/AFPFont.java?rev=734105&r1=734104&r2=734105&view=diff
==============================================================================
--- 
xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/afp/fonts/AFPFont.java 
(original)
+++ 
xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/afp/fonts/AFPFont.java 
Tue Jan 13 03:28:56 2009
@@ -97,15 +97,6 @@
       */
      public abstract CharacterSet getCharacterSet(int size);

-     /**
-     * Determines whether this font contains a particular character/glyph.
-     * @param c character to check
-     * @return True if the character is supported, False otherwise
-     */
-    public boolean hasChar(char c) {
-        return true;
-    }
-
      /** {@inheritDoc} */
      public String toString() {
          return "name=" + name;

Modified: 
xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/afp/fonts/CharacterSet.java
URL: 
http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/afp/fonts/CharacterSet.java?rev=734105&r1=734104&r2=734105&view=diff
==============================================================================
--- 
xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/afp/fonts/CharacterSet.java 
(original)
+++ 
xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/afp/fonts/CharacterSet.java 
Tue Jan 13 03:28:56 2009
@@ -21,10 +21,17 @@

  import java.io.IOException;
  import java.io.UnsupportedEncodingException;
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+import java.nio.charset.CharacterCodingException;
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetEncoder;
+import java.nio.charset.CodingErrorAction;
  import java.util.Map;

  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
+
  import org.apache.fop.afp.AFPConstants;
  import org.apache.fop.afp.util.StringUtils;

@@ -65,6 +72,9 @@
      /** The encoding used for the code page */
      protected String encoding;

+    /** The charset encoder corresponding to this encoding */
+    private CharsetEncoder encoder;
+
      /** The character set relating to the font */
      protected String name;

@@ -104,6 +114,8 @@
          }
          this.codePage = codePage;
          this.encoding = encoding;
+        this.encoder = Charset.forName(encoding).newEncoder();
+        this.encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
          this.path = path;

          this.characterSetOrientations = new java.util.HashMap(4);
@@ -304,6 +316,33 @@
      }

      /**
+     * Indicates whether the given char in the character set.
+     * @param c the character to check
+     * @return true if the character is in the character set
+     */
+    public boolean hasChar(char c) {
+        return encoder.canEncode(c);
+    }
+
+    /**
+     * Encodes a character sequence to a byte array.
+     * @param chars the characters
+     * @return the encoded characters
+     * @throws CharacterCodingException if the encoding operation fails
+     */
+    public byte[] encodeChars(CharSequence chars) throws 
CharacterCodingException {
+        ByteBuffer bb = encoder.encode(CharBuffer.wrap(chars));
+        if (bb.hasArray()) {
+            return bb.array();
+        } else {
+            bb.rewind();
+            byte[] bytes = new byte[bb.remaining()];
+            bb.get(bytes);
+            return bytes;
+        }
+    }
+
+    /**
       * Map a Unicode character to a code point in the font.
       * The code tables are already converted to Unicode therefore
       * we can use the identity mapping.
@@ -312,6 +351,7 @@
       * @return the mapped character
       */
      public char mapChar(char c) {
+        //TODO This is not strictly correct but we'll let it be for the 
moment
          return c;
      }


Modified: 
xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/afp/fonts/CharacterSetOrientation.java
URL: 
http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/afp/fonts/CharacterSetOrientation.java?rev=734105&r1=734104&r2=734105&view=diff
==============================================================================
--- 
xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/afp/fonts/CharacterSetOrientation.java 
(original)
+++ 
xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/afp/fonts/CharacterSetOrientation.java 
Tue Jan 13 03:28:56 2009
@@ -41,16 +41,6 @@
  public class CharacterSetOrientation {

      /**
-     * The code page to which the character set relates
-     */
-    private String codePage;
-
-    /**
-     * The encoding used for the code page
-     */
-    private String encoding;
-
-    /**
       * The ascender height for the character set
       */
      private int ascender;
@@ -66,9 +56,9 @@
      private int capHeight;

      /**
-     * The characters in the charcater set
+     * The character widths in the character set
       */
-    private int[] chars = new int[256];
+    private int[] charsWidths = new int[256];

      /**
       * The height of lowercase letters
@@ -166,8 +156,8 @@
       * @return the widths of all characters
       */
      public int[] getWidths() {
-        int arr[] = new int[(getLastChar() - getFirstChar()) + 1];
-        System.arraycopy(chars, getFirstChar(), arr, 0, (getLastChar() 
- getFirstChar()) + 1);
+        int[] arr = new int[(getLastChar() - getFirstChar()) + 1];
+        System.arraycopy(charsWidths, getFirstChar(), arr, 0, 
(getLastChar() - getFirstChar()) + 1);
          return arr;
      }

@@ -187,11 +177,11 @@
       * @return the widths of the character
       */
      public int getWidth(int characterIndex) {
-        if (characterIndex >= chars.length) {
+        if (characterIndex >= charsWidths.length) {
              throw new IllegalArgumentException("Invalid character index: "
-                    + characterIndex + ", maximum is " + (chars.length 
- 1));
+                    + characterIndex + ", maximum is " + 
(charsWidths.length - 1));
          }
-        return chars[characterIndex];
+        return charsWidths[characterIndex];
      }

      /**
@@ -253,14 +243,13 @@
       * @param width the widths of the character
       */
      public void setWidth(int character, int width) {
-
-        if (character >= chars.length) {
+        if (character >= charsWidths.length) {
              // Increase the size of the array if necessary
-            int arr[] = new int[(character - firstChar) + 1];
-            System.arraycopy(chars, 0, arr, 0, chars.length);
-            chars = arr;
+            int[] arr = new int[(character - firstChar) + 1];
+            System.arraycopy(charsWidths, 0, arr, 0, charsWidths.length);
+            charsWidths = arr;
          }
-        chars[character] = width;
+        charsWidths[character] = width;

      }


Modified: 
xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/afp/fonts/OutlineFont.java
URL: 
http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/afp/fonts/OutlineFont.java?rev=734105&r1=734104&r2=734105&view=diff
==============================================================================
--- 
xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/afp/fonts/OutlineFont.java 
(original)
+++ 
xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/afp/fonts/OutlineFont.java 
Tue Jan 13 03:28:56 2009
@@ -166,6 +166,11 @@
          return getWidths(1000);
      }

+    /** {@inheritDoc} */
+    public boolean hasChar(char c) {
+        return charSet.hasChar(c);
+    }
+
      /**
       * Map a Unicode character to a code point in the font.
       * @param c character to map
@@ -179,4 +184,5 @@
      public String getEncodingName() {
          return charSet.getEncoding();
      }
+
  }
\ No newline at end of file

Modified: 
xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/afp/fonts/RasterFont.java
URL: 
http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/afp/fonts/RasterFont.java?rev=734105&r1=734104&r2=734105&view=diff
==============================================================================
--- 
xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/afp/fonts/RasterFont.java 
(original)
+++ 
xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/afp/fonts/RasterFont.java 
Tue Jan 13 03:28:56 2009
@@ -221,6 +221,11 @@
          return getWidths(1000);
      }

+    /** {@inheritDoc} */
+    public boolean hasChar(char c) {
+        return charSet.hasChar(c);
+    }
+
      /**
       * Map a Unicode character to a code point in the font.
       * @param c character to map

Modified: 
xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/afp/modca/PresentationTextObject.java
URL: 
http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/afp/modca/PresentationTextObject.java?rev=734105&r1=734104&r2=734105&view=diff
==============================================================================
--- 
xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/afp/modca/PresentationTextObject.java 
(original)
+++ 
xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/afp/modca/PresentationTextObject.java 
Tue Jan 13 03:28:56 2009
@@ -191,7 +191,7 @@

      private void handleUnexpectedIOError(IOException ioe) {
          //"Unexpected" since we're currently dealing with 
ByteArrayOutputStreams here.
-        throw new RuntimeException("Unexpected I/O error", ioe);
+        throw new RuntimeException("Unexpected I/O error: " + 
ioe.getMessage(), ioe);
      }

      /** {@inheritDoc} */

Modified: 
xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/afp/AFPPainter.java
URL: 
http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/afp/AFPPainter.java?rev=734105&r1=734104&r2=734105&view=diff
==============================================================================
--- 
xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/afp/AFPPainter.java 
(original)
+++ 
xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/afp/AFPPainter.java 
Tue Jan 13 03:28:56 2009
@@ -51,6 +51,7 @@
  import org.apache.fop.afp.modca.PresentationTextObject;
  import org.apache.fop.afp.ptoca.PtocaBuilder;
  import org.apache.fop.afp.ptoca.PtocaProducer;
+import org.apache.fop.fonts.Font;
  import org.apache.fop.fonts.FontInfo;
  import org.apache.fop.fonts.FontTriplet;
  import org.apache.fop.render.RenderingContext;
@@ -61,6 +62,7 @@
  import org.apache.fop.render.intermediate.IFState;
  import org.apache.fop.traits.BorderProps;
  import org.apache.fop.traits.RuleStyle;
+import org.apache.fop.util.CharUtilities;

  /**
   * IFPainter implementation that produces AFP (MO:DCA).
@@ -305,7 +307,7 @@
      /** {@inheritDoc} */
      public void drawText(int x, int y, final int[] dx, int[] dy, final 
String text)
              throws IFException {
-        int fontSize = this.state.getFontSize();
+        final int fontSize = this.state.getFontSize();
          getPaintingState().setFontSize(fontSize);

          FontTriplet triplet = new FontTriplet(
@@ -316,6 +318,7 @@
          // register font as necessary
          Map/*<String,FontMetrics>*/ fontMetricMap = 
documentHandler.getFontInfo().getFonts();
          final AFPFont afpFont = (AFPFont)fontMetricMap.get(fontKey);
+        final Font font = getFontInfo().getFontInstance(triplet, fontSize);
          AFPPageFonts pageFonts = getPaintingState().getPageFonts();
          AFPFontAttributes fontAttributes = 
pageFonts.registerFont(fontKey, afpFont, fontSize);

@@ -341,40 +344,41 @@
                      builder.setExtendedTextColor(state.getTextColor());
                      builder.setCodedFont((byte)fontReference);

-                    if (dx == null) {
-                        //No glyph-shifting necessary, so take a shortcut
- 
builder.addTransparentData(text.getBytes(charSet.getEncoding()));
-                    } else {
-                        int l = text.length();
-                        int dxl = dx.length;
-                        StringBuffer sb = new StringBuffer();
-
-                        if (dxl > 0 && dx[0] != 0) {
-                            int dxu = 
Math.round(unitConv.mpt2units(dx[0]));
-                            builder.relativeMoveInline(-dxu);
+                    int l = text.length();
+                    int dxl = (dx != null ? dx.length : 0);
+                    StringBuffer sb = new StringBuffer();
+
+                    if (dxl > 0 && dx[0] != 0) {
+                        int dxu = Math.round(unitConv.mpt2units(dx[0]));
+                        builder.relativeMoveInline(-dxu);
+                    }
+                    for (int i = 0; i < l; i++) {
+                        char orgChar = text.charAt(i);
+                        float glyphAdjust = 0;
+                        if (CharUtilities.isFixedWidthSpace(orgChar)) {
+                            sb.append(CharUtilities.SPACE);
+                            int spaceWidth = 
font.getCharWidth(CharUtilities.SPACE);
+                            int charWidth = font.getCharWidth(orgChar);
+                            glyphAdjust += (charWidth - spaceWidth);
+                        } else {
+                            sb.append(orgChar);
                          }
-                        for (int i = 0; i < l; i++) {
-                            sb.append(text.charAt(i));
-                            float glyphAdjust = 0;

-                            if (i < dxl - 1) {
-                                glyphAdjust += dx[i + 1];
-                            }
+                        if (i < dxl - 1) {
+                            glyphAdjust += dx[i + 1];
+                        }

-                            if (glyphAdjust != 0) {
-                                if (sb.length() > 0) {
-                                    String t = sb.toString();
- 
builder.addTransparentData(t.getBytes(charSet.getEncoding()));
-                                    sb.setLength(0);
-                                }
-                                int increment = 
Math.round(unitConv.mpt2units(glyphAdjust));
-                                builder.relativeMoveInline(increment);
+                        if (glyphAdjust != 0) {
+                            if (sb.length() > 0) {
+ 
builder.addTransparentData(charSet.encodeChars(sb));
+                                sb.setLength(0);
                              }
+                            int increment = 
Math.round(unitConv.mpt2units(glyphAdjust));
+                            builder.relativeMoveInline(increment);
                          }
-                        if (sb.length() > 0) {
-                            String t = sb.toString();
- 
builder.addTransparentData(t.getBytes(charSet.getEncoding()));
-                        }
+                    }
+                    if (sb.length() > 0) {
+ 
builder.addTransparentData(charSet.encodeChars(sb));
                      }
                  }




---------------------------------------------------------------------
To unsubscribe, e-mail: fop-commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: fop-commits-help@xmlgraphics.apache.org