Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2011-06

How to I publish the SearchFont method of TFontManager?

12 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

silvioprog

Hi.

The method of SearchFont is declared in protected session. So, how to I use this method in other classes?

Thx.

Silvio Cl?cio =============================================== Blog - <silvioprog.com.br> Twitter - <twitter.com/silvioprog> LazSolutions - <code.google.com/p/lazsolutions> LazWebSolutions - <code.google.com/p/lazwebsolutions> Lazarus-BR - <groups.google.com.br/group/lazarus-br?hl=pt-BR> =============================================== ? ?* Conhe?a nosso canal IRC sobre Lazarus: #lazarus-br * ===============================================

Reply by email

Ludo Brands
to silvioprog

Protected members can be called from a derived class.

Ludo

Show quoted text 32 lines
-----Message d'origine----- De : silvioprog [mailto:] Envoy? : lundi 20 juin 2011 19:51 ? : Lazarus mailing list Objet : [Lazarus] How to I publish the SearchFont method of TFontManager? Hi. The method of SearchFont is declared in protected session. So, how to I use this method in other classes? Thx. -- Silvio Cl?cio =============================================== Blog - <silvioprog.com.br> Twitter - <twitter.com/silvioprog> LazSolutions - <code.google.com/p/lazsolutions> LazWebSolutions - <code.google.com/p/lazwebsolutions> Lazarus-BR - <groups.google.com.br/group/lazarus-br?hl=pt-BR> =============================================== ? ?* Conhe?a nosso canal IRC sobre Lazarus: #lazarus-br * =============================================== -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

silvioprog
to Ludo Brands: Protected members can be called from a derived class. Ludo

Thanks guys.

So, I did not want to re-implement a class only to verifier if one font exists. See my code below:

(...)
  VFreeTypeFont: TFreeTypeFont;
  VFPImageCanvas: TFPImageCanvas;
begin
  VFreeTypeFont := TFreeTypeFont.Create;
  VFPImageCanvas := TFPImageCanvas.Create(AImage);
  try
    VFreeTypeFont.Name := AFontName;
    VFreeTypeFont.Size := AFontSize;
    VFreeTypeFont.FPColor := AFontColor;

{ How to I check if my .ttf file exists without hack or re-implementation of a class? }

    VFPImageCanvas.Font := VFreeTypeFont; <<-- if TTF font not exists

I receive an AV here.
(...)

Silvio Cl?cio =============================================== Blog - <silvioprog.com.br> Twitter - <twitter.com/silvioprog> LazSolutions - <code.google.com/p/lazsolutions> LazWebSolutions - <code.google.com/p/lazwebsolutions> Lazarus-BR - <groups.google.com.br/group/lazarus-br?hl=pt-BR> =============================================== ? ?* Conhe?a nosso canal IRC sobre Lazarus: #lazarus-br * ===============================================

Reply by email

Alberto Narduzzi
to silvioprog
VFPImageCanvas.Font := VFreeTypeFont;<<-- if TTF font not exists I receive an AV here.

you cannot receive an AV just for assigning a (eventually...) NIL value to a var; as NIL, although of little use, _is_ a valid value.

You may want to check the result value of your font after loading it (I have no clue, thou', about how you achieve this), and only then take proper action (i.e. default to some other font...).

Cheers, A.

Reply by email

silvioprog
to Alberto Narduzzi

2011/6/20 Alberto Narduzzi :

Show quoted text 11 lines
> ? ? VFPImageCanvas.Font := VFreeTypeFont;<<-- if TTF font not exists > I receive an AV here. you cannot receive an AV just for assigning a (eventually...) NIL value to a var; as NIL, although of little use, _is_ a valid value. You may want to check the result value of your font after loading it (I have no clue, thou', about how you achieve this), and only then take proper action (i.e. default to some other font...). Cheers, A.

There is no way to verify that TFreeTypeFont found the font.

Another code that not works:

(...)
  VFreeTypeFont: TFreeTypeFont;
  VFPImageCanvas: TFPImageCanvas;
begin
  VFreeTypeFont := TFreeTypeFont.Create;
  VFPImageCanvas := TFPImageCanvas.Create(AImage);
  try
    VFreeTypeFont.Name := AFontName;
    VFreeTypeFont.Size := AFontSize;
    VFreeTypeFont.FPColor := AFontColor;
    if not Assigned(VFreeTypeFont) then
      raise Exception.Create('My custom message');
    VFPImageCanvas.Font := VFreeTypeFont;
(...)

:(

Silvio Cl?cio =============================================== Blog - <silvioprog.com.br> Twitter - <twitter.com/silvioprog> LazSolutions - <code.google.com/p/lazsolutions> LazWebSolutions - <code.google.com/p/lazwebsolutions> Lazarus-BR - <groups.google.com.br/group/lazarus-br?hl=pt-BR> =============================================== ? ?* Conhe?a nosso canal IRC sobre Lazarus: #lazarus-br * ===============================================

Reply by email

Flávio Etrusco
to Alberto Narduzzi: you cannot receive an AV just for assigning a (eventually...) NIL value to a var; as NIL,…
Alberto Narduzzi wrote 5 lines
> ? ? VFPImageCanvas.Font := VFreeTypeFont;<<-- if TTF font not exists > I receive an AV here. you cannot receive an AV just for assigning a (eventually...) NIL value to a var; as NIL, although of little use, _is_ a valid value.

True, but the exception can _appear_ to be raised there since 'Font' is a property and has a setter, and we usually don't use FPC units compiled with debugging info.

Show quoted text 7 lines
You may want to check the result value of your font after loading it (I have no clue, thou', about how you achieve this), and only then take proper action (i.e. default to some other font...). Cheers, A.

TFreeTypeFont constructor doesn't receive a font name, so...

I don't have freetype installed right now (I'm on Windows) so I can't test, but maybe you could try font.AllocateResources or FontMgr.RequestFont.

Best regards,
Fl?vio

Reply by email

Hans-Peter Diettrich
to silvioprog: 2011/6/20 Alberto Narduzzi : There is no way to verify that TFreeTypeFont found the font.…
Show quoted text 10 lines
Another code that not works: (...) VFreeTypeFont: TFreeTypeFont; VFPImageCanvas: TFPImageCanvas; begin VFreeTypeFont := TFreeTypeFont.Create; VFPImageCanvas := TFPImageCanvas.Create(AImage); try VFreeTypeFont.Name := AFontName;

If VFreeTypeFont were Nil, the AV would occur here!

VFreeTypeFont.Size := AFontSize; VFreeTypeFont.FPColor := AFontColor; if not Assigned(VFreeTypeFont) then raise Exception.Create('My custom message');

The following is the really critical statement, where the platform fontmanager is asked to select the (closest) installed font, and assign it to the canvas:

VFPImageCanvas.Font := VFreeTypeFont;

Are you sure that VFPImageCanvas is not Nil???

When it definitely is the font, and not the canvas, then I'd try-except only *this* statement, and when it fails modify the FontName until no exception occurs.

When it is the font name, there should exist means to enumerate the installed fonts. At least the WinAPI allows to ask for the name of the closest font, based on general font attributes (w/o serifes...). Dunno what LCL/RTL offers for that purpose...

I also don't know how e.g. Win32 deals with *FreeType* fonts, in Delphi I've been working with *TrueType* fonts only.

DoDi

Reply by email

silvioprog
to Flávio Etrusco: On Mon, Jun 20, 2011 at 5:21 PM, Alberto Narduzzi True, but the exception can _appear_ to…

Em 20 de junho de 2011 19:39, Fl?vio Etrusco
escreveu:
[...]

Show quoted text 5 lines
TFreeTypeFont constructor doesn't receive a font name, so... I don't have freetype installed right now (I'm on Windows) so I can't test, but maybe you could try font.AllocateResources or FontMgr.RequestFont.

I tested this early on, but not work too.

I solved the problem, please see:

function LSFontExists(const AFileName: TFileName): Boolean;
{$IFDEF UNIX}
const
  VFontPath = '/usr/share/fonts';
{$ENDIF}
begin
  Result := FileExists(AFileName) or
{$IFDEF UNIX}
   (FileExists(VFontPath + AFileName)) or
   (FileExists(VFontPath + '/truetype/' + AFileName)) or
   (FileExists(VFontPath + '/truetype/freefont/')) ;
{$ENDIF}
{$IFDEF MSWINDOWS}
    FileExists(IncludeTrailingBackslash(GetEnvironmentVariable('windir')) +
      'Fonts' + DirectorySeparator + AFileName);
{$ENDIF}
end;
(...)
  VFreeTypeFont: TFreeTypeFont;
  VFPImageCanvas: TFPImageCanvas;
begin
  VFreeTypeFont := TFreeTypeFont.Create;
  VFPImageCanvas := TFPImageCanvas.Create(AImage);
  AImage.SetSize(80, 20);
  try
    VFreeTypeFont.Name := AFontName;
    VFreeTypeFont.Size := AFontSize;
    VFreeTypeFont.FPColor := AFontColor;
    if not function LSFontExists(ftLiberationSerifRegularTTF) then
      raise Exception.Create(Format(sErrFontFileNotFound,
        [ftLiberationSerifRegularTTF]));
      raise
    VFPImageCanvas.Font := VFreeTypeFont;
(...)
Best regards, Fl?vio
Silvio Cl?cio =============================================== Blog - <silvioprog.com.br> Twitter - <twitter.com/silvioprog> LazSolutions - <code.google.com/p/lazsolutions> LazWebSolutions - <code.google.com/p/lazwebsolutions> Lazarus-BR - <groups.google.com.br/group/lazarus-br?hl=pt-BR> =============================================== ? ?* Conhe?a nosso canal IRC sobre Lazarus: #lazarus-br * ===============================================

Reply by email

silvioprog
to Hans-Peter Diettrich: If VFreeTypeFont were Nil, the AV would occur here! The following is the really critical…

2011/6/21 Hans-Peter Diettrich :
[]

Are you sure that VFPImageCanvas is not Nil???

Yes, it's normal.

When it definitely is the font, and not the canvas, then I'd try-except only *this* statement, and when it fails modify the FontName until no exception occurs.

I solved the problem
(http://lists.lazarus.freepascal.org/pipermail/lazarus/2011-June/064239.html).

Thanks DoDi and all,

DoDi
Silvio Cl?cio =============================================== Blog - <silvioprog.com.br> Twitter - <twitter.com/silvioprog> LazSolutions - <code.google.com/p/lazsolutions> LazWebSolutions - <code.google.com/p/lazwebsolutions> Lazarus-BR - <groups.google.com.br/group/lazarus-br?hl=pt-BR> =============================================== ? ?* Conhe?a nosso canal IRC sobre Lazarus: #lazarus-br * ===============================================

Reply by email

Graeme Geldenhuys
to silvioprog: Em 20 de junho de 2011 19:39, Fl?vio Etrusco [...] I tested this early on, but not work…
silvioprog wrote 12 lines
function LSFontExists(const AFileName: TFileName): Boolean; {$IFDEF UNIX} const VFontPath = '/usr/share/fonts'; {$ENDIF} begin Result := FileExists(AFileName) or {$IFDEF UNIX} (FileExists(VFontPath + AFileName)) or (FileExists(VFontPath + '/truetype/' + AFileName)) or (FileExists(VFontPath + '/truetype/freefont/')) ; {$ENDIF}

Maybe one should add ~/.fonts/ to the search path as well? For example, I often have custom fonts installed in my home directory, which are not accessible to other users on the system.

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/

Reply by email

silvioprog
to Graeme Geldenhuys

2011/6/21 Graeme Geldenhuys :

Show quoted text 25 lines
On 21/06/2011 12:30, silvioprog wrote: > function LSFontExists(const AFileName: TFileName): Boolean; > {$IFDEF UNIX} > const > ? VFontPath = '/usr/share/fonts'; > {$ENDIF} > begin > ? Result := FileExists(AFileName) or > {$IFDEF UNIX} > ? ?(FileExists(VFontPath + AFileName)) or > ? ?(FileExists(VFontPath + '/truetype/' + AFileName)) or > ? ?(FileExists(VFontPath + '/truetype/freefont/')) ; > {$ENDIF} Maybe one should add ?~/.fonts/ to the search path as well? For example, I often have custom fonts installed in my home directory, which are not accessible to other users on the system. Regards, ?- Graeme - -- fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/

Good idea. I'll implement it.

Thx Graeme. ;)

Silvio Cl?cio =============================================== Blog - <silvioprog.com.br> Twitter - <twitter.com/silvioprog> LazSolutions - <code.google.com/p/lazsolutions> LazWebSolutions - <code.google.com/p/lazwebsolutions> Lazarus-BR - <groups.google.com.br/group/lazarus-br?hl=pt-BR> =============================================== ? ?* Conhe?a nosso canal IRC sobre Lazarus: #lazarus-br * ===============================================

Reply by email