Here comes support for the CSS margin(-*) and text-align attributes, which makes tags that use this (like the <h*> and <center> tags) look more reasonable.

2006-08-31  Roman Kennke  <[EMAIL PROTECTED]>

        * javax/swing/text/BoxView.java
        (getWidth): Return the width with insets added, not with one
        added and one removed.
        (getHeight): Return the height with insets added, not with one
        added and one removed.
        * javax/swing/text/GlyphView.java
        (DefaultGlyphPainter.viewToModel): Need to add the start offset.
        * javax/swing/text/ParagraphView.java
        (Row.getAlignment): Adjust alignment with respect to
        the justification attribute.
        (Row.getLeftInset): Overridden to adjust for firstLineIndent
        attribute.
        * javax/swing/text/html/CSS.java
        (getValue): Convert length values.
        * javax/swing/text/html/Paragraph.java
        (painter): New field.
        (paint): Implemented to delegate painting to the BoxPainter too.
        (setPropertiesFromAttributes): Implemented to load attributes
        from CSS.
        * javax/swing/text/html/StyleSheet.java
        (BoxPainter.as): Removed field.
        (BoxPainter.leftInset): New field.
        (BoxPainter.bottomInset): New field.
        (BoxPainter.rightInset): New field.
        (BoxPainter.topInset): New field.
        (BoxPainter.BoxPainter): Implemented to load the insets from
        CSS.
        (BoxPainter.getInset): Implemented.
        * gnu/javax/swing/text/html/Length.java: New class.
        Converts CSS length units to usable values.

