feat(plot): per-trace legend with measured layout and swatches
This commit is contained in:
1 parent
9dc3b1e5e6
commit
2d51b978d7
9 files changed
+850
-2
No files matched your search
@@ -563,3 +563,279 @@ PLOT_TEST(grid_lines_are_tick_aligned_data_space_segments) {
|
||||
for (const plot::GridLine& l : g.lines()) minor += l.major ? 0u : 1u;
|
||||
PLOT_CHECK(minor > 0); // minor grid is generated too
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------- legend
|
||||
|
||||
namespace {
|
||||
|
||||
/// The scene the legend cases measure: three named traces on one axis pair, a text
|
||||
/// backend to measure the names with, and a viewport with room to spare. The names
|
||||
/// are deliberately of different widths -- a panel sized to the *widest* one is what
|
||||
/// the first case below checks, and names of one width could not tell that from a
|
||||
/// panel sized to the first or the last.
|
||||
struct LegendScene {
|
||||
plot::render::EmbeddedMonoFont font;
|
||||
plot::data::ArraySeries series;
|
||||
plot::Plot plot;
|
||||
|
||||
LegendScene() : series(ramp(64), 0.0, 1.0) {
|
||||
plot.set_text_backend(&font);
|
||||
plot.set_viewport({0.0f, 0.0f, 800.0f, 400.0f});
|
||||
const u32 ax = plot.add_x_axis({"t", {0.0, 63.0}, false, plot::AxisPlace::Bottom, false});
|
||||
const u32 ay = plot.add_y_axis({"v", {0.0, 63.0}, false, plot::AxisPlace::Left, false});
|
||||
plot.add_trace({&series, plot::Color::hex(0x40C0FF), 1.5f, ax, ay, "sine"});
|
||||
plot.add_trace({&series, plot::Color::hex(0xFFB020), 1.5f, ax, ay, "noise floor"});
|
||||
plot.add_trace({&series, plot::Color::hex(0x00E070), 1.5f, ax, ay, "ramp"});
|
||||
}
|
||||
};
|
||||
|
||||
/// One row's height is the taller of the swatch and a text line: the number every
|
||||
/// other assertion here is written against, derived from the config the layout reads.
|
||||
f32 legend_row_height(const plot::Legend& cfg, const plot::render::TextBackend& font) {
|
||||
return std::max(cfg.swatch_h, font.height());
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
PLOT_TEST(legend_panel_wraps_the_entries) {
|
||||
LegendScene scene;
|
||||
const plot::PlotLayout& l = scene.plot.layout();
|
||||
const plot::LegendLayout& legend = l.legend;
|
||||
const plot::Legend& cfg = scene.plot.legend();
|
||||
|
||||
PLOT_CHECK(legend.rows.size() == 3u);
|
||||
const f32 pad = cfg.pad;
|
||||
const f32 row_h = legend_row_height(cfg, scene.font);
|
||||
const f32 row_gap = cfg.row_gap;
|
||||
const f32 widest = std::max({scene.font.measure("sine"), scene.font.measure("noise floor"),
|
||||
scene.font.measure("ramp")});
|
||||
|
||||
// Anchored in the corner `place` names (top right by default), one `margin` in
|
||||
// from the two edges that corner is near...
|
||||
PLOT_CHECK_NEAR(legend.panel.right(), l.axes.right() - cfg.margin, 1e-4);
|
||||
PLOT_CHECK_NEAR(legend.panel.y, l.axes.y + cfg.margin, 1e-4);
|
||||
// ... and measured, not guessed: a padding, the swatch, a gutter and the widest
|
||||
// name across; a padding, the rows and the gaps between them down.
|
||||
PLOT_CHECK_NEAR(legend.panel.w, 2.0f * pad + cfg.swatch_w + pad + widest, 1e-4);
|
||||
PLOT_CHECK_NEAR(legend.panel.h, 2.0f * pad + 3.0f * row_h + 2.0f * row_gap, 1e-4);
|
||||
|
||||
// Every row in trace order, one row gap below the last, with the boxes the
|
||||
// renderer stages from already placed -- no arithmetic left for a caller.
|
||||
const f32 name_x = legend.panel.x + pad + cfg.swatch_w + pad;
|
||||
f32 row_y = legend.panel.y + pad;
|
||||
for (u32 i = 0; i < legend.rows.size(); ++i) {
|
||||
const plot::LegendRow& row = legend.rows[i];
|
||||
PLOT_CHECK(row.trace == i);
|
||||
PLOT_CHECK(!row.ellipsis);
|
||||
PLOT_CHECK_NEAR(row.swatch.x, legend.panel.x + pad, 1e-4);
|
||||
PLOT_CHECK_NEAR(row.swatch.y, row_y + 0.5f * (row_h - cfg.swatch_h), 1e-4);
|
||||
PLOT_CHECK_NEAR(row.swatch.w, cfg.swatch_w, 1e-4);
|
||||
PLOT_CHECK_NEAR(row.swatch.h, cfg.swatch_h, 1e-4);
|
||||
PLOT_CHECK_NEAR(row.name.x, name_x, 1e-4);
|
||||
PLOT_CHECK_NEAR(row.name.y, row_y + 0.5f * (row_h - scene.font.height()), 1e-4);
|
||||
PLOT_CHECK_NEAR(row.name.h, scene.font.height(), 1e-4);
|
||||
// Both boxes are inside the panel, swatch to panel edge and name to the padding
|
||||
// the panel reserved for it.
|
||||
PLOT_CHECK(row.swatch.x >= legend.panel.x);
|
||||
PLOT_CHECK(row.swatch.right() <= legend.panel.right());
|
||||
PLOT_CHECK(row.name.right() <= legend.panel.right() - pad + 1e-4f);
|
||||
row_y += row_h + row_gap;
|
||||
}
|
||||
|
||||
// The wrapper property, stated as the number it is: the widest row's name ends
|
||||
// exactly one padding short of the panel's edge, which is what "the panel is
|
||||
// sized to the widest name" means. A panel sized to another row's name would fail
|
||||
// this by that row's difference in width.
|
||||
PLOT_CHECK_NEAR(legend.rows[1].name.w, widest, 1e-4);
|
||||
PLOT_CHECK_NEAR(legend.rows[1].name.right() + pad, legend.panel.right(), 1e-4);
|
||||
}
|
||||
|
||||
/// A legend is inside `axes` in every corner, held off the two edges its corner is
|
||||
/// near by `margin` and off the other two by its own measured size. The clamp is
|
||||
/// what stops a legend from being drawn over the tick labels or the data the plot
|
||||
/// reserved no room for -- it is inside the data area by construction, since the fit
|
||||
/// above is measured against exactly this inset.
|
||||
PLOT_TEST(legend_is_clamped_inside_the_axes) {
|
||||
LegendScene scene;
|
||||
const plot::LegendPlace places[4] = {plot::LegendPlace::TopLeft, plot::LegendPlace::TopRight,
|
||||
plot::LegendPlace::BottomLeft,
|
||||
plot::LegendPlace::BottomRight};
|
||||
for (const plot::LegendPlace place : places) {
|
||||
plot::Legend cfg = scene.plot.legend();
|
||||
cfg.place = place;
|
||||
scene.plot.set_legend(cfg);
|
||||
const plot::PlotLayout& l = scene.plot.layout();
|
||||
const plot::Rect& panel = l.legend.panel;
|
||||
PLOT_CHECK(!l.legend.empty());
|
||||
|
||||
// Inside the data area: half-open containment, so the far edges compare directly.
|
||||
PLOT_CHECK(panel.x >= l.axes.x);
|
||||
PLOT_CHECK(panel.y >= l.axes.y);
|
||||
PLOT_CHECK(panel.right() <= l.axes.right());
|
||||
PLOT_CHECK(panel.bottom() <= l.axes.bottom());
|
||||
|
||||
const bool near_right = place == plot::LegendPlace::TopRight ||
|
||||
place == plot::LegendPlace::BottomRight;
|
||||
const bool near_bottom = place == plot::LegendPlace::BottomLeft ||
|
||||
place == plot::LegendPlace::BottomRight;
|
||||
PLOT_CHECK_NEAR(near_right ? panel.right() : panel.x,
|
||||
near_right ? l.axes.right() - cfg.margin : l.axes.x + cfg.margin, 1e-4);
|
||||
PLOT_CHECK_NEAR(near_bottom ? panel.bottom() : panel.y,
|
||||
near_bottom ? l.axes.bottom() - cfg.margin : l.axes.y + cfg.margin, 1e-4);
|
||||
}
|
||||
}
|
||||
|
||||
/// Entries that do not fit are dropped from the end and the panel says so with a
|
||||
/// final marker row; a plot with no room for even one entry and its marker gets no
|
||||
/// legend at all rather than a panel over the axes. Both halves are the same rule
|
||||
/// read in opposite directions, so they are asserted together.
|
||||
PLOT_TEST(legend_drops_overflowing_entries) {
|
||||
plot::render::EmbeddedMonoFont font;
|
||||
plot::data::ArraySeries series(ramp(64), 0.0, 1.0);
|
||||
plot::Plot p;
|
||||
p.set_text_backend(&font);
|
||||
p.set_viewport({0.0f, 0.0f, 400.0f, 260.0f});
|
||||
const u32 ax = p.add_x_axis({"t", {0.0, 63.0}, false, plot::AxisPlace::Bottom, false});
|
||||
const u32 ay = p.add_y_axis({"v", {0.0, 63.0}, false, plot::AxisPlace::Left, false});
|
||||
constexpr u32 kNamed = 20;
|
||||
for (u32 i = 0; i < kNamed; ++i) p.add_trace({&series, plot::Color::hex(0x40C0FF), 1.5f, ax, ay,
|
||||
"trace " + std::to_string(i)});
|
||||
|
||||
const plot::PlotLayout& l = p.layout();
|
||||
const plot::LegendLayout& legend = l.legend;
|
||||
const plot::Legend& cfg = p.legend();
|
||||
const f32 pad = cfg.pad;
|
||||
const f32 row_h = legend_row_height(cfg, font);
|
||||
|
||||
// Re-derive the rule rather than assert a coincidence: the largest number of kept
|
||||
// entries whose panel -- with the marker row it needs once anything was dropped --
|
||||
// still fits the inset box. `k + 1` rows is the truncated case, `k` the untruncated
|
||||
// one; both are tried from the top down, exactly as the layout does.
|
||||
const f32 room_h = l.axes.h - 2.0f * cfg.margin;
|
||||
u32 expected = 0;
|
||||
for (u32 k = kNamed; k > 0u; --k) {
|
||||
const bool truncated = k < kNamed;
|
||||
const f32 rows = static_cast<f32>(k + (truncated ? 1u : 0u));
|
||||
if (2.0f * pad + rows * row_h + (rows - 1.0f) * cfg.row_gap <= room_h) {
|
||||
expected = k;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// The scene has to be one where the rule actually bites, or the case proves
|
||||
// nothing about dropping entries.
|
||||
PLOT_CHECK(expected != 0u);
|
||||
PLOT_CHECK(expected < kNamed);
|
||||
PLOT_CHECK(legend.rows.size() == expected + 1u);
|
||||
|
||||
// The kept entries are the first `expected` ones, in trace order: entries are
|
||||
// dropped from the end, never from the middle.
|
||||
for (u32 i = 0; i < expected; ++i) {
|
||||
PLOT_CHECK(!legend.rows[i].ellipsis);
|
||||
PLOT_CHECK(legend.rows[i].trace == i);
|
||||
}
|
||||
const plot::LegendRow& marker = legend.rows.back();
|
||||
PLOT_CHECK(marker.ellipsis);
|
||||
PLOT_CHECK(marker.name.x == legend.rows[0].name.x); // the marker lines up with the names
|
||||
PLOT_CHECK_NEAR(marker.name.w, font.measure(plot::kLegendEllipsis), 1e-4);
|
||||
PLOT_CHECK(marker.swatch.w == 0.0f && marker.swatch.h == 0.0f); // no trace, no swatch
|
||||
|
||||
// The panel with the marker is inside `axes`, which is the property the drop
|
||||
// exists to keep: a panel that overhung the plot would be worse than a short list.
|
||||
PLOT_CHECK(legend.panel.x >= l.axes.x && legend.panel.right() <= l.axes.right());
|
||||
PLOT_CHECK(legend.panel.y >= l.axes.y && legend.panel.bottom() <= l.axes.bottom());
|
||||
|
||||
// Not even one entry plus the marker fits: no legend at all, and nothing drawn.
|
||||
plot::Plot tiny;
|
||||
tiny.set_text_backend(&font);
|
||||
tiny.set_viewport({0.0f, 0.0f, 120.0f, 60.0f});
|
||||
const u32 tax = tiny.add_x_axis({"t", {0.0, 63.0}, false, plot::AxisPlace::Bottom, false});
|
||||
const u32 tay = tiny.add_y_axis({"v", {0.0, 63.0}, false, plot::AxisPlace::Left, false});
|
||||
tiny.add_trace({&series, plot::Color::hex(0x40C0FF), 1.5f, tax, tay, "sine"});
|
||||
const plot::PlotLayout& tl = tiny.layout();
|
||||
PLOT_CHECK(tl.legend.empty());
|
||||
PLOT_CHECK_NEAR(tl.legend.panel.w, 0.0f, 1e-6);
|
||||
PLOT_CHECK_NEAR(tl.legend.panel.h, 0.0f, 1e-6);
|
||||
}
|
||||
|
||||
/// A trace with no name is not an entry, and neither is a trace with no series to
|
||||
/// draw -- the same "visible" test every other consumer of a trace makes. This is
|
||||
/// the whole cost of the feature for a caller that never names a trace: nothing.
|
||||
PLOT_TEST(unnamed_traces_produce_no_legend) {
|
||||
plot::render::EmbeddedMonoFont font;
|
||||
plot::data::ArraySeries series(ramp(64), 0.0, 1.0);
|
||||
plot::Plot p;
|
||||
p.set_text_backend(&font);
|
||||
p.set_viewport({0.0f, 0.0f, 800.0f, 400.0f});
|
||||
const u32 ax = p.add_x_axis({"t", {0.0, 63.0}, false, plot::AxisPlace::Bottom, false});
|
||||
const u32 ay = p.add_y_axis({"v", {0.0, 63.0}, false, plot::AxisPlace::Left, false});
|
||||
p.add_trace({&series, plot::Color::hex(0x40C0FF), 1.5f, ax, ay}); // no name
|
||||
p.add_trace({nullptr, plot::Color::hex(0xFFB020), 1.5f, ax, ay, "ghost"}); // named, no series
|
||||
p.add_trace({&series, plot::Color::hex(0x00E070), 1.5f, ax, ay, "sig"});
|
||||
|
||||
const plot::PlotLayout& l = p.layout();
|
||||
PLOT_CHECK(l.legend.rows.size() == 1u);
|
||||
PLOT_CHECK(l.legend.rows[0].trace == 2u); // the named, drawable one, by its own id
|
||||
PLOT_CHECK(!l.legend.rows[0].ellipsis);
|
||||
|
||||
// Without the one name, the layout is empty and zero-sized: no panel, no rows.
|
||||
plot::Plot unnamed;
|
||||
unnamed.set_text_backend(&font);
|
||||
unnamed.set_viewport({0.0f, 0.0f, 800.0f, 400.0f});
|
||||
const u32 uax = unnamed.add_x_axis({"t", {0.0, 63.0}, false, plot::AxisPlace::Bottom, false});
|
||||
const u32 uay = unnamed.add_y_axis({"v", {0.0, 63.0}, false, plot::AxisPlace::Left, false});
|
||||
unnamed.add_trace({&series, plot::Color::hex(0x40C0FF), 1.5f, uax, uay});
|
||||
unnamed.add_trace({&series, plot::Color::hex(0xFFB020), 1.5f, uax, uay});
|
||||
const plot::LegendLayout& none = unnamed.layout().legend;
|
||||
PLOT_CHECK(none.empty());
|
||||
PLOT_CHECK_NEAR(none.panel.w, 0.0f, 1e-6);
|
||||
PLOT_CHECK_NEAR(none.panel.h, 0.0f, 1e-6);
|
||||
}
|
||||
|
||||
/// A plot has no font of its own, so a legend with nothing to measure a name with is
|
||||
/// no legend: the plot keeps the defaults it laid out with before legends existed.
|
||||
PLOT_TEST(no_text_backend_produces_no_legend) {
|
||||
plot::data::ArraySeries series(ramp(64), 0.0, 1.0);
|
||||
plot::Plot p;
|
||||
p.set_viewport({0.0f, 0.0f, 800.0f, 400.0f});
|
||||
const u32 ax = p.add_x_axis({"t", {0.0, 63.0}, false, plot::AxisPlace::Bottom, false});
|
||||
const u32 ay = p.add_y_axis({"v", {0.0, 63.0}, false, plot::AxisPlace::Left, false});
|
||||
p.add_trace({&series, plot::Color::hex(0x40C0FF), 1.5f, ax, ay, "sine"});
|
||||
|
||||
const plot::PlotLayout& l = p.layout();
|
||||
PLOT_CHECK(p.text_backend() == nullptr);
|
||||
PLOT_CHECK(l.legend.empty());
|
||||
PLOT_CHECK_NEAR(l.legend.panel.w, 0.0f, 1e-6);
|
||||
PLOT_CHECK_NEAR(l.legend.panel.h, 0.0f, 1e-6);
|
||||
// Nothing else moved either: the strips are the no-backend defaults, not metrics a
|
||||
// legend could not measure.
|
||||
PLOT_CHECK_NEAR(l.y_axis_width, 56.0f, 1e-6);
|
||||
PLOT_CHECK_NEAR(l.x_axis_height, 22.0f, 1e-6);
|
||||
}
|
||||
|
||||
/// Switching the legend off is not the same as the plot having nothing to list: the
|
||||
/// entries exist, the layout is empty, and -- the reason it matters -- the axes rect
|
||||
/// is *identical*, because a legend is drawn inside the data area and reserves none
|
||||
/// of it. A legend that changed `axes` would rescale every plot the moment a trace
|
||||
/// was named.
|
||||
PLOT_TEST(legend_hidden_by_visible_false_draws_nothing) {
|
||||
LegendScene scene;
|
||||
const plot::Rect shown_axes = scene.plot.layout().axes;
|
||||
PLOT_CHECK(!scene.plot.layout().legend.empty());
|
||||
|
||||
plot::Legend cfg = scene.plot.legend();
|
||||
cfg.visible = false;
|
||||
scene.plot.set_legend(cfg);
|
||||
const plot::PlotLayout& l = scene.plot.layout();
|
||||
PLOT_CHECK(l.legend.empty());
|
||||
PLOT_CHECK_NEAR(l.legend.panel.w, 0.0f, 1e-6);
|
||||
PLOT_CHECK_NEAR(l.legend.panel.h, 0.0f, 1e-6);
|
||||
PLOT_CHECK_NEAR(l.axes.x, shown_axes.x, 1e-6);
|
||||
PLOT_CHECK_NEAR(l.axes.y, shown_axes.y, 1e-6);
|
||||
PLOT_CHECK_NEAR(l.axes.w, shown_axes.w, 1e-6);
|
||||
PLOT_CHECK_NEAR(l.axes.h, shown_axes.h, 1e-6);
|
||||
|
||||
// ... and switching it back on lists the same three entries again.
|
||||
cfg.visible = true;
|
||||
scene.plot.set_legend(cfg);
|
||||
PLOT_CHECK(scene.plot.layout().legend.rows.size() == 3u);
|
||||
}
|
||||
@@ -2188,3 +2188,228 @@ PLOT_TEST(text_atlas_reports_a_status_when_the_entry_points_are_missing) {
|
||||
PLOT_CHECK(plot::gl::glGetError() == plot::gl::GL_NO_ERROR);
|
||||
ctx.destroy();
|
||||
}
|
||||
|
||||
/// The legend's two cases need a count, not a presence: "the swatch colour appears"
|
||||
/// is satisfied by the trace that carries the same colour, and "some ink is in the
|
||||
/// panel" is satisfied by any label the frame happens to put there. Counting the
|
||||
/// pixels of an area that are exactly `c` (see `pixel_is`) is what turns both into
|
||||
/// statements about the swatch.
|
||||
plot::u32 exact_pixels(const std::vector<plot::u8>& frame, plot::u32 fb_w, const plot::Rect& area,
|
||||
plot::Color c) {
|
||||
plot::u32 count = 0;
|
||||
for (plot::u32 y = static_cast<plot::u32>(area.y);
|
||||
y < static_cast<plot::u32>(area.bottom()); ++y) {
|
||||
for (plot::u32 x = static_cast<plot::u32>(area.x);
|
||||
x < static_cast<plot::u32>(area.right()); ++x) {
|
||||
if (pixel_is(&frame[(plot::usize(y) * fb_w + x) * 4u], c)) ++count;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/// The whole framebuffer as a Rect, for the counts that ask "and nowhere else".
|
||||
const plot::Rect kFrameRect{0.0f, 0.0f, static_cast<plot::f32>(kW), static_cast<plot::f32>(kH)};
|
||||
|
||||
/// The legend's entries are drawn where `Plot::layout()` measured them: a filled
|
||||
/// swatch per trace in that trace's own colour, and the name's ink inside the panel
|
||||
/// and nowhere else in the data area -- and the panel's presence changes nothing
|
||||
/// outside itself.
|
||||
///
|
||||
/// The swatch colour is what makes the count a test. Both traces here are translucent
|
||||
/// (alpha 0.25), while the swatch is drawn opaque, so the exact colour of a trace is
|
||||
/// one that only a swatch can produce: the trace blended over the background -- or
|
||||
/// over a grid line, which is brighter -- never reaches it, and neither do the grid,
|
||||
/// the axis line or the text. Two colours are used so each entry's swatch has to be
|
||||
/// its own trace's, not whichever colour the legend drew for everything.
|
||||
PLOT_TEST(legend_swatch_and_label_are_drawn) {
|
||||
plottest::HeadlessContext ctx(kW, kH);
|
||||
plot::Status st;
|
||||
PLOT_CHECK(ctx.create(&st));
|
||||
PLOT_CHECK(plot::gl::load(nullptr));
|
||||
plot::Renderer r;
|
||||
PLOT_CHECK(r.init(&st));
|
||||
|
||||
plot::data::ArraySeries s(sine_samples(), 0.0, 1.0);
|
||||
// The second trace is a series of its own, well clear of the first: two
|
||||
// translucent strokes stacked over a grid line reach a brightness the `axis_text`
|
||||
// ink predicate below cannot tell from a label, and the "no name ink in the data
|
||||
// area" half of this case has to be about the legend's ink only.
|
||||
plot::data::ArraySeries flat(std::vector<plot::f32>(64u, 0.75f), 0.0, 1.0);
|
||||
plot::Plot p;
|
||||
p.set_viewport({0, 0, static_cast<plot::f32>(kW), static_cast<plot::f32>(kH)});
|
||||
const plot::u32 ax = p.add_x_axis({"t", s.x_bounds(), true, plot::AxisPlace::Bottom, false});
|
||||
const plot::u32 ay = p.add_y_axis({"v", {-1.0, 1.0}, false, plot::AxisPlace::Left, false});
|
||||
const plot::Color green = plot::Color::hex(0x00E070, 0.25f);
|
||||
const plot::Color pink = plot::Color::hex(0xFF3CC8, 0.25f);
|
||||
PLOT_CHECK(p.add_trace({&s, green, 1.5f, ax, ay, "signal"}) == 0u);
|
||||
PLOT_CHECK(p.add_trace({&flat, pink, 1.5f, ax, ay, "noise"}) == 1u);
|
||||
p.set_x_range(ax, {0.0, 640.0}); // one sample per pixel: a flat, cheap frame
|
||||
|
||||
r.begin_frame(kW, kH, {0, 0, static_cast<plot::f32>(kW), static_cast<plot::f32>(kH)});
|
||||
r.draw(p);
|
||||
r.end_frame();
|
||||
PLOT_CHECK(!r.last_frame_had_gl_error());
|
||||
dump_if_requested(r, "legend.ppm");
|
||||
|
||||
// A copy: the second draw below clears the rows this layout holds.
|
||||
const plot::PlotLayout layout = p.layout();
|
||||
const plot::LegendLayout legend = layout.legend;
|
||||
PLOT_CHECK(legend.rows.size() == 2u);
|
||||
const plot::Legend& cfg = p.legend();
|
||||
const plot::u32 swatch_area = static_cast<plot::u32>(cfg.swatch_w) *
|
||||
static_cast<plot::u32>(cfg.swatch_h);
|
||||
|
||||
const std::vector<plot::u8> frame = read_frame(ctx);
|
||||
|
||||
// Each swatch: its own row's swatch box holds the pixels, and every pixel of that
|
||||
// colour in the whole frame is one of them. The lower bound is the box's area
|
||||
// minus the fringe a 1 px stroke column cannot promise -- a row and a column at
|
||||
// each edge -- and the upper bound lets the outermost columns and rows count
|
||||
// where the fill's edge lands a hair inside them.
|
||||
const plot::Color opaque[2] = {plot::Color{green.r, green.g, green.b, 1.0f},
|
||||
plot::Color{pink.r, pink.g, pink.b, 1.0f}};
|
||||
for (plot::u32 i = 0; i < legend.rows.size(); ++i) {
|
||||
const plot::Rect& box = legend.rows[i].swatch;
|
||||
PLOT_CHECK(box.w > 0.0f && box.h > 0.0f);
|
||||
PLOT_CHECK(box.x >= legend.panel.x && box.right() <= legend.panel.right());
|
||||
PLOT_CHECK(box.y >= legend.panel.y && box.bottom() <= legend.panel.bottom());
|
||||
// The row names its own trace, so the colour under the predicate is that
|
||||
// trace's -- which is what `LegendRow::trace` is for.
|
||||
PLOT_CHECK_NEAR(p.trace(legend.rows[i].trace).color.r, opaque[i].r, 1e-6);
|
||||
const plot::u32 inside = exact_pixels(frame, kW, box, opaque[i]);
|
||||
const plot::u32 anywhere = exact_pixels(frame, kW, kFrameRect, opaque[i]);
|
||||
const plot::u32 in_panel = exact_pixels(frame, kW, legend.panel, opaque[i]);
|
||||
// The swatch's colour fills the swatch's box -- within the row and column an
|
||||
// edge's coverage ramp can reach past it -- and appears nowhere else in the
|
||||
// frame: not over the trace that carries the same colour at 25%, not in the
|
||||
// grid, and not outside the panel, which is where this ink's whole budget is.
|
||||
PLOT_CHECK(inside >= swatch_area - 2u * static_cast<plot::u32>(cfg.swatch_w) -
|
||||
2u * static_cast<plot::u32>(cfg.swatch_h));
|
||||
PLOT_CHECK(anywhere <= swatch_area + 2u * static_cast<plot::u32>(cfg.swatch_w) +
|
||||
2u * static_cast<plot::u32>(cfg.swatch_h));
|
||||
PLOT_CHECK(anywhere == in_panel);
|
||||
// The numbers behind those assertions, so a run says what it measured rather than
|
||||
// only that it passed.
|
||||
std::printf("[legend] row %u trace %u swatch %.2f,%.2f %.2fx%.2f: %u pixels of its colour, "
|
||||
"%u in the frame\n",
|
||||
i, legend.rows[i].trace, box.x, box.y, box.w, box.h, inside, anywhere);
|
||||
}
|
||||
|
||||
// The names: real glyph ink inside the panel, and none in the data area outside it.
|
||||
// This plot has no cursor, no measurement and no annotation, and every other
|
||||
// `axis_text` run -- the tick labels, the titles -- is drawn in a strip outside
|
||||
// `axes`, so the only ink in the data area is the legend's.
|
||||
const plot::Color ink = r.style().axis_text;
|
||||
const plot::u32 in_panel = scan_rect_ink(ctx, legend.panel, ink).pixels;
|
||||
// The data area, inset past the axis lines: they run along the axes' edges and are
|
||||
// brighter than a third of `axis_text` -- the trap `rect_has_exact` exists for -- so
|
||||
// an un-inset scan would be counting the axis rather than a label.
|
||||
const plot::Rect data{layout.axes.x + 2.0f, layout.axes.y + 2.0f, layout.axes.w - 4.0f,
|
||||
layout.axes.h - 4.0f};
|
||||
const plot::u32 in_data = scan_rect_ink(ctx, data, ink).pixels;
|
||||
PLOT_CHECK(in_panel >= 32u); // two names, not one stray anti-aliased pixel
|
||||
PLOT_CHECK(in_data == in_panel); // and every bit of that ink is the legend's
|
||||
// The numbers behind those assertions, so a run says what it measured rather than
|
||||
// only that it passed.
|
||||
std::printf("[legend] panel %.2f,%.2f %.2fx%.2f, %zu rows; name ink %u in the panel, %u in the "
|
||||
"data area\n",
|
||||
legend.panel.x, legend.panel.y, legend.panel.w, legend.panel.h, legend.rows.size(),
|
||||
in_panel, in_data);
|
||||
|
||||
// The panel changes nothing outside itself: the same plot drawn with the legend
|
||||
// switched off -- same traces, same axes rect, same transforms, since a legend
|
||||
// reserves none of the plot -- must come out pixel-identical everywhere except
|
||||
// inside the panel. One pixel of slack on each side, because a fill's stroke
|
||||
// column lets its fringe reach a pixel past the panel's edge.
|
||||
plot::Legend off = cfg;
|
||||
off.visible = false;
|
||||
p.set_legend(off);
|
||||
r.begin_frame(kW, kH, {0, 0, static_cast<plot::f32>(kW), static_cast<plot::f32>(kH)});
|
||||
r.draw(p);
|
||||
r.end_frame();
|
||||
PLOT_CHECK(!r.last_frame_had_gl_error());
|
||||
const std::vector<plot::u8> bare = read_frame(ctx);
|
||||
const plot::Rect changed{legend.panel.x - 1.0f, legend.panel.y - 1.0f, legend.panel.w + 2.0f,
|
||||
legend.panel.h + 2.0f};
|
||||
PLOT_CHECK(changed.x >= layout.axes.x && changed.right() <= layout.axes.right());
|
||||
PLOT_CHECK(changed.bottom() <= layout.axes.bottom()); // so "inside changed" is inside axes
|
||||
plot::u32 differing = 0;
|
||||
for (plot::u32 y = 0; y < ctx.height; ++y) {
|
||||
for (plot::u32 x = 0; x < ctx.width; ++x) {
|
||||
const plot::usize at = (plot::usize(y) * kW + x) * 4u;
|
||||
if (frame[at] == bare[at] && frame[at + 1u] == bare[at + 1u] &&
|
||||
frame[at + 2u] == bare[at + 2u] && frame[at + 3u] == bare[at + 3u]) {
|
||||
continue;
|
||||
}
|
||||
++differing;
|
||||
PLOT_CHECK(changed.contains(static_cast<plot::f32>(x) + 0.5f,
|
||||
static_cast<plot::f32>(y) + 0.5f));
|
||||
}
|
||||
}
|
||||
// The comparison is only evidence if the legend was in the first frame at all.
|
||||
PLOT_CHECK(differing > 0u);
|
||||
ctx.destroy();
|
||||
}
|
||||
|
||||
/// The bottom-left corner is the one a legend anchored from the axes' *opposite*
|
||||
/// corner would miss: the panel has to be down there, inside `axes` and held off the
|
||||
/// two edges that corner is near, with its swatch in the panel and nowhere else --
|
||||
/// including not in the strip below the axes, where the tick labels live.
|
||||
PLOT_TEST(legend_does_not_cover_the_plot_when_placed_bottom_left) {
|
||||
plottest::HeadlessContext ctx(kW, kH);
|
||||
plot::Status st;
|
||||
PLOT_CHECK(ctx.create(&st));
|
||||
PLOT_CHECK(plot::gl::load(nullptr));
|
||||
plot::Renderer r;
|
||||
PLOT_CHECK(r.init(&st));
|
||||
|
||||
plot::data::ArraySeries s(sine_samples(), 0.0, 1.0);
|
||||
plot::Plot p;
|
||||
p.set_viewport({0, 0, static_cast<plot::f32>(kW), static_cast<plot::f32>(kH)});
|
||||
const plot::u32 ax = p.add_x_axis({"t", s.x_bounds(), true, plot::AxisPlace::Bottom, false});
|
||||
const plot::u32 ay = p.add_y_axis({"v", {-1.0, 1.0}, false, plot::AxisPlace::Left, false});
|
||||
// Translucent, as in the case above: the swatch is drawn opaque, so this colour is
|
||||
// the swatch's alone.
|
||||
const plot::Color teal = plot::Color::hex(0x00E070, 0.25f);
|
||||
p.add_trace({&s, teal, 1.5f, ax, ay, "signal"});
|
||||
p.set_x_range(ax, {0.0, 640.0});
|
||||
plot::Legend cfg = p.legend();
|
||||
cfg.place = plot::LegendPlace::BottomLeft;
|
||||
p.set_legend(cfg);
|
||||
|
||||
r.begin_frame(kW, kH, {0, 0, static_cast<plot::f32>(kW), static_cast<plot::f32>(kH)});
|
||||
r.draw(p);
|
||||
r.end_frame();
|
||||
PLOT_CHECK(!r.last_frame_had_gl_error());
|
||||
dump_if_requested(r, "legend_bottom_left.ppm");
|
||||
|
||||
const plot::PlotLayout layout = p.layout();
|
||||
const plot::LegendLayout& legend = layout.legend;
|
||||
PLOT_CHECK(legend.rows.size() == 1u);
|
||||
const plot::Rect& panel = legend.panel;
|
||||
PLOT_CHECK_NEAR(panel.x, layout.axes.x + cfg.margin, 1e-4);
|
||||
PLOT_CHECK_NEAR(panel.bottom(), layout.axes.bottom() - cfg.margin, 1e-4);
|
||||
PLOT_CHECK(panel.right() <= layout.axes.right());
|
||||
PLOT_CHECK(panel.bottom() <= layout.axes.bottom());
|
||||
|
||||
const std::vector<plot::u8> frame = read_frame(ctx);
|
||||
const plot::Color opaque{teal.r, teal.g, teal.b, 1.0f};
|
||||
const plot::u32 in_panel = exact_pixels(frame, kW, panel, opaque);
|
||||
PLOT_CHECK(in_panel >= static_cast<plot::u32>(cfg.swatch_w - 2.0f) *
|
||||
static_cast<plot::u32>(cfg.swatch_h - 2.0f));
|
||||
// Every pixel of the swatch colour is inside the panel: none in the x label strip
|
||||
// below the axes, none in the y strip, none anywhere else in the frame.
|
||||
PLOT_CHECK(exact_pixels(frame, kW, kFrameRect, opaque) == in_panel);
|
||||
const plot::Rect below{0.0f, layout.axes.bottom(), static_cast<plot::f32>(kW),
|
||||
static_cast<plot::f32>(kH) - layout.axes.bottom()};
|
||||
PLOT_CHECK(exact_pixels(frame, kW, below, opaque) == 0u);
|
||||
// ... with the name drawn beside it.
|
||||
const plot::u32 name_ink = scan_rect_ink(ctx, panel, r.style().axis_text).pixels;
|
||||
PLOT_CHECK(name_ink >= 16u);
|
||||
std::printf("[legend] bottom-left panel %.2f,%.2f %.2fx%.2f; swatch %u pixels in the panel, %u "
|
||||
"in the frame, %u below the axes; name ink %u\n",
|
||||
panel.x, panel.y, panel.w, panel.h, in_panel,
|
||||
exact_pixels(frame, kW, kFrameRect, opaque), exact_pixels(frame, kW, below, opaque),
|
||||
name_ink);
|
||||
ctx.destroy();
|
||||
}
|
||||
Reference in new issue
Block a user