Modules

Subtitles

moduleSource

Available on macOS, Linux, Windows, Wasm

#import "Subtitles";

Procedures#

Parse#

(path: string) -> (bool, Subtitles)

Reads and parses a subtitle file. Returns false when it cannot be read or is malformed; the problem is reported through the log. Free the result with free.

(data: string, short_name: string) -> (bool, Subtitles)

Parses subtitle text, which opens with a [1] version line. :name starts a section (::name marks it finalized), =speaker 1.5 starts a timed instant, the lines after it are speech, ++ text is a stage direction, and +name parameter begin end adds an event. The sections point into data, which must outlive them. short_name names the source in error messages.

text := "[1]\n:intro\n=guard 1.5\nHalt!\n++ raises a hand\n";
ok, subtitles := load_subtitles_from_memory(text, "intro");
assert(ok);
section := subtitles.all_sections[0];
assert(section.name == "intro" && section.instants[0].speaker == "guard");
assert(section.instants[0].lines.count == 2);
free(*subtitles);
↗
(subtitles: *Subtitles)

Frees the sections and tables, and the text itself when load_subtitles_from_file read it.

Serialize#

(subtitles: *Subtitles) -> string

Writes subtitles back out in the text form load_subtitles_from_memory reads, starting with a [1] version line. The caller frees the string.

Render#

(builder: *String_Builder, sections: [] *Subtitles_Section, character_to_emphasize := "", name_map: *H.Table(string, string) = null)

Appends a complete HTML page showing sections as dialogue, one block per section. Lines of character_to_emphasize are highlighted; name_map swaps speaker ids for display names.

(s: string) -> string

Returns s in double quotes with inner quotes doubled, as a CSV field. The caller frees it.

General#

(builder: *B.String_Builder, section_name: string, name_map: *H.Table(string, string), _speaker_name: string, lines: [] string, directions: [] string)

Writes the CSV row for one speaker's gathered lines, if any of them has text.

Types#

:: struct

Dialogue scripts for games: sections of timed speech, stage directions and events. Load text with load_subtitles_from_memory or load_subtitles_from_file, write it with serialize, and turn sections into a reading page (format_sections_as_html) or a recording sheet (format_sections_as_csv).

data: string
data_owned: bool
sections_by_name: H.Table(string, *Subtitles_Section)
all_sections: [..] Subtitles_Section
:: struct

One line of a section: something a character says, or a stage direction.

type: enum u8 { UNINITIALIZED :: 0; SPOKEN :: 1; DIRECTION :: 2; }
text: string
:: struct

A moment of dialogue: one speaker, the lines they say, and when (-1 when untimed). A speaker of _ in the source reads back as an empty speaker.

lines: [..] Subtitles_Line
speaker: string
time: float64
:: struct

A named cue (an animation or sound, say) with an optional parameter, tied to an instant or to explicit start and end times. A time of -1 is unset.

name: string
parameter: string
instant_index: s32
time_begin: float64
time_end: float64

Constants#