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

Load Font From Memory (make better documentation) #107

Open
Klebestreifen opened this issue Dec 22, 2021 · 7 comments · May be fixed by #153
Open

Load Font From Memory (make better documentation) #107

Klebestreifen opened this issue Dec 22, 2021 · 7 comments · May be fixed by #153

Comments

@Klebestreifen
Copy link

Hello,

I have problems loading a TTF font from the buffer. Since I don't want to load the font file as such from disk, I would like to use the LoadFontFromMemory function, but this doesn't seem to have a wrapper. I also tried to use the FFI binding but the raw FFI font struct is not readily compatible with the API and the constructor of the wrapped struct seems private.

So I need help loading a font from a [u8]. My sample code:

use raylib::prelude::*;
use raylib::ffi::LoadFontFromMemory;
use std::ffi::CString;
use std::convert::TryInto;
use structopt::StructOpt;

mod options;

fn main() {
    let opt = options::Opt::from_args();
    let (mut rl, thread) = opt.open_window("Logo");
    let (w, h) = (opt.width, opt.height);
    let rust_orange = Color::new(222, 165, 132, 255);
    let ray_white = Color::new(255, 255, 255, 255);

    let fontfile = include_bytes!("../rsc/font/PressStart2P.ttf");
    let fontfile_size = fontfile.len();
    let fontfile_type = CString::new("TTF").unwrap();
    let mut chars = 0i32;
    let font = rl.load_font_from_memory(); // What i want c(^-^c)
    // Not working:
    // let font = Font(unsafe{
    //     LoadFontFromMemory(
    //         fontfile_type.as_ptr(),
    //         fontfile.as_ptr(),
    //         fontfile_size.try_into().unwrap(),
    //         12,
    //         &mut chars,
    //         0
    //     )
    // });
    rl.set_target_fps(60);
    while !rl.window_should_close() {
        // Detect window close button or ESC key
        let mut d = rl.begin_drawing(&thread);
        d.clear_background(ray_white);
        d.draw_text_ex(&font, "Hello World!", Vector2::new(10f32,10f32), 12f32, 0f32, Color::new(0,0,0,255));
    }
}
@enricoKoschel
Copy link
Contributor

I have found that forking raylib-rs and creating your own binding for a function thats missing one is very easy. Just fork the repo, clone it, run git submodule update --init (important!), and then build with cargo +nightly build. After adding your binding push the code to your fork and change your raylib dependency from the one on crates.io to your fork.

@ricmatsui ricmatsui linked a pull request Mar 11, 2023 that will close this issue
@JameEnder
Copy link

You need to write the file type as CString::new(".ttf").unwrap().
The . needs to be included, for whatever reason.

@IoIxD
Copy link
Contributor

IoIxD commented Jan 11, 2024

Here's an actually working example for anybody who stumbles upon this:

    let fontfile = include_bytes!("./Ubuntu-Regular.ttf");
    let fontfile_size = fontfile.len();
    let fontfile_type = CString::new(".ttf").unwrap();
    let chars = null_mut();
    let font = unsafe {
        Font::from_raw(LoadFontFromMemory(
            fontfile_type.as_ptr(),
            fontfile.as_ptr(),
            fontfile_size.try_into().unwrap(),
            256,
            chars,
            100,
        ))
    };
    ```

@IoIxD
Copy link
Contributor

IoIxD commented Jan 13, 2024

I wouldn't consider this closed. This is a less then ideal situation and I think this should be implemented as a safe function.

That said, this repo is severely dormant. This should maybe be reopened/PR'd in the fork mentioned in the resdme.

@Klebestreifen
Copy link
Author

  • Better documentation

@Klebestreifen Klebestreifen reopened this Jan 24, 2024
@Klebestreifen Klebestreifen changed the title Load Font From Memory Load Font From Memory (make better documentation) Jan 24, 2024
@IoIxD
Copy link
Contributor

IoIxD commented Jan 24, 2024

This repo also isn't maintained anymore.

I'll open up a PR in the fork that is maintained.

@IoIxD
Copy link
Contributor

IoIxD commented Jan 31, 2024

UPDATE: It has been merged. It's actually been accepted into the master of that branch meaning you do not even need to upgrade to 5.0. (In fact, you shouldn't, because it has not been merged to the other branches yet).

https://github.com/raylib-rs/raylib-rs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants