mirror of
git://git.tartarus.org/simon/puzzles.git
synced 2025-04-20 23:51:29 -07:00
Refine drawing API semantics to pass drawing *
instead of void *
This changes the drawing API so that implementations receive a `drawing *` pointer with each call, instead of a `void *` pointer as they did previously. The `void *` context pointer has been moved to be a member of the `drawing` structure (which has been made public), from which it can be retrieved via the new `GET_HANDLE_AS_TYPE()` macro. To signal this breaking change to downstream front end authors, I've added a version number to the `drawing_api` struct, which will hopefully force them to notice. The motivation for this change is the upcoming introduction of a draw_polygon_fallback() function, which will use a series of calls to draw_line() to perform software polygon rasterization on platforms without a native polygon fill primitive. This function is fairly large, so I desired that it not be included in the binary distribution, except on platforms which require it (e.g. my Rockbox port). One way to achieve this is via link-time optimization (LTO, a.k.a. "interprocedural optimization"/IPO), so that the code is unconditionally compiled (preventing bit-rot) but only included in the linked executable if it is actually referenced from elsewhere. Practically, this precludes the otherwise straightforward route of including a run-time check of the `draw_polygon` pointer in the drawing.c middleware. Instead, Simon recommended that a front end be able to set its `draw_polygon` field to point to draw_polygon_fallback(). However, the old drawing API's semantics of passing a `void *` pointer prevented this from working in practice, since draw_polygon_fallback(), implemented in middleware, would not be able to perform any drawing operations without a `drawing *` pointer; with the new API, this restriction is removed, clearing the way for that function's introduction. This is a breaking change for front ends, which must update their implementations of the drawing API to conform. The migration process is fairly straightforward: every drawing API function which previously took a `void *` context pointer should be updated to take a `drawing *` pointer in its place. Then, where each such function would have previously casted the `void *` pointer to a meaningful type, they now instead retrieve the context pointer from the `handle` field of the `drawing` structure. To make this transition easier, the `GET_HANDLE_AS_TYPE()` macro is introduced to wrap the context pointer retrieval (see below for usage). As an example, an old drawing API function implementation would have looked like this: void frontend_draw_func(void *handle, ...) { frontend *fe = (frontend *)handle; /* do stuff with fe */ } After this change, that function would be rewritten as: void frontend_draw_func(drawing *dr, ...) { frontend *fe = GET_HANDLE_AS_TYPE(dr, frontend); /* do stuff with fe */ } I have already made these changes to all the in-tree front ends, but out-of-tree front ends will need to follow the procedure outlined above. Simon pointed out that changing the drawing API function pointer signatures to take `drawing *` instead of `void *` results only in a compiler warning, not an outright error. Thus, I've introduced a version field to the beginning of the `drawing_api` struct, which will cause a compilation error and hopefully force front ends to notice this. This field should be set to 1 for now. Going forward, it will provide a clear means of communicating future breaking API changes.
This commit is contained in:

committed by
Simon Tatham

