From patchwork Mon Jul 6 12:35:39 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Lespiau, Damien" X-Patchwork-Id: 6723351 Return-Path: X-Original-To: patchwork-intel-gfx@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 78482C05AC for ; Mon, 6 Jul 2015 12:36:01 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 82F1D205F9 for ; Mon, 6 Jul 2015 12:36:00 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id 6F8D7202F0 for ; Mon, 6 Jul 2015 12:35:59 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id DE7276E875; Mon, 6 Jul 2015 05:35:58 -0700 (PDT) X-Original-To: intel-gfx@lists.freedesktop.org Delivered-To: intel-gfx@lists.freedesktop.org Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by gabe.freedesktop.org (Postfix) with ESMTP id D015A6E879 for ; Mon, 6 Jul 2015 05:35:56 -0700 (PDT) Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga101.jf.intel.com with ESMTP; 06 Jul 2015 05:35:57 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.15,414,1432623600"; d="scan'208";a="741421048" Received: from magarwal-mobl.amr.corp.intel.com (HELO strange.amr.corp.intel.com) ([10.254.77.111]) by fmsmga001.fm.intel.com with ESMTP; 06 Jul 2015 05:35:55 -0700 From: Damien Lespiau To: intel-gfx@lists.freedesktop.org Date: Mon, 6 Jul 2015 13:35:39 +0100 Message-Id: <1436186144-19665-12-git-send-email-damien.lespiau@intel.com> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1436186144-19665-1-git-send-email-damien.lespiau@intel.com> References: <1436186144-19665-1-git-send-email-damien.lespiau@intel.com> MIME-Version: 1.0 Subject: [Intel-gfx] [PATCH i-g-t 11/16] plot: Add a title to plots X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" X-Spam-Status: No, score=-4.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Signed-off-by: Damien Lespiau --- lib/igt_plot.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/igt_plot.h | 2 ++ lib/tests/igt_plot.c | 1 + 3 files changed, 62 insertions(+) diff --git a/lib/igt_plot.c b/lib/igt_plot.c index afe4a1c..2ca005e 100644 --- a/lib/igt_plot.c +++ b/lib/igt_plot.c @@ -303,6 +303,8 @@ void igt_plot_fini(igt_plot_t *plot) { unsigned int i; + free(plot->title); + for (i = 0; i < plot->n_valid_contexts; i++) igt_plot_ctx_fini(&plot->contexts[i]); @@ -314,6 +316,22 @@ void igt_plot_fini(igt_plot_t *plot) } /** + * igt_plot_set_title: + * @plot: An #igt_plot_t instance + * @title: An UTF-8 string + * + * Set a title for the plot. + */ +void igt_plot_set_title(igt_plot_t *plot, const char *title) +{ + free(plot->title); + plot->title = NULL; + if (!title) + return; + plot->title = strndup(title, 64); +} + +/** * igt_plot_set_color: * @plot: An #igt_plot_t instance * @r: Red component in the [0, 1] range @@ -407,9 +425,12 @@ typedef struct { } igt_label_t; typedef struct { + double inner_padding; /* padding for most things */ double tick_label_padding; /* padding between label and axis */ + double title_font_size; double tick_label_font_size; igt_box_t plot_area; + igt_label_t title; igt_label_t *x_tick_labels; igt_label_t *y_tick_labels; } flush_t; @@ -470,6 +491,22 @@ static void igt_plot_draw_background(igt_plot_t *plot, flush_t *flush) cairo_fill(plot->cr); } +static void igt_plot_draw_title(igt_plot_t *plot, flush_t *flush) +{ + igt_box_t *area = &flush->plot_area; + double x, y; + + if (!plot->title) + return; + + cairo_set_font_size(plot->cr, flush->title_font_size); + cairo_set_source_rgb(plot->cr, 0.0, 0.0, 0.0); + + x = area->x1 + (area->x2 - area->x1 ) / 2.0; + y = flush->plot_area.y1 - flush->inner_padding; + igt_plot_draw_text(plot, SNAP(x), SNAP(y), &flush->title); +} + static void igt_plot_draw_grid(igt_plot_t *plot, flush_t *flush) { unsigned int i, n_ticks; @@ -632,6 +669,20 @@ static void igt_plot_draw_axis(igt_plot_t *plot, flush_t *flush) } +static void igt_plot_layout_title(igt_plot_t *plot, flush_t *flush) +{ + igt_label_t *label = &flush->title; + + if (!plot->title) + return; + + label->text = plot->title; + label->halign = IGT_ALIGN_CENTER; + label->valign = IGT_ALIGN_BOTTOM; + cairo_set_font_size(plot->cr, flush->title_font_size); + cairo_text_extents(plot->cr, label->text, &label->extents); +} + static void igt_plot_layout_tick_labels(igt_plot_t *plot, igt_plot_axis_t *axis, igt_label_t *labels, @@ -668,7 +719,9 @@ static void igt_plot_layout(igt_plot_t *plot, flush_t *flush) const double outer_padding = 0.10; double max_width, max_height; + flush->inner_padding = plot_length(plot, 0.05); flush->tick_label_padding = plot_length(plot, 0.02); + flush->title_font_size = round(0.025 * plot->width); flush->tick_label_font_size = round(0.015 * plot->width); /* outer padding */ @@ -677,6 +730,11 @@ static void igt_plot_layout(igt_plot_t *plot, flush_t *flush) flush->plot_area.x2 = SNAP(plot->width * (1.0 - outer_padding)); flush->plot_area.y2 = SNAP(plot->height * (1.0 - outer_padding)); + /* plot title */ + igt_plot_layout_title(plot, flush); + flush->plot_area.y1 += flush->title.extents.height + + flush->inner_padding; + /* measure tick labels and adjust the plot area */ cairo_set_font_size(plot->cr, flush->tick_label_font_size); igt_plot_layout_tick_labels(plot, &plot->x_axis, flush->x_tick_labels, @@ -720,6 +778,7 @@ void igt_plot_write(igt_plot_t *plot, const char *filename) igt_plot_layout(plot, &flush); igt_plot_draw_background(plot, &flush); + igt_plot_draw_title(plot, &flush); igt_plot_draw_axis(plot, &flush); for (i = 0; i < plot->n_valid_contexts; i++) igt_plot_draw_one(plot, &plot->contexts[i], &flush); diff --git a/lib/igt_plot.h b/lib/igt_plot.h index c8b031f..cca498a 100644 --- a/lib/igt_plot.h +++ b/lib/igt_plot.h @@ -103,6 +103,7 @@ typedef struct { /* plot-wide states */ unsigned int width, height; + char *title; igt_trbl_t margin; igt_plot_axis_t x_axis, y_axis; igt_plot_axis_t x_axis_top, y_axis_right; @@ -127,6 +128,7 @@ typedef enum { void igt_plot_init(igt_plot_t *plot, unsigned int width, unsigned int height); void igt_plot_fini(igt_plot_t *plot); +void igt_plot_set_title(igt_plot_t *plot, const char *title); void igt_plot_set_color(igt_plot_t *plot, double r, double g, double b, double a); void igt_plot_set_line_width(igt_plot_t *plot, double width); diff --git a/lib/tests/igt_plot.c b/lib/tests/igt_plot.c index 2f5ad27..81946f3 100644 --- a/lib/tests/igt_plot.c +++ b/lib/tests/igt_plot.c @@ -83,6 +83,7 @@ static void test_simple_plot(void) y = igt_vector_map(x, f); igt_plot_init(&plot, 800, 600); + igt_plot_set_title(&plot, "f(x) = sin(2?x)"); igt_plot_set_color(&plot, 0.0, 0.0, 1.0, 1.0); igt_plot_draw(&plot, x, y); igt_plot_write(&plot, "test_simple_plot.png");