Lazarus

Programming => LCL => Topic started by: ϻαϻɾΣɀО on June 01, 2019, 10:02:44 pm

Title: TCustomFreeTypeFontCollection Enumerator
Post by: ϻαϻɾΣɀО on June 01, 2019, 10:02:44 pm
Hi Guys,
For the TCustomFreeTypeFontCollection there are two enumerators:
Code: Pascal  [Select]
  1.    
  2.     function FontFileEnumerator: IFreeTypeFontEnumerator; virtual; abstract;
  3.     function FamilyEnumerator: IFreeTypeFamilyEnumerator; virtual; abstract;
  4.  

But I cannot figure out how to use them!
Can someone help me?
Title: Re: TCustomFreeTypeFontCollection Enumerator
Post by: ϻαϻɾΣɀО on June 01, 2019, 10:51:04 pm
I found something like this and it works:
Code: Pascal  [Select]
  1. function GetFontFamilies: TStringList;
  2. var
  3.   enm: IFreeTypeFamilyEnumerator;
  4. begin
  5.     Result := TStringList.Create;
  6.     enm := FontCollection.FamilyEnumerator;
  7.     while enm.MoveNext do
  8.       Result.Add(enm.Current.FamilyName);
  9. end;
  10.  
any beter suggestion?
I mean something like
Code: Pascal  [Select]
  1.  for family in FontCollection do...
Title: Re: TCustomFreeTypeFontCollection Enumerator
Post by: jamie on June 02, 2019, 12:27:28 am
Yes you fingered it out  :)

You can even write your enumerators in your classes that you make, all you need is the GetEnumerator function

That allows the loop counter using "IN" to function.


http://wiki.freepascal.org/for-in_loop
Title: Re: TCustomFreeTypeFontCollection Enumerator
Post by: devEric69 on June 02, 2019, 09:53:12 am
Nice insight.
And just as a reminder, for those who are "paranoid" and want to free the memory by themselves, once the interface is used (without letting the interfaces's _refcount do its job as a "garbage collector"), you must assign the interface's variable with nil to free it, like:

Code: Pascal  [Select]
  1. GetFontFamilies function: TStringList;
  2. begin
  3.         .../...
  4.         enm:= nil;  //free the intrfc expressly
  5. end;