ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
renderer.h File Reference

Terminal-to-pixel renderer and render-file output (audio + video). More...

Go to the source code of this file.

Data Structures

struct  term_renderer_config_t

Typedefs

typedef struct terminal_renderer_s terminal_renderer_t
typedef struct render_file_ctx_s render_file_ctx_t

Enumerations

enum  term_renderer_theme_t { TERM_RENDERER_THEME_DARK = 0 , TERM_RENDERER_THEME_LIGHT = 1 , TERM_RENDERER_THEME_AUTO = 2 }
 Terminal rendering theme. More...

Functions

asciichat_error_t term_renderer_create (const term_renderer_config_t *cfg, terminal_renderer_t **out)
asciichat_error_t term_renderer_set_zalgo (terminal_renderer_t *r, int level)
asciichat_error_t term_renderer_feed (terminal_renderer_t *r, const char *ansi_frame, size_t len)
const uint8_t * term_renderer_pixels (terminal_renderer_t *r)
int term_renderer_width_px (terminal_renderer_t *r)
int term_renderer_height_px (terminal_renderer_t *r)
int term_renderer_pitch (terminal_renderer_t *r)
int term_renderer_get_cols (terminal_renderer_t *r)
int term_renderer_get_rows (terminal_renderer_t *r)
void term_renderer_destroy (terminal_renderer_t *r)
asciichat_error_t render_file_create (const char *output_path, int cols, int rows, int fps, int theme, render_file_ctx_t **out)
asciichat_error_t render_file_write_frame (render_file_ctx_t *ctx, const char *ansi_frame, uint64_t captured_ns)
void render_file_set_live_timing (render_file_ctx_t *ctx)
void render_file_set_snapshot_actual_duration (render_file_ctx_t *ctx, double actual_duration_sec)
void render_file_begin_drain (render_file_ctx_t *ctx, uint64_t total, bool total_known)
asciichat_error_t render_file_destroy (render_file_ctx_t *ctx)

Detailed Description

Terminal-to-pixel renderer and render-file output (audio + video).

Supports theme-aware rendering that adapts colors based on the terminal's background theme (dark or light) for optimal readability.

Definition in file renderer.h.

Typedef Documentation

◆ render_file_ctx_t

Definition at line 69 of file renderer.h.

◆ terminal_renderer_t

Definition at line 22 of file renderer.h.

Enumeration Type Documentation

◆ term_renderer_theme_t

Terminal rendering theme.

Determines color palette for pixel-based ANSI text rendering. When TERM_RENDERER_THEME_AUTO is used, theme is detected from terminal_has_dark_background() to adapt colors to user's theme.

Enumerator
TERM_RENDERER_THEME_DARK 

Dark theme: use light colors for dark background

TERM_RENDERER_THEME_LIGHT 

Light theme: use dark colors for light background

TERM_RENDERER_THEME_AUTO 

Auto-detect theme from terminal (default)

Definition at line 31 of file renderer.h.

