KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joshy > html > painter > InlinePainter


1 package org.joshy.html.painter;
2
3 import org.joshy.html.*;
4 import org.joshy.html.box.*;
5 import org.joshy.html.util.*;
6 import java.awt.Color JavaDoc;
7 import java.awt.Rectangle JavaDoc;
8 import java.awt.Graphics JavaDoc;
9 import java.awt.Graphics2D JavaDoc;
10 import java.awt.Font JavaDoc;
11 import java.awt.font.*;
12 import org.joshy.u;
13
14 public class InlinePainter {
15
16     public static void paintInlineContext(Context c, Box box) {
17         BlockBox block = (BlockBox)box;
18         
19         // translate into local coords
20
// account for the origin of the containing box
21
c.getGraphics().translate(box.x,box.y);
22         
23         // for each line box
24
c.getGraphics().setColor(Color.black);
25         //u.p("line count = " + block.boxes.size());
26
for(int i=0; i<block.getChildCount(); i++) {
27             // get the line box
28
//u.p("line = " + block.boxes.get(i));
29
paintLine(c,(LineBox)block.getChild(i));
30         }
31         
32         
33         // translate back to parent coords
34
//c.getGraphics().translate(-c.getExtents().x,-c.getExtents().y);
35
c.getGraphics().translate(-box.x,-box.y);
36         //c.getGraphics().translate(inset_left,inset_top);
37
}
38
39     private static void paintLine(Context c, LineBox line) {
40         //u.p("painting line = " + line);
41
// get x and y
42
int lx = line.x;
43         int ly = line.y + line.baseline;
44         
45         // for each inline box
46
for(int j=0; j<line.getChildCount(); j++) {
47             paintInline(c,(InlineBox)line.getChild(j),lx,ly,line);
48         }
49         if(c.debugDrawLineBoxes()) {
50             GraphicsUtil.drawBox(c.getGraphics(),line,Color.blue);
51         }
52     }
53
54     // inlines are drawn vertically relative to the baseline of the line,
55
// not relative to the origin of the line.
56
// they *are* drawn horizontally (x) relative to the origin of the line
57
// though
58

59     private static void paintInline(Context c, InlineBox inline, int lx, int ly, LineBox line) {
60     
61         if(InlineLayout.isReplaced(inline.node)) {
62             //u.p("painting a replaced block: " + inline);
63
c.getGraphics().translate( line.x, line.y+(line.baseline-inline.height));
64             Layout layout = (Layout)LayoutFactory.getLayout(inline.node);
65             //u.p("inline node = " + inline.node);
66
//u.p("got the layout: " + layout);
67
layout.paint(c,inline);
68             c.getGraphics().translate(-line.x,-(line.y+(line.baseline-inline.height)));
69             return;
70         }
71         
72         
73         if(InlineLayout.isFloatedBlock(inline.node,c)) {
74             //u.p("painting a floated block: " + inline);
75
//u.p("line = " + line);
76
Rectangle JavaDoc oe = c.getExtents();
77             //inline.x = 0;
78
//u.p("extents = " + c.getExtents());
79
c.setExtents(new Rectangle JavaDoc(oe.x, 0, oe.width, oe.height));
80             
81             int xoff = line.x + inline.x;
82             int yoff = line.y + (line.baseline - inline.height) + inline.y;
83             c.getGraphics().translate(xoff, yoff);
84             Layout layout = (Layout)LayoutFactory.getLayout(inline.node);
85             layout.paint(c,inline.sub_block);
86             c.getGraphics().translate(-xoff, -yoff);
87             
88             c.setExtents(oe);
89             return;
90         }
91         
92         
93         if(inline.is_break) {
94             return;
95         }
96         
97         Graphics JavaDoc g = c.getGraphics();
98         
99         // handle relative
100
if(inline.relative) {
101             g.translate(inline.left, inline.top);
102         }
103         
104         // calculate the x and y relative to the baseline of the line (ly) and the
105
// left edge of the line (lx)
106
//String text = inline.text.substring(inline.start_index,inline.end_index);
107
String JavaDoc text = inline.getSubstring();
108         int iy = ly + inline.y;
109         int ix = lx + inline.x;
110         
111         //adjust font for current settings
112
Font JavaDoc oldfont = c.getGraphics().getFont();
113         c.getGraphics().setFont(inline.getFont());
114         Color JavaDoc oldcolor = c.getGraphics().getColor();
115         c.getGraphics().setColor(inline.color);
116         //draw the line
117
//u.p("drawing: " + text + " at " + x + "," + iy);
118
if(text!= null && text.length() > 0) {
119             c.getGraphics().drawString(text,ix,iy);
120         }
121         c.getGraphics().setColor(oldcolor);
122         
123         //draw any text decoration
124
Font JavaDoc cur_font = c.getGraphics().getFont();
125         
126         LineMetrics lm = cur_font.getLineMetrics(text,((Graphics2D JavaDoc)c.getGraphics()).getFontRenderContext());
127         if(inline.underline) {
128             float down = lm.getUnderlineOffset();
129             float thick = lm.getUnderlineThickness();
130             g.fillRect(ix,iy+(int)down,g.getFontMetrics().stringWidth(text),(int)thick);
131         }
132         if(inline.strikethrough) {
133             float down = lm.getStrikethroughOffset();
134             float thick = lm.getStrikethroughThickness();
135             g.fillRect(ix,iy+(int)down,g.getFontMetrics().stringWidth(text),(int)thick);
136         }
137         if(inline.overline) {
138             float down = lm.getAscent();
139             float thick = lm.getUnderlineThickness();
140             g.fillRect(ix,iy-(int)down,g.getFontMetrics().stringWidth(text),(int)thick);
141         }
142         
143         if(c.debugDrawInlineBoxes()) {
144             GraphicsUtil.draw(c.getGraphics(),new Rectangle JavaDoc(lx+inline.x+1,ly+inline.y+1-inline.height,
145                     inline.width-2,inline.height-2),Color.green);
146         }
147         
148         // restore the old font
149
c.getGraphics().setFont(oldfont);
150         
151         // handle relative
152
if(inline.relative) {
153             g.translate(-inline.left, -inline.top);
154         }
155     }
156
157 }
158
Popular Tags