rustybuzz/hb/
shape.rs

1use super::hb_font_t;
2use super::ot_shape::{shape_internal, ShapeContext};
3use super::ot_shape_plan::hb_ot_shape_plan_t;
4use crate::{script, Feature, GlyphBuffer, UnicodeBuffer};
5
6/// Shapes the buffer content using provided font and features.
7///
8/// Consumes the buffer. You can then run [`GlyphBuffer::clear`] to get the [`UnicodeBuffer`] back
9/// without allocating a new one.
10///
11/// If you plan to shape multiple strings using the same [`Face`] prefer [`shape_with_plan`].
12/// This is because [`ShapePlan`] initialization is pretty slow and should preferably be called
13/// once for each [`Face`].
14pub fn shape(face: &hb_font_t, features: &[Feature], mut buffer: UnicodeBuffer) -> GlyphBuffer {
15    buffer.0.guess_segment_properties();
16    let plan = hb_ot_shape_plan_t::new(
17        face,
18        buffer.0.direction,
19        buffer.0.script,
20        buffer.0.language.as_ref(),
21        features,
22    );
23    shape_with_plan(face, &plan, buffer)
24}
25
26/// Shapes the buffer content using the provided font and plan.
27///
28/// Consumes the buffer. You can then run [`GlyphBuffer::clear`] to get the [`UnicodeBuffer`] back
29/// without allocating a new one.
30///
31/// It is up to the caller to ensure that the shape plan matches the properties of the provided
32/// buffer, otherwise the shaping result will likely be incorrect.
33///
34/// # Panics
35///
36/// Will panic when debugging assertions are enabled if the buffer and plan have mismatched
37/// properties.
38pub fn shape_with_plan(
39    face: &hb_font_t,
40    plan: &hb_ot_shape_plan_t,
41    buffer: UnicodeBuffer,
42) -> GlyphBuffer {
43    let mut buffer = buffer.0;
44    buffer.guess_segment_properties();
45
46    debug_assert_eq!(buffer.direction, plan.direction);
47    debug_assert_eq!(
48        buffer.script.unwrap_or(script::UNKNOWN),
49        plan.script.unwrap_or(script::UNKNOWN)
50    );
51
52    if buffer.len > 0 {
53        // Save the original direction, we use it later.
54        let target_direction = buffer.direction;
55        shape_internal(&mut ShapeContext {
56            plan,
57            face,
58            buffer: &mut buffer,
59            target_direction,
60        });
61    }
62
63    GlyphBuffer(buffer)
64}