Friday 7 January 2011

Fonts: Find font files by font family name

Sometime you want to use a piece of software or a library, such as  Imager Graph are not linked to the fontconfig lib. Because of that, you can't specify fonts by simply using their family name (Like 'FreeSerif' for instance).

So you need a way to resolve a font's family name to the full path of the font files on disk. Here's how to do it for the FreeSerif one:


$ fc-list -f "%{family} %{style[0]} %{file}\n" 'FreeSerif'
FreeSerif Bold /usr/share/fonts/truetype/freefont/FreeSerifBold.ttf
FreeSerif Medium /usr/share/fonts/truetype/freefont/FreeSerif.ttf
FreeSerif Italic /usr/share/fonts/truetype/freefont/FreeSerifItalic.ttf
FreeSerif BoldItalic /usr/share/fonts/truetype/freefont/FreeSerifBoldItalic.ttf

Maybe one day there will be a Perl module bound to the libfontconfig. Until there, a simple system call will probably do for me :)

EDIT 31st Jan 2011:

There's no -f option in older systems, you can also use

$ fc-list  'FreeSerif' file
/usr/share/fonts/truetype/freefont/FreeSerifBoldItalic.ttf: 
/usr/share/fonts/truetype/freefont/FreeSerif.ttf: 
/usr/share/fonts/truetype/freefont/FreeSerifBold.ttf: 
/usr/share/fonts/truetype/freefont/FreeSerifItalic.ttf:

Thursday 6 January 2011

Perl: Avoiding the HTML::Mason/TemplateToolkit schism

As a Perl web developer, I've been using HTML::Mason intensely for many projects. Over the past year, I've also been working with Template Toolkit as a rendering layer for a web application.

As always, choosing between two similar technologies seems to be mainly a matter of taste and yet another occasion for an holy war. To pacify the situation, let's try to figure out what are these beasts good at.

TT feels more simple.

Consider the following code:

[% IF customer.is_in('GB') %]
We'll ship your order by Royal Mail
[% ELSE %]
Sorry [% customer.firstname %], delivery will take a while.
[% END %]

It's much easier to understand for a non-programmer than the Mason equivalent:

% if ( $customer->is_in('GB') ) {
We'll ship your order by Royal Mail
% } else {
Sorry <% $customer->firstname() %> ...
% }

So here clearly, TT wins, although as a programmer, and specially as a Perl programmer, Mason also feels just fine. However, a designer or a configuration person will feel more at home with TT, as it resembles infamous visual basic excel macros.

Mason simplifies page composition.

Let's implement a set of pages, each with a specific title and content, but with the same structure (here just a navigation box). In fact the common bread of web development

Let's do it in HTML::Mason:

autohandler:
<html>
 <head>
  <title><& SELF:title &></title>
 </head>
 <body>
  <div id="nav_box">...</div>
  <div id="payload">
% $m->call_next();
  </div>
 </body>
</html>
<%method title>A default title</%method>

content1.html:
  <p>content 1</p>
  <%method title>Page Content 1</%method>

content2.html
  <p>content 2</p>
  <%method title>Page Content 2</%method>

etc..

Now let's do it TT:

page:
<html>
 <head>
   <title>[% title || 'A default title' %]</title>
 </head>
 <body>
 <div id="nav_box">...</div>
 <div id="payload">
  [% content %]
 </div>
 </body>
</html>

content1.html:
[%WRAPPER page
          title = 'Page content 1'
%]
 <p>content 1</p>
[%END%]

content2.html:

[%WRAPPER page
          title = 'Page content 2'
%]
 <p>content 2</p>
[%END%]


From a programmer's point of view, HTML::Mason wins, as its inheritance mechanism allows much better simplicity, elegance and flexibility in page composition. In this example, I only considered a simple example that's possible to implement both in Mason and in TT, but for more advance use, the inheritance mechanisms (with call to PARENT (super) methods), and other Mason-isms largely beat the TT WRAPPER directive.


From these two simple examples, it appears that HTML::Mason has got much more to offer to programmers, as it allows them to use Perl natively, use a fairly complete inheritance mechanism, and much more. On the other hand, what TT has to offer is definitely an easier and simpler solution for simple templating jobs.

To sum up:

- Use Mason where you need elegance and simplicity to build complex things.

- Use TT where the 'easier than Perl' feel is important, with a restricted set of directive to build simple things.