Commit 44724233 authored by Alex Butler's avatar Alex Butler

Merge branch 'layout-lifetimes' into 'master'

Glyph lifetimes for Font::glyphs_for & Font::layout

See merge request !158
parents 9d423252 e049025d
Pipeline #8037 passed with stages
in 7 minutes and 30 seconds
......@@ -137,7 +137,7 @@ impl<'font> Font<'font> {
/// points or glyph ids produced by the given iterator `itr`.
///
/// This is equivalent in behaviour to `itr.map(|c| font.glyph(c))`.
pub fn glyphs_for<I: Iterator>(&self, itr: I) -> GlyphIter<'_, I>
pub fn glyphs_for<'a, I: Iterator>(&'a self, itr: I) -> GlyphIter<'a, 'font, I>
where
I::Item: IntoGlyphId,
{
......@@ -177,25 +177,25 @@ impl<'font> Font<'font> {
/// # let (scale, start) = (Scale::uniform(0.0), point(0.0, 0.0));
/// # let font: Font = unimplemented!();
/// font.glyphs_for("Hello World!".chars())
/// .scan((None, 0.0), |&mut (mut last, mut x), g| {
/// .scan((None, 0.0), |(last, x), g| {
/// let g = g.scaled(scale);
/// if let Some(last) = last {
/// x += font.pair_kerning(scale, last, g.id());
/// *x += font.pair_kerning(scale, *last, g.id());
/// }
/// let w = g.h_metrics().advance_width;
/// let next = g.positioned(start + vector(x, 0.0));
/// last = Some(next.id());
/// x += w;
/// let next = g.positioned(start + vector(*x, 0.0));
/// *last = Some(next.id());
/// *x += w;
/// Some(next)
/// })
/// # ;
/// ```
pub fn layout<'f, 's>(
&'f self,
pub fn layout<'a, 's>(
&'a self,
s: &'s str,
scale: Scale,
start: Point<f32>,
) -> LayoutIter<'f, 's> {
) -> LayoutIter<'a, 'font, 's> {
LayoutIter {
font: self,
chars: s.chars(),
......
......@@ -997,7 +997,7 @@ mod test {
let font_data = include_bytes!("../dev/fonts/wqy-microhei/WenQuanYiMicroHei.ttf");
let font = Font::try_from_bytes(font_data as &[u8]).unwrap();
let mut cache = Cache::builder()
let mut cache: Cache<'static> = Cache::builder()
.dimensions(32, 32)
.scale_tolerance(0.1)
.position_tolerance(0.1)
......
......@@ -528,29 +528,29 @@ impl<G: Into<GlyphId>> IntoGlyphId for G {
}
#[derive(Clone)]
pub struct GlyphIter<'b, I: Iterator>
pub struct GlyphIter<'a, 'font, I: Iterator>
where
I::Item: IntoGlyphId,
{
font: &'b Font<'b>,
font: &'a Font<'font>,
itr: I,
}
impl<'b, I> Iterator for GlyphIter<'b, I>
impl<'a, 'font, I> Iterator for GlyphIter<'a, 'font, I>
where
I: Iterator,
I::Item: IntoGlyphId,
{
type Item = Glyph<'b>;
type Item = Glyph<'font>;
fn next(&mut self) -> Option<Glyph<'b>> {
fn next(&mut self) -> Option<Glyph<'font>> {
self.itr.next().map(|c| self.font.glyph(c))
}
}
#[derive(Clone)]
pub struct LayoutIter<'font, 's> {
font: &'font Font<'font>,
pub struct LayoutIter<'a, 'font, 's> {
font: &'a Font<'font>,
chars: core::str::Chars<'s>,
caret: f32,
scale: Scale,
......@@ -558,7 +558,7 @@ pub struct LayoutIter<'font, 's> {
last_glyph: Option<GlyphId>,
}
impl<'font, 's> Iterator for LayoutIter<'font, 's> {
impl<'a, 'font, 's> Iterator for LayoutIter<'a, 'font, 's> {
type Item = PositionedGlyph<'font>;
fn next(&mut self) -> Option<PositionedGlyph<'font>> {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment