Lazarus

Programming => General => Topic started by: dieselnutjob on October 19, 2019, 07:15:44 pm

Title: AddFile(FileName) using a buffer instead of a file?
Post by: dieselnutjob on October 19, 2019, 07:15:44 pm
Hi,
I am playing with \lazarus\examples\lazfreetype\lazfreetypetest.lpi

within mainform.pas there is this function

Code: Pascal  [Select][+][-]
  1. FontFamilyName := FontCollection.AddFile(FileName).Family.FamilyName;

but instead of loading the .ttf font fine from disc I would like to have it inside the source code.
I was thinking of something like this
Code: Pascal  [Select][+][-]
  1. block: packed array [0..139511] of byte=
  2. ($00,$01,$00,$00,$00,$13,$01,$00,$00,$04,   .....  

where the contents of the array are a hex dump of the .ttf file.

Is there a way to do this (instead of resource files I guess)?
Title: Re: AddFile(FileName) using a buffer instead of a file?
Post by: dieselnutjob on October 19, 2019, 07:19:57 pm
Note that I had a look in the definition of FontCollection and got this

Code: Pascal  [Select][+][-]
  1. TCustomFreeTypeFontCollection = class
  2.   protected
  3.     function GetFont(AFileName: string): TCustomFontCollectionItem; virtual; abstract;
  4.     function GetFamily(AName: string): TCustomFamilyCollectionItem; virtual; abstract;
  5.     function GetFamilyCount: integer; virtual; abstract;
  6.     function GetFontCount: integer; virtual; abstract;
  7.   public
  8.     constructor Create; virtual; abstract;
  9.     procedure Clear; virtual; abstract;
  10.     procedure BeginUpdate; virtual; abstract;
  11.     procedure AddFolder(AFolder: string; AIncludeSubdirs: Boolean = false); virtual; abstract;
  12.     procedure RemoveFolder(AFolder: string); virtual; abstract;
  13.     function AddFile(AFilename: string): TCustomFontCollectionItem; virtual; abstract;
  14.     function RemoveFile(AFilename: string): boolean; virtual; abstract;
  15.     function AddStream(AStream: TStream; AOwned: boolean): boolean; virtual; abstract;
  16.     procedure EndUpdate; virtual; abstract;
  17.     function FontFileEnumerator: IFreeTypeFontEnumerator; virtual; abstract;
  18.     function FamilyEnumerator: IFreeTypeFamilyEnumerator; virtual; abstract;
  19.     property FontFileCount: integer read GetFontCount;
  20.     property FontFile[AFileName: string]: TCustomFontCollectionItem read GetFont;
  21.     property FamilyCount: integer read GetFamilyCount;
  22.     property Family[AName: string]: TCustomFamilyCollectionItem read GetFamily;
  23.   end;              

I notice that there is an AddStream alternative to AddFile, but I can't make it work.

It compile but when I run it I get:-
raised exception class 'EStreamError' with message
Reading from TStream is not supported
Title: Re: AddFile(FileName) using a buffer instead of a file?
Post by: dieselnutjob on October 19, 2019, 07:48:53 pm
I guess that TByteStream is the answer?

https://www.freepascal.org/docs-html/rtl/classes/tbytesstream.html (https://www.freepascal.org/docs-html/rtl/classes/tbytesstream.html)
Title: Re: AddFile(FileName) using a buffer instead of a file?
Post by: Handoko on October 19, 2019, 07:53:34 pm
I believe it should be TFileStream:
https://www.freepascal.org/docs-html/rtl/classes/tfilestream.html (https://www.freepascal.org/docs-html/rtl/classes/tfilestream.html)

It can be TMemoryStream too, if you know how to use it.
Title: Re: AddFile(FileName) using a buffer instead of a file?
Post by: Thaddy on October 19, 2019, 08:35:18 pm
So basically TStream will do perfectly fine. THandlestream is a suggestion...
Title: Re: AddFile(FileName) using a buffer instead of a file?
Post by: dieselnutjob on October 20, 2019, 01:19:14 am
Thanks folks. I got it working.

You can see the code here http://www.christiantena.net/programming/lazfreetype-dnj-mod1.zip (http://www.christiantena.net/programming/lazfreetype-dnj-mod1.zip)
TinyPortal © 2005-2018