ekp/ekp_c/ekp.c
Kinneyzhang 74a780ce95 refactor!: remove the experimental C tokenization path; harden the module
The self-contained C path (ekp-c-break-lines, ekp-c-load-hyphenator,
ekp-c-hyphenate, ekp-c-set-spacing, ekp_paragraph.c, ekp_hyphen.c,
~1500 lines) was never used by ekp.el, diverged semantically from the
real pipeline (no kinsoku, no protrusion, no two-pass emergency), and
contained an exploitable heap overflow reachable from Lisp:
ekp_para_create sized its box array as box_count * 2, but a long word
hyphenates into arbitrarily many syllable boxes, overflowing the
calloc'd buffer.  Deleting the path deletes the bug class.

Hardening of the live path:

- thread pool: sized from the machine's core count instead of a
  hardcoded 8; created lazily on the first multi-paragraph batch
  (single-paragraph users never start worker threads); a full queue
  now blocks the submitter until a worker makes room — tasks were
  silently dropped before, degrading the batch to the Elisp fallback
  exactly when parallelism mattered most.
- unified failure gate: a partial allocation used to silently drop
  kinsoku, hyphenation or protrusion data and continue with a subtly
  different layout; any allocation failure or pending Lisp signal
  (non-local exit from a bad element type) now fails the whole call,
  and ekp.el falls back to the Elisp engine.  The Elisp bridge wraps
  both C entry points in condition-case, and a whole-batch nil no
  longer crashes the per-paragraph loop.
- integer safety: every extracted pixel value is clamped to int32
  instead of silently wrapping.
- EKP_INFINITY (the unreachable-state sentinel) is now a real
  infinity: extremely degenerate paragraphs could legitimately
  accumulate demerits past the old 1e10 constant, making C consider
  reachable states dead and diverge from the Elisp engine.

BREAKING: the four experimental module functions are gone; rebuild
with make -C ekp_c clean all (version gate unchanged at 1.5).

92 ERT green; fuzz 300/300 byte-identical across engines.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-27 01:49:25 +08:00

633 lines
25 KiB
C

