-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFont.java
More file actions
1541 lines (1345 loc) · 49 KB
/
Font.java
File metadata and controls
1541 lines (1345 loc) · 49 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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package java.awt;
import com.android.internal.awt.AndroidGraphics2D;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.font.LineMetrics;
import java.awt.font.TextAttribute;
import java.awt.font.TransformAttribute;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.text.CharacterIterator;
import java.text.AttributedCharacterIterator.Attribute;
import java.util.Hashtable;
import java.util.Locale;
import java.util.Map;
import java.util.StringTokenizer;
import org.apache.harmony.awt.gl.font.AndroidGlyphVector;
import org.apache.harmony.awt.gl.font.CommonGlyphVector;
import org.apache.harmony.awt.gl.font.FontPeerImpl;
import org.apache.harmony.awt.gl.font.FontMetricsImpl;
import org.apache.harmony.awt.gl.font.LineMetricsImpl;
import org.apache.harmony.awt.internal.nls.Messages;
import org.apache.harmony.luni.util.NotImplementedException;
import org.apache.harmony.misc.HashCode;
/**
* The Font class represents fonts for rendering text. This class allow to map
* characters to glyphs.
* <p>
* A glyph is a shape used to render a character or a sequence of characters.
* For example one character of Latin writing system represented by one glyph,
* but in complex writing system such as South and South-East Asian there is
* more complicated correspondence between characters and glyphs.
* <p>
* The Font object is identified by two types of names. The logical font name is
* the name that is used to construct the font. The font name is the name of a
* particular font face (for example, Arial Bold). The family name is the font's
* family name that specifies the typographic design across several faces (for
* example, Arial). In all the Font is identified by three attributes: the
* family name, the style (such as bold or italic), and the size.
*
* @since Android 1.0
*/
public class Font implements Serializable {
/**
* The Constant serialVersionUID.
*/
private static final long serialVersionUID = -4206021311591459213L;
// Identity Transform attribute
/**
* The Constant IDENTITY_TRANSFORM.
*/
private static final TransformAttribute IDENTITY_TRANSFORM = new TransformAttribute(
new AffineTransform());
/**
* The Constant PLAIN indicates font's plain style.
*/
public static final int PLAIN = 0;
/**
* The Constant BOLD indicates font's bold style.
*/
public static final int BOLD = 1;
/**
* The Constant ITALIC indicates font's italic style.
*/
public static final int ITALIC = 2;
/**
* The Constant ROMAN_BASELINE indicated roman baseline.
*/
public static final int ROMAN_BASELINE = 0;
/**
* The Constant CENTER_BASELINE indicates center baseline.
*/
public static final int CENTER_BASELINE = 1;
/**
* The Constant HANGING_BASELINE indicates hanging baseline.
*/
public static final int HANGING_BASELINE = 2;
/**
* The Constant TRUETYPE_FONT indicates a font resource of type TRUETYPE.
*/
public static final int TRUETYPE_FONT = 0;
/**
* The Constant TYPE1_FONT indicates a font resource of type TYPE1.
*/
public static final int TYPE1_FONT = 1;
/**
* The Constant LAYOUT_LEFT_TO_RIGHT indicates that text is left to right.
*/
public static final int LAYOUT_LEFT_TO_RIGHT = 0;
/**
* The Constant LAYOUT_RIGHT_TO_LEFT indicates that text is right to left.
*/
public static final int LAYOUT_RIGHT_TO_LEFT = 1;
/**
* The Constant LAYOUT_NO_START_CONTEXT indicates that the text in the char
* array before the indicated start should not be examined.
*/
public static final int LAYOUT_NO_START_CONTEXT = 2;
/**
* The Constant LAYOUT_NO_LIMIT_CONTEXT indicates that text in the char
* array after the indicated limit should not be examined.
*/
public static final int LAYOUT_NO_LIMIT_CONTEXT = 4;
/**
* The Constant DEFAULT_FONT.
*/
static final Font DEFAULT_FONT = new Font("Dialog", Font.PLAIN, 12); //$NON-NLS-1$
/**
* The name of the Font.
*/
protected String name;
/**
* The style of the Font.
*/
protected int style;
/**
* The size of the Font.
*/
protected int size;
/**
* The point size of the Font.
*/
protected float pointSize;
// Flag if the Font object transformed
/**
* The transformed.
*/
private boolean transformed;
// Set of font attributes
/**
* The requested attributes.
*/
private Hashtable<Attribute, Object> fRequestedAttributes;
// font peer object corresponding to this Font
/**
* The font peer.
*/
private transient FontPeerImpl fontPeer;
// number of glyphs in this Font
/**
* The num glyphs.
*/
private transient int numGlyphs = -1;
// code for missing glyph for this Font
/**
* The missing glyph code.
*/
private transient int missingGlyphCode = -1;
/**
* Writes object to ObjectOutputStream.
*
* @param out
* ObjectOutputStream.
* @throws IOException
* Signals that an I/O exception has occurred.
*/
private void writeObject(java.io.ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
}
/**
* Reads object from ObjectInputStream object and set native platform
* dependent fields to default values.
*
* @param in
* ObjectInputStream object.
* @throws IOException
* Signals that an I/O exception has occurred.
* @throws ClassNotFoundException
* the class not found exception.
*/
private void readObject(java.io.ObjectInputStream in) throws IOException,
ClassNotFoundException {
in.defaultReadObject();
numGlyphs = -1;
missingGlyphCode = -1;
}
/**
* Instantiates a new Font with the specified attributes. The Font will be
* created with default attributes if the attribute's parameter is null.
*
* @param attributes
* the attributes to be assigned to the new Font, or null.
*/
public Font(Map<? extends Attribute, ?> attributes) {
Object currAttr;
// Default values are taken from the documentation of the Font class.
// See Font constructor, decode and getFont sections.
this.name = "default"; //$NON-NLS-1$
this.size = 12;
this.pointSize = 12;
this.style = Font.PLAIN;
if (attributes != null) {
fRequestedAttributes = new Hashtable<Attribute, Object>(attributes);
currAttr = attributes.get(TextAttribute.SIZE);
if (currAttr != null) {
this.pointSize = ((Float)currAttr).floatValue();
this.size = (int)Math.ceil(this.pointSize);
}
currAttr = attributes.get(TextAttribute.POSTURE);
if (currAttr != null && currAttr.equals(TextAttribute.POSTURE_OBLIQUE)) {
this.style |= Font.ITALIC;
}
currAttr = attributes.get(TextAttribute.WEIGHT);
if ((currAttr != null)
&& (((Float)currAttr).floatValue() >= (TextAttribute.WEIGHT_BOLD).floatValue())) {
this.style |= Font.BOLD;
}
currAttr = attributes.get(TextAttribute.FAMILY);
if (currAttr != null) {
this.name = (String)currAttr;
}
currAttr = attributes.get(TextAttribute.TRANSFORM);
if (currAttr != null) {
if (currAttr instanceof TransformAttribute) {
this.transformed = !((TransformAttribute)currAttr).getTransform().isIdentity();
} else if (currAttr instanceof AffineTransform) {
this.transformed = !((AffineTransform)currAttr).isIdentity();
}
}
} else {
fRequestedAttributes = new Hashtable<Attribute, Object>(5);
fRequestedAttributes.put(TextAttribute.TRANSFORM, IDENTITY_TRANSFORM);
this.transformed = false;
fRequestedAttributes.put(TextAttribute.FAMILY, name);
fRequestedAttributes.put(TextAttribute.SIZE, new Float(this.size));
if ((this.style & Font.BOLD) != 0) {
fRequestedAttributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
} else {
fRequestedAttributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_REGULAR);
}
if ((this.style & Font.ITALIC) != 0) {
fRequestedAttributes.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
} else {
fRequestedAttributes.put(TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR);
}
}
}
/**
* Instantiates a new Font with the specified name, style and size.
*
* @param name
* the name of font.
* @param style
* the style of font.
* @param size
* the size of font.
*/
public Font(String name, int style, int size) {
this.name = (name != null) ? name : "Default"; //$NON-NLS-1$
this.size = (size >= 0) ? size : 0;
this.style = (style & ~0x03) == 0 ? style : Font.PLAIN;
this.pointSize = this.size;
fRequestedAttributes = new Hashtable<Attribute, Object>(5);
fRequestedAttributes.put(TextAttribute.TRANSFORM, IDENTITY_TRANSFORM);
this.transformed = false;
fRequestedAttributes.put(TextAttribute.FAMILY, this.name);
fRequestedAttributes.put(TextAttribute.SIZE, new Float(this.size));
if ((this.style & Font.BOLD) != 0) {
fRequestedAttributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
} else {
fRequestedAttributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_REGULAR);
}
if ((this.style & Font.ITALIC) != 0) {
fRequestedAttributes.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
} else {
fRequestedAttributes.put(TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR);
}
}
/**
* Returns true if this Font has a glyph for the specified character.
*
* @param c
* the character.
* @return true if this Font has a glyph for the specified character, false
* otherwise.
*/
public boolean canDisplay(char c) {
FontPeerImpl peer = (FontPeerImpl)this.getPeer();
return peer.canDisplay(c);
}
/**
* Returns true if the Font can display the characters of the the specified
* text from the specified start position to the specified limit position.
*
* @param text
* the text.
* @param start
* the start offset (in the character array).
* @param limit
* the limit offset (in the character array).
* @return the a character's position in the text that this Font can not
* display, or -1 if this Font can display all characters in this
* text.
*/
public int canDisplayUpTo(char[] text, int start, int limit) {
int st = start;
int result;
while ((st < limit) && canDisplay(text[st])) {
st++;
}
if (st == limit) {
result = -1;
} else {
result = st;
}
return result;
}
/**
* Returns true if the Font can display the characters of the the specified
* CharacterIterator from the specified start position and the specified
* limit position.
*
* @param iter
* the CharacterIterator.
* @param start
* the start offset.
* @param limit
* the limit offset.
* @return the a character's position in the CharacterIterator that this
* Font can not display, or -1 if this Font can display all
* characters in this text.
*/
public int canDisplayUpTo(CharacterIterator iter, int start, int limit) {
int st = start;
char c = iter.setIndex(start);
int result;
while ((st < limit) && (canDisplay(c))) {
st++;
c = iter.next();
}
if (st == limit) {
result = -1;
} else {
result = st;
}
return result;
}
/**
* Returns true if this Font can display a specified String.
*
* @param str
* the String.
* @return the a character's position in the String that this Font can not
* display, or -1 if this Font can display all characters in this
* text.
*/
public int canDisplayUpTo(String str) {
char[] chars = str.toCharArray();
return canDisplayUpTo(chars, 0, chars.length);
}
/**
* Creates a GlyphVector of associating characters to glyphs based on the
* Unicode map of this Font.
*
* @param frc
* the FontRenderContext.
* @param chars
* the characters array.
* @return the GlyphVector of associating characters to glyphs based on the
* Unicode map of this Font.
*/
public GlyphVector createGlyphVector(FontRenderContext frc, char[] chars) {
return new AndroidGlyphVector(chars, frc, this, 0);
}
/**
* Creates a GlyphVector of associating characters contained in the
* specified CharacterIterator to glyphs based on the Unicode map of this
* Font.
*
* @param frc
* the FontRenderContext.
* @param iter
* the CharacterIterator.
* @return the GlyphVector of associating characters contained in the
* specified CharacterIterator to glyphs based on the Unicode map of
* this Font.
*/
public GlyphVector createGlyphVector(FontRenderContext frc, CharacterIterator iter) {
throw new RuntimeException("Not implemented!"); //$NON-NLS-1$
}
/**
* Creates a GlyphVector of associating characters to glyphs based on the
* Unicode map of this Font.
*
* @param frc
* the FontRenderContext.
* @param glyphCodes
* the specified integer array of glyph codes.
* @return the GlyphVector of associating characters to glyphs based on the
* Unicode map of this Font.
* @throws NotImplementedException
* if this method is not implemented by a subclass.
*/
public GlyphVector createGlyphVector(FontRenderContext frc, int[] glyphCodes)
throws org.apache.harmony.luni.util.NotImplementedException {
throw new RuntimeException("Not implemented!"); //$NON-NLS-1$
}
/**
* Creates a GlyphVector of associating characters to glyphs based on the
* Unicode map of this Font.
*
* @param frc
* the FontRenderContext.
* @param str
* the specified String.
* @return the GlyphVector of associating characters to glyphs based on the
* Unicode map of this Font.
*/
public GlyphVector createGlyphVector(FontRenderContext frc, String str) {
return new AndroidGlyphVector(str.toCharArray(), frc, this, 0);
}
/**
* Returns the font style constant value corresponding to one of the font
* style names ("BOLD", "ITALIC", "BOLDITALIC"). This method returns
* Font.PLAIN if the argument is not one of the predefined style names.
*
* @param fontStyleName
* font style name.
* @return font style constant value corresponding to the font style name
* specified.
*/
private static int getFontStyle(String fontStyleName) {
int result = Font.PLAIN;
if (fontStyleName.toUpperCase().equals("BOLDITALIC")) { //$NON-NLS-1$
result = Font.BOLD | Font.ITALIC;
} else if (fontStyleName.toUpperCase().equals("BOLD")) { //$NON-NLS-1$
result = Font.BOLD;
} else if (fontStyleName.toUpperCase().equals("ITALIC")) { //$NON-NLS-1$
result = Font.ITALIC;
}
return result;
}
/**
* Decodes the specified string which described the Font. The string should
* have the following format: fontname-style-pointsize. The style can be
* PLAIN, BOLD, BOLDITALIC, or ITALIC.
*
* @param str
* the string which describes the font.
* @return the Font from the specified string.
*/
public static Font decode(String str) {
// XXX: Documentation doesn't describe all cases, e.g. fonts face names
// with
// symbols that are suggested as delimiters in the documentation.
// In this decode implementation only ***-***-*** format is used with
// '-'
// as the delimiter to avoid unexpected parse results of font face names
// with spaces.
if (str == null) {
return DEFAULT_FONT;
}
StringTokenizer strTokens;
String delim = "-"; //$NON-NLS-1$
String substr;
int fontSize = DEFAULT_FONT.size;
int fontStyle = DEFAULT_FONT.style;
String fontName = DEFAULT_FONT.name;
strTokens = new StringTokenizer(str.trim(), delim);
// Font Name
if (strTokens.hasMoreTokens()) {
fontName = strTokens.nextToken(); // first token is the font name
}
// Font Style or Size (if the style is undefined)
if (strTokens.hasMoreTokens()) {
substr = strTokens.nextToken();
try {
// if second token is the font size
fontSize = Integer.parseInt(substr);
} catch (NumberFormatException e) {
// then second token is the font style
fontStyle = getFontStyle(substr);
}
}
// Font Size
if (strTokens.hasMoreTokens()) {
try {
fontSize = Integer.parseInt(strTokens.nextToken());
} catch (NumberFormatException e) {
}
}
return new Font(fontName, fontStyle, fontSize);
}
/**
* Performs the specified affine transform to the Font and returns a new
* Font.
*
* @param trans
* the AffineTransform.
* @return the Font object.
* @throws IllegalArgumentException
* if affine transform parameter is null.
*/
@SuppressWarnings("unchecked")
public Font deriveFont(AffineTransform trans) {
if (trans == null) {
// awt.94=transform can not be null
throw new IllegalArgumentException(Messages.getString("awt.94")); //$NON-NLS-1$
}
Hashtable<Attribute, Object> derivefRequestedAttributes = (Hashtable<Attribute, Object>)fRequestedAttributes
.clone();
derivefRequestedAttributes.put(TextAttribute.TRANSFORM, new TransformAttribute(trans));
return new Font(derivefRequestedAttributes);
}
/**
* Returns a new Font that is a copy of the current Font modified so that
* the size is the specified size.
*
* @param size
* the size of font.
* @return the Font object.
*/
@SuppressWarnings("unchecked")
public Font deriveFont(float size) {
Hashtable<Attribute, Object> derivefRequestedAttributes = (Hashtable<Attribute, Object>)fRequestedAttributes
.clone();
derivefRequestedAttributes.put(TextAttribute.SIZE, new Float(size));
return new Font(derivefRequestedAttributes);
}
/**
* Returns a new Font that is a copy of the current Font modified so that
* the style is the specified style.
*
* @param style
* the style of font.
* @return the Font object.
*/
@SuppressWarnings("unchecked")
public Font deriveFont(int style) {
Hashtable<Attribute, Object> derivefRequestedAttributes = (Hashtable<Attribute, Object>)fRequestedAttributes
.clone();
if ((style & Font.BOLD) != 0) {
derivefRequestedAttributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
} else if (derivefRequestedAttributes.get(TextAttribute.WEIGHT) != null) {
derivefRequestedAttributes.remove(TextAttribute.WEIGHT);
}
if ((style & Font.ITALIC) != 0) {
derivefRequestedAttributes.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
} else if (derivefRequestedAttributes.get(TextAttribute.POSTURE) != null) {
derivefRequestedAttributes.remove(TextAttribute.POSTURE);
}
return new Font(derivefRequestedAttributes);
}
/**
* Returns a new Font that is a copy of the current Font modified to match
* the specified style and with the specified affine transform applied to
* its glyphs.
*
* @param style
* the style of font.
* @param trans
* the AffineTransform.
* @return the Font object.
*/
@SuppressWarnings("unchecked")
public Font deriveFont(int style, AffineTransform trans) {
if (trans == null) {
// awt.94=transform can not be null
throw new IllegalArgumentException(Messages.getString("awt.94")); //$NON-NLS-1$
}
Hashtable<Attribute, Object> derivefRequestedAttributes = (Hashtable<Attribute, Object>)fRequestedAttributes
.clone();
if ((style & BOLD) != 0) {
derivefRequestedAttributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
} else if (derivefRequestedAttributes.get(TextAttribute.WEIGHT) != null) {
derivefRequestedAttributes.remove(TextAttribute.WEIGHT);
}
if ((style & ITALIC) != 0) {
derivefRequestedAttributes.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
} else if (derivefRequestedAttributes.get(TextAttribute.POSTURE) != null) {
derivefRequestedAttributes.remove(TextAttribute.POSTURE);
}
derivefRequestedAttributes.put(TextAttribute.TRANSFORM, new TransformAttribute(trans));
return new Font(derivefRequestedAttributes);
}
/**
* Returns a new Font that is a copy of the current Font modified so that
* the size and style are the specified size and style.
*
* @param style
* the style of font.
* @param size
* the size of font.
* @return the Font object.
*/
@SuppressWarnings("unchecked")
public Font deriveFont(int style, float size) {
Hashtable<Attribute, Object> derivefRequestedAttributes = (Hashtable<Attribute, Object>)fRequestedAttributes
.clone();
if ((style & BOLD) != 0) {
derivefRequestedAttributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
} else if (derivefRequestedAttributes.get(TextAttribute.WEIGHT) != null) {
derivefRequestedAttributes.remove(TextAttribute.WEIGHT);
}
if ((style & ITALIC) != 0) {
derivefRequestedAttributes.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
} else if (derivefRequestedAttributes.get(TextAttribute.POSTURE) != null) {
derivefRequestedAttributes.remove(TextAttribute.POSTURE);
}
derivefRequestedAttributes.put(TextAttribute.SIZE, new Float(size));
return new Font(derivefRequestedAttributes);
}
/**
* Returns a new Font object with a new set of font attributes.
*
* @param attributes
* the map of attributes.
* @return the Font.
*/
@SuppressWarnings("unchecked")
public Font deriveFont(Map<? extends Attribute, ?> attributes) {
Attribute[] avalAttributes = this.getAvailableAttributes();
Hashtable<Attribute, Object> derivefRequestedAttributes = (Hashtable<Attribute, Object>)fRequestedAttributes
.clone();
Object currAttribute;
for (Attribute element : avalAttributes) {
currAttribute = attributes.get(element);
if (currAttribute != null) {
derivefRequestedAttributes.put(element, currAttribute);
}
}
return new Font(derivefRequestedAttributes);
}
/**
* Compares the specified Object with the current Font.
*
* @param obj
* the Object to be compared.
* @return true, if the specified Object is an instance of Font with the
* same family, size, and style as this Font, false otherwise.
*/
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj != null) {
try {
Font font = (Font)obj;
return ((this.style == font.style) && (this.size == font.size)
&& this.name.equals(font.name) && (this.pointSize == font.pointSize) && (this
.getTransform()).equals(font.getTransform()));
} catch (ClassCastException e) {
}
}
return false;
}
/**
* Gets the map of font's attributes.
*
* @return the map of font's attributes.
*/
@SuppressWarnings("unchecked")
public Map<TextAttribute, ?> getAttributes() {
return (Map<TextAttribute, ?>)fRequestedAttributes.clone();
}
/**
* Gets the keys of all available attributes.
*
* @return the keys array of all available attributes.
*/
public Attribute[] getAvailableAttributes() {
Attribute[] attrs = {
TextAttribute.FAMILY, TextAttribute.POSTURE, TextAttribute.SIZE,
TextAttribute.TRANSFORM, TextAttribute.WEIGHT, TextAttribute.SUPERSCRIPT,
TextAttribute.WIDTH
};
return attrs;
}
/**
* Gets the baseline for this character.
*
* @param c
* the character.
* @return the baseline for this character.
*/
public byte getBaselineFor(char c) {
// TODO: implement using TT BASE table data
return 0;
}
/**
* Gets the family name of the Font.
*
* @return the family name of the Font.
*/
public String getFamily() {
if (fRequestedAttributes != null) {
fRequestedAttributes.get(TextAttribute.FAMILY);
}
return null;
}
/**
* Returns the family name of this Font associated with the specified
* locale.
*
* @param l
* the locale.
* @return the family name of this Font associated with the specified
* locale.
*/
public String getFamily(Locale l) {
if (l == null) {
// awt.01='{0}' parameter is null
throw new NullPointerException(Messages.getString("awt.01", "Locale")); //$NON-NLS-1$ //$NON-NLS-2$
}
return getFamily();
}
/**
* Gets a Font with the specified attribute set.
*
* @param attributes
* the attributes to be assigned to the new Font.
* @return the Font.
*/
public static Font getFont(Map<? extends Attribute, ?> attributes) {
Font fnt = (Font)attributes.get(TextAttribute.FONT);
if (fnt != null) {
return fnt;
}
return new Font(attributes);
}
/**
* Gets a Font object from the system properties list with the specified
* name or returns the specified Font if there is no such property.
*
* @param sp
* the specified property name.
* @param f
* the Font.
* @return the Font object from the system properties list with the
* specified name or the specified Font if there is no such
* property.
*/
public static Font getFont(String sp, Font f) {
String pr = System.getProperty(sp);
if (pr == null) {
return f;
}
return decode(pr);
}
/**
* Gets a Font object from the system properties list with the specified
* name.
*
* @param sp
* the system property name.
* @return the Font, or null if there is no such property with the specified
* name.
*/
public static Font getFont(String sp) {
return getFont(sp, null);
}
/**
* Gets the font name.
*
* @return the font name.
*/
public String getFontName() {
if (fRequestedAttributes != null) {
fRequestedAttributes.get(TextAttribute.FAMILY);
}
return null;
}
/**
* Returns the font name associated with the specified locale.
*
* @param l
* the locale.
* @return the font name associated with the specified locale.
*/
public String getFontName(Locale l) {
return getFamily();
}
/**
* Returns a LineMetrics object created with the specified parameters.
*
* @param chars
* the chars array.
* @param start
* the start offset.
* @param end
* the end offset.
* @param frc
* the FontRenderContext.
* @return the LineMetrics for the specified parameters.
*/
public LineMetrics getLineMetrics(char[] chars, int start, int end, FontRenderContext frc) {
if (frc == null) {
// awt.00=FontRenderContext is null
throw new NullPointerException(Messages.getString("awt.00")); //$NON-NLS-1$
}
// FontMetrics fm = AndroidGraphics2D.getInstance().getFontMetrics();
FontMetrics fm = new FontMetricsImpl(this);
float[] fmet = {
fm.getAscent(), fm.getDescent(), fm.getLeading()
};
return new LineMetricsImpl(chars.length, fmet, null);
}
/**
* Returns a LineMetrics object created with the specified parameters.
*
* @param iter
* the CharacterIterator.
* @param start
* the start offset.
* @param end
* the end offset.
* @param frc
* the FontRenderContext.
* @return the LineMetrics for the specified parameters.
*/
public LineMetrics getLineMetrics(CharacterIterator iter, int start, int end,
FontRenderContext frc) {
if (frc == null) {
// awt.00=FontRenderContext is null
throw new NullPointerException(Messages.getString("awt.00")); //$NON-NLS-1$
}
String resultString;
int iterCount;
iterCount = end - start;
if (iterCount < 0) {
resultString = ""; //$NON-NLS-1$
} else {
char[] chars = new char[iterCount];
int i = 0;
for (char c = iter.setIndex(start); c != CharacterIterator.DONE && (i < iterCount); c = iter
.next()) {
chars[i] = c;
i++;
}
resultString = new String(chars);
}
return this.getLineMetrics(resultString, frc);
}
/**
* Returns a LineMetrics object created with the specified parameters.
*
* @param str
* the String.
* @param frc
* the FontRenderContext.
* @return the LineMetrics for the specified parameters.
*/
public LineMetrics getLineMetrics(String str, FontRenderContext frc) {
// FontMetrics fm = AndroidGraphics2D.getInstance().getFontMetrics();
FontMetrics fm = new FontMetricsImpl(this);
float[] fmet = {
fm.getAscent(), fm.getDescent(), fm.getLeading()
};
// Log.i("FONT FMET", fmet.toString());
return new LineMetricsImpl(str.length(), fmet, null);