From 5610713c6457ee848fab8fb6d4ee45f36695e6dc Mon Sep 17 00:00:00 2001 From: Koos Vriezen Date: Fri, 18 Apr 2008 23:13:03 +0200 Subject: [PATCH] Add LaughSize, a type that handles absolute and relative values Add LaughSizeSetting, a type that gets x,y,w,h from various LaughSize combi's --- src/Makefile.am | 2 +- src/laugh-dom.c | 1 - src/laugh-layout.c | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/laugh-layout.h | 27 +++++++++++++ 4 files changed, 139 insertions(+), 2 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 497cd61..99e0487 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,4 +1,4 @@ -AM_CFLAGS = $(DEPS_CFLAGS) +AM_CFLAGS = $(DEPS_CFLAGS) -DLAUGH_DOM_TEST laugh_LDADD = $(DEPS_LIBS) -lexpat laugh_SOURCES = laugh-timing.c laugh-io.c laugh-dom.c laugh-layout.c diff --git a/src/laugh-dom.c b/src/laugh-dom.c index 720bfa6..1c8157b 100644 --- a/src/laugh-dom.c +++ b/src/laugh-dom.c @@ -549,7 +549,6 @@ void laugh_node_init (LaughNode *node, LaughInitializer *initializer) klass->init (node, initializer); } -#define LAUGH_DOM_TEST #ifdef LAUGH_DOM_TEST /* gcc laugh-dom.c laugh-io.c -o domtest `pkg-config --libs --cflags gio-2.0 glib-2.0 gthread-2.0` -lexpat */ #include diff --git a/src/laugh-layout.c b/src/laugh-layout.c index 89dda4c..96cb93f 100644 --- a/src/laugh-layout.c +++ b/src/laugh-layout.c @@ -34,6 +34,7 @@ #include "laugh-io.h" #include +#include #include #define LAUGH_LAYOUT_GET_PRIVATE(obj) \ @@ -156,6 +157,74 @@ GType laugh_layout_get_type (void) return type; } +void laugh_size_set_string (LaughSize *size, const gchar *value) +{ + char *ep; + char *p = strchr (value, '%'); + + if (p) { + size->abs_size = 0.0; + size->perc_size = strtod (value, &ep); + size->is_set = ep != value; + } else { + size->abs_size = strtod (value, &ep); + size->perc_size = 0.0; + size->is_set = ep != value; + } +} + +float laugh_size_get_size (LaughSize *size, float relative_to) +{ + return size->abs_size + size->perc_size * relative_to / 100; +} + +void laugh_size_setting_get (LaughSizeSetting *sizes, float pw, float ph, + float *x, float *y, float *w, float *h) { + if (sizes->left.is_set) { + *x = laugh_size_get_size (&sizes->left, pw); + } else if (sizes->width.is_set) { + if (sizes->right.is_set) + *x = pw - + laugh_size_get_size (&sizes->width, pw) - + laugh_size_get_size (&sizes->right, pw); + else + *x = (pw - laugh_size_get_size (&sizes->width, pw)) / 2; + } else { + *x = 0; + } + + if (sizes->top.is_set) { + *y = laugh_size_get_size (&sizes->top, ph); + } else if (sizes->height.is_set) { + if (sizes->bottom.is_set) + *y = ph - + laugh_size_get_size (&sizes->height, ph) - + laugh_size_get_size (&sizes->bottom, ph); + else + *y = (ph - laugh_size_get_size (&sizes->height, ph)) / 2; + } else { + *y = 0; + } + + if (sizes->width.is_set) + *w = laugh_size_get_size (&sizes->width, pw); + else if (sizes->right.is_set) + *w = pw - *x -laugh_size_get_size (&sizes->right, pw); + else + *w = pw - *x; + if (*w < 0) + *w = 0; + + if (sizes->height.is_set) + *h = laugh_size_get_size (&sizes->height, ph); + else if (sizes->bottom.is_set) + *h = ph - *y - laugh_size_get_size (&sizes->bottom, ph); + else + *h = ph - *y; + if (*h < 0) + *h = 0; +} + LaughNode *laugh_layout_new (LaughDocument *doc, LaughNodeTagId id, GHashTable *attributes) { @@ -165,3 +234,45 @@ LaughNode *laugh_layout_new (LaughDocument *doc, LaughNodeTagId id, return node; } + +#ifdef LAIGH_TEST_SIZES +/* gcc laugh-dom.c laugh-io.c laugh-layout.c -o sizestest `pkg-config --libs --cflags glib-2.0 gio-2.0 gthread-2.0` -lexpat -DLAIGH_TEST_SIZES */ +void test_sizes (float pw, float ph, + const gchar *left, const gchar *top, + const gchar *width, const gchar *height, + const gchar *right, const gchar *bottom) +{ + LaughSizeSetting sizes; + float x, y, w, h; + memset (&sizes, 0, sizeof (LaughSizeSetting)); + if (left) + laugh_size_set_string (&sizes.left, left); + if (top) + laugh_size_set_string (&sizes.top, top); + if (width) + laugh_size_set_string (&sizes.width, width); + if (height) + laugh_size_set_string (&sizes.height, height); + if (right) + laugh_size_set_string (&sizes.right, right); + if (bottom) + laugh_size_set_string (&sizes.bottom, bottom); + laugh_size_setting_get (&sizes, pw, ph, &x, &y, &w, &h); + g_printf ("left %s top %s width %s height %s right %s bottom %s\n" + "\t(%.1fx%.1f) => [%.1f, %.1f %.1fx%.1f]\n", + pw, ph, + left, top, width, height, right, bottom, + x, y, w, h); +} + +int main() +{ + test_sizes (320, 240, "50", "30", "200", "180", NULL, NULL); + test_sizes (320, 240, "50", "30", NULL, NULL, "40", "20"); + test_sizes (320, 240, "50", "30", "50%", "60%", NULL, NULL); + test_sizes (320, 240, "50", "30", NULL, NULL, "10%", "20%"); + + return 0; +} + +#endif diff --git a/src/laugh-layout.h b/src/laugh-layout.h index 88e8922..f884478 100644 --- a/src/laugh-layout.h +++ b/src/laugh-layout.h @@ -53,12 +53,32 @@ G_BEGIN_DECLS typedef struct _LaughLayout LaughLayout; typedef struct _LaughLayoutClass LaughLayoutClass; typedef struct _LaughLayoutPrivate LaughLayoutPrivate; +typedef struct _LaughSize LaughSize; +typedef struct _LaughSizeSetting LaughSizeSetting; + +struct _LaughSize +{ + float perc_size; + float abs_size; + gboolean is_set; +}; + +struct _LaughSizeSetting +{ + LaughSize left; + LaughSize top; + LaughSize width; + LaughSize height; + LaughSize right; + LaughSize bottom; +}; struct _LaughLayout { LaughNode parent; /*< public >*/ + LaughSizeSetting size_setting; /*< private >*/ LaughLayoutPrivate *priv; @@ -71,6 +91,13 @@ struct _LaughLayoutClass GType laugh_layout_get_type (void) G_GNUC_CONST; +void laugh_size_set_string (LaughSize *size, const gchar *value); + +float laugh_size_get_size (LaughSize *size, float relative_to); + +void laugh_size_setting_get (LaughSizeSetting *sizes, float pw, float ph, + float *x, float *y, float *w, float *h); + LaughNode *laugh_layout_new (LaughDocument *doc, LaughNodeTagId id, GHashTable *attributes); -- 2.11.4.GIT