Skip to content

Commit

Permalink
core: Make a glyph shape handle Optional, for ie whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinnerbone authored and adrian17 committed Sep 8, 2023
1 parent bcf4762 commit f092fa5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 38 deletions.
57 changes: 29 additions & 28 deletions core/src/display_object/edit_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -918,37 +918,38 @@ impl<'gc> EditText<'gc> {
self.text_transform(color, baseline_adjustment),
params,
|pos, transform, glyph: &Glyph, advance, x| {
// If it's highlighted, override the color.
match visible_selection {
Some(visible_selection) if visible_selection.contains(start + pos) => {
// Draw black selection rect
let selection_box = context.transform_stack.transform().matrix
* Matrix::create_box(
advance.to_pixels() as f32,
params.height().to_pixels() as f32,
0.0,
x + Twips::from_pixels(-1.0),
Twips::from_pixels(2.0),
);
context.commands.draw_rect(Color::BLACK, selection_box);
if let Some(glyph_shape_handle) = glyph.shape_handle(context.renderer) {
// If it's highlighted, override the color.
match visible_selection {
Some(visible_selection) if visible_selection.contains(start + pos) => {
// Draw black selection rect
let selection_box = context.transform_stack.transform().matrix
* Matrix::create_box(
advance.to_pixels() as f32,
params.height().to_pixels() as f32,
0.0,
x + Twips::from_pixels(-1.0),
Twips::from_pixels(2.0),
);
context.commands.draw_rect(Color::BLACK, selection_box);

// Set text color to white
context.transform_stack.push(&Transform {
matrix: transform.matrix,
color_transform: ColorTransform::IDENTITY,
});
}
_ => {
context.transform_stack.push(transform);
// Set text color to white
context.transform_stack.push(&Transform {
matrix: transform.matrix,
color_transform: ColorTransform::IDENTITY,
});
}
_ => {
context.transform_stack.push(transform);
}
}
}

// Render glyph.
let glyph_shape_handle = glyph.shape_handle(context.renderer);
context
.commands
.render_shape(glyph_shape_handle, context.transform_stack.transform());
context.transform_stack.pop();
// Render glyph.
context
.commands
.render_shape(glyph_shape_handle, context.transform_stack.transform());
context.transform_stack.pop();
}

if let Some((caret_pos, length)) = caret {
if caret_pos == pos {
Expand Down
15 changes: 9 additions & 6 deletions core/src/display_object/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,15 @@ impl<'gc> TDisplayObject<'gc> for Text<'gc> {
transform.color_transform.set_mult_color(&color);
for c in &block.glyphs {
if let Some(glyph) = font.get_glyph(c.index as usize) {
context.transform_stack.push(&transform);
let glyph_shape_handle = glyph.shape_handle(context.renderer);
context
.commands
.render_shape(glyph_shape_handle, context.transform_stack.transform());
context.transform_stack.pop();
if let Some(glyph_shape_handle) = glyph.shape_handle(context.renderer) {
context.transform_stack.push(&transform);
context.commands.render_shape(
glyph_shape_handle,
context.transform_stack.transform(),
);
context.transform_stack.pop();
}

transform.matrix.tx += Twips::new(c.advance);
}
}
Expand Down
9 changes: 5 additions & 4 deletions core/src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,11 +505,11 @@ impl GlyphShape {
}
}

pub fn register(&self, renderer: &mut dyn RenderBackend) -> ShapeHandle {
pub fn register(&self, renderer: &mut dyn RenderBackend) -> Option<ShapeHandle> {
match self {
GlyphShape::Swf(glyph) => {
let mut glyph = glyph.borrow_mut();
renderer.register_shape((&*glyph.shape()).into(), &NullBitmapSource)
Some(renderer.register_shape((&*glyph.shape()).into(), &NullBitmapSource))
}
}
}
Expand All @@ -519,14 +519,15 @@ impl GlyphShape {
pub struct Glyph {
// Handle to registered shape.
// If None, it'll be loaded lazily on first render of this glyph.
shape_handle: RefCell<Option<ShapeHandle>>,
// It's a double option; the outer one is "have we registered", the inner one is option because it may not exist
shape_handle: RefCell<Option<Option<ShapeHandle>>>,

shape: GlyphShape,
advance: Twips,
}

impl Glyph {
pub fn shape_handle(&self, renderer: &mut dyn RenderBackend) -> ShapeHandle {
pub fn shape_handle(&self, renderer: &mut dyn RenderBackend) -> Option<ShapeHandle> {
self.shape_handle
.borrow_mut()
.get_or_insert_with(|| self.shape.register(renderer))
Expand Down

0 comments on commit f092fa5

Please sign in to comment.