Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / site / j2s / java / awt / Font.js
1 Clazz.declarePackage ("java.awt");
2 Clazz.load (null, "java.awt.Font", ["java.awt.font.TextAttribute", "java.awt.geom.AffineTransform", "swingjs.JSToolkit"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.family = null;
5 this.name = null;
6 this.style = 0;
7 this.size = 0;
8 this.pointSize = 0;
9 this.$hasLayoutAttributes = false;
10 this.nonIdentityTx = false;
11 this.hash = 0;
12 Clazz.instantialize (this, arguments);
13 }, java.awt, "Font");
14 Clazz.makeConstructor (c$, 
15 function (name, style, size) {
16 this.name = (name != null) ? name : "Default";
17 this.style = (style & -4) == 0 ? style : 0;
18 this.size = size;
19 this.pointSize = size;
20 }, "~S,~N,~N");
21 Clazz.makeConstructor (c$, 
22  function (name, style, sizePts) {
23 this.name = (name != null) ? name : "Default";
24 this.style = (style & -4) == 0 ? style : 0;
25 this.size = Clazz.doubleToInt (sizePts + 0.5);
26 this.pointSize = sizePts;
27 }, "~S,~N,~N");
28 Clazz.makeConstructor (c$, 
29 function (font) {
30 this.name = font.name;
31 this.style = font.style;
32 this.size = font.size;
33 this.pointSize = font.pointSize;
34 }, "java.awt.Font");
35 Clazz.defineMethod (c$, "getTransform", 
36 function () {
37 return  new java.awt.geom.AffineTransform ();
38 });
39 Clazz.defineMethod (c$, "getFamily", 
40 function () {
41 return (this.family == null ? this.family = swingjs.JSToolkit.getFontFamily (this) : this.family);
42 });
43 Clazz.defineMethod (c$, "getName", 
44 function () {
45 return this.name;
46 });
47 Clazz.defineMethod (c$, "getFontName", 
48 function () {
49 return this.name;
50 });
51 Clazz.defineMethod (c$, "getStyle", 
52 function () {
53 return this.style;
54 });
55 Clazz.defineMethod (c$, "getSize", 
56 function () {
57 return this.size;
58 });
59 Clazz.defineMethod (c$, "getSize2D", 
60 function () {
61 return this.pointSize;
62 });
63 Clazz.defineMethod (c$, "isPlain", 
64 function () {
65 return this.style == 0;
66 });
67 Clazz.defineMethod (c$, "isBold", 
68 function () {
69 return (this.style & 1) != 0;
70 });
71 Clazz.defineMethod (c$, "isItalic", 
72 function () {
73 return (this.style & 2) != 0;
74 });
75 Clazz.defineMethod (c$, "isTransformed", 
76 function () {
77 return this.nonIdentityTx;
78 });
79 Clazz.defineMethod (c$, "hasLayoutAttributes", 
80 function () {
81 return this.$hasLayoutAttributes;
82 });
83 c$.getFont = Clazz.defineMethod (c$, "getFont", 
84 function (nm) {
85 return java.awt.Font.getFont (nm, null);
86 }, "~S");
87 c$.decode = Clazz.defineMethod (c$, "decode", 
88 function (str) {
89 var fontName = str;
90 var styleName = "";
91 var fontSize = 12;
92 var fontStyle = 0;
93 if (str == null) {
94 return  new java.awt.Font ("Dialog", fontStyle, fontSize);
95 }var lastHyphen = str.lastIndexOf ('-');
96 var lastSpace = str.lastIndexOf (' ');
97 var sepChar = (lastHyphen > lastSpace) ? '-' : ' ';
98 var sizeIndex = str.lastIndexOf (sepChar);
99 var styleIndex = str.lastIndexOf (sepChar, sizeIndex - 1);
100 var strlen = str.length;
101 if (sizeIndex > 0 && sizeIndex + 1 < strlen) {
102 try {
103 fontSize = Integer.$valueOf (str.substring (sizeIndex + 1)).intValue ();
104 if (fontSize <= 0) {
105 fontSize = 12;
106 }} catch (e) {
107 if (Clazz.exceptionOf (e, NumberFormatException)) {
108 styleIndex = sizeIndex;
109 sizeIndex = strlen;
110 if (str.charAt (sizeIndex - 1) == sepChar) {
111 sizeIndex--;
112 }} else {
113 throw e;
114 }
115 }
116 }if (styleIndex >= 0 && styleIndex + 1 < strlen) {
117 styleName = str.substring (styleIndex + 1, sizeIndex);
118 styleName = styleName.toLowerCase ();
119 if (styleName.equals ("bolditalic")) {
120 fontStyle = 3;
121 } else if (styleName.equals ("italic")) {
122 fontStyle = 2;
123 } else if (styleName.equals ("bold")) {
124 fontStyle = 1;
125 } else if (styleName.equals ("plain")) {
126 fontStyle = 0;
127 } else {
128 styleIndex = sizeIndex;
129 if (str.charAt (styleIndex - 1) == sepChar) {
130 styleIndex--;
131 }}fontName = str.substring (0, styleIndex);
132 } else {
133 var fontEnd = strlen;
134 if (styleIndex > 0) {
135 fontEnd = styleIndex;
136 } else if (sizeIndex > 0) {
137 fontEnd = sizeIndex;
138 }if (fontEnd > 0 && str.charAt (fontEnd - 1) == sepChar) {
139 fontEnd--;
140 }fontName = str.substring (0, fontEnd);
141 }return  new java.awt.Font (fontName, fontStyle, fontSize);
142 }, "~S");
143 c$.getFont = Clazz.defineMethod (c$, "getFont", 
144 function (nm, font) {
145 var str = null;
146 try {
147 str = System.getProperty (nm);
148 } catch (e) {
149 if (Clazz.exceptionOf (e, SecurityException)) {
150 } else {
151 throw e;
152 }
153 }
154 if (str == null) {
155 return font;
156 }return java.awt.Font.decode (str);
157 }, "~S,java.awt.Font");
158 Clazz.overrideMethod (c$, "hashCode", 
159 function () {
160 if (this.hash == 0) {
161 this.hash = this.name.hashCode () ^ this.style ^ this.size;
162 }return this.hash;
163 });
164 Clazz.overrideMethod (c$, "equals", 
165 function (obj) {
166 if (obj === this) {
167 return true;
168 }if (obj != null) {
169 try {
170 var font = obj;
171 if (this.size == font.size && this.style == font.style && this.nonIdentityTx == font.nonIdentityTx && this.$hasLayoutAttributes == font.$hasLayoutAttributes && this.pointSize == font.pointSize && this.name.equals (font.name)) {
172 return true;
173 }} catch (e) {
174 if (Clazz.exceptionOf (e, ClassCastException)) {
175 } else {
176 throw e;
177 }
178 }
179 }return false;
180 }, "~O");
181 Clazz.overrideMethod (c$, "toString", 
182 function () {
183 var strStyle;
184 if (this.isBold ()) {
185 strStyle = this.isItalic () ? "bolditalic" : "bold";
186 } else {
187 strStyle = this.isItalic () ? "italic" : "plain";
188 }return this.getClass ().getName () + "[family=" + this.getFamily () + ",name=" + this.name + ",style=" + strStyle + ",size=" + this.size + "]";
189 });
190 Clazz.defineMethod (c$, "getAvailableAttributes", 
191 function () {
192 var attributes =  Clazz.newArray (-1, [java.awt.font.TextAttribute.FAMILY, java.awt.font.TextAttribute.WEIGHT, java.awt.font.TextAttribute.WIDTH, java.awt.font.TextAttribute.SIZE, java.awt.font.TextAttribute.UNDERLINE, java.awt.font.TextAttribute.STRIKETHROUGH]);
193 return attributes;
194 });
195 Clazz.defineMethod (c$, "deriveFont", 
196 function (style, size) {
197 return  new java.awt.Font (this.name, style, size);
198 }, "~N,~N");
199 Clazz.defineMethod (c$, "deriveFont", 
200 function (size) {
201 return  new java.awt.Font (this.name, this.style, size);
202 }, "~N");
203 Clazz.defineMethod (c$, "deriveFont", 
204 function (style) {
205 return  new java.awt.Font (this.name, style, this.size);
206 }, "~N");
207 Clazz.defineMethod (c$, "hasUniformLineMetrics", 
208 function () {
209 return false;
210 });
211 Clazz.overrideMethod (c$, "finalize", 
212 function () {
213 });
214 Clazz.defineStatics (c$,
215 "DIALOG", "Dialog",
216 "DIALOG_INPUT", "DialogInput",
217 "SANS_SERIF", "SansSerif",
218 "SERIF", "Serif",
219 "MONOSPACED", "Monospaced",
220 "PLAIN", 0,
221 "BOLD", 1,
222 "ITALIC", 2,
223 "ROMAN_BASELINE", 0,
224 "CENTER_BASELINE", 1,
225 "HANGING_BASELINE", 2,
226 "TRUETYPE_FONT", 0,
227 "TYPE1_FONT", 1,
228 "LAYOUT_LEFT_TO_RIGHT", 0,
229 "LAYOUT_RIGHT_TO_LEFT", 1,
230 "LAYOUT_NO_START_CONTEXT", 2,
231 "LAYOUT_NO_LIMIT_CONTEXT", 4);
232 });