<commit_before>use std::cell::RefCell;
use std::rc::Rc;
use std::sync::Arc;

use orbclient::Window as OrbWindow;
use orbfont::Font;

use self::backend::{OrbitalBackend, OrbitalBackendRunner};
use backend::{Backend, FontMeasure};
use structs::Rect;
use theme::{Theme, ROBOTO_REGULAR_FONT};

mod backend;
mod renderer;

pub fn target_backend(
    title: &str,
    bounds: Rect,
    theme: Theme,
) -> (Box<OrbitalBackendRunner>, Rc<RefCell<Backend>>) {
    let backend = Rc::new(RefCell::new(OrbitalBackend::new(
        theme,
        OrbWindow::new_flags(bounds.x, bounds.y, bounds.width, bounds.height, title, &[]).unwrap(),
    )));

    let backend_runner = Box::new(OrbitalBackendRunner {
        backend: backend.clone(),
        world: None,
    });

    (backend_runner, backend)
}

pub struct OrbFontMeasure {
    font: Option<Font>
}

impl FontMeasure for OrbFontMeasure {
    fn measure(&self, text: &str, font_size: u32) -> (u32, u32) {
        if let Some(font) = &self.font {
            let text = font.render(text, font_size as f32);
            return (text.width(), text.height())
        } 

        (text.len() as u32 * 8 + 2, 18)
    }
}

lazy_static! {
    pub static ref FONT_MEASURE: Arc<OrbFontMeasure> = {
        let font = {
            if let Ok(font) = Font::from_data(ROBOTO_REGULAR_FONT.to_vec().into_boxed_slice()) {
                Some(font)
            } else {
                None<commit_msg>Fix the text width measurement for the bitmap font<commit_after>use std::cell::RefCell;
use std::rc::Rc;
use std::sync::Arc;

use orbclient::Window as OrbWindow;
use orbfont::Font;

use self::backend::{OrbitalBackend, OrbitalBackendRunner};
use backend::{Backend, FontMeasure};
use structs::Rect;
use theme::{Theme, ROBOTO_REGULAR_FONT};

mod backend;
mod renderer;

pub fn target_backend(
    title: &str,
    bounds: Rect,
    theme: Theme,
) -> (Box<OrbitalBackendRunner>, Rc<RefCell<Backend>>) {
    let backend = Rc::new(RefCell::new(OrbitalBackend::new(
        theme,
        OrbWindow::new_flags(bounds.x, bounds.y, bounds.width, bounds.height, title, &[]).unwrap(),
    )));

    let backend_runner = Box::new(OrbitalBackendRunner {
        backend: backend.clone(),
        world: None,
    });

    (backend_runner, backend)
}

pub struct OrbFontMeasure {
    font: Option<Font>
}

impl FontMeasure for OrbFontMeasure {
    fn measure(&self, text: &str, font_size: u32) -> (u32, u32) {
        if let Some(font) = &self.font {
            let text = font.render(text, font_size as f32);
            return (text.width(), text.height())
        } 

        (text.chars().count() as u32 * 8 + 2, 18)
    }
}

lazy_static! {
    pub static ref FONT_MEASURE: Arc<OrbFontMeasure> = {
        let font = {
            if let Ok(font) = Font::from_data(ROBOTO_REGULAR_FONT.to_vec().into_boxed_slice()) {
                Some(font)
            } else {
                None
{'fraction_non_alphanumeric': 0.14079173838209982, 'fraction_numerical': 0.00963855421686747, 'mean_word_length': 3.0249307479224377, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2236809', 'n_tokens_mistral': 1009, 'n_tokens_neox': 980, 'n_words': 223}