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

Mailing lists / lazarus / 2011-06
12 messages · 6 people · –
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Hi.
The method of SearchFont is declared in protected session. So, how to I use this method in other classes?
Thx.
Protected members can be called from a derived class.
Ludo
Hi. The method of SearchFont is declared in protected session. So, how to I use this…
Hi. The method of SearchFont is declared in protected session. So, how to I use this method in other classes?
IIRC, this means that you can _still_ see it within your derived class...
Anyone orrect me if I'm wring on this one ;-)
Cheers, A.
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 existsI receive an AV here.
(...)
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.
2011/6/20 Alberto Narduzzi :
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;
(...):(
you cannot receive an AV just for assigning a (eventually...) NIL value to a var; as NIL,…
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.
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
2011/6/20 Alberto Narduzzi : There is no way to verify that TFreeTypeFont found the font.…
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
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:
[...]
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
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
Em 20 de junho de 2011 19:39, Fl?vio Etrusco [...] I tested this early on, but not work…
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 -
2011/6/21 Graeme Geldenhuys :
Good idea. I'll implement it.
Thx Graeme. ;)