Muskovitz has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I am actually programming a GUI in Perl Tk but i am stuck at adding a new font in Perl Tk i actually tried searching on the internet but i get nothing.

What i wanted:
Is i want to include my downloaded font(.ttf file extension) in Perl Tk so it would match the design of my GUI program.
Thanks in advance.

Replies are listed 'Best First'.
Re: How can i add a font file in Perl Tk?
by Discipulus (Canon) on Jul 27, 2017 at 09:58 UTC
    Hello Muskovitz,

    I think the font file you downoladed is a system thing not a Tk one: I mean that, once you installed the font on your system you can use it in your Tk program.

    The font name becomes the family in the Tk terminology. In the following example you can see a code named font ( single name that refers to a combination of family, size..) using the courier family already installed on the system.

    my $code_font = $mw->fontCreate('code', -family => 'courier', -size => + 14);

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      Hey, thanks for the reply! :)
      Yes. I actually thought of that installing the Font into the system but what i am trying to do is I'll pack my GUI program into standalone executable file 'BUT' if there's no way to do what i need. I guess I'll just follow your advice. Thanks again.
        Ah! this is another story..

        I think it can be accomplished too, even if more complicated. You can build your executable including the font file, then you can start your program (maybe a BEGIN block?) checking if the font is present.

        If the font is not already present you can automate the installation. In this case you must be aware of different font installation way depending on the OS the program is running on checking $^O variable.

        UPDATE if you do not want automate the font installation you can have a default one set at top of the program, before checking the existence of the font on the system, if not present or failed font installation your program will use the default one. This seems wise.

        So you'll have font options specified with  -font => [$font_name, 14]

        L*

        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: How can i add a font file in Perl Tk?
by kcott (Archbishop) on Jul 28, 2017 at 07:06 UTC

    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

Re: How can i add a font file in Perl Tk?
by beech (Parson) on Jul 28, 2017 at 04:00 UTC