parent
a993fd45eb
commit
f37913002a
65
ps.c
65
ps.c
@ -99,11 +99,11 @@ static void ps_stroke(psdata *ps, int colour)
|
||||
ps_setcolour_internal(ps, colour, " stroke");
|
||||
}
|
||||
|
||||
static void ps_draw_text(void *handle, int x, int y, int fonttype,
|
||||
static void ps_draw_text(drawing *dr, int x, int y, int fonttype,
|
||||
int fontsize, int align, int colour,
|
||||
const char *text)
|
||||
{
|
||||
psdata *ps = (psdata *)handle;
|
||||
psdata *ps = GET_HANDLE_AS_TYPE(dr, psdata);
|
||||
|
||||
y = ps->ytop - y;
|
||||
ps_setcolour(ps, colour);
|
||||
@ -133,9 +133,9 @@ static void ps_draw_text(void *handle, int x, int y, int fonttype,
|
||||
ps_printf(ps, "show\n");
|
||||
}
|
||||
|
||||
static void ps_draw_rect(void *handle, int x, int y, int w, int h, int colour)
|
||||
static void ps_draw_rect(drawing *dr, int x, int y, int w, int h, int colour)
|
||||
{
|
||||
psdata *ps = (psdata *)handle;
|
||||
psdata *ps = GET_HANDLE_AS_TYPE(dr, psdata);
|
||||
|
||||
y = ps->ytop - y;
|
||||
/*
|
||||
@ -146,10 +146,10 @@ static void ps_draw_rect(void *handle, int x, int y, int w, int h, int colour)
|
||||
ps_fill(ps, colour);
|
||||
}
|
||||
|
||||
static void ps_draw_line(void *handle, int x1, int y1, int x2, int y2,
|
||||
static void ps_draw_line(drawing *dr, int x1, int y1, int x2, int y2,
|
||||
int colour)
|
||||
{
|
||||
psdata *ps = (psdata *)handle;
|
||||
psdata *ps = GET_HANDLE_AS_TYPE(dr, psdata);
|
||||
|
||||
y1 = ps->ytop - y1;
|
||||
y2 = ps->ytop - y2;
|
||||
@ -157,10 +157,10 @@ static void ps_draw_line(void *handle, int x1, int y1, int x2, int y2,
|
||||
ps_stroke(ps, colour);
|
||||
}
|
||||
|
||||
static void ps_draw_polygon(void *handle, const int *coords, int npoints,
|
||||
static void ps_draw_polygon(drawing *dr, const int *coords, int npoints,
|
||||
int fillcolour, int outlinecolour)
|
||||
{
|
||||
psdata *ps = (psdata *)handle;
|
||||
psdata *ps = GET_HANDLE_AS_TYPE(dr, psdata);
|
||||
|
||||
int i;
|
||||
|
||||
@ -179,10 +179,10 @@ static void ps_draw_polygon(void *handle, const int *coords, int npoints,
|
||||
ps_stroke(ps, outlinecolour);
|
||||
}
|
||||
|
||||
static void ps_draw_circle(void *handle, int cx, int cy, int radius,
|
||||
static void ps_draw_circle(drawing *dr, int cx, int cy, int radius,
|
||||
int fillcolour, int outlinecolour)
|
||||
{
|
||||
psdata *ps = (psdata *)handle;
|
||||
psdata *ps = GET_HANDLE_AS_TYPE(dr, psdata);
|
||||
|
||||
cy = ps->ytop - cy;
|
||||
|
||||
@ -196,21 +196,21 @@ static void ps_draw_circle(void *handle, int cx, int cy, int radius,
|
||||
ps_stroke(ps, outlinecolour);
|
||||
}
|
||||
|
||||
static void ps_unclip(void *handle)
|
||||
static void ps_unclip(drawing *dr)
|
||||
{
|
||||
psdata *ps = (psdata *)handle;
|
||||
psdata *ps = GET_HANDLE_AS_TYPE(dr, psdata);
|
||||
|
||||
assert(ps->clipped);
|
||||
ps_printf(ps, "grestore\n");
|
||||
ps->clipped = false;
|
||||
}
|
||||
|
||||
static void ps_clip(void *handle, int x, int y, int w, int h)
|
||||
static void ps_clip(drawing *dr, int x, int y, int w, int h)
|
||||
{
|
||||
psdata *ps = (psdata *)handle;
|
||||
psdata *ps = GET_HANDLE_AS_TYPE(dr, psdata);
|
||||
|
||||
if (ps->clipped)
|
||||
ps_unclip(ps);
|
||||
ps_unclip(dr);
|
||||
|
||||
y = ps->ytop - y;
|
||||
/*
|
||||
@ -223,16 +223,16 @@ static void ps_clip(void *handle, int x, int y, int w, int h)
|
||||
ps->clipped = true;
|
||||
}
|
||||
|
||||
static void ps_line_width(void *handle, float width)
|
||||
static void ps_line_width(drawing *dr, float width)
|
||||
{
|
||||
psdata *ps = (psdata *)handle;
|
||||
psdata *ps = GET_HANDLE_AS_TYPE(dr, psdata);
|
||||
|
||||
ps_printf(ps, "%g setlinewidth\n", width);
|
||||
}
|
||||
|
||||
static void ps_line_dotted(void *handle, bool dotted)
|
||||
static void ps_line_dotted(drawing *dr, bool dotted)
|
||||
{
|
||||
psdata *ps = (psdata *)handle;
|
||||
psdata *ps = GET_HANDLE_AS_TYPE(dr, psdata);
|
||||
|
||||
if (dotted) {
|
||||
ps_printf(ps, "[ currentlinewidth 3 mul ] 0 setdash\n");
|
||||
@ -241,7 +241,7 @@ static void ps_line_dotted(void *handle, bool dotted)
|
||||
}
|
||||
}
|
||||
|
||||
static char *ps_text_fallback(void *handle, const char *const *strings,
|
||||
static char *ps_text_fallback(drawing *dr, const char *const *strings,
|
||||
int nstrings)
|
||||
{
|
||||
/*
|
||||
@ -284,9 +284,9 @@ static char *ps_text_fallback(void *handle, const char *const *strings,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void ps_begin_doc(void *handle, int pages)
|
||||
static void ps_begin_doc(drawing *dr, int pages)
|
||||
{
|
||||
psdata *ps = (psdata *)handle;
|
||||
psdata *ps = GET_HANDLE_AS_TYPE(dr, psdata);
|
||||
|
||||
fputs("%!PS-Adobe-3.0\n", ps->fp);
|
||||
fputs("%%Creator: Simon Tatham's Portable Puzzle Collection\n", ps->fp);
|
||||
@ -331,18 +331,18 @@ static void ps_begin_doc(void *handle, int pages)
|
||||
fputs("%%EndProlog\n", ps->fp);
|
||||
}
|
||||
|
||||
static void ps_begin_page(void *handle, int number)
|
||||
static void ps_begin_page(drawing *dr, int number)
|
||||
{
|
||||
psdata *ps = (psdata *)handle;
|
||||
psdata *ps = GET_HANDLE_AS_TYPE(dr, psdata);
|
||||
|
||||
fprintf(ps->fp, "%%%%Page: %d %d\ngsave save\n%g dup scale\n",
|
||||
number, number, 72.0 / 25.4);
|
||||
}
|
||||
|
||||
static void ps_begin_puzzle(void *handle, float xm, float xc,
|
||||
static void ps_begin_puzzle(drawing *dr, float xm, float xc,
|
||||
float ym, float yc, int pw, int ph, float wmm)
|
||||
{
|
||||
psdata *ps = (psdata *)handle;
|
||||
psdata *ps = GET_HANDLE_AS_TYPE(dr, psdata);
|
||||
|
||||
fprintf(ps->fp, "gsave\n"
|
||||
"clippath flattenpath pathbbox pop pop translate\n"
|
||||
@ -358,28 +358,29 @@ static void ps_begin_puzzle(void *handle, float xm, float xc,
|
||||
ps->hatchspace = 1.0 * pw / wmm;
|
||||
}
|
||||
|
||||
static void ps_end_puzzle(void *handle)
|
||||
static void ps_end_puzzle(drawing *dr)
|
||||
{
|
||||
psdata *ps = (psdata *)handle;
|
||||
psdata *ps = GET_HANDLE_AS_TYPE(dr, psdata);
|
||||
|
||||
fputs("grestore\n", ps->fp);
|
||||
}
|
||||
|
||||
static void ps_end_page(void *handle, int number)
|
||||
static void ps_end_page(drawing *dr, int number)
|
||||
{
|
||||
psdata *ps = (psdata *)handle;
|
||||
psdata *ps = GET_HANDLE_AS_TYPE(dr, psdata);
|
||||
|
||||
fputs("restore grestore showpage\n", ps->fp);
|
||||
}
|
||||
|
||||
static void ps_end_doc(void *handle)
|
||||
static void ps_end_doc(drawing *dr)
|
||||
{
|
||||
psdata *ps = (psdata *)handle;
|
||||
psdata *ps = GET_HANDLE_AS_TYPE(dr, psdata);
|
||||
|
||||
fputs("%%EOF\n", ps->fp);
|
||||
}
|
||||
|
||||
static const struct drawing_api ps_drawing = {
|
||||
1,
|
||||
ps_draw_text,
|
||||
ps_draw_rect,
|
||||
ps_draw_line,
|
||||
|
Reference in New Issue
Block a user