pop-os / cosmic-text

Pure Rust multi-line text handling

Home Page:https://pop-os.github.io/cosmic-text/cosmic_text/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Expose public C/C++ API

Theramar opened this issue · comments

It is not my desire at this time to provide a C interface for cosmic-text. If you can do the work, though, I would potentially merge it.

At the moment cosmic-text is pretty unstable and exposes a lot of the implementation details of the underlying crates. If it were me I would wait for greater stability before writing a C API

Is there any updates on this? I would love to use this project in my game engine, but I am not using Rust so I would love to have a C API for this. I am roughly familiar with Rust and would be happy to contribute, but I'm not sure where I would start.

I assume a PR which added this would add the header generation stuff to build.rs and make any adjustments to the rest of the codebase to make the API interop-friendly. Is "greater API stability" something that will be achieved any time in the near future, or is it more of a long-term goal?

commented

I made some C++ bindings, but they are very incomplete. Currently I am just rendering some text to the screen with raylib. I generate C++ bindings, but you can make it generate some C bindings, you just need to make a new cbindgen toml file and specify that you want to build a C header.

Here is what it looks like:

#include <cosmic-text.hpp>
#include "raylib.h"

constexpr int SCREEN_WIDTH = 900;
constexpr int SCREEN_HEIGHT = 450;
constexpr int WINDOW_PADDING = 20;

void draw_fn(int32_t x, int32_t y, uint32_t w, uint32_t h, cosmic_text::Color color) {
    const Color rayib_color = {
        cosmic_text::color_r(color),
        cosmic_text::color_g(color),
        cosmic_text::color_b(color),
        cosmic_text::color_a(color)
    };

    DrawRectangle(x + WINDOW_PADDING, y + WINDOW_PADDING, static_cast<int>(w), static_cast<int>(h), rayib_color);
}

int main() {

    cosmic_text::Metrics a = cosmic_text::metrics_constructor(20.0f, 40.0f);

    // Opaque pointers.
    auto font_system = cosmic_text::font_system_constructor();
    auto swash_cache = cosmic_text::swash_cache_constructor();
    auto buffer = cosmic_text::buffer_constructor(&font_system, a);

    cosmic_text::buffer_set_size(buffer, font_system, SCREEN_WIDTH - WINDOW_PADDING, SCREEN_HEIGHT - WINDOW_PADDING);
    cosmic_text::Attrs attrs = cosmic_text::attrs_constructor();
    cosmic_text::buffer_set_text(buffer, font_system, "Hello, C++!\nおはよう (ja) (ohayō) (morning), こんにちは (ja) (konnichi wa) (daytime), こんばんは (ja) (konban wa) (evening)", attrs, cosmic_text::Shaping::Advanced);
    cosmic_text::buffer_shape_until_scroll(buffer, font_system, true);

    const cosmic_text::Color text_color = cosmic_text::color_rgba(0, 0, 0, 255);

    SetTraceLogLevel(TraceLogLevel::LOG_WARNING);
    InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "cosmic-text-raylib-example");
    SetTargetFPS(60);

    while (!WindowShouldClose())
    {
        BeginDrawing();

        ClearBackground(WHITE);
        cosmic_text::buffer_draw(buffer, font_system, swash_cache, text_color, draw_fn);
        EndDrawing();
    }

    // Make sure to release your memory!
    cosmic_text::buffer_destructor(buffer);
    cosmic_text::swash_cache_destructor(swash_cache);
    cosmic_text::font_system_destructor(font_system);

    CloseWindow();
    return 0;
}

intro

(WIP)
*Only tested on Windows
https://github.com/NoahR02/cosmic-text-cpp