xmlgraphics-general mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Max Berger <...@berger.name>
Subject Re: [PATCH] implementation of drawString(AttributedCharIterator,...) for AbstractGraphics2D
Date Mon, 11 Sep 2006 15:21:37 GMT
Jeremias,
developers,

strange. My attachments must have been removed. I'll send it inline  
(at the very bottom of this mail)

Max Berger
e-mail: max@berger.name

--
PGP/GnuPG ID: E81592BC   Print: F489F8759D4132923EC4  
BC7E072AB73AE81592BC
For information about me or my projects please see http:// 
max.berger.name


On Sep 10, 2006, at 6:59 AM, Jeremias Maerki wrote:

> There's no Bugzilla for XML Graphics Commons, yet. So you were  
> right to
> send the patch here in this case. However, I did not find any patches
> attached to your post.
>
> On 09.09.2006 10:48:00 Max Berger wrote:
>> Dear xml-commons developers,
>>
>> as I am unable to find an issues database for xml-commons I'll send
>> the patch here,  Is there an issues db for commons? If so, where is
>> the link to it?
>>
>> This patch implements the drawString
>> (AttributesCharacterIterator,float,float) in Abstract2DGraphics using
>> TextLayout (shamelessly stolen this idea from batik 1.6). It removes
>> the incomplete implementation in PSGraphics2D.


-------------- drawStringAttributed.patch
Index: src/java/org/apache/xmlgraphics/java2d/ps/PSGraphics2D.java
===================================================================
--- src/java/org/apache/xmlgraphics/java2d/ps/PSGraphics2D.java	 
(revision 441726)
+++ src/java/org/apache/xmlgraphics/java2d/ps/PSGraphics2D.java	 
(working copy)
@@ -678,66 +678,6 @@
      }
      /**
-     * Renders the text of the specified iterator, using the
-     * <code>Graphics2D</code> context's current <code>Paint</code>.
 
The
-     * iterator must specify a font
-     * for each character. The baseline of the
-     * first character is at position (<i>x</i>,&nbsp;<i>y</i>)
in the
-     * User Space.
-     * The rendering attributes applied include the <code>Clip</code>,
-     * <code>Transform</code>, <code>Paint</code>, and
-     * <code>Composite</code> attributes.
-     * For characters in script systems such as Hebrew and Arabic,
-     * the glyphs can be rendered from right to left, in which case the
-     * coordinate supplied is the location of the leftmost character
-     * on the baseline.
-     * @param iterator the iterator whose text is to be rendered
-     * @param x the x-coordinate where the iterator's text is to be
-     * rendered
-     * @param y the y-coordinate where the iterator's text is to be
-     * rendered
-     * @see #setPaint
-     * @see java.awt.Graphics#setColor
-     * @see #setTransform
-     * @see #setComposite
-     * @see #setClip
-     */
-    public void drawString(AttributedCharacterIterator iterator,  
float x,
-                           float y) {
-        preparePainting();
-        System.err.println("NYI: drawString 
(AttributedCharacterIterator)");
-        /*
-        try {
-            gen.writeln("BT");
-            Shape imclip = getClip();
-            writeClip(imclip);
-            establishColor(getColor());
-
-            AffineTransform trans = getTransform();
-            trans.translate(x, y);
-            double[] vals = new double[6];
-            trans.getMatrix(vals);
-
-            for (char ch = iterator.first(); ch !=  
CharacterIterator.DONE;
-                    ch = iterator.next()) {
-                //Map attr = iterator.getAttributes();
-
-                gen.writeln(gen.formatDouble(vals[0]) + " "
-                          + gen.formatDouble(vals[1]) + " "
-                          + gen.formatDouble(vals[2]) + " "
-                          + gen.formatDouble(vals[3]) + " "
-                          + gen.formatDouble(vals[4]) + " "
-                          + gen.formatDouble(vals[5]) + " "
-                          + gen.formatDouble(vals[6]) + " Tm [" + ch
-                          + "]");
-            }
-            gen.writeln("ET");
-        } catch (IOException ioe) {
-            handleIOException(ioe);
-        }*/
-    }
-
-    /**
       * Fills the interior of a <code>Shape</code> using the  
settings of the
       * <code>Graphics2D</code> context. The rendering attributes  
applied
       * include the <code>Clip</code>, <code>Transform</code>,
Index: src/java/org/apache/xmlgraphics/java2d/AbstractGraphics2D.java
===================================================================
--- src/java/org/apache/xmlgraphics/java2d/AbstractGraphics2D.java	 
(revision 441726)
+++ src/java/org/apache/xmlgraphics/java2d/AbstractGraphics2D.java	 
(working copy)
@@ -32,6 +32,7 @@
import java.awt.Stroke;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
+import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.geom.Arc2D;
import java.awt.geom.Ellipse2D;
@@ -76,6 +77,11 @@
       * Text handling strategy.
       */
      protected boolean textAsShapes = false;
+
+    /**
+     * Protection agains infinite recursion
+     */
+    protected boolean inPossibleRecursion = false;
      /**
       * @param textAsShapes if true, all text is turned into shapes  
in the
@@ -607,6 +613,31 @@
      public void drawString(String str, int x, int y){
          drawString(str, (float)x, (float)y);
      }
+
+    /**
+     * Generic implementation for drawing attributed strings using  
TextLayout.
+     *
+     * @param iterator -
+     *            the iterator whose text is to be rendered
+     * @param x
+     *            the x coordinate where the iterator's text is to  
be rendered
+     * @param y
+     *            the y coordinate where the iterator's text is to  
be rendered
+     * @see java.awt.Graphics2D#drawString 
(java.text.AttributedCharacterIterator,
+     *      float, float)
+     */
+    public void drawString(AttributedCharacterIterator iterator,  
float x,
+            float y) {
+        if (inPossibleRecursion) {
+            System.err
+                    .println("Called itself: drawString 
(AttributedCharacterIterator)");
+        } else {
+            inPossibleRecursion = true;
+            TextLayout layout = new TextLayout(iterator,  
getFontRenderContext());
+            layout.draw(this, x, y);
+            inPossibleRecursion = false;
+        }
+    }
      /**
-------------- PSTest.java
package name.berger.max.test.xmlgraphics;

import java.awt.font.TextAttribute;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.AttributedString;

import org.apache.xmlgraphics.java2d.GraphicContext;
import org.apache.xmlgraphics.java2d.ps.PSDocumentGraphics2D;

public class PSTest {

     /**
      * @param args
      */
     public static void main(String[] args) {
         try {
             PSDocumentGraphics2D doc = new PSDocumentGraphics2D(false,
                     new FileOutputStream("/tmp/test.ps"), 612, 792);
             doc.setGraphicContext(new GraphicContext());
             doc.drawString("This is a regular text", 72, 72);

             AttributedString aString = new AttributedString(
                     "This is a special Text");

             aString.addAttribute(TextAttribute.FAMILY, "SansSerif");
             aString.addAttribute(TextAttribute.FAMILY, "Serif", 10,  
17);

             doc.drawString(aString.getIterator(), 72, 144);

             doc.finish();
         } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }

     }
}
--------------


Mime
View raw message