ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
epub.h
Go to the documentation of this file.
1
54
55#pragma once
56
57#ifdef __cplusplus
58extern "C" {
59#endif
60
61#include <stdalign.h>
62#include <stddef.h>
63#include <stdint.h>
64
65#include "epub_miniz_alloc.h"
66#include "ra8_err.h"
67#include "xml.h"
68
69/* ===========================================================================
70 * Compile-time limits
71 * ===========================================================================
72 */
73
98
112
124typedef enum : uint8_t {
129
147
157typedef enum : uint8_t {
160
161/* ===========================================================================
162 * Opaque media interface
163 * ===========================================================================
164 */
165
179typedef struct {
180 const uint8_t* data;
181 size_t size;
183
212typedef size_t (*epub_stream_read_fn)(void* ctx, uint64_t offset, void* buf, size_t len);
213
232typedef struct {
234 void* ctx;
235 uint64_t size;
237
238/* ===========================================================================
239 * Manifest item
240 * ===========================================================================
241 */
242
265
266/* ===========================================================================
267 * Book handle
268 * ===========================================================================
269 */
270
286typedef struct {
287 /* --- Backing storage (caller-owned) ---------------------------------- */
288 const uint8_t* zip_bytes;
289 size_t zip_size;
290
291 /* --- miniz state ----------------------------------------------------- */
292 /* Inline storage for `mz_zip_archive`. Sized via static_assert in
293 * epub_open.c; the book record owns the archive in-place rather
294 * than allocating it on the heap (NASA Rule 3). Cast through
295 * `(mz_zip_archive*)&book->zip_archive_storage[0]` inside the
296 * implementation. The exact upstream type would force this header
297 * to include `miniz.h`; we hold an opaque byte buffer instead.
298 *
299 * `alignas(max_align_t)` is mandatory: ``mz_zip_archive`` carries
300 * pointer-typed and ``uint64_t`` fields that require 8-byte
301 * alignment. Without the alignment specifier the cast is
302 * undefined behaviour on strict-alignment architectures. */
303 alignas(max_align_t) uint8_t
312
313 /* --- Streamed backing (caller-seekable, no resident blob) (#151) ----- */
314 /* For a book opened via `epub_open_streamed()`, miniz's `m_pIO_opaque`
315 * points at this inline descriptor (a stable address for the book's
316 * lifetime), and `zip_bytes == NULL`. For the resident `epub_open()`
317 * path this is left zeroed and unused. */
319
320 /* --- Chapter table --------------------------------------------------- */
321 uint16_t chapter_count;
324
325 /* --- Metadata -------------------------------------------------------- */
330
331 /* --- Cover ----------------------------------------------------------- */
333
334 /* --- Embedded fonts (#109) ------------------------------------------- */
338
339 /* --- Manifest (document order, #151) -------------------------------- */
340 uint16_t manifest_count;
343
344 /* --- OPF base directory --------------------------------------------- */
346
347 /* --- Table of contents ---------------------------------------------- */
349 uint8_t toc_kind;
350 uint16_t toc_count;
352
353 /* --- Optional embedded font for glyph rendering --------------------- */
354 const uint8_t* font_data;
355 size_t font_size;
356
357 /* --- Lifecycle ------------------------------------------------------- */
358 uint8_t in_use;
360
361/* ===========================================================================
362 * Metadata struct
363 * ===========================================================================
364 */
365
370typedef struct {
371 const char* title;
372 const char* author;
373 const char* language;
374 const char* identifier;
376
377/* ===========================================================================
378 * Public API -- lifecycle
379 * ===========================================================================
380 */
381
427[[nodiscard]] ra8_err_t epub_open(const void* media, const char* path, epub_book_t* out_book);
428
476[[nodiscard]] ra8_err_t
477epub_open_streamed(const epub_stream_media_t* media, const char* path, epub_book_t* out_book);
478
495[[nodiscard]] ra8_err_t epub_close(epub_book_t* book);
496
497/* ===========================================================================
498 * Public API -- chapter iteration
499 * ===========================================================================
500 */
501
517[[nodiscard]] ra8_err_t epub_get_chapter_count(const epub_book_t* book, uint16_t* out_count);
518
541[[nodiscard]] ra8_err_t epub_load_chapter(epub_book_t* book,
542 uint16_t idx,
543 uint8_t* out_xhtml,
544 size_t max_len,
545 size_t* got_len);
546
547/* ===========================================================================
548 * Public API -- table of contents
549 * ===========================================================================
550 */
551
570[[nodiscard]] ra8_err_t epub_get_toc_kind(const epub_book_t* book, uint8_t* out_kind);
571
594[[nodiscard]] ra8_err_t epub_get_toc_count(const epub_book_t* book, uint16_t* out_count);
595
616[[nodiscard]] ra8_err_t
617epub_get_toc_entry(const epub_book_t* book, uint16_t idx, epub_toc_entry_t* out_entry);
618
648[[nodiscard]] ra8_err_t
649epub_toc_entry_to_chapter(const epub_book_t* book, uint16_t toc_idx, uint16_t* out_chapter_idx);
650
651/* ===========================================================================
652 * Public API -- metadata + cover + glyph rasterise
653 * ===========================================================================
654 */
655
673[[nodiscard]] ra8_err_t epub_get_metadata(const epub_book_t* book, epub_metadata_t* out_meta);
674
698[[nodiscard]] ra8_err_t
699epub_get_cover_image(epub_book_t* book, uint8_t* out_buf, size_t max_len, size_t* got_len);
700
735[[nodiscard]] ra8_err_t epub_get_resource(epub_book_t* book,
736 const char* path,
737 uint8_t* out_buf,
738 size_t max_len,
739 size_t* got_len);
740
761[[nodiscard]] uint16_t epub_manifest_count(const epub_book_t* book);
762
782[[nodiscard]] const epub_manifest_item_t* epub_manifest_item(const epub_book_t* book,
783 uint16_t index);
784
814[[nodiscard]] ra8_err_t epub_get_embedded_font_count(const epub_book_t* book, uint16_t* out_count);
815
849 uint16_t idx,
850 uint8_t* out_buf,
851 size_t max_len,
852 size_t* got_len);
853
876[[nodiscard]] ra8_err_t
877epub_set_font(epub_book_t* book, const uint8_t* font_data, size_t font_size);
878
905[[nodiscard]] ra8_err_t epub_render_glyph(const epub_book_t* book,
906 int32_t codepoint,
907 float font_size,
908 uint8_t* out_bitmap,
909 size_t max_pixels,
910 uint32_t* out_w,
911 uint32_t* out_h);
912#ifdef __cplusplus
913}
914#endif
uint16_t epub_manifest_count(const epub_book_t *book)
Number of <manifest> <item> entries retained (#151).
ra8_err_t epub_set_font(epub_book_t *book, const uint8_t *font_data, size_t font_size)
Attach a TTF font blob to be used by epub_render_glyph().
const epub_manifest_item_t * epub_manifest_item(const epub_book_t *book, uint16_t index)
Borrow the manifest item at index (OPF document order, #151).
ra8_err_t epub_toc_entry_to_chapter(const epub_book_t *book, uint16_t toc_idx, uint16_t *out_chapter_idx)
Resolve a TOC entry to the spine chapter index it points into.
ra8_err_t epub_get_embedded_font_count(const epub_book_t *book, uint16_t *out_count)
Count the fonts the EPUB ships in its OPF manifest (#109).
ra8_err_t epub_close(epub_book_t *book)
Close a previously opened EPUB book.
Definition epub_open.c:507
ra8_err_t epub_get_cover_image(epub_book_t *book, uint8_t *out_buf, size_t max_len, size_t *got_len)
Copy the raw bytes of the cover image into the caller's buffer.
ra8_err_t epub_render_glyph(const epub_book_t *book, int32_t codepoint, float font_size, uint8_t *out_bitmap, size_t max_pixels, uint32_t *out_w, uint32_t *out_h)
Rasterise a single Unicode code point into an alpha-8 bitmap.
ra8_err_t epub_get_toc_kind(const epub_book_t *book, uint8_t *out_kind)
Report which navigation document the TOC was parsed from.
size_t(* epub_stream_read_fn)(void *ctx, uint64_t offset, void *buf, size_t len)
Seek+read callback backing a streamed (non-resident) EPUB open (#151).
Definition epub.h:212
ra8_err_t epub_open_streamed(const epub_stream_media_t *media, const char *path, epub_book_t *out_book)
Open an EPUB book from a seekable stream, with no whole-file residency (#151).
Definition epub_open.c:455
ra8_err_t epub_get_metadata(const epub_book_t *book, epub_metadata_t *out_meta)
Return Dublin Core metadata strings.
ra8_err_t epub_load_chapter(epub_book_t *book, uint16_t idx, uint8_t *out_xhtml, size_t max_len, size_t *got_len)
Extract the raw XHTML bytes of one chapter into the caller's buffer.
epub_limits_t
Static-allocation caps for the EPUB reader.
Definition epub.h:86
@ k_epub_id_len
Max manifest item id length (incl.
Definition epub.h:93
@ k_epub_max_manifest
Max <manifest> <item> entries kept (OPF order).
Definition epub.h:92
@ k_epub_max_fonts
Max embedded font manifest items kept.
Definition epub.h:91
@ k_epub_zip_archive_bytes
Storage for mz_zip_archive (miniz 3.0.2 sizeof=112; margin; static_assert in .c).
Definition epub.h:95
@ k_epub_media_len
Max media-type length (incl.
Definition epub.h:94
@ k_epub_max_toc
Max table-of-contents entries we keep.
Definition epub.h:88
@ k_epub_max_chapters
Max spine length we accept.
Definition epub.h:87
@ k_epub_max_path_len
Max href length (incl.
Definition epub.h:89
@ k_epub_meta_len
Max bytes per metadata field.
Definition epub.h:90
ra8_err_t epub_get_toc_entry(const epub_book_t *book, uint16_t idx, epub_toc_entry_t *out_entry)
Copy one table-of-contents entry into the caller's struct.
ra8_err_t epub_get_embedded_font(epub_book_t *book, uint16_t idx, uint8_t *out_buf, size_t max_len, size_t *got_len)
Extract one EPUB-shipped font's bytes into a caller buffer (#109).
epub_render_t
Pixel-format selectors for epub_render_glyph().
Definition epub.h:157
@ k_epub_render_alpha8
8 bits per pixel, alpha mask.
Definition epub.h:158
epub_toc_kind_t
Which navigation document the table of contents was parsed from.
Definition epub.h:124
@ k_epub_toc_ncx
Parsed from an EPUB 2 NCX document.
Definition epub.h:126
@ k_epub_toc_nav
Parsed from an EPUB 3 nav.xhtml.
Definition epub.h:127
@ k_epub_toc_none
No table of contents parsed.
Definition epub.h:125
ra8_err_t epub_open(const void *media, const char *path, epub_book_t *out_book)
Open an EPUB book from an opaque media handle.
Definition epub_open.c:405
ra8_err_t epub_get_chapter_count(const epub_book_t *book, uint16_t *out_count)
Report how many chapters the spine contains.
ra8_err_t epub_get_toc_count(const epub_book_t *book, uint16_t *out_count)
Report how many table-of-contents entries the book exposes.
ra8_err_t epub_get_resource(epub_book_t *book, const char *path, uint8_t *out_buf, size_t max_len, size_t *got_len)
Copy the raw bytes of an arbitrary archive resource into the caller's buffer (#140 external styleshee...
Caller-owned bounded-arena allocator for miniz.
Error Code Definitions for ra8-firmware.
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Definition ra8_err.h:546
Opened EPUB book.
Definition epub.h:286
char author[k_epub_meta_len]
Dublin Core <dc:creator>.
Definition epub.h:327
char toc_path[k_epub_max_path_len]
Nav/NCX href (rel.
Definition epub.h:348
char chapter_paths[k_epub_max_chapters][k_epub_max_path_len]
Manifest hrefs (relative to OPF dir).
Definition epub.h:323
epub_miniz_workspace_t miniz_workspace
Per-book bounded allocator workspace; never shared with another book.
Definition epub.h:309
size_t font_size
TTF blob length.
Definition epub.h:355
size_t zip_size
Length of the EPUB blob.
Definition epub.h:289
char language[k_epub_meta_len]
Dublin Core <dc:language>.
Definition epub.h:328
char cover_path[k_epub_max_path_len]
Path to cover image (or empty).
Definition epub.h:332
uint16_t chapter_count
Spine length actually stored.
Definition epub.h:321
epub_xml_workspace_t xml_workspace
Per-book XML parser state; never shared with another book.
Definition epub.h:311
char title[k_epub_meta_len]
Dublin Core <dc:title>.
Definition epub.h:326
uint16_t toc_count
Number of entries stored in toc.
Definition epub.h:350
uint8_t toc_kind
epub_toc_kind_t: source of the TOC.
Definition epub.h:349
epub_manifest_item_t manifest[k_epub_max_manifest]
Items, OPF order.
Definition epub.h:342
uint16_t embedded_font_count
Manifest font items found (<= cap).
Definition epub.h:335
epub_miniz_arena_t miniz_arena
Per-book allocator descriptor bound through miniz's opaque pointer.
Definition epub.h:307
epub_toc_entry_t toc[k_epub_max_toc]
Flattened TOC, document order.
Definition epub.h:351
epub_stream_media_t stream_media
Streamed media descriptor; {} for the resident path.
Definition epub.h:318
uint16_t manifest_count
<manifest> <item> entries stored (<= cap).
Definition epub.h:340
uint8_t zip_archive_storage[k_epub_zip_archive_bytes]
Zip archive storage.
Definition epub.h:304
uint8_t zip_archive_active
1 = mz_zip_reader_init succeeded.
Definition epub.h:305
char identifier[k_epub_meta_len]
Dublin Core <dc:identifier> (unique book id).
Definition epub.h:329
uint8_t in_use
1 = open, 0 = closed.
Definition epub.h:358
char opf_dir[k_epub_max_path_len]
Directory portion of the OPF.
Definition epub.h:345
const uint8_t * zip_bytes
Pointer to the EPUB blob.
Definition epub.h:288
char embedded_font_paths[k_epub_max_fonts][k_epub_max_path_len]
Font hrefs (rel.
Definition epub.h:337
const uint8_t * font_data
TTF blob; NULL if unset.
Definition epub.h:354
One <manifest> <item> entry, retained in OPF document order.
Definition epub.h:260
char media_type[k_epub_media_len]
Item media-type (or "" if absent).
Definition epub.h:263
char href[k_epub_max_path_len]
Item href (relative to the OPF dir).
Definition epub.h:262
In-memory EPUB media descriptor.
Definition epub.h:179
const uint8_t * data
Pointer to the EPUB byte stream.
Definition epub.h:180
size_t size
Length of the EPUB byte stream, bytes.
Definition epub.h:181
Dublin Core metadata bundle returned by epub_get_metadata().
Definition epub.h:370
const char * identifier
NUL-terminated unique id; "" if absent.
Definition epub.h:374
const char * author
NUL-terminated creator; "" if absent.
Definition epub.h:372
const char * title
NUL-terminated title; "" if absent.
Definition epub.h:371
const char * language
NUL-terminated language tag; "" if absent.
Definition epub.h:373
Descriptor for one caller-owned miniz allocation arena.
Exactly-sized, maximally-aligned storage for one miniz arena.
Seekable EPUB media descriptor – opens with no whole-file residency (#151).
Definition epub.h:232
void * ctx
Opaque backing passed to read (out-lives the book).
Definition epub.h:234
uint64_t size
Total archive length in bytes (> 0).
Definition epub.h:235
epub_stream_read_fn read
Seek+read callback (non-NULL).
Definition epub.h:233
One titled entry in the table of contents.
Definition epub.h:142
char href[k_epub_max_path_len]
Target href (rel.
Definition epub.h:144
char title[k_epub_meta_len]
Navigation label text ("" if none).
Definition epub.h:143
uint8_t depth
Nesting level; 0 == top level.
Definition epub.h:145
Per-book bounded storage for strict OPF/container/TOC parsing.
Definition epub.h:105
xml_workspace_t reader
4096-byte open-element stack.
Definition epub.h:106
xml_span_t references[k_epub_max_chapters]
Spine idref spans retained between OPF passes.
Definition epub.h:108
xml_span_t legacy_cover_id
EPUB 2 cover manifest id.
Definition epub.h:109
uint16_t reference_count
Valid entries in references.
Definition epub.h:110
Immutable byte span expressed relative to the source.
Definition xml.h:44
Exactly bounded caller-owned nesting storage.
Definition xml.h:57
Bounded, caller-owned, no-heap XML pull reader.