Recent

Author Topic: [Solved] Linux - Any units for rendering font glyphs to memory?  (Read 822 times)

SuperSathanas

  • New Member
  • *
  • Posts: 16
With Delphi (on Windows of course), I would use the units from TextShaping4Delphi (https://github.com/projekter/TextShaping4Delphi), specifically the uFreeType unit, to render glyphs to memory buffers, which I could then use to build texture atlases for use with OpenGL for text rendering. There doesn't seem to be a suitable (or complete) alternative that I can use with Free Pascal or that works on Linux. I've tried to modifying uFreeType.pas to work with FPC on Linux, but I haven't been able to make it work.

I'm not using the form editor, and so I am not using components, and I don't want to have to do something like creating a TBitmap, TImage or similar, drawing text to a canvas, querying the canvas for the text extents, and then having to get the raw image data from the canvas to shove in an OpenGL texture.

The freetype and freetynehdyn units (and their includes) seem to be incomplete or written against an older version of FreeType, not including functions like FT_RENDER_GLYPH that I would use to actually render those glyphs to a memory buffer.

The other units/libraries I've checked out also don't offer what I'm looking for. They usually provide their own form of canvas or bitmap buffer and abstract/hide the actual text rendering functionality. I want to be able to either use FreeType directly, or otherwise have thin wrapper over it.

If there isn't something available, then I guess my next step is to either create my own uFreeType.pas equivalent that works with FPC on Linux, or otherwise create what I want as a shared library with C++ and then write the header translation, which means I'd have to learn how to go about creating those shared libraries.

Edit: I could use SFML or SDL2, which I know have working bindings for FPC, but I'm wanting to write my own text rendering code that goes along with my own windowing and input handling code.
« Last Edit: May 31, 2024, 04:36:17 pm by SuperSathanas »

TRon

  • Hero Member
  • *****
  • Posts: 2780
Re: Linux - Any units for rendering font glyphs to memory?
« Reply #1 on: May 27, 2024, 02:28:18 pm »
The freetype and freetynehdyn units (and their includes) seem to be incomplete or written against an older version of FreeType, not including functions like FT_RENDER_GLYPH that I would use to actually render those glyphs to a memory buffer.

The other units/libraries I've checked out also don't offer what I'm looking for. They usually provide their own form of canvas or bitmap buffer and abstract/hide the actual text rendering functionality. I want to be able to either use FreeType directly, or otherwise have thin wrapper over it.
Then add the function header. That is what I did to render glyphs to a PTC canvas. The existing freetype unit is part of another project and only the headers that where required for that package where implemented. And I agree that it is not ideal because of inconsistency in the translation(s) but it is do-able.

BTW: it takes less than 5 minutes to convert ufreetype unit you linked to into something that compiles for FPC. Most time is wasted removing all the  brainless Windows and Delphi only crap.
« Last Edit: May 27, 2024, 02:43:23 pm by TRon »

SuperSathanas

  • New Member
  • *
  • Posts: 16
Re: Linux - Any units for rendering font glyphs to memory?
« Reply #2 on: May 31, 2024, 04:35:59 am »
BTW: it takes less than 5 minutes to convert ufreetype unit you linked to into something that compiles for FPC. Most time is wasted removing all the  brainless Windows and Delphi only crap.

I tried replacing all the "[Ref]  Const" with "constref" like it says at the top of the unit, I had to rename all the record fields named "Generic", and then I changed the FreeTypeDLL const to 'libfreetype.so' instead of 'freetype.dll'. This allowed it to compile, but on execution, it fails the the assert for FreeType version under TFTManager.Initialize(). I tried also using libfreetype.so.6 and libfreetype.so.6.20.1 which also reside in my /usr/lib, but it still fails that assert. I can comment that assert out, but nothing works anyway.

That's about as far as I've gotten with trying to get it to work with FPC.

TRon

  • Hero Member
  • *****
  • Posts: 2780
Re: Linux - Any units for rendering font glyphs to memory?
« Reply #3 on: May 31, 2024, 05:19:19 am »
Apologies as I can't comment on the quality of the code.

You linked to it probably with the expectation that it works but from experience I know that many of these things do not work due to shoddy coding. The code you linked to is riddled with wrong assumptions on enumerations and has lots of range check errors (which is a reasonable indication the code is of bad quality).

In fact I deemed the quality so bad that I did not even attempt to get it working. imho it is easier to start from scratch or as suggested add some missing implementations to the existing freetype unit present in Free Pascal.

I have added the following two functions to a new unit in order to be able to render a font from memory:
Code: Pascal  [Select][+][-]
  1. unit  myfftunit;
  2. interface
  3. uses
  4.   freetypeh;
  5. ...
  6. type
  7.   PFT_Byte = ^FT_Byte;
  8.  
  9.   function  FT_Render_Glyph(slot : PFT_GlyphSlot; render_mode : FT_Render_Mode ) : FT_Error; cdecl; external freetypedll name 'FT_Render_Glyph';
  10.   function  FT_New_Memory_Face(aLibrary: PFT_Library; file_base: PFT_Byte; file_size, face_index: FT_LONG; var face: PFT_Face): FT_Error; cdecl; external freetypedll name 'FT_New_Memory_Face';
  11. ..
  12.  
As that was basically enough for me but your requirements might perhaps vary.

SuperSathanas

  • New Member
  • *
  • Posts: 16
Re: Linux - Any units for rendering font glyphs to memory?
« Reply #4 on: May 31, 2024, 05:53:03 am »
Yeah, that did it. I guess it is just that the uFreeType unit is wonky. I was able to load a glyph, render it, and write it to a bitmap on disk. I might do an entire translation of FreeType at some point, but for the moment it seems like just adding what I need is going to work for my needs. Thank you sir.

 

TinyPortal © 2005-2018