31 {
term_renderer_theme_t
Terminal rendering theme.
Definition renderer.h:31
@ TERM_RENDERER_THEME_LIGHT
Definition renderer.h:33
@ TERM_RENDERER_THEME_DARK
Definition renderer.h:32
@ TERM_RENDERER_THEME_AUTO
Definition renderer.h:34

Function Documentation

◆ render_file_begin_drain()

void render_file_begin_drain ( render_file_ctx_t * ctx,
uint64_t total,
bool total_known )

Definition at line 345 of file renderer.c.

345 {
346 if (ctx)
347 render_progress_begin(ctx->progress, total, total_known);
348}
void render_progress_begin(render_progress_t *progress, uint64_t total, bool total_known)
render_progress_t * progress
Definition renderer.c:33

References render_file_ctx_s::progress, and render_progress_begin().

Referenced by session_display_begin_drain().

◆ render_file_create()

asciichat_error_t render_file_create ( const char * output_path,
int cols,
int rows,
int fps,
int theme,
render_file_ctx_t ** out )

Definition at line 53 of file renderer.c.

53 {
54 if (!output_path || !out || cols <= 0 || rows <= 0)
55 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing render output, grid, or context");
56 *out = NULL;
57 log_info("output_path=%s, cols=%d, rows=%d, fps=%d, theme=%d, out=%p", output_path ? output_path : "(null)", cols, rows, fps, theme, (void *)out);
58 render_file_ctx_t *ctx = SAFE_CALLOC(1, sizeof(*ctx), render_file_ctx_t *);
59 if (strcmp(output_path, "/dev/null") == 0) {
60 ctx->discard = true;
61 ctx->error = ASCIICHAT_OK;
62 *out = ctx;
63 return ASCIICHAT_OK;
64 }
65 ctx->kitty_only = strcmp(output_path, "--kitty") == 0;
66 ctx->kitty = ctx->kitty_only || GET_OPTION(render_kitty);
67 if (ctx->kitty && !GET_OPTION(snapshot_mode)) {
68 SAFE_FREE(ctx);
69 return SET_ERRNO(ERROR_INVALID_PARAM, "Kitty graphics output requires --snapshot");
70 }
71 if (ctx->kitty && (strcmp(output_path, "-") == 0 || strcmp(output_path, "pipe:") == 0)) {
72 SAFE_FREE(ctx);
73 return SET_ERRNO(ERROR_INVALID_PARAM, "Kitty graphics and binary media cannot share stdout");
74 }
75 const char *term = SAFE_GETENV("TERM");
76 if (ctx->kitty && terminal_is_stdout_tty() && (!term || strcmp(term, "dumb") == 0 || strcmp(term, "linux") == 0 || strcmp(term, "vt100") == 0)) {
77 SAFE_FREE(ctx);
78 return SET_ERRNO(ERROR_NOT_SUPPORTED, "Kitty graphics require a capable terminal; redirect stdout to export the protocol");
79 }
80
81 // Resolve font spec → platform-appropriate file path, family name, or bundled data.
82 char font_spec[PLATFORM_MAX_PATH_LENGTH] = {0};
83 bool font_is_path = false;
84 const uint8_t *font_data = NULL;
85 size_t font_data_size = 0;
86 const char *raw_font = GET_OPTION(render_font);
87
88 // Log the font option value from GET_OPTION (for WASM font debugging)
89 log_info("[WASM_FONT_GETTER] GET_OPTION(render_font) returned: '%s' (empty=%d), GET_OPTION(matrix_rain)=%d", raw_font ? raw_font : "(null)", (!raw_font || raw_font[0] == '\0') ? 1 : 0,
90 GET_OPTION(matrix_rain));
91
92 // Auto-select matrix font when --matrix flag is set, unless user explicitly overrides with --render-font
93 log_debug("raw_font=%s, matrix_rain=%d", raw_font, GET_OPTION(matrix_rain));
94 if ((!raw_font || raw_font[0] == '\0') && GET_OPTION(matrix_rain)) {
95 raw_font = "matrix";
96 log_debug("[MATRIX] Using matrix font (enabled by --matrix flag)");
97 }
98
99 platform_font_register("matrix", g_font_matrix_resurrected, g_font_matrix_resurrected_size);
100 platform_font_register("default", g_font_default, g_font_default_size);
101 platform_font_register("DejaVu Sans Mono", g_font_default, g_font_default_size);
102 asciichat_error_t fe = platform_font_resolve(raw_font, font_spec, sizeof(font_spec), &font_is_path, &font_data, &font_data_size);
103 if (fe != ASCIICHAT_OK) {
104 if (raw_font && raw_font[0]) {
105 SAFE_FREE(ctx);
106 return fe;
107 }
108 log_warn("font resolution failed for '%s' — using bundled default", raw_font ? raw_font : "(explicit system)");
109 fe = platform_font_resolve("default", font_spec, sizeof(font_spec), &font_is_path, &font_data, &font_data_size);
110 if (fe != ASCIICHAT_OK) {
111 SAFE_FREE(ctx);
112 return fe;
113 }
114 }
115
116 log_debug("Font resolved: font_spec='%s', font_is_path=%d, font_data=%p (size=%zu)", font_spec, font_is_path, (void *)font_data, font_data_size);
117
118 // A zero size selects a compact default for video; explicit font sizes keep
119 // their exact dimensions. Images and Kitty snapshots retain the 12 pt default.
120 double font_size_pt = GET_OPTION(render_font_size);
121 const char *extension = strrchr(output_path, '.');
122 bool is_image = extension && (!strcmp(extension, ".png") || !strcmp(extension, ".jpg") || !strcmp(extension, ".jpeg") || !strcmp(extension, ".bmp") || !strcmp(extension, ".webp"));
123 bool automatic_video_size = font_size_pt == 0.0 && !is_image && !ctx->kitty;
124 if (font_size_pt == 0.0) {
125 font_size_pt = RENDER_AUTO_FONT_SIZE_PT;
126 if (automatic_video_size) {
127 double width_scale = (double)RENDER_AUTO_VIDEO_MAX_DIMENSION / ((double)cols * 10.0);
128 double height_scale = (double)RENDER_AUTO_VIDEO_MAX_DIMENSION / ((double)rows * 20.0);
129 double scale = width_scale < height_scale ? width_scale : height_scale;
130 if (scale < 1.0)
131 font_size_pt *= scale;
132 if (font_size_pt < RENDER_AUTO_MIN_FONT_SIZE_PT)
133 font_size_pt = RENDER_AUTO_MIN_FONT_SIZE_PT;
134 }
135 }
136 term_renderer_theme_t resolved_theme = (term_renderer_theme_t)theme;
137 if (resolved_theme == TERM_RENDERER_THEME_AUTO) {
139 log_debug("auto theme resolved to %s", resolved_theme == TERM_RENDERER_THEME_DARK ? "dark" : "light");
140 }
141
142 term_renderer_config_t tr_cfg = {
143 .cols = cols,
144 .rows = rows,
145 .font_size_pt = font_size_pt,
146 .theme = resolved_theme,
147 .font_is_path = font_is_path,
148 .font_data = font_data,
149 .font_data_size = font_data_size,
150 };
151 SAFE_STRNCPY(tr_cfg.font_spec, font_spec, sizeof(tr_cfg.font_spec));
152
153 asciichat_error_t err = term_renderer_create(&tr_cfg, &ctx->renderer);
154 if (err != ASCIICHAT_OK) {
155 log_error("term_renderer_create failed: %s", asciichat_error_format(NULL, err));
156 SAFE_FREE(ctx);
157 return err;
158 }
159 int actual_width_px = term_renderer_width_px(ctx->renderer);
160 int actual_height_px = term_renderer_height_px(ctx->renderer);
161 // Hinting and font metrics vary. Recalculate once if the initial estimate
162 // exceeds the target, retaining the original renderer if a bitmap font
163 // cannot be scaled down.
164 if (automatic_video_size && (actual_width_px > RENDER_AUTO_VIDEO_MAX_DIMENSION || actual_height_px > RENDER_AUTO_VIDEO_MAX_DIMENSION) && font_size_pt > RENDER_AUTO_MIN_FONT_SIZE_PT) {
165 double width_scale = (double)RENDER_AUTO_VIDEO_MAX_DIMENSION / actual_width_px;
166 double height_scale = (double)RENDER_AUTO_VIDEO_MAX_DIMENSION / actual_height_px;
167 double scale = width_scale < height_scale ? width_scale : height_scale;
168 double smaller = font_size_pt * scale * 0.98;
169 if (smaller < RENDER_AUTO_MIN_FONT_SIZE_PT)
171 tr_cfg.font_size_pt = smaller;
172 terminal_renderer_t *candidate = NULL;
173 err = term_renderer_create(&tr_cfg, &candidate);
174 if (err == ASCIICHAT_OK) {
175 int candidate_width = term_renderer_width_px(candidate);
176 int candidate_height = term_renderer_height_px(candidate);
177 if (candidate_width <= actual_width_px && candidate_height <= actual_height_px && (candidate_width < actual_width_px || candidate_height < actual_height_px)) {
179 ctx->renderer = candidate;
180 actual_width_px = candidate_width;
181 actual_height_px = candidate_height;
182 } else {
183 term_renderer_destroy(candidate);
184 }
185 } else {
187 SAFE_FREE(ctx);
188 return err;
189 }
190 }
191 log_info("GRID DIMENSIONS: %ux%u cells -> PIXEL DIMENSIONS: %dx%d px", cols, rows, actual_width_px, actual_height_px);
192 log_debug("term_renderer created (%dx%d cells, %dx%d px)", cols, rows, actual_width_px, actual_height_px);
193
194 if (ctx->kitty_only) {
195 ctx->fps = fps;
196 ctx->progress = render_progress_create(true, "terminal");
197 NAMED_REGISTER_CONTEXT(ctx, "render_file_ctx", output_path, NULL);
199 *out = ctx;
200 return ASCIICHAT_OK;
201 }
202 int output_width_px = actual_width_px;
203 int output_height_px = actual_height_px;
204 if (automatic_video_size && (output_width_px > RENDER_AUTO_VIDEO_MAX_DIMENSION || output_height_px > RENDER_AUTO_VIDEO_MAX_DIMENSION)) {
205 double scale_x = (double)RENDER_AUTO_VIDEO_MAX_DIMENSION / output_width_px;
206 double scale_y = (double)RENDER_AUTO_VIDEO_MAX_DIMENSION / output_height_px;
207 double scale = scale_x < scale_y ? scale_x : scale_y;
208 output_width_px = ((int)(output_width_px * scale)) & ~1;
209 output_height_px = ((int)(output_height_px * scale)) & ~1;
210 if (output_width_px < 2)
211 output_width_px = 2;
212 if (output_height_px < 2)
213 output_height_px = 2;
214 }
215 err = ffmpeg_encoder_create_scaled(output_path, actual_width_px, actual_height_px, output_width_px, output_height_px, fps, &ctx->encoder);
216 if (err != ASCIICHAT_OK) {
217 log_error("ffmpeg_encoder_create failed: %s", asciichat_error_format(NULL, err));
219 SAFE_FREE(ctx);
220 return err;
221 }
222 log_debug("ffmpeg_encoder created successfully");
223
224 // Initialize audio fields
225 ctx->audio_sample_rate = 48000; // Audio pipeline is 48kHz
226 ctx->fps = fps;
227 // Allocate large buffer for snapshot mode (5 seconds at 48kHz with ~14 FPS = 3,428 samples per frame)
228 // Use 8192 to comfortably handle snapshot reads without excessive overhead
229 ctx->audio_buf_size = 8192; // Temporary buffer for reading audio (in floats)
230 ctx->audio_read_buf = SAFE_MALLOC(ctx->audio_buf_size * sizeof(float), float *);
231
233 if (err != ASCIICHAT_OK) {
237 SAFE_FREE(ctx);
238 return err;
239 }
240 log_info("initialized encoder for %s", output_path);
241
242 // Register render file context for debugging
243 NAMED_REGISTER_CONTEXT(ctx, "render_file_ctx", output_path, NULL);
244
245 bool binary_stdout = strcmp(output_path, "-") == 0 || strcmp(output_path, "pipe:") == 0;
246 ctx->progress = render_progress_create(!binary_stdout, output_path);
248 *out = ctx;
249 return ASCIICHAT_OK;
250}
const char * asciichat_error_format(const char *message, asciichat_error_t code)
#define SET_ERRNO(code,...)
terminal_background_t colorscheme_current_background(void)
@ TERM_BACKGROUND_DARK
Definition colorscheme.h:71
asciichat_error_t ffmpeg_encoder_create_scaled(const char *output_path, int source_width, int source_height, int width_px, int height_px, int fps, ffmpeg_encoder_t **out)
asciichat_error_t ffmpeg_encoder_destroy(ffmpeg_encoder_t *enc)
#define SAFE_STRNCPY(dst, src, size)
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
#define SAFE_GETENV(name)
#define SAFE_CALLOC(count, size, cast)
unsigned char uint8_t
#define NAMED_REGISTER_CONTEXT(context, context_type, name, parent_ptr)
Register a generic context with automatic format specifier.
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
@ ERROR_NOT_SUPPORTED
Definition error_codes.h:74
#define log_warn(...)
Log a WARN message.
Definition log/log.h:591
#define log_error(...)
Log an ERROR message.
Definition log/log.h:604
#define log_info(...)
Log an INFO message.
Definition log/log.h:578
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:565
#define GET_OPTION(field)
Definition options.h:1339
const size_t g_font_matrix_resurrected_size
const unsigned char g_font_matrix_resurrected[]
const size_t g_font_default_size
const unsigned char g_font_default[]
int term_renderer_width_px(terminal_renderer_t *r)
asciichat_error_t term_renderer_create(const term_renderer_config_t *cfg, terminal_renderer_t **out)
void term_renderer_destroy(terminal_renderer_t *r)
int term_renderer_height_px(terminal_renderer_t *r)
static bool size_t gotel_query_t * out
Definition queries.h:10
asciichat_error_t audio_recording_create_channels(audio_recording_t **out, unsigned channels)
Definition recording.c:35
render_progress_t * render_progress_create(bool enabled, const char *destination)
#define RENDER_AUTO_VIDEO_MAX_DIMENSION
Definition renderer.c:24
#define RENDER_AUTO_FONT_SIZE_PT
Definition renderer.c:23
#define RENDER_AUTO_MIN_FONT_SIZE_PT
Definition renderer.c:25
struct terminal_renderer_s terminal_renderer_t
Definition renderer.h:22
struct render_file_ctx_s render_file_ctx_t
Definition renderer.h:69
bool terminal_is_stdout_tty(void)
asciichat_error_t error
Definition renderer.c:34
terminal_renderer_t * renderer
Definition renderer.c:35
ffmpeg_encoder_t * encoder
Definition renderer.c:36
uint32_t audio_sample_rate
Definition renderer.c:47
audio_recording_t * recording
Definition renderer.c:37
float * audio_read_buf
Definition renderer.c:49
char font_spec[PLATFORM_MAX_PATH_LENGTH]
Definition renderer.h:42
void ui_controller_title_render(ui_title_render_t phase)
@ UI_TITLE_RENDERING

References asciichat_error_format(), ASCIICHAT_OK, render_file_ctx_s::audio_buf_size, render_file_ctx_s::audio_read_buf, audio_recording_create_channels(), render_file_ctx_s::audio_sample_rate, colorscheme_current_background(), render_file_ctx_s::discard, render_file_ctx_s::encoder, render_file_ctx_s::error, ERROR_INVALID_PARAM, ERROR_NOT_SUPPORTED, ffmpeg_encoder_create_scaled(), ffmpeg_encoder_destroy(), term_renderer_config_t::font_size_pt, term_renderer_config_t::font_spec, render_file_ctx_s::fps, g_font_default, g_font_default_size, g_font_matrix_resurrected, g_font_matrix_resurrected_size, GET_OPTION, render_file_ctx_s::kitty, render_file_ctx_s::kitty_only, log_debug, log_error, log_info, log_warn, NAMED_REGISTER_CONTEXT, out, render_file_ctx_s::progress, render_file_ctx_s::recording, RENDER_AUTO_FONT_SIZE_PT, RENDER_AUTO_MIN_FONT_SIZE_PT, RENDER_AUTO_VIDEO_MAX_DIMENSION, render_progress_create(), render_file_ctx_s::renderer, SAFE_CALLOC, SAFE_FREE, SAFE_GETENV, SAFE_MALLOC, SAFE_STRNCPY, SET_ERRNO, TERM_BACKGROUND_DARK, term_renderer_create(), term_renderer_destroy(), term_renderer_height_px(), TERM_RENDERER_THEME_AUTO, TERM_RENDERER_THEME_DARK, TERM_RENDERER_THEME_LIGHT, term_renderer_width_px(), terminal_is_stdout_tty(), ui_controller_title_render(), and UI_TITLE_RENDERING.

Referenced by session_display_init_render_file().

◆ render_file_destroy()

asciichat_error_t render_file_destroy ( render_file_ctx_t * ctx)

Definition at line 356 of file renderer.c.

356 {
357 if (!ctx)
358 return ASCIICHAT_OK;
359 if (ctx->discard) {
360 SAFE_FREE(ctx);
361 return ASCIICHAT_OK;
362 }
363 NAMED_UNREGISTER(ctx);
366 uint64_t target = ctx->audio_target;
368 while (ctx->audio_samples_written < target) {
369 int count = (int)((target - ctx->audio_samples_written) > (uint64_t)ctx->audio_buf_size ? (uint64_t)ctx->audio_buf_size : target - ctx->audio_samples_written);
371 err = ffmpeg_encoder_write_audio(ctx->encoder, ctx->audio_read_buf, count);
372 if (err != ASCIICHAT_OK)
373 break;
374 ctx->audio_samples_written += count;
375 }
378 if (err == ASCIICHAT_OK)
379 err = close_err;
380 if (ctx->error != ASCIICHAT_OK)
381 err = ctx->error;
383 if (err == ASCIICHAT_OK && ctx->kitty && ctx->frames_written == 0)
384 err = SET_ERRNO(ERROR_MEDIA_INIT, "No snapshot was rendered for Kitty output");
385 if (err == ASCIICHAT_OK && ctx->kitty && ctx->frames_written > 0)
390 SAFE_FREE(ctx);
391 return err;
392}
asciichat_error_t ffmpeg_encoder_write_audio(ffmpeg_encoder_t *enc, const float *samples, int num_samples)
unsigned long long uint64_t
#define NAMED_UNREGISTER(ptr)
Unregister a pointer.
@ ERROR_MEDIA_INIT
Definition error_codes.h:70
const uint8_t * term_renderer_pixels(terminal_renderer_t *r)
int term_renderer_pitch(terminal_renderer_t *r)
asciichat_error_t kitty_image_write(FILE *stream, const uint8_t *rgba, int width, int height, int pitch)
void audio_recording_read(audio_recording_t *recording, float *samples, size_t count)
Definition recording.c:145
void audio_recording_destroy(audio_recording_t *recording)
Definition recording.c:59
void render_progress_finalize(render_progress_t *progress)
void render_progress_destroy(render_progress_t *progress)
uint64_t audio_samples_written
Definition renderer.c:39
uint64_t frames_written
Definition renderer.c:38
uint64_t audio_target
Definition renderer.c:42
@ UI_TITLE_FINALIZING
@ UI_TITLE_FAILED
@ UI_TITLE_IDLE

References ASCIICHAT_OK, render_file_ctx_s::audio_buf_size, render_file_ctx_s::audio_read_buf, audio_recording_destroy(), audio_recording_read(), render_file_ctx_s::audio_samples_written, render_file_ctx_s::audio_target, render_file_ctx_s::discard, render_file_ctx_s::encoder, render_file_ctx_s::error, ERROR_MEDIA_INIT, ffmpeg_encoder_destroy(), ffmpeg_encoder_write_audio(), render_file_ctx_s::frames_written, render_file_ctx_s::kitty, kitty_image_write(), NAMED_UNREGISTER, render_file_ctx_s::progress, render_file_ctx_s::recording, render_progress_destroy(), render_progress_finalize(), render_file_ctx_s::renderer, SAFE_FREE, SET_ERRNO, term_renderer_destroy(), term_renderer_height_px(), term_renderer_pitch(), term_renderer_pixels(), term_renderer_width_px(), ui_controller_title_render(), UI_TITLE_FAILED, UI_TITLE_FINALIZING, and UI_TITLE_IDLE.

Referenced by session_display_finish_render_file().

◆ render_file_set_live_timing()

void render_file_set_live_timing ( render_file_ctx_t * ctx)

Definition at line 252 of file renderer.c.

252 {
253 if (ctx) {
254 ctx->live_timing = true;
255 if (ctx->encoder)
257 }
258}
void ffmpeg_encoder_set_live_timing(ffmpeg_encoder_t *enc)

References render_file_ctx_s::encoder, ffmpeg_encoder_set_live_timing(), and render_file_ctx_s::live_timing.

Referenced by session_display_render_frame(), and session_display_set_render_live_timing().

◆ render_file_set_snapshot_actual_duration()

void render_file_set_snapshot_actual_duration ( render_file_ctx_t * ctx,
double actual_duration_sec )

Definition at line 350 of file renderer.c.

350 {
351 if (!ctx || !ctx->encoder)
352 return;
353 ffmpeg_encoder_set_snapshot_actual_duration(ctx->encoder, actual_duration_sec);
354}
void ffmpeg_encoder_set_snapshot_actual_duration(ffmpeg_encoder_t *enc, double actual_duration_sec)

References render_file_ctx_s::encoder, and ffmpeg_encoder_set_snapshot_actual_duration().

Referenced by session_display_set_snapshot_actual_duration().

◆ render_file_write_frame()

asciichat_error_t render_file_write_frame ( render_file_ctx_t * ctx,
const char * ansi_frame,
uint64_t captured_ns )

Definition at line 329 of file renderer.c.

329 {
330 if (!ctx || !frame)
331 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing render context or frame");
332 if (ctx->error != ASCIICHAT_OK)
333 return ctx->error;
334 if (ctx->discard)
335 return ASCIICHAT_OK;
336 asciichat_error_t err = render_file_write_frame_internal(ctx, frame, captured_ns);
337 if (err != ASCIICHAT_OK)
339 render_progress_frame(ctx->progress, frame, err == ASCIICHAT_OK);
340 if (err != ASCIICHAT_OK)
341 ctx->error = err;
342 return err;
343}
void render_progress_frame(render_progress_t *progress, const char *frame, bool succeeded)

References ASCIICHAT_OK, render_file_ctx_s::discard, render_file_ctx_s::error, ERROR_INVALID_PARAM, ERROR_NOT_SUPPORTED, render_file_ctx_s::progress, render_progress_frame(), SET_ERRNO, ui_controller_title_render(), and UI_TITLE_FAILED.

Referenced by session_display_encode_frame(), and session_display_render_frame().

◆ term_renderer_create()

asciichat_error_t term_renderer_create ( const term_renderer_config_t * cfg,
terminal_renderer_t ** out )

Definition at line 308 of file lib/media/render/terminal.c.

308 {
309 if (!cfg || !out || cfg->cols <= 0 || cfg->rows <= 0)
310 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing font renderer configuration or output");
311 *out = NULL;
312 if (cfg->font_data_size > LONG_MAX || (cfg->font_data_size && !cfg->font_data))
313 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid font data or size");
315 r->cols = cfg->cols;
316 r->rows = cfg->rows;
317 r->theme = cfg->theme;
318
319 if (FT_Init_FreeType(&r->ft_lib)) {
320 SAFE_FREE(r);
321 return SET_ERRNO(ERROR_INIT, "FreeType init failed");
322 }
323
324 FT_Error face_error;
325 if (cfg->font_data && cfg->font_data_size > 0) {
326 face_error = FT_New_Memory_Face(r->ft_lib, cfg->font_data, (FT_Long)cfg->font_data_size, 0, &r->ft_face);
327 } else {
328 face_error = FT_New_Face(r->ft_lib, cfg->font_spec, 0, &r->ft_face);
329 }
330 if (face_error) {
331 FT_Done_FreeType(r->ft_lib);
332 SAFE_FREE(r);
333 if (face_error == FT_Err_Cannot_Open_Resource)
334 return SET_ERRNO(ERROR_NOT_FOUND, "Cannot open font '%s' (FreeType error %d)", cfg->font_spec, face_error);
335 // Older FreeType versions recognize an HVF SFNT wrapper, then reject its
336 // missing TrueType outline tables instead of reporting an unknown format.
337 if (face_error == FT_Err_Unknown_File_Format || face_error == FT_Err_Unimplemented_Feature || face_error == FT_Err_Locations_Missing)
339 "Unsupported font format: '%s' (FreeType error %d). HVF fonts require HVF-enabled FreeType "
340 "2.15+ on macOS 15.4+ or iOS 18.4+",
341 cfg->font_spec, face_error);
342 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid font '%s' (FreeType error %d)", cfg->font_spec, face_error);
343 }
344
345 // Some older FreeType drivers can open an SFNT container without supporting
346 // its outlines. Check the parsed table directory before accepting that face.
347 FT_ULong hvf_size = 0;
348 bool is_hvf = FT_IS_SFNT(r->ft_face) && FT_Load_Sfnt_Table(r->ft_face, FT_MAKE_TAG('h', 'v', 'g', 'l'), 0, NULL, &hvf_size) == 0;
349 const char *font_format = FT_Get_Font_Format(r->ft_face);
350 if (is_hvf) {
351 bool supported = false;
352#ifdef __APPLE__
353 if (__builtin_available(macOS 15.4, iOS 18.4, *))
354 supported = font_format && strcmp(font_format, "HVF") == 0;
355#endif
356 if (!supported) {
357 FT_Done_Face(r->ft_face);
358 FT_Done_FreeType(r->ft_lib);
359 SAFE_FREE(r);
360 return SET_ERRNO(ERROR_NOT_SUPPORTED, "Unsupported HVF font '%s': requires HVF-enabled FreeType 2.15+ on macOS 15.4+ or iOS 18.4+", cfg->font_spec);
361 }
362 }
363 log_debug("Loaded font '%s' using FreeType format %s", cfg->font_spec, font_format ? font_format : "unknown");
364
365 // Detect if this is the matrix font (needs character mapping to Private Use Area)
366 r->is_matrix_font = (strstr(cfg->font_spec, "matrix") != NULL || strstr(cfg->font_spec, "Matrix") != NULL) || (cfg->font_data != NULL && GET_OPTION(matrix_rain));
367 if (r->is_matrix_font) {
368 log_debug("Detected matrix font - will use Private Use Area character mapping");
369 }
370
371 // Handle scalable vs bitmap fonts differently
372 // Bitmap fonts (like matrix) don't respond to FT_Set_Char_Size
373 // Instead, use FT_Set_Pixel_Sizes to request a specific pixel size
374 if (r->ft_face->num_fixed_sizes > 0) {
375 // Bitmap font: use FT_Set_Pixel_Sizes to select appropriate size (12px is reasonable for ASCII)
376 int err = FT_Set_Pixel_Sizes(r->ft_face, 12, 12);
377 log_debug("[BITMAP] FT_Set_Pixel_Sizes(12, 12) returned %d", err);
378
379 // If that fails, try FT_Select_Size as fallback
380 if (err != 0) {
381 err = FT_Select_Size(r->ft_face, 0); // Use first available strike
382 log_debug("[BITMAP FALLBACK] FT_Select_Size(0) returned %d", err);
383 }
384 if (err != 0) {
385 FT_Done_Face(r->ft_face);
386 FT_Done_FreeType(r->ft_lib);
387 SAFE_FREE(r);
388 return SET_ERRNO(ERROR_NOT_SUPPORTED, "Font '%s' has no usable bitmap size (FreeType error %d)", cfg->font_spec, err);
389 }
390 } else {
391 // Scalable font: use FT_Set_Char_Size
392 // FT_Set_Char_Size takes 1/64pt units and DPI — supports fractional point sizes.
393 // 96 DPI is the standard screen DPI used here; the 64 factor is the 26.6 fixed-point scale.
394 int err = FT_Set_Char_Size(r->ft_face, 0, (FT_F26Dot6)(cfg->font_size_pt * 64.0), 96, 96);
395 log_debug("[SCALABLE] FT_Set_Char_Size(size_pt=%.1f → %ld 1/64pt) returned %d", cfg->font_size_pt, (long)(cfg->font_size_pt * 64.0), err);
396 if (err != 0) {
397 FT_Done_Face(r->ft_face);
398 FT_Done_FreeType(r->ft_lib);
399 SAFE_FREE(r);
400 return SET_ERRNO(ERROR_NOT_SUPPORTED, "Font '%s' cannot be sized (FreeType error %d)", cfg->font_spec, err);
401 }
402 }
403
404 // For matrix fonts, try PUA glyph first (U+E900) since ASCII chars might not have bitmap data
405 // For other fonts, start with 'M'
406 int load_err;
407 if (r->is_matrix_font) {
408 log_debug("Matrix font detected: trying PUA glyph U+E900 first");
409 load_err = FT_Load_Char(r->ft_face, 0xE900, FT_LOAD_RENDER);
410 log_debug("FT_Load_Char(U+E900) returned %d", load_err);
411
412 // If PUA glyph fails, try ASCII fallback
413 if (load_err != 0) {
414 log_debug("U+E900 failed, trying fallback character 'M'");
415 load_err = FT_Load_Char(r->ft_face, 'M', FT_LOAD_RENDER);
416 log_debug("FT_Load_Char('M') returned %d", load_err);
417 }
418 } else {
419 // For non-matrix fonts, standard approach
420 load_err = FT_Load_Char(r->ft_face, 'M', FT_LOAD_RENDER);
421 log_debug("FT_Load_Char('M', FT_LOAD_RENDER) returned %d", load_err);
422
423 // For bitmap fonts (like matrix), FT_Load_Char may fail if the character doesn't exist.
424 // Try a fallback character if 'M' fails.
425 if (load_err != 0 && r->ft_face->num_fixed_sizes > 0) {
426 log_debug("FT_Load_Char('M') failed for bitmap font, trying fallback character 'A'");
427 load_err = FT_Load_Char(r->ft_face, 'A', FT_LOAD_RENDER);
428 log_debug("FT_Load_Char('A') returned %d", load_err);
429
430 if (load_err != 0) {
431 // Last resort: try a PUA glyph for unknown bitmap fonts
432 log_debug("FT_Load_Char('A') also failed, trying PUA glyph U+E900");
433 load_err = FT_Load_Char(r->ft_face, 0xE900, FT_LOAD_RENDER);
434 log_debug("FT_Load_Char(U+E900) returned %d", load_err);
435 }
436 }
437 }
438
439 if (load_err != 0) {
440 FT_Done_Face(r->ft_face);
441 FT_Done_FreeType(r->ft_lib);
442 SAFE_FREE(r);
443 return SET_ERRNO(ERROR_NOT_SUPPORTED, "Font '%s' cannot render glyphs (FreeType error %d)", cfg->font_spec, load_err);
444 }
445
446 // For monospace ASCII grid: use advance.x (proper character spacing) and rendered height
447 FT_Pos advance_x_26_6 = r->ft_face->glyph->advance.x;
448 log_debug("advance.x (26.6pt)=%ld", advance_x_26_6);
449
450 // Calculate cell width from font metrics
451 r->cell_w = (int)(advance_x_26_6 >> 6);
452
453 // Safeguard: for bitmap fonts, cell_w might be wrong if the font metrics are off
454 // Ensure we have a reasonable minimum width
455 if (r->cell_w <= 0) {
456 log_warn("cell_w is %d (invalid), using default 10px", r->cell_w);
457 r->cell_w = 10;
458 }
459
460 // Only add spacing to fill entire frame if --stretch is specified
461 bool stretch_mode = GET_OPTION(stretch);
462 if (stretch_mode) {
463 r->cell_w++;
464 log_debug("ADVANCE_X: value=%ld (26.6pt) → cell_w=%d (advance + 1 for stretch), cols=%d → width_px=%d", advance_x_26_6, r->cell_w, r->cols, r->cols * r->cell_w);
465 } else {
466 log_debug("ADVANCE_X: value=%ld (26.6pt) → cell_w=%d (preserve aspect ratio), cols=%d → width_px=%d", advance_x_26_6, r->cell_w, r->cols, r->cols * r->cell_w);
467 }
468
469 // For both bitmap and scalable fonts, use the actual glyph bitmap height
470 // This ensures text doesn't overflow cells. Using size->metrics.height (line spacing)
471 // instead of bitmap.rows causes cells to be too large and text to overflow.
472 r->cell_h = r->ft_face->glyph->bitmap.rows;
473 int raw_bitmap_top = r->ft_face->glyph->bitmap_top;
474 log_debug("cell_h=%d (from bitmap.rows), raw_bitmap_top=%d", r->cell_h, raw_bitmap_top);
475
476 // Fallback: if cell_h is 0 (bitmap failed to load), use aspect ratio calculation
477 if (r->cell_h <= 0) {
478 log_warn("cell_h is 0 (FT_Load_Char failed or no bitmap), using aspect ratio fallback");
479 r->cell_h = r->cell_w * 2;
480 log_debug("FALLBACK: cell_h set to %d (2x width=%d)", r->cell_h, r->cell_w);
481 }
482
483 // Apply aspect ratio correction unless --stretch is specified
484 // Terminal characters are typically 2:1 (height:width) to appear normal
485 // BUT: only correct if the loaded glyph height is reasonably close (±30%)
486 // This prevents huge jumps from small glyphs (e.g., 8px → 32px)
487 if (!stretch_mode && r->cell_h > 0) {
488 int corrected_h = r->cell_w * 2;
489 int min_acceptable = (corrected_h * 70) / 100; // 70% of target
490 int max_acceptable = (corrected_h * 130) / 100; // 130% of target
491
492 if (r->cell_h < min_acceptable || r->cell_h > max_acceptable) {
493 // Loaded height is far from expected, apply correction
494 log_debug("ASPECT_RATIO: Correcting cell_h from %d to %d (out of range [%d,%d], 2x width=%d)", r->cell_h, corrected_h, min_acceptable, max_acceptable, r->cell_w);
495 r->cell_h = corrected_h;
496 } else if (corrected_h != r->cell_h) {
497 // Loaded height is close to expected, fine-tune by small amount only
498 int delta = corrected_h - r->cell_h;
499 if (delta > 4 || delta < -4) {
500 // Only apply if difference is more than 4px
501 log_debug("ASPECT_RATIO: Fine-tuning cell_h from %d to %d (within acceptable range, 2x width=%d)", r->cell_h, corrected_h, r->cell_w);
502 r->cell_h = corrected_h;
503 } else {
504 log_debug("ASPECT_RATIO: Keeping cell_h=%d (close to target %d, delta=%d)", r->cell_h, corrected_h, delta);
505 }
506 }
507 }
508
509 // Position baseline vertically within the cell.
510 // Baseline should be positioned at approximately 75% of cell height to allow proper
511 // glyph rendering with space both above and below. This prevents glyphs from being
512 // clipped at the cell boundaries.
513 //
514 // Baseline position: y_glyph = py + r->baseline - g->bitmap_top
515 // We want: 0 <= baseline - bitmap_top < cell_h (glyph fits within cell)
516 // Choose: baseline = (3 * cell_h) / 4 (positions at 75% height)
517 r->baseline = (3 * r->cell_h) / 4;
518 log_debug("baseline set to %d (75%% of cell_h=%d)", r->baseline, r->cell_h);
519
520 r->width_px = r->cols * r->cell_w;
521 r->height_px = r->rows * r->cell_h;
522 log_debug("calculated dimensions: width_px=%d (cols=%d * cell_w=%d), height_px=%d (rows=%d * cell_h=%d)", r->width_px, r->cols, r->cell_w, r->height_px, r->rows, r->cell_h);
523 // Pitch in bytes: each pixel is 4 bytes (RGBA), already aligned to 4-byte boundary
524 int base_pitch = r->width_px * 4;
525 r->pitch = base_pitch;
526 log_debug("PITCH_CALC: width_px=%d, base_pitch=%d, final_pitch=%d (RGBA, no padding needed)", r->width_px, base_pitch, r->pitch);
527 log_debug("Allocating framebuffer: %d bytes total (%d pitch * %d height)", (int)((size_t)r->pitch * r->height_px), r->pitch, r->height_px);
528 r->framebuffer = SAFE_MALLOC((size_t)r->pitch * r->height_px, uint8_t *);
529
530 r->vt = vterm_new(r->rows, r->cols);
531 r->vts = vterm_obtain_screen(r->vt);
532
533 // Enable UTF-8 encoding so vterm correctly parses multi-byte characters
534 vterm_set_utf8(r->vt, 1);
535
536 // Set size BEFORE reset to ensure screen is allocated with correct dimensions
537 vterm_set_size(r->vt, r->rows, r->cols);
538 log_debug("vterm_set_size called with rows=%d cols=%d", r->rows, r->cols);
539
540 vterm_screen_set_callbacks(r->vts, &g_vterm_cbs, r);
541 // Reset MUST come before any input writing to initialize state correctly
542 vterm_screen_reset(r->vts, 1);
543
544 // Verify the size was set correctly after reset
545 int vt_rows, vt_cols;
546 vterm_get_size(r->vt, &vt_rows, &vt_cols);
547 log_debug("vterm_get_size returned rows=%d cols=%d (expected %d,%d)", vt_rows, vt_cols, r->rows, r->cols);
548
549 // CRITICAL: Set size AGAIN after reset, as reset may have affected it
550 vterm_set_size(r->vt, r->rows, r->cols);
551 log_debug("vterm_set_size called AGAIN after reset");
552
553 // Initialize per-renderer glyph cache (empty, will populate on demand)
554 r->glyph_cache = NULL;
555
556 *out = r;
557 return ASCIICHAT_OK;
558}
@ ERROR_NOT_FOUND
@ ERROR_INIT
Definition error_codes.h:60
const uint8_t * font_data
Definition renderer.h:44
term_renderer_theme_t theme
Definition renderer.h:41
term_renderer_theme_t theme
glyph_cache_entry_t * glyph_cache

