FontWidthEstimatorBuilder.frink

View or download FontWidthEstimatorBuilder.frink in plain text format


// This program builds character width tables for use in estimating
// the length of a given string.  It is used to build the
// frink.graphics.FontWidthEstimator tables.

bi = newJava["java.awt.image.BufferedImage", [100, 100, staticJava["java.awt.image.BufferedImage", "TYPE_INT_ARGB"]]]
g2 = bi.createGraphics[]
frc = g2.getFontRenderContext[]

fontnames = ["Serif", "SansSerif"] //, "Monospaced"]
rep = 40

min = 0
max = 0x1400

sum = new array
maxA = new array
for i=min to max
{
   sum@i = 0
   maxA@i = 0
}

for fn = fontnames
{
   for size = 5 to 40
   {
      f = newJava["java.awt.Font", [fn, 3, size]]
      for c = min to max
      {
         str = repeat[char[c], rep]
         rect = f.getStringBounds[str, frc]
         width = rect.width
         normalized = width / size / rep
         sum@c = sum@c + normalized
         if normalized > maxA@c
            maxA@c = normalized
//         println[char[c] + "\t" + normalized]
      }
   }
}

for i = min to max
{
   print[format[maxA@i,1,4]+ "f /* \\u" + padLeft[hex[i], 4, "0"]]
   if i>=32 and i<=127
      print[" " + char[i]]
   print[" */
, "]
   if (i % 3 == 2)
      println[]
}


View or download FontWidthEstimatorBuilder.frink in plain text format


This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen was born 17592 days, 15 hours, 46 minutes ago.