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]

[gui][PATCH] fix attributed strings and iterators


hi,

this fix integrates the patches Guilhem had waiting against AttributedString / AttributedStringIterator and the one I did. this enables proper glyph vector painting with java.awt.TextLayout (previously it was truncating characters).

-graydon

2004-03-23 Graydon Hoare <graydon@redhat.com>

	* java/text/AttributedString.java
	(addAttribute): Fix off-by-one.
	(getIterator): Likewise.	
	* java/text/AttributedStringIterator.java
	(getRunLimit): Correct logic.
	(getRunStart): Likewise.
	(getAttribute): Fix inequality.
	(getAttributes): Likewise.
	* testsuite/libjava.mauve/xfails: Remove AttributedString xfail.

diff -u -b -w -r1.2 AttributedString.java
--- java/text/AttributedString.java	22 Jan 2002 22:40:37 -0000	1.2
+++ java/text/AttributedString.java	23 Mar 2004 22:31:10 -0000
@@ -38,6 +38,7 @@
 
 package java.text;
 
+import java.util.Arrays;
 import java.util.Iterator;
 import java.util.Hashtable;
 import java.util.HashSet;
@@ -218,26 +219,7 @@
   // Get the valid attribute list
   Set all_attribs = aci.getAllAttributeKeys();
   if (attributes != null)
-    {
-      Set valid_attribs = new HashSet();
-      Iterator iter = all_attribs.iterator();
-      while (iter.hasNext())
-        {
-          Object obj = iter.next();
-
-          int i;
-          for (i = 0; i < attributes.length; i++)
-            if (obj.equals(attributes[0]))
-              break;
-
-          if (i == attributes.length)
-            continue;
-
-          valid_attribs.add(obj);
-        }
-
-      all_attribs = valid_attribs;
-    } 
+    all_attribs.retainAll(Arrays.asList(attributes));
 
   // Loop through and extract the attributes
   char c = aci.setIndex(begin_index);
@@ -319,7 +301,7 @@
 public void
 addAttribute(AttributedCharacterIterator.Attribute attrib, Object value)
 {
-  addAttribute(attrib, value, 0, sci.getEndIndex() - 1);
+  addAttribute(attrib, value, 0, sci.getEndIndex());
 }
 
 /*************************************************************************/
@@ -388,8 +370,7 @@
 public AttributedCharacterIterator
 getIterator()
 {
-  return(new AttributedStringIterator(sci, attribs, 0, sci.getEndIndex() - 1,
-                                      null));
+  return(new AttributedStringIterator(sci, attribs, 0, sci.getEndIndex(), null));
 }
 
 /*************************************************************************/
@@ -408,7 +389,7 @@
 public AttributedCharacterIterator
 getIterator(AttributedCharacterIterator.Attribute[] attributes)
 {
-  return(getIterator(attributes, 0, sci.getEndIndex() - 1));
+  return(getIterator(attributes, 0, sci.getEndIndex()));
 }
 
 /*************************************************************************/
