foxygit / Hush Log in
commits tags

/src/wrap.h · 1.19 KB

raw
#ifndef HUSH_WRAP_H
#define HUSH_WRAP_H

/* A contiguous byte range belonging to one styled render run, in source order. */
typedef struct {
    int start, end;
} WrapRunSpan;

typedef float (*WrapMeasureFn)(void *ctx, int runIndex, const char *text, int start, int len);

/* One styled piece of text placed on a single visual line. `runIndex` refers back
   into the WrapRunSpan array passed to wrap_layout (i.e. the caller's render-run list). */
typedef struct {
    int runIndex;
    int start, end;
    float width;
} WrapSegment;

typedef struct {
    WrapSegment *segs;
    int count, cap;
} WrapLine;

typedef struct {
    WrapLine *lines;
    int count, cap;
} WrapResult;

void wrap_result_init(WrapResult *wr);
void wrap_result_free(WrapResult *wr);

/* Greedily word-wraps `text` (using the styled run spans in `runs`) to fit within `maxWidth`.
   Always produces at least one (possibly empty) line. Breaks only occur at space characters;
   a run of atoms glued together with no space between them is never split across lines. */
void wrap_layout(const char *text, const WrapRunSpan *runs, int runCount, float maxWidth,
                  WrapMeasureFn measure, void *measureCtx, WrapResult *out);

#endif