FileDocCategorySizeDatePackage
RenderContext.javaAPI DocphoneME MR2 API (J2ME)25419Wed May 02 18:00:34 BST 2007com.sun.perseus.j2d

RenderContext

public class RenderContext extends Object implements TextRenderingProperties
{@link com.sun.perseus.model.ModelNode ModelNode} are rendered into a {@link RenderGraphics RenderGraphics} which extends the RenderContext class. A RenderContext captures the current state of the properties used in painting operations. When a ModelNode tree is rendered, the RenderGraphics is passed down the tree to children nodes as they are painted. This provides the solution for computing CSS inherited values: if a ModelNode inherits one of its properties, it will simply use the one currently set in the RenderContext.
Initially (i.e., before any painting operation happens), the RenderContext holds values which correspond to the CSS 2 concept of initial values.
At any time during a rendering operation, the RenderContext holds values which correspond to the CSS 2 computed values. Note that in addition to properties that correspond to CSS2 properties, the RenderContext also keeps an AffineTransform which is not a CSS2 property. The transform represents the current user space to device space transformation.
see
com.sun.perseus.model.ModelNode
see
com.sun.perseus.model.ElementNode#paint
version
$Id: RenderContext.java,v 1.5 2006/04/21 06:35:38 st125089 Exp $

Fields Summary
protected static int
INITIAL_STROKE_WIDTH_IMPL
Default stroke width
protected static int
INITIAL_STROKE_MITER_LIMIT_IMPL
Default miter limit
protected static int[]
INITIAL_STROKE_DASH_ARRAY_IMPL
Default dash array
protected static int
INITIAL_STROKE_DASH_OFFSET_IMPL
Default dash offset
protected static final int
FONT_STYLE_MASK
protected static final int
FONT_WEIGHT_MASK
protected static final int
TEXT_ANCHOR_MASK
protected static final int
STROKE_OPACITY_MASK
protected static final int
FILL_OPACITY_MASK
protected static final int
FILL_RULE_MASK
protected static final int
STROKE_LINE_CAP_MASK
protected static final int
STROKE_LINE_JOIN_MASK
protected static final int
VISIBILITY_MASK
protected static final int
DISPLAY_MASK
protected static final int
OPACITY_MASK
protected static final int
FONT_STYLE_NORMAL_IMPL
protected static final int
FONT_STYLE_ITALIC_IMPL
protected static final int
FONT_STYLE_OBLIQUE_IMPL
protected static final int
FONT_WEIGHT_100_IMPL
protected static final int
FONT_WEIGHT_200_IMPL
protected static final int
FONT_WEIGHT_300_IMPL
protected static final int
FONT_WEIGHT_400_IMPL
protected static final int
FONT_WEIGHT_500_IMPL
protected static final int
FONT_WEIGHT_600_IMPL
protected static final int
FONT_WEIGHT_700_IMPL
protected static final int
FONT_WEIGHT_800_IMPL
protected static final int
FONT_WEIGHT_900_IMPL
protected static final int
TEXT_ANCHOR_MIDDLE_IMPL
protected static final int
TEXT_ANCHOR_START_IMPL
protected static final int
TEXT_ANCHOR_END_IMPL
protected static final int
CAP_BUTT_IMPL
protected static final int
CAP_ROUND_IMPL
protected static final int
CAP_SQUARE_IMPL
protected static final int
JOIN_MITER_IMPL
protected static final int
JOIN_ROUND_IMPL
protected static final int
JOIN_BEVEL_IMPL
protected PaintServer
fill
Color used for fill operations
protected PaintServer
stroke
Color used for fill operations
protected RGB
color
Context Color. Corresponds to the concept of 'current color' in CSS
protected int[]
strokeDashArray
Controls the strokeDash pattern for stroke operations. If the array is null, a solid line is used
protected int
strokeWidth
Controls the width of the stroke. 15.16 format.
protected int
strokeMiterLimit
Controls the miter limit for stroke operations. 15.16 format.
protected int
strokeDashOffset
Controls the offset in the dash array for stroke operations. 15.16 format.
protected String[]
fontFamily
Controls the current font family. The font family is a list, which means that a set of fonts may be used to render a given text string. The fonts are queried in order to check which ones can render characters in the drawn text.
protected float
fontSize
Controls the current font size
protected int
pack
protected static final int
INITIAL_FONT_STYLE_IMPL
protected static final int
INITIAL_FONT_WEIGHT_IMPL
protected static final int
INITIAL_TEXT_ANCHOR_IMPL
protected static final int
INITIAL_STROKE_OPACITY_IMPL
protected static final int
INITIAL_FILL_OPACITY_IMPL
protected static final int
INITIAL_FILL_RULE_IMPL
protected static final int
INITIAL_STROKE_LINE_CAP_IMPL
protected static final int
INITIAL_STROKE_LINE_JOIN_IMPL
protected static final int
INITIAL_VISIBILITY_IMPL
protected static final int
INITIAL_DISPLAY_IMPL
protected static final int
INITIAL_PACK
protected int
pack2
protected static final int
INITIAL_OPACITY_IMPL
protected static final int
INITIAL_PACK2
Constructors Summary
public RenderContext()
Default constructor


           
      
    
public RenderContext(RenderContext rcs)
Copy constructor

