java - The maximum digit width on different font and font size -


i need maximum width of 1,2,3,....8,9 on different font , font size.

in c#, open source code is:

    protected double maxdigitwidth(string fontname, double dblsizept)     {         double dblmaxdigitwidth = 0.0;          // excel not use specified font size,          // instead, font size rounded integral pixel size         // check if works on non-windows platforms, otherwise 96dpi fine here       //  graphics g = system.drawing.graphics.fromhwnd(new intptr());         float fsizepxl = (float)math.round(96 * dblsizept / 72);         float fsizept = (72 * fsizepxl / 96);         fontstyle fontstyle;         if (fontname == "monotype corsiva")             fontstyle = fontstyle.italic;         else             fontstyle = fontstyle.regular;          font font = new font(fontname, fsizept, fontstyle, system.drawing.graphicsunit.point, ((byte)(0)));         (int = 0; < 10; i++)         {             // use label on .net 2.0 form measure width of digit             form f = new form();             label l = new label();             l.usecompatibletextrendering = false;             l.font = font;             l.autosize = true;             f.controls.add(l);             l.text = convert.tostring(i) + convert.tostring(i);             f.resumelayout(false);             f.performlayout();             int iwidth2 = l.width;              l.text = convert.tostring(i);             f.performlayout();              // measure twice can remove padding             int iwidth1 = l.width;              if (iwidth2 - iwidth1 > dblmaxdigitwidth)             {                 dblmaxdigitwidth = iwidth2 - iwidth1;             }         }         return dblmaxdigitwidth;      } 

i implemented in java. results different.

protected double maxdigitwidth(string fontname, double dblsizept) {     double dblmaxdigitwidth = 0.0;     font f;     if (fontname == "monotype corsiva")         f = new font(fontname, font.italic ,(int)dblsizept);     else         f = new font(fontname, font.plain ,(int)dblsizept);     jcomponent t = new jlabel();     fontmetrics fm = t.getfontmetrics(f);     (int = 0; < 10; i++)     {             int iwidth2 = fm.stringwidth(string.valueof(i)+string.valueof(i));      int iwidth1 = fm.stringwidth(string.valueof(i));       if (iwidth2 - iwidth1 > dblmaxdigitwidth)      {          dblmaxdigitwidth = iwidth2 - iwidth1;      }     }      return dblmaxdigitwidth;  } 

but result different betweent c# , java.(i run them on same machine.)

  1. the result of c#

          font , font size                maximum digit widht      = maxdigitwidth("arial", 11.0);    8     = maxdigitwidth("arial", 12.0);    9      = maxdigitwidth("arial", 13.0);    9      = maxdigitwidth("arial", 14.0);    11      = maxdigitwidth("arial", 15.0);    11      = maxdigitwidth("arial", 16.0);    12      = maxdigitwidth("arial", 17.0);    13      = maxdigitwidth("arial", 18.0);    13        = maxdigitwidth("arial", 19.0);    14        = maxdigitwidth("arial", 20.0);    15    
  2. the result of java

        arial-11           6.0     arial-12           7.0     arial-13           7.0     arial-14           8.0     arial-15           8.0     arial-16           9.0     arial-17           9.0     arial-18           10.0     arial-19           11.0     arial-20           11.0 

the difference between .net , java due different assumptions screen resolution. .net works 96dpi, java 72dpi. if ask java 12pt font have different size on screen if ask .net same font. on paper, same.

if convert results mm (or inch), you'll find same except lost precision due rounding.

i've verified numbers printing rows of digits microsoft word , measuring them. such reality check helps.

you're way of measuring text length in .net pretty funny. why not this:

using system; using system.drawing;  namespace codo {     class program     {         static void main(string[] args)         {             bitmap bitmap = new bitmap(100, 100);             graphics graphics = graphics.fromimage(bitmap);             graphics.pageunit = graphicsunit.pixel;             stringformat strformat = new stringformat(stringformat.generictypographic);             (int fontsize = 11; fontsize <= 20; fontsize++)             {                 font font = new font("arial", fontsize, fontstyle.regular);                 float digitwidth = graphics.measurestring("00000000", font, 1000, strformat).width / 8;                 font.dispose();                 console.writeline("width of digit 0 {0} {1}pt: {2} (96dpi)", font.fontfamily.name, font.sizeinpoints, digitwidth);                 digitwidth = digitwidth / 96 * 72;                 console.writeline("width of digit 0 {0} {1}pt: {2} (72dpi)", font.fontfamily.name, font.sizeinpoints, digitwidth);             }             graphics.dispose();             bitmap.dispose();         }     } } 

Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -