Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add freetype font rendering #2855

Merged
merged 23 commits into from
Nov 13, 2022
Merged

Add freetype font rendering #2855

merged 23 commits into from
Nov 13, 2022

Conversation

Ghabry
Copy link
Member

@Ghabry Ghabry commented Oct 29, 2022

This adds experimental freetype rendering support.

Still marked as draft as I must test some stuff. But the basic rendering works.

When fonts are provided as "Font/Font.ttf" (gothic) or "Font/Font2.ttf" (mincho) these fonts are used instead of the built-in. Should also work for the web player and translations can provide custom fonts.

Also supports complex stuff like Thai (not using Harfbuzz though as Freetype is good enough to render Thai properly).

TODO: Testing testing testing. Fallback logic?

screenshot_0
screenshot_1
screenshot_2

Fix #2857

When loading a translation a custom font can be provided.

Also works for the web player.
@Ghabry Ghabry added the Fonts label Oct 29, 2022
@Ghabry Ghabry added this to the 0.7.1 milestone Oct 29, 2022
@Ghabry Ghabry marked this pull request as draft October 29, 2022 12:45
@Ghabry
Copy link
Member Author

Ghabry commented Oct 30, 2022

Fixed the rendering of FON. The metrics are wrong but this is a font issue. Will see if I can detect the font and then just force correct metrics...

screenshot_3

@Ghabry
Copy link
Member Author

Ghabry commented Oct 30, 2022

Added a detection for RM2000 and RMG2000: When these fonts are used the offset is overwritten with a good one.

screenshot_4

@BlisterB
Copy link
Member

As discussed in the Discord :

  • The file "Font.xxx" doesn't seem to be detected. When selected, EasyRPG uses the default font. "Font2.xxx" works correctly.

I haven't saw any other problem for now, everything seem to work nicely. Awesome feature. ^^

@jetrotal
Copy link
Contributor

jetrotal commented Oct 31, 2022

More discussion from discord:

Some fonts are not optimized for EasyRPG proportion Settings:

image
Fonts Used.zip
FindersKeepers font is better displayed at size = 16 than at the default size = 12

To solve that, I'd like to suggest an optional settings layer, that could be something like:

{
  "fontConfig": {
    "0": {
      "source": "./MSGOTHIC.TTF",
      "size": 12,
      "spacing": "auto",
      "etc": "etc"
    },
    "1": {
      "source": "auto",
      "size": "auto",
      "spacing": "auto",
      "etc": "etc"
    }
  }
}

I would like to also suggest Font Fallback for Missing Glyphs.

I Tried a font called Material Design Icons, that adds a lot of custom Glyphs:
image

But the lack of A,B,C... glyphs made the font blank:
image

Maybe, A character going back to the default font whenever it fails loading its glyph could help on this.


Last feature I'd like to suggest is a changing font mid Text input.

like in this mockup:

Helen: Yo Don Miguel!
I have got \fon[1]Cool Compass\fon[0]!
I can sell it to ya!

image


This PR is so amazing, I love it!

@Ghabry
Copy link
Member Author

Ghabry commented Oct 31, 2022

All this font config stuff is a good idea but honestly I would prefer to have this as part of the database which means this is a job for the editor.

Just want to avoid having lots of config files for stuff that should be in the database. But you are totally right that we need something like this.

For now I will only add a fallback to our built-in fonts when a glyph is missing.

@Ghabry
Copy link
Member Author

Ghabry commented Nov 1, 2022

The file "Font.xxx" doesn't seem to be detected

This is fixed now.

Also not crasing anymore when closing the player ;)

Also do not parse non-po files when processing map translations
Remove Windows specific font support.
Add more extensions (fnt and bdf) to the font list.
@Ghabry
Copy link
Member Author

Ghabry commented Nov 1, 2022

Note to self: It was suggested to support color TTF fonts. Problem is that no font with such small image size exists. The ones from Google etc. don't work.

How to get color out with FreeType:

  • Instead of FT_Set_Pixel_Sizes call FT_Select_Size(face, 0)
  • Instead of FT_LOAD_MONOCHROME use FT_LOAD_COLOR
  • Instead of FT_RENDER_MODE_MONO use FT_RENDER_MODE_NORMAL
  • Check for color font with FT_HAS_COLOR(face)
  • To render it something like this should work:
