Add byte information to PositionedGlyph (#17900)
# Objective Adds information about the byte length and index of a glyph to `PositionedGlyph`. Useful for text picking, allows for picking with multi-byte characters. Also adds a `line` field that helps with converting back and forth from cosmic's `Cursor`. ## Solution Retrieve the relevant data from cosmic and add it to the glyph in the text pipeline. ## Testing `cargo r -p ci` --- ## Migration Guide `PositionedGlyph::new()` has been removed as there is no longer an unused field. Create new `PositionedGlyph`s directly.
This commit is contained in:
parent
ab38b61001
commit
64d57fad08
@ -20,24 +20,12 @@ pub struct PositionedGlyph {
|
||||
pub atlas_info: GlyphAtlasInfo,
|
||||
/// The index of the glyph in the [`ComputedTextBlock`](crate::ComputedTextBlock)'s tracked spans.
|
||||
pub span_index: usize,
|
||||
/// TODO: In order to do text editing, we need access to the size of glyphs and their index in the associated String.
|
||||
/// For example, to figure out where to place the cursor in an input box from the mouse's position.
|
||||
/// Without this, it's only possible in texts where each glyph is one byte. Cosmic text has methods for this
|
||||
/// cosmic-texts [hit detection](https://pop-os.github.io/cosmic-text/cosmic_text/struct.Buffer.html#method.hit)
|
||||
byte_index: usize,
|
||||
}
|
||||
|
||||
impl PositionedGlyph {
|
||||
/// Creates a new [`PositionedGlyph`]
|
||||
pub fn new(position: Vec2, size: Vec2, atlas_info: GlyphAtlasInfo, span_index: usize) -> Self {
|
||||
Self {
|
||||
position,
|
||||
size,
|
||||
atlas_info,
|
||||
span_index,
|
||||
byte_index: 0,
|
||||
}
|
||||
}
|
||||
/// The index of the glyph's line.
|
||||
pub line_index: usize,
|
||||
/// The byte index of the glyph in it's line.
|
||||
pub byte_index: usize,
|
||||
/// The byte length of the glyph.
|
||||
pub byte_length: usize,
|
||||
}
|
||||
|
||||
/// Information about a glyph in an atlas.
|
||||
|
||||
@ -263,14 +263,12 @@ impl TextPipeline {
|
||||
let buffer = &mut computed.buffer;
|
||||
let box_size = buffer_dimensions(buffer);
|
||||
|
||||
let result = buffer
|
||||
.layout_runs()
|
||||
.flat_map(|run| {
|
||||
run.glyphs
|
||||
let result = buffer.layout_runs().try_for_each(|run| {
|
||||
let result = run
|
||||
.glyphs
|
||||
.iter()
|
||||
.map(move |layout_glyph| (layout_glyph, run.line_y))
|
||||
})
|
||||
.try_for_each(|(layout_glyph, line_y)| {
|
||||
.map(move |layout_glyph| (layout_glyph, run.line_y, run.line_i))
|
||||
.try_for_each(|(layout_glyph, line_y, line_i)| {
|
||||
let mut temp_glyph;
|
||||
let span_index = layout_glyph.metadata;
|
||||
let font_id = glyph_info[span_index].0;
|
||||
@ -319,7 +317,8 @@ impl TextPipeline {
|
||||
|
||||
// offset by half the size because the origin is center
|
||||
let x = glyph_size.x as f32 / 2.0 + left + physical_glyph.x as f32;
|
||||
let y = line_y.round() + physical_glyph.y as f32 - top + glyph_size.y as f32 / 2.0;
|
||||
let y =
|
||||
line_y.round() + physical_glyph.y as f32 - top + glyph_size.y as f32 / 2.0;
|
||||
let y = match y_axis_orientation {
|
||||
YAxisOrientation::TopToBottom => y,
|
||||
YAxisOrientation::BottomToTop => box_size.y - y,
|
||||
@ -327,14 +326,22 @@ impl TextPipeline {
|
||||
|
||||
let position = Vec2::new(x, y);
|
||||
|
||||
// TODO: recreate the byte index, that keeps track of where a cursor is,
|
||||
// when glyphs are not limited to single byte representation, relevant for #1319
|
||||
let pos_glyph =
|
||||
PositionedGlyph::new(position, glyph_size.as_vec2(), atlas_info, span_index);
|
||||
let pos_glyph = PositionedGlyph {
|
||||
position,
|
||||
size: glyph_size.as_vec2(),
|
||||
atlas_info,
|
||||
span_index,
|
||||
byte_index: layout_glyph.start,
|
||||
byte_length: layout_glyph.end - layout_glyph.start,
|
||||
line_index: line_i,
|
||||
};
|
||||
layout_info.glyphs.push(pos_glyph);
|
||||
Ok(())
|
||||
});
|
||||
|
||||
result
|
||||
});
|
||||
|
||||
// Return the scratch vec.
|
||||
self.glyph_info = glyph_info;
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user