1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
/*******************************************************************************
* Copyright (c) 2004, 2008 John Krasnay and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* John Krasnay - initial API and implementation
*******************************************************************************/
package org.eclipse.wst.xml.vex.core.internal.layout;
import org.eclipse.wst.xml.vex.core.internal.dom.Element;
/**
* Represents a line of text and inline images.
*/
public class LineBox extends CompositeInlineBox {
private Element element;
private InlineBox[] children;
private InlineBox firstContentChild = null;
private InlineBox lastContentChild = null;
private int baseline;
/**
* Class constructor.
*
* @param context
* LayoutContext for this layout.
* @param children
* InlineBoxes that make up this line.
*/
public LineBox(LayoutContext context, Element element, InlineBox[] children) {
this.element = element;
this.children = children;
int height = 0;
int x = 0;
this.baseline = 0;
for (int i = 0; i < children.length; i++) {
InlineBox child = children[i];
child.setX(x);
child.setY(0); // TODO: do proper vertical alignment
this.baseline = Math.max(this.baseline, child.getBaseline());
x += child.getWidth();
height = Math.max(height, child.getHeight());
if (child.hasContent()) {
if (this.firstContentChild == null) {
this.firstContentChild = child;
}
this.lastContentChild = child;
}
}
this.setHeight(height);
this.setWidth(x);
}
/**
* @see org.eclipse.wst.xml.vex.core.internal.layout.InlineBox#getBaseline()
*/
public int getBaseline() {
return this.baseline;
}
public Box[] getChildren() {
return this.children;
}
/**
* @see org.eclipse.wst.xml.vex.core.internal.layout.Box#getElement()
*/
public Element getElement() {
return this.element;
}
/**
* @see org.eclipse.wst.xml.vex.core.internal.layout.Box#getEndOffset()
*/
public int getEndOffset() {
return this.lastContentChild.getEndOffset();
}
/**
* @see org.eclipse.wst.xml.vex.core.internal.layout.Box#getStartOffset()
*/
public int getStartOffset() {
return this.firstContentChild.getStartOffset();
}
/**
* @see org.eclipse.wst.xml.vex.core.internal.layout.Box#hasContent()
*/
public boolean hasContent() {
return this.firstContentChild != null;
}
/**
* @see org.eclipse.wst.xml.vex.core.internal.layout.CompositeInlineBox#split(org.eclipse.wst.xml.vex.core.internal.layout.LayoutContext,
* org.eclipse.wst.xml.vex.core.internal.layout.InlineBox[],
* org.eclipse.wst.xml.vex.core.internal.layout.InlineBox[])
*/
public Pair split(LayoutContext context, InlineBox[] lefts,
InlineBox[] rights) {
LineBox left = null;
LineBox right = null;
if (lefts.length > 0) {
left = new LineBox(context, this.getElement(), lefts);
}
if (rights.length > 0) {
right = new LineBox(context, this.getElement(), rights);
}
return new Pair(left, right);
}
/**
* @see java.lang.Object#toString()
*/
public String toString() {
Box[] children = this.getChildren();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < children.length; i++) {
sb.append(children[i]);
}
return sb.toString();
}
// ========================================================== PRIVATE
}
|