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


In reply to Re: How can i add a font file in Perl Tk? by kcott
in thread How can i add a font file in Perl Tk? by Muskovitz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.