From 7eac66b37321edad0f7f0dd648bb4e9581a1ad6b Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 17 Jun 2022 19:15:02 +0800 Subject: [PATCH] add centered text layout modes top-aligned has been tested. bottom aligned has yet to be tested. --- services/graphics-server/src/api/text.rs | 12 +++++++++++ services/graphics-server/src/main.rs | 27 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/services/graphics-server/src/api/text.rs b/services/graphics-server/src/api/text.rs index 2cde478e9..54beb1f58 100644 --- a/services/graphics-server/src/api/text.rs +++ b/services/graphics-server/src/api/text.rs @@ -15,6 +15,10 @@ pub enum TextBounds { GrowableFromBl(Point, u16), // fixed width, grows down from top right GrowableFromTr(Point, u16), + // fixed width, centered aligned top + CenteredTop(Rectangle), + // fixed width, centered aligned bottom + CenteredBot(Rectangle), } impl TextBounds { pub fn translate(&self, by: Point) -> TextBounds { @@ -35,6 +39,14 @@ impl TextBounds { TextBounds::GrowableFromTr(origin, width) => { TextBounds::GrowableFromTr(origin.add(by), width) } + TextBounds::CenteredTop(mut r) => { + r.translate(by); + TextBounds::BoundingBox(r) + } + TextBounds::CenteredBot(mut r) => { + r.translate(by); + TextBounds::BoundingBox(r) + } } } } diff --git a/services/graphics-server/src/main.rs b/services/graphics-server/src/main.rs index b3dff0c20..53132f4ca 100644 --- a/services/graphics-server/src/main.rs +++ b/services/graphics-server/src/main.rs @@ -247,6 +247,10 @@ fn wrapped_main() -> ! { Pt::new(width as i16 - tv.margin.x * 2, (clip_rect.br().y - clip_rect.tl().y - tl.y) - tv.margin.y * 2), TextBounds::GrowableFromTr(tr, width) => Pt::new(width as i16 - tv.margin.x * 2, (clip_rect.br().y - clip_rect.tl().y - tr.y) - tv.margin.y * 2), + TextBounds::CenteredTop(r) => + Pt::new(r.br().x - r.tl().x - tv.margin.x * 2, r.br().y - r.tl().y - tv.margin.y * 2), + TextBounds::CenteredBot(r) => + Pt::new(r.br().x - r.tl().x - tv.margin.x * 2, r.br().y - r.tl().y - tv.margin.y * 2), }; let mut typesetter = Typesetter::setup( tv.to_str(), @@ -274,6 +278,29 @@ fn wrapped_main() -> ! { tl.add(tv.margin), TextBounds::GrowableFromTr(tr, _width) => Point::new(tr.x - (composition.bb_width() as i16 + tv.margin.x), tr.y + tv.margin.y), + TextBounds::CenteredTop(r) => { + if r.width() as i16 > composition.bb_width() { + r.tl().add(Point::new( + (r.width() as i16 - composition.bb_width()) / 2, 0 + )) + } else { + r.tl().add(tv.margin) + } + }, + TextBounds::CenteredBot(r) => { + if r.width() as i16 > composition.bb_width() { + r.tl().add(Point::new( + (r.width() as i16 - composition.bb_width()) / 2, + if (r.height() as i16) > (composition.bb_height() + tv.margin.y) { + (r.height() as i16) - (composition.bb_height() + tv.margin.y) + } else { + 0 + } + )) + } else { + r.tl().add(tv.margin) + } + } } .add(screen_offset);