param
rcs the RenderContext to copy values from. Should not be null.

        restore(rcs);
    
Methods Summary
intcomputeStrokeDashOffset()
Implemnetation: Handles negative strokeDashOffset

return
a positive strokeDashOffset.

        if (strokeDashArray == null) {
            // The stroke dash offset does not matter, simply return 0
            return 0;
        }

        if (strokeDashOffset >= 0) {
            return strokeDashOffset;
        }

        int length = 0;
        for (int i = 0; i < strokeDashArray.length; i++) {
            length += strokeDashArray[i];
        }

        if (length <= 0) {
            return 0;
        }

        int sdo = strokeDashOffset;
        while (sdo < 0) {
            sdo += length;
        }

        return sdo;
    
public RGBgetColor()

return
the context's color property value

        return this.color;
    
public booleangetDisplay()

return
the context' display property value

        return ((pack & DISPLAY_MASK) == DISPLAY_MASK);
    
public PaintServergetFill()

return
the context's fill property value

        return fill;
    
public floatgetFillOpacity()

return
the context's fill opacity property value

        return ((pack & FILL_OPACITY_MASK) >> 7) / 200.0f;
    
intgetFillOpacityImpl()

return
the context's fill opacity property value

        return (int) (((pack & FILL_OPACITY_MASK) >> 7) * 255f / 200f);
    
public intgetFillRule()

return
the context's fill rule property value

        if ((pack & FILL_RULE_MASK) == FILL_RULE_MASK) {
            return WIND_EVEN_ODD;
        }
        return WIND_NON_ZERO;
    
public java.lang.String[]getFontFamily()

return
the context's set of font families

        return fontFamily;
    
public floatgetFontSize()

return
the context's font size property

        return fontSize;
    
public intgetFontStyle()

return
the context's font style property value

        switch(pack & FONT_STYLE_MASK) {
        case FONT_STYLE_NORMAL_IMPL:
            return FONT_STYLE_NORMAL;
        case FONT_STYLE_ITALIC_IMPL:
            return FONT_STYLE_ITALIC;
        default:
            return FONT_STYLE_OBLIQUE;
        }
    
public intgetFontWeight()

return
the current context's font weight property

        switch(pack & FONT_WEIGHT_MASK) {
        case FONT_WEIGHT_100_IMPL:
            return FONT_WEIGHT_100;
        case FONT_WEIGHT_200_IMPL:
            return FONT_WEIGHT_200;
        case FONT_WEIGHT_300_IMPL:
            return FONT_WEIGHT_300;
        case FONT_WEIGHT_400_IMPL:
            return FONT_WEIGHT_400;
        case FONT_WEIGHT_500_IMPL:
            return FONT_WEIGHT_500;
        case FONT_WEIGHT_600_IMPL:
            return FONT_WEIGHT_600;
        case FONT_WEIGHT_700_IMPL:
            return FONT_WEIGHT_700;
        case FONT_WEIGHT_800_IMPL:
            return FONT_WEIGHT_800;
        default:
            return FONT_WEIGHT_900;
        }
    
