[Lazarus] Fonts

Duncan Parsons D.Parsons at seamsltd.com
Wed Jul 21 10:54:52 CEST 2010


From: Mark Morgan Lloyd [mailto:markMLl.lazarus at telemetry.co.uk] 
Sent: 21 July 2010 08:52

>Kjow wrote:
>> Hi all,
>> 
>> I'm developing a multiplatform app and I need to have the same look
on
>> all OS. Is it possible to include a my personal font in the Lazarus 
>> compiled binary to have the same graphic/size/etc on all OS?
>
>Since nobody else has commented: I've done this with Delphi in Windows,
embedding a (bitmap)
>font as a resource. Quite frankly I didn't find it entirely
satisfactory since if for some
>reason the OS though that the binary was locked it couldn't access the
resource and fell back >to a "close equivalent".
>
>I'm not sure that the same can be done in a portable fashion because of
the X/unix case where >the binary and the fonts are potentially stored
on different hosts. It /might/ be possible to >handle that particular
case by making the app look like an X font server, but that would
>clearly fail if there was already one set up.
>
>-- 
>Mark Morgan Lloyd
>markMLl .AT. telemetry.co .DOT. uk
>
>[Opinions above are the author's, not those of his employers or
colleagues]

The way I've done it in the past with Delphi, is to have a TrueType font
in the resources and something along these lines

=-=-=-=-=-=
const
  cFont:array[0..0,0..2] of string=
             (('tmp\','SciCalc','scicalc2_small.ttf'));

function LoadFont(FontIdx:Integer; Load:boolean):boolean;
var fnt,FontRes : string;
    Res : TResourceStream;
begin
  result:=false; //assume it all went wrong...
  fnt:=SettingsRootDir+cFont[FontIdx,0]+cFont[FontIdx,2];

  if Load then
    begin
      if not(FileExists(fnt)) then
        begin
          try
            Res :=TResourceStream.Create(Hinstance,
                                           cFont[FontIdx,1],
                                           RT_RCDATA);
            Res.SaveToFile(fnt);
            Res.Free;
          except
          end;
        end;

      if FileExists(fnt) then
        begin
          FontRes:=fnt+#0;
          result:=(AddFontResource(@FontRes[1])<>0);
          if result then PostMessage(HWND_BROADCAST,WM_FONTCHANGE,0,0);
        end;
    end
   else
    begin
      FontRes:=fnt+#0;
      result:=RemoveFontResource(@FontRes[1]);
      if result then PostMessage(HWND_BROADCAST,WM_FONTCHANGE,0,0);
	//Could Delete file is wanted :shrug: depends on how app was
installed
    end;
end;

=-=-=-=-=-=

Now, this is very Windows centric, but the idea is there. Windows cannot
load a font from memory - it /has/ to be a file, hence the persisting
part. Something XPlatform along these lines would be super, but alas I'm
not the man to do it. However this code posted here is up for grabs as a
working windows implementation for whoever wants to use it.

DSP




More information about the Lazarus mailing list