~ubuntu-branches/ubuntu/utopic/libjmathtex-java/utopic

« back to all changes in this revision

Viewing changes to be/ugent/caagt/jmathtex/VerticalBox.java

  • Committer: Bazaar Package Importer
  • Author(s): Adriaan Peeters
  • Date: 2007-09-25 14:07:55 UTC
  • Revision ID: james.westby@ubuntu.com-20070925140755-mro37pqu4wkhqa9t
Tags: upstream-0.7~pre
ImportĀ upstreamĀ versionĀ 0.7~pre

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* VerticalBox.java
 
2
 * =========================================================================
 
3
 * This file is part of the JMathTeX Library - http://jmathtex.sourceforge.net
 
4
 *
 
5
 * Copyright (C) 2004-2007 Universiteit Gent
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or (at
 
10
 * your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful, but
 
13
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
 * General Public License for more details.
 
16
 *
 
17
 * A copy of the GNU General Public License can be found in the file
 
18
 * LICENSE.txt provided with the source distribution of this program (see
 
19
 * the META-INF directory in the source jar). This license can also be
 
20
 * found on the GNU website at http://www.gnu.org/licenses/gpl.html.
 
21
 *
 
22
 * If you did not receive a copy of the GNU General Public License along
 
23
 * with this program, contact the lead developer, or write to the Free
 
24
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
25
 * 02110-1301, USA.
 
26
 *
 
27
 */
 
28
 
 
29
package be.ugent.caagt.jmathtex;
 
30
 
 
31
import java.awt.Graphics2D;
 
32
import java.util.ListIterator;
 
33
 
 
34
/**
 
35
 * A box composed of other boxes, put one above the other.
 
36
 */
 
37
class VerticalBox extends Box {
 
38
    
 
39
    private float leftMostPos = Float.MAX_VALUE;
 
40
    private float rightMostPos = Float.MIN_VALUE; // NOPMD
 
41
    
 
42
    public VerticalBox() {
 
43
        // empty
 
44
    }
 
45
    
 
46
    public VerticalBox(Box b, float rest, int alignment) {
 
47
        this();
 
48
        add(b);
 
49
        if (alignment == TeXConstants.ALIGN_CENTER) {
 
50
            StrutBox s = new StrutBox(0, rest / 2, 0, 0);
 
51
            super.add(0, s);
 
52
            height += rest / 2;
 
53
            depth += rest / 2;
 
54
            super.add(s);
 
55
        } else if (alignment == TeXConstants.ALIGN_TOP) {
 
56
            depth += rest;
 
57
            super.add(new StrutBox(0, rest, 0, 0));
 
58
        } else if (alignment == TeXConstants.ALIGN_BOTTOM) {
 
59
            height += rest;
 
60
            super.add(0, new StrutBox(0, rest, 0, 0));
 
61
        }
 
62
    }
 
63
    
 
64
    public final void add(Box b) {
 
65
        super.add(b);
 
66
        if (children.size() == 1) {
 
67
            height = b.height;
 
68
            depth = b.depth;
 
69
        } else
 
70
            depth += b.height + b.depth;
 
71
        recalculateWidth(b);
 
72
    }
 
73
    
 
74
    private void recalculateWidth(Box b) {
 
75
        leftMostPos = Math.min(leftMostPos, b.shift);
 
76
        rightMostPos = Math.max(rightMostPos, b.shift
 
77
                + (b.width > 0 ? b.width : 0));
 
78
        width = rightMostPos - leftMostPos;
 
79
    }
 
80
    
 
81
    public void add(int pos, Box b) {
 
82
        super.add(pos, b);
 
83
        if (pos == 0) {
 
84
            depth += b.depth + height;
 
85
            height = b.height;
 
86
        } else
 
87
            depth += b.height + b.depth;
 
88
        recalculateWidth(b);
 
89
    }
 
90
    
 
91
    public void draw(Graphics2D g2, float x, float y) {
 
92
        float yPos = y - height;
 
93
        for (Box b : children) {
 
94
            yPos += b.getHeight();
 
95
            b.draw(g2, x + b.getShift() - leftMostPos, yPos);
 
96
            yPos += b.getDepth();
 
97
        }
 
98
    }
 
99
    
 
100
    public int getSize() {
 
101
        return children.size();
 
102
    }
 
103
    
 
104
    public int getLastFontId() {
 
105
        // iterate from the last child box (the lowest) to the first (the highest)
 
106
        // untill a font id is found that's not equal to NO_FONT
 
107
        int fontId = TeXFont.NO_FONT;
 
108
        for (ListIterator it = children.listIterator(children.size()); fontId == TeXFont.NO_FONT
 
109
                && it.hasPrevious();)
 
110
            fontId = ((Box) it.previous()).getLastFontId();
 
111
        
 
112
        return fontId;
 
113
    }
 
114
}