summaryrefslogtreecommitdiffstats
path: root/gfx/wr/wr_glyph_rasterizer/examples/basic.rs
blob: 4c46004cdfc2ed0966c04d59262b5fb84c2158e5 (plain)
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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::sync::Arc;
use std::mem;

use api::{
    IdNamespace, FontTemplate, FontKey, FontInstanceKey, FontInstanceOptions,
    FontInstancePlatformOptions, ColorF, FontInstanceFlags, units::DevicePoint,
};
use glutin::ContextBuilder;
use glutin::dpi::PhysicalSize;
use glutin::event::{Event, WindowEvent};
use glutin::event_loop::{ControlFlow, EventLoop};
use glutin::window::WindowBuilder;
use rayon::ThreadPoolBuilder;
use wr_glyph_rasterizer::RasterizedGlyph;
use wr_glyph_rasterizer::{
    SharedFontResources, BaseFontInstance, GlyphRasterizer, FontInstance, GlyphKey,
    SubpixelDirection, profiler::GlyphRasterizeProfiler,
};

#[path = "common/boilerplate.rs"]
mod boilerplate;

struct Profiler;

impl GlyphRasterizeProfiler for Profiler {
    fn start_time(&mut self) {}
    fn end_time(&mut self) -> f64 {
        0.
    }
    fn set(&mut self, _value: f64) {}
}

fn load_glyphs() -> Vec<RasterizedGlyph> {
    let namespace = IdNamespace(0);
    let mut fonts = SharedFontResources::new(namespace);

    let font_key = FontKey::new(namespace, 0);
    let raw_font_data = include_bytes!("../../wrench/reftests/text/FreeSans.ttf");
    let font_template = FontTemplate::Raw(Arc::new(raw_font_data.to_vec()), 0);
    let shared_font_key = fonts
        .font_keys
        .add_key(&font_key, &font_template)
        .expect("Failed to add font key");

    let font_instance_key = FontInstanceKey::new(namespace, 1);
    fonts.templates.add_font(shared_font_key, font_template);
    assert!(fonts.templates.has_font(&shared_font_key));

    // AddFontInstance will only be processed here, not in the resource cache, so it
    // is safe to take the options rather than clone them.
    let base = BaseFontInstance::new(
        font_instance_key,
        shared_font_key,
        32.,
        mem::take(&mut Some(FontInstanceOptions::default())),
        mem::take(&mut Some(FontInstancePlatformOptions::default())),
        mem::take(&mut Vec::new()),
    );
    let shared_instance = fonts
        .instance_keys
        .add_key(base)
        .expect("Failed to add font instance key");
    fonts.instances.add_font_instance(shared_instance);

    let workers = {
        let worker = ThreadPoolBuilder::new()
            .thread_name(|idx| format!("WRWorker#{}", idx))
            .build();
        Arc::new(worker.unwrap())
    };
    let mut glyph_rasterizer = GlyphRasterizer::new(workers, false);

    glyph_rasterizer.add_font(
        shared_font_key,
        fonts
            .templates
            .get_font(&shared_font_key)
            .expect("Could not find FontTemplate"),
    );

    let mut font = FontInstance::new(
        fonts
            .instances
            .get_font_instance(font_instance_key)
            .expect("Could not found BaseFontInstance"),
        ColorF::BLACK.into(),
        api::FontRenderMode::Alpha,
        FontInstanceFlags::SUBPIXEL_POSITION | FontInstanceFlags::FONT_SMOOTHING,
    );

    glyph_rasterizer.prepare_font(&mut font);

    let glyph_keys = [
        glyph_rasterizer
            .get_glyph_index(shared_font_key, 'A')
            .expect("Failed to get glyph index"),
        glyph_rasterizer
            .get_glyph_index(shared_font_key, 'B')
            .expect("Failed to get glyph index"),
        glyph_rasterizer
            .get_glyph_index(shared_font_key, 'C')
            .expect("Failed to get glyph index"),
    ];

    let glyph_keys = glyph_keys.map(|g| {
        GlyphKey::new(
            g,
            DevicePoint::new(100., 100.),
            SubpixelDirection::Horizontal,
        )
    });

    glyph_rasterizer.request_glyphs(font, &glyph_keys, |_| true);

    let mut glyphs = vec![];
    glyph_rasterizer.resolve_glyphs(
        |job, _| {
            if let Ok(glyph) = job.result {
                glyphs.push(glyph);
            }
        },
        &mut Profiler,
    );

    glyphs
}

fn main() {
    let glyphs = load_glyphs();

    let el = EventLoop::new();
    let wb = WindowBuilder::new()
        .with_title("A fantastic window!")
        .with_inner_size(PhysicalSize::new(1900. as f64, 1300. as f64));

    let windowed_context = ContextBuilder::new()
        .with_gl(glutin::GlRequest::GlThenGles {
            opengl_version: (3, 2),
            opengles_version: (3, 0),
        })
        .build_windowed(wb, &el)
        .unwrap();

    let windowed_context = unsafe { windowed_context.make_current().unwrap() };

    let gl = boilerplate::load(windowed_context.context(), glyphs);

    el.run(move |event, _, control_flow| {
        *control_flow = ControlFlow::Wait;

        match event {
            Event::LoopDestroyed => (),
            Event::WindowEvent { event, .. } => match event {
                WindowEvent::Resized(physical_size) => windowed_context.resize(physical_size),
                WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
                _ => (),
            },
            Event::RedrawRequested(_) => {
                let size = windowed_context.window().inner_size();
                let scale_factor = windowed_context.window().scale_factor();
                gl.draw_frame(
                    size.width as f32,
                    size.height as f32,
                    [0., 0., 0., 1.0],
                    [1., 1., 1., 1.0],
                    scale_factor as f32,
                );
                windowed_context.swap_buffers().unwrap();
            }
            _ => (),
        }
    });
}