if (ft_bitmap->pixel_mode == FT_PIXEL_MODE_BGRA) {
    bm = Bitmap::Create(ft_bitmap->buffer, width, height, 0, format_B8G8R8A8_a().format());

I will add it when a proper font is provided :)

@Ghabry
Copy link
Member Author

Ghabry commented Nov 1, 2022

The system graphic color mask is now correctly applied:

screenshot_3

Bonus feature: Colorful ExFonts

screenshot_2

Open issues:

  • Ugly half-width space in Help Window
  • FreeType requires Fallback to built-in fonts

@Ghabry Ghabry marked this pull request as ready for review November 3, 2022 13:14
@Ghabry
Copy link
Member Author

Ghabry commented Nov 3, 2022

This is now ready.

There is one minor breaking change: ExFont was relocated to "Font/ExFont" for consistency. Emscripten will enforce this but local Player will check both locations.

All changes outside of the FreeType code will return the same font metrics as before so no code behaviour should change when no FreeType Font is loaded.

New features:

  • FreeType font loading (obviously)
  • Font/Font and Font/Font2 are loaded depending on Font in the system graphic setting (currently hardcoded to 12 px size, font config should be an editor setting later)
  • Support for TTF and Pixel Fonts (FON, BDF, etc.). Everything that FreeType supports
  • Complex shaping (Harfbuzz) is currently not supported due to how our code is designed but Thai appears to work good enough without it
  • Support for RGB Color in ExFont. Like you know with Emojis this will not recolor the glyph with the system graphic but the drop-shadow is added.
  • Support for Color Glyphs in Fonts (the same feature Color Emojis use). EXPERIMENTAL (!). There are no fonts in-the-wild that have 12x12 color glyphs. I'm currently preparing a Python script to make generating of such fonts possible. :). (Why is that needed: Fonts can contain arbitrary PNG images so you can (ab)use this in many creative way)
  • FreeType falls back to built-in Bitmap when a glyph is not found

The renderer likely has bad performance because there is no glyph caching etc. involved. If this becomes an issue this can be improved later.

Also I do not recommend using this on any low memory platforms because the entire font file is loaded into memory. So this wastes many megabyte.

@Ghabry
Copy link
Member Author

Ghabry commented Nov 3, 2022

Example of creative (ab)use ;). (§ is mapped in a TTF to a color glyph that shows this health bar image)

screenshot_9


(asked by TrunX): Is it possible to use this in the menu. Answer: For sure

screenshot_12

src/player.cpp Show resolved Hide resolved
".opus", ".oga", ".ogg", ".wav", ".mid", ".midi", ".mp3", ".wma");
constexpr const auto SOUND_TYPES = Utils::MakeSvArray(
".opus", ".oga", ".ogg", ".wav", ".mp3", ".wma");
constexpr const auto FONTS_TYPES = Utils::MakeSvArray(".ttf", ".ttc", ".otf", ".fon", ".fnt", ".bdf");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.woff and .woff2 OpenType formats are useful for significantly smaller emscripten asset downloads (may require extra freetype dependencies, e.g. brotli).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The FileFinder code aborts early when the directory is missing so for most games adding more extensions won't hurt. I add a commit :)

@Ghabry
Copy link
Member Author

Ghabry commented Nov 3, 2022

Jenkins: Test this please

This includes FPS, Message Overlay and Error reporting.

Otherwise, this looks awful.
It looks awful for most graphics.
With full RGB support you can paint your own shadow if needed.
Enable Monochrome Hinter

Update docs
@Ghabry
Copy link
Member Author

Ghabry commented Nov 6, 2022

(This is now really, really finished. Backported all fixed for issues I found while working on Harfbuzz)

@fdelapena fdelapena merged commit 08da34b into EasyRPG:master Nov 13, 2022
@Ghabry Ghabry mentioned this pull request Nov 13, 2022
@Ghabry Ghabry deleted the freetype branch June 12, 2023 18:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

TTF color font support
4 participants