public floatgetOpacity()

return
the context's opacity property value

        return (pack2 & OPACITY_MASK) / 200.0f;
    
intgetOpacityImpl()

return
the context's opacity property value

        return (int) ((pack2 & OPACITY_MASK) * 255f / 200f);
    
public PaintServergetStroke()

return
the context's stroke property value

        return stroke;
    
public float[]getStrokeDashArray()

return
the context's stroke dash array property value

        if (strokeDashArray == null) {
            return null;
        }

        float[] sda = new float[strokeDashArray.length];
        for (int i = 0; i < sda.length; i++) {
            sda[i] = strokeDashArray[i] / 65536.0f;
        }

        return sda;
    
public floatgetStrokeDashOffset()

return
the context's stroke dash offset property

        return strokeDashOffset / 65536.0f;
    
public intgetStrokeLineCap()

return
the context's stroke line cap property value

        switch (pack & STROKE_LINE_CAP_MASK) {
        case CAP_BUTT_IMPL:
            return CAP_BUTT;
        case CAP_ROUND_IMPL:
            return CAP_ROUND;
        default:
            return CAP_SQUARE;
        }
    
public intgetStrokeLineJoin()

return
the context's stroke line join property value

        switch (pack & STROKE_LINE_JOIN_MASK) {
        case JOIN_MITER_IMPL:
            return JOIN_MITER;
        case JOIN_ROUND_IMPL:
            return JOIN_ROUND;
        default:
            return JOIN_BEVEL;
        }
    
public floatgetStrokeMiterLimit()

return
the context's stroke miter limit property value

        return strokeMiterLimit / 65536.0f;
    
public floatgetStrokeOpacity()

return
the context's stroke opacity property value

        return ((pack & STROKE_OPACITY_MASK) >> 15) / 200.0f;
    
public intgetStrokeOpacityImpl()

return
the context's stroke opacity property value

        return (int) (((pack & STROKE_OPACITY_MASK) >> 15) * 255f / 200f);
    
public floatgetStrokeWidth()

return
the context's stroke width property value

        return strokeWidth / 65536.0f;
    
public intgetTextAnchor()

return
the text anchor property

        switch(pack & TEXT_ANCHOR_MASK) {
        case TEXT_ANCHOR_START_IMPL:
            return TEXT_ANCHOR_START;
        case TEXT_ANCHOR_MIDDLE_IMPL:
            return TEXT_ANCHOR_MIDDLE;
        default:
            return TEXT_ANCHOR_END;
        }
    
public booleangetVisibility()

return
the context's visibility

        return ((pack & VISIBILITY_MASK) == VISIBILITY_MASK);
    
public voidreset()
Resets context to initial values

        this.fill = INITIAL_FILL;
        this.stroke = INITIAL_STROKE;
        this.color = INITIAL_COLOR;
        this.strokeDashArray = INITIAL_STROKE_DASH_ARRAY_IMPL;
        this.strokeWidth = INITIAL_STROKE_WIDTH_IMPL;
        this.strokeMiterLimit = INITIAL_STROKE_MITER_LIMIT_IMPL;
        this.strokeDashOffset = INITIAL_STROKE_DASH_OFFSET_IMPL;
        this.fontFamily = INITIAL_FONT_FAMILY;
        this.fontSize = INITIAL_FONT_SIZE;
        this.pack = INITIAL_PACK;
        this.pack2 = INITIAL_PACK2;
    
public voidrestore(com.sun.perseus.j2d.RenderContext rcs)

