Skip to content

Commit

Permalink
Drawing context refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
frang75 committed Dec 28, 2023
1 parent cb91082 commit 607d845
Show file tree
Hide file tree
Showing 26 changed files with 1,028 additions and 770 deletions.
6 changes: 3 additions & 3 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
* TableView force row height [Doc](https://nappgui.com/en/gui/tableview.html#h7). `tableview_header_height()` and `tableview_row_height()`.
* TableView column-by-column horizontal navigation [Doc](https://nappgui.com/en/gui/tableview.html#h4). `tableview_hkey_scroll()`.
* TableView focus row [Doc](https://nappgui.com/en/gui/tableview.html#h4). `tableview_focus_row()`, `tableview_get_focus_row()`.
* TableView hide scrollbars [Doc](https://nappgui.com/docs/gui/tableview.html#h7). `tableview_scroll_visible()`, `view_scroll_visible()` and `view_scroll_size()`.
* TableView multiline headers [Doc](https://nappgui.com/docs/gui/tableview.html#h5). `'\n'` in `tableview_header_title()`.
* TableView hide scrollbars [Doc](https://nappgui.com/en/gui/tableview.html#h7). `tableview_scroll_visible()`, `view_scroll_visible()` and `view_scroll_size()`.
* TableView multiline headers [Doc](https://nappgui.com/en/gui/tableview.html#h5). `'\n'` in `tableview_header_title()`.
* TextView get full text: `textview_get_text()`.
* TextView select text: `textview_select()`.
* TextView scroll to caret: `textview_scroll_caret()`.
Expand All @@ -31,7 +31,7 @@
* Window modal avoid hide when close: `ekWINDOW_MODAL_NOHIDE` [Doc](https://nappgui.com/en/gui/window.html#h3).
* View scrollbar event: `view_OnScroll()`, `gui_scroll_t`.
* View new constructor: `view_custom()`.
* View draw overlays: `view_OnOverlay()` [Doc](https://nappgui.com/docs/r4716/en/gui/view.html#h3).
* View draw overlays: `view_OnOverlay()` [Doc](https://nappgui.com/en/gui/view.html#h3).
* Panel new constructor: `panel_custom()`.
* Button vertical padding: `button_vpadding()` and `button_get_height()`.
* Edit vertical padding: `edit_vpadding()`.
Expand Down
2 changes: 1 addition & 1 deletion prj/build.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4745
4760
22 changes: 13 additions & 9 deletions src/draw2d/dctxh.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,45 @@ _draw2d_api void dctx_set_gcontext(DCtx *ctx, void *gcontext, const uint32_t wid

_draw2d_api void dctx_unset_gcontext(DCtx *ctx);

_draw2d_api void dctx_update_view(DCtx *ctx, void *view);

_draw2d_api void dctx_set_flipped(DCtx *ctx, const bool_t flipped);

_draw2d_api void dctx_size(const DCtx *ctx, uint32_t *width, uint32_t *height);

_draw2d_api void dctx_offset(const DCtx *ctx, real32_t *offset_x, real32_t *offset_y);

_draw2d_api void dctx_line_dash(const DCtx *ctx, real32_t *pattern, uint32_t *size);

_draw2d_api real32_t dctx_text_width(const DCtx *ctx);

_draw2d_api align_t dctx_text_intalign(const DCtx *ctx);

_draw2d_api color_t dctx_text_color(const DCtx *ctx);

_draw2d_api color_t dctx_line_color(const DCtx *ctx);

_draw2d_api color_t dctx_background_color(const DCtx *ctx);

_draw2d_api ellipsis_t dctx_text_trim(const DCtx *ctx);

_draw2d_api void dctx_set_raster_mode(DCtx *ctx);

_draw2d_api void *dctx_native(DCtx *ctx);

_draw2d_api void *dctx_internal_bitmap(DCtx *ctx);

_draw2d_api void dctx_text_raster(DCtx *ctx, const char_t *text, const real32_t x, const real32_t y);
_draw2d_api void dctx_data_imp(DCtx *ctx, void *data, FPtr_destroy func_destroy_data);

_draw2d_api void dctx_image_raster(DCtx *ctx, const Image *image, const real32_t x, const real32_t y);
_draw2d_api void *dctx_get_data_imp(const DCtx *ctx);

_draw2d_api void dctx_set_default_osfont(DCtx *ctx, const void *font);

_draw2d_api void dctx_data_imp(DCtx *ctx, void *data, FPtr_destroy func_destroy_data);
_draw2d_api void draw_set_raster_mode(DCtx *ctx);

_draw2d_api void *dctx_get_data_imp(const DCtx *ctx);
_draw2d_api void draw_text_raster(DCtx *ctx, const char_t *text, const real32_t x, const real32_t y);

_draw2d_api void draw_image_raster(DCtx *ctx, const Image *image, const real32_t x, const real32_t y);

_draw2d_api void draw_line_imp(DCtx *ctx, const real32_t x0, const real32_t y0, const real32_t x1, const real32_t y1, const bool_t raster);

_draw2d_api void draw_lineimp(DCtx *ctx, const real32_t x0, const real32_t y0, const real32_t x1, const real32_t y1, const bool_t raster);
_draw2d_api void draw_rect_imp(DCtx *ctx, const drawop_t op, const real32_t x, const real32_t y, const real32_t width, const real32_t height, const bool_t raster);

__END_C

Expand Down
18 changes: 3 additions & 15 deletions src/draw2d/draw2d.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <core/strings.h>
#include <osbs/log.h>
#include <sewer/blib.h>
#include <sewer/bmath.h>
#include <sewer/bmem.h>
#include <sewer/cassert.h>

Expand Down Expand Up @@ -175,19 +176,6 @@ color_t draw2d_get_indexed_color(const uint16_t index)

/*---------------------------------------------------------------------------*/

static __INLINE real32_t i_ceil(const real32_t n)
{
int32_t in = (int32_t)n;
if (n == (real32_t)in)
{
return (real32_t)in;
}

return (real32_t)(in + 1);
}

/*---------------------------------------------------------------------------*/

static const char_t *i_jump_blanks(const char_t *str)
{
cassert_no_null(str);
Expand Down Expand Up @@ -384,6 +372,6 @@ void draw2d_extents_imp(void *data, FPtr_word_extents func_word_extents, const b
ctext = next_text;
}

*width = i_ceil(*width);
*height = i_ceil(*height);
*width = bmath_ceilf(*width);
*height = bmath_ceilf(*height);
}
131 changes: 85 additions & 46 deletions src/draw2d/gtk3/dctx_gtk.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,6 @@ void dctx_unset_gcontext(DCtx *ctx)

/*---------------------------------------------------------------------------*/

void dctx_update_view(DCtx *ctx, void *view)
{
cassert_no_null(ctx);
unref(view);
}

/*---------------------------------------------------------------------------*/

void dctx_set_flipped(DCtx *ctx, const bool_t flipped)
{
unref(ctx);
Expand Down Expand Up @@ -133,6 +125,16 @@ void dctx_offset(const DCtx *ctx, real32_t *offset_x, real32_t *offset_y)

/*---------------------------------------------------------------------------*/

void dctx_line_dash(const DCtx *ctx, real32_t *pattern, uint32_t *size)
{
unref(ctx);
unref(pattern);
unref(size);
cassert(FALSE);
}

/*---------------------------------------------------------------------------*/

real32_t dctx_text_width(const DCtx *ctx)
{
unref(ctx);
Expand Down Expand Up @@ -160,6 +162,15 @@ color_t dctx_text_color(const DCtx *ctx)

/*---------------------------------------------------------------------------*/

color_t dctx_line_color(const DCtx *ctx)
{
unref(ctx);
cassert(FALSE);
return 0;
}

/*---------------------------------------------------------------------------*/

color_t dctx_background_color(const DCtx *ctx)
{
unref(ctx);
Expand All @@ -169,6 +180,34 @@ color_t dctx_background_color(const DCtx *ctx)

/*---------------------------------------------------------------------------*/

static __INLINE ellipsis_t i_nellipsis(const PangoEllipsizeMode ellipsis)
{
switch (ellipsis)
{
case PANGO_ELLIPSIZE_NONE:
return ekELLIPNONE;
case PANGO_ELLIPSIZE_START:
return ekELLIPBEGIN;
case PANGO_ELLIPSIZE_MIDDLE:
return ekELLIPMIDDLE;
case PANGO_ELLIPSIZE_END:
return ekELLIPEND;
cassert_default();
}

return ekELLIPNONE;
}

/*---------------------------------------------------------------------------*/

ellipsis_t dctx_text_trim(const DCtx *ctx)
{
cassert_no_null(ctx);
return i_nellipsis(ctx->ellipsis);
}

/*---------------------------------------------------------------------------*/

void *dctx_native(DCtx *ctx)
{
cassert_no_null(ctx);
Expand Down Expand Up @@ -205,44 +244,6 @@ void *dctx_get_data_imp(const DCtx *ctx)

/*---------------------------------------------------------------------------*/

void dctx_transform(DCtx *ctx, const T2Df *t2d, const bool_t cartesian)
{
cairo_matrix_t transform;
cassert_no_null(ctx);
cassert_no_null(t2d);
transform.xx = (double)t2d->i.x;
transform.yx = (double)t2d->i.y;
transform.xy = (double)t2d->j.x;
transform.yy = (double)t2d->j.y;
transform.x0 = (double)t2d->p.x;
transform.y0 = (double)t2d->p.y;
ctx->transform = transform;
ctx->cartesian_system = cartesian;

if (ctx->raster_mode == FALSE)
{
cairo_set_matrix(ctx->cairo, &ctx->origin);
cairo_transform(ctx->cairo, &transform);
_dctx_gradient_transform(ctx);
}
}

/*---------------------------------------------------------------------------*/

void _dctx_gradient_transform(DCtx *ctx)
{
cassert_no_null(ctx);
if (ctx->lpattern != NULL)
{
cairo_matrix_t matrix = ctx->pattern_matrix;
cairo_matrix_invert(&matrix);
cairo_matrix_multiply(&matrix, &ctx->transform, &matrix);
cairo_pattern_set_matrix(ctx->lpattern, &matrix);
}
}

/*---------------------------------------------------------------------------*/

DCtx *dctx_bitmap(const uint32_t width, const uint32_t height, const pixformat_t format)
{
DCtx *ctx = heap_new0(DCtx);
Expand Down Expand Up @@ -280,6 +281,30 @@ DCtx *dctx_bitmap(const uint32_t width, const uint32_t height, const pixformat_t

/*---------------------------------------------------------------------------*/

void dctx_transform(DCtx *ctx, const T2Df *t2d, const bool_t cartesian)
{
cairo_matrix_t transform;
cassert_no_null(ctx);
cassert_no_null(t2d);
transform.xx = (double)t2d->i.x;
transform.yx = (double)t2d->i.y;
transform.xy = (double)t2d->j.x;
transform.yy = (double)t2d->j.y;
transform.x0 = (double)t2d->p.x;
transform.y0 = (double)t2d->p.y;
ctx->transform = transform;
ctx->cartesian_system = cartesian;

if (ctx->raster_mode == FALSE)
{
cairo_set_matrix(ctx->cairo, &ctx->origin);
cairo_transform(ctx->cairo, &transform);
_dctx_gradient_transform(ctx);
}
}

/*---------------------------------------------------------------------------*/

static __INLINE void i_color(cairo_t *cairo, const color_t color, color_t *source_color)
{
/*if (color != *source_color)*/
Expand Down Expand Up @@ -315,3 +340,17 @@ void draw_antialias(DCtx *ctx, const bool_t on)

cairo_set_antialias(ctx->cairo, anti);
}

/*---------------------------------------------------------------------------*/

void _dctx_gradient_transform(DCtx *ctx)
{
cassert_no_null(ctx);
if (ctx->lpattern != NULL)
{
cairo_matrix_t matrix = ctx->pattern_matrix;
cairo_matrix_invert(&matrix);
cairo_matrix_multiply(&matrix, &ctx->transform, &matrix);
cairo_pattern_set_matrix(ctx->lpattern, &matrix);
}
}

0 comments on commit 607d845

Please sign in to comment.