pub struct LayoutSession<S: AsRef<str>> { /* private fields */ }
Implementations§
Source§impl<S: AsRef<str>> LayoutSession<S>
impl<S: AsRef<str>> LayoutSession<S>
Sourcepub fn create(
text: S,
style: &TextStyle,
collection: &FontCollection,
) -> LayoutSession<S>
pub fn create( text: S, style: &TextStyle, collection: &FontCollection, ) -> LayoutSession<S>
Examples found in repository?
examples/render.rs (line 265)
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
fn main() {
let font = SystemSource::new()
.select_best_match(&[FamilyName::SansSerif], &Properties::new())
.unwrap()
.load()
.unwrap();
let data = font.copy_font_data();
println!("font data: {:?} bytes", data.map(|d| d.len()));
let style = TextStyle { size: 32.0 };
let glyph_id = font.glyph_for_char('O').unwrap();
println!("glyph id = {}", glyph_id);
println!(
"glyph typo bounds: {:?}",
font.typographic_bounds(glyph_id).unwrap()
);
println!(
"glyph raster bounds: {:?}",
font.raster_bounds(
glyph_id,
32.0,
Transform2F::default(),
HintingOptions::None,
RasterizationOptions::GrayscaleAa
)
);
let mut canvas = Canvas::new(vec2i(32, 32), Format::A8);
font.rasterize_glyph(
&mut canvas,
glyph_id,
// TODO(font-kit): this is missing anamorphic and skew features
style.size,
Transform2F::default(),
HintingOptions::None,
RasterizationOptions::GrayscaleAa,
)
.unwrap();
// TODO(font-kit): FreeType is top-aligned, CoreText is bottom-aligned, and FT seems to ignore origin
font.rasterize_glyph(
&mut canvas,
glyph_id,
style.size,
Transform2F::from_translation(vec2f(16.0, 16.0)),
HintingOptions::None,
RasterizationOptions::GrayscaleAa,
)
.unwrap();
let mut args = std::env::args();
args.next();
let text = args
.next()
.unwrap_or("Hello हिन्दी".to_string());
//let layout = make_layout(&style, &font, &text);
let collection = make_collection();
/*
let layout = layout(&style, &collection, &text);
println!("{:?}", layout);
*/
let mut layout = LayoutSession::create(&text, &style, &collection);
let mut surface = SimpleSurface::new(200, 50);
surface.paint_layout_session(&mut layout, 0, 35, 0..text.len());
surface.write_pgm("out.pgm").unwrap();
}
Sourcepub fn iter_all(&self) -> LayoutRangeIter<'_>
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.
Sourcepub fn iter_substr(&mut self, range: Range<usize>) -> LayoutRangeIter<'_>
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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
fn paint_layout_session<S: AsRef<str>>(
&mut self,
layout: &mut LayoutSession<S>,
x: i32,
y: i32,
range: Range<usize>,
) {
for run in layout.iter_substr(range) {
let font = run.font();
let size = 32.0; // TODO: probably should get this from run
println!("run, font = {:?}", font);
for glyph in run.glyphs() {
let glyph_id = glyph.glyph_id;
let glyph_x = (glyph.offset.x() as i32) + x;
let glyph_y = (glyph.offset.y() as i32) + y;
let bounds = font
.font
.raster_bounds(
glyph_id,
size,
Transform2F::default(),
HintingOptions::None,
RasterizationOptions::GrayscaleAa,
)
.unwrap();
println!(
"glyph {}, bounds {:?}, {},{}",
glyph_id, bounds, glyph_x, glyph_y
);
if bounds.width() > 0 && bounds.height() > 0 {
let origin_adj = bounds.origin().to_f32();
let neg_origin = -origin_adj;
let mut canvas = Canvas::new(
// Not sure why we need to add the extra pixel of height, probably a rounding isssue.
// In any case, seems to get the job done (with CoreText rendering, anyway).
bounds.size() + vec2i(0, 1),
Format::A8,
);
font.font
.rasterize_glyph(
&mut canvas,
glyph_id,
// TODO(font-kit): this is missing anamorphic and skew features
size,
Transform2F::from_translation(neg_origin),
HintingOptions::None,
RasterizationOptions::GrayscaleAa,
)
.unwrap();
self.paint_from_canvas(
&canvas,
glyph_x + bounds.origin_x(),
glyph_y - bounds.origin_y(),
);
}
println!("glyph {} @ {:?}", glyph.glyph_id, glyph.offset);
}
}
}
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more