Function fltk::draw::set_font

source ·
pub fn set_font(face: Font, fsize: i32)
Expand description

Sets the current font, which is then used in various drawing routines

Examples found in repository?
examples/gradients.rs (line 23)
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
fn create_vertical_gradient_frame(
    x: i32,
    y: i32,
    w: i32,
    h: i32,
    col1: Color,
    col2: Color,
) -> frame::Frame {
    let mut frame = frame::Frame::new(x, y, w, h, "Vertical");
    frame.draw(move |f| {
        let imax = f.h();
        let d = if imax > 0 { imax } else { 1 };
        for i in 0..=imax {
            let w = 1.0 - i as f32 / d as f32;
            set_draw_color(Color::inactive(&Color::color_average(col1, col2, w)));
            draw_xyline(f.x(), f.y() + i, f.x() + f.w());
        }
        set_draw_color(Color::Black);
        set_font(Font::Helvetica, app::font_size());
        draw_text2(&f.label(), f.x(), f.y(), f.w(), f.h(), f.align());
    });
    frame
}

fn create_horizontal_gradient_frame(
    x: i32,
    y: i32,
    w: i32,
    h: i32,
    col1: Color,
    col2: Color,
) -> frame::Frame {
    let mut frame = frame::Frame::new(x, y, w, h, "Horizontal");
    frame.draw(move |f| {
        let imax = f.w();
        let d = if imax > 0 { imax } else { 1 };
        for i in 0..=imax {
            let w = 1.0 - i as f32 / d as f32;
            set_draw_color(Color::inactive(&Color::color_average(col1, col2, w)));
            draw_yxline(f.x() + i, f.y(), f.y() + f.h());
        }
        set_draw_color(Color::Black);
        set_font(Font::Helvetica, app::font_size());
        draw_text2(&f.label(), f.x(), f.y(), f.w(), f.h(), f.align());
    });
    frame
}

fn create_horizontal_svg_gradient_frame(
    x: i32,
    y: i32,
    w: i32,
    h: i32,
    col1: Color,
    col2: Color,
) -> frame::Frame {
    let mut frame = frame::Frame::new(x, y, w, h, "Svg");
    frame.draw(move |f| {
        let (r1, g1, b1) = Color::inactive(&col1).to_rgb();
        let (r2, g2, b2) = Color::inactive(&col2).to_rgb();
        let svg = format!(
            "<svg viewBox='0 0 {} {}'>
        <defs>
        <linearGradient id='grad1' x1='0%' y1='0%' x2='0%' y2='100%'>
        <stop offset='0%' style='stop-color:rgb({},{},{});stop-opacity:1' />
        <stop offset='100%' style='stop-color:rgb({},{},{});stop-opacity:1' />
        </linearGradient>
        </defs>
        <rect width='100%' height='100%' fill='url(#grad1)' />
        </svg>",
            f.w(),
            f.h() + 1,
            r1,
            g1,
            b1,
            r2,
            g2,
            b2
        );
        let mut image = image::SvgImage::from_data(&svg).unwrap();
        image.draw(f.x(), f.y(), f.w(), f.h());
        set_draw_color(Color::Black);
        set_font(Font::Helvetica, app::font_size());
        draw_text2(&f.label(), f.x(), f.y(), f.w(), f.h(), f.align());
    });
    frame
}
More examples
Hide additional examples
examples/table.rs (line 27)
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
fn main() {
    let app = app::App::default().with_scheme(app::Scheme::Gtk);
    let mut wind = window::Window::default().with_size(800, 600);
    let mut table = table::Table::default()
        .with_size(800 - 10, 600 - 10)
        .center_of(&wind);

    table.set_rows(30);
    table.set_row_header(true);
    table.set_row_resize(true);
    table.set_cols(26);
    table.set_col_header(true);
    table.set_col_width_all(80);
    table.set_col_resize(true);
    table.end();

    wind.make_resizable(true);
    wind.end();
    wind.show();

    // Called when the table is drawn then when it's redrawn due to events
    table.draw_cell(move |t, ctx, row, col, x, y, w, h| match ctx {
        table::TableContext::StartPage => draw::set_font(enums::Font::Helvetica, 14),
        table::TableContext::ColHeader => {
            draw_header(&format!("{}", (col + 65) as u8 as char), x, y, w, h)
        } // Column titles
        table::TableContext::RowHeader => draw_header(&format!("{}", row + 1), x, y, w, h), // Row titles
        table::TableContext::Cell => draw_data(
            &format!("{}", row + col),
            x,
            y,
            w,
            h,
            t.is_selected(row, col),
        ), // Data in cells
        _ => (),
    });

    app.run().unwrap();
}

fn draw_header(txt: &str, x: i32, y: i32, w: i32, h: i32) {
    draw::push_clip(x, y, w, h);
    draw::draw_box(
        enums::FrameType::ThinUpBox,
        x,
        y,
        w,
        h,
        enums::Color::FrameDefault,
    );
    draw::set_draw_color(enums::Color::Black);
    draw::set_font(enums::Font::Helvetica, 14);
    draw::draw_text2(txt, x, y, w, h, enums::Align::Center);
    draw::pop_clip();
}

// The selected flag sets the color of the cell to a grayish color, otherwise white
fn draw_data(txt: &str, x: i32, y: i32, w: i32, h: i32, selected: bool) {
    draw::push_clip(x, y, w, h);
    if selected {
        draw::set_draw_color(enums::Color::from_u32(0x00D3_D3D3));
    } else {
        draw::set_draw_color(enums::Color::White);
    }
    draw::draw_rectf(x, y, w, h);
    draw::set_draw_color(enums::Color::Gray0);
    draw::set_font(enums::Font::Helvetica, 14);
    draw::draw_text2(txt, x, y, w, h, enums::Align::Center);
    draw::draw_rect(x, y, w, h);
    draw::pop_clip();
}
examples/spreadsheet.rs (line 63)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
    pub fn new(mut inp: input::Input) -> Self {
        let mut table = table::Table::default()
            .with_size(800 - 10, 600 - 10)
            .center_of_parent();
        let data = Rc::from(RefCell::from(vec![vec![String::from(""); 26]; 28]));
        let cell = Rc::from(RefCell::from(CellData::default()));

        table.set_rows(28);
        table.set_row_header(true);
        table.set_row_resize(true);
        table.set_cols(26);
        table.set_col_header(true);
        table.set_col_width_all(80);
        table.set_col_resize(true);
        table.end();

        let cell_c = cell.clone();
        let data_c = data.clone();

        // Called when the table is drawn then when it's redrawn due to events
        table.draw_cell(move |t, ctx, row, col, x, y, w, h| match ctx {
            table::TableContext::StartPage => draw::set_font(enums::Font::Helvetica, 14),
            table::TableContext::ColHeader => {
                Self::draw_header(&format!("{}", (col + 65) as u8 as char), x, y, w, h)
            } // Column titles
            table::TableContext::RowHeader => {
                Self::draw_header(&format!("{}", row + 1), x, y, w, h)
            } // Row titles
            table::TableContext::Cell => {
                if t.is_selected(row, col) {
                    cell_c.borrow_mut().select(row, col, x, y, w, h); // Captures the cell information
                }
                Self::draw_data(
                    &data_c.borrow()[row as usize][col as usize].to_string(),
                    x,
                    y,
                    w,
                    h,
                    t.is_selected(row, col),
                );
            }
            _ => (),
        });

        let cell_c = cell.clone();
        let data_c = data.clone();

        table.handle(move |_, ev| match ev {
            // Event::Push will happen before the focus is moved,
            // thus giving the previous coordinates.
            // Event::Released gives an accurate position
            enums::Event::Released => {
                let c = cell_c.borrow();
                inp.resize(c.x, c.y, c.w, c.h);
                inp.set_value(&data_c.borrow_mut()[c.row as usize][c.col as usize]);
                inp.show();
                inp.take_focus().ok();
                inp.redraw();
                true
            }
            _ => false,
        });

        Self { table, data, cell }
    }

    pub fn redraw(&mut self) {
        self.table.redraw()
    }

    fn draw_header(txt: &str, x: i32, y: i32, w: i32, h: i32) {
        draw::push_clip(x, y, w, h);
        draw::draw_box(
            enums::FrameType::ThinUpBox,
            x,
            y,
            w,
            h,
            enums::Color::FrameDefault,
        );
        draw::set_draw_color(enums::Color::Black);
        draw::set_font(enums::Font::Helvetica, 14);
        draw::draw_text2(txt, x, y, w, h, enums::Align::Center);
        draw::pop_clip();
    }

    // The selected flag sets the color of the cell to a grayish color, otherwise white
    fn draw_data(txt: &str, x: i32, y: i32, w: i32, h: i32, selected: bool) {
        draw::push_clip(x, y, w, h);
        if selected {
            draw::set_draw_color(enums::Color::from_u32(0x00D3_D3D3));
        } else {
            draw::set_draw_color(enums::Color::White);
        }
        draw::draw_rectf(x, y, w, h);
        draw::set_draw_color(enums::Color::Gray0);
        draw::set_font(enums::Font::Helvetica, 14);
        draw::draw_text2(txt, x, y, w, h, enums::Align::Center);
        draw::draw_rect(x, y, w, h);
        draw::pop_clip();
    }