Cleanup of UV space handling
This commit is contained in:
1 parent
8d17f8e64f
commit
7b925876a5
9 files changed
+73
-118
No files matched your search
+11
-7
@@ -2,23 +2,24 @@ use std::num::NonZeroU32;
|
||||
|
||||
use crate::{
|
||||
color::Srgba,
|
||||
dpi::{Normalize, NormalizedRect},
|
||||
math::{Point, Size},
|
||||
math::{Point, Rect, Scale, Size},
|
||||
renderer::{
|
||||
image::{DataOrder, ImageData, ImageDelta},
|
||||
textures::TextureOptions,
|
||||
},
|
||||
uv::{UvRect, UvSpace},
|
||||
};
|
||||
use image::{GenericImage, RgbaImage, SubImage};
|
||||
|
||||
pub struct FontAtlasSpace;
|
||||
pub type FontAtlasPoint = Point<u32, FontAtlasSpace>;
|
||||
pub type FontAtlasSize = Size<u32, FontAtlasSpace>;
|
||||
pub type FontAtlasRect = Rect<u32, FontAtlasSpace>;
|
||||
|
||||
pub struct AllocatedGlyph<'a> {
|
||||
pub sub_image: SubImage<&'a mut RgbaImage>,
|
||||
pub layer_idx: u32,
|
||||
pub uv: NormalizedRect,
|
||||
pub uv: UvRect,
|
||||
}
|
||||
|
||||
struct Layer {
|
||||
@@ -36,6 +37,8 @@ pub struct FontAtlas {
|
||||
// atlas has been altered and needs to be reuploaded to the GPU
|
||||
// TODO: only mark changed regions as dirty and perform partial texture update
|
||||
dirty: bool,
|
||||
// used to convert to normalized UV coordinates
|
||||
to_uv: Scale<f32, FontAtlasSpace, UvSpace>,
|
||||
}
|
||||
|
||||
impl FontAtlas {
|
||||
@@ -45,6 +48,7 @@ impl FontAtlas {
|
||||
max_layers,
|
||||
layers: Vec::new(),
|
||||
dirty: false,
|
||||
to_uv: Scale::new(1.0 / layer_size as f32),
|
||||
};
|
||||
|
||||
atlas.push_layer();
|
||||
@@ -105,6 +109,9 @@ impl FontAtlas {
|
||||
layer.cursor.x += requested_size.width + PADDING;
|
||||
self.dirty = true;
|
||||
|
||||
let glyph_rect = FontAtlasRect::from_origin_and_size(pos, requested_size);
|
||||
let uv = self.to_uv.transform_box2d(&glyph_rect.cast());
|
||||
|
||||
AllocatedGlyph {
|
||||
sub_image: layer.image.sub_image(
|
||||
pos.x,
|
||||
@@ -113,10 +120,7 @@ impl FontAtlas {
|
||||
requested_size.height,
|
||||
),
|
||||
layer_idx: idx as u32,
|
||||
uv: NormalizedRect::new(
|
||||
pos.normalize(self.layer_size),
|
||||
(pos + requested_size.to_vector()).normalize(self.layer_size),
|
||||
),
|
||||
uv,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+3
-2
@@ -1,6 +1,7 @@
|
||||
use crate::{
|
||||
color::Srgba,
|
||||
dpi::{LogicalPoint, LogicalRect, NormalizedRect},
|
||||
dpi::{LogicalPoint, LogicalRect},
|
||||
uv::UvRect,
|
||||
};
|
||||
use ordered_float::OrderedFloat;
|
||||
use parley::{FontFamily, FontFamilyName};
|
||||
@@ -106,7 +107,7 @@ pub struct PositionedGlyph {
|
||||
// relative to layout origin
|
||||
// TODO: introduce new "layout space"?
|
||||
pub rect: LogicalRect<f32>,
|
||||
pub uv: NormalizedRect,
|
||||
pub uv: UvRect,
|
||||
pub layer_idx: u32,
|
||||
pub color: Srgba,
|
||||
}
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
use crate::{
|
||||
color::Srgba,
|
||||
dpi::{
|
||||
LogicalRect, LogicalVector, NormalizedRect, PhysicalPoint, PhysicalRect, PhysicalVector,
|
||||
ScaleFactor,
|
||||
},
|
||||
dpi::{LogicalRect, LogicalVector, PhysicalPoint, PhysicalRect, PhysicalVector, ScaleFactor},
|
||||
fonts::{atlas::FontAtlas, glyph_key::GlyphKey},
|
||||
math::Size,
|
||||
uv::UvRect,
|
||||
};
|
||||
use ahash::HashMap;
|
||||
use image::GenericImage;
|
||||
@@ -52,7 +50,7 @@ impl<'a> StyleKey<'a> {
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct CachedGlyph {
|
||||
pub rect: PhysicalRect<u32>,
|
||||
pub uv: NormalizedRect,
|
||||
pub uv: UvRect,
|
||||
pub texture_layer_idx: u32,
|
||||
// offset from top/left to baseline
|
||||
pub baseline_offset: PhysicalVector<i32>,
|
||||
@@ -61,7 +59,7 @@ pub struct CachedGlyph {
|
||||
pub struct RasterizedGlyph {
|
||||
// NOTE: this is relative to the layout origin
|
||||
pub rect: LogicalRect<f32>,
|
||||
pub uv: NormalizedRect,
|
||||
pub uv: UvRect,
|
||||
pub texture_layer_idx: u32,
|
||||
pub color: Srgba,
|
||||
}
|
||||
@@ -244,7 +242,7 @@ impl GlyphRasterizer {
|
||||
fn write_to_atlas(
|
||||
image: &swash::scale::image::Image,
|
||||
atlas: &mut FontAtlas,
|
||||
) -> (PhysicalRect<u32>, NormalizedRect, u32) {
|
||||
) -> (PhysicalRect<u32>, UvRect, u32) {
|
||||
let mut allocated_glyph =
|
||||
atlas.allocate(Size::new(image.placement.width, image.placement.height));
|
||||
|
||||
|
||||
Reference in new issue
Block a user