renderer: Enforce valid alignment

No renderer_interface function (except change_alignment) should be
called with a NONE alignment in the context.
This commit is contained in:
patrick96 2022-03-05 13:06:19 +01:00 committed by Patrick Ziegler
parent fdaecdb113
commit f37124f5af
2 changed files with 13 additions and 3 deletions

View File

@ -673,6 +673,7 @@ void renderer::fill_borders() {
* Draw text contents
*/
void renderer::render_text(const tags::context& ctxt, const string&& contents) {
assert(ctxt.get_alignment() != alignment::NONE && ctxt.get_alignment() == m_align);
m_log.trace_x("renderer: text(%s)", contents.c_str());
cairo::abspos origin{};
@ -742,6 +743,7 @@ void renderer::draw_offset(rgba color, double x, double w) {
}
void renderer::render_offset(const tags::context& ctxt, const extent_val offset) {
assert(ctxt.get_alignment() != alignment::NONE && ctxt.get_alignment() == m_align);
m_log.trace_x("renderer: offset_pixel(%f)", offset);
int offset_width = units_utils::extent_to_pixel(offset, m_bar.dpi_x);
@ -752,6 +754,7 @@ void renderer::render_offset(const tags::context& ctxt, const extent_val offset)
void renderer::change_alignment(const tags::context& ctxt) {
auto align = ctxt.get_alignment();
assert(align != alignment::NONE);
if (align != m_align) {
m_log.trace_x("renderer: change_alignment(%i)", static_cast<int>(align));
@ -772,6 +775,7 @@ void renderer::change_alignment(const tags::context& ctxt) {
}
double renderer::get_x(const tags::context& ctxt) const {
assert(ctxt.get_alignment() != alignment::NONE && ctxt.get_alignment() == m_align);
return m_blocks.at(ctxt.get_alignment()).x;
}

View File

@ -31,16 +31,21 @@ class FakeRenderer : public renderer_interface {
FakeRenderer(action_context& action_ctxt) : renderer_interface(action_ctxt){};
void render_offset(const tags::context& ctxt, const extent_val offset) override {
EXPECT_NE(alignment::NONE, ctxt.get_alignment());
block_x[ctxt.get_alignment()] += offset.value;
};
void render_text(const tags::context& ctxt, const string&& str) override {
EXPECT_NE(alignment::NONE, ctxt.get_alignment());
block_x[ctxt.get_alignment()] += str.size();
};
void change_alignment(const tags::context&) override{};
void change_alignment(const tags::context& ctxt) override {
EXPECT_NE(alignment::NONE, ctxt.get_alignment());
};
double get_x(const tags::context& ctxt) const override {
EXPECT_NE(alignment::NONE, ctxt.get_alignment());
return block_x.at(ctxt.get_alignment());
};
@ -120,7 +125,7 @@ TEST_F(DispatchTest, ignoreFormatting) {
}
bar_settings settings;
m_dispatch->parse(settings, r, "%{O10}%{F#ff0000}abc%{F-}foo");
m_dispatch->parse(settings, r, "%{l}%{O10}%{F#ff0000}abc%{F-}foo");
}
TEST_F(DispatchTest, formatting) {
@ -135,6 +140,7 @@ TEST_F(DispatchTest, formatting) {
{
InSequence seq;
EXPECT_CALL(r, change_alignment(match_left_align)).Times(1);
EXPECT_CALL(r, render_offset(_, extent_val{extent_type::PIXEL, 10})).Times(1);
EXPECT_CALL(r, render_text(match_fg(c1), string{"abc"})).Times(1);
EXPECT_CALL(r, render_text(match_fg(bar_fg), string{"foo"})).Times(1);
@ -149,7 +155,7 @@ TEST_F(DispatchTest, formatting) {
}
m_dispatch->parse(settings, r,
"%{O10}%{F#ff0000}abc%{F-}foo%{l}%{B#00ff00}bar%{R}123%{c}baz%{r}%{T123}def%{PR}"
"%{l}%{O10}%{F#ff0000}abc%{F-}foo%{l}%{B#00ff00}bar%{R}123%{c}baz%{r}%{T123}def%{PR}"
"ghi");
}