We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 3046 - SDL_ttf + HarfBuzz
Summary: SDL_ttf + HarfBuzz
Status: ASSIGNED
Alias: None
Product: SDL_ttf
Classification: Unclassified
Component: misc (show other bugs)
Version: unspecified
Hardware: x86_64 Linux
: P2 normal
Assignee: Sylvain
QA Contact: Sam Lantinga
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-06-30 17:18 UTC by Sylvain
Modified: 2021-02-02 07:15 UTC (History)
2 users (show)

See Also:


Attachments
SDL_ttf.c + HarfBuzz (69.08 KB, text/x-csrc)
2015-06-30 17:19 UTC, Sylvain
Details
Two new APIs to choose the rendering direction + script (11.75 KB, text/x-chdr)
2015-06-30 17:20 UTC, Sylvain
Details
compilation file for Android (708 bytes, text/x-makefile)
2015-06-30 17:20 UTC, Sylvain
Details
Android.mk for HarfBuzz (3.31 KB, text/x-matlab)
2015-08-18 20:11 UTC, Sylvain
Details
SDL_ttf.c + HarfBuzz (112.96 KB, text/x-csrc)
2019-04-02 08:07 UTC, Sylvain
Details
SDL_ttf.h + HarfBuzz (15.49 KB, text/x-chdr)
2019-04-02 08:08 UTC, Sylvain
Details
Android.mk for HarfBuzz 2.3.1 (3.74 KB, text/x-matlab)
2019-04-02 08:10 UTC, Sylvain
Details
Android.mk for HarfBuzz 2.3.1 (3.74 KB, text/x-matlab)
2019-04-02 08:11 UTC, Sylvain
Details
Modified Android.mk for SDL_ttf (748 bytes, text/x-makefile)
2019-04-02 08:12 UTC, Sylvain
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Sylvain 2015-06-30 17:18:34 UTC
Here's a ticket with a modification of SDL_ttf to get it working with the text shaping engine "HarfBuzz" (http://www.freedesktop.org/wiki/Software/HarfBuzz).

SDL_ttf render correctly many languages, but for a few others there are troubles : wrong glyphs, inversed characters, bad positioning.
For instance, all this languages render incorrectly: arabic, bengali, burmese, hindi, kannada, khmer, lao, malayalam, marathi, nepali, persian, sinhala, tamil, telugu, thai.