References ASCIICHAT_OK, terminal_renderer_s::baseline, terminal_renderer_s::cell_h, terminal_renderer_s::cell_w, term_renderer_config_t::cols, terminal_renderer_s::cols, ERROR_INIT, ERROR_INVALID_PARAM, ERROR_NOT_FOUND, ERROR_NOT_SUPPORTED, term_renderer_config_t::font_data, term_renderer_config_t::font_data_size, term_renderer_config_t::font_size_pt, term_renderer_config_t::font_spec, terminal_renderer_s::framebuffer, terminal_renderer_s::ft_face, terminal_renderer_s::ft_lib, GET_OPTION, terminal_renderer_s::glyph_cache, terminal_renderer_s::height_px, terminal_renderer_s::is_matrix_font, log_debug, log_warn, out, terminal_renderer_s::pitch, term_renderer_config_t::rows, terminal_renderer_s::rows, SAFE_CALLOC, SAFE_FREE, SAFE_MALLOC, SET_ERRNO, term_renderer_config_t::theme, terminal_renderer_s::theme, terminal_renderer_s::vt, terminal_renderer_s::vts, and terminal_renderer_s::width_px.

Referenced by AsciiRendererBinding::AsciiRendererBinding(), and render_file_create().

◆ term_renderer_destroy()

