Trex is a font rasterizer, atlas generator and text shaping library written in C++. It uses FreeType and HarfBuzz libraries under the hood. It provides a simple API that makes it easy to integrate high quality typography into your application.
The text rendered by this library is every pixel identical to that rendered by the Chrome and Firefox browsers. As such, Trex is an excellent choice when you need to display text in your application that, when exported to SVG or HTML format, should look identical.
- Text Rendering - Trex allows you to render text with FreeType, providing high-quality and accurate glyph rendering on various platforms.
- Glyph Atlas Generation - With Trex, you can generate efficient and minimal glyph atlases. Sides of a power of 2 are always used.
- Text Shaping - Trex integrates HarfBuzz to shape text, ensuring proper placement and shaping of complex scripts and languages.
- High Performance - The library is very fast as it relies on algorithms implemented in state-of-the-art FreeType and HarfBuzz libraries.
- Platform Independent - Trex is platform-independent and so are its dependencies.
- Easy Integration - The library provides a simple and minimalistic API, making it easy to integrate text rendering capabilities into you projects.
- Static Library - Trex uses CMake and it is configured as a static library.
- UTF-8 and Unicode Support - Trex supports UTF-8 and Unicode text.
- SDF rendering - Trex can render glyphs using the Signed Distance Field technique.
- Subpixel Rendering (ClearType) - Trex supports subpixel rendering for RGB LCD displays.
- Emojis ❤️ - Trex can render fonts with colors, like emojis 💪😎👍
Trex::Atlas atlas("arial.ttf", 32, Trex::Charset::Ascii());
atlas.SaveToFile("atlas.png");
Trex::TextShaper shaper(atlas);
Trex::ShapedGlyphs glyphs = shaper.ShapeAscii("Hello world!");
float cursorX = 100;
float cursorY = 100;
for (const Trex::ShapedGlyph& glyph : glyphs)
{
float x = cursorX + glyph.xOffset + glyph.info.bearingX;
float y = cursorY + glyph.yOffset - glyph.info.bearingY;
int atlasGlyphX = glyph.info.x;
int atlasGlyphY = glyph.info.y;
int atlasGlyphWidth = glyph.info.width;
int atlasGlyphHeight = glyph.info.height;
// Draw a glyph at (x, y) by taking the atlas bitmap fragment
// ... atlas.GetBitmap() ...
cursorX += glyph.xAdvance;
cursorY += glyph.yAdvance;
}
To get started with Trex, follow the instructions below:
- Clone the repository:
git clone https://github.com/KyrietS/trex.git
- In your project's CMakeLists.txt add:
add_subdirectory(path_to_trex)
target_link_libraries(your_target trex)
- Rebuild your project.
- Check the examples/ to learn how to use Trex or go to the docs/ to learn about Trex API.
Note: You can also use CMake's FetchContent
module to download and configure Trex automatically.
include(FetchContent)
FetchContent_Declare(
trex
GIT_REPOSITORY https://github.com/KyrietS/trex.git
GIT_TAG master
)
FetchContent_MakeAvailable(trex)
target_link_libraries(your_target trex)
The text produced by trex has exactly the same size as the text rendered by Google Chrome and Firefox.
This is the reason why I created trex. I needed "Save to SVG" option in my application so I had to make sure that the text rendered by the application will perfectly match the SVG exported image.
Emoji support
Roboto ASCII grayscale (512 x 512)
Roboto all glyphs grayscale (2048 x 1024)
OpenMoji all emojis
The documentation for the Trex API can be found here.
Trex has the following dependencies:
- FreeType - A high-quality font engine for rendering text.
- HarfBuzz - A text shaping engine for accurate and complex text shaping.
- stb_image_write - A header-only library for saving atlas bitmaps to PNG or BMP files.
Examples use raylib library to render text on the screen.
Tests use Google Test framework.
All dependencies are fetched and configured automatically by CMake.
Copyright © 2023-2024 KyrietS
Use of this software is granted under the terms of the MIT License.
See the LICENSE file for more details.