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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
use crate::{
asset_protocols::font::FontAsset,
components::text_instance::{HaTextElement, HaTextInstance},
material::domains::surface::SurfaceTextDomain,
math::*,
mesh::{
geometry::{
Geometry, GeometryPrimitives, GeometryTriangle, GeometryVertices,
GeometryVerticesColumn,
},
vertex_factory::StaticVertexFactory,
MeshError,
},
};
struct TextGlyph {
character: char,
page: usize,
position: Vec2,
uvs: Rect,
size: Vec2,
color: Rgba,
outline: Rgba,
// thickness: f32,
cursive_shift: f32,
baseline: f32,
}
#[derive(Debug, Copy, Clone)]
pub struct SurfaceTextFactory;
impl SurfaceTextFactory {
pub fn geometry(
text: &HaTextInstance,
font: &FontAsset,
meta: bool,
) -> Result<Geometry, MeshError> {
let count = text.glyphs_count();
let bounds_width = text.bounds_width().unwrap_or(f32::INFINITY);
let bounds_height = text.bounds_height().unwrap_or(f32::INFINITY);
let extra_y = text.lines_extra_space();
let mut line_cache = Vec::<TextGlyph>::with_capacity(count);
let mut lines = Vec::with_capacity(text.lines_count());
let mut x = 0.0;
let mut y = 0.0;
let mut width = text.bounds_width().unwrap_or(0.0);
let mut height = text.bounds_height().unwrap_or(0.0);
let mut line_width: f32 = 0.0;
let mut line_height: f32 = 0.0;
let mut line_base: f32 = 0.0;
macro_rules! move_to_new_line {
(@push) => {
{
for glyph in &mut line_cache {
glyph.position.y += line_base - glyph.baseline;
}
lines.push((
line_width,
std::mem::replace(&mut line_cache, Vec::with_capacity(count)),
));
y += line_height;
height = height.max(y);
}
};
(@reset) => {
{
x = 0.0;
line_width = 0.0;
line_height = 0.0;
line_base = 0.0;
}
};
() => {
{
move_to_new_line!(@push);
move_to_new_line!(@reset)
}
};
}
for element in text.iter() {
match element {
HaTextElement::Invalid => {}
HaTextElement::NewLine => {
move_to_new_line!();
}
HaTextElement::Glyph {
character,
size,
color,
outline,
// thickness,
cursive,
..
} => {
if let Some(c) = font.characters.get(&character) {
if let Some((page_size, _)) = font.pages_image_assets.get(c.page) {
let scale = size / font.line_height as f32;
let xadvance = c.line_advance * scale;
let yadvance = (font.line_height as f32 + extra_y) * scale;
if x + xadvance > bounds_width {
move_to_new_line!();
// TODO: use wrapping to break lines: `wrapping.can_wrap(character)`
}
if x + xadvance > bounds_width || y + yadvance > bounds_height {
break;
}
let baseline = (font.line_base as f32 + extra_y) * scale;
let size = c.image_size * scale;
let offset = c.offset * scale;
let cursive_shift = yadvance * cursive;
line_height = line_height.max(yadvance);
line_cache.push(TextGlyph {
character,
page: c.page as _,
position: Vec2::new(x, y) + offset,
uvs: Rect::new(
c.image_location.x / page_size.x,
c.image_location.y / page_size.y,
c.image_size.x / page_size.x,
c.image_size.y / page_size.y,
),
size,
color,
outline,
// thickness,
cursive_shift,
baseline,
});
x += xadvance;
line_width = line_width.max(x);
width = width.max(line_width);
line_base = line_base.max(baseline);
}
}
}
}
}
move_to_new_line!(@push);
let yalign = (height - y) * text.alignment().y;
let xpivot = width * text.pivot().x;
let ypivot = height * text.pivot().y;
for (line_width, line) in &mut lines {
let xalign = (width - *line_width) * text.alignment().x;
for glyph in line {
glyph.position.x += xalign - xpivot;
glyph.position.y += yalign - ypivot;
}
}
Ok(Geometry::new(
GeometryVertices::default().with_columns([
GeometryVerticesColumn::new(
"position",
lines
.iter()
.flat_map(|(_, glyphs)| glyphs)
.flat_map(|glyph| {
[
Vec2::new(glyph.position.x + glyph.cursive_shift, glyph.position.y),
Vec2::new(
glyph.position.x + glyph.size.x + glyph.cursive_shift,
glyph.position.y,
),
Vec2::new(
glyph.position.x + glyph.size.x - glyph.cursive_shift,
glyph.position.y + glyph.size.y,
),
Vec2::new(
glyph.position.x - glyph.cursive_shift,
glyph.position.y + glyph.size.y,
),
]
})
.collect(),
),
GeometryVerticesColumn::new(
"textureCoord",
lines
.iter()
.flat_map(|(_, glyphs)| glyphs)
.flat_map(|glyph| {
[
Vec3::new(glyph.uvs.x, glyph.uvs.y, glyph.page as _),
Vec3::new(glyph.uvs.x + glyph.uvs.w, glyph.uvs.y, glyph.page as _),
Vec3::new(
glyph.uvs.x + glyph.uvs.w,
glyph.uvs.y + glyph.uvs.h,
glyph.page as _,
),
Vec3::new(glyph.uvs.x, glyph.uvs.y + glyph.uvs.h, glyph.page as _),
]
})
.collect(),
),
GeometryVerticesColumn::new(
"color",
lines
.iter()
.flat_map(|(_, glyphs)| glyphs)
.flat_map(|glyph| {
[
Vec4::from(glyph.color),
Vec4::from(glyph.color),
Vec4::from(glyph.color),
Vec4::from(glyph.color),
]
})
.collect(),
),
GeometryVerticesColumn::new(
"outline",
lines
.iter()
.flat_map(|(_, glyphs)| glyphs)
.flat_map(|glyph| {
[
Vec4::from(glyph.outline),
Vec4::from(glyph.outline),
Vec4::from(glyph.outline),
Vec4::from(glyph.outline),
]
})
.collect(),
),
])?,
GeometryPrimitives::triangles(
lines
.iter()
.flat_map(|(_, glyphs)| glyphs)
.enumerate()
.flat_map(|(index, glyph)| {
let i = index * 4;
let mut a = GeometryTriangle::new([i, i + 1, i + 2]);
let mut b = GeometryTriangle::new([i + 2, i + 3, i]);
if meta {
a.attributes.set("index", index as i32);
b.attributes.set("index", index as i32);
a.attributes.set("character", glyph.character.to_string());
b.attributes.set("character", glyph.character.to_string());
}
[a, b]
})
.collect::<Vec<_>>(),
),
))
}
pub fn factory<T>(
text: &HaTextInstance,
font: &FontAsset,
) -> Result<StaticVertexFactory, MeshError>
where
T: SurfaceTextDomain,
{
Self::geometry(text, font, false)?.factory::<T>()
}
}