void term_renderer_destroy ( terminal_renderer_t * r)

Definition at line 912 of file lib/media/render/terminal.c.

912 {
913 if (!r)
914 return;
915 vterm_free(r->vt);
916 if (r->zalgo_face)
917 FT_Done_Face(r->zalgo_face);
918 glyph_cache_destroy(&r->zalgo_cache);
919 FT_Done_Face(r->ft_face);
920 FT_Done_FreeType(r->ft_lib);
922 glyph_cache_destroy(&r->glyph_cache);
923 for (size_t i = 0; i < CELL_CACHE_SIZE; i++)
924 SAFE_FREE(r->cells[i].pixels);
925 SAFE_FREE(r);
926}
#define CELL_CACHE_SIZE
glyph_cache_entry_t * zalgo_cache

References CELL_CACHE_SIZE, terminal_renderer_s::cells, terminal_renderer_s::framebuffer, terminal_renderer_s::ft_face, terminal_renderer_s::ft_lib, terminal_renderer_s::glyph_cache, rendered_cell_t::pixels, SAFE_FREE, terminal_renderer_s::vt, terminal_renderer_s::zalgo_cache, and terminal_renderer_s::zalgo_face.

Referenced by AsciiRendererBinding::close(), render_file_create(), and render_file_destroy().

