maug
Quick and dirty C mini-augmentation library.
Loading...
Searching...
No Matches
retrohtr.h
Go to the documentation of this file.
1
2#ifndef RETROHTR_H
3#define RETROHTR_H
4
10
11#define RETROHTR_TREE_FLAG_GUI_ACTIVE 1
12
13#define RETROHTR_NODE_FLAG_DIRTY 2
14
15#ifndef RETROHTR_RENDER_NODES_INIT_SZ
16# define RETROHTR_RENDER_NODES_INIT_SZ 10
17#endif /* !RETROHTR_RENDER_NODES_INIT_SZ */
18
19#ifndef RETROHTR_TRACE_LVL
20# define RETROHTR_TRACE_LVL 0
21#endif /* !RETROHTR_TRACE_LVL */
22
23#define RETROHTR_EDGE_UNKNOWN 0
24#define RETROHTR_EDGE_LEFT 1
25#define RETROHTR_EDGE_TOP 2
26#define RETROHTR_EDGE_INSIDE 4
27
29 uint8_t flags;
30 /* TODO: Maybe get rid of these and replace them with MCSS_STYLE node? */
31 ssize_t x;
32 ssize_t y;
33 size_t w;
34 size_t h;
35 size_t m_l;
36 size_t m_r;
37 size_t m_t;
38 size_t m_b;
39 uint8_t pos;
40 uint8_t pos_flags;
41 uint8_t edge;
44#ifdef RETROGXC_PRESENT
45 ssize_t font_idx;
46#else
47 MAUG_MHANDLE font_h;
48#endif /* RETROGXC_PRESENT */
49 ssize_t tag;
51 ssize_t parent;
53 ssize_t first_child;
55 ssize_t next_sibling;
56 struct RETROFLAT_BITMAP bitmap;
57};
58
60 uint8_t flags;
61 MAUG_MHANDLE nodes_h;
65 size_t nodes_sz;
68 struct RETROGUI gui;
69};
70
71/* TODO: Function names should be verb_noun! */
72
73#define retrohtr_node( tree, idx ) \
74 (0 <= (ssize_t)idx ? &((tree)->nodes[idx]) : NULL)
75
76#define retrohtr_node_parent( tree, idx ) \
77 (0 <= idx && 0 <= (tree)->nodes[idx].parent ? \
78 &((tree)->nodes[(tree)->nodes[idx].parent]) : NULL)
79
80#define retrohtr_tree_lock( tree ) \
81 if( NULL == (tree)->nodes ) { \
82 maug_mlock( (tree)->nodes_h, (tree)->nodes ); \
83 maug_cleanup_if_null_alloc( struct RETROHTR_RENDER_NODE*, (tree)->nodes ); \
84 }
85
86#define retrohtr_tree_unlock( tree ) \
87 if( NULL != (tree)->nodes ) { \
88 maug_munlock( (tree)->nodes_h, (tree)->nodes ); \
89 }
90
91#define retrohtr_tree_is_locked( tree ) (NULL != (tree)->nodes)
92
93/* TODO: Make these offset by element scroll on screen. */
94
95#define retrohtr_node_screen_x( tree, node_idx ) \
96 ((tree)->nodes[node_idx].x)
97
98#define retrohtr_node_screen_y( tree, node_idx ) \
99 ((tree)->nodes[node_idx].y)
100
101MERROR_RETVAL retrohtr_tree_create(
102 struct MHTML_PARSER* parser, struct RETROHTR_RENDER_TREE* tree,
103 size_t x, size_t y, size_t w, size_t h,
104 ssize_t tag_idx, ssize_t node_idx, size_t d );
105
118 struct MHTML_PARSER* parser, struct RETROHTR_RENDER_TREE* tree,
119 struct MCSS_STYLE* parent_style, struct MCSS_STYLE* effect_style,
120 ssize_t tag_idx );
121
122MERROR_RETVAL retrohtr_tree_size(
123 struct MHTML_PARSER* parser, struct RETROHTR_RENDER_TREE* tree,
124 struct MCSS_STYLE* prev_sibling_style,
125 struct MCSS_STYLE* parent_style, ssize_t node_idx, size_t d );
126
127MERROR_RETVAL retrohtr_tree_pos(
128 struct MHTML_PARSER* parser, struct RETROHTR_RENDER_TREE* tree,
129 struct MCSS_STYLE* prev_sibling_style,
130 struct MCSS_STYLE* parent_style, ssize_t node_idx, size_t d );
131
132MERROR_RETVAL retrohtr_tree_draw(
133 struct MHTML_PARSER* parser, struct RETROHTR_RENDER_TREE* tree,
134 ssize_t node_idx, size_t d );
135
136retrogui_idc_t retrohtr_tree_poll_ctls(
137 struct RETROHTR_RENDER_TREE* tree,
138 RETROFLAT_IN_KEY* input,
139 struct RETROFLAT_INPUT* input_evt );
140
141MERROR_RETVAL retrohtr_tree_dump(
142 struct RETROHTR_RENDER_TREE* tree, struct MHTML_PARSER* parser,
143 ssize_t iter, size_t d );
144
145void retrohtr_tree_free( struct RETROHTR_RENDER_TREE* tree );
146
147MERROR_RETVAL retrohtr_tree_init( struct RETROHTR_RENDER_TREE* tree );
148
149#ifdef RETROHTR_C
150
151ssize_t retrohtr_get_next_free_node( struct RETROHTR_RENDER_TREE* tree ) {
152 uint8_t auto_unlocked = 0;
153 ssize_t retidx = -1;
154 MAUG_MHANDLE new_nodes_h = (MAUG_MHANDLE)NULL;
155
156 if( NULL != tree->nodes ) {
157 debug_printf( RETROHTR_TRACE_LVL, "auto-unlocking nodes..." );
158 maug_munlock( tree->nodes_h, tree->nodes );
159 auto_unlocked = 1;
160 }
161
162 assert( 0 < tree->nodes_sz_max );
163 assert( NULL == tree->nodes );
164 assert( (MAUG_MHANDLE)NULL != tree->nodes_h );
165 if( tree->nodes_sz_max <= tree->nodes_sz + 1 ) {
166 /* We've run out of nodes, so double the available number. */
167 /* TODO: Check for sz overflow. */
168 new_nodes_h = maug_mrealloc( tree->nodes_h, tree->nodes_sz_max * 2,
169 sizeof( struct RETROHTR_RENDER_NODE ) );
170 if( (MAUG_MHANDLE)NULL == new_nodes_h ) {
171 error_printf(
172 "unable to reallocate " SIZE_T_FMT " nodes!",
173 tree->nodes_sz_max * 2 );
174 goto cleanup;
175 }
176 tree->nodes_h = new_nodes_h;
177 tree->nodes_sz_max *= 2;
178 }
179
180 /* Assume handle is unlocked. */
181 assert( NULL == tree->nodes );
182 maug_mlock( tree->nodes_h, tree->nodes );
183 if( NULL == tree->nodes ) {
184 error_printf( "unable to lock nodes!" );
185 goto cleanup;
186 }
187
188 /* Zero out the last node, add it to the list, and return its index. */
189 debug_printf( RETROHTR_TRACE_LVL,
190 "zeroing node " SIZE_T_FMT " (of " SIZE_T_FMT ")...",
191 tree->nodes_sz, tree->nodes_sz_max );
192 maug_mzero( &(tree->nodes[tree->nodes_sz]),
193 sizeof( struct RETROHTR_RENDER_NODE ) );
194 retidx = tree->nodes_sz;
195 tree->nodes_sz++;
196
197 /* Compensate for cleanup below. */
198 maug_munlock( tree->nodes_h, tree->nodes );
199
200cleanup:
201
202 if( auto_unlocked ) {
203 debug_printf( RETROHTR_TRACE_LVL, "auto-locking nodes..." );
204 maug_mlock( tree->nodes_h, tree->nodes );
205 }
206
207 return retidx;
208}
209
210ssize_t retrohtr_add_node_child(
211 struct RETROHTR_RENDER_TREE* tree, ssize_t node_parent_idx
212) {
213 ssize_t node_new_idx = -1,
214 node_sibling_idx = -1;
215
216 node_new_idx = retrohtr_get_next_free_node( tree );
217 if( 0 > node_new_idx ) {
218 goto cleanup;
219 }
220
221#ifdef RETROGXC_PRESENT
222 retrohtr_node( tree, node_new_idx )->font_idx = -1;
223#endif /* RETROGXC_PRESENT */
224 retrohtr_node( tree, node_new_idx )->parent = node_parent_idx;
225 retrohtr_node( tree, node_new_idx )->first_child = -1;
226 retrohtr_node( tree, node_new_idx )->next_sibling = -1;
227
228 if( 0 > node_parent_idx ) {
229 debug_printf(
230 1, "adding root node under " SSIZE_T_FMT "...", node_parent_idx );
231 goto cleanup;
232 } else {
233 debug_printf(
234 1, "adding node " SSIZE_T_FMT " under " SSIZE_T_FMT,
235 node_new_idx, node_parent_idx );
236 }
237
238 /* Add new child under current node. */
239 if( 0 > retrohtr_node( tree, node_parent_idx )->first_child ) {
240 debug_printf( RETROHTR_TRACE_LVL, "adding first child..." );
241 assert( -1 == retrohtr_node( tree, node_parent_idx )->first_child );
242 retrohtr_node( tree, node_parent_idx )->first_child = node_new_idx;
243 } else {
244 assert( NULL != retrohtr_node( tree, node_parent_idx ) );
245 node_sibling_idx = retrohtr_node( tree, node_parent_idx )->first_child;
246 assert( NULL != retrohtr_node( tree, node_sibling_idx ) );
247 while( 0 <= retrohtr_node( tree, node_sibling_idx )->next_sibling ) {
248 node_sibling_idx =
249 retrohtr_node( tree, node_sibling_idx )->next_sibling;
250 }
251 retrohtr_node( tree, node_sibling_idx )->next_sibling = node_new_idx;
252 }
253
254cleanup:
255
256 return node_new_idx;
257}
258
259MERROR_RETVAL retrohtr_tree_create(
260 struct MHTML_PARSER* parser, struct RETROHTR_RENDER_TREE* tree,
261 size_t x, size_t y, size_t w, size_t h,
262 ssize_t tag_idx, ssize_t node_idx, size_t d
263) {
264 ssize_t node_new_idx = -1;
265 ssize_t tag_iter_idx = -1;
266 MERROR_RETVAL retval = MERROR_OK;
267 union MHTML_TAG* p_tag_iter = NULL;
268 ssize_t tag_next_idx = 0;
269
270 debug_printf( RETROHTR_TRACE_LVL,
271 "creating render node for tag: " SSIZE_T_FMT, tag_idx );
272
273 mdata_vector_lock( &(parser->tags) );
274
275 if( 0 > tag_idx ) {
276 goto cleanup;
277 }
278 p_tag_iter = mdata_vector_get( &(parser->tags), tag_idx, union MHTML_TAG );
279 if( NULL == p_tag_iter ) {
280 goto cleanup;
281 }
282
283 /* Make sure we have a single root node. */
284 if( 0 > node_idx ) {
285 assert( MHTML_TAG_TYPE_BODY == p_tag_iter->base.type );
286
287 node_new_idx = retrohtr_add_node_child( tree, node_idx );
288 if( 0 > node_new_idx ) {
289 goto cleanup;
290 }
291 debug_printf( RETROHTR_TRACE_LVL,
292 "created initial root node: " SIZE_T_FMT, node_new_idx );
293
294 node_idx = node_new_idx;
295
296 /* The common root is the body tag. */
297 retrohtr_node( tree, node_idx )->tag = tag_idx;
298
299 retrohtr_node( tree, node_idx )->x = x;
300 retrohtr_node( tree, node_idx )->y = y;
301 retrohtr_node( tree, node_idx )->w = w;
302 retrohtr_node( tree, node_idx )->h = h;
303 }
304
305 tag_iter_idx = p_tag_iter->base.first_child;
306 while( 0 <= tag_iter_idx ) {
307 node_new_idx = retrohtr_add_node_child( tree, node_idx );
308 p_tag_iter = mdata_vector_get(
309 &(parser->tags), tag_iter_idx, union MHTML_TAG );
310 assert( NULL != p_tag_iter );
311 if( 0 > node_new_idx ) {
312 goto cleanup;
313 }
314
315 retrohtr_node( tree, node_new_idx )->tag = tag_iter_idx;
316
317 debug_printf( RETROHTR_TRACE_LVL,
318 "rendering node " SSIZE_T_FMT " (%s) under node " SSIZE_T_FMT,
319 node_new_idx,
320 gc_mhtml_tag_names[p_tag_iter->base.type],
321 node_idx );
322
323 /* Tag-specific rendering preparations. */
324 if( MHTML_TAG_TYPE_IMG == p_tag_iter->base.type ) {
325 /* Load the image for rendering later. */
326 retval = retroflat_load_bitmap(
327 p_tag_iter->IMG.src,
328 &(retrohtr_node( tree, node_new_idx )->bitmap),
330 if( MERROR_OK == retval ) {
331 debug_printf( RETROHTR_TRACE_LVL, "loaded img: %s",
332 p_tag_iter->IMG.src );
333 } else {
334 error_printf( "could not load img: %s", p_tag_iter->IMG.src );
335 }
336 }
337
338 tag_next_idx = p_tag_iter->base.next_sibling;
339
340 mdata_vector_unlock( &(parser->tags) );
341
342 retval = retrohtr_tree_create( parser, tree, x, y, w, h,
343 tag_iter_idx, node_new_idx, d + 1 );
344 maug_cleanup_if_not_ok();
345
346 mdata_vector_lock( &(parser->tags) );
347
348 tag_iter_idx = tag_next_idx;
349 }
350
351cleanup:
352
353 if( mdata_vector_is_locked( &(parser->tags) ) ) {
354 mdata_vector_unlock( &(parser->tags) );
355 }
356
357 return retval;
358}
359
361 struct MHTML_PARSER* parser, struct RETROHTR_RENDER_TREE* tree,
362 struct MCSS_STYLE* parent_style, struct MCSS_STYLE* effect_style,
363 ssize_t tag_idx
364) {
365 MERROR_RETVAL retval = MERROR_OK;
366 ssize_t tag_style_idx = -1;
367 size_t tag_type = 0,
368 i = 0;
369 struct MCSS_STYLE* style = NULL;
370 union MHTML_TAG* p_tag_iter = NULL;
371
372 debug_printf( RETROHTR_TRACE_LVL,
373 "applying styles for tag: " SSIZE_T_FMT, tag_idx );
374
375 assert( !mdata_vector_is_locked( &(parser->tags) ) );
376 mdata_vector_lock( &(parser->styler.styles) );
377 mdata_vector_lock( &(parser->tags) );
378
379 maug_mzero( effect_style, sizeof( struct MCSS_STYLE ) );
380
381 if( 0 >= tag_idx ) {
382 goto cleanup;
383 }
384 p_tag_iter = mdata_vector_get( &(parser->tags), tag_idx, union MHTML_TAG );
385 if( NULL == p_tag_iter ) {
386 goto cleanup;
387 }
388
389 tag_type = p_tag_iter->base.type;
390
391 /* Merge style based on HTML element class. */
392 if( 0 < p_tag_iter->base.classes_sz ) {
393 for( i = 0 ; mdata_vector_ct( &(parser->styler.styles) ) > i ; i++ ) {
394 style = mdata_vector_get(
395 &(parser->styler.styles), i, struct MCSS_STYLE );
396
397 if(
398 NULL != style &&
399 0 == strncmp(
400 p_tag_iter->base.classes,
401 style->class,
402 p_tag_iter->base.classes_sz
403 )
404 ) {
405 debug_printf( RETROHTR_TRACE_LVL, "found style for tag class: %s",
406 style->class );
407
408 mcssmerge_styles( effect_style, parent_style, style, tag_type );
409 }
410 }
411 }
412
413 /* Merge style based on HTML element ID. */
414 if( 0 < p_tag_iter->base.id_sz ) {
415 for( i = 0 ; mdata_vector_ct( &(parser->styler.styles) ) > i ; i++ ) {
416 style = mdata_vector_get(
417 &(parser->styler.styles), i, struct MCSS_STYLE );
418
419 if(
420 NULL != style &&
421 0 == strncmp(
422 p_tag_iter->base.id,
423 style->id,
424 p_tag_iter->base.id_sz
425 )
426 ) {
427 debug_printf( RETROHTR_TRACE_LVL, "found style for tag ID: %s",
428 style->id );
429
430 mcssmerge_styles( effect_style, parent_style, style, tag_type );
431 }
432 }
433 }
434
435 /* Grab element-specific style last. */
436 tag_style_idx = p_tag_iter->base.style;
437
438cleanup:
439
440 /* TODO: Separate this out of cleanup phase. */
441
442 /* This might be NULL! */
443 style = mdata_vector_get(
444 &(parser->styler.styles), tag_style_idx, struct MCSS_STYLE );
445
446 /* Make sure we have a root style. */
447 mcssmerge_styles( effect_style, parent_style, style, tag_type );
448
449 mdata_vector_unlock( &(parser->tags) );
450 mdata_vector_unlock( &(parser->styler.styles) );
451
452 return retval;
453}
454
455static MERROR_RETVAL retrohtr_load_font(
456 struct MCSS_PARSER* styler,
457#ifdef RETROGXC_PRESENT
458 ssize_t* font_idx_p,
459#else
460 MAUG_MHANDLE* font_h_p,
461#endif /* RETROGXC_PRESENT */
462 struct MCSS_STYLE* effect_style
463) {
464 MERROR_RETVAL retval = MERROR_OK;
465
466#ifdef RETROGXC_PRESENT
467 if( 0 <= *font_idx_p ) {
468 error_printf( "tried to load font but font already loaded, idx: "
469 SSIZE_T_FMT, *font_idx_p );
470#else
471 if( (MAUG_MHANDLE)NULL != *font_h_p ) {
472 error_printf( "tried to load font but font already loaded, p: %p",
473 *font_h_p );
474#endif /* RETROGXC_PRESENT */
475 goto cleanup;
476 }
477
478 mdata_strpool_lock( &(styler->strpool) );
479
480 debug_printf( RETROHTR_TRACE_LVL,
481 "loading font: %s (" SSIZE_T_FMT ")",
482 mdata_strpool_get( &(styler->strpool), effect_style->FONT_FAMILY ),
483 effect_style->FONT_FAMILY );
484
485 if( 0 >= effect_style->FONT_FAMILY ) {
486 error_printf( "style has no font associated!" );
487 /* TODO: Load fallback font? */
488 retval = MERROR_GUI;
489 goto cleanup;
490 }
491
492 /* Load the font into the cache. */
493#ifdef RETROGXC_PRESENT
494 *font_idx_p =
495 retrogxc_load_font(
496 mdata_strpool_get( &(styler->strpool), effect_style->FONT_FAMILY ),
497 0, 33, 93 );
498#else
499 retval = retrofont_load(
500 mdata_strpool_get( &(styler->strpool), effect_style->FONT_FAMILY ),
501 font_h_p, 0, 33, 93 );
502#endif /* RETROGXC_PRESENT */
503
504cleanup:
505
506 mdata_strpool_unlock( &(styler->strpool) );
507
508 return retval;
509}
510
511MERROR_RETVAL retrohtr_tree_gui(
512 struct RETROHTR_RENDER_TREE* tree, struct MCSS_PARSER* styler,
513 struct MCSS_STYLE* effect_style
514) {
515 MERROR_RETVAL retval = MERROR_OK;
516
517 /* Create a GUI handler just for this tree. */
518 if(
519 RETROHTR_TREE_FLAG_GUI_ACTIVE ==
520 (RETROHTR_TREE_FLAG_GUI_ACTIVE & tree->flags)
521 ) {
522 debug_printf( RETROHTR_TRACE_LVL, "tree GUI already active!" );
523 goto cleanup;
524 }
525
526 /* This means all GUI items will use the font from the first node
527 * loaded with a GUI item!
528 */
529 retval = retrogui_init( &(tree->gui) );
530 maug_cleanup_if_not_ok();
531
532 retval = retrohtr_load_font(
533 styler,
534#ifdef RETROGXC_PRESENT
535 &(tree->gui.font_idx),
536#else
537 &(tree->gui.font_h),
538#endif /* RETROGXC_PRESENT */
539 effect_style );
540 maug_cleanup_if_not_ok();
541
542 tree->flags |= RETROHTR_TREE_FLAG_GUI_ACTIVE;
543
544 debug_printf( RETROHTR_TRACE_LVL, "tree GUI initialized!" );
545
546cleanup:
547 return retval;
548}
549
550MERROR_RETVAL retrohtr_tree_size(
551 struct MHTML_PARSER* parser, struct RETROHTR_RENDER_TREE* tree,
552 struct MCSS_STYLE* prev_sibling_style,
553 struct MCSS_STYLE* parent_style, ssize_t node_idx, size_t d
554) {
555 struct MCSS_STYLE effect_style;
556 struct MCSS_STYLE child_prev_sibling_style;
557 struct MCSS_STYLE child_style;
558 ssize_t child_iter_idx = -1;
559 ssize_t tag_idx = -1;
560 ssize_t node_iter_idx = -1;
561 size_t this_line_w = 0;
562 size_t this_line_h = 0;
563 MERROR_RETVAL retval = MERROR_OK;
564 union RETROGUI_CTL ctl;
565 union MHTML_TAG* p_tag_iter = NULL;
566 union MHTML_TAG* p_tag_node = NULL;
567
568 if( NULL == retrohtr_node( tree, node_idx ) ) {
569 goto cleanup;
570 }
571
572 tag_idx = retrohtr_node( tree, node_idx )->tag;
573
574 retval = retrohtr_apply_styles(
575 parser, tree, parent_style, &effect_style, tag_idx );
576 maug_cleanup_if_not_ok();
577
578 assert( !mdata_vector_is_locked( &(parser->tags) ) );
579 mdata_vector_lock( &(parser->tags) );
580
581 p_tag_iter = mdata_vector_get( &(parser->tags), tag_idx, union MHTML_TAG );
582 assert( NULL != p_tag_iter );
583
584 /* position */
585
586 if( mcss_prop_is_active( effect_style.POSITION ) ) {
587 debug_printf( RETROHTR_TRACE_LVL,
588 "node " SSIZE_T_FMT ": applying %s positioning",
589 node_idx, gc_mcss_position_names[effect_style.POSITION] );
590 /* TODO: MCSS_POS_NOTE: We'd like to get rid of this so all positioning
591 * is done through CSS... unfortunately, we only track the current
592 * and previous effective styles while working that out later, so
593 * we need to pin this to the element directly so we can rule it
594 * out of the box model e.g. when determining x/y coords of its
595 * neighbors.
596 */
597 retrohtr_node( tree, node_idx )->pos = effect_style.POSITION;
598 retrohtr_node( tree, node_idx )->pos_flags = effect_style.POSITION_flags;
599 }
600
601 /* Grab fixed dimensions before content-based calculations of children, so
602 * we know if there are constraints. If these aren't set, then we'll size
603 * based on childrens' sizes after we determine childrens' sizes below.
604 */
605
606 if( mcss_prop_is_active_NOT_flag( effect_style.WIDTH, AUTO ) ) {
607 retrohtr_node( tree, node_idx )->w = effect_style.WIDTH;
608 }
609
610 if( mcss_prop_is_active_NOT_flag( effect_style.HEIGHT, AUTO ) ) {
611 retrohtr_node( tree, node_idx )->h = effect_style.HEIGHT;
612 }
613
614 /* Figure out how big the contents of this node are. */
615
616 /* Font is heritable, so load it for all nodes even if we don't use it. */
617 retval = retrohtr_load_font(
618 &(parser->styler),
619#ifdef RETROGXC_PRESENT
620 &(retrohtr_node( tree, node_idx )->font_idx),
621#else
622 &(retrohtr_node( tree, node_idx )->font_h),
623#endif /* RETROGXC_PRESENT */
624 &effect_style );
625 maug_cleanup_if_not_ok();
626
627 if( 0 <= tag_idx && MHTML_TAG_TYPE_TEXT == p_tag_iter->base.type ) {
628 /* Get text size to use in calculations below. */
629
630 mdata_strpool_lock( &(parser->strpool) );
631
632#ifdef RETROGXC_PRESENT
633 retrogxc_string_sz(
634#else
635 retrofont_string_sz(
636#endif /* RETROGXC_PRESENT */
637 NULL,
638 mdata_strpool_get( &(parser->strpool), p_tag_iter->TEXT.content_idx ),
639 p_tag_iter->TEXT.content_sz,
640#ifdef RETROGXC_PRESENT
641 retrohtr_node( tree, node_idx )->font_idx,
642#else
643 retrohtr_node( tree, node_idx )->font_h,
644#endif /* RETROGXC_PRESENT */
645 /* Constrain node text size to parent size. */
646 retrohtr_node_parent( tree, node_idx )->w,
647 retrohtr_node_parent( tree, node_idx )->h,
648 &(retrohtr_node( tree, node_idx )->w),
649 &(retrohtr_node( tree, node_idx )->h), 0 );
650
651 debug_printf( RETROHTR_TRACE_LVL, "TEXT w: " SIZE_T_FMT,
652 retrohtr_node( tree, node_idx )->w );
653
654 mdata_strpool_unlock( &(parser->strpool) );
655
656 } else if(
657 0 <= tag_idx &&
658 MHTML_TAG_TYPE_INPUT == p_tag_iter->base.type
659 ) {
660 /* Push the control (for the client renderer to redraw later). */
661
662 retval = retrohtr_tree_gui( tree, &(parser->styler), &effect_style );
663
664 if(
665 /* Use the same ID for the node and control it creates. */
666 MERROR_OK != retrogui_init_ctl(
667 &ctl, RETROGUI_CTL_TYPE_BUTTON, node_idx )
668 ) {
669 error_printf( "could not initialize control!" );
670 goto cleanup;
671 }
672
673 p_tag_node = mdata_vector_get(
674 &(parser->tags),
675 retrohtr_node( tree, node_idx )->tag, union MHTML_TAG );
676
677 ctl.base.x = retrohtr_node( tree, node_idx )->x;
678 ctl.base.y = retrohtr_node( tree, node_idx )->y;
679 ctl.base.w = 0;
680 ctl.base.h = 0;
681 ctl.BUTTON.label = p_tag_node->INPUT.value;
682
683 /* Grab determined size back from control. */
684 retrohtr_node( tree, node_idx )->w = ctl.base.w;
685 retrohtr_node( tree, node_idx )->h = ctl.base.h;
686
687 debug_printf( RETROHTR_TRACE_LVL, "initialized control for INPUT..." );
688
689 retrogui_push_ctl( &(tree->gui), &ctl );
690
691 } else if( 0 <= tag_idx && MHTML_TAG_TYPE_IMG == p_tag_iter->base.type ) {
692
693 if( retroflat_bitmap_ok( &(retrohtr_node( tree, node_idx )->bitmap) ) ) {
694 retrohtr_node( tree, node_idx )->w =
695 retroflat_bitmap_w( &(retrohtr_node( tree, node_idx )->bitmap) );
696 retrohtr_node( tree, node_idx )->h =
697 retroflat_bitmap_h( &(retrohtr_node( tree, node_idx )->bitmap) );
698 }
699
700 debug_printf( RETROHTR_TRACE_LVL, "TEXT w: " SIZE_T_FMT,
701 retrohtr_node( tree, node_idx )->w );
702
703 } else {
704 /* Get sizing of child nodes. */
705
706 maug_mzero( &child_prev_sibling_style, sizeof( struct MCSS_STYLE ) );
707 node_iter_idx = retrohtr_node( tree, node_idx )->first_child;
708 mdata_vector_unlock( &(parser->tags) );
709 while( 0 <= node_iter_idx ) {
710 retrohtr_tree_size(
711 parser, tree, &child_prev_sibling_style, &effect_style,
712 node_iter_idx, d + 1 );
713
714 node_iter_idx = retrohtr_node( tree, node_iter_idx )->next_sibling;
715 }
716 }
717
718 if( mdata_vector_is_locked( &(parser->tags) ) ) {
719 mdata_vector_unlock( &(parser->tags) );
720 }
721
722 /* If our width is still zero, then size based on children. */
723 if( 0 == retrohtr_node( tree, node_idx )->w ) {
724 if(
725 MCSS_DISPLAY_BLOCK == effect_style.DISPLAY &&
726 0 <= retrohtr_node( tree, node_idx )->parent
727 ) {
728 /* Use parent width. */
729 /* TODO: Subtract parent padding! */
730 retrohtr_node( tree, node_idx )->w =
731 retrohtr_node_parent( tree, node_idx )->w;
732 }
733
734 /* Cycle through children and use greatest width. */
735 child_iter_idx = retrohtr_node( tree, node_idx )->first_child;
736 while( 0 <= child_iter_idx ) {
737 assert( !mdata_vector_is_locked( &(parser->tags) ) );
738 retval = retrohtr_apply_styles(
739 parser, tree, &effect_style, &child_style,
740 retrohtr_node( tree, child_iter_idx )->tag );
741 maug_cleanup_if_not_ok();
742
743 /* Skip ABSOLUTE nodes. */
744 if( MCSS_POSITION_ABSOLUTE == child_style.POSITION ) {
745 child_iter_idx =
746 retrohtr_node( tree, child_iter_idx )->next_sibling;
747 continue;
748 }
749
750 if( MCSS_DISPLAY_BLOCK == child_style.DISPLAY ) {
751 /* Reset the line width counter for coming BLOCK node. */
752 this_line_w = 0;
753
754 if(
755 retrohtr_node( tree, child_iter_idx )->w >
756 retrohtr_node( tree, node_idx )->w
757 ) {
758 /* This BLOCK node is the longest yet! */
759 retrohtr_node( tree, node_idx )->w =
760 retrohtr_node( tree, child_iter_idx )->w;
761 }
762 } else {
763 /* Add inline node to this node line's width. */
764 this_line_w += retrohtr_node( tree, child_iter_idx )->w;
765
766 if( this_line_w > retrohtr_node( tree, node_idx )->w ) {
767 /* The line of nodes we've been adding up is the longest yet! */
768 retrohtr_node( tree, node_idx )->w = this_line_w;
769 }
770 }
771 child_iter_idx = retrohtr_node( tree, child_iter_idx )->next_sibling;
772 }
773 }
774
775 /* If our height is still zero, then size based on children. */
776 if( 0 == retrohtr_node( tree, node_idx )->h ) {
777 /* Cycle through children and add heights. */
778 child_iter_idx = retrohtr_node( tree, node_idx )->first_child;
779 while( 0 <= child_iter_idx ) {
780 assert( !mdata_vector_is_locked( &(parser->tags) ) );
782 parser, tree, &effect_style, &child_style,
783 retrohtr_node( tree, child_iter_idx )->tag );
784
785 /* Skip ABSOLUTE nodes. */
786 if( MCSS_POSITION_ABSOLUTE == child_style.POSITION ) {
787 child_iter_idx = retrohtr_node( tree, child_iter_idx )->next_sibling;
788 continue;
789 }
790
791 if( MCSS_DISPLAY_BLOCK == child_style.DISPLAY ) {
792 /* Add the last line to the running height. */
793 retrohtr_node( tree, node_idx )->h += this_line_h;
794
795 /* Start a new running line height with this BLOCK node. */
796 this_line_h = retrohtr_node( tree, child_iter_idx )->h;
797 } else {
798 /* Make sure this line is at least as tall as this INLINE node. */
799 if( this_line_h < retrohtr_node( tree, child_iter_idx )->h ) {
800 this_line_h = retrohtr_node( tree, child_iter_idx )->h;
801 }
802 }
803
804 child_iter_idx = retrohtr_node( tree, child_iter_idx )->next_sibling;
805 }
806
807 /* Add the last line height the node height. */
808 retrohtr_node( tree, node_idx )->h += this_line_h;
809 this_line_h = 0;
810 }
811
812 /* Apply additional modifiers (padding, etc) after children have all been
813 * calculated.
814 */
815
816 /* Try specific left padding first, then try general padding. */
817 if( mcss_prop_is_active_NOT_flag( effect_style.PADDING_LEFT, AUTO ) ) {
818 retrohtr_node( tree, node_idx )->w += effect_style.PADDING_LEFT;
819 } else if( mcss_prop_is_active_NOT_flag( effect_style.PADDING, AUTO ) ) {
820 retrohtr_node( tree, node_idx )->w += effect_style.PADDING;
821 }
822
823 /* Try specific right padding first, then try general padding. */
824 if( mcss_prop_is_active_NOT_flag( effect_style.PADDING_RIGHT, AUTO ) ) {
825 retrohtr_node( tree, node_idx )->w += effect_style.PADDING_RIGHT;
826 } else if( mcss_prop_is_active_NOT_flag( effect_style.PADDING, AUTO ) ) {
827 retrohtr_node( tree, node_idx )->w += effect_style.PADDING;
828 }
829
830 /* Try specific top padding first, then try general padding. */
831 if( mcss_prop_is_active_NOT_flag( effect_style.PADDING_TOP, AUTO ) ) {
832 retrohtr_node( tree, node_idx )->h += effect_style.PADDING_TOP;
833 } else if( mcss_prop_is_active_NOT_flag( effect_style.PADDING, AUTO ) ) {
834 retrohtr_node( tree, node_idx )->h += effect_style.PADDING;
835 }
836
837 /* Try specific bottom padding first, then try general padding. */
838 if( mcss_prop_is_active_NOT_flag( effect_style.PADDING_BOTTOM, AUTO ) ) {
839 retrohtr_node( tree, node_idx )->h += effect_style.PADDING_BOTTOM;
840 } else if( mcss_prop_is_active_NOT_flag( effect_style.PADDING, AUTO ) ) {
841 retrohtr_node( tree, node_idx )->h += effect_style.PADDING;
842 }
843
844 debug_printf( RETROHTR_TRACE_LVL,
845 "setting node " SIZE_T_FMT " dirty...", node_idx );
846 retrohtr_node( tree, node_idx )->flags |= RETROHTR_NODE_FLAG_DIRTY;
847
848cleanup:
849
850 if( mdata_vector_is_locked( &(parser->tags) ) ) {
851 mdata_vector_unlock( &(parser->tags) );
852 }
853
854 /* We're done with the prev_sibling_style for this iter, so prepare it for
855 * the next called by the parent!
856 */
857 if( NULL != prev_sibling_style ) {
858 maug_mcpy(
859 prev_sibling_style, &effect_style,
860 sizeof( struct MCSS_STYLE ) );
861 }
862
863 return retval;
864}
865
866/* TODO: See MCSS_POS_NOTE. */
868#define retrohtr_break_on_active_pos( iter_idx ) \
869 if( mcss_prop_is_active( retrohtr_node( tree, iter_idx )->pos ) ) { \
870 break; \
871 }
872
873static ssize_t retrohtr_find_prev_sibling_in_box_model(
874 struct RETROHTR_RENDER_TREE* tree,
875 ssize_t node_idx
876) {
877 ssize_t sibling_iter_idx = -1;
878 ssize_t sibling_found_idx = -1;
879
880 if( 0 > retrohtr_node( tree, node_idx )->parent ) {
881 /* Can't determine sibling! */
882 goto cleanup;
883 }
884
885 sibling_iter_idx = retrohtr_node_parent( tree, node_idx )->first_child;
886
887 if( sibling_iter_idx == node_idx ) {
888 /* No previous siblings! */
889 goto cleanup;
890 }
891
892 while( 0 <= sibling_iter_idx && node_idx != sibling_iter_idx ) {
893 if(
894 /* TODO: See MCSS_POS_NOTE. This is what we were talking about. */
895 MCSS_POSITION_ABSOLUTE != retrohtr_node( tree, sibling_iter_idx )->pos
896 ) {
897 sibling_found_idx = sibling_iter_idx;
898 }
899
900 /* TODO: Reset on <br />? */
901
902 sibling_iter_idx = retrohtr_node( tree, sibling_iter_idx )->next_sibling;
903 }
904
905cleanup:
906 return sibling_found_idx;
907}
908
909static MERROR_RETVAL retrohtr_mark_edge_child_nodes(
910 struct MHTML_PARSER* parser, struct RETROHTR_RENDER_TREE* tree,
911 ssize_t node_parent_idx
912) {
913 ssize_t node_sibling_idx = -1;
914 MERROR_RETVAL retval = MERROR_OK;
915 struct MCSS_STYLE effect_style;
916 size_t col_idx = 0; /* How many nodes right (X)? */
917 size_t row_idx = 0; /* How many nodes down (Y)? */
918 union MHTML_TAG* p_tag_iter = NULL;
919
920 node_sibling_idx = retrohtr_node( tree, node_parent_idx )->first_child;
921 while( 0 <= node_sibling_idx ) {
922 maug_mzero( &effect_style, sizeof( struct MCSS_STYLE ) );
924 parser, tree, NULL, &effect_style,
925 retrohtr_node( tree, node_sibling_idx )->tag );
926
927 if( MCSS_POSITION_ABSOLUTE == effect_style.POSITION ) {
928 /* Absolute nodes are never on the edge. */
929 retrohtr_node( tree, node_sibling_idx )->edge |= RETROHTR_EDGE_INSIDE;
930
931 } else if( MCSS_DISPLAY_INLINE == effect_style.DISPLAY ) {
932 /* Inline, or something that follows previous column. */
933 if( 0 == col_idx ) {
934 retrohtr_node( tree, node_sibling_idx )->edge |= RETROHTR_EDGE_LEFT;
935 }
936 if( 0 == row_idx ) {
937 retrohtr_node( tree, node_sibling_idx )->edge |= RETROHTR_EDGE_TOP;
938 }
939 if( 0 < row_idx && 0 < col_idx ) {
940 retrohtr_node( tree, node_sibling_idx )->edge |= RETROHTR_EDGE_INSIDE;
941 }
942 col_idx++;
943
944 } else {
945 /* Block element will be on the next line, so take that into account
946 * when deciding the edge below.
947 */
948 row_idx++;
949 col_idx = 0;
950
951 /* Block, or something else in a new row. */
952 if( 0 == row_idx ) {
953 retrohtr_node( tree, node_sibling_idx )->edge |= RETROHTR_EDGE_TOP;
954 }
955
956 /* Assume block is always on a new line. */
957 retrohtr_node( tree, node_sibling_idx )->edge |= RETROHTR_EDGE_LEFT;
958 }
959
960 assert( !mdata_vector_is_locked( &(parser->tags) ) );
961 mdata_vector_lock( &(parser->tags) );
962
963 p_tag_iter = mdata_vector_get( &(parser->tags),
964 retrohtr_node( tree, node_sibling_idx )->tag,
965 union MHTML_TAG );
966 assert( NULL != p_tag_iter );
967
968 debug_printf( 1, "marking node " SIZE_T_FMT " (%s) edge: %u",
969 node_sibling_idx,
970 gc_mhtml_tag_names[p_tag_iter->base.type],
971 retrohtr_node( tree, node_sibling_idx )->edge );
972
973 mdata_vector_unlock( &(parser->tags) );
974
975 node_sibling_idx =
976 retrohtr_node( tree, node_sibling_idx )->next_sibling;
977 }
978
979cleanup:
980
981 if( mdata_vector_is_locked( &(parser->tags) ) ) {
982 mdata_vector_unlock( &(parser->tags) );
983 }
984
985 return retval;
986}
987
988MERROR_RETVAL retrohtr_tree_pos(
989 struct MHTML_PARSER* parser, struct RETROHTR_RENDER_TREE* tree,
990 struct MCSS_STYLE* prev_sibling_style,
991 struct MCSS_STYLE* parent_style, ssize_t node_idx, size_t d
992) {
993 struct MCSS_STYLE child_prev_sibling_style;
994 struct MCSS_STYLE effect_style;
995 ssize_t child_iter_idx = -1;
996 ssize_t tag_idx = -1;
997 ssize_t node_iter_idx = -1;
998 ssize_t prev_sibling_idx = -1;
999 MERROR_RETVAL retval = MERROR_OK;
1000 union MHTML_TAG* p_tag_iter = NULL;
1001
1002 if( NULL == retrohtr_node( tree, node_idx ) ) {
1003 goto cleanup;
1004 }
1005
1006 tag_idx = retrohtr_node( tree, node_idx )->tag;
1007
1009 parser, tree, parent_style, &effect_style, tag_idx );
1010
1011 prev_sibling_idx =
1012 retrohtr_find_prev_sibling_in_box_model( tree, node_idx );
1013
1014 /* x */
1015
1016 if( MCSS_POSITION_ABSOLUTE == effect_style.POSITION ) {
1017 /* This node is positioned absolutely. (Relatively) simple! */
1018
1019 if( mcss_prop_is_active_NOT_flag( effect_style.LEFT, AUTO ) ) {
1020
1021 child_iter_idx = retrohtr_node( tree, node_idx )->parent;
1022 while( 0 <= retrohtr_node( tree, child_iter_idx )->parent ) {
1023 retrohtr_break_on_active_pos( child_iter_idx );
1024 child_iter_idx = retrohtr_node( tree, child_iter_idx )->parent;
1025 }
1026
1027 /* Set X to highest non-explicit ancestor. */
1028 retrohtr_node( tree, node_idx )->x =
1029 retrohtr_node( tree, child_iter_idx )->x + effect_style.LEFT;
1030 }
1031 if( mcss_prop_is_active_NOT_flag( effect_style.RIGHT, AUTO ) ) {
1032
1033 child_iter_idx = retrohtr_node( tree, node_idx )->parent;
1034 while( 0 <= retrohtr_node( tree, child_iter_idx )->parent ) {
1035 retrohtr_break_on_active_pos( child_iter_idx );
1036 child_iter_idx = retrohtr_node( tree, child_iter_idx )->parent;
1037 }
1038
1039 /* Set X to highest non-explicit ancestor. */
1040 retrohtr_node( tree, node_idx )->x =
1041 retrohtr_node( tree, child_iter_idx )->w -
1042 retrohtr_node( tree, node_idx )->w -
1043 effect_style.RIGHT;
1044 }
1045
1046 } else if(
1047 MCSS_DISPLAY_INLINE == effect_style.DISPLAY &&
1048 MCSS_DISPLAY_INLINE == prev_sibling_style->DISPLAY &&
1049 0 <= prev_sibling_idx
1050 ) {
1051 /* Place to the right of the previous sibling. */
1052 retrohtr_node( tree, node_idx )->x =
1053 retrohtr_node( tree, prev_sibling_idx )->x +
1054 retrohtr_node( tree, prev_sibling_idx )->w;
1055
1056 } else if( 0 <= retrohtr_node( tree, node_idx )->parent ) {
1057 retrohtr_node( tree, node_idx )->x = retrohtr_node_parent( tree, node_idx )->x;
1058 }
1059
1060 /* y */
1061
1062 /* TODO: Add margins of children? */
1063
1064 if( MCSS_POSITION_ABSOLUTE == effect_style.POSITION ) {
1065 /* This node is positioned absolutely. (Relatively) simple! */
1066
1067 if( mcss_prop_is_active_NOT_flag( effect_style.TOP, AUTO ) ) {
1068
1069 child_iter_idx = retrohtr_node( tree, node_idx )->parent;
1070 while( 0 <= retrohtr_node( tree, child_iter_idx )->parent ) {
1071 retrohtr_break_on_active_pos( child_iter_idx );
1072 child_iter_idx = retrohtr_node( tree, child_iter_idx )->parent;
1073 }
1074
1075 /* Set Y to highest non-explicit ancestor. */
1076 retrohtr_node( tree, node_idx )->y =
1077 retrohtr_node( tree, child_iter_idx )->y + effect_style.TOP;
1078 }
1079 if( mcss_prop_is_active_NOT_flag( effect_style.BOTTOM, AUTO ) ) {
1080
1081 child_iter_idx = retrohtr_node( tree, node_idx )->parent;
1082 while( 0 <= retrohtr_node( tree, child_iter_idx )->parent ) {
1083 retrohtr_break_on_active_pos( child_iter_idx );
1084 child_iter_idx = retrohtr_node( tree, child_iter_idx )->parent;
1085 }
1086
1087 /* Set Y to highest non-explicit ancestor. */
1088 retrohtr_node( tree, node_idx )->y =
1089 retrohtr_node( tree, child_iter_idx )->h -
1090 retrohtr_node( tree, node_idx )->h -
1091 effect_style.BOTTOM;
1092 }
1093
1094 } else if(
1095 MCSS_DISPLAY_INLINE == effect_style.DISPLAY &&
1096 MCSS_DISPLAY_INLINE == prev_sibling_style->DISPLAY &&
1097 0 <= prev_sibling_idx
1098 ) {
1099 /* Place to the right of the previous sibling. */
1100 retrohtr_node( tree, node_idx )->y = retrohtr_node( tree, prev_sibling_idx )->y;
1101
1102 } else if( 0 <= prev_sibling_idx ) {
1103 /* Place below the previous block sibling. */
1104
1105 /* TODO: We should probably use the tallest element on the prev sibling's
1106 * line, but that seems hard...
1107 */
1108
1109 retrohtr_node( tree, node_idx )->y =
1110 retrohtr_node( tree, prev_sibling_idx )->y +
1111 retrohtr_node( tree, prev_sibling_idx )->h;
1112
1113 } else if( 0 <= retrohtr_node( tree, node_idx )->parent ) {
1114 /* Position relative to other nodes. */
1115
1116 retrohtr_node( tree, node_idx )->y = retrohtr_node_parent( tree, node_idx )->y;
1117 }
1118
1119 /* margin-left, margin-right */
1120
1121 if(
1122 MCSS_POSITION_ABSOLUTE != retrohtr_node( tree, node_idx )->pos &&
1123 0 <= retrohtr_node( tree, node_idx )->parent &&
1124 mcss_prop_is_active_flag( effect_style.MARGIN_LEFT, AUTO ) &&
1125 mcss_prop_is_active_flag( effect_style.MARGIN_RIGHT, AUTO )
1126 ) {
1127 /* Center */
1128 retrohtr_node( tree, node_idx )->x =
1129 retrohtr_node_parent( tree, node_idx )->x +
1130 (retrohtr_node_parent( tree, node_idx )->w >> 1) -
1131 (retrohtr_node( tree, node_idx )->w >> 1);
1132
1133 } else if(
1134 0 <= retrohtr_node( tree, node_idx )->parent &&
1135 mcss_prop_is_active_flag( effect_style.MARGIN_LEFT, AUTO ) &&
1136 mcss_prop_is_active_NOT_flag( effect_style.MARGIN_RIGHT, AUTO )
1137 ) {
1138 /* Justify right. */
1139 /* TODO: Subtract padding below, as well. */
1140 retrohtr_node( tree, node_idx )->x =
1141 retrohtr_node_parent( tree, node_idx )->w -
1142 retrohtr_node( tree, node_idx )->w;
1143
1144 } else if( mcss_prop_is_active( effect_style.MARGIN_LEFT ) ) {
1145 /* Justify left. */
1146 retrohtr_node( tree, node_idx )->x += effect_style.MARGIN_LEFT;
1147 }
1148
1149 /* padding */
1150
1151 /* TODO: Padding is still broken. Needs more involved understanding of
1152 * where elements are in their container.
1153 */
1154
1155 debug_printf( 1, "(d: " SIZE_T_FMT ") node " SIZE_T_FMT " is on edge: %u",
1156 d, node_idx, retrohtr_node( tree, node_idx )->edge );
1157
1158 assert(
1159 0 == node_idx ||
1160 RETROHTR_EDGE_UNKNOWN != retrohtr_node( tree, node_idx )->edge );
1161
1162 if(
1163 RETROHTR_EDGE_LEFT ==
1164 (RETROHTR_EDGE_LEFT & retrohtr_node( tree, node_idx )->edge)
1165 ) {
1166 /* Try specific left padding first, then try general padding. */
1167 if( mcss_prop_is_active_NOT_flag( parent_style->PADDING_LEFT, AUTO ) ) {
1168 retrohtr_node( tree, node_idx )->x += parent_style->PADDING_LEFT;
1169 } else if( mcss_prop_is_active_NOT_flag( parent_style->PADDING, AUTO ) ) {
1170 retrohtr_node( tree, node_idx )->x += parent_style->PADDING;
1171 }
1172 }
1173
1174 if(
1175 RETROHTR_EDGE_TOP ==
1176 (RETROHTR_EDGE_TOP & retrohtr_node( tree, node_idx )->edge) &&
1177 /* Only apply padding to first node in line. The rest will pick it up. */
1178 RETROHTR_EDGE_LEFT ==
1179 (RETROHTR_EDGE_LEFT & retrohtr_node( tree, node_idx )->edge)
1180 ) {
1181 /* Try specific top padding first, then try general padding. */
1182 if( mcss_prop_is_active_NOT_flag( parent_style->PADDING_TOP, AUTO ) ) {
1183 retrohtr_node( tree, node_idx )->y += parent_style->PADDING_TOP;
1184 } else if( mcss_prop_is_active_NOT_flag( parent_style->PADDING, AUTO ) ) {
1185 retrohtr_node( tree, node_idx )->y += parent_style->PADDING;
1186 }
1187 }
1188
1189 /* color */
1190
1191 if( mcss_prop_is_active( effect_style.COLOR ) ) {
1192 retrohtr_node( tree, node_idx )->fg = effect_style.COLOR;
1193 }
1194
1195 if( mcss_prop_is_active( effect_style.BACKGROUND_COLOR ) ) {
1196 retrohtr_node( tree, node_idx )->bg = effect_style.BACKGROUND_COLOR;
1197 }
1198
1199 /* Figure out child positions. */
1200
1201 retrohtr_mark_edge_child_nodes( parser, tree, node_idx );
1202
1203 maug_mzero( &child_prev_sibling_style, sizeof( struct MCSS_STYLE ) );
1204 node_iter_idx = retrohtr_node( tree, node_idx )->first_child;
1205 while( 0 <= node_iter_idx ) {
1206 /* Mark child nodes on the edge so applying padding can be done. */
1207
1208 /* Figure out child node positioning. */
1209 retrohtr_tree_pos(
1210 parser, tree, &child_prev_sibling_style, &effect_style,
1211 node_iter_idx, d + 1 );
1212
1213 node_iter_idx = retrohtr_node( tree, node_iter_idx )->next_sibling;
1214 }
1215
1216 assert( !mdata_vector_is_locked( &(parser->tags) ) );
1217 mdata_vector_lock( &(parser->tags) );
1218 p_tag_iter = mdata_vector_get( &(parser->tags), tag_idx, union MHTML_TAG );
1219 assert( NULL != p_tag_iter );
1220
1221 if( MHTML_TAG_TYPE_INPUT == p_tag_iter->base.type ) {
1222 /* Feed the position back to the GUI control created during tree_size. */
1223 retval = retrogui_pos_ctl( &(tree->gui), node_idx,
1224 retrohtr_node_screen_x( tree, node_idx ),
1225 retrohtr_node_screen_y( tree, node_idx ),
1226 retrohtr_node( tree, node_idx )->w,
1227 retrohtr_node( tree, node_idx )->h );
1228 maug_cleanup_if_not_ok();
1229 }
1230
1231 debug_printf( RETROHTR_TRACE_LVL,
1232 "setting node " SIZE_T_FMT " dirty...", node_idx );
1233 retrohtr_node( tree, node_idx )->flags |= RETROHTR_NODE_FLAG_DIRTY;
1234
1235cleanup:
1236
1237 if( mdata_vector_is_locked( &(parser->tags) ) ) {
1238 mdata_vector_unlock( &(parser->tags) );
1239 }
1240
1241 /* We're done with the prev_sibling_style for this iter, so prepare it for
1242 * the next called by the parent!
1243 */
1244 if( NULL != prev_sibling_style ) {
1245 maug_mcpy(
1246 prev_sibling_style, &effect_style,
1247 sizeof( struct MCSS_STYLE ) );
1248 }
1249
1250 return retval;
1251}
1252
1253MERROR_RETVAL retrohtr_tree_draw(
1254 struct MHTML_PARSER* parser, struct RETROHTR_RENDER_TREE* tree,
1255 ssize_t node_idx, size_t d
1256) {
1257 union MHTML_TAG* p_tag = NULL;
1258 struct RETROHTR_RENDER_NODE* node = NULL;
1259 MERROR_RETVAL retval = MERROR_OK;
1260
1261 node = retrohtr_node( tree, node_idx );
1262
1263 if( NULL == node ) {
1264 return MERROR_OK;
1265 }
1266
1267 /* TODO: Multi-pass, draw absolute pos afterwards. */
1268
1269 if( 0 > node->tag ) {
1270 goto cleanup;
1271 }
1272
1273 if( RETROHTR_NODE_FLAG_DIRTY != (RETROHTR_NODE_FLAG_DIRTY & node->flags) ) {
1274 goto cleanup;
1275 }
1276
1277 assert( !mdata_vector_is_locked( &(parser->tags) ) );
1278 mdata_vector_lock( &(parser->tags) );
1279
1280 p_tag = mdata_vector_get( &(parser->tags), node->tag, union MHTML_TAG );
1281 if( NULL == p_tag ) {
1282 goto cleanup;
1283 }
1284
1285 /* Perform drawing. */
1286 if( MHTML_TAG_TYPE_TEXT == p_tag->base.type ) {
1287
1288 if(
1289 0 > p_tag->TEXT.content_idx ||
1290#ifdef RETROGXC_PRESENT
1291 0 > node->font_idx
1292#else
1293 (MAUG_MHANDLE)NULL == node->font_h
1294#endif /* RETROGXC_PRESENT */
1295 ) {
1296 goto cleanup;
1297 }
1298
1299 mdata_strpool_lock( &(parser->strpool) );
1300
1301#ifdef RETROGXC_PRESENT
1302 retrogxc_string(
1303#else
1305#endif /* RETROGXC_PRESENT */
1306 NULL, node->fg,
1307 mdata_strpool_get( &(parser->strpool), p_tag->TEXT.content_idx ),
1308 p_tag->TEXT.content_sz,
1309#ifdef RETROGXC_PRESENT
1310 node->font_idx,
1311#else
1312 node->font_h,
1313#endif /* RETROGXC_PRESENT */
1314 retrohtr_node_screen_x( tree, node_idx ),
1315 retrohtr_node_screen_y( tree, node_idx ),
1316 node->w, node->h, 0 );
1317
1318 mdata_strpool_unlock( &(parser->strpool) );
1319
1320 } else if( MHTML_TAG_TYPE_BODY == p_tag->base.type ) {
1321
1322 debug_printf(
1323 RETROHTR_TRACE_LVL, "drawing BODY node " SIZE_T_FMT "...", node_idx );
1324
1325 /* Draw body BG. */
1326 if( RETROFLAT_COLOR_NULL != node->bg ) {
1327 retroflat_rect(
1328 NULL, node->bg,
1329 retrohtr_node_screen_x( tree, node_idx ),
1330 retrohtr_node_screen_y( tree, node_idx ),
1331 retrohtr_node( tree, node_idx )->w,
1332 retrohtr_node( tree, node_idx )->h,
1333 RETROFLAT_FLAGS_FILL );
1334 }
1335
1336 } else if( MHTML_TAG_TYPE_IMG == p_tag->base.type ) {
1337 /* Blit the image. */
1338
1339 if( !retroflat_bitmap_ok( &(retrohtr_node( tree, node_idx )->bitmap) ) ) {
1340 goto cleanup;
1341 }
1342
1343 debug_printf(
1344 RETROHTR_TRACE_LVL, "drawing IMG node " SIZE_T_FMT "...", node_idx );
1345
1347 NULL, &(retrohtr_node( tree, node_idx )->bitmap),
1348 0, 0,
1349 retrohtr_node_screen_x( tree, node_idx ),
1350 retrohtr_node_screen_y( tree, node_idx ),
1351 retroflat_bitmap_w( &(retrohtr_node( tree, node_idx )->bitmap) ),
1352 retroflat_bitmap_h( &(retrohtr_node( tree, node_idx )->bitmap) ),
1354 /* retrohtr_node( tree, node_idx )->w,
1355 retrohtr_node( tree, node_idx )->h */ );
1356
1357 } else if( MHTML_TAG_TYPE_INPUT == p_tag->base.type ) {
1358
1359 debug_printf(
1360 RETROHTR_TRACE_LVL, "setting tree GUI dirty..." );
1361
1362 tree->gui.flags |= RETROGUI_FLAGS_DIRTY;
1363
1364 } else {
1365 if( RETROFLAT_COLOR_NULL == node->bg ) {
1366 goto cleanup;
1367 }
1368
1369 debug_printf(
1370 RETROHTR_TRACE_LVL, "drawing xs node " SIZE_T_FMT "...",
1371 /* gc_mhtml_tag_names[mhtml_tag( parser,
1372 retrohtr_node( tree, node_idx )->tag )->base.type], */
1373 node_idx );
1374
1376 NULL, node->bg,
1377 retrohtr_node_screen_x( tree, node_idx ),
1378 retrohtr_node_screen_y( tree, node_idx ),
1379 node->w, node->h,
1381 }
1382
1383 node->flags &= ~RETROHTR_NODE_FLAG_DIRTY;
1384
1385cleanup:
1386
1387 if( mdata_vector_is_locked( &(parser->tags) ) ) {
1388 mdata_vector_unlock( &(parser->tags) );
1389 }
1390
1391 if( MERROR_OK != retval ) {
1392 error_printf( "failed drawing node: " SIZE_T_FMT, node_idx );
1393 }
1394
1395 /* Keep trying to render children, tho. */
1396
1397 retrohtr_tree_draw( parser, tree, node->first_child, d + 1 );
1398
1399 retrohtr_tree_draw( parser, tree, node->next_sibling, d );
1400
1401 /* If this is the root redraw call, redraw GUI elements. */
1402 if(
1403 0 == d &&
1404 RETROHTR_TREE_FLAG_GUI_ACTIVE ==
1405 (tree->flags & RETROHTR_TREE_FLAG_GUI_ACTIVE)
1406 ) {
1407 retrogui_redraw_ctls( &(tree->gui) );
1408 }
1409
1410 return retval;
1411}
1412
1413retrogui_idc_t retrohtr_tree_poll_ctls(
1414 struct RETROHTR_RENDER_TREE* tree,
1415 RETROFLAT_IN_KEY* input,
1416 struct RETROFLAT_INPUT* input_evt
1417) {
1418 retrogui_idc_t idc = 0;
1419 MERROR_RETVAL retval = MERROR_OK;
1420
1421 assert( retrohtr_tree_is_locked( tree ) );
1422
1423 if(
1424 RETROHTR_TREE_FLAG_GUI_ACTIVE !=
1425 (RETROHTR_TREE_FLAG_GUI_ACTIVE & tree->flags)
1426 ) {
1427 /* No GUI, so exit without even unlocking. */
1428 return 0;
1429 }
1430
1431 idc = retrogui_poll_ctls( &(tree->gui), input, input_evt );
1432
1433 if( 0 < idc ) {
1434 debug_printf(
1435 RETROHTR_TRACE_LVL, "setting node " SIZE_T_FMT " dirty...", idc );
1436 retrohtr_node( tree, idc )->flags |= RETROHTR_NODE_FLAG_DIRTY;
1437 }
1438
1439 if( MERROR_OK != retval ) {
1440 idc = 0;
1441 }
1442
1443 return idc;
1444}
1445
1446MERROR_RETVAL retrohtr_tree_dump(
1447 struct RETROHTR_RENDER_TREE* tree, struct MHTML_PARSER* parser,
1448 ssize_t node_idx, size_t d
1449) {
1450 size_t i = 0;
1451 char indents[31];
1452 union MHTML_TAG* p_tag_iter = NULL;
1453 MERROR_RETVAL retval = MERROR_OK;
1454
1455 if( 0 > node_idx ) {
1456 return MERROR_OK;
1457 }
1458
1459 assert( !mdata_vector_is_locked( &(parser->tags) ) );
1460 mdata_vector_lock( &(parser->tags) );
1461
1462 p_tag_iter = mdata_vector_get(
1463 &(parser->tags), tree->nodes[node_idx].tag, union MHTML_TAG );
1464 if( NULL == p_tag_iter ) {
1465 goto cleanup;
1466 }
1467
1468 /* Generate the indentation. */
1469 maug_mzero( indents, 30 );
1470 for( i = 0 ; d > i ; i++ ) {
1471 if( maug_strlen( indents ) >= 30 ) {
1472 break;
1473 }
1474 strcat( indents, " " );
1475 }
1476
1477 /* Print the debug line. */
1478 debug_printf(
1479 1,
1480 "%s" SSIZE_T_FMT " (tag %s): x: " SSIZE_T_FMT ", y: " SSIZE_T_FMT
1481 " (" SSIZE_T_FMT " x " SSIZE_T_FMT ") f: "
1482#ifdef RETROGXC_PRESENT
1483 SSIZE_T_FMT,
1484#else
1485 "%p",
1486#endif /* RETROGXC_PRESENT */
1487 indents, node_idx,
1488 0 <= tree->nodes[node_idx].tag ?
1489 gc_mhtml_tag_names[p_tag_iter->base.type] : "ROOT",
1490 tree->nodes[node_idx].x, tree->nodes[node_idx].y,
1491 tree->nodes[node_idx].w, tree->nodes[node_idx].h,
1492#ifdef RETROGXC_PRESENT
1493 tree->nodes[node_idx].font_idx
1494#else
1495 tree->nodes[node_idx].font_h
1496#endif /* RETROGXC_PRESENT */
1497 );
1498
1499 mdata_vector_unlock( &(parser->tags) );
1500
1501 retval = retrohtr_tree_dump(
1502 tree, parser, tree->nodes[node_idx].first_child, d + 1 );
1503 maug_cleanup_if_not_ok();
1504
1505 retval = retrohtr_tree_dump(
1506 tree, parser, tree->nodes[node_idx].next_sibling, d );
1507 maug_cleanup_if_not_ok();
1508
1509cleanup:
1510
1511 return retval;
1512}
1513
1514void retrohtr_tree_free( struct RETROHTR_RENDER_TREE* tree ) {
1515
1516 debug_printf( RETROHTR_TRACE_LVL, "freeing render nodes..." );
1517
1518 /* TODO: Free bitmaps from img! */
1519
1520 /* TODO: Free node->font_h! */
1521
1522 /* Free GUI if present. */
1523 if(
1524 RETROHTR_TREE_FLAG_GUI_ACTIVE ==
1525 (tree->flags & RETROHTR_TREE_FLAG_GUI_ACTIVE)
1526 ) {
1527 retrogui_destroy( &(tree->gui) );
1528 }
1529
1530 /* Unlock nodes before trying to free them. */
1531 retrohtr_tree_unlock( tree );
1532
1533 if( (MAUG_MHANDLE)NULL != tree->nodes_h ) {
1534 maug_mfree( tree->nodes_h );
1535 }
1536}
1537
1538MERROR_RETVAL retrohtr_tree_init( struct RETROHTR_RENDER_TREE* tree ) {
1539 MERROR_RETVAL retval = MERROR_OK;
1540
1541 maug_mzero( tree, sizeof( struct RETROHTR_RENDER_TREE ) );
1542
1543 /* Perform initial node allocation. */
1544 tree->nodes_sz_max = MHTML_PARSER_TAGS_INIT_SZ;
1545 debug_printf( RETROHTR_TRACE_LVL,
1546 "allocating " SIZE_T_FMT " nodes...", tree->nodes_sz_max );
1547 tree->nodes_h = maug_malloc(
1548 tree->nodes_sz_max, sizeof( struct RETROHTR_RENDER_NODE ) );
1549 maug_cleanup_if_null_alloc( MAUG_MHANDLE, tree->nodes_h );
1550
1551 /* XXX
1552 r.w_max = retroflat_screen_w();
1553 r.h_max = retroflat_screen_h(); */
1554
1555cleanup:
1556
1557 return retval;
1558}
1559
1560#endif /* RETROHTR_C */
1561
1562#endif /* !RETROHTR_H */
1563
uint16_t MERROR_RETVAL
Return type indicating function returns a value from this list.
Definition merror.h:19
MERROR_RETVAL retroflat_blit_bitmap(struct RETROFLAT_BITMAP *target, struct RETROFLAT_BITMAP *src, size_t s_x, size_t s_y, int16_t d_x, int16_t d_y, size_t w, size_t h, int16_t instance)
Blit the contents of a ::RETROFLAT_BITMAP onto another ::RETROFLAT_BITMAP.
MERROR_RETVAL retroflat_load_bitmap(const char *filename, struct RETROFLAT_BITMAP *bmp_out, uint8_t flags)
Load a bitmap into the given ::RETROFLAT_BITMAP structure if it is available. Bitmaps are subject to ...
#define RETROFLAT_INSTANCE_NULL
Pass to retroflat_blit_bitmap() instance arg if this is not a sprite (i.e. if it is a background tile...
Definition retroflt.h:569
int8_t RETROFLAT_COLOR
Defines an index in the platform-specific color-table.
Definition retroflt.h:325
#define RETROFLAT_FLAGS_LITERAL_PATH
Flag for retroflat_load_bitmap() to not use assets path.
Definition retroflt.h:384
#define RETROFLAT_FLAGS_BITMAP_SILENT
flag for retroflat_load_bitmap() to not show an error dialog if a bitmap fails to load (on supported ...
Definition retroflt.h:390
void retroflat_rect(struct RETROFLAT_BITMAP *target, const RETROFLAT_COLOR color, int16_t x, int16_t y, int16_t w, int16_t h, uint8_t flags)
Draw a rectangle onto the target ::RETROFLAT_BITMAP.
#define RETROFLAT_FLAGS_FILL
Flag for retroflat_rect() or retroflat_ellipse(), indicating drawn shape should be filled.
Definition retroflt.h:373
retrogui_idc_t retrogui_poll_ctls(struct RETROGUI *gui, RETROFLAT_IN_KEY *p_input, struct RETROFLAT_INPUT *input_evt)
Poll for the last clicked control and maintain listboxes and menus.
int16_t retrogui_idc_t
Unique identifying constant number for controls.
Definition retrogui.h:292
void retrofont_string(retroflat_blit_t *target, RETROFLAT_COLOR color, const char *str, size_t str_sz, MAUG_MHANDLE font_h, size_t x, size_t y, size_t max_w, size_t max_h, uint8_t flags)
Draw a string with the given font.
MERROR_RETVAL retrofont_load(const char *font_name, MAUG_MHANDLE *p_font_h, uint8_t glyph_h, uint16_t first_glyph, uint16_t glyphs_count)
Load a font for drawing.
size_t nodes_sz_max
Current alloc'd number of nodes in RETROHTR_RENDER_NODE::nodes_h.
Definition retrohtr.h:67
ssize_t parent
Index of container's render node in RETROHTR_RENDER_TREE.
Definition retrohtr.h:51
ssize_t first_child
Index of first child's render node in RETROHTR_RENDER_TREE.
Definition retrohtr.h:53
MERROR_RETVAL retrohtr_apply_styles(struct MHTML_PARSER *parser, struct RETROHTR_RENDER_TREE *tree, struct MCSS_STYLE *parent_style, struct MCSS_STYLE *effect_style, ssize_t tag_idx)
Create a style node that is a composite of a parent style and the styles applicable to the classes/ID...
ssize_t next_sibling
Index of next sibling's render node in RETROHTR_RENDER_TREE.
Definition retrohtr.h:55
size_t nodes_sz
Current active number of nodes in RETROHTR_RENDER_NODE::nodes_h.
Definition retrohtr.h:65
struct RETROHTR_RENDER_NODE * nodes
Locked pointer to nodes when locked with retrohtr_tree_lock().
Definition retrohtr.h:63
Definition mhtml.h:150
Struct passed to retroflat_poll_input() to hold return data.
Definition retroflt.h:814
Definition retrogui.h:430
MAUG_MHANDLE font_h
Font used to draw any attached RETROGUI_CTL.
Definition retrogui.h:456
Definition retrohtr.h:28
Definition retrohtr.h:59
Definition mhtml.h:145
Definition retrogui.h:412