--- java/text/AttributedStringIterator.java	22 Jan 2002 22:40:37 -0000	1.2
+++ java/text/AttributedStringIterator.java	23 Mar 2004 22:31:10 -0000
@@ -181,6 +181,10 @@
 
   for (int i = 0; i < attribs.length; i++)
     {
+      if (attribs[i].begin_index > getEndIndex()
+         || attribs[i].end_index <= getBeginIndex())
+       continue;
+
       Set key_set = attribs[i].attribs.keySet();
       Iterator iter = key_set.iterator();
       while (iter.hasNext())
@@ -217,39 +221,28 @@
 public synchronized int
 getRunLimit(Set attribute_set)
 {
-  int orig_index = ci.getIndex();
-  int run_limit;
+  boolean hit = false;
+  int runLimit = ci.getEndIndex ();
+  int pos = ci.getIndex ();
 
-  do  
+  for (int i = 0; i < attribs.length; ++i)
+    {
+      if (pos >= attribs[i].begin_index &&
+          pos <= attribs[i].end_index)
     {
-      run_limit = ci.getIndex();
-
-      Map attribute_map = getAttributes();
-
-      boolean found = false;
       Iterator iter = attribute_set.iterator();
       while(iter.hasNext()) 
-        if (!attribute_map.containsKey(iter.next()))
+            if (attribs[i].attribs.containsKey(iter.next()))
           {
-            found = true;
-            break;
+                hit = true;
+                runLimit = Math.min(runLimit, attribs[i].end_index);
           }
-
-      if (found)
-        break;
     }
-  while (ci.next() != CharacterIterator.DONE);
-
-  boolean hit_end = (ci.previous() == CharacterIterator.DONE);
-
-  ci.setIndex(orig_index);
-
-  if (run_limit == orig_index)
-    return(-1); // No characters match the given attributes
-//  else if (!hit_end)
-//    --run_limit;
-
-  return(run_limit); 
+    }
+  if (hit)
+    return runLimit;
+  else
+    return -1;
 }
 
 /*************************************************************************/
@@ -277,35 +270,28 @@
 public int
 getRunStart(Set attribute_set)
 {
-  int orig_index = ci.getIndex();
-  int run_start;
+  boolean hit = false;
+  int runBegin = 0;
+  int pos = ci.getIndex ();
 
-  do  
+  for (int i = 0; i < attribs.length; ++i)
+    {
+      if (pos >= attribs[i].begin_index &&
+          pos <= attribs[i].end_index)
     {
-      run_start = ci.getIndex();
-
-      Map attribute_map = getAttributes();
-
       Iterator iter = attribute_set.iterator();
       while(iter.hasNext())
-        if (!attribute_map.containsKey(iter.next()))
-          break;
-
-      if (iter.hasNext())
-        break;
+            if (attribs[i].attribs.containsKey(iter.next()))
+              {
+                hit = true;
+                runBegin = Math.max(runBegin, attribs[i].begin_index);
     }
-  while (ci.previous() != CharacterIterator.DONE);
-
-  boolean hit_beginning = (ci.previous() == CharacterIterator.DONE);
-
-  ci.setIndex(orig_index);
-
-  if (run_start == orig_index)
-    return(-1); // No characters match the given attributes
-  else if (!hit_beginning)
-    ++run_start;
-
-  return(run_start); 
+        }
+    }
+  if (hit)
+    return runBegin;
+  else
+    return -1;
 }
 
 /*************************************************************************/
@@ -327,7 +313,7 @@
           // Check for attribute match and range match
           if (obj.equals(attrib))
             if ((ci.getIndex() >= attribs[i].begin_index) &&
-                (ci.getIndex() <= attribs[i].end_index))
+                (ci.getIndex() < attribs[i].end_index))
               return(attribs[i].attribs.get(obj));
         }
     }
@@ -351,7 +337,7 @@
   for (int i = 0; i < attribs.length; i++)
     {
        if ((ci.getIndex() >= attribs[i].begin_index) &&
-           (ci.getIndex() <= attribs[i].end_index))
+           (ci.getIndex() < attribs[i].end_index))
          m.putAll(attribs[i].attribs);
     }
 
--- testsuite/libjava.mauve/xfails	31 Dec 2003 10:29:05 -0000	1.20
+++ testsuite/libjava.mauve/xfails	23 Mar 2004 22:31:10 -0000
@@ -32,7 +32,6 @@
 FAIL: gnu.testlet.java.lang.String.getBytes14: String.getBytes("ISO8859_15") (number 1)
 FAIL: gnu.testlet.java.lang.String.getBytes14: String.getBytes("UTF-16BE") (number 1)
 FAIL: gnu.testlet.java.lang.String.getBytes14: String.getBytes("UTF-16LE") (number 1)
-FAIL: gnu.testlet.java.text.AttributedString.Test: Attribute key count (number 1)
 FAIL: gnu.testlet.java.text.DateFormatSymbols.Test: patterns (number 2)
 FAIL: gnu.testlet.java.text.SimpleDateFormat.getAndSet2DigitYearStart: get2DigitYearStart() initial (number 1)
 FAIL: gnu.testlet.java.text.DateFormatSymbols.Test: invalid locale (number 1)

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