◆ term_renderer_feed()

asciichat_error_t term_renderer_feed ( terminal_renderer_t * r,
const char * ansi_frame,
size_t len )

Definition at line 632 of file lib/media/render/terminal.c.

632 {
633 if (!r || (!ansi_frame && len > 0)) {
634 return SET_ERRNO(ERROR_INVALID_PARAM, "renderer or frame is invalid");
635 }
636
637 uint64_t stage_start_ns = time_get_ns();
638 // libvterm natively implements CSI REP. Expanding those runs here turns a
639 // compact network frame into synchronous browser-main-thread work before
640 // every paint.
641 const char *frame = ansi_frame;
642 const size_t frame_len = len;
643
644 // Clear framebuffer to ensure no leftover pixels from previous frames
645 uint8_t def_fg_r, def_fg_g, def_fg_b;
646 uint8_t def_bg_r, def_bg_g, def_bg_b;
647 terminal_get_default_foreground_color(r->theme, &def_fg_r, &def_fg_g, &def_fg_b);
648 terminal_get_default_background_color(r->theme, &def_bg_r, &def_bg_g, &def_bg_b);
649 uint8_t def_bg = def_bg_r;
650 memset(r->framebuffer, def_bg, (size_t)r->pitch * r->height_px);
651
652 // Reset scrolling region and cursor position to prevent vertical shifts
653 static const char reset_sequence[] = "\033[2J\033[H\033[r\033[?7l";
654 vterm_input_write(r->vt, reset_sequence, sizeof(reset_sequence) - 1);
655
656 // Count newlines to determine CRLF conversion size
657 int newline_count = 0;
658 for (size_t i = 0; i < frame_len; i++) {
659 if (frame[i] == '\n')
660 newline_count++;
661 }
662
663 // Convert LF-only line endings to CRLF (vterm expects CRLF)
664 // This fixes cursor positioning on alternate rows
665 char *fixed_frame = SAFE_MALLOC(frame_len + (size_t)newline_count, char *);
666 if (!fixed_frame) {
667 log_error("failed to allocate memory for CRLF conversion");
668 return SET_ERRNO(ERROR_MEMORY, "CRLF conversion allocation failed");
669 }
670
671 size_t fixed_pos = 0;
672 int frame_row = 0;
673 for (size_t i = 0; i < frame_len; i++) {
674 // A video frame is a fixed grid, not a scrolling terminal transcript.
675 // A trailing newline on the bottom row must not scroll the whole image.
676 if (frame[i] == '\n' && frame_row >= r->rows - 1) {
677 continue;
678 }
679 fixed_frame[fixed_pos++] = frame[i];
680 if (frame[i] == '\n' && (i == 0 || frame[i - 1] != '\r')) {
681 // Insert carriage return before LF if not already preceded by CR
682 fixed_frame[fixed_pos - 1] = '\r';
683 fixed_frame[fixed_pos++] = '\n';
684 }
685 if (frame[i] == '\n')
686 frame_row++;
687 }
688
689 vterm_input_write(r->vt, fixed_frame, fixed_pos);
690 uint64_t parsed_ns = time_get_ns();
691 SAFE_FREE(fixed_frame);
692
693 // Calculate centering offsets to position grid in center of canvas
694 int grid_width_px = r->cols * r->cell_w;
695 int grid_height_px = r->rows * r->cell_h;
696 int offset_x = (r->width_px - grid_width_px) / 2;
697 int offset_y = (r->height_px - grid_height_px) / 2;
698
699 static int frame_count = 0;
700 static char prev_row_sigs[RENDER_DEBUG_SIGNATURE_ROWS][RENDER_DEBUG_SIGNATURE_BUFFER_SIZE] = {0}; // Store signatures of previous frame's rows
701 frame_count++;
702
703 // Log if any row signature changed to detect vertical shifts
704 if (frame_count % 10 == 0) {
706 for (int row = 0; row < r->rows && row < RENDER_DEBUG_SIGNATURE_ROWS; row++) {
707 // Get first 4 chars as signature
708 for (int col = 0; col < RENDER_DEBUG_SIGNATURE_COLUMNS && col < r->cols; col++) {
709 VTermScreenCell cell;
710 vterm_screen_get_cell(r->vts, (VTermPos){row, col}, &cell);
711 curr_row_sigs[row][col] = (char)((cell.chars[0] >= 32 && cell.chars[0] <= 126) ? cell.chars[0] : '.');
712 }
713 curr_row_sigs[row][RENDER_DEBUG_SIGNATURE_COLUMNS] = 0;
714 }
715 memcpy(prev_row_sigs, curr_row_sigs, sizeof(curr_row_sigs));
716 }
717
718 int glyph_rendered_count = 0;
719 int occupied_cell_count = 0;
720 // ANSI 16/256 colors are indexed. Converting them through libvterm for
721 // every cell is especially expensive in the single-threaded WASM renderer.
722 // A terminal palette has at most 256 entries and is stable for a frame.
723 uint32_t indexed_rgb[ASCII_LUMINANCE_LEVELS] = {0};
724 bool indexed_rgb_valid[ASCII_LUMINANCE_LEVELS] = {false};
725 for (int row = 0; row < r->rows; row++) {
726 for (int col = 0; col < r->cols; col++) {
727 VTermScreenCell cell;
728 vterm_screen_get_cell(r->vts, (VTermPos){row, col}, &cell);
729
730 uint8_t fr, fg, fb, br, bg, bb;
731 if (VTERM_COLOR_IS_RGB(&cell.fg)) {
732 fr = cell.fg.rgb.red;
733 fg = cell.fg.rgb.green;
734 fb = cell.fg.rgb.blue;
735 } else if (VTERM_COLOR_IS_INDEXED(&cell.fg)) {
736 const uint8_t index = cell.fg.indexed.idx;
737 if (!indexed_rgb_valid[index]) {
738 VTermColor foreground = cell.fg;
739 vterm_screen_convert_color_to_rgb(r->vts, &foreground);
740 indexed_rgb[index] = ((uint32_t)foreground.rgb.red << 16) | ((uint32_t)foreground.rgb.green << 8) | foreground.rgb.blue;
741 indexed_rgb_valid[index] = true;
742 }
743 fr = (uint8_t)(indexed_rgb[index] >> 16);
744 fg = (uint8_t)(indexed_rgb[index] >> 8);
745 fb = (uint8_t)indexed_rgb[index];
746 } else {
747 fr = def_fg_r;
748 fg = def_fg_g;
749 fb = def_fg_b;
750 }
751
752 if (VTERM_COLOR_IS_RGB(&cell.bg)) {
753 br = cell.bg.rgb.red;
754 bg = cell.bg.rgb.green;
755 bb = cell.bg.rgb.blue;
756 } else if (VTERM_COLOR_IS_INDEXED(&cell.bg)) {
757 const uint8_t index = cell.bg.indexed.idx;
758 if (!indexed_rgb_valid[index]) {
759 VTermColor background = cell.bg;
760 vterm_screen_convert_color_to_rgb(r->vts, &background);
761 indexed_rgb[index] = ((uint32_t)background.rgb.red << 16) | ((uint32_t)background.rgb.green << 8) | background.rgb.blue;
762 indexed_rgb_valid[index] = true;
763 }
764 br = (uint8_t)(indexed_rgb[index] >> 16);
765 bg = (uint8_t)(indexed_rgb[index] >> 8);
766 bb = (uint8_t)indexed_rgb[index];
767 } else {
768 br = def_bg_r;
769 bg = def_bg_g;
770 bb = def_bg_b;
771 }
772
773 int px = offset_x + col * r->cell_w, py = offset_y + row * r->cell_h;
774 uint32_t foreground = ((uint32_t)fr << 16) | ((uint32_t)fg << 8) | fb;
775 uint32_t background = ((uint32_t)br << 16) | ((uint32_t)bg << 8) | bb;
776 // Retain the low 32 bits without overflowing the intermediate products.
777 uint32_t cell_hash = (uint32_t)((uint64_t)cell.chars[0] * UINT64_C(2654435761) ^ (uint64_t)foreground * UINT64_C(2246822519) ^ background);
778 cell_hash ^= cell_hash >> 16;
779 rendered_cell_t *cached_cell = &r->cells[cell_hash % CELL_CACHE_SIZE];
780 bool contained = px >= 0 && py >= 0 && px + r->cell_w <= r->width_px && py + r->cell_h <= r->height_px;
781 if (contained && cached_cell->valid && cached_cell->character == cell.chars[0] && cached_cell->foreground == foreground && cached_cell->background == background) {
782 for (int dy = 0; dy < r->cell_h; dy++) {
783 memcpy(r->framebuffer + (py + dy) * r->pitch + px * 4, cached_cell->pixels + dy * r->cell_w * 4, (size_t)r->cell_w * 4);
784 }
785 if (cell.chars[0] && cell.chars[0] != ' ') {
786 occupied_cell_count++;
787 glyph_rendered_count++;
788 }
789 continue;
790 }
791 // Block elements represent cell geometry, independent of font bearings and line spacing.
792 bool block = cell.chars[0] == 0x2580 || cell.chars[0] == 0x2584 || cell.chars[0] == 0x2588;
793 for (int dy = 0; dy < r->cell_h; dy++) {
794 bool block_foreground = block && (cell.chars[0] == 0x2588 || (cell.chars[0] == 0x2580 ? dy < r->cell_h / 2 : dy >= r->cell_h / 2));
795 int y = py + dy;
796 if (y < 0 || y >= r->height_px)
797 continue;
798 uint8_t *line = r->framebuffer + y * r->pitch + px * 4;
799 for (int dx = 0; dx < r->cell_w; dx++) {
800 int x = px + dx;
801 if (x < 0 || x >= r->width_px)
802 continue;
803 line[dx * 4] = block_foreground ? fr : br;
804 line[dx * 4 + 1] = block_foreground ? fg : bg;
805 line[dx * 4 + 2] = block_foreground ? fb : bb;
806 line[dx * 4 + 3] = 255;
807 }
808 }
809
810 if (block) {
811 occupied_cell_count++;
812 glyph_rendered_count++;
813 } else if (cell.chars[0] && cell.chars[0] != ' ') {
814 occupied_cell_count++;
815 // For matrix font, map ASCII characters to Private Use Area glyphs (U+E900-U+E91A)
816 uint32_t char_to_render = r->is_matrix_font ? matrix_char_map(cell.chars[0]) : cell.chars[0];
817
818 // Use per-renderer glyph cache to avoid rasterizing every glyph every frame
819 FT_Bitmap *cached_bitmap = glyph_cache_get(r, r->ft_face, char_to_render);
820 if (cached_bitmap && cached_bitmap->width > 0 && cached_bitmap->rows > 0) {
821 // Find cache entry to get positioning offsets
822 glyph_cache_entry_t *cache_entry = NULL;
823 HASH_FIND_INT(r->glyph_cache, &char_to_render, cache_entry);
824 if (cache_entry) {
825 // Overhanging glyphs must retain the normal cross-cell drawing order.
826 contained = contained && cache_entry->bitmap_left >= 0 && cache_entry->bitmap_left + cache_entry->width <= r->cell_w && r->baseline - cache_entry->bitmap_top >= 0 &&
827 r->baseline - cache_entry->bitmap_top + cache_entry->rows <= r->cell_h;
828 blit_glyph(r, cached_bitmap, px + cache_entry->bitmap_left, py + r->baseline - cache_entry->bitmap_top, fr, fg, fb, br, bg, bb);
829 glyph_rendered_count++;
830 }
831 } else if (cell.chars[0] >= 0x2800 && cell.chars[0] <= 0x28ff) {
832 // The bundled font lacks Braille. Draw its eight-dot pattern in the
833 // cell when the selected font cannot supply the requested glyph.
834 static const unsigned dot_bits[4][2] = {{0, 3}, {1, 4}, {2, 5}, {6, 7}};
835 unsigned pattern = cell.chars[0] - 0x2800;
836 int dot_w = r->cell_w / 4 > 0 ? r->cell_w / 4 : 1;
837 int dot_h = r->cell_h / 8 > 0 ? r->cell_h / 8 : 1;
838 for (int dot_row = 0; dot_row < 4; dot_row++) {
839 for (int dot_col = 0; dot_col < 2; dot_col++) {
840 if (!(pattern & (1U << dot_bits[dot_row][dot_col])))
841 continue;
842 int left = px + (2 * dot_col + 1) * r->cell_w / 4 - dot_w / 2;
843 int top = py + (2 * dot_row + 1) * r->cell_h / 8 - dot_h / 2;
844 for (int dy = 0; dy < dot_h; dy++) {
845 for (int dx = 0; dx < dot_w; dx++) {
846 int x = left + dx, y = top + dy;
847 if (x < px || x >= px + r->cell_w || y < py || y >= py + r->cell_h || x < 0 || x >= r->width_px || y < 0 || y >= r->height_px)
848 continue;
849 uint8_t *pixel = r->framebuffer + y * r->pitch + x * 4;
850 pixel[0] = fr;
851 pixel[1] = fg;
852 pixel[2] = fb;
853 pixel[3] = 255;
854 }
855 }
856 }
857 }
858 glyph_rendered_count++;
859 } else {
860 return SET_ERRNO(ERROR_NOT_SUPPORTED, "Font cannot render Unicode character U+%04X", cell.chars[0]);
861 }
862 }
863 if (contained) {
864 if (!cached_cell->pixels)
865 cached_cell->pixels = SAFE_MALLOC((size_t)r->cell_w * r->cell_h * 4, uint8_t *);
866 if (cached_cell->pixels) {
867 for (int dy = 0; dy < r->cell_h; dy++) {
868 memcpy(cached_cell->pixels + dy * r->cell_w * 4, r->framebuffer + (py + dy) * r->pitch + px * 4, (size_t)r->cell_w * 4);
869 }
870 cached_cell->character = cell.chars[0];
871 cached_cell->foreground = foreground;
872 cached_cell->background = background;
873 cached_cell->valid = true;
874 }
875 }
876 }
877 }
878 render_zalgo(r, offset_x, offset_y);
879 uint64_t rendered_ns = time_get_ns();
880 log_debug_every(US_PER_SEC_INT, "RECORD_RASTER: input_bytes=%zu parse_ms=%.2f paint_ms=%.2f cells=%dx%d", len, (double)(parsed_ns - stage_start_ns) / NS_PER_MS_INT,
881 (double)(rendered_ns - parsed_ns) / NS_PER_MS_INT, r->cols, r->rows);
882 log_debug_every(5 * US_PER_SEC_INT, "Rendered %d glyphs from %d occupied cells out of %d cells", glyph_rendered_count, occupied_cell_count, r->rows * r->cols);
883
884 return ASCIICHAT_OK;
885}
unsigned int uint32_t
@ ERROR_MEMORY
Definition error_codes.h:55
#define NS_PER_MS_INT
Definition time.h:172
#define US_PER_SEC_INT
Definition time.h:177
#define ASCII_LUMINANCE_LEVELS
Number of luminance levels supported (256).
Definition ascii.h:438
#define RENDER_DEBUG_SIGNATURE_ROWS
#define RENDER_DEBUG_SIGNATURE_COLUMNS
#define RENDER_DEBUG_SIGNATURE_BUFFER_SIZE
#define log_debug_every(interval_us, fmt,...)
Rate-limited DEBUG logging.
Definition log/log.h:719
static bool size_t index
Definition queries.h:10
void terminal_get_default_background_color(int theme, uint8_t *r, uint8_t *g, uint8_t *b)
void terminal_get_default_foreground_color(int theme, uint8_t *r, uint8_t *g, uint8_t *b)
Owns the temporary storage used by glyph cache entry. The data pointers and size or cursor fields des...
int bitmap_left
int bitmap_top
int rows
int width

