41typedef enum : uint8_t {
43 k_v2_image_default_w_px = 64U,
44 k_v2_image_default_h_px = 64U,
45 k_v2_pt_to_px_num = 4U,
46 k_v2_pt_to_px_den = 3U,
49 k_v2_descent_den = 5U,
50 k_v2_xheight_den = 2U,
51 k_v2_media_resolution_dpi = 96U,
52 k_v2_html_wrap_reserve = 64U,
55class v2_container :
public litehtml::document_container {
57 v2_container(
int viewport_w,
int viewport_h,
int font_px)
58 : viewport_w_(viewport_w), viewport_h_(viewport_h), font_px_(font_px)
61 litehtml::uint_ptr create_font(
const litehtml::font_description& ,
62 const litehtml::document* ,
63 litehtml::font_metrics* fm)
override
66 fm->font_size =
static_cast<litehtml::pixel_t
>(font_px_);
67 fm->height =
static_cast<litehtml::pixel_t
>(font_px_);
68 fm->ascent = (
static_cast<litehtml::pixel_t
>(font_px_) *
69 static_cast<litehtml::pixel_t
>(k_v2_ascent_num)) /
70 static_cast<litehtml::pixel_t
>(k_v2_ascent_den);
72 static_cast<litehtml::pixel_t
>(font_px_) /
static_cast<litehtml::pixel_t
>(k_v2_descent_den);
74 static_cast<litehtml::pixel_t
>(font_px_) /
static_cast<litehtml::pixel_t
>(k_v2_xheight_den);
75 fm->ch_width =
static_cast<int>(k_v2_glyph_w_px);
76 fm->draw_spaces =
true;
80 void delete_font(litehtml::uint_ptr )
override {}
81 litehtml::pixel_t text_width(
const char* t, litehtml::uint_ptr )
override
83 return t ?
static_cast<litehtml::pixel_t
>(std::strlen(t)) *
84 static_cast<litehtml::pixel_t
>(k_v2_glyph_w_px)
87 void draw_text(litehtml::uint_ptr ,
91 const litehtml::position& )
override
93 litehtml::pixel_t pt_to_px(
float pt)
const override
95 return static_cast<litehtml::pixel_t
>((pt *
static_cast<float>(k_v2_pt_to_px_num)) /
96 static_cast<float>(k_v2_pt_to_px_den));
98 litehtml::pixel_t get_default_font_size()
const override
100 return static_cast<litehtml::pixel_t
>(font_px_);
102 const char* get_default_font_name()
const override
106 void draw_list_marker(litehtml::uint_ptr ,
107 const litehtml::list_marker& )
override
109 void load_image(
const char* ,
const char* ,
bool )
override
111 void get_image_size(
const char* ,
const char* , litehtml::size& sz)
override
113 sz.width =
static_cast<int>(k_v2_image_default_w_px);
114 sz.height =
static_cast<int>(k_v2_image_default_h_px);
116 void draw_image(litehtml::uint_ptr ,
117 const litehtml::background_layer& ,
119 const std::string& )
override
121 void draw_solid_fill(litehtml::uint_ptr ,
122 const litehtml::background_layer& ,
123 const litehtml::web_color& )
override
126 draw_linear_gradient(litehtml::uint_ptr ,
127 const litehtml::background_layer& ,
128 const litehtml::background_layer::linear_gradient& )
override
131 draw_radial_gradient(litehtml::uint_ptr ,
132 const litehtml::background_layer& ,
133 const litehtml::background_layer::radial_gradient& )
override
135 void draw_conic_gradient(litehtml::uint_ptr ,
136 const litehtml::background_layer& ,
137 const litehtml::background_layer::conic_gradient& )
override
139 void draw_borders(litehtml::uint_ptr ,
140 const litehtml::borders& ,
141 const litehtml::position& ,
144 void set_caption(
const char* )
override {}
145 void set_base_url(
const char* )
override {}
146 void link(
const std::shared_ptr<litehtml::document>& ,
147 const litehtml::element::ptr& )
override
149 void on_anchor_click(
const char* ,
const litehtml::element::ptr& )
override {}
150 void on_mouse_event(
const litehtml::element::ptr& ,
151 litehtml::mouse_event )
override
153 void set_cursor(
const char* )
override {}
154 void transform_text(litehtml::string& , litehtml::text_transform )
override {}
155 void import_css(litehtml::string& t,
156 const litehtml::string& ,
157 litehtml::string& )
override
161 void set_clip(
const litehtml::position& ,
162 const litehtml::border_radiuses& )
override
164 void del_clip()
override {}
165 void get_viewport(litehtml::position& v)
const override
169 v.width =
static_cast<litehtml::pixel_t
>(viewport_w_);
170 v.height =
static_cast<litehtml::pixel_t
>(viewport_h_);
172 litehtml::element::ptr create_element(
const char* ,
173 const litehtml::string_map& ,
174 const std::shared_ptr<litehtml::document>& )
override
178 void get_media_features(litehtml::media_features& m)
const override
180 m = litehtml::media_features{};
181 m.type = litehtml::media_type_screen;
182 m.width =
static_cast<litehtml::pixel_t
>(viewport_w_);
183 m.height =
static_cast<litehtml::pixel_t
>(viewport_h_);
184 m.device_width =
static_cast<litehtml::pixel_t
>(viewport_w_);
185 m.device_height =
static_cast<litehtml::pixel_t
>(viewport_h_);
187 m.resolution =
static_cast<int>(k_v2_media_resolution_dpi);
189 void get_language(litehtml::string& l, litehtml::string& c)
const override
207 litehtml::document::ptr document;
208 std::unique_ptr<v2_container> container;
226RA8_INTERNAL static inline void internal_v2_state_clear(v2_state& s)
232typedef enum : uint8_t {
233 k_v2_max_engines = 4U
236struct v2_cache_slot {
237 const reflow_t* engine =
nullptr;
241v2_cache_slot s_v2_cache[k_v2_max_engines];
260 for (uint8_t i = 0; i < (uint8_t)k_v2_max_engines; ++i) {
261 if (s_v2_cache[i].engine == engine) {
262 return &s_v2_cache[i].state;
268 for (uint8_t i = 0; i < (uint8_t)k_v2_max_engines; ++i) {
269 if (s_v2_cache[i].engine ==
nullptr) {
270 s_v2_cache[i].engine = engine;
271 internal_v2_state_clear(s_v2_cache[i].state);
272 return &s_v2_cache[i].state;
291 for (uint8_t i = 0; i < (uint8_t)k_v2_max_engines; ++i) {
292 if (s_v2_cache[i].engine == engine) {
293 internal_v2_state_clear(s_v2_cache[i].state);
294 s_v2_cache[i].engine =
nullptr;
316 if (engine ==
nullptr) {
319 if (engine->
in_use != 1U) {
344 state->document.reset();
345 state->container.reset();
347 state->container = std::make_unique<v2_container>(
static_cast<int>(engine->
viewport_w),
349 static_cast<int>(engine->
font_px));
351 const std::string body(
reinterpret_cast<const char*
>(engine->
xhtml_buf), engine->
xhtml_len);
353 html.reserve(body.size() + k_v2_html_wrap_reserve);
354 html.append(
"<!DOCTYPE html><html><head></head><body>");
356 html.append(
"</body></html>");
358 const litehtml::estring src(html);
359 state->document = litehtml::document::createFromString(src, state->container.get());
378 const int vh =
static_cast<int>(engine->
viewport_h);
379 int pages = (content_height + vh - 1) / vh;
386 engine->
page_count =
static_cast<uint32_t
>(pages);
387 for (
int i = 0; i < pages; ++i) {
410 v2_state* state = internal_v2_state_for(engine,
true);
411 if (state ==
nullptr) {
414 const ra8_err_t err = internal_v2_build_document(state, engine);
419 (void)state->document->render(
static_cast<litehtml::pixel_t
>(engine->
viewport_w));
420 int content_height =
static_cast<int>(state->document->height());
421 if (content_height < 0) {
425 internal_v2_paginate(engine, content_height);
426 if (out_total_pages !=
nullptr) {
439 const uint8_t* font_data,
446 if ((font_data ==
nullptr) || (out_engine ==
nullptr)) {
451 (void)std::memset(out_engine, 0,
sizeof(*out_engine));
454 if (font_len < 16U) {
455 (void)std::memset(out_engine, 0,
sizeof(*out_engine));
458 (void)std::memset(out_engine, 0,
sizeof(*out_engine));
472 if (engine ==
nullptr) {
475 if (engine->
in_use != 1U) {
478 internal_v2_state_drop(engine);
485 const uint8_t* xhtml_buf,
487 uint32_t* out_total_pages)
489 const ra8_err_t err = internal_check_engine(engine);
493 if ((xhtml_buf ==
nullptr) || (out_total_pages ==
nullptr)) {
496 if (xhtml_len == 0U) {
501 return internal_v2_run_layout(engine, out_total_pages);
508 const ra8_err_t err = internal_check_engine(engine);
520 const ra8_err_t err = internal_check_engine(engine);
524 if (out_count ==
nullptr) {
533 const ra8_err_t err = internal_check_engine(engine);
546 return internal_v2_run_layout(engine, &total);
552 if (engine ==
nullptr) {
562 if (engine ==
nullptr) {
566 return internal_v2_run_layout(engine, &total);
Annotation-attribute framework macros for ra8-firmware.
#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_invalid_state
Module in wrong state for requested operation.
@ 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_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.
HTML / CSS reflow + paginate engine for the ra8d2 ereader.
@ k_reflow_max_pages
Max paginated pages.
@ k_reflow_min_font_px
Smallest accepted font size.
@ k_reflow_max_font_px
Largest accepted font size.
ra8_err_t reflow_render_page(const reflow_t *engine, uint32_t page_idx, void *framebuffer)
Render one page into the active ra8_gfx framebuffer.
ra8_err_t reflow_run_layout(reflow_t *engine)
Run the line-break + page-break pass over engine->tokens[].
ra8_err_t reflow_layout_chapter(reflow_t *engine, const uint8_t *xhtml_buf, size_t xhtml_len, uint32_t *out_total_pages)
Parse + lay out one chapter of XHTML.
ra8_err_t reflow_parse_xhtml(reflow_t *engine, const uint8_t *xhtml_buf, size_t xhtml_len)
Parse the XHTML buffer into the engine's token stream.
ra8_err_t reflow_set_font_size(reflow_t *engine, uint16_t new_font_px)
Change the body font size and re-flow the cached chapter.
ra8_err_t reflow_init(uint16_t viewport_w, uint16_t viewport_h, const uint8_t *font_data, size_t font_len, uint16_t font_px, uint32_t body_color, uint32_t link_color, reflow_t *out_engine)
Initialise a reflow engine for a given viewport / font.
ra8_err_t reflow_get_page_count(const reflow_t *engine, uint32_t *out_count)
Report the laid-out page count.
ra8_err_t reflow_close(reflow_t *engine)
Release a previously initialized engine.
uint32_t glyph_count
Number of glyphs in this page.
uint32_t glyph_first
Index of first glyph in this page.
Reflow / pagination engine state.
uint32_t page_count
Pages used.
const uint8_t * font_data
TTF blob; outlives the engine.
uint32_t link_color
Anchor text colour (0xRRGGBB).
reflow_page_t pages[k_reflow_max_pages]
Page index ranges.
uint8_t in_use
1 = initialized, 0 = closed.
size_t font_len
Length of font_data, bytes.
uint16_t font_px
Body font size in pixels.
uint16_t viewport_w
Viewport width, pixels.
const uint8_t * xhtml_buf
Last layout_chapter input.
uint16_t viewport_h
Viewport height, pixels.
size_t xhtml_len
Length of xhtml_buf.
uint32_t body_color
Body text colour (0xRRGGBB).