Files
Roman ChojnackiandClaude Opus 5.5 348e611aa0 Icons: keep the stroke color as a color
The image cache keys an icon's stroke as a struct nk_color and writes it
out with reaktor_hex(). The stroke width keeps its printed quantization:
rounding sw * 100 hands the rasterizer a different float at ties such as
0.125 or 1.625.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 17:00:53 +01:00

1157 lines
36 KiB
C

#include "internal.h"
#include "reaktor/launch.h"
#include "locale.h"
static const int g_font_px[FONT_STEPS] =
{ 12, 14, 16, 19, 23, 28, 34, 41 };
static int
parse_color(const char *spec, char *out, size_t cap)
{
if (spec[0] != '#' || strlen(spec) >= cap) return 0;
strcpy(out, spec);
return 1;
}
static struct tex_slot *
slot_at(void *slots, size_t size, int i)
{
return (struct tex_slot *)((char *)slots + (size_t)i * size);
}
static struct tex_slot *
slot_take(App *app, void *slots, size_t size, int *count, int max)
{
unsigned now = app->ctx->seq;
struct tex_slot *k = NULL, *s;
int i;
if (*count < max) return slot_at(slots, size, (*count)++);
for (i = 0; i < max; i++) {
s = slot_at(slots, size, i);
if (s->seen != now && (!k || now - s->seen > now - k->seen)) k = s;
}
if (k && k->tex) SDL_DestroyTexture(k->tex);
return k;
}
void
img_cache_clear(App *app)
{
int i;
for (i = 0; i < app->img_count; i++)
if (app->img[i].s.tex) SDL_DestroyTexture(app->img[i].s.tex);
app->img_count = 0;
}
void
img_cache_free(App *app)
{
img_cache_clear(app);
SDL_free(app->img);
app->img = NULL;
app->img_cap = 0;
if (app->blank) SDL_DestroyTexture(app->blank);
app->blank = NULL;
}
static struct img_slot *
img_grow(App *app)
{
int cap = app->img_cap ? app->img_cap * 2 : IMG_CACHE_MAX;
struct img_slot *more =
(struct img_slot *)SDL_realloc(app->img, (size_t)cap * sizeof *more);
if (!more) return NULL;
app->img = more;
app->img_cap = cap;
return &app->img[app->img_count++];
}
static struct nk_image
blank_image(App *app)
{
static const Uint32 clear = 0;
if (!app->blank) {
app->blank = SDL_CreateTexture(app->ren, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STATIC, 1, 1);
if (!app->blank) return nk_image_id(0);
reaktor_texture_serial(app->blank);
SDL_UpdateTexture(app->blank, NULL, &clear, (int)sizeof clear);
SDL_SetTextureBlendMode(app->blank, SDL_BLENDMODE_BLEND);
}
return nk_image_ptr(app->blank);
}
#define ION_DIR "external/ionicons/src/svg/"
struct img_key {
const char *src;
int ion;
struct nk_color stroke;
float sw;
};
static const char *
icon_source(App *app, const char *rel, char *out, size_t cap)
{
const char *base;
int i;
if (SDL_strncmp(rel, ION_DIR, sizeof(ION_DIR) - 1) != 0) return rel;
base = rel + sizeof(ION_DIR) - 1;
for (i = 0; i < app->icon_dir_count; i++) {
SDL_snprintf(out, cap, "%s/%s", app->icon_dirs[i], base);
if (reaktor_asset_exists(out)) return out;
}
SDL_snprintf(out, cap, "icons/%s", base);
return reaktor_asset_exists(out) ? out : rel;
}
static plutovg_surface_t *
raster_surface(const char *path)
{
size_t len = 0;
char *bytes = reaktor_asset_load(path, &len);
plutovg_surface_t *surf = bytes ? plutovg_surface_load_from_image_data(
bytes, (int)len)
: NULL;
reaktor_free(bytes);
return surf;
}
static void
src_query(char *rel, char *ocol, char *icol, size_t cap, float *sw)
{
char *opts = strchr(rel, '?');
const char *tok;
if (!opts) return;
*opts++ = 0;
for (tok = SDL_strtok_r(opts, "&", &opts); tok;
tok = SDL_strtok_r(NULL, "&", &opts)) {
if (SDL_strncmp(tok, "stroke=", 7) == 0)
parse_color(tok + 7, ocol, cap);
else if (SDL_strncmp(tok, "fill=", 5) == 0)
parse_color(tok + 5, icol, cap);
else if (SDL_strncmp(tok, "sw=", 3) == 0)
*sw = (float)SDL_atof(tok + 3);
}
}
static struct img_slot *
img_lookup(App *app, const struct img_key *k, int px)
{
char rel[192], found[1024], ocol[16] = {0}, icol[16] = {0};
float swk = 0.0f;
const char *q;
plutovg_surface_t *surf;
SDL_Surface *sdlsurf;
SDL_Texture *tex;
struct img_slot *slot;
unsigned hash;
int i, w, h, stride;
if (!k->src) return NULL;
if (px < 8) px = 8;
if (px > 512) px = 512;
hash = reaktor_fnv(REAKTOR_FNV ^ (unsigned)px, k->src);
for (i = 0; i < app->img_count; i++) {
slot = &app->img[i];
if (slot->hash == hash && slot->px == px && slot->ion == k->ion &&
!SDL_memcmp(&slot->stroke, &k->stroke, sizeof k->stroke) &&
slot->sw == k->sw && strcmp(slot->src, k->src) == 0) {
slot->s.seen = app->ctx->seq;
return slot->s.tex ? slot : NULL;
}
}
slot = (struct img_slot *)slot_take(app, app->img, sizeof app->img[0],
&app->img_count, app->img_cap);
if (!slot) slot = img_grow(app);
if (!slot) return NULL;
if (k->ion) {
SDL_snprintf(rel, sizeof(rel), ION_DIR "%s.svg", k->src);
reaktor_hex(k->stroke, ocol);
if (k->sw >= 0.0f) {
char num[32];
SDL_snprintf(num, sizeof(num), "%.2f", (double)k->sw);
swk = (float)SDL_atof(num);
}
} else {
SDL_strlcpy(rel, k->src, sizeof(rel));
src_query(rel, ocol, icol, sizeof(ocol), &swk);
}
q = SDL_strrchr(rel, '.');
surf = q && SDL_strcasecmp(q, ".svg") != 0
? raster_surface(rel)
: reaktor_svg_surface_path(icon_source(app, rel, found, sizeof(found)),
px, ocol[0] ? ocol : NULL,
icol[0] ? icol : NULL, swk);
SDL_strlcpy(slot->src, k->src, sizeof(slot->src));
slot->ion = k->ion;
slot->stroke = k->stroke;
slot->sw = k->sw;
slot->hash = hash;
slot->px = px;
slot->s.tex = NULL;
slot->w = slot->h = 0;
slot->ink_l = 0.0f;
slot->ink_r = 1.0f;
slot->s.seen = app->ctx->seq;
if (!surf) return NULL;
w = plutovg_surface_get_width(surf);
h = plutovg_surface_get_height(surf);
stride = plutovg_surface_get_stride(surf);
sdlsurf = SDL_CreateSurfaceFrom(w, h, SDL_PIXELFORMAT_ARGB8888,
plutovg_surface_get_data(surf), stride);
while (sdlsurf && (w >= 2 * px || h >= 2 * px) && w > 1 && h > 1) {
SDL_Surface *half = SDL_ScaleSurface(sdlsurf, w / 2, h / 2,
SDL_SCALEMODE_LINEAR);
SDL_DestroySurface(sdlsurf);
sdlsurf = half;
w /= 2;
h /= 2;
}
if (sdlsurf) {
int x, y, lo = w, hi = -1;
reaktor_unpremultiply((unsigned char *)sdlsurf->pixels, w, h,
sdlsurf->pitch);
for (y = 0; y < h; y++) {
const Uint32 *row = (const Uint32 *)((const Uint8 *)sdlsurf->pixels + y * sdlsurf->pitch);
for (x = 0; x < w; x++)
if (row[x] >> 24 > 32) {
if (x < lo) lo = x;
if (x > hi) hi = x;
}
}
if (hi >= lo) {
slot->ink_l = (float)lo / (float)w;
slot->ink_r = (float)(hi + 1) / (float)w;
}
}
tex = sdlsurf ? SDL_CreateTextureFromSurface(app->ren, sdlsurf) : NULL;
if (sdlsurf) SDL_DestroySurface(sdlsurf);
plutovg_surface_destroy(surf);
if (!tex) return NULL;
reaktor_texture_serial(tex);
SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND);
SDL_SetTextureScaleMode(tex, SDL_SCALEMODE_LINEAR);
slot->s.tex = tex;
slot->w = w;
slot->h = h;
return slot;
}
static struct nk_image
slot_image(App *app, const struct img_slot *slot)
{
if (!slot) return blank_image(app);
return nk_subimage_ptr(slot->s.tex, (nk_ushort)slot->w, (nk_ushort)slot->h,
nk_rect(0.0f, 0.0f, (float)slot->w, (float)slot->h));
}
static struct img_slot *
icon_slot(App *app, const struct img_key *k, int px, float over)
{
return img_lookup(app, k, (int)(px * reaktor_scale() * over + 0.5f));
}
static struct img_key
ion_key(const char *name, struct nk_color stroke, float sw)
{
struct img_key k;
k.src = name;
k.ion = 1;
k.stroke = nk_rgb(stroke.r, stroke.g, stroke.b);
k.sw = sw;
return k;
}
struct nk_image
ionicon_over(App *app, const char *name, struct nk_color stroke, float sw,
int px, float over)
{
struct img_key k = ion_key(name, stroke, sw);
return slot_image(app, icon_slot(app, &k, px, over));
}
struct nk_image
ionicon_ink(App *app, const char *name, struct nk_color stroke, float sw,
int px, float *left, float *right)
{
struct img_key k = ion_key(name, stroke, sw);
struct img_slot *slot = icon_slot(app, &k, px, 2.0f);
*left = slot ? slot->ink_l : 0.0f;
*right = slot ? slot->ink_r : 1.0f;
return slot_image(app, slot);
}
struct nk_image
reaktor_image_file(App *app, const char *src, int px)
{
struct img_key k = { src, 0, { 0, 0, 0, 0 }, 0.0f };
return slot_image(app, icon_slot(app, &k, px, 2.0f));
}
void
reaktor_image_centered(struct nk_context *ctx, struct nk_image im, int px)
{
struct nk_rect b = nk_widget_bounds(ctx);
float s = (float)px;
struct nk_rect r = nk_rect(b.x + (b.w - s) * 0.5f,
b.y + (b.h - s) * 0.5f, s, s);
nk_draw_image(nk_window_get_canvas(ctx), r, &im, nk_rgb(255, 255, 255));
nk_spacing(ctx, 1);
}
static struct nk_image
round_mask(App *app, int r, int t)
{
int d = 2 * r + 1, i;
struct round_slot *slot;
plutovg_surface_t *pv;
SDL_Texture *tex = NULL;
for (i = 0; i < app->round_count; i++)
if (app->round[i].r == r && app->round[i].t == t) {
app->round[i].s.seen = app->ctx->seq;
return app->round[i].s.tex
? nk_subimage_ptr(app->round[i].s.tex, (nk_ushort)d,
(nk_ushort)d,
nk_rect(0.0f, 0.0f, (float)d, (float)d))
: nk_image_id(0);
}
slot = (struct round_slot *)slot_take(app, app->round, sizeof app->round[0],
&app->round_count, ROUND_CACHE_MAX);
if (!slot) return nk_image_id(0);
pv = plutovg_surface_create(d, d);
if (pv) {
plutovg_canvas_t *cv = plutovg_canvas_create(pv);
SDL_Surface *surf;
if (cv) {
plutovg_canvas_set_rgba(cv, 1.0f, 1.0f, 1.0f, 1.0f);
plutovg_canvas_set_fill_rule(cv, PLUTOVG_FILL_RULE_EVEN_ODD);
plutovg_canvas_round_rect(cv, 0.0f, 0.0f, (float)d, (float)d,
(float)r, (float)r);
if (t > 0 && d > 2 * t) {
float in = r > t ? (float)(r - t) : 0.0f;
plutovg_canvas_round_rect(cv, (float)t, (float)t,
(float)(d - 2 * t), (float)(d - 2 * t),
in, in);
}
plutovg_canvas_fill(cv);
plutovg_canvas_destroy(cv);
}
reaktor_unpremultiply(plutovg_surface_get_data(pv), d, d,
plutovg_surface_get_stride(pv));
surf = SDL_CreateSurfaceFrom(d, d, SDL_PIXELFORMAT_ARGB8888,
plutovg_surface_get_data(pv),
plutovg_surface_get_stride(pv));
if (surf) {
tex = SDL_CreateTextureFromSurface(app->ren, surf);
SDL_DestroySurface(surf);
}
plutovg_surface_destroy(pv);
}
if (tex) {
reaktor_texture_serial(tex);
SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND);
SDL_SetTextureScaleMode(tex, SDL_SCALEMODE_NEAREST);
}
slot->r = r;
slot->t = t;
slot->s.tex = tex;
slot->s.seen = app->ctx->seq;
if (!tex) return nk_image_id(0);
return nk_subimage_ptr(tex, (nk_ushort)d, (nk_ushort)d,
nk_rect(0.0f, 0.0f, (float)d, (float)d));
}
static int
mask_px(float logical)
{
int n = reaktor_px(logical);
return n < 1 ? 1 : n;
}
static nk_size
splice_fill(App *app, nk_size at, struct nk_rect clip)
{
struct nk_context *ctx = app->ctx;
struct nk_command_rect_filled f =
*(struct nk_command_rect_filled *)((nk_byte *)ctx->memory.memory.ptr + at);
struct nk_command_buffer run;
nk_byte *base;
nk_size first;
SDL_zero(run);
run.base = &ctx->memory;
run.use_clipping = NK_CLIPPING_OFF;
run.clip = clip;
run.begin = run.end = run.last = ctx->memory.allocated;
nk_push_scissor(&run, clip);
if (run.end == run.begin) return at;
first = run.last;
reaktor_fill_round(app, &run, nk_rect(f.x, f.y, f.w, f.h), f.rounding, f.color);
base = (nk_byte *)ctx->memory.memory.ptr;
((struct nk_command *)(base + run.last))->next = f.header.next;
((struct nk_command *)(base + at))->type = NK_COMMAND_NOP;
((struct nk_command *)(base + at))->next = first;
return run.last;
}
void
reaktor_mask_rounded_fills(App *app)
{
struct nk_context *ctx = app->ctx;
const struct nk_command *c = nk__begin(ctx);
struct nk_rect clip = REAKTOR_NO_CLIP;
nk_size end = ctx->memory.allocated, at, next, tail = 0;
int spliced = 0;
if (!c) return;
at = (nk_size)((const nk_byte *)c - (const nk_byte *)ctx->memory.memory.ptr);
for (;;) {
struct nk_command *cmd =
(struct nk_command *)((nk_byte *)ctx->memory.memory.ptr + at);
next = cmd->next;
tail = at;
if (cmd->type == NK_COMMAND_SCISSOR) {
const struct nk_command_scissor *s =
(const struct nk_command_scissor *)cmd;
clip = nk_rect(s->x, s->y, s->w, s->h);
} else if (cmd->type == NK_COMMAND_RECT_FILLED &&
((struct nk_command_rect_filled *)cmd)->rounding &&
((struct nk_command_rect_filled *)cmd)->color.a) {
tail = splice_fill(app, at, clip);
spliced = 1;
}
if (next >= end) break;
at = next;
}
if (spliced)
((struct nk_command *)((nk_byte *)ctx->memory.memory.ptr + tail))->next =
ctx->memory.allocated;
}
/* A software frame that opens by filling the whole window has no use for the
clear. A GPU keeps it, as SDL asks; so does a feathered fill, whose edge
blends with what is under it. */
static int
first_fill_covers(App *app)
{
struct nk_context *ctx = app->ctx;
const struct nk_command *c;
float sx = 1.0f, sy = 1.0f;
int ow = 0, oh = 0, clipped = 0;
if (!SDL_GetCurrentRenderOutputSize(app->ren, &ow, &oh) ||
!SDL_GetRenderScale(app->ren, &sx, &sy))
return 0;
nk_foreach(c, ctx) {
if (c->type == NK_COMMAND_SCISSOR) {
const struct nk_command_scissor *s =
(const struct nk_command_scissor *)c;
clipped = s->x > 0 || s->y > 0 || (s->x + s->w) * sx < (float)ow ||
(s->y + s->h) * sy < (float)oh;
} else if (c->type != NK_COMMAND_NOP) {
const struct nk_command_rect_filled *f =
(const struct nk_command_rect_filled *)c;
return !clipped && c->type == NK_COMMAND_RECT_FILLED &&
!f->rounding && f->color.a == 255 && f->x <= 0 && f->y <= 0 &&
(f->x + f->w) * sx >= (float)ow &&
(f->y + f->h) * sy >= (float)oh;
}
}
return 0;
}
int
reaktor_aa_lines(const App *app)
{
return app->aa_mode != REAKTOR_AA_OFF;
}
int
reaktor_aa_fills(const App *app)
{
return app->aa_mode == REAKTOR_AA_ON ||
(app->aa_mode == REAKTOR_AA_AUTO && !app->renderer_is_sw);
}
static void
clear_rects(App *app, const SDL_Rect *rects, int n)
{
SDL_FRect f[REAKTOR_DAMAGE_RECTS];
SDL_BlendMode blend = SDL_BLENDMODE_BLEND;
int i;
n = SDL_min(n, REAKTOR_DAMAGE_RECTS);
for (i = 0; i < n; i++) {
f[i].x = (float)rects[i].x;
f[i].y = (float)rects[i].y;
f[i].w = (float)rects[i].w;
f[i].h = (float)rects[i].h;
}
SDL_GetRenderDrawBlendMode(app->ren, &blend);
SDL_SetRenderDrawBlendMode(app->ren, SDL_BLENDMODE_NONE);
SDL_SetRenderDrawColor(app->ren, app->clear.r, app->clear.g, app->clear.b,
app->clear.a);
SDL_RenderFillRects(app->ren, f, n);
SDL_SetRenderDrawBlendMode(app->ren, blend);
}
void
reaktor_render(App *app, const SDL_Rect *rects, int n)
{
if (n > 0) {
clear_rects(app, rects, n);
} else if (!app->renderer_is_sw || reaktor_aa_fills(app) || !first_fill_covers(app)) {
SDL_SetRenderDrawColor(app->ren, app->clear.r, app->clear.g, app->clear.b,
app->clear.a);
SDL_RenderClear(app->ren);
}
if (!app->secondary) reaktor_text_prepare(app->ctx, app->ren);
nk_sdl_render_area(app->ctx,
reaktor_aa_fills(app) ? NK_ANTI_ALIASING_ON : NK_ANTI_ALIASING_OFF,
reaktor_aa_lines(app) ? NK_ANTI_ALIASING_ON : NK_ANTI_ALIASING_OFF,
rects, n);
}
nk_size
reaktor_fade_mark(const struct nk_command_buffer *cv)
{
return cv->begin == cv->end ? (nk_size)-1 : cv->last;
}
static void
fade(struct nk_color *c, float opacity)
{
c->a = (nk_byte)((float)c->a * opacity + 0.5f);
}
void
reaktor_fade_since(struct nk_context *ctx, const struct nk_command_buffer *cv,
nk_size mark, float opacity)
{
nk_byte *base = (nk_byte *)ctx->memory.memory.ptr;
nk_size at;
if (cv->begin == cv->end || cv->last == mark) return;
at = mark == (nk_size)-1 ? cv->begin
: ((const struct nk_command *)(base + mark))->next;
for (;;) {
struct nk_command *c = (struct nk_command *)(base + at);
#define FADE(T, f) fade(&((struct T *)c)->f, opacity)
switch (c->type) {
case NK_COMMAND_LINE: FADE(nk_command_line, color); break;
case NK_COMMAND_CURVE: FADE(nk_command_curve, color); break;
case NK_COMMAND_RECT: FADE(nk_command_rect, color); break;
case NK_COMMAND_RECT_FILLED: FADE(nk_command_rect_filled, color); break;
case NK_COMMAND_RECT_MULTI_COLOR:
FADE(nk_command_rect_multi_color, left);
FADE(nk_command_rect_multi_color, top);
FADE(nk_command_rect_multi_color, right);
FADE(nk_command_rect_multi_color, bottom);
break;
case NK_COMMAND_CIRCLE: FADE(nk_command_circle, color); break;
case NK_COMMAND_CIRCLE_FILLED: FADE(nk_command_circle_filled, color); break;
case NK_COMMAND_ARC: FADE(nk_command_arc, color); break;
case NK_COMMAND_ARC_FILLED: FADE(nk_command_arc_filled, color); break;
case NK_COMMAND_TRIANGLE: FADE(nk_command_triangle, color); break;
case NK_COMMAND_TRIANGLE_FILLED: FADE(nk_command_triangle_filled, color); break;
case NK_COMMAND_POLYGON: FADE(nk_command_polygon, color); break;
case NK_COMMAND_POLYGON_FILLED: FADE(nk_command_polygon_filled, color); break;
case NK_COMMAND_POLYLINE: FADE(nk_command_polyline, color); break;
case NK_COMMAND_TEXT:
FADE(nk_command_text, background);
FADE(nk_command_text, foreground);
break;
case NK_COMMAND_IMAGE: FADE(nk_command_image, col); break;
default: break;
}
#undef FADE
if (at == cv->last) break;
at = c->next;
}
}
struct nk_rect
reaktor_rect_trunc(struct nk_rect b)
{
return nk_rect((float)(int)b.x, (float)(int)b.y, (float)(int)b.w, (float)(int)b.h);
}
static struct nk_rect
whole_px(struct nk_rect b)
{
float x0 = (float)(int)(b.x + 0.5f);
float y0 = (float)(int)(b.y + 0.5f);
float x1 = (float)(int)(b.x + b.w + 0.5f);
float y1 = (float)(int)(b.y + b.h + 0.5f);
return nk_rect(x0, y0, x1 - x0, y1 - y0);
}
static int
corner_px(struct nk_rect b, float rounding)
{
int r = (int)(rounding + 0.5f);
if (2 * r > (int)b.w) r = (int)b.w / 2;
if (2 * r > (int)b.h) r = (int)b.h / 2;
return r;
}
static void
draw_corners(struct nk_command_buffer *cv, struct nk_image mask, int m,
struct nk_rect b, int r, struct nk_color col)
{
float M = (float)m, D = (float)(2 * m + 1), R = (float)r;
int i;
for (i = 0; i < 4; i++) {
struct nk_rect at = nk_rect((i & 1) ? b.x + b.w - R : b.x,
(i & 2) ? b.y + b.h - R : b.y, R, R);
struct nk_image q = nk_subimage_handle(
mask.handle, (nk_ushort)D, (nk_ushort)D,
nk_rect((i & 1) ? M + 1.0f : 0.0f, (i & 2) ? M + 1.0f : 0.0f, M, M));
nk_draw_image(cv, at, &q, col);
}
}
typedef struct ramp {
struct nk_rect b;
struct nk_color from, to;
int across, flat;
} ramp;
static struct nk_color
ramp_at(const ramp *g, float at)
{
float span = g->across ? g->b.w : g->b.h;
float t = (at - (g->across ? g->b.x : g->b.y)) / span;
if (t < 0.0f) t = 0.0f;
if (t > 1.0f) t = 1.0f;
return nk_rgba((int)(g->from.r + (g->to.r - g->from.r) * t + 0.5f),
(int)(g->from.g + (g->to.g - g->from.g) * t + 0.5f),
(int)(g->from.b + (g->to.b - g->from.b) * t + 0.5f),
(int)(g->from.a + (g->to.a - g->from.a) * t + 0.5f));
}
static int
ramp_levels(const ramp *g)
{
int d[4] = { g->to.r - g->from.r, g->to.g - g->from.g, g->to.b - g->from.b,
g->to.a - g->from.a };
int i, most = 0;
for (i = 0; i < 4; i++)
if (SDL_abs(d[i]) > most) most = SDL_abs(d[i]);
return most;
}
/* A run in solid bands, one per level the ramp changes by across it: SDL fills
a solid rectangle far faster than it shades one from its corners. */
static void
ramp_rect(struct nk_command_buffer *cv, const ramp *g, struct nk_rect r)
{
float at = g->across ? r.x : r.y, len = g->across ? r.w : r.h;
int n, j;
if (g->flat) { nk_fill_rect(cv, r, 0.0f, g->from); return; }
n = (int)SDL_ceilf((float)ramp_levels(g) * len / (g->across ? g->b.w : g->b.h));
if (n > (int)len) n = (int)len;
if (n < 1) n = 1;
for (j = 0; j < n; j++) {
float s0 = at + (float)(int)(len * (float)j / (float)n + 0.5f);
float s1 = at + (float)(int)(len * (float)(j + 1) / (float)n + 0.5f);
struct nk_color c = ramp_at(g, (s0 + s1) * 0.5f);
if (s1 <= s0) continue;
nk_fill_rect(cv, g->across ? nk_rect(s0, r.y, s1 - s0, r.h)
: nk_rect(r.x, s0, r.w, s1 - s0), 0.0f, c);
}
}
static void
ramp_corners(struct nk_command_buffer *cv, const ramp *g, struct nk_image mask, int m,
int r)
{
float M = (float)m, D = (float)(2 * m + 1), R = (float)r;
int i, k;
if (g->flat) { draw_corners(cv, mask, m, g->b, r, g->from); return; }
for (i = 0; i < 4; i++) {
float x = (i & 1) ? g->b.x + g->b.w - R : g->b.x;
float y = (i & 2) ? g->b.y + g->b.h - R : g->b.y;
float u = (i & 1) ? M + 1.0f : 0.0f;
float v = (i & 2) ? M + 1.0f : 0.0f;
for (k = 0; k < m; k++) {
float s0 = R * (float)k / M, s1 = R * (float)(k + 1) / M;
struct nk_rect at = g->across ? nk_rect(x + s0, y, s1 - s0, R)
: nk_rect(x, y + s0, R, s1 - s0);
struct nk_rect src = g->across ? nk_rect(u + (float)k, v, 1.0f, M)
: nk_rect(u, v + (float)k, M, 1.0f);
struct nk_image q = nk_subimage_handle(mask.handle, (nk_ushort)D,
(nk_ushort)D, src);
nk_draw_image(cv, at, &q,
ramp_at(g, g->across ? at.x + at.w * 0.5f
: at.y + at.h * 0.5f));
}
}
}
static void
fill_shape(App *app, struct nk_command_buffer *cv, float rounding, const ramp *g)
{
struct nk_rect b = g->b;
struct nk_image disc;
float R, D;
int r, m;
r = corner_px(b, rounding);
if (r < 1) { ramp_rect(cv, g, b); return; }
m = mask_px((float)r);
disc = round_mask(app, m, 0);
if (!disc.handle.ptr) {
if (g->flat) nk_fill_rect(cv, b, (float)r, g->from);
else ramp_rect(cv, g, b);
return;
}
ramp_corners(cv, g, disc, m, r);
R = (float)r;
D = 2.0f * R;
if (!g->flat && g->across) {
if (b.w > D)
ramp_rect(cv, g, nk_rect(b.x + R, b.y, b.w - D, b.h));
if (b.h > D) {
ramp_rect(cv, g, nk_rect(b.x, b.y + R, R, b.h - D));
ramp_rect(cv, g, nk_rect(b.x + b.w - R, b.y + R, R, b.h - D));
}
return;
}
if (b.h > D)
ramp_rect(cv, g, nk_rect(b.x, b.y + R, b.w, b.h - D));
if (b.w > D) {
ramp_rect(cv, g, nk_rect(b.x + R, b.y, b.w - D, R));
ramp_rect(cv, g, nk_rect(b.x + R, b.y + b.h - R, b.w - D, R));
}
}
void
reaktor_fill_round(App *app, struct nk_command_buffer *cv, struct nk_rect b,
float rounding, struct nk_color col)
{
ramp g;
if (b.w <= 0.0f || b.h <= 0.0f || !col.a) return;
if (app) {
app->surface_at = b;
app->surface = col;
}
g.b = whole_px(b);
g.from = g.to = col;
g.across = 1;
g.flat = 1;
if (g.b.w <= 0.0f || g.b.h <= 0.0f) return;
fill_shape(app, cv, rounding, &g);
}
void
reaktor_fill_gradient(App *app, struct nk_command_buffer *cv, struct nk_rect b,
float rounding, struct nk_color from, struct nk_color to,
int axis)
{
ramp g;
if (axis == REAKTOR_GRADIENT_NONE || !memcmp(&from, &to, sizeof from)) {
reaktor_fill_round(app, cv, b, rounding, from);
return;
}
if (b.w <= 0.0f || b.h <= 0.0f || (!from.a && !to.a)) return;
g.b = whole_px(b);
g.from = from;
g.to = to;
g.across = axis == REAKTOR_GRADIENT_ACROSS;
g.flat = 0;
if (g.b.w <= 0.0f || g.b.h <= 0.0f) return;
if (app) {
app->surface_at = b;
app->surface = ramp_at(&g, g.across ? g.b.x + g.b.w * 0.5f
: g.b.y + g.b.h * 0.5f);
}
fill_shape(app, cv, rounding, &g);
}
void
reaktor_fill_style(App *app, struct nk_command_buffer *cv, struct nk_rect b,
float rounding, const reaktor_style *s)
{
if (s->gradient)
reaktor_fill_gradient(app, cv, b, rounding, nk_rgba_bv(s->gradient_from),
nk_rgba_bv(s->gradient_to), s->gradient);
else
reaktor_fill_round(app, cv, b, rounding, nk_rgba_bv(s->bg));
}
static void
center_glyphs_optically(struct reaktor_font *f)
{
const struct reaktor_glyph *ex = reaktor_font_glyph(f, 'x');
float shift;
int i;
if (!ex) return;
shift = f->height * 0.5f - (ex->y0 + ex->y1) * 0.5f;
if (shift > -0.01f && shift < 0.01f) return;
for (i = 0; i < f->glyph_count; i++) {
f->glyphs[i].y0 += shift;
f->glyphs[i].y1 += shift;
}
f->baseline += shift;
}
static void
nbsp_as_space(struct reaktor_font *f)
{
const struct reaktor_glyph *sp = reaktor_font_glyph(f, 0x20);
struct reaktor_glyph *nbsp = (struct reaktor_glyph *)reaktor_font_glyph(f, 0xA0);
if (!sp || !nbsp) return;
*nbsp = *sp;
nbsp->codepoint = 0xA0;
}
static void
finish_face(struct reaktor_font *f, float height, const char *path, int fresh)
{
if (!f) return;
f->handle.height = height;
if (fresh) {
center_glyphs_optically(f);
nbsp_as_space(f);
}
reaktor_text_attach_font(f, path);
}
const struct nk_user_font *
pick_font(App *app, int px, int bold)
{
int best = 0, i, bd = 1 << 30;
if (px <= 0) px = FONT_SIZE;
for (i = 0; i < FONT_STEPS; i++) {
int d = g_font_px[i] > px ? g_font_px[i] - px : px - g_font_px[i];
if (d < bd) { bd = d; best = i; }
}
if (bold && app->bolds[best]) return &app->bolds[best]->handle;
if (app->faces[best]) return &app->faces[best]->handle;
return app->ctx->style.font;
}
const struct nk_user_font *
base_font(App *app)
{
reaktor_style body;
reaktor_style_get("body", &body);
return pick_font(app, body.matched ? body.font_px : 0, 0);
}
static const char *
face_label(const char *name)
{
const char *base = reaktor_path_leaf(name);
return SDL_strncmp(base, "Aileron-", 8) == 0 ? "Aileron" : base;
}
static char *
take_face(const char *name, size_t *size)
{
char *b = reaktor_asset_load(name, size);
if (b && !reaktor_font_valid(b, *size)) {
reaktor_free(b);
b = NULL;
}
return b;
}
static char *
load_face(const char **name, const char *stand_in, size_t *size)
{
char *b = take_face(*name, size);
if (b || SDL_strcmp(*name, stand_in) == 0) return b;
SDL_Log("%s is not a font that can be read; %s stands in", *name, stand_in);
*name = stand_in;
return take_face(*name, size);
}
static struct {
struct reaktor_fontset set;
const char *face, *bold;
} g_kept;
void
atlas_forget(void)
{
reaktor_fontset_free(&g_kept.set);
SDL_zero(g_kept);
}
static int
keeps(const App *app)
{
return g_kept.set.pixels && app->font_face == g_kept.face && app->font_face_bold == g_kept.bold;
}
static int
bake(App *app, struct reaktor_fontset *set, int *bold)
{
static const nk_rune latin[] = { 0x0020, 0x00FF, 0 };
struct reaktor_face_src src[2 * FONT_STEPS];
size_t size = 0, bsize = 0;
char *ttf, *bttf = NULL;
unsigned *ranges;
Uint64 t0;
int n = 0, ok, i;
ttf = load_face(&app->font_face, FONT_FILE, &size);
if (ttf) bttf = load_face(&app->font_face_bold, FONT_BOLD_FILE, &bsize);
for (i = 0; ttf && i < FONT_STEPS; i++) {
src[n].ttf = ttf;
src[n].size = size;
src[n++].px = (float)reaktor_px(g_font_px[i]);
}
for (i = 0; bttf && i < FONT_STEPS; i++) {
src[n].ttf = bttf;
src[n].size = bsize;
src[n++].px = (float)reaktor_px(g_font_px[i]);
}
*bold = bttf != NULL;
ranges = reaktor_locale_glyphs();
t0 = SDL_GetPerformanceCounter();
ok = n > 0 && reaktor_fontset_bake(set, src, n, ranges ? ranges : latin);
app->atlas_ms_x100 = (int)(100000.0 * (double)(SDL_GetPerformanceCounter() - t0) /
(double)SDL_GetPerformanceFrequency());
SDL_free(ranges);
reaktor_free(ttf);
reaktor_free(bttf);
if (!ok) {
SDL_Log("could not bake %s; keeping the font already baked", app->font_face);
if (!app->fonts.count)
SDL_snprintf(app->font_status, sizeof(app->font_status),
"FALLBACK - could not load %s", app->font_face);
}
return ok;
}
void
rebuild_font(App *app)
{
struct reaktor_fontset set;
struct reaktor_font *font = NULL;
unsigned char *pixels;
nk_handle atlas;
int bold, shared, i;
shared = app->secondary && keeps(app) && reaktor_fontset_copy(&set, &g_kept.set);
if (shared) bold = set.count > FONT_STEPS;
else if (!bake(app, &set, &bold)) return;
if (!app->secondary) {
reaktor_text_attach_font(NULL, NULL);
atlas_forget();
}
reaktor_fontset_free(&app->fonts);
app->fonts = set;
pixels = shared ? g_kept.set.pixels : set.pixels;
atlas = nk_sdl_font_upload(app->ctx, pixels, set.w, set.h);
reaktor_texture_serial((SDL_Texture *)atlas.ptr);
reaktor_fontset_bind(&app->fonts, atlas);
app->fonts.pixels = NULL;
for (i = 0; i < FONT_STEPS; i++) {
app->faces[i] = &app->fonts.fonts[i];
app->bolds[i] = bold ? &app->fonts.fonts[FONT_STEPS + i] : NULL;
if (g_font_px[i] == FONT_SIZE) font = app->faces[i];
}
if (!font) font = app->faces[0];
SDL_snprintf(app->font_status, sizeof(app->font_status),
"%s, %d sizes %d-%dpx%s", face_label(app->font_face), FONT_STEPS,
reaktor_px(g_font_px[0]), reaktor_px(g_font_px[FONT_STEPS - 1]),
bold ? ", bold at each" : "");
for (i = 0; i < FONT_STEPS; i++) {
finish_face(app->faces[i], (float)g_font_px[i],
app->secondary ? NULL : app->font_face, !shared);
finish_face(app->bolds[i], (float)g_font_px[i],
app->secondary ? NULL : app->font_face_bold, !shared);
}
if (app->secondary && !shared && !g_kept.set.pixels &&
reaktor_fontset_copy(&g_kept.set, &app->fonts)) {
g_kept.set.pixels = pixels;
g_kept.face = app->font_face;
g_kept.bold = app->font_face_bold;
} else if (!shared) {
SDL_free(pixels);
}
app->atlas_w = app->atlas_h = app->atlas_bpp = 0;
if (font->handle.texture.ptr) {
SDL_Texture *tex = (SDL_Texture *)font->handle.texture.ptr;
float tw = 0.0f, th = 0.0f;
if (SDL_GetTextureSize(tex, &tw, &th)) {
app->atlas_w = (int)tw;
app->atlas_h = (int)th;
}
app->atlas_bpp = SDL_BYTESPERPIXEL(tex->format);
}
nk_style_set_font(app->ctx, base_font(app));
}
void
apply_render_scale(App *app)
{
float s = reaktor_scale();
app->render_scale = s;
if (!reaktor_damage_views(app)) SDL_SetRenderScale(app->ren, s, s);
}
SDL_Surface *
reaktor_icon_surface(const char *name, int px)
{
plutovg_surface_t *svg;
SDL_Surface *view, *copy = NULL;
int w, h, stride;
svg = reaktor_svg_surface_path(name, px, NULL, NULL, 0.0f);
if (!svg && SDL_strcmp(name, REAKTOR_MARK) != 0)
svg = reaktor_svg_surface_path(REAKTOR_MARK, px, NULL, NULL, 0.0f);
if (!svg) return NULL;
w = plutovg_surface_get_width(svg);
h = plutovg_surface_get_height(svg);
stride = plutovg_surface_get_stride(svg);
reaktor_unpremultiply(plutovg_surface_get_data(svg), w, h, stride);
view = SDL_CreateSurfaceFrom(w, h, SDL_PIXELFORMAT_ARGB8888,
plutovg_surface_get_data(svg), stride);
if (view) {
copy = SDL_DuplicateSurface(view);
SDL_DestroySurface(view);
}
plutovg_surface_destroy(svg);
return copy;
}
void
reaktor_set_window_icon(SDL_Window *win, const char *name)
{
SDL_Surface *ico = reaktor_icon_surface(name, 64);
if (!ico) return;
SDL_SetWindowIcon(win, ico);
SDL_DestroySurface(ico);
}
void
reaktor_edge_round(App *app, struct nk_command_buffer *cv, struct nk_rect b,
float rounding, float width, struct nk_color col)
{
struct nk_image ring = nk_image_id(0);
float R, T;
int r, t, m;
if (b.w <= 0.0f || b.h <= 0.0f || width <= 0.0f || !col.a) return;
b = whole_px(b);
t = (int)(width + 0.5f);
if (t < 1) t = 1;
if (2.0f * (float)t >= b.w || 2.0f * (float)t >= b.h) {
reaktor_fill_round(app, cv, b, rounding, col);
return;
}
r = corner_px(b, rounding);
m = mask_px((float)r);
if (r >= 1) ring = round_mask(app, m, mask_px((float)t));
if (!ring.handle.ptr) r = 0;
if (r) draw_corners(cv, ring, m, b, r, col);
R = (float)r;
T = (float)t;
if (r >= t) {
float M = (float)m, D = 2.0f * M + 1.0f;
struct nk_image top = nk_subimage_handle(ring.handle, (nk_ushort)D, (nk_ushort)D,
nk_rect(M, 0.0f, 1.0f, M));
struct nk_image low = nk_subimage_handle(ring.handle, (nk_ushort)D, (nk_ushort)D,
nk_rect(M, M + 1.0f, 1.0f, M));
struct nk_image lft = nk_subimage_handle(ring.handle, (nk_ushort)D, (nk_ushort)D,
nk_rect(0.0f, M, M, 1.0f));
struct nk_image rgt = nk_subimage_handle(ring.handle, (nk_ushort)D, (nk_ushort)D,
nk_rect(M + 1.0f, M, M, 1.0f));
if (b.w > 2.0f * R) {
nk_draw_image(cv, nk_rect(b.x + R, b.y, b.w - 2.0f * R, R), &top, col);
nk_draw_image(cv, nk_rect(b.x + R, b.y + b.h - R, b.w - 2.0f * R, R),
&low, col);
}
if (b.h > 2.0f * R) {
nk_draw_image(cv, nk_rect(b.x, b.y + R, R, b.h - 2.0f * R), &lft, col);
nk_draw_image(cv, nk_rect(b.x + b.w - R, b.y + R, R, b.h - 2.0f * R),
&rgt, col);
}
return;
}
if (r) {
nk_fill_rect(cv, nk_rect(b.x + R, b.y, b.w - 2.0f * R, R), 0.0f, col);
nk_fill_rect(cv, nk_rect(b.x + R, b.y + b.h - R, b.w - 2.0f * R, R),
0.0f, col);
}
nk_fill_rect(cv, nk_rect(b.x, b.y + R, b.w, T - R), 0.0f, col);
nk_fill_rect(cv, nk_rect(b.x, b.y + b.h - T, b.w, T - R), 0.0f, col);
nk_fill_rect(cv, nk_rect(b.x, b.y + T, T, b.h - 2.0f * T), 0.0f, col);
nk_fill_rect(cv, nk_rect(b.x + b.w - T, b.y + T, T, b.h - 2.0f * T), 0.0f, col);
}