reuse code.

This commit is contained in:
Martin Preuss
2024-05-23 13:34:23 +02:00
parent 5d869ce215
commit 5fc413c46d

View File

@@ -41,6 +41,13 @@ static int _getTextWidth(AQDG_DRAW_CONTEXT *g, int fontId, const char *text);
static int _getTextHeight(AQDG_DRAW_CONTEXT *g, int fontId, const char *text); static int _getTextHeight(AQDG_DRAW_CONTEXT *g, int fontId, const char *text);
static int _finish(AQDG_DRAW_CONTEXT *g); static int _finish(AQDG_DRAW_CONTEXT *g);
static AQDG_DRAW_CONTEXT_CAIRO *_ensureFullCairoContext(const AQDG_DRAW_CONTEXT *g);
static AQDG_DRAW_CONTEXT_CAIRO *_ensureCairoSurface(const AQDG_DRAW_CONTEXT *g);
static void _setColourAndWidthFromPen(cairo_t *cr, const AQDG_DRAW_PEN *pen);
static void _setDashType(cairo_t *cr, int dashType);
static void _setFont(cairo_t *cr, AQDG_DRAW_FONT *font);
/* ------------------------------------------------------------------------------------------------ /* ------------------------------------------------------------------------------------------------
@@ -238,12 +245,9 @@ int _penCreate(AQDG_DRAW_CONTEXT *g, uint32_t frontColour, int lineWidth, int da
AQDG_DRAW_CONTEXT_CAIRO *xg; AQDG_DRAW_CONTEXT_CAIRO *xg;
AQDG_DRAW_PEN *pen; AQDG_DRAW_PEN *pen;
assert(g); xg=_ensureCairoSurface(g);
xg=GWEN_INHERIT_GETDATA(AQDG_DRAW_CONTEXT, AQDG_DRAW_CONTEXT_CAIRO, g); if (xg==NULL) {
assert(xg); DBG_INFO(NULL, "here");
if (xg->surface==NULL) {
DBG_ERROR(0, "No surface created");
return GWEN_ERROR_INVALID; return GWEN_ERROR_INVALID;
} }
@@ -264,12 +268,9 @@ int _penRelease(AQDG_DRAW_CONTEXT *g, int penId)
AQDG_DRAW_CONTEXT_CAIRO *xg; AQDG_DRAW_CONTEXT_CAIRO *xg;
AQDG_DRAW_PEN *pen; AQDG_DRAW_PEN *pen;
assert(g); xg=_ensureCairoSurface(g);
xg=GWEN_INHERIT_GETDATA(AQDG_DRAW_CONTEXT, AQDG_DRAW_CONTEXT_CAIRO, g); if (xg==NULL) {
assert(xg); DBG_INFO(NULL, "here");
if (xg->surface==NULL) {
DBG_ERROR(0, "No surface created");
return GWEN_ERROR_INVALID; return GWEN_ERROR_INVALID;
} }
@@ -290,22 +291,10 @@ int _drawLine(AQDG_DRAW_CONTEXT *g, int penId, int xStart, int yStart, int xEnd,
{ {
AQDG_DRAW_CONTEXT_CAIRO *xg; AQDG_DRAW_CONTEXT_CAIRO *xg;
AQDG_DRAW_PEN *pen; AQDG_DRAW_PEN *pen;
uint32_t fgCol;
double red, green, blue;
int i;
int dashType;
assert(g); xg=_ensureFullCairoContext(g);
xg=GWEN_INHERIT_GETDATA(AQDG_DRAW_CONTEXT, AQDG_DRAW_CONTEXT_CAIRO, g); if (xg==NULL) {
assert(xg); DBG_INFO(NULL, "here");
if (xg->surface==NULL) {
DBG_ERROR(0, "No surface created");
return GWEN_ERROR_INVALID;
}
if (xg->cr==NULL) {
DBG_ERROR(0, "Graphics context not active, call \"AQDG_Draw_Context_BeginDraw()\" before");
return GWEN_ERROR_INVALID; return GWEN_ERROR_INVALID;
} }
@@ -315,40 +304,8 @@ int _drawLine(AQDG_DRAW_CONTEXT *g, int penId, int xStart, int yStart, int xEnd,
return GWEN_ERROR_NOT_FOUND; return GWEN_ERROR_NOT_FOUND;
} }
/* set foreground color */; _setColourAndWidthFromPen(xg->cr, pen);
fgCol=AQDG_Draw_Pen_GetFrontColour(pen); _setDashType(xg->cr, AQDG_Draw_Pen_GetDashType(pen));
red=(double)((fgCol>>24) & 0xff)/255.0;
green=(double)((fgCol>>16) & 0xff)/255.0;
blue=(double)((fgCol>>8) & 0xff)/255.0;
cairo_set_source_rgb(xg->cr, red, green, blue);
/* set line width */
i=AQDG_Draw_Pen_GetLineWidth(pen);
cairo_set_line_width(xg->cr, i);
dashType=AQDG_Draw_Pen_GetDashType(pen);
switch (dashType) {
default:
case 0:
cairo_set_dash(xg->cr, NULL, 0, 0.0);
break;
case 1:
cairo_set_dash(xg->cr, AQDG_Draw_ContextCairo_DashPattern1, 2, 0.0);
break;
case 2:
cairo_set_dash(xg->cr, AQDG_Draw_ContextCairo_DashPattern2, 2, 0.0);
break;
case 3:
cairo_set_dash(xg->cr, AQDG_Draw_ContextCairo_DashPattern3, 2, 0.0);
break;
case 4:
cairo_set_dash(xg->cr, AQDG_Draw_ContextCairo_DashPattern4, 2, 0.0);
break;
case 5:
cairo_set_dash(xg->cr, AQDG_Draw_ContextCairo_DashPattern5, 2, 0.0);
break;
}
/* draw line from start to end position */ /* draw line from start to end position */
cairo_move_to(xg->cr, xStart, yStart); cairo_move_to(xg->cr, xStart, yStart);
@@ -364,21 +321,10 @@ int _drawRect(AQDG_DRAW_CONTEXT *g, int penId, int x, int y, int w, int h)
{ {
AQDG_DRAW_CONTEXT_CAIRO *xg; AQDG_DRAW_CONTEXT_CAIRO *xg;
AQDG_DRAW_PEN *pen; AQDG_DRAW_PEN *pen;
uint32_t fgCol;
double red, green, blue;
int i;
assert(g); xg=_ensureFullCairoContext(g);
xg=GWEN_INHERIT_GETDATA(AQDG_DRAW_CONTEXT, AQDG_DRAW_CONTEXT_CAIRO, g); if (xg==NULL) {
assert(xg); DBG_INFO(NULL, "here");
if (xg->surface==NULL) {
DBG_ERROR(0, "No surface created");
return GWEN_ERROR_INVALID;
}
if (xg->cr==NULL) {
DBG_INFO(0, "Graphics context not active, call \"AQDG_Draw_Context_BeginDraw()\" before");
return GWEN_ERROR_INVALID; return GWEN_ERROR_INVALID;
} }
@@ -388,16 +334,7 @@ int _drawRect(AQDG_DRAW_CONTEXT *g, int penId, int x, int y, int w, int h)
return GWEN_ERROR_NOT_FOUND; return GWEN_ERROR_NOT_FOUND;
} }
/* set foreground color */; _setColourAndWidthFromPen(xg->cr, pen);
fgCol=AQDG_Draw_Pen_GetFrontColour(pen);
red=(double)((fgCol>>24) & 0xff)/255.0;
green=(double)((fgCol>>16) & 0xff)/255.0;
blue=(double)((fgCol>>8) & 0xff)/255.0;
cairo_set_source_rgb(xg->cr, red, green, blue);
/* set line width */
i=AQDG_Draw_Pen_GetLineWidth(pen);
cairo_set_line_width(xg->cr, i);
cairo_set_dash(xg->cr, NULL, 0, 0.0); cairo_set_dash(xg->cr, NULL, 0, 0.0);
@@ -414,21 +351,10 @@ int _drawFilledRect(AQDG_DRAW_CONTEXT *g, int penId, int x, int y, int w, int h)
{ {
AQDG_DRAW_CONTEXT_CAIRO *xg; AQDG_DRAW_CONTEXT_CAIRO *xg;
AQDG_DRAW_PEN *pen; AQDG_DRAW_PEN *pen;
uint32_t fgCol;
double red, green, blue;
int i;
assert(g); xg=_ensureFullCairoContext(g);
xg=GWEN_INHERIT_GETDATA(AQDG_DRAW_CONTEXT, AQDG_DRAW_CONTEXT_CAIRO, g); if (xg==NULL) {
assert(xg); DBG_INFO(NULL, "here");
if (xg->surface==NULL) {
DBG_ERROR(0, "No surface created");
return GWEN_ERROR_INVALID;
}
if (xg->cr==NULL) {
DBG_INFO(0, "Graphics context not active, call \"AQDG_Draw_Context_BeginDraw()\" before");
return GWEN_ERROR_INVALID; return GWEN_ERROR_INVALID;
} }
@@ -438,16 +364,7 @@ int _drawFilledRect(AQDG_DRAW_CONTEXT *g, int penId, int x, int y, int w, int h)
return GWEN_ERROR_NOT_FOUND; return GWEN_ERROR_NOT_FOUND;
} }
/* set foreground color */; _setColourAndWidthFromPen(xg->cr, pen);
fgCol=AQDG_Draw_Pen_GetFrontColour(pen);
red=(double)((fgCol>>24) & 0xff)/255.0;
green=(double)((fgCol>>16) & 0xff)/255.0;
blue=(double)((fgCol>>8) & 0xff)/255.0;
cairo_set_source_rgb(xg->cr, red, green, blue);
/* set line width */
i=AQDG_Draw_Pen_GetLineWidth(pen);
cairo_set_line_width(xg->cr, i);
cairo_set_dash(xg->cr, NULL, 0, 0.0); cairo_set_dash(xg->cr, NULL, 0, 0.0);
@@ -466,12 +383,9 @@ int _fontCreate(AQDG_DRAW_CONTEXT *g, const char *face, int fontSize, int fontSl
AQDG_DRAW_CONTEXT_CAIRO *xg; AQDG_DRAW_CONTEXT_CAIRO *xg;
AQDG_DRAW_FONT *font; AQDG_DRAW_FONT *font;
assert(g); xg=_ensureCairoSurface(g);
xg=GWEN_INHERIT_GETDATA(AQDG_DRAW_CONTEXT, AQDG_DRAW_CONTEXT_CAIRO, g); if (xg==NULL) {
assert(xg); DBG_INFO(NULL, "here");
if (xg->surface==NULL) {
DBG_ERROR(0, "No surface created");
return GWEN_ERROR_INVALID; return GWEN_ERROR_INVALID;
} }
@@ -494,12 +408,9 @@ int _fontRelease(AQDG_DRAW_CONTEXT *g, int fontId)
AQDG_DRAW_CONTEXT_CAIRO *xg; AQDG_DRAW_CONTEXT_CAIRO *xg;
AQDG_DRAW_FONT *font; AQDG_DRAW_FONT *font;
assert(g); xg=_ensureCairoSurface(g);
xg=GWEN_INHERIT_GETDATA(AQDG_DRAW_CONTEXT, AQDG_DRAW_CONTEXT_CAIRO, g); if (xg==NULL) {
assert(xg); DBG_INFO(NULL, "here");
if (xg->surface==NULL) {
DBG_ERROR(0, "No surface created");
return GWEN_ERROR_INVALID; return GWEN_ERROR_INVALID;
} }
@@ -520,25 +431,12 @@ int _drawText(AQDG_DRAW_CONTEXT *g, int penId, int fontId, int direction, int x,
{ {
AQDG_DRAW_CONTEXT_CAIRO *xg; AQDG_DRAW_CONTEXT_CAIRO *xg;
AQDG_DRAW_FONT *font; AQDG_DRAW_FONT *font;
int crSlant;
int crWeight;
AQDG_DRAW_PEN *pen; AQDG_DRAW_PEN *pen;
uint32_t fgCol;
double red, green, blue;
int i;
cairo_font_extents_t extents; cairo_font_extents_t extents;
assert(g); xg=_ensureFullCairoContext(g);
xg=GWEN_INHERIT_GETDATA(AQDG_DRAW_CONTEXT, AQDG_DRAW_CONTEXT_CAIRO, g); if (xg==NULL) {
assert(xg); DBG_INFO(NULL, "here");
if (xg->surface==NULL) {
DBG_ERROR(0, "No surface created");
return GWEN_ERROR_INVALID;
}
if (xg->cr==NULL) {
DBG_INFO(0, "Graphics context not active, call \"AQDG_Draw_Context_BeginDraw()\" before");
return GWEN_ERROR_INVALID; return GWEN_ERROR_INVALID;
} }
@@ -549,16 +447,7 @@ int _drawText(AQDG_DRAW_CONTEXT *g, int penId, int fontId, int direction, int x,
return GWEN_ERROR_NOT_FOUND; return GWEN_ERROR_NOT_FOUND;
} }
/* set foreground color */; _setColourAndWidthFromPen(xg->cr, pen);
fgCol=AQDG_Draw_Pen_GetFrontColour(pen);
red=(double)((fgCol>>24) & 0xff)/255.0;
green=(double)((fgCol>>16) & 0xff)/255.0;
blue=(double)((fgCol>>8) & 0xff)/255.0;
cairo_set_source_rgb(xg->cr, red, green, blue);
/* set line width */
i=AQDG_Draw_Pen_GetLineWidth(pen);
cairo_set_line_width(xg->cr, i);
/* get and handle font */ /* get and handle font */
font=AQDG_Draw_Font_List_GetById(xg->fontList, fontId); font=AQDG_Draw_Font_List_GetById(xg->fontList, fontId);
@@ -566,28 +455,7 @@ int _drawText(AQDG_DRAW_CONTEXT *g, int penId, int fontId, int direction, int x,
DBG_INFO(0, "Font %d not found", fontId); DBG_INFO(0, "Font %d not found", fontId);
return GWEN_ERROR_NOT_FOUND; return GWEN_ERROR_NOT_FOUND;
} }
_setFont(xg->cr, font);
switch (AQDG_Draw_Font_GetFontSlant(font)) {
default:
case AQDG_Slant_None:
crSlant=CAIRO_FONT_SLANT_NORMAL;
break;
case AQDG_Slant_Italic:
crSlant=CAIRO_FONT_SLANT_ITALIC;
break;
}
switch (AQDG_Draw_Font_GetFontWeight(font)) {
default:
case AQDG_Weight_None:
crWeight=CAIRO_FONT_WEIGHT_NORMAL;
break;
case AQDG_Weight_Bold:
crWeight=CAIRO_FONT_WEIGHT_BOLD;
break;
}
cairo_select_font_face(xg->cr, AQDG_Draw_Font_GetFontName(font), crSlant, crWeight);
cairo_set_font_size(xg->cr, AQDG_Draw_Font_GetFontSize(font));
cairo_font_extents(xg->cr, &extents); cairo_font_extents(xg->cr, &extents);
@@ -604,21 +472,11 @@ int _getTextWidth(AQDG_DRAW_CONTEXT *g, int fontId, const char *text)
{ {
AQDG_DRAW_CONTEXT_CAIRO *xg; AQDG_DRAW_CONTEXT_CAIRO *xg;
AQDG_DRAW_FONT *font; AQDG_DRAW_FONT *font;
int crSlant;
int crWeight;
cairo_text_extents_t extents; cairo_text_extents_t extents;
assert(g); xg=_ensureFullCairoContext(g);
xg=GWEN_INHERIT_GETDATA(AQDG_DRAW_CONTEXT, AQDG_DRAW_CONTEXT_CAIRO, g); if (xg==NULL) {
assert(xg); DBG_INFO(NULL, "here");
if (xg->surface==NULL) {
DBG_ERROR(0, "No surface created");
return GWEN_ERROR_INVALID;
}
if (xg->cr==NULL) {
DBG_INFO(0, "Graphics context not active, call \"AQDG_Draw_Context_BeginDraw()\" before");
return GWEN_ERROR_INVALID; return GWEN_ERROR_INVALID;
} }
@@ -629,27 +487,7 @@ int _getTextWidth(AQDG_DRAW_CONTEXT *g, int fontId, const char *text)
return GWEN_ERROR_NOT_FOUND; return GWEN_ERROR_NOT_FOUND;
} }
switch (AQDG_Draw_Font_GetFontSlant(font)) { _setFont(xg->cr, font);
default:
case AQDG_Slant_None:
crSlant=CAIRO_FONT_SLANT_NORMAL;
break;
case AQDG_Slant_Italic:
crSlant=CAIRO_FONT_SLANT_ITALIC;
break;
}
switch (AQDG_Draw_Font_GetFontWeight(font)) {
default:
case AQDG_Weight_None:
crWeight=CAIRO_FONT_WEIGHT_NORMAL;
break;
case AQDG_Weight_Bold:
crWeight=CAIRO_FONT_WEIGHT_BOLD;
break;
}
cairo_select_font_face(xg->cr, AQDG_Draw_Font_GetFontName(font), crSlant, crWeight);
cairo_set_font_size(xg->cr, AQDG_Draw_Font_GetFontSize(font));
cairo_text_extents(xg->cr, text, &extents); cairo_text_extents(xg->cr, text, &extents);
@@ -662,21 +500,11 @@ int _getTextHeight(AQDG_DRAW_CONTEXT *g, int fontId, const char *text)
{ {
AQDG_DRAW_CONTEXT_CAIRO *xg; AQDG_DRAW_CONTEXT_CAIRO *xg;
AQDG_DRAW_FONT *font; AQDG_DRAW_FONT *font;
int crSlant;
int crWeight;
cairo_text_extents_t extents; cairo_text_extents_t extents;
assert(g); xg=_ensureFullCairoContext(g);
xg=GWEN_INHERIT_GETDATA(AQDG_DRAW_CONTEXT, AQDG_DRAW_CONTEXT_CAIRO, g); if (xg==NULL) {
assert(xg); DBG_INFO(NULL, "here");
if (xg->surface==NULL) {
DBG_ERROR(0, "No surface created");
return GWEN_ERROR_INVALID;
}
if (xg->cr==NULL) {
DBG_INFO(0, "Graphics context not active, call \"AQDG_Draw_Context_BeginDraw()\" before");
return GWEN_ERROR_INVALID; return GWEN_ERROR_INVALID;
} }
@@ -687,27 +515,7 @@ int _getTextHeight(AQDG_DRAW_CONTEXT *g, int fontId, const char *text)
return GWEN_ERROR_NOT_FOUND; return GWEN_ERROR_NOT_FOUND;
} }
switch (AQDG_Draw_Font_GetFontSlant(font)) { _setFont(xg->cr, font);
default:
case AQDG_Slant_None:
crSlant=CAIRO_FONT_SLANT_NORMAL;
break;
case AQDG_Slant_Italic:
crSlant=CAIRO_FONT_SLANT_ITALIC;
break;
}
switch (AQDG_Draw_Font_GetFontWeight(font)) {
default:
case AQDG_Weight_None:
crWeight=CAIRO_FONT_WEIGHT_NORMAL;
break;
case AQDG_Weight_Bold:
crWeight=CAIRO_FONT_WEIGHT_BOLD;
break;
}
cairo_select_font_face(xg->cr, AQDG_Draw_Font_GetFontName(font), crSlant, crWeight);
cairo_set_font_size(xg->cr, AQDG_Draw_Font_GetFontSize(font));
cairo_text_extents(xg->cr, text, &extents); cairo_text_extents(xg->cr, text, &extents);
@@ -720,17 +528,9 @@ int _finish(AQDG_DRAW_CONTEXT *g)
{ {
AQDG_DRAW_CONTEXT_CAIRO *xg; AQDG_DRAW_CONTEXT_CAIRO *xg;
assert(g); xg=_ensureFullCairoContext(g);
xg=GWEN_INHERIT_GETDATA(AQDG_DRAW_CONTEXT, AQDG_DRAW_CONTEXT_CAIRO, g); if (xg==NULL) {
assert(xg); DBG_INFO(NULL, "here");
if (xg->surface==NULL) {
DBG_ERROR(0, "No surface created");
return GWEN_ERROR_INVALID;
}
if (xg->cr==NULL) {
DBG_INFO(0, "Graphics context not active, call \"AQDG_Draw_Context_BeginDraw()\" before");
return GWEN_ERROR_INVALID; return GWEN_ERROR_INVALID;
} }
@@ -764,6 +564,115 @@ int _finish(AQDG_DRAW_CONTEXT *g)
AQDG_DRAW_CONTEXT_CAIRO *_ensureFullCairoContext(const AQDG_DRAW_CONTEXT *g)
{
if (g) {
AQDG_DRAW_CONTEXT_CAIRO *xg;
xg=_ensureCairoSurface(g);
if (xg) {
if (xg->cr==NULL) {
DBG_INFO(0, "Graphics context not active, call \"AQDG_Draw_Context_BeginDraw()\" first");
return NULL;
}
return xg;
}
}
return NULL;
}
AQDG_DRAW_CONTEXT_CAIRO *_ensureCairoSurface(const AQDG_DRAW_CONTEXT *g)
{
if (g) {
AQDG_DRAW_CONTEXT_CAIRO *xg;
xg=GWEN_INHERIT_GETDATA(AQDG_DRAW_CONTEXT, AQDG_DRAW_CONTEXT_CAIRO, g);
if (xg) {
if (xg->surface==NULL) {
DBG_ERROR(0, "No surface created");
return NULL;
}
}
return xg;
}
return NULL;
}
void _setColourAndWidthFromPen(cairo_t *cr, const AQDG_DRAW_PEN *pen)
{
uint32_t fgCol;
double red, green, blue;
/* set foreground color */;
fgCol=AQDG_Draw_Pen_GetFrontColour(pen);
red=(double)((fgCol>>24) & 0xff)/255.0;
green=(double)((fgCol>>16) & 0xff)/255.0;
blue=(double)((fgCol>>8) & 0xff)/255.0;
cairo_set_source_rgb(cr, red, green, blue);
/* set line width */
cairo_set_line_width(cr, AQDG_Draw_Pen_GetLineWidth(pen));
}
void _setDashType(cairo_t *cr, int dashType)
{
switch (dashType) {
default:
case 0:
cairo_set_dash(cr, NULL, 0, 0.0);
break;
case 1:
cairo_set_dash(cr, AQDG_Draw_ContextCairo_DashPattern1, 2, 0.0);
break;
case 2:
cairo_set_dash(cr, AQDG_Draw_ContextCairo_DashPattern2, 2, 0.0);
break;
case 3:
cairo_set_dash(cr, AQDG_Draw_ContextCairo_DashPattern3, 2, 0.0);
break;
case 4:
cairo_set_dash(cr, AQDG_Draw_ContextCairo_DashPattern4, 2, 0.0);
break;
case 5:
cairo_set_dash(cr, AQDG_Draw_ContextCairo_DashPattern5, 2, 0.0);
break;
}
}
void _setFont(cairo_t *cr, AQDG_DRAW_FONT *font)
{
int crSlant;
int crWeight;
switch (AQDG_Draw_Font_GetFontSlant(font)) {
default:
case AQDG_Slant_None:
crSlant=CAIRO_FONT_SLANT_NORMAL;
break;
case AQDG_Slant_Italic:
crSlant=CAIRO_FONT_SLANT_ITALIC;
break;
}
switch (AQDG_Draw_Font_GetFontWeight(font)) {
default:
case AQDG_Weight_None:
crWeight=CAIRO_FONT_WEIGHT_NORMAL;
break;
case AQDG_Weight_Bold:
crWeight=CAIRO_FONT_WEIGHT_BOLD;
break;
}
cairo_select_font_face(cr, AQDG_Draw_Font_GetFontName(font), crSlant, crWeight);
cairo_set_font_size(cr, AQDG_Draw_Font_GetFontSize(font));
}