Skip to content
/ trex Public

Font rendering, atlas generation and text shaping library written in C++

License

Notifications You must be signed in to change notification settings

KyrietS/trex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

83 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Trex

trex-banner Build Tests Examples Lincense Trex release

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.

Features

  • 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 💪😎👍

Basic Example

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;
}

Getting Started

To get started with Trex, follow the instructions below:

  1. Clone the repository:
git clone https://github.com/KyrietS/trex.git
  1. In your project's CMakeLists.txt add:
add_subdirectory(path_to_trex)
target_link_libraries(your_target trex)
  1. Rebuild your project.
  2. 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)

Showcase

The text produced by trex has exactly the same size as the text rendered by Google Chrome and Firefox.

trex-vs-chrome

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

emojis

Roboto ASCII grayscale (512 x 512)

ASCII atlas

Roboto all glyphs grayscale (2048 x 1024)

Roboto atlas

OpenMoji all emojis

open-moji

Documentation

The documentation for the Trex API can be found here.

Dependencies

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.

License

Copyright © 2023-2024 KyrietS
Use of this software is granted under the terms of the MIT License.

See the LICENSE file for more details.