References ASCII_LUMINANCE_LEVELS, ASCIICHAT_OK, rendered_cell_t::background, terminal_renderer_s::baseline, glyph_cache_entry_t::bitmap_left, glyph_cache_entry_t::bitmap_top, CELL_CACHE_SIZE, terminal_renderer_s::cell_h, terminal_renderer_s::cell_w, terminal_renderer_s::cells, rendered_cell_t::character, terminal_renderer_s::cols, ERROR_INVALID_PARAM, ERROR_MEMORY, ERROR_NOT_SUPPORTED, rendered_cell_t::foreground, terminal_renderer_s::framebuffer, terminal_renderer_s::ft_face, terminal_renderer_s::glyph_cache, terminal_renderer_s::height_px, index, terminal_renderer_s::is_matrix_font, log_debug_every, log_error, NS_PER_MS_INT, terminal_renderer_s::pitch, rendered_cell_t::pixels, RENDER_DEBUG_SIGNATURE_BUFFER_SIZE, RENDER_DEBUG_SIGNATURE_COLUMNS, RENDER_DEBUG_SIGNATURE_ROWS, glyph_cache_entry_t::rows, terminal_renderer_s::rows, SAFE_FREE, SAFE_MALLOC, SET_ERRNO, terminal_get_default_background_color(), terminal_get_default_foreground_color(), terminal_renderer_s::theme, US_PER_SEC_INT, rendered_cell_t::valid, terminal_renderer_s::vt, terminal_renderer_s::vts, glyph_cache_entry_t::width, and terminal_renderer_s::width_px.

