This improves the ListPainter so that the bullets in HTML lists are
correctly aligned wrt to the list item. This patch also removes some
debug output.

2006-11-19  Roman Kennke  <[EMAIL PROTECTED]>

        * javax/swing/text/html/StyleSheet.java
        (getFontSize): Removed debug output.
        (ListPainter.tmpRect): New field.
        (ListPainter.paint): Align bullet vertically centered to
        the first line of the paragraph.

/Roman

Index: javax/swing/text/html/StyleSheet.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/html/StyleSheet.java,v
retrieving revision 1.17
diff -u -1 -5 -r1.17 StyleSheet.java
--- javax/swing/text/html/StyleSheet.java	17 Nov 2006 22:12:11 -0000	1.17
+++ javax/swing/text/html/StyleSheet.java	19 Nov 2006 18:44:09 -0000
@@ -38,30 +38,32 @@
 
 package javax.swing.text.html;
 
 import gnu.javax.swing.text.html.css.CSSColor;
 import gnu.javax.swing.text.html.css.CSSParser;
 import gnu.javax.swing.text.html.css.CSSParserCallback;
 import gnu.javax.swing.text.html.css.FontSize;
 import gnu.javax.swing.text.html.css.FontStyle;
 import gnu.javax.swing.text.html.css.FontWeight;
 import gnu.javax.swing.text.html.css.Length;
 import gnu.javax.swing.text.html.css.Selector;
 
 import java.awt.Color;
 import java.awt.Font;
 import java.awt.Graphics;
+import java.awt.Rectangle;
+import java.awt.Shape;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.Reader;
 import java.io.Serializable;
 import java.io.StringReader;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 
@@ -884,32 +886,32 @@
    *
    * @param atts the attributes
    *
    * @return the resolved font size
    */
   private int getFontSize(AttributeSet atts)
   {
     int size = 12;
     if (atts.isDefined(CSS.Attribute.FONT_SIZE))
       {
         FontSize fs = (FontSize) atts.getAttribute(CSS.Attribute.FONT_SIZE);
         if (fs.isRelative())
           {
             int parSize = 12;
             AttributeSet resolver = atts.getResolveParent();
-            if (resolver != null) {
-              parSize = getFontSize(resolver);System.err.println("parent size: " + parSize);    }
+            if (resolver != null)
+              parSize = getFontSize(resolver);
             size = fs.getValue(parSize); 
           }
         else
           {
             size = fs.getValue();
           }
       }
     else
       {
         AttributeSet resolver = atts.getResolveParent();
         if (resolver != null)
           size = getFontSize(resolver);
       }
     return size;
   }
@@ -1262,45 +1264,75 @@
     private String type;
 
     /**
      * Package-private constructor.
      * 
      * @param as - AttributeSet for painter
      */
     ListPainter(AttributeSet as, StyleSheet ss)
     {
       attributes = as;
       styleSheet = ss;
       type = (String) as.getAttribute(CSS.Attribute.LIST_STYLE_TYPE);
     }
 
     /**
+     * Cached rectangle re-used in the paint method below.
+     */
+    private final Rectangle tmpRect = new Rectangle();
+
+    /**
      * Paints the CSS list decoration according to the attributes given.
      * 
      * @param g - the graphics configuration
      * @param x - the x coordinate
      * @param y - the y coordinate
      * @param w - the width of the allocated area
      * @param h - the height of the allocated area
      * @param v - the view making the request
      * @param item - the list item to be painted >=0.
      */
     public void paint(Graphics g, float x, float y, float w, float h, View v,
                       int item)
     {
       // FIXME: This is a very simplistic list rendering. We still need
       // to implement different bullet types (see type field) and custom
       // bullets via images.
       View itemView = v.getView(item);
       AttributeSet viewAtts = itemView.getAttributes();
       Object tag = viewAtts.getAttribute(StyleConstants.NameAttribute);
       // Only paint something here when the child view is an LI tag
       // and the calling view is some of the list tags then).
       if (tag != null && tag == HTML.Tag.LI)
         {
           g.setColor(Color.BLACK);
-          g.fillOval((int) x - 15, (int) (h / 2 - 3 + y), 6, 6);
+          int centerX = (int) (x - 12);
+          int centerY = -1;
+          // For paragraphs (almost all cases) center bullet vertically
+          // in the middle of the first line.
+          tmpRect.setBounds((int) x, (int) y, (int) w, (int) h);
+          if (itemView.getViewCount() > 0)
+            {
+              View v1 = itemView.getView(0);
+              if (v1 instanceof ParagraphView && v1.getViewCount() > 0)
+                {             
+                  Shape a1 = itemView.getChildAllocation(0, tmpRect);
+                  Rectangle r1 = a1 instanceof Rectangle ? (Rectangle) a1
+                                                         : a1.getBounds();
+                  ParagraphView par = (ParagraphView) v1;
+                  Shape a = par.getChildAllocation(0, r1);
+                  Rectangle r = a instanceof Rectangle ? (Rectangle) a
+                                                       : a.getBounds();
+                  centerY = (int) (r.height / 2 + r.y);
+                }
+            }
+          if (centerY == -1)
+            {
+              System.err.println("WARNING LI child is not a paragraph view " + itemView.getView(0) + ", " + itemView.getViewCount());
+              centerY =(int) (h / 2 + y);
+            }
+          g.fillOval(centerX - 3, centerY - 3, 6, 6);
         }
     }
   }
 
 }

Reply via email to