in reply to How can i add a font file in Perl Tk?

G'day Muskovitz,

I see you've already received good advice[1,2] from ++Discipulus regarding this.

There are a number of potential issues: how many platforms are involved? how are you planning to distribute? can your users install fonts (not just physically do it, but also allowed by the organisation to do it)? and so on. Unfortunately, none of that information is available to me.

Here's an alternative way that may suit your needs. Let's say the font family is "Special Sans", and the file is special_sans.ttf; and noting that Tk guarantees to support a sans-serif font family named "Helvetica".

Firstly, distribute special_sans.ttf with your GUI, and recommend its installation in INSTALL (or equivalent file), stating that it's not obligatory.

Then, near the start of your GUI code, after you've created your MainWindow, do something along these lines:

my @preferred_sans_families = ( 'Special Sans', 'DejaVu Sans', 'Verdana', 'Helvetica', ); my %all_families = map { $_ => 1 } $mw->fontNames; my $found_sans_family = first { $all_families{$_} } @preferred_sans_fa +milies; my $sans_font = $mw->fontCreate(... -family => $found_sans_family ...) +;

[first is from List::Util]

If a user manages to install special_sans.ttf, then "Special Sans" should be in $sans_font. If not, then the next best option from @preferred_sans_families is used. Aim to put a selection of font families from the target platforms in that list; put one of the guaranteed families last.

See Tk::Font for more details on those "font*" methods and other related information.

— Ken