~ubuntu-branches/ubuntu/utopic/libjlatexmath-java/utopic

« back to all changes in this revision

Viewing changes to src/org/scilab/forge/jlatexmath/FractionAtom.java

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-27 14:24:03 UTC
  • mfrom: (4.1.5 experimental)
  • Revision ID: package-import@ubuntu.com-20130527142403-etv7j7excnzoa71l
Tags: 1.0.2-1
* Upload to unstable
* Standards-Version updated to 3.9.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
 * An atom representing a fraction.
34
34
 */
35
35
public class FractionAtom extends Atom {
36
 
    
 
36
 
37
37
    // whether the default thickness should not be used for the fraction line
38
38
    private boolean noDefault = false;
39
 
    
 
39
 
40
40
    // unit used for the thickness of the fraction line
41
41
    private int unit;
42
 
    
 
42
 
43
43
    // alignment settings for the numerator and denominator
44
44
    private int numAlign = TeXConstants.ALIGN_CENTER,
45
 
            denomAlign = TeXConstants.ALIGN_CENTER;
46
 
    
 
45
        denomAlign = TeXConstants.ALIGN_CENTER;
 
46
 
47
47
    // the atoms representing the numerator and denominator
48
48
    private Atom numerator, denominator;
49
 
    
 
49
 
50
50
    // thickness of the fraction line
51
51
    private float thickness;
52
 
    
 
52
 
53
53
    // thickness of the fraction line relative to the default thickness
54
54
    private float defFactor;
55
 
    
 
55
 
56
56
    // whether the "defFactor" value should be used
57
57
    private boolean defFactorSet = false;
58
 
    
 
58
 
59
59
    /**
60
60
     * Uses the default thickness for the fraction line
61
61
     *
65
65
    public FractionAtom(Atom num, Atom den) {
66
66
        this(num, den, true);
67
67
    }
68
 
    
 
68
 
69
69
    /**
70
70
     * Uses the default thickness for the fraction line
71
71
     *
76
76
    public FractionAtom(Atom num, Atom den, boolean rule) {
77
77
        this(num, den, !rule, TeXConstants.UNIT_PIXEL, 0f);
78
78
    }
79
 
    
 
79
 
80
80
    /**
81
81
     * Depending on noDef, the given thickness and unit will be used (<-> the default
82
82
     * thickness).
89
89
     * @throws InvalidUnitException if the given integer is not a valid unit constant
90
90
     */
91
91
    public FractionAtom(Atom num, Atom den, boolean noDef, int unit, float t)
92
 
