java - Monospace and UTF8 -
i correcting font display in 1 of our old java swing application. changed after using "-dfile.encoding=utf-8" option @ run time.
fontclass.jar sample jar file.
option 1: javaw -xmx256m -jar -dfile.encoding=utf-8 "fontclass.jar" option 2: javaw -xmx256m -jar "fontclass.jar"
with option 1: monospaced display
with option 2: non monospaced display.
limitation:
i) -dfile.encoding=utf-8 option have added have our streams support utf-8 support. insteading of modifing each streams manually.
ii) using java swing frame work , in many placeses. uisng default font. changing lot of work.
so question there command line option using can monospace display option 1:
below sample code:
class show extends frame { fontmetrics fontm; string outstring; show(string target, string title, font font) { settitle(title); outstring = target; fontm = getfontmetrics(font); setfont(font); int size = 0; (int = 0; < outstring.length(); i++) { size += fontm.charwidth(outstring.charat(i)); } size += 24; setsize(size, fontm.getheight() + 60); setlocation(getsize().width / 2, getsize().height / 2); setvisible(true); } public void paint(graphics g) { insets insets = getinsets(); int x = insets.left; int y = insets.top; g.drawstring(outstring, x + 6, y + fontm.getascent() + 14); } } public class fontclass { public static void main(string[] args) { string jastring = new string("\u65e5\u672c\u8a9e\u6587\u5b57\u5217"); string inputstring = "\niiii\naaaaiiii"; string displaystring = jastring + " " + inputstring; font font; font = new font("courier",3, 24); new show(displaystring, "demo",font); } }
from fontmetrics
api, defines advance in terms of charwidth()
, "note advance of string
not sum of advances of characters." moreover, font named "courier"
may monospaced, may not same font specified font.monospaced
.
instead, use textlayout
derive required geometry, shown here, here , here.
Comments
Post a Comment