Skip to content

Commit 797c8bc

Browse files
committed
Move char_style_indices out of the per-item ShapeOptions
The style indices are text-wide, whereas the options are now per-item. Fixes the TODO from linebender#727 (comment).
1 parent f109c7b commit 797c8bc

4 files changed

Lines changed: 39 additions & 17 deletions

File tree

parley/src/shape/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ pub(crate) fn shape_text<'a, B: Brush>(
173173
font_size: item_style.font_size,
174174
features: &style_features[item_style_index as usize],
175175
variations: rcx.variations(item_style.font_variations).unwrap_or(&[]),
176-
char_style_indices,
177176
},
178177
})
179178
})
@@ -184,6 +183,7 @@ pub(crate) fn shape_text<'a, B: Brush>(
184183
scx.shape_text(
185184
text,
186185
analysis,
186+
char_style_indices,
187187
items,
188188
font_selector,
189189
&mut layout.data.shaped_text,

parley_engine/src/shape/atom.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -858,10 +858,16 @@ mod tests {
858858
language: None,
859859
features: &[],
860860
variations: &[],
861-
char_style_indices: &char_style_indices,
862861
},
863862
}];
864-
shaper.shape_text(text, &analysis, items, SingleFont(font), &mut shaped);
863+
shaper.shape_text(
864+
text,
865+
&analysis,
866+
&char_style_indices,
867+
items,
868+
SingleFont(font),
869+
&mut shaped,
870+
);
865871
shaped
866872
}
867873

parley_engine/src/shape/shaped_text.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ impl ShapedText {
236236
item: &Segment,
237237
options: &ShapeOptions<'_>,
238238
char_info: &[CharInfo],
239+
char_style_indices: &[u16],
239240
font: &FontInstance,
240241
glyph_buffer: &harfrust::GlyphBuffer,
241242
normalized_coords: &[harfrust::NormalizedCoord],
@@ -318,7 +319,7 @@ impl ShapedText {
318319
for (((byte_offset, ch), info), style_index) in text[range.byte_range.clone()]
319320
.char_indices()
320321
.zip(&char_info[range.char_range.clone()])
321-
.zip(&options.char_style_indices[range.char_range.clone()])
322+
.zip(&char_style_indices[range.char_range.clone()])
322323
{
323324
self.characters.push(Character {
324325
text_byte_start: (range.byte_range.start + byte_offset) as u32,
@@ -340,7 +341,7 @@ impl ShapedText {
340341
scale_factor,
341342
glyph_infos.iter(),
342343
glyph_positions.iter(),
343-
&options.char_style_indices[range.char_range.clone()],
344+
&char_style_indices[range.char_range.clone()],
344345
&self.characters,
345346
characters_start,
346347
);
@@ -351,7 +352,7 @@ impl ShapedText {
351352
scale_factor,
352353
glyph_infos.iter().rev(),
353354
glyph_positions.iter().rev(),
354-
&options.char_style_indices[range.char_range.clone()],
355+
&char_style_indices[range.char_range.clone()],
355356
&self.characters,
356357
characters_start,
357358
);
@@ -621,10 +622,16 @@ mod tests {
621622
language: None,
622623
features: &[],
623624
variations: &[],
624-
char_style_indices: &char_style_indices,
625625
},
626626
}];
627-
shaper.shape_text(text, &analysis, items, SingleFont(font), &mut shaped);
627+
shaper.shape_text(
628+
text,
629+
&analysis,
630+
&char_style_indices,
631+
items,
632+
SingleFont(font),
633+
&mut shaped,
634+
);
628635
shaped
629636
}
630637

@@ -732,7 +739,6 @@ mod tests {
732739
language: None,
733740
features: &[],
734741
variations: &[],
735-
char_style_indices: &char_style_indices,
736742
},
737743
},
738744
Item {
@@ -742,11 +748,17 @@ mod tests {
742748
language: None,
743749
features: &[],
744750
variations: &[],
745-
char_style_indices: &char_style_indices,
746751
},
747752
},
748753
];
749-
shaper.shape_text(text, &analysis, items, SingleFont(font), &mut shaped);
754+
shaper.shape_text(
755+
text,
756+
&analysis,
757+
&char_style_indices,
758+
items,
759+
SingleFont(font),
760+
&mut shaped,
761+
);
750762

751763
let grapheme_starts: Vec<bool> =
752764
shaped.characters.iter().map(|c| c.grapheme_start).collect();

parley_engine/src/shape/shaper.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ pub struct ShapeOptions<'a> {
3434
pub features: &'a [FontFeature],
3535
/// The font variations that are constant over an item.
3636
pub variations: &'a [FontVariation],
37-
/// The per-character style indices.
38-
// TODO: rename to something like `user_data` (s.t. we don't assume it's a style per se).
39-
// TODO: probably move this out of `ShapeOptions`, and supply it as a parameter on
40-
// `Shaper::shape_text`.
41-
pub char_style_indices: &'a [u16],
4237
}
4338

4439
/// The font instance to shape an item with.
@@ -92,6 +87,10 @@ impl Shaper {
9287
/// split on properties like shaping-relevant style changes (e.g., font size) or properties like
9388
/// language.
9489
///
90+
/// `char_style_indices` holds per-character style indices; these are copied onto
91+
/// [`Character`][super::Character`] and [`ShapedCluster`][super::ShapedCluster]. A shaped
92+
/// cluster's style index is that of its logically first constituent character.
93+
///
9594
/// Characters that don't have a particular script have their script resolved based on
9695
/// surrounding context (see [`Segment::script`]).
9796
///
@@ -106,6 +105,8 @@ impl Shaper {
106105
&mut self,
107106
text: &str,
108107
analysis: &Analysis,
108+
// TODO: rename to something like `user_data` (s.t. we don't assume it's a style per se).
109+
char_style_indices: &[u16],
109110
items: impl IntoIterator<Item = Item<'options>>,
110111
mut select_font: impl FontSelector,
111112
shaped_text: &mut ShapedText,
@@ -142,6 +143,7 @@ impl Shaper {
142143
&item.options,
143144
&mut select_font,
144145
analysis.char_info(),
146+
char_style_indices,
145147
shaped_text,
146148
)
147149
.is_err()
@@ -182,6 +184,7 @@ fn shape_segment(
182184
options: &ShapeOptions<'_>,
183185
select_font: &mut impl FontSelector,
184186
char_info: &[CharInfo],
187+
char_style_indices: &[u16],
185188
shaped_text: &mut ShapedText,
186189
) -> Result<(), ()> {
187190
select_font.begin_segment(item, options);
@@ -193,7 +196,7 @@ fn shape_segment(
193196

194197
// Only process current item
195198
let item_char_info = &char_info[char_range.start..char_range.end];
196-
let item_char_style_indices = &options.char_style_indices[char_range.start..char_range.end];
199+
let item_char_style_indices = &char_style_indices[char_range.start..char_range.end];
197200

198201
if item_text.is_empty() {
199202
return Ok(()); // No clusters
@@ -378,6 +381,7 @@ fn shape_segment(
378381
item,
379382
options,
380383
char_info,
384+
char_style_indices,
381385
&font,
382386
&glyph_buffer,
383387
harf_shaper.coords(),

0 commit comments

Comments
 (0)