2020-05-24 09:36:24 +00:00
|
|
|
|
use std::sync::Arc;
|
2019-01-12 23:19:53 +00:00
|
|
|
|
|
2020-05-24 09:36:24 +00:00
|
|
|
|
use {
|
|
|
|
|
ahash::AHashMap,
|
2020-10-13 22:29:11 +00:00
|
|
|
|
parking_lot::RwLock,
|
2020-05-24 09:36:24 +00:00
|
|
|
|
rusttype::{point, Scale},
|
|
|
|
|
};
|
2019-01-04 13:14:32 +00:00
|
|
|
|
|
2020-10-13 22:29:11 +00:00
|
|
|
|
use crate::{
|
|
|
|
|
math::{vec2, Vec2},
|
|
|
|
|
mutex::Mutex,
|
2020-11-13 00:23:36 +00:00
|
|
|
|
paint::{Galley, Row},
|
2020-10-13 22:29:11 +00:00
|
|
|
|
};
|
2020-05-19 20:28:57 +00:00
|
|
|
|
|
|
|
|
|
use super::texture_atlas::TextureAtlas;
|
2019-01-10 20:09:37 +00:00
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
2020-05-16 17:38:46 +00:00
|
|
|
|
// const REPLACEMENT_CHAR: char = '\u{25A1}'; // □ white square Replaces a missing or unsupported Unicode character.
|
|
|
|
|
// const REPLACEMENT_CHAR: char = '\u{FFFD}'; // <20> REPLACEMENT CHARACTER
|
|
|
|
|
const REPLACEMENT_CHAR: char = '?';
|
|
|
|
|
|
2019-01-19 16:10:28 +00:00
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
2019-01-05 20:23:53 +00:00
|
|
|
|
pub struct UvRect {
|
2019-01-19 16:10:28 +00:00
|
|
|
|
/// X/Y offset for nice rendering (unit: points).
|
|
|
|
|
pub offset: Vec2,
|
|
|
|
|
pub size: Vec2,
|
2019-01-04 13:14:32 +00:00
|
|
|
|
|
2019-01-19 16:10:28 +00:00
|
|
|
|
/// Top left corner UV in texture.
|
2019-01-05 21:55:09 +00:00
|
|
|
|
pub min: (u16, u16),
|
2019-01-04 13:14:32 +00:00
|
|
|
|
|
2019-01-19 16:10:28 +00:00
|
|
|
|
/// Bottom right corner (exclusive).
|
2019-01-05 21:55:09 +00:00
|
|
|
|
pub max: (u16, u16),
|
2019-01-04 13:14:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-19 16:10:28 +00:00
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
2019-01-05 20:23:53 +00:00
|
|
|
|
pub struct GlyphInfo {
|
|
|
|
|
id: rusttype::GlyphId,
|
|
|
|
|
|
2019-01-19 16:10:28 +00:00
|
|
|
|
/// Unit: points.
|
2019-01-05 20:23:53 +00:00
|
|
|
|
pub advance_width: f32,
|
|
|
|
|
|
|
|
|
|
/// Texture coordinates. None for space.
|
2020-04-18 15:09:01 +00:00
|
|
|
|
pub uv_rect: Option<UvRect>,
|
2019-01-05 20:23:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-19 16:10:28 +00:00
|
|
|
|
/// The interface uses points as the unit for everything.
|
2019-01-04 13:14:32 +00:00
|
|
|
|
pub struct Font {
|
2019-01-05 20:23:53 +00:00
|
|
|
|
font: rusttype::Font<'static>,
|
2019-01-04 13:14:32 +00:00
|
|
|
|
/// Maximum character height
|
2019-01-19 16:10:28 +00:00
|
|
|
|
scale_in_pixels: f32,
|
|
|
|
|
pixels_per_point: f32,
|
2020-09-09 12:24:13 +00:00
|
|
|
|
replacement_glyph_info: GlyphInfo,
|
|
|
|
|
glyph_infos: RwLock<AHashMap<char, GlyphInfo>>,
|
2019-01-12 23:19:53 +00:00
|
|
|
|
atlas: Arc<Mutex<TextureAtlas>>,
|
2019-01-04 13:14:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Font {
|
2019-01-19 16:10:28 +00:00
|
|
|
|
pub fn new(
|
|
|
|
|
atlas: Arc<Mutex<TextureAtlas>>,
|
|
|
|
|
font_data: &'static [u8],
|
|
|
|
|
scale_in_points: f32,
|
|
|
|
|
pixels_per_point: f32,
|
|
|
|
|
) -> Font {
|
2020-09-09 10:14:53 +00:00
|
|
|
|
assert!(scale_in_points > 0.0);
|
|
|
|
|
assert!(pixels_per_point > 0.0);
|
|
|
|
|
|
2020-04-24 17:07:33 +00:00
|
|
|
|
let font = rusttype::Font::try_from_bytes(font_data).expect("Error constructing Font");
|
2019-01-19 16:10:28 +00:00
|
|
|
|
let scale_in_pixels = pixels_per_point * scale_in_points;
|
2019-01-04 13:14:32 +00:00
|
|
|
|
|
2020-09-09 12:24:13 +00:00
|
|
|
|
let replacement_glyph_info = allocate_glyph(
|
|
|
|
|
&mut atlas.lock(),
|
|
|
|
|
REPLACEMENT_CHAR,
|
|
|
|
|
&font,
|
|
|
|
|
scale_in_pixels,
|
|
|
|
|
pixels_per_point,
|
|
|
|
|
)
|
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
|
panic!(
|
|
|
|
|
"Failed to find replacement character {:?}",
|
|
|
|
|
REPLACEMENT_CHAR
|
|
|
|
|
)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let font = Font {
|
2019-01-05 20:23:53 +00:00
|
|
|
|
font,
|
2019-01-19 16:10:28 +00:00
|
|
|
|
scale_in_pixels,
|
|
|
|
|
pixels_per_point,
|
2020-09-09 12:24:13 +00:00
|
|
|
|
replacement_glyph_info,
|
2020-04-18 15:09:01 +00:00
|
|
|
|
glyph_infos: Default::default(),
|
2019-01-04 13:14:32 +00:00
|
|
|
|
atlas,
|
2020-04-18 15:09:01 +00:00
|
|
|
|
};
|
|
|
|
|
|
2020-09-09 12:24:13 +00:00
|
|
|
|
font.glyph_infos
|
|
|
|
|
.write()
|
|
|
|
|
.insert(REPLACEMENT_CHAR, font.replacement_glyph_info);
|
|
|
|
|
|
|
|
|
|
// Preload the printable ASCII characters [32, 126] (which excludes control codes):
|
2020-04-18 15:09:01 +00:00
|
|
|
|
const FIRST_ASCII: usize = 32; // 32 == space
|
|
|
|
|
const LAST_ASCII: usize = 126;
|
|
|
|
|
for c in (FIRST_ASCII..=LAST_ASCII).map(|c| c as u8 as char) {
|
2020-09-09 12:24:13 +00:00
|
|
|
|
font.glyph_info(c);
|
2019-01-04 13:14:32 +00:00
|
|
|
|
}
|
2020-09-09 12:24:13 +00:00
|
|
|
|
font.glyph_info('°');
|
2020-05-16 17:38:46 +00:00
|
|
|
|
|
2020-04-18 15:09:01 +00:00
|
|
|
|
font
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-01 00:08:01 +00:00
|
|
|
|
pub fn round_to_pixel(&self, point: f32) -> f32 {
|
|
|
|
|
(point * self.pixels_per_point).round() / self.pixels_per_point
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-13 00:23:36 +00:00
|
|
|
|
/// Height of one row of text. In points
|
|
|
|
|
pub fn row_height(&self) -> f32 {
|
2020-05-01 00:08:01 +00:00
|
|
|
|
self.scale_in_pixels / self.pixels_per_point
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn uv_rect(&self, c: char) -> Option<UvRect> {
|
2020-09-09 12:24:13 +00:00
|
|
|
|
self.glyph_infos.read().get(&c).and_then(|gi| gi.uv_rect)
|
2020-05-16 17:38:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-08 21:09:32 +00:00
|
|
|
|
pub fn glyph_width(&self, c: char) -> f32 {
|
|
|
|
|
self.glyph_info(c).advance_width
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-08 20:09:33 +00:00
|
|
|
|
/// `\n` will (intentionally) show up as '?' (`REPLACEMENT_CHAR`)
|
2020-09-09 12:24:13 +00:00
|
|
|
|
fn glyph_info(&self, c: char) -> GlyphInfo {
|
|
|
|
|
{
|
|
|
|
|
if let Some(glyph_info) = self.glyph_infos.read().get(&c) {
|
|
|
|
|
return *glyph_info;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-18 15:09:01 +00:00
|
|
|
|
|
2020-09-09 12:24:13 +00:00
|
|
|
|
// Add new character:
|
|
|
|
|
let glyph_info = allocate_glyph(
|
|
|
|
|
&mut self.atlas.lock(),
|
2020-04-18 15:09:01 +00:00
|
|
|
|
c,
|
2020-09-09 12:24:13 +00:00
|
|
|
|
&self.font,
|
|
|
|
|
self.scale_in_pixels,
|
|
|
|
|
self.pixels_per_point,
|
2020-04-18 15:09:01 +00:00
|
|
|
|
);
|
2020-09-09 12:24:13 +00:00
|
|
|
|
// debug_assert!(glyph_info.is_some(), "Failed to find {:?}", c);
|
|
|
|
|
let glyph_info = glyph_info.unwrap_or(self.replacement_glyph_info);
|
|
|
|
|
self.glyph_infos.write().insert(c, glyph_info);
|
|
|
|
|
glyph_info
|
2019-01-04 13:14:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-13 00:23:36 +00:00
|
|
|
|
/// Typeset the given text onto one row.
|
2020-11-08 20:09:33 +00:00
|
|
|
|
/// Any `\n` will show up as `REPLACEMENT_CHAR` ('?').
|
2020-11-13 00:23:36 +00:00
|
|
|
|
/// Always returns exactly one `Row` in the `Galley`.
|
2020-05-20 19:22:42 +00:00
|
|
|
|
pub fn layout_single_line(&self, text: String) -> Galley {
|
2020-11-13 00:23:36 +00:00
|
|
|
|
let x_offsets = self.layout_single_row_fragment(&text);
|
|
|
|
|
let row = Row {
|
2020-05-16 18:05:52 +00:00
|
|
|
|
x_offsets,
|
|
|
|
|
y_min: 0.0,
|
2020-11-13 00:23:36 +00:00
|
|
|
|
y_max: self.row_height(),
|
2020-05-16 18:54:01 +00:00
|
|
|
|
ends_with_newline: false,
|
2020-05-16 18:05:52 +00:00
|
|
|
|
};
|
2020-11-13 00:23:36 +00:00
|
|
|
|
let width = row.max_x();
|
|
|
|
|
let size = vec2(width, self.row_height());
|
2020-05-16 18:05:52 +00:00
|
|
|
|
let galley = Galley {
|
2020-05-20 19:22:42 +00:00
|
|
|
|
text,
|
2020-11-13 00:23:36 +00:00
|
|
|
|
rows: vec![row],
|
2020-05-16 18:05:52 +00:00
|
|
|
|
size,
|
|
|
|
|
};
|
|
|
|
|
galley.sanity_check();
|
|
|
|
|
galley
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-20 19:22:42 +00:00
|
|
|
|
pub fn layout_multiline(&self, text: String, max_width_in_points: f32) -> Galley {
|
2020-11-13 22:22:22 +00:00
|
|
|
|
let row_height = self.row_height();
|
2020-05-16 18:05:52 +00:00
|
|
|
|
let mut cursor_y = 0.0;
|
2020-11-13 00:23:36 +00:00
|
|
|
|
let mut rows = Vec::new();
|
2020-05-16 18:05:52 +00:00
|
|
|
|
|
|
|
|
|
let mut paragraph_start = 0;
|
|
|
|
|
|
|
|
|
|
while paragraph_start < text.len() {
|
|
|
|
|
let next_newline = text[paragraph_start..].find('\n');
|
|
|
|
|
let paragraph_end = next_newline
|
2020-11-08 20:09:33 +00:00
|
|
|
|
.map(|newline| paragraph_start + newline)
|
2020-05-16 18:05:52 +00:00
|
|
|
|
.unwrap_or_else(|| text.len());
|
|
|
|
|
|
2020-11-08 20:09:33 +00:00
|
|
|
|
assert!(paragraph_start <= paragraph_end);
|
2020-05-16 18:05:52 +00:00
|
|
|
|
let paragraph_text = &text[paragraph_start..paragraph_end];
|
2020-11-13 00:23:36 +00:00
|
|
|
|
let mut paragraph_rows =
|
2020-05-16 18:05:52 +00:00
|
|
|
|
self.layout_paragraph_max_width(paragraph_text, max_width_in_points);
|
2020-11-13 00:23:36 +00:00
|
|
|
|
assert!(!paragraph_rows.is_empty());
|
|
|
|
|
paragraph_rows.last_mut().unwrap().ends_with_newline = next_newline.is_some();
|
2020-05-16 18:05:52 +00:00
|
|
|
|
|
2020-11-13 00:23:36 +00:00
|
|
|
|
for row in &mut paragraph_rows {
|
|
|
|
|
row.y_min += cursor_y;
|
|
|
|
|
row.y_max += cursor_y;
|
2020-05-16 18:05:52 +00:00
|
|
|
|
}
|
2020-11-13 00:23:36 +00:00
|
|
|
|
cursor_y = paragraph_rows.last().unwrap().y_max;
|
2020-11-13 22:22:22 +00:00
|
|
|
|
cursor_y += row_height * 0.4; // Extra spacing between paragraphs. TODO: less hacky
|
2020-05-16 18:05:52 +00:00
|
|
|
|
|
2020-11-13 00:23:36 +00:00
|
|
|
|
rows.append(&mut paragraph_rows);
|
2020-05-16 18:05:52 +00:00
|
|
|
|
|
2020-11-08 20:09:33 +00:00
|
|
|
|
paragraph_start = paragraph_end + 1;
|
2020-05-16 18:05:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if text.is_empty() || text.ends_with('\n') {
|
2020-11-13 00:23:36 +00:00
|
|
|
|
rows.push(Row {
|
2020-05-16 18:05:52 +00:00
|
|
|
|
x_offsets: vec![0.0],
|
|
|
|
|
y_min: cursor_y,
|
2020-11-13 22:22:22 +00:00
|
|
|
|
y_max: cursor_y + row_height,
|
2020-11-08 20:09:33 +00:00
|
|
|
|
ends_with_newline: false,
|
2020-05-16 18:05:52 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-13 00:23:36 +00:00
|
|
|
|
let mut widest_row = 0.0;
|
|
|
|
|
for row in &rows {
|
|
|
|
|
widest_row = row.max_x().max(widest_row);
|
2020-05-16 18:05:52 +00:00
|
|
|
|
}
|
2020-11-13 00:23:36 +00:00
|
|
|
|
let size = vec2(widest_row, rows.last().unwrap().y_max);
|
2020-05-16 18:05:52 +00:00
|
|
|
|
|
2020-11-13 00:23:36 +00:00
|
|
|
|
let galley = Galley { text, rows, size };
|
2020-05-16 18:05:52 +00:00
|
|
|
|
galley.sanity_check();
|
|
|
|
|
galley
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-13 00:23:36 +00:00
|
|
|
|
/// Typeset the given text onto one row.
|
2020-11-08 20:09:33 +00:00
|
|
|
|
/// Assumes there are no `\n` in the text.
|
2020-05-30 09:16:31 +00:00
|
|
|
|
/// Return `x_offsets`, one longer than the number of characters in the text.
|
2020-11-13 00:23:36 +00:00
|
|
|
|
fn layout_single_row_fragment(&self, text: &str) -> Vec<f32> {
|
2019-01-19 16:10:28 +00:00
|
|
|
|
let scale_in_pixels = Scale::uniform(self.scale_in_pixels);
|
2019-01-05 20:23:53 +00:00
|
|
|
|
|
2020-05-20 19:22:42 +00:00
|
|
|
|
let mut x_offsets = Vec::with_capacity(text.chars().count() + 1);
|
|
|
|
|
x_offsets.push(0.0);
|
|
|
|
|
|
2019-01-19 16:10:28 +00:00
|
|
|
|
let mut cursor_x_in_points = 0.0f32;
|
2019-01-05 20:23:53 +00:00
|
|
|
|
let mut last_glyph_id = None;
|
2019-01-10 20:09:37 +00:00
|
|
|
|
|
2019-01-05 14:28:07 +00:00
|
|
|
|
for c in text.chars() {
|
2020-09-09 12:24:13 +00:00
|
|
|
|
let glyph = self.glyph_info(c);
|
2020-05-16 17:38:46 +00:00
|
|
|
|
|
|
|
|
|
if let Some(last_glyph_id) = last_glyph_id {
|
|
|
|
|
cursor_x_in_points +=
|
|
|
|
|
self.font
|
|
|
|
|
.pair_kerning(scale_in_pixels, last_glyph_id, glyph.id)
|
|
|
|
|
/ self.pixels_per_point
|
2019-01-05 20:23:53 +00:00
|
|
|
|
}
|
2020-05-16 17:38:46 +00:00
|
|
|
|
cursor_x_in_points += glyph.advance_width;
|
|
|
|
|
cursor_x_in_points = self.round_to_pixel(cursor_x_in_points);
|
|
|
|
|
last_glyph_id = Some(glyph.id);
|
|
|
|
|
|
2020-05-16 15:28:15 +00:00
|
|
|
|
x_offsets.push(cursor_x_in_points);
|
2019-01-05 14:28:07 +00:00
|
|
|
|
}
|
2019-01-10 20:09:37 +00:00
|
|
|
|
|
2020-05-16 15:28:15 +00:00
|
|
|
|
x_offsets
|
2020-05-16 09:27:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-18 15:09:01 +00:00
|
|
|
|
/// A paragraph is text with no line break character in it.
|
2020-11-08 20:09:33 +00:00
|
|
|
|
/// The text will be wrapped by the given `max_width_in_points`.
|
2020-11-13 00:23:36 +00:00
|
|
|
|
fn layout_paragraph_max_width(&self, text: &str, max_width_in_points: f32) -> Vec<Row> {
|
2020-11-08 20:09:33 +00:00
|
|
|
|
if text == "" {
|
2020-11-13 00:23:36 +00:00
|
|
|
|
return vec![Row {
|
2020-11-08 20:09:33 +00:00
|
|
|
|
x_offsets: vec![0.0],
|
|
|
|
|
y_min: 0.0,
|
2020-11-13 00:23:36 +00:00
|
|
|
|
y_max: self.row_height(),
|
2020-11-08 20:09:33 +00:00
|
|
|
|
ends_with_newline: false,
|
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-13 00:23:36 +00:00
|
|
|
|
let full_x_offsets = self.layout_single_row_fragment(text);
|
2019-01-10 20:09:37 +00:00
|
|
|
|
|
2020-11-13 00:23:36 +00:00
|
|
|
|
let mut row_start_x = full_x_offsets[0];
|
2020-05-30 09:16:31 +00:00
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
#![allow(clippy::float_cmp)]
|
2020-11-13 00:23:36 +00:00
|
|
|
|
assert_eq!(row_start_x, 0.0);
|
2020-05-30 09:16:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-10 20:09:37 +00:00
|
|
|
|
let mut cursor_y = 0.0;
|
2020-11-13 00:23:36 +00:00
|
|
|
|
let mut row_start_idx = 0;
|
2020-05-16 15:28:15 +00:00
|
|
|
|
|
2020-11-13 00:23:36 +00:00
|
|
|
|
// start index of the last space. A candidate for a new row.
|
2020-05-16 15:28:15 +00:00
|
|
|
|
let mut last_space = None;
|
|
|
|
|
|
2020-11-13 00:23:36 +00:00
|
|
|
|
let mut out_rows = vec![];
|
2020-05-16 15:28:15 +00:00
|
|
|
|
|
|
|
|
|
for (i, (x, chr)) in full_x_offsets.iter().skip(1).zip(text.chars()).enumerate() {
|
2020-11-08 20:09:33 +00:00
|
|
|
|
debug_assert!(chr != '\n');
|
2020-11-13 00:23:36 +00:00
|
|
|
|
let potential_row_width = x - row_start_x;
|
2020-05-16 15:28:15 +00:00
|
|
|
|
|
2020-11-13 00:23:36 +00:00
|
|
|
|
if potential_row_width > max_width_in_points {
|
2020-05-16 15:28:15 +00:00
|
|
|
|
if let Some(last_space_idx) = last_space {
|
|
|
|
|
let include_trailing_space = true;
|
2020-11-13 00:23:36 +00:00
|
|
|
|
let row = if include_trailing_space {
|
|
|
|
|
Row {
|
|
|
|
|
x_offsets: full_x_offsets[row_start_idx..=last_space_idx + 1]
|
2020-05-16 15:28:15 +00:00
|
|
|
|
.iter()
|
2020-11-13 00:23:36 +00:00
|
|
|
|
.map(|x| x - row_start_x)
|
2020-05-16 15:28:15 +00:00
|
|
|
|
.collect(),
|
2020-05-16 18:05:52 +00:00
|
|
|
|
y_min: cursor_y,
|
2020-11-13 00:23:36 +00:00
|
|
|
|
y_max: cursor_y + self.row_height(),
|
2020-11-08 20:09:33 +00:00
|
|
|
|
ends_with_newline: false,
|
2020-05-16 15:28:15 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2020-11-13 00:23:36 +00:00
|
|
|
|
Row {
|
|
|
|
|
x_offsets: full_x_offsets[row_start_idx..=last_space_idx]
|
2020-05-16 15:28:15 +00:00
|
|
|
|
.iter()
|
2020-11-13 00:23:36 +00:00
|
|
|
|
.map(|x| x - row_start_x)
|
2020-05-16 15:28:15 +00:00
|
|
|
|
.collect(),
|
2020-05-16 18:05:52 +00:00
|
|
|
|
y_min: cursor_y,
|
2020-11-13 00:23:36 +00:00
|
|
|
|
y_max: cursor_y + self.row_height(),
|
2020-11-08 20:09:33 +00:00
|
|
|
|
ends_with_newline: false,
|
2020-05-16 15:28:15 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
2020-11-13 00:23:36 +00:00
|
|
|
|
row.sanity_check();
|
|
|
|
|
out_rows.push(row);
|
2020-05-16 15:28:15 +00:00
|
|
|
|
|
2020-11-13 00:23:36 +00:00
|
|
|
|
row_start_idx = last_space_idx + 1;
|
|
|
|
|
row_start_x = full_x_offsets[row_start_idx];
|
2020-05-16 15:28:15 +00:00
|
|
|
|
last_space = None;
|
2020-11-13 00:23:36 +00:00
|
|
|
|
cursor_y += self.row_height();
|
2020-05-16 15:28:15 +00:00
|
|
|
|
cursor_y = self.round_to_pixel(cursor_y);
|
|
|
|
|
}
|
2019-01-10 20:09:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-16 15:28:15 +00:00
|
|
|
|
const NON_BREAKING_SPACE: char = '\u{A0}';
|
|
|
|
|
if chr.is_whitespace() && chr != NON_BREAKING_SPACE {
|
|
|
|
|
last_space = Some(i);
|
2019-01-10 20:09:37 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-13 00:23:36 +00:00
|
|
|
|
if row_start_idx + 1 < full_x_offsets.len() {
|
|
|
|
|
let row = Row {
|
|
|
|
|
x_offsets: full_x_offsets[row_start_idx..]
|
2020-05-16 15:28:15 +00:00
|
|
|
|
.iter()
|
2020-11-13 00:23:36 +00:00
|
|
|
|
.map(|x| x - row_start_x)
|
2020-05-16 15:28:15 +00:00
|
|
|
|
.collect(),
|
2020-05-16 18:05:52 +00:00
|
|
|
|
y_min: cursor_y,
|
2020-11-13 00:23:36 +00:00
|
|
|
|
y_max: cursor_y + self.row_height(),
|
2020-11-08 20:09:33 +00:00
|
|
|
|
ends_with_newline: false,
|
2020-05-16 15:28:15 +00:00
|
|
|
|
};
|
2020-11-13 00:23:36 +00:00
|
|
|
|
row.sanity_check();
|
|
|
|
|
out_rows.push(row);
|
2020-05-16 15:28:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-13 00:23:36 +00:00
|
|
|
|
out_rows
|
2019-01-10 20:09:37 +00:00
|
|
|
|
}
|
2019-01-04 13:14:32 +00:00
|
|
|
|
}
|
2020-09-09 12:24:13 +00:00
|
|
|
|
|
|
|
|
|
fn allocate_glyph(
|
|
|
|
|
atlas: &mut TextureAtlas,
|
|
|
|
|
c: char,
|
|
|
|
|
font: &rusttype::Font<'static>,
|
|
|
|
|
scale_in_pixels: f32,
|
|
|
|
|
pixels_per_point: f32,
|
|
|
|
|
) -> Option<GlyphInfo> {
|
|
|
|
|
let glyph = font.glyph(c);
|
|
|
|
|
if glyph.id().0 == 0 {
|
|
|
|
|
return None; // Failed to find a glyph for the character
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let glyph = glyph.scaled(Scale::uniform(scale_in_pixels));
|
|
|
|
|
let glyph = glyph.positioned(point(0.0, 0.0));
|
|
|
|
|
|
|
|
|
|
let uv_rect = if let Some(bb) = glyph.pixel_bounding_box() {
|
|
|
|
|
let glyph_width = bb.width() as usize;
|
|
|
|
|
let glyph_height = bb.height() as usize;
|
|
|
|
|
assert!(glyph_width >= 1);
|
|
|
|
|
assert!(glyph_height >= 1);
|
|
|
|
|
|
|
|
|
|
let glyph_pos = atlas.allocate((glyph_width, glyph_height));
|
|
|
|
|
|
|
|
|
|
let texture = atlas.texture_mut();
|
|
|
|
|
glyph.draw(|x, y, v| {
|
|
|
|
|
if v > 0.0 {
|
|
|
|
|
let px = glyph_pos.0 + x as usize;
|
|
|
|
|
let py = glyph_pos.1 + y as usize;
|
|
|
|
|
texture[(px, py)] = (v * 255.0).round() as u8;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let offset_y_in_pixels = scale_in_pixels as f32 + bb.min.y as f32 - 4.0 * pixels_per_point; // TODO: use font.v_metrics
|
|
|
|
|
Some(UvRect {
|
|
|
|
|
offset: vec2(
|
|
|
|
|
bb.min.x as f32 / pixels_per_point,
|
|
|
|
|
offset_y_in_pixels / pixels_per_point,
|
|
|
|
|
),
|
|
|
|
|
size: vec2(glyph_width as f32, glyph_height as f32) / pixels_per_point,
|
|
|
|
|
min: (glyph_pos.0 as u16, glyph_pos.1 as u16),
|
|
|
|
|
max: (
|
|
|
|
|
(glyph_pos.0 + glyph_width) as u16,
|
|
|
|
|
(glyph_pos.1 + glyph_height) as u16,
|
|
|
|
|
),
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
// No bounding box. Maybe a space?
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let advance_width_in_points = glyph.unpositioned().h_metrics().advance_width / pixels_per_point;
|
|
|
|
|
|
|
|
|
|
Some(GlyphInfo {
|
|
|
|
|
id: glyph.id(),
|
|
|
|
|
advance_width: advance_width_in_points,
|
|
|
|
|
uv_rect,
|
|
|
|
|
})
|
|
|
|
|
}
|