Struct LayoutSession

Source
pub struct LayoutSession<S: AsRef<str>> { /* private fields */ }

Implementations§

Source§

impl<S: AsRef<str>> LayoutSession<S>

Source

pub fn create( text: S, style: &TextStyle, collection: &FontCollection, ) -> LayoutSession<S>

Examples found in repository?
examples/render.rs (line 265)
205fn main() {
206    let font = SystemSource::new()
207        .select_best_match(&[FamilyName::SansSerif], &Properties::new())
208        .unwrap()
209        .load()
210        .unwrap();
211
212    let data = font.copy_font_data();
213    println!("font data: {:?} bytes", data.map(|d| d.len()));
214
215    let style = TextStyle { size: 32.0 };
216    let glyph_id = font.glyph_for_char('O').unwrap();
217    println!("glyph id = {}", glyph_id);
218    println!(
219        "glyph typo bounds: {:?}",
220        font.typographic_bounds(glyph_id).unwrap()
221    );
222    println!(
223        "glyph raster bounds: {:?}",
224        font.raster_bounds(
225            glyph_id,
226            32.0,
227            Transform2F::default(),
228            HintingOptions::None,
229            RasterizationOptions::GrayscaleAa
230        )
231    );
232    let mut canvas = Canvas::new(vec2i(32, 32), Format::A8);
233    font.rasterize_glyph(
234        &mut canvas,
235        glyph_id,
236        // TODO(font-kit): this is missing anamorphic and skew features
237        style.size,
238        Transform2F::default(),
239        HintingOptions::None,
240        RasterizationOptions::GrayscaleAa,
241    )
242    .unwrap();
243    // TODO(font-kit): FreeType is top-aligned, CoreText is bottom-aligned, and FT seems to ignore origin
244    font.rasterize_glyph(
245        &mut canvas,
246        glyph_id,
247        style.size,
248        Transform2F::from_translation(vec2f(16.0, 16.0)),
249        HintingOptions::None,
250        RasterizationOptions::GrayscaleAa,
251    )
252    .unwrap();
253
254    let mut args = std::env::args();
255    args.next();
256    let text = args
257        .next()
258        .unwrap_or("Hello हिन्दी".to_string());
259    //let layout = make_layout(&style, &font, &text);
260    let collection = make_collection();
261    /*
262    let layout = layout(&style, &collection, &text);
263    println!("{:?}", layout);
264    */
265    let mut layout = LayoutSession::create(&text, &style, &collection);
266    let mut surface = SimpleSurface::new(200, 50);
267    surface.paint_layout_session(&mut layout, 0, 35, 0..text.len());
268    surface.write_pgm("out.pgm").unwrap();
269}
Source

pub fn iter_all(&self) -> LayoutRangeIter<'_>

Iterate through all glyphs in the layout.

Note: this is redundant with iter_substr with the whole string, might not keep it.

Source

pub fn iter_substr(&mut self, range: Range<usize>) -> LayoutRangeIter<'_>

Iterate through the glyphs in the layout of the substring.

This method reuses as much of the original layout as practical, almost entirely reusing the itemization, but possibly doing re-layout.

Examples found in repository?
examples/render.rs (line 131)
124    fn paint_layout_session<S: AsRef<str>>(
125        &mut self,
126        layout: &mut LayoutSession<S>,
127        x: i32,
128        y: i32,
129        range: Range<usize>,
130    ) {
131        for run in layout.iter_substr(range) {
132            let font = run.font();
133            let size = 32.0; // TODO: probably should get this from run
134            println!("run, font = {:?}", font);
135            for glyph in run.glyphs() {
136                let glyph_id = glyph.glyph_id;
137                let glyph_x = (glyph.offset.x() as i32) + x;
138                let glyph_y = (glyph.offset.y() as i32) + y;
139                let bounds = font
140                    .font
141                    .raster_bounds(
142                        glyph_id,
143                        size,
144                        Transform2F::default(),
145                        HintingOptions::None,
146                        RasterizationOptions::GrayscaleAa,
147                    )
148                    .unwrap();
149                println!(
150                    "glyph {}, bounds {:?}, {},{}",
151                    glyph_id, bounds, glyph_x, glyph_y
152                );
153                if bounds.width() > 0 && bounds.height() > 0 {
154                    let origin_adj = bounds.origin().to_f32();
155                    let neg_origin = -origin_adj;
156                    let mut canvas = Canvas::new(
157                        // Not sure why we need to add the extra pixel of height, probably a rounding isssue.
158                        // In any case, seems to get the job done (with CoreText rendering, anyway).
159                        bounds.size() + vec2i(0, 1),
160                        Format::A8,
161                    );
162                    font.font
163                        .rasterize_glyph(
164                            &mut canvas,
165                            glyph_id,
166                            // TODO(font-kit): this is missing anamorphic and skew features
167                            size,
168                            Transform2F::from_translation(neg_origin),
169                            HintingOptions::None,
170                            RasterizationOptions::GrayscaleAa,
171                        )
172                        .unwrap();
173                    self.paint_from_canvas(
174                        &canvas,
175                        glyph_x + bounds.origin_x(),
176                        glyph_y - bounds.origin_y(),
177                    );
178                }
179                println!("glyph {} @ {:?}", glyph.glyph_id, glyph.offset);
180            }
181        }
182    }

Auto Trait Implementations§

§

impl<S> Freeze for LayoutSession<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for LayoutSession<S>
where S: RefUnwindSafe,

§

impl<S> !Send for LayoutSession<S>

§

impl<S> !Sync for LayoutSession<S>

§

impl<S> Unpin for LayoutSession<S>
where S: Unpin,

§

impl<S> UnwindSafe for LayoutSession<S>
where S: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.