add centered text layout modes

top-aligned has been tested. bottom aligned has yet to be tested.
This commit is contained in:
bunnie 2022-06-17 19:15:02 +08:00
parent 37b2ee444d
commit 7eac66b373
2 changed files with 39 additions and 0 deletions

View file

@ -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)
}
}
}
}

View file

@ -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);