This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] text layout implementation


hi,

this patch implements simple awt text layouts using attributed strings
and j2d glyph vectors. it will probably need further work, but it is
enough to get the swing text system limping along.

-graydon

2003-11-18  Graydon Hoare  <graydon@redhat.com>

	* java/awt/font/TextLayout.java: Implement simple layouts
	using attributed strings and glyph vectors.

--- java/awt/font/TextLayout.java       17 Feb 2003 08:05:50 -0000      1.1
+++ java/awt/font/TextLayout.java       18 Nov 2003 21:27:04 -0000
@@ -43,8 +43,12 @@
 import java.awt.Shape;
 import java.awt.geom.AffineTransform;
 import java.awt.geom.Rectangle2D;
+import java.text.CharacterIterator;
 import java.text.AttributedCharacterIterator;
+import java.text.AttributedString;
 import java.util.Map;
+import java.awt.font.TextAttribute;
+
 
 /**
  * @author Michael Koch
@@ -67,24 +71,26 @@
     }
   }
 
+  private AttributedString attributedString;
   private FontRenderContext fontRenderContext;
   
   public TextLayout (AttributedCharacterIterator text, FontRenderContext frc)
   {
-    // FIXME
-    this.fontRenderContext = frc;
+    attributedString = new AttributedString (text);
+    fontRenderContext = frc;
   }
 
   public TextLayout (String string, Font font, FontRenderContext frc) 
   {
-    // FIXME
-    this.fontRenderContext = frc;
+    attributedString = new AttributedString (string);
+    attributedString.addAttribute (TextAttribute.FONT, font);
+    fontRenderContext = frc;
   }
 
   public TextLayout (String string, Map attributes, FontRenderContext frc) 
   {
-    // FIXME
-    this.fontRenderContext = frc;
+    attributedString = new AttributedString (string, attributes);
+    fontRenderContext = frc;
   }
 
   protected Object clone ()
@@ -102,7 +108,42 @@
 
   public void draw (Graphics2D g2, float x, float y) 
   {
-    throw new Error ("not implemented");
+    AttributedCharacterIterator ci = attributedString.getIterator ();
+    Font defFont = g2.getFont ();
+
+    for (char c = ci.first ();
+         c != CharacterIterator.DONE;
+         c = ci.next ())
+      {                
+        Object fnt = ci.getAttribute(TextAttribute.FONT);
+
+        int limit = ci.getRunLimit(TextAttribute.FONT);
+        String run = new String();
+        while (ci.current () != CharacterIterator.DONE && 
+               ci.getIndex() < limit)
+          {
+            run += ci.current ();
+            ci.next ();
+          }
+
+        GlyphVector gv;
+        if (fnt != null && fnt instanceof Font)          
+          gv = ((Font)fnt).createGlyphVector (fontRenderContext, run);
+        else
+          gv = defFont.createGlyphVector (fontRenderContext, run);
+
+        g2.drawGlyphVector (gv, x, y);
+
+        int n = gv.getNumGlyphs ();
+        for (int i = 0; i < n; ++i)
+          {
+            GlyphMetrics gm = gv.getGlyphMetrics (i);
+            if (gm.getAdvanceX() == gm.getAdvance ())
+              x += gm.getAdvanceX ();
+            else
+              y += gm.getAdvanceY ();
+          }
+      }
   }
 
   public boolean equals (Object obj)

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]