param
rcs the RenderContext state to restore. Should not be null.

        this.fill = rcs.fill;
        this.stroke = rcs.stroke;
        this.color = rcs.color;
        this.strokeDashArray = rcs.strokeDashArray;
        this.strokeWidth = rcs.strokeWidth;
        this.strokeMiterLimit = rcs.strokeMiterLimit;
        this.strokeDashOffset = rcs.strokeDashOffset;
        this.fontFamily = rcs.fontFamily;
        this.fontSize = rcs.fontSize;
        this.pack = rcs.pack;
        this.pack2 = rcs.pack2;
    
public com.sun.perseus.j2d.RenderContextsave()

return
a copy of this RenderContext

        return new RenderContext(this);
    
public voidsetColor(RGB newColor)

param
newColor the new value for the color property

        this.color = newColor;
    
public voidsetDisplay(boolean newDisplay)

param
newDisplay the new value for the display property

        if (newDisplay) {
            pack |= DISPLAY_MASK;
        } else {
            pack &= ~DISPLAY_MASK;
        }
    
public voidsetFill(PaintServer newFill)

param
newFill the new value for the fill property

        this.fill = newFill;
    
public voidsetFillOpacity(float newFillOpacity)

param
newFillOpacity the new value for the fill opacity property. The input value is clamped to the [0, 1] range.

        if (newFillOpacity < 0) {
            newFillOpacity = 0;
        } else if (newFillOpacity > 1) {
            newFillOpacity = 1;
        }
        
        pack &= ~FILL_OPACITY_MASK;
        pack |= ((((int) (newFillOpacity * 200)) << 7) & FILL_OPACITY_MASK);
    
public voidsetFillRule(int newFillRule)

param
newFillRule the new value for the fill rule property

        if (newFillRule == WIND_EVEN_ODD) {
            pack |= FILL_RULE_MASK;
        } else {
            pack &= ~FILL_RULE_MASK;
        }
    
public voidsetFontFamily(java.lang.String[] newFontFamily)

param
newFontFamily the new set of font families to use for font matching.

        this.fontFamily = newFontFamily;
    
public voidsetFontSize(float newFontSize)

param
newFontSize the new value for the font size property

        this.fontSize = newFontSize;
    
public voidsetFontStyle(int newFontStyle)

param
newFontStyle the new value for the font style property

        // Clear font-style.
        pack &= ~FONT_STYLE_MASK;

        switch(newFontStyle) {
        case FONT_STYLE_NORMAL:
            pack |= FONT_STYLE_NORMAL_IMPL;
            break;
        case FONT_STYLE_ITALIC:
            pack |= FONT_STYLE_ITALIC_IMPL;
            break;
        default:
            pack |= FONT_STYLE_OBLIQUE_IMPL;
            break;
        }
    
public voidsetFontWeight(int newFontWeight)

param
newFontWeight new font weight property value

        // Clear font-weight
        pack &= ~FONT_WEIGHT_MASK;

        switch(newFontWeight) {
        case FONT_WEIGHT_100:
            pack |= FONT_WEIGHT_100_IMPL;
            break;
        case FONT_WEIGHT_200:
            pack |= FONT_WEIGHT_200_IMPL;
            break;
        case FONT_WEIGHT_300:
            pack |= FONT_WEIGHT_300_IMPL;
            break;
        case FONT_WEIGHT_400:
            pack |= FONT_WEIGHT_400_IMPL;
            break;
        case FONT_WEIGHT_500:
            pack |= FONT_WEIGHT_500_IMPL;
            break;
        case FONT_WEIGHT_600:
            pack |= FONT_WEIGHT_600_IMPL;
            break;
        case FONT_WEIGHT_700:
            pack |= FONT_WEIGHT_700_IMPL;
            break;
        case FONT_WEIGHT_800:
            pack |= FONT_WEIGHT_800_IMPL;
            break;
        default:
            pack |= FONT_WEIGHT_900_IMPL;
            break;
        }
    
public voidsetOpacity(float newOpacity)