    throws InvalidUnitException {
 
92
        throws InvalidUnitException {
93
93
        // check unit
94
94
        SpaceAtom.checkUnit(unit);
95
 
        
 
95
 
96
96
        // unit ok
97
97
        numerator = num;
98
98
        denominator = den;
101
101
        this.unit = unit;
102
102
        type = TeXConstants.TYPE_INNER;
103
103
    }
104
 
    
 
104
 
105
105
    /**
106
106
     * Uses the default thickness for the fraction line.
107
107
     *
112
112
     * @param denomAlign alignment of the denominator
113
113
     */
114
114
    public FractionAtom(Atom num, Atom den, boolean rule, int numAlign,
115
 
            int denomAlign) {
 
115
                        int denomAlign) {
116
116
        this(num, den, rule);
117
117
        this.numAlign = checkAlignment(numAlign);
118
118
        this.denomAlign = checkAlignment(denomAlign);
119
119
    }
120
 
    
 
120
 
121
121
    /**
122
122
     * The thickness of the fraction line will be "defFactor" times the default thickness.
123
123
     *
128
128
     * @param denomAlign alignment of the denominator
129
129
     */
130
130
    public FractionAtom(Atom num, Atom den, float defFactor, int numAlign,
131
 
            int denomAlign) {
 
131
                        int denomAlign) {
132
132
        this(num, den, true, numAlign, denomAlign);
133
133
        this.defFactor = defFactor;
134
134
        defFactorSet = true;
135
135
    }
136
 
    
 
136
 
137
137
    /**
138
138
     * The thickness of the fraction line is determined by the given value "t" in the
139
139
     * given unit.
146
146
     * @param denomAlign alignment of the denominator
147
147
     */
148
148
    public FractionAtom(Atom num, Atom den, int unit, float t, int numAlign,
149
 
            int denomAlign) {
 
149
                        int denomAlign) {
150
150
        this(num, den, unit, t);
151
151
        this.numAlign = checkAlignment(numAlign);
152
152
        this.denomAlign = checkAlignment(denomAlign);
153
153
    }
154
 
    
 
154
 
155
155
    /**
156
156
     * The thickness of the fraction line is determined by the given value "t" in the
157
157
     * given unit.
164
164
    public FractionAtom(Atom num, Atom den, int unit, float t) {
165
165
        this(num, den, true, unit, t);
166
166
    }
167
 
    
 
167
 
168
168
    // Checks if the alignment constant is valid.
169
169
    // If not, a default value will be used.
170
170
    private int checkAlignment(int align) {
171
171
        if (align == TeXConstants.ALIGN_LEFT ||
172
 
                align == TeXConstants.ALIGN_RIGHT)
 
172
            align == TeXConstants.ALIGN_RIGHT)
173
173
            return align;
174
174
        else
175
175
            return TeXConstants.ALIGN_CENTER;
176
176
    }
177
 
    
 
177
 
178
178
    public Box createBox(TeXEnvironment env) {
179
179
        TeXFont tf = env.getTeXFont();
180
180
        int style = env.getStyle();
182
182
        float drt = tf.getDefaultRuleThickness(style);
183
183
        if (noDefault)
184
184
            // convert the thickness to pixels
185
 
            thickness *= SpaceAtom.getFactor(unit, env); 
 
185
            thickness *= SpaceAtom.getFactor(unit, env);
186
186
        else
187
187
            thickness = (defFactorSet ? defFactor * drt : drt);
188
 
        
 
188
 
189
189
        // create equal width boxes (in appropriate styles)
190
190
        Box num = (numerator == null ? new StrutBox(0, 0, 0, 0) : numerator
191
 
                .createBox(env.numStyle()));
 
191
                   .createBox(env.numStyle()));
192
192
        Box denom = (denominator == null ? new StrutBox(0, 0, 0, 0) : denominator
193
 
                .createBox(env.denomStyle()));
194
 
        
 
193
                     .createBox(env.denomStyle()));
 
194
 
195
195
        if (num.getWidth() < denom.getWidth())
196
196
            num = new HorizontalBox(num, denom.getWidth(), numAlign);
197
197
        else
198
198
            denom = new HorizontalBox(denom, num.getWidth(), denomAlign);
199
 
        
 
199
 
200
200
        // calculate default shift amounts
201
201
        float shiftUp, shiftDown;
202
202
        if (style < TeXConstants.STYLE_TEXT) {
209
209
            else
210
210
                shiftUp = tf.getNum3(style);
211
211
        }
212
 
        
 
212
 
213
213
        // upper part of vertical box = numerator
214
214
        VerticalBox vBox = new VerticalBox();
215
215
        vBox.add(num);
216
 
        
 
216
 
217
217
        // calculate clearance clr, adjust shift amounts and create vertical box
218
218
        float clr, delta, axis = tf.getAxisHeight(style);
219
 
        
 
219
 
220
220
        if (thickness > 0) { // WITH fraction rule
221
221
            // clearance clr
222
222
            if (style < TeXConstants.STYLE_TEXT)
223
223
                clr = 3 * thickness;
224
224
            else
225
225
                clr = thickness;
226
 
            
 
226
 
227
227
            // adjust shift amounts
228
228
            delta = thickness / 2;
229
229
            float kern1 = shiftUp - num.getDepth() - (axis + delta), kern2 = axis
230
 
                    - delta - (denom.getHeight() - shiftDown);
 
230
                - delta - (denom.getHeight() - shiftDown);
231
231
            float delta1 = clr - kern1, delta2 = clr - kern2;
232
232
            if (delta1 > 0) {
233
233
                shiftUp += delta1;
237
237
                shiftDown += delta2;
238
238
                kern2 += delta2;
239
239
            }
240
 
            
 
240
 
241
241
            // fill vertical box
242
242
            vBox.add(new StrutBox(0, kern1, 0, 0));
243
243
            vBox.add(new HorizontalRule(thickness, num.getWidth(), 0));
248
248
                clr = 7 * drt;
249
249
            else
250
250
                clr = 3 * drt;
251
 
            
 
251
 
252
252
            // adjust shift amounts
253
253
            float kern = shiftUp - num.getDepth()
254
 
            - (denom.getHeight() - shiftDown);
 
254
                - (denom.getHeight() - shiftDown);
255
255
            delta = (clr - kern) / 2;
256
256
            if (delta > 0) {
257
257
                shiftUp += delta;
258
258
                shiftDown += delta;
259
259
                kern += 2 * delta;
260
260
            }
261
 
            
 
261
 
262
262
            // fill vertical box
263
263
            vBox.add(new StrutBox(0, kern, 0, 0));
264
264
        }
265
 
        
 
265
 
266
266
        // finish vertical box
267
267
        vBox.add(denom);
268
268
        vBox.setHeight(shiftUp + num.getHeight());
269
269
        vBox.setDepth(shiftDown + denom.getDepth());
270
 
        return vBox;
 
270
 
 
271
        // \nulldelimiterspace is set by default to 1.2pt = 0.12em)
 
272
        float f = new SpaceAtom(TeXConstants.UNIT_EM, 0.12f, 0, 0).createBox(env).getWidth();
 
273
 
 
274
        return new HorizontalBox(vBox, vBox.getWidth() + 2 * f, TeXConstants.ALIGN_CENTER);
271
275
    }
272
276
}