6bf2670c65ddbfe971dc195f9afbeb008a656ae3
[jalviewjs.git] / site / swingjs / j2s / jssun / font / FontDesignMetrics.js
1 Clazz.declarePackage ("jssun.font");\r
2 Clazz.load (["java.awt.FontMetrics", "java.util.Hashtable"], "jssun.font.FontDesignMetrics", ["java.lang.IndexOutOfBoundsException", "java.awt.Toolkit", "swingjs.JSToolkit"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.ascent = 0;\r
5 this.descent = 0;\r
6 this.leading = 0;\r
7 this.height = -1;\r
8 Clazz.instantialize (this, arguments);\r
9 }, jssun.font, "FontDesignMetrics", java.awt.FontMetrics);\r
10 c$.getMetrics = Clazz.defineMethod (c$, "getMetrics", \r
11 function (font) {\r
12 var m = null;\r
13 var r;\r
14 r = jssun.font.FontDesignMetrics.metricsCache.get (font);\r
15 if (r != null) {\r
16 m = r.get ();\r
17 }if (m == null) {\r
18 m =  new jssun.font.FontDesignMetrics (font);\r
19 jssun.font.FontDesignMetrics.metricsCache.put (font,  new jssun.font.FontDesignMetrics.KeyReference (font, m));\r
20 }for (var i = 0; i < jssun.font.FontDesignMetrics.recentMetrics.length; i++) {\r
21 if (jssun.font.FontDesignMetrics.recentMetrics[i] === m) {\r
22 return m;\r
23 }}\r
24 {\r
25 jssun.font.FontDesignMetrics.recentMetrics[jssun.font.FontDesignMetrics.recentIndex++] = m;\r
26 if (jssun.font.FontDesignMetrics.recentIndex == 5) {\r
27 jssun.font.FontDesignMetrics.recentIndex = 0;\r
28 }}return m;\r
29 }, "java.awt.Font");\r
30 Clazz.makeConstructor (c$, \r
31  function (font) {\r
32 Clazz.superConstructor (this, jssun.font.FontDesignMetrics, [font]);\r
33 this.font = font;\r
34 this.initMatrixAndMetrics ();\r
35 }, "java.awt.Font");\r
36 Clazz.defineMethod (c$, "initMatrixAndMetrics", \r
37  function () {\r
38 {\r
39 //need to calculate ascent, descent, leading, and maxAdvance\r
40 }});\r
41 Clazz.defineMethod (c$, "charWidth", \r
42 function (ch) {\r
43 var s = "";\r
44 {\r
45 s = "" + ch;\r
46 }return this.stringWidth (s);\r
47 }, "~S");\r
48 Clazz.overrideMethod (c$, "stringWidth", \r
49 function (str) {\r
50 return Clazz.doubleToInt (0.5 + this.getWidth (str));\r
51 }, "~S");\r
52 Clazz.defineMethod (c$, "getWidth", \r
53  function (str) {\r
54 return swingjs.JSToolkit.getStringWidth (null, this.font, str);\r
55 }, "~S");\r
56 Clazz.overrideMethod (c$, "charsWidth", \r
57 function (data, off, len) {\r
58 var width = 0;\r
59 if (len < 0) {\r
60 throw  new IndexOutOfBoundsException ("len=" + len);\r
61 }var limit = off + len;\r
62 for (var i = off; i < limit; i++) {\r
63 var ch = data[i];\r
64 width += this.stringWidth ("" + ch);\r
65 }\r
66 return Clazz.doubleToInt (0.5 + width);\r
67 }, "~A,~N,~N");\r
68 Clazz.overrideMethod (c$, "getWidths", \r
69 function () {\r
70 var widths =  Clazz.newIntArray (256, 0);\r
71 return widths;\r
72 });\r
73 Clazz.defineMethod (c$, "getAscent", \r
74 function () {\r
75 if (this.ascent == 0) this.ascent = java.awt.Toolkit.getDefaultToolkit ().getFontMetrics (this.font).getAscent ();\r
76 return Clazz.floatToInt (jssun.font.FontDesignMetrics.roundingUpValue + this.ascent);\r
77 });\r
78 Clazz.defineMethod (c$, "getDescent", \r
79 function () {\r
80 if (this.descent == 0) this.descent = java.awt.Toolkit.getDefaultToolkit ().getFontMetrics (this.font).getDescent ();\r
81 return Clazz.floatToInt (jssun.font.FontDesignMetrics.roundingUpValue + this.descent);\r
82 });\r
83 Clazz.overrideMethod (c$, "getLeading", \r
84 function () {\r
85 return Clazz.floatToInt (jssun.font.FontDesignMetrics.roundingUpValue + this.descent + this.leading) - Clazz.floatToInt (jssun.font.FontDesignMetrics.roundingUpValue + this.descent);\r
86 });\r
87 Clazz.overrideMethod (c$, "getHeight", \r
88 function () {\r
89 if (this.height < 0) {\r
90 this.height = this.getAscent () + Clazz.floatToInt (jssun.font.FontDesignMetrics.roundingUpValue + this.descent + this.leading);\r
91 }return this.height;\r
92 });\r
93 Clazz.pu$h(self.c$);\r
94 c$ = Clazz.decorateAsClass (function () {\r
95 this.key = null;\r
96 this.val = null;\r
97 Clazz.instantialize (this, arguments);\r
98 }, jssun.font.FontDesignMetrics, "KeyReference");\r
99 Clazz.makeConstructor (c$, \r
100 function (a, b) {\r
101 this.key = a;\r
102 this.val = b;\r
103 }, "~O,~O");\r
104 Clazz.defineMethod (c$, "get", \r
105 function () {\r
106 return this.val;\r
107 });\r
108 Clazz.defineMethod (c$, "dispose", \r
109 function () {\r
110 if (jssun.font.FontDesignMetrics.metricsCache.get (this.key) === this) {\r
111 jssun.font.FontDesignMetrics.metricsCache.remove (this.key);\r
112 }});\r
113 c$ = Clazz.p0p ();\r
114 Clazz.defineStatics (c$,\r
115 "roundingUpValue", 0.95);\r
116 c$.metricsCache = c$.prototype.metricsCache =  new java.util.Hashtable ();\r
117 Clazz.defineStatics (c$,\r
118 "MAXRECENT", 5);\r
119 c$.recentMetrics = c$.prototype.recentMetrics =  new Array (5);\r
120 Clazz.defineStatics (c$,\r
121 "recentIndex", 0);\r
122 });\r