The issue is that SDL_tff library has to process the string before rendering it, in order to do things like "typographic ligature" and "contextual pattern substitutions".
Those rules are stored in the font ".ttf" file, in the GSUB table (http://partners.adobe.com/public/developer/opentype/index_table_formats1.html), this quite difficult to decode and also to apply.
That's what HarfBuzz does (among other things).

The patch is not meant to be merged because it adds a new dependency that may not be wanted. But, if you plan to render some of the previous languages, you definitely need it.
I have tried ~70 languages, on Linux + Android with success.
Comment 1 Sylvain 2015-06-30 17:19:03 UTC
Created attachment 2208 [details]
SDL_ttf.c + HarfBuzz
Comment 2 Sylvain 2015-06-30 17:20:03 UTC
Created attachment 2209 [details]
Two new APIs to choose the rendering direction + script
Comment 3 Sylvain 2015-06-30 17:20:43 UTC
Created attachment 2210 [details]
compilation file for Android
Comment 4 Sylvain 2015-06-30 17:24:58 UTC
More information:

The file contains also this patch: https://bugzilla.libsdl.org/show_bug.cgi?id=2749

Once you have downloaded the source of HarfBuzz, you need to configure it not to add extra dependencies: 
ie: ./configure --with-icu=no --with-cairo=no --with-glib=no --with-gobject=no

Plus, you need to call "make" once, even if it fails (e.g. on android), so that it generates some internal state machine/automate.
Comment 5 Sylvain 2015-08-18 20:11:37 UTC
Created attachment 2247 [details]
Android.mk for HarfBuzz

Android.mk used to compile HarfBuzz on Android
Comment 6 Sylvain 2019-04-02 08:07:49 UTC
Created attachment 3722 [details]
SDL_ttf.c + HarfBuzz

Update to use HarfBuzz with latest SDL_ttf.c
Comment 7 Sylvain 2019-04-02 08:08:21 UTC
Created attachment 3723 [details]
SDL_ttf.h + HarfBuzz
Comment 8 Sylvain 2019-04-02 08:10:22 UTC
Created attachment 3724 [details]
Android.mk for HarfBuzz 2.3.1

This is the Android.mk to use for Harfbuzz 2.3.1
Comment 9 Sylvain 2019-04-02 08:11:11 UTC
Created attachment 3725 [details]
Android.mk for HarfBuzz 2.3.1

mark previous ones as obsolete
Comment 10 Sylvain 2019-04-02 08:12:55 UTC
Created attachment 3726 [details]
Modified Android.mk for SDL_ttf
Comment 11 Sam Lantinga 2019-04-04 21:36:03 UTC
Is it possible to make this a configure option (disabled by default) so we can merge this and make it an option for people?
Comment 12 Sylvain 2019-04-05 07:46:40 UTC
Ok, I'll add this. 
So with the source package external/harfbuzz-2.3.1
and a setting in Android.mk to be able to build
Comment 13 Sylvain 2019-04-05 13:44:27 UTC
Just added !

external/harfbuzz-2.3.1 from https://github.com/harfbuzz/harfbuzz/releases
with docs, test and ChangeLog

It needs an Android.mk and a config.h
https://hg.libsdl.org/SDL_ttf/rev/868bbc9fbf4c
Maybe we don't need the config.h because there is a HAVE_CONFIG_H, not sure..

SDL_ttf is patch to use harfbuzz
https://hg.libsdl.org/SDL_ttf/rev/b357aefce885
Comment 14 Rene Dudfield 2019-08-29 09:23:10 UTC
Hello. Thanks!

We (pygame) are very interested in this, as a lot of users have requested this feature. The pygame issue is here for anyone interested in following along: https://github.com/pygame/pygame/issues/464
Comment 15 Sylvain 2019-08-29 10:29:20 UTC
Hello,

You just need the latest head source of SDL_ttf, and compiles with TTF_USE_HARFBUZZ or SUPPORT_HARFBUZZ for Android.

Then pick the right font, set script var (I think sometimes it helps), and renders text. The code is tested on Android, IOS, MacosX, Linux, Windows 10. You maybe need to update your build configuration.

Latest version also include assembly optimizations, bug fix, and subpixel positioning. (https://hg.libsdl.org/SDL_ttf/rev/9f46efc0fde2 )
Comment 16 Rene Dudfield 2019-08-29 11:27:47 UTC
Hello. Thanks for the tips. Some very nice changes coming up in SDL_ttf indeed.

What do you mean by "set script var"?
Comment 17 Sylvain 2019-08-29 11:53:01 UTC
I meant TTF_SetScript() to tell harfbuzz the language/script.
Comment 18 Rene Dudfield 2019-08-29 12:04:21 UTC
Ah yes.

How does a user know which script to set for a font?
Comment 19 Sylvain 2019-08-29 13:10:35 UTC
Because of the language he want to write. See harfbuzz doc for scripts.

He also has to choose his fonts, because not all fonts render all languages.

I think HB also has a function to auto detect script.
Comment 20 Mehdi Sadeghi 2020-07-28 20:02:30 UTC
While trying to add RTL to INSTEAD game engine I came across this patch. Since Sylvain has already made a patch and Sam showed the green light for merging it as an option, is there any chance to move this forward?

Thanks for spending your (free) time on this.
Comment 21 Sylvain 2020-07-28 21:25:20 UTC
hi thanks,
many things were merged in the SDL_ttf head already
see 
https://hg.libsdl.org/SDL_ttf/
https://hg.libsdl.org/SDL_ttf/shortlog
Comment 22 Mehdi Sadeghi 2020-07-29 12:22:51 UTC
(In reply to Sylvain from comment #21)
> hi thanks,
> many things were merged in the SDL_ttf head already
> see 
> https://hg.libsdl.org/SDL_ttf/
> https://hg.libsdl.org/SDL_ttf/shortlog

Would you mind pointing me to the right compile procedure to get the latest SDL_ttf compiled for Linux desktop (not Android) with HarfBuzz support included?

I was initially able to compile SDL_ttf with your patch from here (https://discourse.libsdl.org/t/sdl-for-farsi-or-persian-text/25292/7) and by calling TTF_SetDirection( HB_DIRECTION_RTL ) and TTF_SetScript(HB_SCRIPT_ARABIC), and using a ttf font such as DejaVuSans.ttf I could see proper rendered text. 

However, with the latest changes, no matter what I do I get boxes again. Probably I have messed up things when fighting with cmake and make to enable the feature. The program compiles though, so it means the functions are there but have no effect.

Thanks in advance for any tips.
Comment 23 Sylvain 2020-07-29 12:31:26 UTC
Edit the SDL_ttf.c source and set TTF_USE_HARFBUZZ to 1, or define it from upper level.
Compile with configure for instance. you need to also provide include and library path to harfbuzz.
Comment 24 Sylvain 2020-07-29 12:31:46 UTC
you still need TTF_SetDirection( HB_DIRECTION_RTL ) and TTF_SetScript(HB_SCRIPT_ARABIC),
Comment 25 Mehdi Sadeghi 2020-07-29 21:16:35 UTC
(In reply to Sylvain from comment #23)
> Edit the SDL_ttf.c source and set TTF_USE_HARFBUZZ to 1, or define it from
> upper level.
> Compile with configure for instance. you need to also provide include and
> library path to harfbuzz.

sorry, it was my fault. The font in my program was pointing to a wrong file. I changed the SDL_ttf.c and it worked like charm. However, I was unable to set it via configure. Is there any specific configure flag for it? I searched the autotools source files and even tried to define the variable using AC_DEFINE to no avail.
Comment 26 Sylvain 2020-07-30 12:43:17 UTC
sorry there was no configure modification.
though you can set some CFLAGS environment variable to set the TTF_USE_HARFBUZZ define.

Fell free to submit a patch so it can be enable with configure, but sync with Sam about the usual way to do it.
Comment 27 Mehdi Sadeghi 2021-02-01 19:41:02 UTC
I though I'll let you know that this change is now included in the emscripten-core by default, i.e. TTF_USE_HARFBUZZ is set to 1. Providing `-s USE_SDL_TTF=2` when compiling should be enough to compile C code to WASM with HarfBuzz support.

See:
https://github.com/emscripten-core/emscripten/commit/1690a5802cd1241adc9714fb7fa2f633d38860dc#diff-874f33563125619a7a5cb567ebe523c59258662d69858d78d24d947b275f9c6cR40
Comment 28 Sylvain 2021-02-02 07:15:35 UTC
Ok, thanks for heads up!