Referenced by AsciiRendererBinding::render().

◆ term_renderer_get_cols()

int term_renderer_get_cols ( terminal_renderer_t * r)

Definition at line 900 of file lib/media/render/terminal.c.

900 {
901 if (!r)
902 return 0;
903 return r->cols;
904}

References terminal_renderer_s::cols.

Referenced by AsciiRendererBinding::dimensions().

◆ term_renderer_get_rows()

int term_renderer_get_rows ( terminal_renderer_t * r)

Definition at line 906 of file lib/media/render/terminal.c.

906 {
907 if (!r)
908 return 0;
909 return r->rows;
910}

References terminal_renderer_s::rows.

Referenced by AsciiRendererBinding::dimensions().

◆ term_renderer_height_px()

int term_renderer_height_px ( terminal_renderer_t * r)

◆ term_renderer_pitch()

int term_renderer_pitch ( terminal_renderer_t * r)

Definition at line 896 of file lib/media/render/terminal.c.

896 {
897 return r->pitch;
898}

References terminal_renderer_s::pitch.

Referenced by AsciiRendererBinding::render(), and render_file_destroy().

◆ term_renderer_pixels()

const uint8_t * term_renderer_pixels ( terminal_renderer_t * r)

Definition at line 887 of file lib/media/render/terminal.c.

