Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / site / j2s / java / awt / FontMetrics.js
1 Clazz.declarePackage ("java.awt");
2 Clazz.load (["java.awt.font.FontRenderContext"], "java.awt.FontMetrics", ["java.lang.Character"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.font = null;
5 Clazz.instantialize (this, arguments);
6 }, java.awt, "FontMetrics");
7 Clazz.makeConstructor (c$, 
8 function (font) {
9 this.font = font;
10 }, "java.awt.Font");
11 Clazz.defineMethod (c$, "getFont", 
12 function () {
13 return this.font;
14 });
15 Clazz.defineMethod (c$, "getFontRenderContext", 
16 function () {
17 return java.awt.FontMetrics.DEFAULT_FRC;
18 });
19 Clazz.defineMethod (c$, "getLeading", 
20 function () {
21 return 0;
22 });
23 Clazz.defineMethod (c$, "getAscent", 
24 function () {
25 return this.font.getSize ();
26 });
27 Clazz.defineMethod (c$, "getDescent", 
28 function () {
29 return 0;
30 });
31 Clazz.defineMethod (c$, "getHeight", 
32 function () {
33 return this.getLeading () + this.getAscent () + this.getDescent ();
34 });
35 Clazz.defineMethod (c$, "getMaxAscent", 
36 function () {
37 return this.getAscent ();
38 });
39 Clazz.defineMethod (c$, "getMaxDescent", 
40 function () {
41 return this.getDescent ();
42 });
43 Clazz.defineMethod (c$, "getMaxDecent", 
44 function () {
45 return this.getMaxDescent ();
46 });
47 Clazz.defineMethod (c$, "getMaxAdvance", 
48 function () {
49 return -1;
50 });
51 Clazz.defineMethod (c$, "charWidth", 
52 function (codePoint) {
53 if (!Character.isValidCodePoint (codePoint)) {
54 codePoint = 0xffff;
55 }if (codePoint < 256) {
56 return this.getWidths ()[codePoint];
57 } else {
58 var buffer =  Clazz.newCharArray (2, '\0');
59 var len = Character.toChars (codePoint, buffer, 0);
60 return this.charsWidth (buffer, 0, len);
61 }}, "~N");
62 Clazz.defineMethod (c$, "charWidth", 
63 function (ch) {
64 if (ch.charCodeAt (0) < 256) {
65 return this.getWidths ()[ch.charCodeAt (0)];
66 }var data =  Clazz.newCharArray (-1, [ch]);
67 return this.charsWidth (data, 0, 1);
68 }, "~S");
69 Clazz.defineMethod (c$, "stringWidth", 
70 function (str) {
71 var len = str.length;
72 var data =  Clazz.newCharArray (len, '\0');
73 str.getChars (0, len, data, 0);
74 return this.charsWidth (data, 0, len);
75 }, "~S");
76 Clazz.defineMethod (c$, "charsWidth", 
77 function (data, off, len) {
78 return this.stringWidth ( String.instantialize (data, off, len));
79 }, "~A,~N,~N");
80 Clazz.defineMethod (c$, "bytesWidth", 
81 function (data, off, len) {
82 return this.stringWidth ( String.instantialize (data, 0, off, len));
83 }, "~A,~N,~N");
84 Clazz.defineMethod (c$, "getWidths", 
85 function () {
86 var widths =  Clazz.newIntArray (256, 0);
87 for (var ch = String.fromCharCode (0); ch.charCodeAt (0) < 256; ch = String.fromCharCode (ch.charCodeAt (0) + 1)) {
88 widths[ch.charCodeAt (0)] = this.charWidth (ch);
89 }
90 return widths;
91 });
92 Clazz.overrideMethod (c$, "toString", 
93 function () {
94 return this.getClass ().getName () + "[font=" + this.getFont () + "ascent=" + this.getAscent () + ", descent=" + this.getDescent () + ", height=" + this.getHeight () + "]";
95 });
96 c$.DEFAULT_FRC = c$.prototype.DEFAULT_FRC =  new java.awt.font.FontRenderContext (null, false, false);
97 });