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