887 {
888 return r->framebuffer;
889}

References terminal_renderer_s::framebuffer.

Referenced by AsciiRendererBinding::render(), and render_file_destroy().

◆ term_renderer_set_zalgo()

asciichat_error_t term_renderer_set_zalgo ( terminal_renderer_t * r,
int level )

Enable raster Zalgo (1-10), or disable with zero.

Definition at line 560 of file lib/media/render/terminal.c.

560 {
561 if (!r || level < 0 || level > 10)
562 return SET_ERRNO(ERROR_INVALID_PARAM, "Zalgo raster level must be 0-10");
563 if (level && !r->zalgo_face) {
564 if (FT_New_Memory_Face(r->ft_lib, g_font_default, (FT_Long)g_font_default_size, 0, &r->zalgo_face))
565 return SET_ERRNO(ERROR_INIT, "Cannot load Zalgo combining font");
566 if (FT_Set_Pixel_Sizes(r->zalgo_face, 0, (FT_UInt)(r->cell_w * 2))) {
567 FT_Done_Face(r->zalgo_face);
568 r->zalgo_face = NULL;
569 return SET_ERRNO(ERROR_INIT, "Cannot size Zalgo combining font");
570 }
571 }
572 r->zalgo_level = level;
573 return ASCIICHAT_OK;
574}

References ASCIICHAT_OK, terminal_renderer_s::cell_w, ERROR_INIT, ERROR_INVALID_PARAM, terminal_renderer_s::ft_lib, g_font_default, g_font_default_size, SET_ERRNO, terminal_renderer_s::zalgo_face, and terminal_renderer_s::zalgo_level.

Referenced by AsciiRendererBinding::render().

◆ term_renderer_width_px()

int term_renderer_width_px ( terminal_renderer_t * r)