42#include "stb_truetype.h"
53typedef enum : uint16_t {
79 return (w < 0) || (h < 0);
99 return (in_use == 0U) || (zip_archive_active == 0U);
120 if ((dst ==
nullptr) || (cap == 0U)) {
125 if (dir !=
nullptr) {
126 while ((off < (cap - 1U)) && (dir[off] !=
'\0')) {
131 if (name !=
nullptr) {
133 while ((off < (cap - 1U)) && (name[i] !=
'\0')) {
166 const char* prefixed_path,
167 const char* bare_path,
172 int32_t file_idx = mz_zip_reader_locate_file(zip, prefixed_path,
nullptr, 0U);
174 file_idx = mz_zip_reader_locate_file(zip, bare_path,
nullptr, 0U);
179 mz_zip_archive_file_stat st;
182 if (mz_zip_reader_file_stat(zip, (mz_uint)file_idx, &st) == MZ_FALSE) {
189 if ((
size_t)st.m_uncomp_size > max_len) {
194 if (mz_zip_reader_extract_to_mem(zip, (mz_uint)file_idx, out_buf, max_len, 0U) == MZ_FALSE) {
197 *got_len = (size_t)st.m_uncomp_size;
224 const int32_t offset = stbtt_GetFontOffsetForIndex(book->
font_data, 0);
277 const float scale = stbtt_ScaleForPixelHeight(font, font_size);
282 stbtt_GetCodepointBitmapBox(font, codepoint, scale, scale, &x0, &y0, &x1, &y1);
283 const int w = x1 - x0;
284 const int h = y1 - y0;
291 const size_t total = (size_t)w * (
size_t)h;
292 if (total > max_pixels) {
298 stbtt_MakeCodepointBitmap(font, out_bitmap, w, h, w, scale, scale, codepoint);
300 *out_w = (uint32_t)w;
301 *out_h = (uint32_t)h;
312 if ((book ==
nullptr) || (out_count ==
nullptr)) {
328 if ((book ==
nullptr) || (out_xhtml ==
nullptr) || (got_len ==
nullptr)) {
346 mz_zip_archive* zip = (mz_zip_archive*)zip_storage;
357 if ((book ==
nullptr) || (out_kind ==
nullptr)) {
369 if ((book ==
nullptr) || (out_count ==
nullptr)) {
381 if ((book ==
nullptr) || (out_entry ==
nullptr)) {
390 *out_entry = book->
toc[idx];
397 if ((book ==
nullptr) || (out_chapter_idx ==
nullptr)) {
409 const char*
const href = book->
toc[toc_idx].
href;
410 size_t base_len = 0U;
411 while ((href[base_len] !=
'\0') && (href[base_len] !=
'#')) {
417 if (
strlen(path) != base_len) {
420 if (
strncmp(path, href, base_len) != 0) {
423 *out_chapter_idx = i;
431 if ((book ==
nullptr) || (out_meta ==
nullptr)) {
446 if ((book ==
nullptr) || (out_buf ==
nullptr) || (got_len ==
nullptr)) {
464 mz_zip_archive* zip = (mz_zip_archive*)zip_storage;
474 if ((book ==
nullptr) || (path ==
nullptr) || (out_buf ==
nullptr) || (got_len ==
nullptr)) {
489 mz_zip_archive* zip = (mz_zip_archive*)zip_storage;
495 if ((book ==
nullptr) || (out_count ==
nullptr)) {
507 if (book ==
nullptr) {
518 if ((book ==
nullptr) || (book->
in_use == 0U)) {
533 if ((book ==
nullptr) || (out_buf ==
nullptr) || (got_len ==
nullptr)) {
551 mz_zip_archive* zip = (mz_zip_archive*)zip_storage;
562 if ((book ==
nullptr) || (font_data ==
nullptr)) {
584 if ((book ==
nullptr) || (out_bitmap ==
nullptr) || (out_w ==
nullptr) || (out_h ==
nullptr)) {
592 if (font_size <= 0.0F) {
EPUB (.epub) reader and chapter iterator for ra8-firmware.
@ k_epub_max_path_len
Max href length (incl.
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).
static ra8_err_t internal_locate_extract(mz_zip_archive *zip, const char *prefixed_path, const char *bare_path, uint8_t *out_buf, size_t max_len, size_t *got_len)
Locate-then-extract a file from the open zip into a caller buffer.
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.
bool priv_epub_glyph_dim_invalid(int w, int h)
Pure glyph-dim-invalid predicate.
epub_chapter_internal_t
Implementation-only sizing constants.
@ k_epub_min_font_bytes
Smallest TTF blob we'll accept.
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.
ra8_err_t epub_get_metadata(const epub_book_t *book, epub_metadata_t *out_meta)
Return Dublin Core metadata strings.
bool priv_epub_book_not_ready(uint8_t in_use, uint8_t zip_archive_active)
Pure book-not-ready predicate.
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.
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.
static ra8_err_t internal_font_init(const epub_book_t *book, stbtt_fontinfo *out_font)
Initialise an stbtt_fontinfo from the book's font blob.
static ra8_err_t internal_render_into(const stbtt_fontinfo *font, int32_t codepoint, float font_size, uint8_t *out_bitmap, size_t max_pixels, uint32_t *out_w, uint32_t *out_h)
Rasterise the code point and copy into the caller buffer.
void priv_epub_join_path(const char *dir, const char *name, char *dst, size_t cap)
Concatenate dir + name into dst, NUL-terminated.
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).
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...
Test-access surface for epub internal helpers (MC/DC).
ra8_err_t priv_epub_zip_guard_entry(const mz_zip_archive_file_stat *st)
Guard one ZIP entry's declared sizes against the policy.
Annotation-attribute framework macros for ra8-firmware.
#define RA8_PRIV
Module-private helper: shared across TUs but only inside one library.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
Error Code Definitions for ra8-firmware.
@ k_ra8_err_no_mem
Static buffer exhausted (no dynamic memory on this project).
@ k_ra8_err_out_of_range
Sensor or peripheral output out of valid range.
@ k_ra8_err_invalid_arg
Invalid function argument.
@ k_ra8_err_validation_failed
Validation rule failed (caller-supplied invariant not satisfied).
@ k_ra8_err_not_initialized
Module not initialized – _init() not yet called successfully.
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
@ k_ra8_err_null_ptr
Pointer was NULL where a valid pointer was required.
@ k_ra8_err_not_found
Requested item not found (lookup / search missed).
@ k_ra8_err_invalid_size
Invalid size parameter (too large, too small, or misaligned).
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
int strncmp(const char *s1, const char *s2, size_t n)
Compare two strings up to a specified length.
size_t strlen(const char *s)
Calculate string length.
sfnt (TrueType/OpenType) table-directory bounds guard for stb_truetype.
bool ra8_stbtt_sfnt_dir_in_bounds(const uint8_t *data, size_t len, uint32_t fontstart)
Verify that a font's sfnt table directory lies within its buffer.
char author[k_epub_meta_len]
Dublin Core <dc:creator>.
char chapter_paths[k_epub_max_chapters][k_epub_max_path_len]
Manifest hrefs (relative to OPF dir).
size_t font_size
TTF blob length.
char language[k_epub_meta_len]
Dublin Core <dc:language>.
char cover_path[k_epub_max_path_len]
Path to cover image (or empty).
uint16_t chapter_count
Spine length actually stored.
char title[k_epub_meta_len]
Dublin Core <dc:title>.
uint16_t toc_count
Number of entries stored in toc.
uint8_t toc_kind
epub_toc_kind_t: source of the TOC.
epub_manifest_item_t manifest[k_epub_max_manifest]
Items, OPF order.
uint16_t embedded_font_count
Manifest font items found (<= cap).
epub_toc_entry_t toc[k_epub_max_toc]
Flattened TOC, document order.
uint16_t manifest_count
<manifest> <item> entries stored (<= cap).
uint8_t zip_archive_storage[k_epub_zip_archive_bytes]
Zip archive storage.
uint8_t zip_archive_active
1 = mz_zip_reader_init succeeded.
char identifier[k_epub_meta_len]
Dublin Core <dc:identifier> (unique book id).
uint8_t in_use
1 = open, 0 = closed.
char opf_dir[k_epub_max_path_len]
Directory portion of the OPF.
char embedded_font_paths[k_epub_max_fonts][k_epub_max_path_len]
Font hrefs (rel.
const uint8_t * font_data
TTF blob; NULL if unset.
One <manifest> <item> entry, retained in OPF document order.
One titled entry in the table of contents.
char href[k_epub_max_path_len]
Target href (rel.