/*
* ekp.c - Emacs Knuth-Plass module entry point
*
* Copyright (C) 2024-2026 Kinney Zhang
* SPDX-License-Identifier: GPL-3.0-or-later
*
* This file is part of emacs-kp, which is free software: you can
* redistribute it and/or modify it under the terms of the GNU General
* Public License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
* It is distributed WITHOUT ANY WARRANTY; see the GNU General Public
* License (COPYING) for details.
*
* This is the main entry file for the Emacs dynamic module.
* Naming follows Emacs module convention: module name = file name.
* (require 'ekp-c) loads ekp.so, entry point in ekp.c
*
* Bridges C implementation to Emacs Lisp. Keep the interface minimal:
* pixel measurement callback from Emacs, everything else in C.
*/
#include "ekp_module.h"
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
/* Required for Emacs modules */
int plugin_is_GPL_compatible;
/* All pixel quantities travel as int32; clamp instead of silently
* wrapping if Elisp ever hands us something absurd. */
static inline int32_t clamp32(intmax_t v)
{
if (v > INT32_MAX) return INT32_MAX;
if (v < INT32_MIN) return INT32_MIN;
return (int32_t)v;
}
/*
* ekp-c-init: Initialize the module
*/
static emacs_value Fekp_c_init(emacs_env *env, ptrdiff_t nargs,
emacs_value *args, void *data)
{
(void)nargs; (void)args; (void)data;
if (ekp_init() != 0) {
emacs_value signal = env->intern(env, "error");
emacs_value msg = env->make_string(env, "Failed to initialize ekp-c", 26);
env->non_local_exit_signal(env, signal, msg);
return env->intern(env, "nil");
}
return env->intern(env, "t");
}
/*
* ekp-c-cleanup: Cleanup resources
*/
static emacs_value Fekp_c_cleanup(emacs_env *env, ptrdiff_t nargs,
emacs_value *args, void *data)
{
(void)nargs; (void)args; (void)data;
ekp_cleanup();
return env->intern(env, "t");
}
/*
* ekp-c-set-penalties: Set K-P parameters
*/
static emacs_value Fekp_c_set_penalties(emacs_env *env, ptrdiff_t nargs,
emacs_value *args, void *data)
{
(void)data;
if (!ekp_global || nargs < 4)
return env->intern(env, "nil");
ekp_global->line_penalty = clamp32(env->extract_integer(env, args[0]));
ekp_global->hyphen_penalty = clamp32(env->extract_integer(env, args[1]));
ekp_global->fitness_penalty = clamp32(env->extract_integer(env, args[2]));
ekp_global->last_line_ratio = env->extract_float(env, args[3]);
if (nargs > 4)
ekp_global->consec_hyphen_penalty = clamp32(env->extract_integer(env, args[4]));
if (nargs > 5)
ekp_global->last_line_short_penalty = env->extract_float(env, args[5]);
/* Per-line extra stretch for non-justify alignment; reset to 0
* when the caller omits it so stale values never leak. */
ekp_global->extra_stretch =
(nargs > 6) ? (int32_t)clamp32(env->extract_integer(env, args[6])) : 0;
return env->intern(env, "t");
}
/*
* ekp-c-version: Return module version
*/
static emacs_value Fekp_c_version(emacs_env *env, ptrdiff_t nargs,
emacs_value *args, void *data)
{
(void)nargs; (void)args; (void)data;
char version[32];
snprintf(version, sizeof(version), "%d.%d",
EKP_VERSION_MAJOR, EKP_VERSION_MINOR);
return env->make_string(env, version, strlen(version));
}
/*
* ekp-c-thread-count: Return number of worker threads
*/
static emacs_value Fekp_c_thread_count(emacs_env *env, ptrdiff_t nargs,
emacs_value *args, void *data)
{
(void)nargs; (void)args; (void)data;
/* Pool is created lazily; report its actual size once it exists,
* else the size it will get. */
if (ekp_global && ekp_global->pool)
return env->make_integer(env, (intmax_t)ekp_global->pool->thread_count);
return env->make_integer(env, (intmax_t)ekp_pool_default_threads());
}
/*
* ekp-c-break-with-arrays: Pure DP with Elisp-provided prefix arrays
*
* Args: (ideal-prefix min-prefix max-prefix glue-ideals glue-shrinks
* glue-stretches hyphen-positions hyphen-width line-width
* lead-spaces trail-spaces forbidden-positions tail-protrudes
* hyphen-protrude first-line-width)
*
* Array sizes must be consistent:
* - ideal/min/max-prefix, lead/trail-spaces, tail-protrudes: n+1
* - glue-ideals/shrinks/stretches: n elements
*
* Returns: (breaks . total-cost) where breaks is a list of box indices.
*
* This is the preferred API: Elisp computes all font-dependent values,
* C module only does pure O(n²) DP computation.
*/
static emacs_value Fekp_c_break_with_arrays(emacs_env *env, ptrdiff_t nargs,
emacs_value *args, void *data)
{
(void)data;
if (!ekp_global || nargs < 15)
return env->intern(env, "nil");
/* Get prefix array sizes (n+1 elements) */
ptrdiff_t prefix_len = env->vec_size(env, args[0]);
if (prefix_len <= 1)
return env->intern(env, "nil");
size_t n = prefix_len - 1; /* number of boxes */
/* Allocate arrays */
int32_t *ideal_prefix = malloc(prefix_len * sizeof(int32_t));
int32_t *min_prefix = malloc(prefix_len * sizeof(int32_t));
int32_t *max_prefix = malloc(prefix_len * sizeof(int32_t));
int32_t *glue_ideals = malloc(n * sizeof(int32_t));
int32_t *glue_shrinks = malloc(n * sizeof(int32_t));
int32_t *glue_stretches = malloc(n * sizeof(int32_t));
int32_t *lead_spaces = malloc(prefix_len * sizeof(int32_t));
int32_t *trail_spaces = malloc(prefix_len * sizeof(int32_t));
if (!ideal_prefix || !min_prefix || !max_prefix ||
!glue_ideals || !glue_shrinks || !glue_stretches ||
!lead_spaces || !trail_spaces) {
free(ideal_prefix); free(min_prefix); free(max_prefix);
free(glue_ideals); free(glue_shrinks); free(glue_stretches);
free(lead_spaces); free(trail_spaces);
return env->intern(env, "nil");
}
/* Extract prefix arrays */
for (ptrdiff_t i = 0; i < prefix_len; i++) {
ideal_prefix[i] = clamp32(env->extract_integer(env, env->vec_get(env, args[0], i)));
min_prefix[i] = clamp32(env->extract_integer(env, env->vec_get(env, args[1], i)));
max_prefix[i] = clamp32(env->extract_integer(env, env->vec_get(env, args[2], i)));
lead_spaces[i] = clamp32(env->extract_integer(env, env->vec_get(env, args[9], i)));
trail_spaces[i] = clamp32(env->extract_integer(env, env->vec_get(env, args[10], i)));
}
/* Extract glue arrays */
for (size_t i = 0; i < n; i++) {
glue_ideals[i] = clamp32(env->extract_integer(env, env->vec_get(env, args[3], i)));
glue_shrinks[i] = clamp32(env->extract_integer(env, env->vec_get(env, args[4], i)));
glue_stretches[i] = clamp32(env->extract_integer(env, env->vec_get(env, args[5], i)));
}
/* Get hyphen positions vector */
ptrdiff_t hyph_count = env->vec_size(env, args[6]);
int32_t *hyph_pos = NULL;
if (hyph_count > 0) {
hyph_pos = malloc(hyph_count * sizeof(int32_t));
if (hyph_pos) {
for (ptrdiff_t i = 0; i < hyph_count; i++) {
hyph_pos[i] = clamp32(env->extract_integer(env, env->vec_get(env, args[6], i)));
}
}
}
int32_t hyph_width = clamp32(env->extract_integer(env, args[7]));
int32_t line_width = clamp32(env->extract_integer(env, args[8]));
/* Right-edge protrusion: per-gap array (n+1) and hyphen scalar */
int32_t *tail_pro = malloc(prefix_len * sizeof(int32_t));
if (tail_pro) {
for (ptrdiff_t i = 0; i < prefix_len; i++) {
tail_pro[i] = clamp32(env->extract_integer(env, env->vec_get(env, args[12], i)));
}
}
int32_t hyphen_protrude = clamp32(env->extract_integer(env, args[13]));
int32_t first_line_width = clamp32(env->extract_integer(env, args[14]));
/* Forbidden break positions (sorted gap indices, may be empty) */
ptrdiff_t forb_count = env->vec_size(env, args[11]);
int32_t *forb_pos = NULL;
if (forb_count > 0) {
forb_pos = malloc(forb_count * sizeof(int32_t));
if (forb_pos) {
for (ptrdiff_t i = 0; i < forb_count; i++) {
forb_pos[i] = clamp32(env->extract_integer(env, env->vec_get(env, args[11], i)));
}
}
}
/* One consolidated gate: any partial allocation above (silent
* "no kinsoku / no hyphenation" degradation) or a pending Lisp
* signal from a bad element type must fail the whole call — the
* Elisp engine is the correct fallback, not a subtly different
* layout. */
if ((hyph_count > 0 && !hyph_pos) ||
(forb_count > 0 && !forb_pos) ||
!tail_pro ||
env->non_local_exit_check(env) != emacs_funcall_exit_return) {
free(ideal_prefix); free(min_prefix); free(max_prefix);
free(glue_ideals); free(glue_shrinks); free(glue_stretches);
free(lead_spaces); free(trail_spaces);
free(hyph_pos); free(forb_pos); free(tail_pro);
return env->intern(env, "nil");
}
/* Call the pure DP function */
ekp_result_t *result = ekp_break_with_prefixes(
ideal_prefix, min_prefix, max_prefix,
glue_ideals, glue_shrinks, glue_stretches,
n,
hyph_pos, hyph_count > 0 ? (size_t)hyph_count : 0,
hyph_width, line_width,
lead_spaces, trail_spaces,
forb_pos, (forb_pos && forb_count > 0) ? (size_t)forb_count : 0,
tail_pro, hyphen_protrude, first_line_width);
free(ideal_prefix); free(min_prefix); free(max_prefix);
free(glue_ideals); free(glue_shrinks); free(glue_stretches);
free(lead_spaces); free(trail_spaces);
free(hyph_pos);
free(forb_pos);
free(tail_pro);
if (!result)
return env->intern(env, "nil");
/* Build result: ((breaks...) . cost) */
emacs_value breaks_list = env->intern(env, "nil");
emacs_value cons_sym = env->intern(env, "cons");
for (size_t i = result->break_count; i > 0; i--) {
emacs_value brk = env->make_integer(env, result->breaks[i - 1]);
emacs_value args2[2] = {brk, breaks_list};
breaks_list = env->funcall(env, cons_sym, 2, args2);
}
emacs_value cost = env->make_float(env, result->total_cost);
emacs_value args2[2] = {breaks_list, cost};
emacs_value final = env->funcall(env, cons_sym, 2, args2);
ekp_result_destroy(result);
return final;
}
/*
* Helper to extract paragraph data from Elisp vectors
*/
static bool extract_paragraph_data(
emacs_env *env, emacs_value *args,
int32_t **ideal_prefix, int32_t **min_prefix, int32_t **max_prefix,
int32_t **glue_ideals, int32_t **glue_shrinks, int32_t **glue_stretches,
int32_t **hyph_pos, size_t *n, ptrdiff_t *hyph_count,
int32_t *hyph_width, int32_t *line_width,
int32_t **lead_spaces, int32_t **trail_spaces,
int32_t **forb_pos, ptrdiff_t *forb_count,
int32_t **tail_pro, int32_t *hyphen_protrude,
int32_t *first_line_width)
{
ptrdiff_t prefix_len = env->vec_size(env, args[0]);
if (prefix_len <= 1)
return false;
*n = prefix_len - 1;
*ideal_prefix = malloc(prefix_len * sizeof(int32_t));
*min_prefix = malloc(prefix_len * sizeof(int32_t));
*max_prefix = malloc(prefix_len * sizeof(int32_t));
*glue_ideals = malloc(*n * sizeof(int32_t));
*glue_shrinks = malloc(*n * sizeof(int32_t));
*glue_stretches = malloc(*n * sizeof(int32_t));
*lead_spaces = malloc(prefix_len * sizeof(int32_t));
*trail_spaces = malloc(prefix_len * sizeof(int32_t));
if (!*ideal_prefix || !*min_prefix || !*max_prefix ||
!*glue_ideals || !*glue_shrinks || !*glue_stretches ||
!*lead_spaces || !*trail_spaces) {
free(*ideal_prefix); free(*min_prefix); free(*max_prefix);
free(*glue_ideals); free(*glue_shrinks); free(*glue_stretches);
free(*lead_spaces); free(*trail_spaces);
return false;
}
for (ptrdiff_t i = 0; i < prefix_len; i++) {
(*ideal_prefix)[i] = clamp32(env->extract_integer(env, env->vec_get(env, args[0], i)));
(*min_prefix)[i] = clamp32(env->extract_integer(env, env->vec_get(env, args[1], i)));
(*max_prefix)[i] = clamp32(env->extract_integer(env, env->vec_get(env, args[2], i)));
(*lead_spaces)[i] = clamp32(env->extract_integer(env, env->vec_get(env, args[9], i)));
(*trail_spaces)[i] = clamp32(env->extract_integer(env, env->vec_get(env, args[10], i)));
}
for (size_t i = 0; i < *n; i++) {
(*glue_ideals)[i] = clamp32(env->extract_integer(env, env->vec_get(env, args[3], i)));
(*glue_shrinks)[i] = clamp32(env->extract_integer(env, env->vec_get(env, args[4], i)));
(*glue_stretches)[i] = clamp32(env->extract_integer(env, env->vec_get(env, args[5], i)));
}
*hyph_count = env->vec_size(env, args[6]);
*hyph_pos = NULL;
if (*hyph_count > 0) {
*hyph_pos = malloc(*hyph_count * sizeof(int32_t));
if (*hyph_pos) {
for (ptrdiff_t i = 0; i < *hyph_count; i++) {
(*hyph_pos)[i] = clamp32(env->extract_integer(env, env->vec_get(env, args[6], i)));
}
}
}
*hyph_width = clamp32(env->extract_integer(env, args[7]));
*line_width = clamp32(env->extract_integer(env, args[8]));
*forb_count = env->vec_size(env, args[11]);
*forb_pos = NULL;
if (*forb_count > 0) {
*forb_pos = malloc(*forb_count * sizeof(int32_t));
if (*forb_pos) {
for (ptrdiff_t i = 0; i < *forb_count; i++) {
(*forb_pos)[i] = clamp32(env->extract_integer(env, env->vec_get(env, args[11], i)));
}
}
}
*tail_pro = malloc(prefix_len * sizeof(int32_t));
if (*tail_pro) {
for (ptrdiff_t i = 0; i < prefix_len; i++) {
(*tail_pro)[i] = clamp32(env->extract_integer(env, env->vec_get(env, args[12], i)));
}
}
*hyphen_protrude = clamp32(env->extract_integer(env, args[13]));
*first_line_width = clamp32(env->extract_integer(env, args[14]));
if ((*hyph_count > 0 && !*hyph_pos) ||
(*forb_count > 0 && !*forb_pos) ||
!*tail_pro ||
env->non_local_exit_check(env) != emacs_funcall_exit_return) {
free(*ideal_prefix); free(*min_prefix); free(*max_prefix);
free(*glue_ideals); free(*glue_shrinks); free(*glue_stretches);
free(*lead_spaces); free(*trail_spaces);
free(*hyph_pos); free(*forb_pos); free(*tail_pro);
*ideal_prefix = *min_prefix = *max_prefix = NULL;
*glue_ideals = *glue_shrinks = *glue_stretches = NULL;
*lead_spaces = *trail_spaces = NULL;
*hyph_pos = *forb_pos = *tail_pro = NULL;
return false;
}
return true;
}
/*
* ekp-c-break-batch: Process multiple paragraphs in parallel
*
* Args: vector of (ideal-prefix min-prefix max-prefix glue-ideals glue-shrinks
* glue-stretches hyphen-positions hyphen-width line-width)
*
* Each element is a vector of 9 elements (same as ekp-c-break-with-arrays args).
* Returns vector of (breaks . total-cost) for each paragraph.
*
* This is the high-performance API for processing multi-paragraph text.
*/
static emacs_value Fekp_c_break_batch(emacs_env *env, ptrdiff_t nargs,
emacs_value *args, void *data)
{
(void)data;
if (!ekp_global || nargs < 1)
return env->intern(env, "nil");
ptrdiff_t para_count = env->vec_size(env, args[0]);
if (para_count <= 0)
return env->intern(env, "nil");
/* Allocate batch inputs and temporary storage */
ekp_batch_input_t *inputs = calloc(para_count, sizeof(ekp_batch_input_t));
int32_t **all_ideal = calloc(para_count, sizeof(int32_t *));
int32_t **all_min = calloc(para_count, sizeof(int32_t *));
int32_t **all_max = calloc(para_count, sizeof(int32_t *));
int32_t **all_glue_i = calloc(para_count, sizeof(int32_t *));
int32_t **all_glue_sh = calloc(para_count, sizeof(int32_t *));
int32_t **all_glue_st = calloc(para_count, sizeof(int32_t *));
int32_t **all_hyph = calloc(para_count, sizeof(int32_t *));
int32_t **all_lead = calloc(para_count, sizeof(int32_t *));
int32_t **all_trail = calloc(para_count, sizeof(int32_t *));
int32_t **all_forb = calloc(para_count, sizeof(int32_t *));
int32_t **all_pro = calloc(para_count, sizeof(int32_t *));
if (!inputs || !all_ideal || !all_min || !all_max ||
!all_glue_i || !all_glue_sh || !all_glue_st || !all_hyph ||
!all_lead || !all_trail || !all_forb || !all_pro) {
free(inputs); free(all_ideal); free(all_min); free(all_max);
free(all_glue_i); free(all_glue_sh); free(all_glue_st); free(all_hyph);
free(all_lead); free(all_trail); free(all_forb); free(all_pro);
return env->intern(env, "nil");
}
/* Extract all paragraph data */
for (ptrdiff_t p = 0; p < para_count; p++) {
emacs_value para_vec = env->vec_get(env, args[0], p);
/* Extract 15 arguments from this paragraph's vector */
emacs_value para_args[15];
for (int i = 0; i < 15; i++) {
para_args[i] = env->vec_get(env, para_vec, i);
}
size_t n;
ptrdiff_t hyph_count, forb_count;
int32_t hyph_width, line_width, hyphen_protrude, first_line_width;
if (!extract_paragraph_data(env, para_args,
&all_ideal[p], &all_min[p], &all_max[p],
&all_glue_i[p], &all_glue_sh[p], &all_glue_st[p],
&all_hyph[p], &n, &hyph_count,
&hyph_width, &line_width,
&all_lead[p], &all_trail[p],
&all_forb[p], &forb_count,
&all_pro[p], &hyphen_protrude,
&first_line_width)) {
/* Cleanup on failure */
for (ptrdiff_t j = 0; j < p; j++) {
free(all_ideal[j]); free(all_min[j]); free(all_max[j]);
free(all_glue_i[j]); free(all_glue_sh[j]); free(all_glue_st[j]);
free(all_hyph[j]); free(all_lead[j]); free(all_trail[j]);
free(all_forb[j]); free(all_pro[j]);
}
free(inputs); free(all_ideal); free(all_min); free(all_max);
free(all_glue_i); free(all_glue_sh); free(all_glue_st); free(all_hyph);
free(all_lead); free(all_trail); free(all_forb); free(all_pro);
return env->intern(env, "nil");
}
inputs[p].ideal_prefix = all_ideal[p];
inputs[p].min_prefix = all_min[p];
inputs[p].max_prefix = all_max[p];
inputs[p].glue_ideals = all_glue_i[p];
inputs[p].glue_shrinks = all_glue_sh[p];
inputs[p].glue_stretches = all_glue_st[p];
inputs[p].n = n;
inputs[p].hyphen_positions = all_hyph[p];
inputs[p].hyphen_count = hyph_count > 0 ? (size_t)hyph_count : 0;
inputs[p].hyphen_width = hyph_width;
inputs[p].line_width = line_width;
inputs[p].lead_spaces = all_lead[p];
inputs[p].trail_spaces = all_trail[p];
inputs[p].forbidden_positions = all_forb[p];
inputs[p].forbidden_count =
(all_forb[p] && forb_count > 0) ? (size_t)forb_count : 0;
inputs[p].tail_protrudes = all_pro[p];
inputs[p].hyphen_protrude = hyphen_protrude;
inputs[p].first_line_width = first_line_width;
}
/* Process all paragraphs in parallel */
ekp_result_t **results = ekp_break_batch(inputs, para_count);
/* Cleanup input arrays */
for (ptrdiff_t p = 0; p < para_count; p++) {
free(all_ideal[p]); free(all_min[p]); free(all_max[p]);
free(all_glue_i[p]); free(all_glue_sh[p]); free(all_glue_st[p]);
free(all_hyph[p]); free(all_lead[p]); free(all_trail[p]);
free(all_forb[p]); free(all_pro[p]);
}
free(inputs); free(all_ideal); free(all_min); free(all_max);
free(all_glue_i); free(all_glue_sh); free(all_glue_st); free(all_hyph);
free(all_lead); free(all_trail); free(all_forb); free(all_pro);
if (!results)
return env->intern(env, "nil");
/* Build result vector */
emacs_value result_vec = env->funcall(env, env->intern(env, "make-vector"),
2, (emacs_value[]){
env->make_integer(env, para_count),
env->intern(env, "nil")
});
emacs_value cons_sym = env->intern(env, "cons");
for (ptrdiff_t p = 0; p < para_count; p++) {
ekp_result_t *r = results[p];
emacs_value entry;
if (r) {
/* Build (breaks . cost) */
emacs_value breaks_list = env->intern(env, "nil");
for (size_t i = r->break_count; i > 0; i--) {
emacs_value brk = env->make_integer(env, r->breaks[i - 1]);
emacs_value args2[2] = {brk, breaks_list};
breaks_list = env->funcall(env, cons_sym, 2, args2);
}
emacs_value cost = env->make_float(env, r->total_cost);
emacs_value args2[2] = {breaks_list, cost};
entry = env->funcall(env, cons_sym, 2, args2);
ekp_result_destroy(r);
} else {
entry = env->intern(env, "nil");
}
env->vec_set(env, result_vec, p, entry);
}
free(results);
return result_vec;
}
/*
* Helper to define functions
*/
static void defun(emacs_env *env, const char *name,
ptrdiff_t min_arity, ptrdiff_t max_arity,
emacs_value (*func)(emacs_env *, ptrdiff_t, emacs_value *, void *),
const char *doc)
{
emacs_value fn = env->make_function(env, min_arity, max_arity, func, doc, NULL);
emacs_value sym = env->intern(env, name);
emacs_value args[2] = {sym, fn};
env->funcall(env, env->intern(env, "fset"), 2, args);
}
/*
* Module entry point - required by Emacs dynamic module spec
*/
int emacs_module_init(struct emacs_runtime *runtime)
{
if ((size_t)runtime->size < sizeof(*runtime))
return 1;
emacs_env *env = runtime->get_environment(runtime);
if ((size_t)env->size < sizeof(*env))
return 2;
/* Define functions */
defun(env, "ekp-c-init", 0, 0, Fekp_c_init,
"Initialize EKP C module with thread pool.");
defun(env, "ekp-c-cleanup", 0, 0, Fekp_c_cleanup,
"Cleanup EKP C module resources.");
defun(env, "ekp-c-set-penalties", 4, 7, Fekp_c_set_penalties,
"Set Knuth-Plass algorithm penalties.\n\n\
LINE-PENALTY: base penalty per line break (default 10)\n\
HYPHEN-PENALTY: penalty for hyphenated breaks (default 50)\n\
FITNESS-PENALTY: penalty for adjacent line tightness mismatch (default 100)\n\
LAST-LINE-RATIO: minimum fill ratio for last line (default 0.5)\n\
CONSEC-HYPHEN-PENALTY: multiplier for consecutive hyphen runs (default 100)\n\
LAST-LINE-SHORT-PENALTY: multiplier for short last lines (default 50.0)\n\n\
(fn LINE-PENALTY HYPHEN-PENALTY FITNESS-PENALTY LAST-LINE-RATIO \
&optional CONSEC-HYPHEN-PENALTY LAST-LINE-SHORT-PENALTY)");
defun(env, "ekp-c-break-with-arrays", 15, 15, Fekp_c_break_with_arrays,
"Break lines using Elisp's pre-computed prefix arrays (preferred API).\n\n\
IDEAL-PREFIX: vector of ideal width prefix sums (n+1 elements)\n\
MIN-PREFIX: vector of min width prefix sums (n+1 elements)\n\
MAX-PREFIX: vector of max width prefix sums (n+1 elements)\n\
GLUE-IDEALS: vector of glue ideal widths (n elements)\n\
GLUE-SHRINKS: vector of glue shrink amounts (n elements)\n\
GLUE-STRETCHES: vector of glue stretch amounts (n elements)\n\
HYPHEN-POS: vector of hyphenable box indices (sorted)\n\
HYPHEN-WIDTH: pixel width of hyphen character\n\
LINE-WIDTH: target line width in pixels\n\
LEAD-SPACES: vector (n+1) of space-box run widths starting at box i\n\
TRAIL-SPACES: vector (n+1) of space-box run widths ending at box k-1\n\
FORBIDDEN-POS: vector of gap indices where breaking is forbidden (sorted)\n\
TAIL-PROTRUDES: vector (n+1) of right-edge protrusion pixels per gap\n\
HYPHEN-PROTRUDE: protrusion pixels for the soft hyphen\n\
FIRST-LINE-WIDTH: width of line 0 (first-line indent); <=0 = LINE-WIDTH\n\n\
Returns (BREAKS . TOTAL-COST) where BREAKS is list of box indices.\n\
This API ensures C uses Elisp's font-dependent measurements.\n\n\
(fn IDEAL-PREFIX MIN-PREFIX MAX-PREFIX GLUE-IDEALS GLUE-SHRINKS GLUE-STRETCHES \
HYPHEN-POS HYPHEN-WIDTH LINE-WIDTH LEAD-SPACES TRAIL-SPACES FORBIDDEN-POS \
TAIL-PROTRUDES HYPHEN-PROTRUDE FIRST-LINE-WIDTH)");
defun(env, "ekp-c-version", 0, 0, Fekp_c_version,
"Return EKP C module version string.");
defun(env, "ekp-c-thread-count", 0, 0, Fekp_c_thread_count,
"Return number of worker threads in the thread pool.");
defun(env, "ekp-c-break-batch", 1, 1, Fekp_c_break_batch,
"Break multiple paragraphs in parallel.\n\n\
PARAGRAPHS: vector of paragraph data, each element a vector of the\n\
same 15 items `ekp-c-break-with-arrays' takes, in the same order.\n\n\
Returns vector of (BREAKS . COST) for each paragraph.\n\
This is the high-performance API for multi-paragraph processing.\n\n\
(fn PARAGRAPHS)");
/* Provide feature */
emacs_value provide_args[1] = {env->intern(env, "ekp-c")};
env->funcall(env, env->intern(env, "provide"), 1, provide_args);
return 0;
}