JAL-1807 Bob
[jalviewjs.git] / site / j2s / swingjs / JSFontMetrics.js
1 Clazz.declarePackage ("swingjs");
2 Clazz.load (["java.awt.FontMetrics"], "swingjs.JSFontMetrics", ["swingjs.JSToolkit"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.widths = null;
5 this.iwidths = null;
6 Clazz.instantialize (this, arguments);
7 }, swingjs, "JSFontMetrics", java.awt.FontMetrics);
8 Clazz.makeConstructor (c$, 
9 function () {
10 Clazz.superConstructor (this, swingjs.JSFontMetrics, [null]);
11 });
12 Clazz.defineMethod (c$, "setFont", 
13 function (f) {
14 this.font = f;
15 }, "java.awt.Font");
16 Clazz.overrideMethod (c$, "getLeading", 
17 function () {
18 return Clazz.doubleToInt (this.font.getSize () / 20) + 1;
19 });
20 Clazz.overrideMethod (c$, "getAscent", 
21 function () {
22 return this.font.getSize ();
23 });
24 Clazz.overrideMethod (c$, "getDescent", 
25 function () {
26 return Clazz.doubleToInt (this.font.getSize () / 4) + 1;
27 });
28 Clazz.defineMethod (c$, "charWidth", 
29 function (pt) {
30 return (pt.charCodeAt (0) < 256 ? Clazz.floatToInt (this.getWidthsFloat ()[pt.charCodeAt (0)]) : this.stringWidth ("" + pt));
31 }, "~S");
32 Clazz.defineMethod (c$, "charWidth", 
33 function (pt) {
34 return (pt < 256 ? Clazz.floatToInt (this.getWidthsFloat ()[pt]) : this.stringWidth ("" + String.fromCharCode (pt)));
35 }, "~N");
36 Clazz.overrideMethod (c$, "stringWidth", 
37 function (s) {
38 return Clazz.floatToInt (swingjs.JSToolkit.getStringWidth (null, this.font, s));
39 }, "~S");
40 Clazz.overrideMethod (c$, "getWidths", 
41 function () {
42 if (this.iwidths != null) return this.iwidths;
43 this.iwidths =  Clazz.newIntArray (256, 0);
44 this.getWidthsFloat ();
45 for (var ch = 0; ch < 256; ch++) {
46 this.iwidths[ch] = Clazz.floatToInt (this.widths[ch]);
47 }
48 return this.iwidths;
49 });
50 Clazz.defineMethod (c$, "getWidthsFloat", 
51 function () {
52 if (this.widths != null) return this.widths;
53 this.widths =  Clazz.newFloatArray (256, 0);
54 for (var ch = 0; ch < 256; ch++) {
55 this.widths[ch] = swingjs.JSToolkit.getStringWidth (null, this.font, "" + String.fromCharCode (ch));
56 }
57 return this.widths;
58 });
59 });