param
newOpacity the new value for the opacity property. The input value is clamped to the [0, 1] range.
param
newOpacity the new opacity value to set

        if (newOpacity < 0) {
            newOpacity = 0;
        } else if (newOpacity > 1) {
            newOpacity = 1;
        }
        
        pack2 &= ~OPACITY_MASK;
        pack2 |= (((int) (newOpacity * 200)) & OPACITY_MASK);
    
public voidsetStroke(PaintServer newStroke)

param
newStroke the new value for the stroke property

        this.stroke = newStroke;
    
public voidsetStrokeDashArray(float[] newStrokeDashArray)

param
newStrokeDashArray the new stroke dash array property value

        if (newStrokeDashArray == null) {
            strokeDashArray = null;
        } else {
            strokeDashArray = new int[newStrokeDashArray.length];
            for (int i = 0; i < strokeDashArray.length; i++) {
                strokeDashArray[i] = (int) (newStrokeDashArray[i] * 65536);
            }
        }
    
public voidsetStrokeDashOffset(float newStrokeDashOffset)

param
newStrokeDashOffset the new stroke dash offset property value

        strokeDashOffset = (int) (newStrokeDashOffset * 65536);
    
public voidsetStrokeLineCap(int newStrokeLineCap)

param
newStrokeLineCap the new stroke line cap property value

        // Clear stroke-linecap
        pack &= ~STROKE_LINE_CAP_MASK;

        switch (newStrokeLineCap) {
        case CAP_BUTT:
            pack |= CAP_BUTT_IMPL;
            break;
        case CAP_ROUND:
            pack |= CAP_ROUND_IMPL;
            break;
        default:
            pack |= CAP_SQUARE_IMPL;
            break;
        }
    
public voidsetStrokeLineJoin(int newStrokeLineJoin)

param
newStrokeLineJoin the new stroke line join property value

        // Clear stroke-linejoin
        pack &= ~STROKE_LINE_JOIN_MASK;

        switch (newStrokeLineJoin) {
        case JOIN_MITER:
            pack |= JOIN_MITER_IMPL;
            break;
        case JOIN_ROUND:
            pack |= JOIN_ROUND_IMPL;
            break;
        default:
            pack |= JOIN_BEVEL_IMPL;
            break;
        }
    
public voidsetStrokeMiterLimit(float newStrokeMiterLimit)

param
newStrokeMiterLimit the new stroke miter limit property value

        strokeMiterLimit = (int) (newStrokeMiterLimit * 65536);
    
public voidsetStrokeOpacity(float newStrokeOpacity)

param
newStrokeOpacity the new value for the stroke opacity property. The input value is clamped to the [0, 1] range.

        if (newStrokeOpacity < 0) {
            newStrokeOpacity = 0;
        } else if (newStrokeOpacity > 1) {
            newStrokeOpacity = 1;
        }
        
        pack &= ~STROKE_OPACITY_MASK;
        pack |= ((((int) (newStrokeOpacity * 200)) << 15) & STROKE_OPACITY_MASK);
    
public voidsetStrokeWidth(float newStrokeWidth)

param
newStrokeWidth the new stroke width property value

        strokeWidth = (int) (newStrokeWidth * 65536);
    
public voidsetTextAnchor(int newTextAnchor)

param
newTextAnchor the new text anchor property value

        // Clear text-anchor.
        pack &= ~TEXT_ANCHOR_MASK;

        switch(newTextAnchor) {
        case TEXT_ANCHOR_START:
            pack |= TEXT_ANCHOR_START_IMPL;
            break;
        case TEXT_ANCHOR_MIDDLE:
            pack |= TEXT_ANCHOR_MIDDLE_IMPL;
            break;
        default:
            pack |= TEXT_ANCHOR_END_IMPL;
            break;
        }
    
public voidsetVisibility(boolean newVisibility)

param
newVisibility the new visibility property value

        if (newVisibility) {
            pack |= VISIBILITY_MASK;
        } else {
            pack &= ~VISIBILITY_MASK;
        }