1
0
mirror of git://projects.qi-hardware.com/eda-tools.git synced 2024-11-17 21:46:15 +02:00

eeshow/gui/over.c (overlay_size, _all): calculate height and width of overlay

This commit is contained in:
Werner Almesberger 2016-08-18 14:44:06 -03:00
parent d364b60ebe
commit 434d942377
2 changed files with 84 additions and 0 deletions

View File

@ -222,6 +222,81 @@ void overlay_draw_all(struct overlay *overlays, cairo_t *cr, int x, int y)
} }
/* ----- Sizing ------------------------------------------------------------ */
void overlay_size(const struct overlay *over, PangoContext *pango_context,
int *w, int *h)
{
const struct overlay_style *style = &over->style;
PangoLayout *layout;
PangoFontDescription *desc;
PangoRectangle ink_rect;
unsigned ink_w, ink_h; /* effectively used text area size */
/*
* Note that we need the caller to provide the Cairo context, because
* the font size changes subtly even between image (which we could
* create locally) and screen (which is better left to the outside
* world).
*/
desc = pango_font_description_from_string(style->font);
layout = pango_layout_new(pango_context);
pango_layout_set_font_description(layout, desc);
pango_layout_set_markup(layout, over->s, -1);
pango_font_description_free(desc);
pango_layout_get_extents(layout, &ink_rect, NULL);
g_object_unref(layout);
ink_w = ink_rect.width / PANGO_SCALE;
ink_h = ink_rect.height / PANGO_SCALE;
ink_w = ink_w > style->wmin ? ink_w : style->wmin;
ink_w = !style->wmax || ink_w < style->wmax ? ink_w : style->wmax;
ink_h = ink_h > style->hmin ? ink_h : style->hmin;
ink_h = !style->hmax || ink_h < style->hmax ? ink_h : style->hmax;
if (w)
*w = ink_w + 2 * over->style.pad;
if (h)
*h = ink_h + 2 * over->style.pad;
}
void overlay_size_all(const struct overlay *overlays,
PangoContext *pango_context, bool dx, bool dy, int *w, int *h)
{
const struct overlay *over;
int w1, h1;
if (w)
*w = 0;
if (h)
*h = 0;
for (over = overlays; over; over = over->next) {
int skip = over == overlays ? 0 : over->style.skip;
overlay_size(over, pango_context, &w1, &h1);
if (w) {
if (dx)
*w += w1 + skip;
else
*w = *w > w1 ? *w : w1;
}
if (h) {
if (dy)
*h += h1 + skip;
else
*h = *h > h1 ? *h : h1;
}
}
}
/* ----- Creation ---------------------------------------------------------- */ /* ----- Creation ---------------------------------------------------------- */

View File

@ -17,6 +17,8 @@
#include <stdint.h> #include <stdint.h>
#include <cairo/cairo.h> #include <cairo/cairo.h>
#include <pango/pangocairo.h>
#include "gui/aoi.h" #include "gui/aoi.h"
@ -46,11 +48,18 @@ void overlay_draw_all_d(struct overlay *overlays, cairo_t *cr,
unsigned x, unsigned y, int dx, int dy); unsigned x, unsigned y, int dx, int dy);
void overlay_draw_all(struct overlay *overlays, cairo_t *cr, int x, int y); void overlay_draw_all(struct overlay *overlays, cairo_t *cr, int x, int y);
void overlay_size(const struct overlay *over, PangoContext *pango_context,
int *w, int *h);
void overlay_size_all(const struct overlay *overlays,
PangoContext *pango_context, bool dx, bool dy, int *w, int *h);
struct overlay *overlay_add(struct overlay **overlays, struct aoi **aois, struct overlay *overlay_add(struct overlay **overlays, struct aoi **aois,
bool (*hover)(void *user, bool on), void (*click)(void *user), void *user); bool (*hover)(void *user, bool on), void (*click)(void *user), void *user);
void overlay_text_raw(struct overlay *over, const char *s); void overlay_text_raw(struct overlay *over, const char *s);
void overlay_text(struct overlay *over, const char *fmt, ...); void overlay_text(struct overlay *over, const char *fmt, ...);
void overlay_style(struct overlay *over, const struct overlay_style *style); void overlay_style(struct overlay *over, const struct overlay_style *style);
void overlay_remove(struct overlay **overlays, struct overlay *over); void overlay_remove(struct overlay **overlays, struct overlay *over);
void overlay_remove_all(struct overlay **overlays); void overlay_remove_all(struct overlay **overlays);