Find mono spaced fonts

SeS 4th Nov 2015

Following is created to find all mono spaced font types in a given OS. This can be handy in case of source code editors, their fonts often preferred to be mono spaced. An example how it is used, see Another Font Chooser Dialog.

# ---------------------------------------------------------------------
# Purpose    : extract mono spaced font names within OS system
proc findFontClass {{type mono}} {
  foreach f [font families] {
    if {[font measure "{$f} 8" "A"]==[font measure "{$f} 8" "."]} {lappend fm $f} {lappend fv $f}
  }
  if {"$type"=="mono"} {return $fm} {return $fv}
}

This proc is now part of generic_tcl.tcl library of tcl-functions, in tG² v1.09.01 - beta 2. Tested within a wish.exe console v8.4.19.


kpv: I believe that you can just query the font metrics to determine if the font is fixed width: font metrics font_name -fixed


SeS Thank you kpv! I will adapt the little proc, using the apparently already available method. I thought I had a good look to the available options in the user manual, missed it I guess.


bogdan 2022-06-25 This is my variant with caching feature. On my system, measuring procedure duration is ~35s with 100% CPU load. If I use this procedure more than one time, than I need to speeding up this process. This variant use previous results of measuring.

# ---------------------------------------------------------------------
# Purpose    : extract mono spaced font names within OS system
proc findFontClass {{type mono} {recache 0}} {
  variable fm
  variable fv
  if {$recache != 0 || ![info exists fm] || ![info exists fv]} {
    set fm {}; set fv {}
    foreach f [font families] {
      if {$f in $fm || $f in $fv} continue
      if {[font measure "{$f} 8" "A"] == [font measure "{$f} 8" "."]} {lappend fm $f} {lappend fv $f};
    }
    update; # This unfreeze Tk window
  };
  if {$type eq "mono"} {return $fm} {return $fv}
}