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