1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//! # rich-sdl2-ttf-rust
//!
//! The rich-sdl2-ttf-rust provides wrapper for SDL2_ttf and abstractions of font rendering APIs.

#![warn(missing_docs)]

use rich_sdl2_rust::{Result, Sdl, SdlError, SdlVersion};
use static_assertions::assert_not_impl_all;
use std::{cell::Cell, marker::PhantomData, os::raw::c_int};

/// Rust FFI to `SDL_ttf.h`
#[allow(warnings)]
mod bind;
pub mod font;
pub mod script;

/// A root SDL2_ttf controller.
#[derive(Debug)]
pub struct Ttf {
    _phantom: PhantomData<Cell<u8>>,
}

assert_not_impl_all!(Ttf: Send, Sync);

impl Ttf {
    /// Constructs a root controller.
    pub fn new() -> Self {
        let ret = unsafe { bind::TTF_Init() };
        if ret == -1 {
            Sdl::error_then_panic("Ttf");
        }
        Self {
            _phantom: PhantomData,
        }
    }

    /// Returns the library version of SDL2_ttf.
    pub fn version() -> SdlVersion {
        let raw = unsafe { &*bind::TTF_Linked_Version() };
        SdlVersion {
            major: raw.major,
            minor: raw.minor,
            patch: raw.patch,
        }
    }
}

impl Drop for Ttf {
    fn drop(&mut self) {
        unsafe { bind::TTF_Quit() }
    }
}

impl Default for Ttf {
    fn default() -> Self {
        Self::new()
    }
}

/// A direction of a text segment.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Direction {
    /// A writing from left to right.
    Ltr,
    /// A writing from right to left.
    Rtl,
    /// A writing from top to bottom.
    Ttb,
    /// A writing from bottom to top.
    Btt,
}

impl Direction {
    fn into_raw(self) -> c_int {
        match self {
            Direction::Ltr => 4,
            Direction::Rtl => 5,
            Direction::Ttb => 6,
            Direction::Btt => 7,
        }
    }

    /// Sets the direction of a text segment.
    pub fn set_direction(self) -> Result<()> {
        let ret = unsafe { bind::TTF_SetDirection(self.into_raw()) };
        if ret != 0 {
            Err(SdlError::UnsupportedFeature)
        } else {
            Ok(())
        }
    }
}