zat.dev/zds
atproto pds · mirror of https://tangled.org/zat.dev/zds
zds 0.25.1 Latest 2 packages, all images
0
0
#define STB_TRUETYPE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#define STBI_WRITE_NO_STDIO
#include "stb_truetype.h"
#include "stb_image_write.h"
#define WIDTH 1200
#define HEIGHT 630
static void blend(unsigned char *pixels, int x, int y, const int color[3], float alpha) {
if (x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT) return;
unsigned char *p = pixels + (y * WIDTH + x) * 3;
for (int c = 0; c < 3; c++) p[c] = (unsigned char)(p[c] * (1 - alpha) + color[c] * alpha);
}
static void rounded_rect(unsigned char *pixels, int left, int top, int width, int height, int radius, const int color[3]) {
for (int y = top; y < top + height; y++) {
for (int x = left; x < left + width; x++) {
float dx = fmaxf(fmaxf(left + radius - x, x - (left + width - radius - 1)), 0);
float dy = fmaxf(fmaxf(top + radius - y, y - (top + height - radius - 1)), 0);
float alpha = fminf(fmaxf(radius - sqrtf(dx * dx + dy * dy), 0), 1);
blend(pixels, x, y, color, alpha);
}
}
}
static int text(unsigned char *pixels, stbtt_fontinfo *font, const unsigned char *value, size_t length,
int x, int baseline, float height, int max_width, const int color[3]) {
int advance, bearing;
stbtt_GetCodepointHMetrics(font, 'M', &advance, &bearing);
float scale = stbtt_ScaleForPixelHeight(font, height);
if (length && length * advance * scale > max_width) scale = max_width / (float)(length * advance);
float cursor = x;
for (size_t i = 0; i < length; i++) {
int w, h, dx, dy;
int codepoint = value[i] < 128 ? value[i] : '?';
unsigned char *glyph = stbtt_GetCodepointBitmap(font, scale, scale, codepoint, &w, &h, &dx, &dy);
if (!glyph && w && h) return 0;
for (int row = 0; row < h; row++)
for (int col = 0; col < w; col++)
blend(pixels, (int)cursor + dx + col, baseline + dy + row, color, glyph[row * w + col] / 255.0f);
stbtt_FreeBitmap(glyph, NULL);
cursor += advance * scale;
}
return 1;
}
/* Proportional text: measured with the font's own advances and kerning,
shrunk to fit max_width. Returns the drawn width, or -1 on failure. */
static float measure(stbtt_fontinfo *font, const unsigned char *value, size_t length, float scale) {
float width = 0;
for (size_t i = 0; i < length; i++) {
int advance, bearing;
int cp = value[i] < 128 ? value[i] : '?';
stbtt_GetCodepointHMetrics(font, cp, &advance, &bearing);
width += advance * scale;
if (i + 1 < length) width += stbtt_GetCodepointKernAdvance(font, cp, value[i + 1] < 128 ? value[i + 1] : '?') * scale;
}
return width;
}
static float prose(unsigned char *pixels, stbtt_fontinfo *font, const unsigned char *value, size_t length,
int x, int baseline, float height, int max_width, const int color[3]) {
float scale = stbtt_ScaleForPixelHeight(font, height);
float width = measure(font, value, length, scale);
if (width > max_width) { scale *= max_width / width; width = max_width; }
float cursor = x;
for (size_t i = 0; i < length; i++) {
int w, h, dx, dy, advance, bearing;
int cp = value[i] < 128 ? value[i] : '?';
unsigned char *glyph = stbtt_GetCodepointBitmapSubpixel(font, scale, scale, cursor - floorf(cursor), 0, cp, &w, &h, &dx, &dy);
if (!glyph && w && h) return -1;
for (int row = 0; row < h; row++)
for (int col = 0; col < w; col++)
blend(pixels, (int)floorf(cursor) + dx + col, baseline + dy + row, color, glyph[row * w + col] / 255.0f);
stbtt_FreeBitmap(glyph, NULL);
stbtt_GetCodepointHMetrics(font, cp, &advance, &bearing);
cursor += advance * scale;
if (i + 1 < length) cursor += stbtt_GetCodepointKernAdvance(font, cp, value[i + 1] < 128 ? value[i + 1] : '?') * scale;
}
return width;
}
/* A leaf: the lens where two discs overlap, antialiased on both edges. */
static void leaf(unsigned char *pixels, float ax, float ay, float bx, float by, float r, const int color[3]) {
int left = (int)fminf(ax, bx) - (int)r, right = (int)fmaxf(ax, bx) + (int)r;
int top = (int)fminf(ay, by) - (int)r, bottom = (int)fmaxf(ay, by) + (int)r;
for (int y = top; y <= bottom; y++)
for (int x = left; x <= right; x++) {
float da = r - hypotf(x - ax, y - ay), db = r - hypotf(x - bx, y - by);
float alpha = fminf(fmaxf(fminf(da, db), 0), 1);
if (alpha > 0) blend(pixels, x, y, color, alpha);
}
}
static unsigned int hash(unsigned int v) {
v ^= v >> 16; v *= 0x7feb352d; v ^= v >> 15; v *= 0x846ca68b; v ^= v >> 16;
return v;
}
/* The paper and garden card: a cream sheet with a thick edge resting on a
green ground, the sprout mark, the host in Nunito. */
static unsigned char *paper_png(const unsigned char *bold_data, const unsigned char *semi_data,
const unsigned char *host, size_t host_len, int *size) {
stbtt_fontinfo bold, semi;
if (!stbtt_InitFont(&bold, bold_data, 0) || !stbtt_InitFont(&semi, semi_data, 0)) return NULL;
unsigned char *pixels = malloc(WIDTH * HEIGHT * 3);
if (!pixels) return NULL;
const int ink[] = {35, 35, 40}, muted[] = {93, 93, 101}, edge[] = {210, 203, 187}, paper[] = {246, 244, 238};
const int sheet2[] = {235, 232, 224}, leaf_dark[] = {95, 154, 60}, leaf_light[] = {124, 184, 79}, stem[] = {63, 107, 42};
const int tuft[] = {124, 158, 92}, shade[] = {74, 101, 52};
for (int y = 0; y < HEIGHT; y++)
for (int x = 0; x < WIDTH; x++) {
float light = fmaxf(0, 1 - hypotf(x - 260, y - 40) / 900);
float dusk = fmaxf(0, hypotf(x - 600, y - 315) / 760 - .35f);
unsigned int n = hash((unsigned int)(y * WIDTH + x));
float grain = ((n & 255) / 255.0f - .5f) * 7;
unsigned char *p = pixels + (y * WIDTH + x) * 3;
p[0] = (unsigned char)fminf(255, fmaxf(0, 88 + light * 26 - dusk * 30 + grain));
p[1] = (unsigned char)fminf(255, fmaxf(0, 120 + light * 30 - dusk * 38 + grain));
p[2] = (unsigned char)fminf(255, fmaxf(0, 62 + light * 16 - dusk * 24 + grain));
}
for (int i = 0; i < 260; i++) {
unsigned int n = hash(9001 + i * 7919);
int x = n % WIDTH, y = (n >> 12) % HEIGHT;
if (x > 70 && x < 1130 && y > 80 && y < 560) continue;
int r = 2 + (n >> 24) % 3;
rounded_rect(pixels, x, y, r * 2, r * 2, r, (n & 1) ? tuft : shade);
}
/* soft cast shadow, then the edge, then the sheet */
for (int k = 22; k > 0; k -= 2) {
int c[3] = {40, 58, 28};
for (int y = 128 - k / 2 + 26; y < 128 + 412 + k + 26; y++)
for (int x = 96 - k; x < 96 + 1008 + k; x++) blend(pixels, x, y, c, .012f);
}
rounded_rect(pixels, 96, 140, 1008, 400, 46, edge);
rounded_rect(pixels, 96, 126, 1008, 400, 46, paper);
/* the mark: a small sheet with a sprout */
rounded_rect(pixels, 160, 212, 220, 214, 44, edge);
rounded_rect(pixels, 160, 196, 220, 214, 44, sheet2);
rounded_rect(pixels, 260, 262, 20, 112, 10, stem);
leaf(pixels, 270, 312, 201, 248, 65, leaf_dark);
leaf(pixels, 270, 295, 340, 231, 65, leaf_light);
if (prose(pixels, &bold, (const unsigned char *)"zds", 3, 160, 176, 30, 200, muted) < 0) goto fail;
/* host: one line, shrunk to fit; very long hosts break at dots */
size_t start = 0, ends[8], count = 0;
while (start < host_len && count < 8) {
size_t end = start + 22 < host_len ? start + 22 : host_len;
if (end < host_len)
for (size_t i = end; i > start + 12; i--)
if (host[i - 1] == '.') { end = i; break; }
ends[count++] = end;
start = end;
}
float line = count == 1 ? 92 : fminf(76, 230.0f / count);
int baseline = 318 - (int)((count - 1) * line / 2);
start = 0;
for (size_t i = 0; i < count; i++) {
if (prose(pixels, &bold, host + start, ends[i] - start, 430, baseline + (int)(i * line), line, 610, ink) < 0) goto fail;
start = ends[i];
}
const char *subtitle = "an atproto personal data server";
if (prose(pixels, &semi, (const unsigned char *)subtitle, strlen(subtitle), 432, baseline + (int)((count - 1) * line) + 62, 32, 610, muted) < 0) goto fail;
unsigned char *png = stbi_write_png_to_mem(pixels, WIDTH * 3, WIDTH, HEIGHT, 3, size);
free(pixels);
return png;
fail:
free(pixels);
return NULL;
}
unsigned char *zds_preview_paper_png(const unsigned char *bold, const unsigned char *semi, const unsigned char *host, size_t host_len, int *size) {
return paper_png(bold, semi, host, host_len, size);
}
unsigned char *zds_preview_png(const unsigned char *font_data, const unsigned char *host, size_t host_len, int *size) {
stbtt_fontinfo font;
if (!stbtt_InitFont(&font, font_data, 0)) return NULL;
unsigned char *pixels = malloc(WIDTH * HEIGHT * 3);
if (!pixels) return NULL;
const int white[] = {240, 241, 236}, muted[] = {170, 166, 155};
const int green[] = {144, 211, 166}, dark[] = {11, 11, 15}, border[] = {39, 48, 38};
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
float a = fmaxf(0, 1 - hypotf(x - 170, y - 80) / 650);
float b = fmaxf(0, 1 - hypotf(x - 1100, y - 590) / 600);
unsigned char *p = pixels + (y * WIDTH + x) * 3;
p[0] = (unsigned char)(7 + a * 5 + b * 15);
p[1] = (unsigned char)(8 + a * 30 + b * 20);
p[2] = (unsigned char)(7 + a * 14 + b * 38);
}
}
rounded_rect(pixels, 80, 190, 250, 250, 55, border);
rounded_rect(pixels, 83, 193, 244, 244, 52, dark);
for (int i = 0; i < 3; i++) {
rounded_rect(pixels, 146, 256 + i * 43, 118, 32, 10, green);
rounded_rect(pixels, 159, 265 + i * 43, 14, 14, 7, dark);
}
if (!text(pixels, &font, (const unsigned char *)"zds", 3, 80, 86, 28, 200, white)) goto fail;
size_t start = 0, ends[16], count = 0, longest = 0;
while (start < host_len && count < 16) {
size_t end = start + 28 < host_len ? start + 28 : host_len;
if (end < host_len) {
for (size_t i = end; i > start + 20; i--) {
if (host[i - 1] == '.') { end = i; break; }
}
}
ends[count++] = end;
if (end - start > longest) longest = end - start;
start = end;
}
float line_height = fminf(80, 280.0f / (count ? count : 1));
int advance, bearing;
stbtt_GetCodepointHMetrics(&font, 'M', &advance, &bearing);
float text_width = longest * advance * stbtt_ScaleForPixelHeight(&font, line_height);
float font_height = line_height * fminf(1, 730.0f / text_width);
int baseline = 320 - (int)((count - 1) * line_height / 2);
start = 0;
for (size_t i = 0; i < count; i++) {
if (!text(pixels, &font, host + start, ends[i] - start, 390, baseline + (int)(i * line_height), font_height, 730, white)) goto fail;
start = ends[i];
}
const char *subtitle = "an at protocol personal data server";
int subtitle_y = baseline + (int)((count - 1) * line_height) + 55;
if (!text(pixels, &font, (const unsigned char *)subtitle, strlen(subtitle), 390, subtitle_y, 25, 730, muted)) goto fail;
unsigned char *png = stbi_write_png_to_mem(pixels, WIDTH * 3, WIDTH, HEIGHT, 3, size);
free(pixels);
return png;
fail:
free(pixels);
return NULL;
}
void zds_preview_free(unsigned char *png) { free(png); }