Recent

Author Topic: AddFile(FileName) using a buffer instead of a file?  (Read 418 times)

dieselnutjob

  • Full Member
  • ***
  • Posts: 141
AddFile(FileName) using a buffer instead of a file?
« 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)?

dieselnutjob

  • Full Member
  • ***
  • Posts: 141
Re: AddFile(FileName) using a buffer instead of a file?
« Reply #1 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
« Last Edit: October 19, 2019, 07:24:46 pm by dieselnutjob »

dieselnutjob

  • Full Member
  • ***
  • Posts: 141
Re: AddFile(FileName) using a buffer instead of a file?
« Reply #2 on: October 19, 2019, 07:48:53 pm »

Handoko

  • Hero Member
  • *****
  • Posts: 3182
  • My goal: build my own game engine using Lazarus
Re: AddFile(FileName) using a buffer instead of a file?
« Reply #3 on: October 19, 2019, 07:53:34 pm »
I believe it should be TFileStream:
https://www.freepascal.org/docs-html/rtl/classes/tfilestream.html

It can be TMemoryStream too, if you know how to use it.

Thaddy

  • Hero Member
  • *****
  • Posts: 9142
Re: AddFile(FileName) using a buffer instead of a file?
« Reply #4 on: October 19, 2019, 08:35:18 pm »
So basically TStream will do perfectly fine. THandlestream is a suggestion...
also related to equus asinus.

dieselnutjob

  • Full Member
  • ***
  • Posts: 141
Re: AddFile(FileName) using a buffer instead of a file?
« Reply #5 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