-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathTextLine.java
More file actions
1021 lines (930 loc) · 23.5 KB
/
TextLine.java
File metadata and controls
1021 lines (930 loc) · 23.5 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Open Source Physics software is free software as described near the bottom of this code file.
*
* For additional information and documentation on Open Source Physics please see:
* <http://www.opensourcephysics.org/>
*/
package org.opensourcephysics.display;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.geom.Rectangle2D;
import java.util.Stack;
import java.util.Vector;
/*
**************************************************************************
**
** Class TextLine
**
**************************************************************************
** Copyright (C) 1996 Leigh Brookshaw
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
*/
/**
* TextLine is designed to bundle together all the information required
* to draw short Strings with subscripts and superscripts.
*
* TextLine was modified by W. Christian to add Greek characters using TeX notation.
*
* @author Leigh Brookshaw
*/
public class TextLine {
/**
* Center the Text over the point
*/
public final static int CENTER = 0;
/**
* Position the Text to the Left of the point
*/
public final static int LEFT = 1;
/**
* Position the Text to the Right of the point
*/
public final static int RIGHT = 2;
/**
* Format to use when parsing a double
*/
public final static int SCIENTIFIC = 1;
/**
* Format to use when parsing a double
*/
public final static int ALGEBRAIC = 2;
/*
** Minimum Point size allowed for script characters
*/
final static int MINIMUM_SIZE = 6;
/*
**********************
**
** Protected Variables
**
*********************/
/**
* Decrease in size of successive script levels
*/
protected double script_fraction = 0.8;
/**
* Superscript offset
*/
protected double sup_offset = 0.6;
/**
* Subscript offset
*/
protected double sub_offset = 0.7;
/**
* Font to use for text
*/
protected Font font = null;
/**
* Text color
*/
protected Color color = null;
/**
* Background Color
*/
protected Color background = null;
/**
* The text to display
*/
protected String text = null;
/**
* The logical name of the font to use
*/
protected String fontname = "TimesRoman"; //$NON-NLS-1$
/**
* The font size
*/
protected int fontsize = 0;
/**
* The font style
*/
protected int fontstyle = Font.PLAIN;
/**
* Text justification. Either CENTER, LEFT or RIGHT
*/
protected int justification = LEFT;
/**
* The width of the text using the current Font
*/
protected int width = 0;
/**
* The ascent using the current font
*/
protected int ascent = 0;
/**
* The maximum ascent using the current font
*/
protected int maxAscent = 0;
/**
* The descent using the current font
*/
protected int descent = 0;
/**
* The maximum descent using the current font
*/
protected int maxDescent = 0;
/**
* The height using the current font ie ascent+descent+leading
*/
protected int height = 0;
/**
* The leading using the current font
*/
protected int leading = 0;
/**
* Has the string been parsed! This only needs to be done once
* unless the font is altered.
*/
protected boolean parse = true;
/**
* Local graphics context.
*/
protected Graphics lg = null;
/**
* The parsed string. Each element in the vector represents
* a change of context in the string ie font change and offset.
*/
protected Vector<TextState> list = new Vector<TextState>(8, 4);
/*
**********************
**
** Constructors
**
*********************/
/**
* Instantiate the class
*/
public TextLine() {}
/**
* Instantiate the class.
* @param s String to parse.
*/
public TextLine(String s) {
this.text = TeXParser.parseTeX(s);
}
/**
* Instantiate the class
* @param s String to parse.
* @param f Font to use.
*/
public TextLine(String s, Font f) {
this(s);
font = f;
if(font==null) {
return;
}
fontname = f.getName();
fontstyle = f.getStyle();
fontsize = f.getSize();
}
/**
* Instantiate the class
* @param s String to parse.
* @param f Font to use.
* @param c Color to use
* @param j Justification
*/
public TextLine(String s, Font f, Color c, int j) {
this(s, f);
color = c;
justification = j;
}
/**
* Instantiate the class
* @param s String to parse.
* @param c Color to use
*/
public TextLine(String s, Color c) {
this(s);
color = c;
}
/**
* Instantiate the class
* @param f Font to use.
* @param c Color to use
* @param j Justification
*/
public TextLine(Font f, Color c, int j) {
font = f;
color = c;
justification = j;
if(font==null) {
return;
}
fontname = f.getName();
fontstyle = f.getStyle();
fontsize = f.getSize();
}
/*
*****************
**
** Public Methods
**
*****************/
/**
* Create a New Textline object copying the state of the existing
* object into the new one. The state of the class is the color,
* font, and justification ie everything but the string.
*/
public TextLine copyState() {
return new TextLine(font, color, justification);
}
/**
* Copy the state of the parsed Textline into the existing
* object.
* @param t The TextLine to get the state information from.
*/
public void copyState(TextLine t) {
if(t==null) {
return;
}
font = t.getFont();
color = t.getColor();
justification = t.getJustification();
if(font==null) {
return;
}
fontname = font.getName();
fontstyle = font.getStyle();
fontsize = font.getSize();
parse = true;
}
/**
* Set the Font to use with the class
* @param f Font
*/
public void setFont(Font f) {
if(f==null) {
return;
}
font = f;
fontname = f.getName();
fontstyle = f.getStyle();
fontsize = f.getSize();
parse = true;
}
/**
* Set the String to use with the class
* @param s String
*/
public void setText(String s) {
text = TeXParser.parseTeX(s);
parse = true;
}
/**
* Set the Color to use with the class
* @param c Color
*/
public void setColor(Color c) {
color = c;
}
/**
* Set the Background Color to use with the class
* @param c Color
*/
public void setBackground(Color c) {
background = c;
}
/**
* Set the Justification to use with the class
* @param i Justification
*/
public void setJustification(int i) {
switch(i) {
case CENTER :
justification = CENTER;
break;
case LEFT :
default :
justification = LEFT;
break;
case RIGHT :
justification = RIGHT;
break;
}
}
/**
* @return the font the class is using
*/
public Font getFont() {
return font;
}
/**
* @return the String the class is using.
*/
public String getText() {
return text;
}
/**
* @return the Color the class is using.
*/
public Color getColor() {
return color;
}
/**
* @return the Background Color the class is using.
*/
public Color getBackground() {
return background;
}
/**
* @return the Justification the class is using.
*/
public int getJustification() {
return justification;
}
/**
* @param g Graphics context.
* @return the Fontmetrics the class is using.
*/
public FontMetrics getFM(Graphics g) {
if(g==null) {
return null;
}
if(font==null) {
return g.getFontMetrics();
}
return g.getFontMetrics(font);
}
/**
* @param g Graphics context.
* @param ch The character.
* @return the width of the character.
*/
public int charWidth(Graphics g, char ch) {
FontMetrics fm;
if(g==null) {
return 0;
}
if(font==null) {
fm = g.getFontMetrics();
} else {
fm = g.getFontMetrics(font);
}
return fm.charWidth(ch);
}
/**
* Returns the bounding box for this string.
* @param g Graphics
* @return Rectangle2D
*/
public Rectangle2D getStringBounds(Graphics g) {
parseText(g);
return new Rectangle2D.Float(0, 0, width, height);
}
/**
* @param g Graphics context.
* @return the width of the parsed text.
*/
public int getWidth(Graphics g) {
parseText(g);
return width;
}
/**
* @param g Graphics context.
* @return the height of the parsed text.
*/
public int getHeight(Graphics g) {
parseText(g);
return height;
}
/**
* @param g Graphics context.
* @return the ascent of the parsed text.
*/
public int getAscent(Graphics g) {
if(g==null) {
return 0;
}
parseText(g);
return ascent;
}
/**
* @param g Graphics context.
* @return the maximum ascent of the parsed text.
*/
public int getMaxAscent(Graphics g) {
if(g==null) {
return 0;
}
parseText(g);
return maxAscent;
}
/**
* @param g Graphics context.
* @return the descent of the parsed text.
*/
public int getDescent(Graphics g) {
if(g==null) {
return 0;
}
parseText(g);
return descent;
}
/**
* @param g Graphics context.
* @return the maximum descent of the parsed text.
*/
public int getMaxDescent(Graphics g) {
if(g==null) {
return 0;
}
parseText(g);
return maxDescent;
}
/**
* @param g Graphics context.
* @return the leading of the parsed text.
*/
public int getLeading(Graphics g) {
if(g==null) {
return 0;
}
parseText(g);
return leading;
}
/**
* parse the text. When the text is parsed the width, height, leading
* are all calculated. The text will only be truly parsed if
* the graphics context has changed or the text has changed or
* the font has changed. Otherwise nothing is done when this
* method is called.
* @param g Graphics context.
*/
public void parseText(Graphics g) {
TextState current = new TextState();
char ch;
Stack<TextState> state = new Stack<TextState>();
int w = 0;
if(lg!=g) {
parse = true;
}
lg = g;
if(!parse) {
return;
}
parse = false;
width = 0;
leading = 0;
ascent = 0;
descent = 0;
height = 0;
maxAscent = 0;
maxDescent = 0;
if((text==null)||(g==null)) {
return;
}
list.removeAllElements();
if(font==null) {
current.f = g.getFont();
} else {
current.f = font;
}
state.push(current);
list.addElement(current);
for(int i = 0; i<text.length(); i++) {
ch = text.charAt(i);
switch(ch) {
case '$' :
i++;
if(i<text.length()) {
current.s.append(text.charAt(i));
}
break;
/*
** Push the current state onto the state stack
** and start a new storage string
*/
case '{' :
w = current.getWidth(g);
if(!current.isEmpty()) {
current = current.copyState();
list.addElement(current);
}
state.push(current);
current.x += w;
break;
/*
** Pop the state off the state stack and set the current
** state to the top of the state stack
*/
case '}' :
w = current.x+current.getWidth(g);
state.pop();
current = state.peek().copyState();
list.addElement(current);
current.x = w;
break;
case '^' :
w = current.getWidth(g);
if(!current.isEmpty()) {
current = current.copyState();
list.addElement(current);
}
current.f = getScriptFont(current.f);
current.x += w;
current.y -= (int) ((current.getAscent(g))*sup_offset+0.5);
break;
case '_' :
w = current.getWidth(g);
if(!current.isEmpty()) {
current = current.copyState();
list.addElement(current);
}
current.f = getScriptFont(current.f);
current.x += w;
current.y += (int) ((current.getDescent(g))*sub_offset+0.5);
break;
default :
current.s.append(ch);
break;
}
}
Vector<TextState> vec; // added by W. Christian in case a parse is called during drawing.
synchronized(list) {
vec = new Vector<TextState>(list);
} // added by W. Christian
for(int i = 0; i<vec.size(); i++) {
current = (vec.elementAt(i));
if(!current.isEmpty()) {
width += current.getWidth(g);
ascent = Math.max(ascent, Math.abs(current.y)+current.getAscent(g));
descent = Math.max(descent, Math.abs(current.y)+current.getDescent(g));
leading = Math.max(leading, current.getLeading(g));
maxDescent = Math.max(maxDescent, Math.abs(current.y)+current.getMaxDescent(g));
maxAscent = Math.max(maxAscent, Math.abs(current.y)+current.getMaxAscent(g));
}
}
height = ascent+descent+leading;
return;
}
/**
* @return true if the text has never been set or is null
*/
public boolean isNull() {
return(text==null);
}
/**
* Parse the text then draw it.
* @param g Graphics context
* @param x pixel position of the text
* @param y pixel position of the text
* @param j justification of the text
*/
public void drawText(Graphics g, int x, int y, int j) {
justification = j;
if(g==null) {
return;
}
drawText(g, x, y);
}
/**
* Parse the text then draw it without any rotation.
* @param g Graphics context
* @param x pixel position of the text
* @param y pixel position of the text
*/
public void drawText(Graphics g, int x, int y) {
TextState ts;
int xoffset = x;
int yoffset = y;
if((g==null)||(text==null)) {
return;
}
Graphics lg = g.create();
if(lg==null) {
return; // added by W. Christian
}
parseText(g);
if(justification==CENTER) {
xoffset = x-width/2;
} else if(justification==RIGHT) {
xoffset = x-width;
}
if(background!=null) {
lg.setColor(background);
lg.fillRect(xoffset, yoffset-ascent, width, height);
lg.setColor(g.getColor());
}
if(font!=null) {
lg.setFont(font);
}
if(color!=null) {
lg.setColor(color);
}
Vector<TextState> vec; // added by W. Christian in case a parse is called during drawing.
synchronized(list) {
vec = new Vector<TextState>(list);
} // added by W. Christian
for(int i = 0; i<vec.size(); i++) {
ts = (vec.elementAt(i));
if(ts.f!=null) {
lg.setFont(ts.f);
}
if(ts.s!=null) {
lg.drawString(ts.toString(), ts.x+xoffset, ts.y+yoffset);
}
}
lg.dispose();
lg = null;
}
/**
* @return Logical font name of the set font
*/
public String getFontName() {
return fontname;
}
/**
* @return Style of the set font
*/
public int getFontStyle() {
return fontstyle;
}
/**
* @return Size of the set font
*/
public int getFontSize() {
return fontsize;
}
/**
* Set the Logical font name of the current font
* @param s Logical font name.
*/
public void setFontName(String s) {
if(s==null) {
return;
}
fontname = s;
rebuildFont();
}
/**
* Set the Font style of the current font
* @param i Font style.
*/
public void setFontStyle(int i) {
fontstyle = i;
rebuildFont();
}
/**
* Set the Font size of the current font
* @param i Font size.
*/
public void setFontSize(int i) {
fontsize = i;
rebuildFont();
}
/*
** Rebuild the font using the current fontname, fontstyle, and fontsize.
*/
private void rebuildFont() {
parse = true;
if((fontsize<=0)||(fontname==null)) {
font = null;
} else {
font = new Font(fontname, fontstyle, fontsize);
}
}
/**
* @param f Font
* @return The script font version of the parsed font using the
* script_fraction variable.
*/
public Font getScriptFont(Font f) {
int size;
if(f==null) {
return f;
}
size = f.getSize();
if(size<=MINIMUM_SIZE) {
return f;
}
size = (int) ((f.getSize())*script_fraction+0.5);
if(size<=MINIMUM_SIZE) {
return f;
}
return new Font(f.getName(), f.getStyle(), size);
}
/**
* Parse a double value. Precision is 6 figures, with 7 significant
* figures.
* @param d double to parse
* return <i>true</i> if the parse was successful
*/
public boolean parseDouble(double d) {
return parseDouble(d, 7, 6, ALGEBRAIC);
}
/**
* Parse a double value. Number of significant figures is 1 greater than
* the precision.
* @param d double to parse
* @param p precision of the number
* return <i>true</i> if the parse was successful
*/
public boolean parseDouble(double d, int p) {
return parseDouble(d, p+1, p, ALGEBRAIC);
}
/**
* Parse a double value
* @param d double to parse
* @param n number of significant figures
* @param p precision of the number
* @param f format of the number scientific, algebraic etc.
* return <i>true</i> if the parse was successful
*/
public boolean parseDouble(double d, int n, int p, int f) {
double x = d;
int left = n-p;
double right = 0;
int power;
int exponent;
int i;
StringBuffer s = new StringBuffer(n+4);
if(left<0) {
System.out.println("TextLine.parseDouble: Precision > significant figures!"); //$NON-NLS-1$
return false;
}
if(d<0.0) {
x = -d;
s.append("-"); //$NON-NLS-1$
}
// System.out.println("parseDouble: value = "+x);
if(d==0.0) {
exponent = 0;
} else {
exponent = (int) (Math.floor(TextLine.log10(x)));
}
// System.out.println("parseDouble: exponent = "+exponent);
power = exponent-(left-1);
// System.out.println("parseDouble: power = "+power);
if(power<0) {
for(i = power; i<0; i++) {
x *= 10.0;
}
} else {
for(i = 0; i<power; i++) {
x /= 10.0;
}
}
// System.out.println("parseDouble: adjusted value = "+x);
left = (int) x;
s.append(left);
// System.out.println("parseDouble: left = "+left);
if(p>0) {
s.append('.');
right = x-left;
for(i = 0; i<p; i++) {
right *= 10;
// if(i==p-1) right += 0.5;
// s.append((int)(right));
// right -= (int)right;
int digit = (int) Math.round(right);
s.append(digit);
right -= digit;
}
}
// System.out.println("parseDouble: right = "+right);
if(power!=0) {
if(f==SCIENTIFIC) {
s.append('E');
if(power<0) {
s.append('-');
} else {
s.append('+');
}
power = Math.abs(power);
if(power>9) {
s.append(power);
} else {
s.append('0');
s.append(power);
}
} else {
s.append("x10{^"); //$NON-NLS-1$
s.append(power);
s.append("}"); //$NON-NLS-1$
}
}
setText(s.toString());
return true;
}
/**
* @param x a double value
* @return The log<sub>10</sub>
*/
static public double log10(double x) throws ArithmeticException {
if(x<=0.0) {
throw new ArithmeticException("range exception"); //$NON-NLS-1$
}
return Math.log(x)/2.30258509299404568401;
}
}
/**
* A structure class used exclusively with the TextLine class.
* When the Text changes state (new font, new color, new offset)
* then this class holds the information plus the substring
* that the state pertains to.
*/
class TextState extends Object {
Font f = null;
StringBuffer s = null;
int x = 0;
int y = 0;
/**
* Constructor TextState
*/
public TextState() {
s = new StringBuffer();
}
public TextState copyAll() {
TextState tmp = copyState();
if(s.length()==0) {
return tmp;
}
for(int i = 0; i<s.length(); i++) {
tmp.s.append(s.charAt(i));
}
return tmp;
}
public TextState copyState() {
TextState tmp = new TextState();
tmp.f = f;
tmp.x = x;
tmp.y = y;
return tmp;
}
public String toString() {
return s.toString();
}
public boolean isEmpty() {
return(s.length()==0);
}
public int getWidth(Graphics g) {
if((g==null)||(f==null)||(s.length()==0)) {
return 0;
}
return g.getFontMetrics(f).stringWidth(s.toString());
}
public int getHeight(Graphics g) {
if((g==null)||(f==null)) {
return 0;
}
return g.getFontMetrics(f).getHeight();
}
public int getAscent(Graphics g) {
if((g==null)||(f==null)) {
return 0;
}
return g.getFontMetrics(f).getAscent();
}
public int getDescent(Graphics g) {
if((g==null)||(f==null)) {
return 0;
}
return g.getFontMetrics(f).getDescent();
}
public int getMaxAscent(Graphics g) {
if((g==null)||(f==null)) {
return 0;
}
return g.getFontMetrics(f).getMaxAscent();
}
public int getMaxDescent(Graphics g) {
if((g==null)||(f==null)) {
return 0;
}
return g.getFontMetrics(f).getMaxDescent();
}
public int getLeading(Graphics g) {
if((g==null)||(f==null)) {
return 0;
}
return g.getFontMetrics(f).getLeading();
}
}
/*
* Open Source Physics software is free software; you can redistribute