/Roman
Index: gnu/javax/swing/text/html/css/Length.java
===================================================================
RCS file: gnu/javax/swing/text/html/css/Length.java
diff -N gnu/javax/swing/text/html/css/Length.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ gnu/javax/swing/text/html/css/Length.java	31 Aug 2006 21:05:42 -0000
@@ -0,0 +1,90 @@
+/* Length.java -- Converts CSS length values
+   Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package gnu.javax.swing.text.html.css;
+
+/**
+ * Converts CSS length values to usable length values.
+ *
+ * @author Roman Kennke ([EMAIL PROTECTED])
+ */
+public class Length
+{
+
+  /**
+   * The original value.
+   */
+  private String value;
+
+  /**
+   * The converted value.
+   */
+  private float floatValue;
+
+  /**
+   * Creates a new length converter instance.
+   *
+   * @param val the CSS value
+   */
+  public Length(String val)
+  {
+    value = val;
+    int i = value.indexOf("px");
+    floatValue = 0.0F;
+    if (i != -1)
+      {
+        String sub = value.substring(0, i);
+        floatValue = Float.parseFloat(sub);
+      }
+    else
+      {
+        // TODO: Implement other length options.
+        floatValue = Float.parseFloat(value);
+      }
+  }
+
+  /**
+   * Returns the value converted to pixels.
+   *
+   * @return the value converted to pixels
+   */
+  public float getValue()
+  {
+    return floatValue;
+  }
+}
Index: javax/swing/text/BoxView.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/BoxView.java,v
retrieving revision 1.24
diff -u -1 -2 -r1.24 BoxView.java
--- javax/swing/text/BoxView.java	28 Aug 2006 21:41:57 -0000	1.24
+++ javax/swing/text/BoxView.java	31 Aug 2006 21:05:42 -0000
@@ -901,35 +901,39 @@
   protected boolean isAllocationValid()
   {
     return isLayoutValid(X_AXIS) && isLayoutValid(Y_AXIS);
   }
 
   /**
    * Return the current width of the box. This is the last allocated width.
    *
    * @return the current width of the box
    */
   public int getWidth()
   {
-    return span[X_AXIS] + getLeftInset() - getRightInset();
+    // The RI returns the following here, however, I'd think that is a bug.
+    // return span[X_AXIS] + getLeftInset() - getRightInset();
+    return span[X_AXIS] + getLeftInset() + getRightInset();
   }
 
   /**
    * Return the current height of the box. This is the last allocated height.
    *
    * @return the current height of the box
    */
   public int getHeight()
   {
-    return span[Y_AXIS] + getTopInset() - getBottomInset();
+    // The RI returns the following here, however, I'd think that is a bug.
+    // return span[Y_AXIS] + getTopInset() - getBottomInset();
+    return span[Y_AXIS] + getTopInset() + getBottomInset();
   }
 
   /**
    * Sets the size of the view. If the actual size has changed, the layout
    * is updated accordingly.
    *
    * @param width the new width
    * @param height the new height
    */
   public void setSize(float width, float height)
   {
     layout((int) (width - getLeftInset() - getRightInset()),
Index: javax/swing/text/GlyphView.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/GlyphView.java,v
retrieving revision 1.20
diff -u -1 -2 -r1.20 GlyphView.java
--- javax/swing/text/GlyphView.java	25 Aug 2006 11:20:46 -0000	1.20
+++ javax/swing/text/GlyphView.java	31 Aug 2006 21:05:43 -0000
@@ -446,25 +446,25 @@
      * @param x the X coordinate of the visual position
      * @param y the Y coordinate of the visual position
      * @param a the allocated region
      * @param biasRet filled with the bias of the model location on method exit
      *
      * @return the model location that represents the specified view location
      */
     public int viewToModel(GlyphView v, float x, float y, Shape a,
                            Bias[] biasRet)
     {
       Rectangle b = a.getBounds();
       int pos = getBoundedPosition(v, v.getStartOffset(), b.x, x - b.x);
-      return pos;
+      return pos + v.getStartOffset();
     }
   }
 
   /**
    * The GlyphPainer used for painting the glyphs.
    */
   GlyphPainter glyphPainter;
 
   /**
    * The start offset within the document for this view.
    */
   private int startOffset;
Index: javax/swing/text/ParagraphView.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/ParagraphView.java,v
retrieving revision 1.11
diff -u -1 -2 -r1.11 ParagraphView.java
--- javax/swing/text/ParagraphView.java	25 Aug 2006 11:20:46 -0000	1.11
+++ javax/swing/text/ParagraphView.java	31 Aug 2006 21:05:43 -0000
@@ -56,29 +56,57 @@
    * one row in a <code>ParagraphView</code>.
    */
   class Row extends BoxView
   {
     /**
      * Creates a new instance of <code>Row</code>.
      */
     Row(Element el)
     {
       super(el, X_AXIS);
     }
 
+    /**
+     * Overridden to adjust when we are the first line, and firstLineIndent
+     * is not 0.
+     */
+    public short getLeftInset()
+    {
+      short leftInset = super.getLeftInset();
+      View parent = getParent();
+      if (parent != null)
+        {
+          if (parent.getView(0) == this)
+            leftInset += firstLineIndent;
+        }
+      return leftInset;
+    }
+
     public float getAlignment(int axis)
     {
       float align;
       if (axis == X_AXIS)
-        align = 0.0F; // TODO: Implement according to justification
+        switch (justification)
+          {
+          case StyleConstants.ALIGN_RIGHT:
+            align = 1.0F;
+            break;
+          case StyleConstants.ALIGN_CENTER:
+          case StyleConstants.ALIGN_JUSTIFIED:
+            align = 0.5F;
+            break;
+          case StyleConstants.ALIGN_LEFT:
+          default:
+            align = 0.0F;
+          }
       else
         align = super.getAlignment(axis);
       return align;
     }
 
     /**
      * Allows rows to span the whole parent view.
      */
     public float getMaximumSpan(int axis)
     {
       float max;
       if (axis == X_AXIS)
Index: javax/swing/text/html/CSS.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/html/CSS.java,v
retrieving revision 1.5
diff -u -1 -2 -r1.5 CSS.java
--- javax/swing/text/html/CSS.java	25 Aug 2006 11:40:44 -0000	1.5
+++ javax/swing/text/html/CSS.java	31 Aug 2006 21:05:44 -0000
@@ -32,24 +32,25 @@
 module.  An independent module is a module which is not derived from
 or based on this library.  If you modify this library, you may extend
 this exception to your version of the library, but you are not
 obligated to do so.  If you do not wish to do so, delete this
 exception statement from your version. */
 
 package javax.swing.text.html;
 
 import gnu.javax.swing.text.html.css.CSSColor;
 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 java.io.Serializable;
 import java.util.HashMap;
 
 /**
  * Provides CSS attributes to be used by the HTML view classes. The constants
  * defined here are used as keys for text attributes for use in
  * [EMAIL PROTECTED] javax.swing.text.AttributeSet}s of [EMAIL PROTECTED] javax.swing.text.Element}s.
  *
  * @author Roman Kennke ([EMAIL PROTECTED])
  */
 public class CSS implements Serializable
@@ -476,17 +477,21 @@
    */
   static Object getValue(Attribute att, String v)
   {
     Object o;
     if (att == Attribute.FONT_SIZE)
       o = new FontSize(v);
     else if (att == Attribute.FONT_WEIGHT)
       o = new FontWeight(v);
     else if (att == Attribute.FONT_STYLE)
       o = new FontStyle(v);
     else if (att == Attribute.COLOR || att == Attribute.BACKGROUND_COLOR)
       o = new CSSColor(v);
+    else if (att == Attribute.MARGIN || att == Attribute.MARGIN_BOTTOM
+             || att == Attribute.MARGIN_LEFT || att == Attribute.MARGIN_RIGHT
+             || att == Attribute.MARGIN_TOP)
+      o = new Length(v);
     else
       o = v;
     return o;
   }
 }
Index: javax/swing/text/html/ParagraphView.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/html/ParagraphView.java,v
retrieving revision 1.2
diff -u -1 -2 -r1.2 ParagraphView.java
--- javax/swing/text/html/ParagraphView.java	24 Aug 2006 16:58:11 -0000	1.2
+++ javax/swing/text/html/ParagraphView.java	31 Aug 2006 21:05:44 -0000
@@ -30,49 +30,56 @@
 terms of your choice, provided that you also meet, for each linked
 independent module, the terms and conditions of the license of that
 module.  An independent module is a module which is not derived from
 or based on this library.  If you modify this library, you may extend
 this exception to your version of the library, but you are not
 obligated to do so.  If you do not wish to do so, delete this
 exception statement from your version. */
 
 
 package javax.swing.text.html;
 
 import java.awt.Graphics;
+import java.awt.Rectangle;
 import java.awt.Shape;
 
 import javax.swing.SizeRequirements;
 import javax.swing.text.AttributeSet;
 import javax.swing.text.Document;
 import javax.swing.text.Element;
+import javax.swing.text.StyleConstants;
 import javax.swing.text.View;
 
 /**
  * Renders a paragraph in HTML. This is a subclass of
  * [EMAIL PROTECTED] javax.swing.text.ParagraphView} with some adjustments for
  * understanding stylesheets.
  *
  * @author Roman Kennke ([EMAIL PROTECTED])
  */
 public class ParagraphView
-    extends javax.swing.text.ParagraphView
+  extends javax.swing.text.ParagraphView
 {
 
   /**
    * The attributes used by this view.
    */
   private AttributeSet attributes;
 
   /**
+   * The stylesheet's box painter.
+   */
+  private StyleSheet.BoxPainter painter;
+
+  /**
    * Creates a new ParagraphView for the specified element.
    *
    * @param element the element
    */
   public ParagraphView(Element element)
   {
     super(element);
   }
 
   /**
    * Sets the parent of this view. This is implemented to call the parent
    * functionality and then trigger [EMAIL PROTECTED] #setPropertiesFromAttributes} in
@@ -97,25 +104,50 @@
       {
         attributes = getStyleSheet().getViewAttributes(this);
       }
     return attributes;
   }
 
   /**
    * Loads the visual properties of the ParagraphView from the element's
    * attributes and the stylesheet of the HTML document.
    */
   protected void setPropertiesFromAttributes()
   {
-    // FIXME: Implement this.
+    super.setPropertiesFromAttributes();
+
+    // Fetch CSS attributes.
+    AttributeSet atts = getAttributes();
+    Object o = atts.getAttribute(CSS.Attribute.TEXT_ALIGN);
+    if (o != null)
+      {
+        String align = o.toString();
+        if (align.equals("left"))
+          setJustification(StyleConstants.ALIGN_LEFT);
+        else if (align.equals("right"))
+          setJustification(StyleConstants.ALIGN_RIGHT);
+        else if (align.equals("center"))
+          setJustification(StyleConstants.ALIGN_CENTER);
+        else if (align.equals("justify"))
+          setJustification(StyleConstants.ALIGN_JUSTIFIED);
+      }
+
+    // Fetch StyleSheet's box painter.
+    painter = getStyleSheet().getBoxPainter(atts);
+    setInsets((short) painter.getInset(TOP, this),
+              (short) painter.getInset(LEFT, this),
+              (short) painter.getInset(BOTTOM, this),
+              (short) painter.getInset(RIGHT, this));
+
+    // TODO: Handle CSS width and height attributes somehow.
   }
 
   /**
    * Returns the stylesheet used by this view.
    *
    * @return the stylesheet used by this view
    */
   protected StyleSheet getStyleSheet()
   {
     Document doc = getDocument();
     StyleSheet styleSheet = null;
     if (doc instanceof HTMLDocument)
@@ -146,33 +178,38 @@
    * visible and the only visible child is the break that ends the paragraph,
    * this paragraph is not considered to be visible.
    *
    * @return the visibility of this paragraph
    */
   public boolean isVisible()
   {
     // FIXME: Implement the above specified behaviour.
     return super.isVisible();
   }
 
   /**
-   * Paints this view. This delegates to the superclass after the coordinates
-   * have been updated for tab calculations.
+   * Paints this view. This paints the box using the stylesheet's
+   * box painter for this view and delegates to the super class paint()
+   * afterwards.
    *
    * @param g the graphics object
    * @param a the current allocation of this view
    */
   public void paint(Graphics g, Shape a)
   {
-    // FIXME: Implement the above specified behaviour.
+    if (a != null)
+      {
+        Rectangle r = a instanceof Rectangle ? (Rectangle) a : a.getBounds();
+        painter.paint(g, r.x, r.y, r.width, r.height, this);
+      }
     super.paint(g, a);
   }
 
   /**
    * Returns the preferred span of this view. If this view is not visible,
    * we return <code>0</code>, otherwise the super class is called.
    *
    * @param axis the axis
    *
    * @return the preferred span of this view
    */
   public float getPreferredSpan(int axis)
Index: javax/swing/text/html/StyleSheet.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/html/StyleSheet.java,v
retrieving revision 1.8
diff -u -1 -2 -r1.8 StyleSheet.java
--- javax/swing/text/html/StyleSheet.java	25 Aug 2006 11:40:44 -0000	1.8
+++ javax/swing/text/html/StyleSheet.java	31 Aug 2006 21:05:44 -0000
@@ -36,24 +36,25 @@
 exception statement from your version. */
 
 
 package javax.swing.text.html;
 
 import gnu.classpath.NotImplementedException;
 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 java.awt.Color;
 import java.awt.Font;
 import java.awt.Graphics;
 import java.io.IOException;
 import java.io.Reader;
 import java.io.Serializable;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.List;
@@ -896,55 +897,84 @@
   
   /**
    * This class carries out some of the duties of CSS formatting. This enables views
    * to present the CSS formatting while not knowing how the CSS values are cached.
    * 
    * This object is reponsible for the insets of a View and making sure
    * the background is maintained according to the CSS attributes.
    * 
    * @author Lillian Angel ([EMAIL PROTECTED])
    */
   public static class BoxPainter extends Object implements Serializable
   {
-    
-    /**
-     * Attribute set for painter
-     */
-    AttributeSet as;
-    
+
+    private float leftInset;
+    private float rightInset;
+    private float topInset;
+    private float bottomInset;
+
     /**
      * Package-private constructor.
      * 
      * @param as - AttributeSet for painter
      */
     BoxPainter(AttributeSet as)
     {
-      this.as = as;
+      Length l = (Length) as.getAttribute(CSS.Attribute.MARGIN_LEFT);
+      if (l != null)
+        leftInset = l.getValue();
+      l = (Length) as.getAttribute(CSS.Attribute.MARGIN_RIGHT);
+      if (l != null)
+        rightInset = l.getValue();
+      l = (Length) as.getAttribute(CSS.Attribute.MARGIN_TOP);
+      if (l != null)
+        topInset = l.getValue();
+      l = (Length) as.getAttribute(CSS.Attribute.MARGIN_BOTTOM);
+      if (l != null)
+        bottomInset = l.getValue();
     }
     
+    
     /**
      * Gets the inset needed on a given side to account for the margin, border
      * and padding.
      * 
      * @param size - the size of the box to get the inset for. View.TOP, View.LEFT,
      * View.BOTTOM or View.RIGHT.
      * @param v - the view making the request. This is used to get the AttributeSet,
      * amd may be used to resolve percentage arguments.
      * @return the inset
      * @throws IllegalArgumentException - for an invalid direction.
      */
     public float getInset(int size, View v)
     {
-      // FIXME: Not implemented.
-      return 0;       
+      float inset;
+      switch (size)
+        {
+        case View.TOP:
+          inset = topInset;
+          break;
+        case View.BOTTOM:
+          inset = bottomInset;
+          break;
+        case View.LEFT:
+          inset = leftInset;
+          break;
+        case View.RIGHT:
+          inset = rightInset;
+          break;
+        default:
+          inset = 0.0F;
+      }
+      return inset;
     }
     
     /**
      * Paints the CSS box according to the attributes given. This should
      * paint the border, padding and background.
      * 
      * @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

Reply via email to