Coarser grain ScaleContext

This patch sets up a ScaleContext for the duration of a draw_text call,
where it was previously per-glyph.
This commit is contained in:
Raph Levien 2021-10-21 12:10:57 -07:00
parent 8b4a6c54cd
commit 529e5cce5e

View file

@ -1,6 +1,6 @@
use std::ops::RangeBounds;
use swash::scale::ScaleContext;
use swash::scale::{ScaleContext, Scaler};
use swash::zeno::{Vector, Verb};
use swash::{FontRef, GlyphId};
@ -59,6 +59,10 @@ pub struct PathEncoder {
n_colr_layers: usize,
}
struct TextRenderCtx<'a> {
scaler: Scaler<'a>,
}
impl PietGpuText {
pub(crate) fn new(font: Font) -> PietGpuText {
PietGpuText { font }
@ -122,18 +126,10 @@ impl Font {
Font { font_ref }
}
fn make_path(&self, glyph_id: GlyphId) -> PathEncoder {
/*
fn make_path<'a>(&self, glyph_id: GlyphId, tc: &mut TextRenderCtx<'a>) -> PathEncoder {
let mut encoder = PathEncoder::default();
self.face.outline_glyph(glyph_id, &mut encoder);
encoder
*/
// Should the scale context be in the font? In the RenderCtx?
let mut scale_context = ScaleContext::new();
let mut scaler = scale_context.builder(self.font_ref).size(2048.).build();
let mut encoder = PathEncoder::default();
if scaler.has_color_outlines() {
if let Some(outline) = scaler.scale_color_outline(glyph_id) {
if tc.scaler.has_color_outlines() {
if let Some(outline) = tc.scaler.scale_color_outline(glyph_id) {
// TODO: be more sophisticated choosing a palette
let palette = self.font_ref.color_palettes().next().unwrap();
let mut i = 0;
@ -148,7 +144,7 @@ impl Font {
return encoder;
}
}
if let Some(outline) = scaler.scale_outline(glyph_id) {
if let Some(outline) = tc.scaler.scale_outline(glyph_id) {
encoder.append_outline(outline.verbs(), outline.points());
}
encoder
@ -175,6 +171,12 @@ impl PietGpuTextLayout {
}
pub(crate) fn draw_text(&self, ctx: &mut PietGpuRenderContext, pos: Point) {
let mut scale_ctx = ScaleContext::new();
let scaler = scale_ctx.builder(self.font.font_ref).size(2048.)
.build();
let mut tc = TextRenderCtx {
scaler,
};
// Should we use ppem from font, or let swash scale?
const DEFAULT_UPEM: u16 = 2048;
let scale = self.size as f32 / DEFAULT_UPEM as f32;
@ -213,7 +215,7 @@ impl PietGpuTextLayout {
last_x = glyph.x;
//println!("{:?}, {:?}", transform.mat, transform.translate);
ctx.encode_transform(transform);
let path = self.font.make_path(glyph.glyph_id);
let path = self.font.make_path(glyph.glyph_id, &mut tc);
ctx.append_path_encoder(&path);
if path.n_colr_layers == 0 {
ctx.fill_glyph(0xff_ff_ff_ff);