summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume Seguin <guillaume@segu.in>2008-08-22 20:50:30 +0200
committerGuillaume Seguin <guillaume@segu.in>2008-08-22 20:50:30 +0200
commit213e415c7879d9f1b9d2a3a285e79b8974d12591 (patch)
tree4fbd9482624dc66957d26447ee42e78cd233a8f2
parent9012ba238ba4391550f9f9342f6043c452d3d81a (diff)
downloadgmathlib-213e415c7879d9f1b9d2a3a285e79b8974d12591.tar.gz
gmathlib-213e415c7879d9f1b9d2a3a285e79b8974d12591.tar.bz2
* Improve gmathcairo_render_cross_symbol and editor mode support
-rw-r--r--gmathcairo/gmathcairo_renderers.c101
-rw-r--r--gmathcairo/gmathcairo_utils.c87
-rw-r--r--include/gmathcairo.h3
3 files changed, 136 insertions, 55 deletions
diff --git a/gmathcairo/gmathcairo_renderers.c b/gmathcairo/gmathcairo_renderers.c
index 46a21b8..2e5b403 100644
--- a/gmathcairo/gmathcairo_renderers.c
+++ b/gmathcairo/gmathcairo_renderers.c
@@ -515,7 +515,7 @@ render_boundary (GMathCairoContext *ccontext, GMathSymbol *symbol,
if (!symbol || !value)
return NULL;
- return gmathcairo_render_cross_symbol (ccontext, value,
+ return gmathcairo_render_cross_symbol (ccontext, value, NULL,
NULL, symbol->children[1],
NULL, symbol->children[0],
false, true, false, true,
@@ -564,7 +564,7 @@ render_appr (GMathCairoContext *ccontext, GMathSymbol *symbol, float size)
if (!symbol)
return NULL;
- return gmathcairo_render_cross_symbol (ccontext, "→",
+ return gmathcairo_render_cross_symbol (ccontext, "→", NULL,
symbol->children[0],
symbol->children[1],
NULL, NULL,
@@ -743,7 +743,7 @@ render_sum (GMathCairoContext *ccontext, GMathSymbol *symbol, float size)
int x, y, x0, y0;
float size_small = size * 0.5;
- if (!symbol || !symbol->children[2])
+ if (!symbol)
return NULL;
children = g_new0 (GMathBoxed*, 3);
@@ -751,15 +751,24 @@ render_sum (GMathCairoContext *ccontext, GMathSymbol *symbol, float size)
dwr = dwt = dwb = 0;
dhr = dht = dhb = 0;
- children[2] = gmathcairo_render_symbol (ccontext, symbol->children[2],
- size);
- if (!children[2])
+ if (symbol->children[2])
{
- g_free (children);
- return NULL;
+ children[2] = gmathcairo_render_symbol (ccontext, symbol->children[2],
+ size);
+ if (!children[2])
+ {
+ g_free (children);
+ return NULL;
+ }
+ width = dwr = children[2]->box.width;
+ height = dhr = children[2]->box.height;
+ }
+ else
+ {
+ dwr = dhr = 0;
+ width = 0;
+ height = size;
}
- width = dwr = children[2]->box.width;
- height = dhr = children[2]->box.height;
height0 = height;
width0 = height0 * 0.8;
@@ -883,7 +892,7 @@ render_product (GMathCairoContext *ccontext, GMathSymbol *symbol, float size)
int x, y, x0, y0;
float size_small = size * 0.5;
- if (!symbol || !symbol->children[2])
+ if (!symbol)
return NULL;
children = g_new0 (GMathBoxed*, 3);
@@ -891,15 +900,30 @@ render_product (GMathCairoContext *ccontext, GMathSymbol *symbol, float size)
dwr = dwt = dwb = 0;
dhr = dht = dhb = 0;
- children[2] = gmathcairo_render_symbol (ccontext, symbol->children[2],
- size);
- if (!children[2])
+ if (symbol->children[2] || ccontext->editor_mode)
{
- g_free (children);
- return NULL;
+ if (symbol->children[2])
+ children[2] = gmathcairo_render_symbol (ccontext,
+ symbol->children[2],
+ size);
+ if (ccontext->editor_mode && !children[2])
+ children[2] = gmathcairo_render_empty (ccontext,
+ symbol->children[2],
+ size);
+ if (!children[2])
+ {
+ g_free (children);
+ return NULL;
+ }
+ width = dwr = children[2]->box.width;
+ height = dhr = children[2]->box.height;
+ }
+ else
+ {
+ dwr = dhr = 0;
+ width = 0;
+ height = size;
}
- width = dwr = children[2]->box.width;
- height = dhr = children[2]->box.height;
height0 = height;
width0 = height0 * 0.8;
@@ -908,10 +932,12 @@ render_product (GMathCairoContext *ccontext, GMathSymbol *symbol, float size)
width += width0;
width1 = width0;
- if (symbol->children[0])
+ if (symbol->children[0] || ccontext->editor_mode)
{
- children[0] = gmathcairo_render_symbol (ccontext, symbol->children[0],
- size_small);
+ if (symbol->children[0])
+ children[0] = gmathcairo_render_symbol (ccontext,
+ symbol->children[0],
+ size_small);
if (ccontext->editor_mode && !children[0])
children[0] = gmathcairo_render_empty (ccontext,
symbol->children[0],
@@ -928,10 +954,12 @@ render_product (GMathCairoContext *ccontext, GMathSymbol *symbol, float size)
height += dhb;
}
}
- if (symbol->children[1])
+ if (symbol->children[1] || ccontext->editor_mode)
{
- children[1] = gmathcairo_render_symbol (ccontext, symbol->children[1],
- size_small);
+ if (symbol->children[1])
+ children[1] = gmathcairo_render_symbol (ccontext,
+ symbol->children[1],
+ size_small);
if (ccontext->editor_mode && !children[1])
children[1] = gmathcairo_render_empty (ccontext,
symbol->children[1],
@@ -1023,7 +1051,7 @@ render_integral (GMathCairoContext *ccontext, GMathSymbol *symbol, float size)
int x, y, x0, y0;
float size_small = size * 0.5;
- if (!symbol || !symbol->children[2])
+ if (!symbol)
return NULL;
children = g_new0 (GMathBoxed*, 3);
@@ -1031,15 +1059,24 @@ render_integral (GMathCairoContext *ccontext, GMathSymbol *symbol, float size)
dwr = dwt = dwb = 0;
dhr = dht = dhb = 0;
- children[2] = gmathcairo_render_symbol (ccontext, symbol->children[2],
- size);
- if (!children[2])
+ if (symbol->children[2])
{
- g_free (children);
- return NULL;
+ children[2] = gmathcairo_render_symbol (ccontext, symbol->children[2],
+ size);
+ if (!children[2])
+ {
+ g_free (children);
+ return NULL;
+ }
+ width = dwr = children[2]->box.width;
+ height = dhr = children[2]->box.height;
+ }
+ else
+ {
+ dwr = dhr = 0;
+ width = 0;
+ height = size;
}
- width = dwr = children[2]->box.width;
- height = dhr = children[2]->box.height;
height0 = height;
width0 = height0 * 0.6;
diff --git a/gmathcairo/gmathcairo_utils.c b/gmathcairo/gmathcairo_utils.c
index a4fedac..4946c39 100644
--- a/gmathcairo/gmathcairo_utils.c
+++ b/gmathcairo/gmathcairo_utils.c
@@ -152,6 +152,7 @@ gmathcairo_render_empty (GMathCairoContext *ccontext, GMathSymbol *symbol,
* The top & bottom children are rendered at half (0.5) the size */
GMathBoxed*
gmathcairo_render_cross_symbol (GMathCairoContext *ccontext, gchar *value,
+ GMathDrawFunc drawfunc,
GMathSymbol *lsymbol, GMathSymbol *rsymbol,
GMathSymbol *tsymbol, GMathSymbol *bsymbol,
bool has_left, bool has_right,
@@ -162,6 +163,7 @@ gmathcairo_render_cross_symbol (GMathCairoContext *ccontext, gchar *value,
cairo_surface_t *surface;
GMathBoxed **children;
cairo_t *cr;
+ cairo_surface_t *main_surf;
int width, height, width0, height0, width1, height1;
int dwl, dhl, dwr, dhr, dwt, dht, dwb, dhb;
int empty_height;
@@ -169,21 +171,64 @@ gmathcairo_render_cross_symbol (GMathCairoContext *ccontext, gchar *value,
PangoLayout *layout;
float size_small = size * 0.5;
- if (!value)
+ if (!value && !drawfunc)
return NULL;
boxed = gmathboxed_new (symbol);
children = g_new0 (GMathBoxed*, 4);
- gmathcairo_get_layout_and_size (ccontext, value, size,
- &layout, &width0, &height0);
-
dwl = dwr = dwt = dwb = 0;
dhl = dhr = dht = dhb = 0;
empty_height = 0;
- width = width1 = width0;
- height = height0;
+
+ if (value)
+ {
+ gmathcairo_get_layout_and_size (ccontext, value, size,
+ &layout, &width0, &height0);
+ width = width1 = width0;
+ height = height0;
+ if (has_right)
+ {
+ if (rsymbol)
+ children[1] = gmathcairo_render_symbol (ccontext, rsymbol, size);
+ if (ccontext->editor_mode && !children[1])
+ children[1] = gmathcairo_render_empty (ccontext, rsymbol, size);
+ if (children[1])
+ {
+ dwr = children[1]->box.width;
+ dhr = children[1]->box.height;
+ width += dwr;
+ height = MAX (height, dhr);
+ }
+ }
+ }
+ else
+ {
+ if (has_right)
+ {
+ if (rsymbol)
+ children[1] = gmathcairo_render_symbol (ccontext, rsymbol, size);
+ if (ccontext->editor_mode && !children[1])
+ children[1] = gmathcairo_render_empty (ccontext, rsymbol, size);
+ }
+ if (children[1])
+ {
+ dwr = children[1]->box.width;
+ dhr = children[1]->box.height;
+ width = dwr;
+ height = dhr;
+ }
+ else
+ {
+ width = 0;
+ height = size;
+ }
+ height0 = height;
+ width1 = width0 = height0 * 0.6;
+ width += width1;
+ main_surf = (*drawfunc) (width0, height0);
+ }
if (has_left)
{
@@ -199,20 +244,6 @@ gmathcairo_render_cross_symbol (GMathCairoContext *ccontext, gchar *value,
height = MAX (height, dhl);
}
}
- if (has_right)
- {
- if (rsymbol)
- children[1] = gmathcairo_render_symbol (ccontext, rsymbol, size);
- if (ccontext->editor_mode && !children[1])
- children[1] = gmathcairo_render_empty (ccontext, rsymbol, size);
- if (children[1])
- {
- dwr = children[1]->box.width;
- dhr = children[1]->box.height;
- width += dwr;
- height = MAX (height, dhr);
- }
- }
height1 = height;
empty_height = (height - height0) / 2;
if (has_top)
@@ -271,8 +302,17 @@ gmathcairo_render_cross_symbol (GMathCairoContext *ccontext, gchar *value,
cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
cairo_save (cr);
- cairo_move_to (cr, x0, y0);
- pango_cairo_show_layout (cr, layout);
+ if (value)
+ {
+ cairo_move_to (cr, x0, y0);
+ pango_cairo_show_layout (cr, layout);
+ }
+ else
+ {
+ cairo_set_source_surface (cr, main_surf, x0, y0);
+ cairo_paint (cr);
+ cairo_surface_destroy (main_surf);
+ }
cairo_restore (cr);
if (children[0])
@@ -330,7 +370,8 @@ gmathcairo_render_cross_symbol (GMathCairoContext *ccontext, gchar *value,
}
/* Free things */
- g_object_unref (layout);
+ if (value)
+ g_object_unref (layout);
cairo_destroy (cr);
g_free (children);
diff --git a/include/gmathcairo.h b/include/gmathcairo.h
index 8017ef1..fc99d06 100644
--- a/include/gmathcairo.h
+++ b/include/gmathcairo.h
@@ -44,6 +44,8 @@ typedef GMathBoxed* (*GMathCairoRenderProc) (GMathCairoContext *ccontext,
GMathSymbol *symbol,
float size);
+typedef cairo_surface_t* (*GMathDrawFunc) (int width, int height);
+
struct _GMathCairoContext
{
GMathContext *context; /* Associated GMathContext */
@@ -131,6 +133,7 @@ gmathcairo_get_surface_size (cairo_surface_t *surface, int *width, int *height);
GMathBoxed*
gmathcairo_render_cross_symbol (GMathCairoContext *ccontext, gchar *value,
+ GMathDrawFunc drawfunc,
GMathSymbol *lsymbol, GMathSymbol *rsymbol,
GMathSymbol *tsymbol, GMathSymbol *bsymbol,
bool has_left, bool has_right,