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 char* strpool = NULL;
466
467#ifdef RETROGXC_PRESENT
468 if( 0 <= *font_idx_p ) {
469 error_printf( "tried to load font but font already loaded, idx: "
470 SSIZE_T_FMT, *font_idx_p );
471#else
472 if( (MAUG_MHANDLE)NULL != *font_h_p ) {
473 error_printf( "tried to load font but font already loaded, p: %p",
474 *font_h_p );
475#endif /* RETROGXC_PRESENT */
476 goto cleanup;
477 }
478
479 mdata_strpool_lock( &(styler->strpool), strpool );
480
481 debug_printf( RETROHTR_TRACE_LVL,
482 "loading font: %s (" SSIZE_T_FMT ")",
483 &(strpool[effect_style->FONT_FAMILY]), 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( &(strpool[effect_style->FONT_FAMILY]), 0, 33, 93 );
496#else
497 retval = retrofont_load(
498 &(strpool[effect_style->FONT_FAMILY]), font_h_p, 0, 33, 93 );
499#endif /* RETROGXC_PRESENT */
500
501cleanup:
502
503 mdata_strpool_unlock( &(styler->strpool), strpool );
504
505 return retval;
506}
507
508MERROR_RETVAL retrohtr_tree_gui(
509 struct RETROHTR_RENDER_TREE* tree, struct MCSS_PARSER* styler,
510 struct MCSS_STYLE* effect_style
511) {
512 MERROR_RETVAL retval = MERROR_OK;
513
514 /* Create a GUI handler just for this tree. */
515 if(
516 RETROHTR_TREE_FLAG_GUI_ACTIVE ==
517 (RETROHTR_TREE_FLAG_GUI_ACTIVE & tree->flags)
518 ) {
519 debug_printf( RETROHTR_TRACE_LVL, "tree GUI already active!" );
520 goto cleanup;
521 }
522
523 /* This means all GUI items will use the font from the first node
524 * loaded with a GUI item!
525 */
526 retval = retrogui_init( &(tree->gui) );
527 maug_cleanup_if_not_ok();
528
529 retval = retrohtr_load_font(
530 styler,
531#ifdef RETROGXC_PRESENT
532 &(tree->gui.font_idx),
533#else
534 &(tree->gui.font_h),
535#endif /* RETROGXC_PRESENT */
536 effect_style );
537 maug_cleanup_if_not_ok();
538
539 tree->flags |= RETROHTR_TREE_FLAG_GUI_ACTIVE;
540
541 debug_printf( RETROHTR_TRACE_LVL, "tree GUI initialized!" );
542
543cleanup:
544 return retval;
545}
546
547MERROR_RETVAL retrohtr_tree_size(
548 struct MHTML_PARSER* parser, struct RETROHTR_RENDER_TREE* tree,
549 struct MCSS_STYLE* prev_sibling_style,
550 struct MCSS_STYLE* parent_style, ssize_t node_idx, size_t d
551) {
552 struct MCSS_STYLE effect_style;
553 struct MCSS_STYLE child_prev_sibling_style;
554 struct MCSS_STYLE child_style;
555 char* strpool = NULL;
556 ssize_t child_iter_idx = -1;
557 ssize_t tag_idx = -1;
558 ssize_t node_iter_idx = -1;
559 size_t this_line_w = 0;
560 size_t this_line_h = 0;
561 MERROR_RETVAL retval = MERROR_OK;
562 union RETROGUI_CTL ctl;
563 union MHTML_TAG* p_tag_iter = NULL;
564 union MHTML_TAG* p_tag_node = NULL;
565
566 if( NULL == retrohtr_node( tree, node_idx ) ) {
567 goto cleanup;
568 }
569
570 tag_idx = retrohtr_node( tree, node_idx )->tag;
571
572 retval = retrohtr_apply_styles(
573 parser, tree, parent_style, &effect_style, tag_idx );
574 maug_cleanup_if_not_ok();
575
576 assert( !mdata_vector_is_locked( &(parser->tags) ) );
577 mdata_vector_lock( &(parser->tags) );
578
579 p_tag_iter = mdata_vector_get( &(parser->tags), tag_idx, union MHTML_TAG );
580 assert( NULL != p_tag_iter );
581
582 /* position */
583
584 if( mcss_prop_is_active( effect_style.POSITION ) ) {
585 debug_printf( RETROHTR_TRACE_LVL,
586 "node " SSIZE_T_FMT ": applying %s positioning",
587 node_idx, gc_mcss_position_names[effect_style.POSITION] );
588 /* TODO: MCSS_POS_NOTE: We'd like to get rid of this so all positioning
589 * is done through CSS... unfortunately, we only track the current
590 * and previous effective styles while working that out later, so
591 * we need to pin this to the element directly so we can rule it
592 * out of the box model e.g. when determining x/y coords of its
593 * neighbors.
594 */
595 retrohtr_node( tree, node_idx )->pos = effect_style.POSITION;
596 retrohtr_node( tree, node_idx )->pos_flags = effect_style.POSITION_flags;
597 }
598
599 /* Grab fixed dimensions before content-based calculations of children, so
600 * we know if there are constraints. If these aren't set, then we'll size
601 * based on childrens' sizes after we determine childrens' sizes below.
602 */
603
604 if( mcss_prop_is_active_NOT_flag( effect_style.WIDTH, AUTO ) ) {
605 retrohtr_node( tree, node_idx )->w = effect_style.WIDTH;
606 }
607
608 if( mcss_prop_is_active_NOT_flag( effect_style.HEIGHT, AUTO ) ) {
609 retrohtr_node( tree, node_idx )->h = effect_style.HEIGHT;
610 }
611
612 /* Figure out how big the contents of this node are. */
613
614 /* Font is heritable, so load it for all nodes even if we don't use it. */
615 retval = retrohtr_load_font(
616 &(parser->styler),
617#ifdef RETROGXC_PRESENT
618 &(retrohtr_node( tree, node_idx )->font_idx),
619#else
620 &(retrohtr_node( tree, node_idx )->font_h),
621#endif /* RETROGXC_PRESENT */
622 &effect_style );
623 maug_cleanup_if_not_ok();
624
625 if( 0 <= tag_idx && MHTML_TAG_TYPE_TEXT == p_tag_iter->base.type ) {
626 /* Get text size to use in calculations below. */
627
628 mdata_strpool_lock( &(parser->strpool), strpool );
629
630#ifdef RETROGXC_PRESENT
631 retrogxc_string_sz(
632#else
633 retrofont_string_sz(
634#endif /* RETROGXC_PRESENT */
635 NULL, &(strpool[p_tag_iter->TEXT.content_idx]),
636 p_tag_iter->TEXT.content_sz,
637#ifdef RETROGXC_PRESENT
638 retrohtr_node( tree, node_idx )->font_idx,
639#else
640 retrohtr_node( tree, node_idx )->font_h,
641#endif /* RETROGXC_PRESENT */
642 /* Constrain node text size to parent size. */
643 retrohtr_node_parent( tree, node_idx )->w,
644 retrohtr_node_parent( tree, node_idx )->h,
645 &(retrohtr_node( tree, node_idx )->w),
646 &(retrohtr_node( tree, node_idx )->h), 0 );
647
648 debug_printf( RETROHTR_TRACE_LVL, "TEXT w: " SIZE_T_FMT,
649 retrohtr_node( tree, node_idx )->w );
650
651 mdata_strpool_unlock( &(parser->strpool), strpool );
652
653 } else if(
654 0 <= tag_idx &&
655 MHTML_TAG_TYPE_INPUT == p_tag_iter->base.type
656 ) {
657 /* Push the control (for the client renderer to redraw later). */
658
659 retval = retrohtr_tree_gui( tree, &(parser->styler), &effect_style );
660
661 if(
662 /* Use the same ID for the node and control it creates. */
663 MERROR_OK != retrogui_init_ctl(
664 &ctl, RETROGUI_CTL_TYPE_BUTTON, node_idx )
665 ) {
666 error_printf( "could not initialize control!" );
667 goto cleanup;
668 }
669
670 p_tag_node = mdata_vector_get(
671 &(parser->tags),
672 retrohtr_node( tree, node_idx )->tag, union MHTML_TAG );
673
674 ctl.base.x = retrohtr_node( tree, node_idx )->x;
675 ctl.base.y = retrohtr_node( tree, node_idx )->y;
676 ctl.base.w = 0;
677 ctl.base.h = 0;
678 ctl.BUTTON.label = p_tag_node->INPUT.value;
679
680 /* Grab determined size back from control. */
681 retrohtr_node( tree, node_idx )->w = ctl.base.w;
682 retrohtr_node( tree, node_idx )->h = ctl.base.h;
683
684 debug_printf( RETROHTR_TRACE_LVL, "initialized control for INPUT..." );
685
686 retrogui_push_ctl( &(tree->gui), &ctl );
687
688 } else if( 0 <= tag_idx && MHTML_TAG_TYPE_IMG == p_tag_iter->base.type ) {
689
690 if( retroflat_bitmap_ok( &(retrohtr_node( tree, node_idx )->bitmap) ) ) {
691 retrohtr_node( tree, node_idx )->w =
692 retroflat_bitmap_w( &(retrohtr_node( tree, node_idx )->bitmap) );
693 retrohtr_node( tree, node_idx )->h =
694 retroflat_bitmap_h( &(retrohtr_node( tree, node_idx )->bitmap) );
695 }
696
697 debug_printf( RETROHTR_TRACE_LVL, "TEXT w: " SIZE_T_FMT,
698 retrohtr_node( tree, node_idx )->w );
699
700 } else {
701 /* Get sizing of child nodes. */
702
703 maug_mzero( &child_prev_sibling_style, sizeof( struct MCSS_STYLE ) );
704 node_iter_idx = retrohtr_node( tree, node_idx )->first_child;
705 mdata_vector_unlock( &(parser->tags) );
706 while( 0 <= node_iter_idx ) {
707 retrohtr_tree_size(
708 parser, tree, &child_prev_sibling_style, &effect_style,
709 node_iter_idx, d + 1 );
710
711 node_iter_idx = retrohtr_node( tree, node_iter_idx )->next_sibling;
712 }
713 }
714
715 if( mdata_vector_is_locked( &(parser->tags) ) ) {
716 mdata_vector_unlock( &(parser->tags) );
717 }
718
719 /* If our width is still zero, then size based on children. */
720 if( 0 == retrohtr_node( tree, node_idx )->w ) {
721 if(
722 MCSS_DISPLAY_BLOCK == effect_style.DISPLAY &&
723 0 <= retrohtr_node( tree, node_idx )->parent
724 ) {
725 /* Use parent width. */
726 /* TODO: Subtract parent padding! */
727 retrohtr_node( tree, node_idx )->w =
728 retrohtr_node_parent( tree, node_idx )->w;
729 }
730
731 /* Cycle through children and use greatest width. */
732 child_iter_idx = retrohtr_node( tree, node_idx )->first_child;
733 while( 0 <= child_iter_idx ) {
734 assert( !mdata_vector_is_locked( &(parser->tags) ) );
735 retval = retrohtr_apply_styles(
736 parser, tree, &effect_style, &child_style,
737 retrohtr_node( tree, child_iter_idx )->tag );
738 maug_cleanup_if_not_ok();
739
740 /* Skip ABSOLUTE nodes. */
741 if( MCSS_POSITION_ABSOLUTE == child_style.POSITION ) {
742 child_iter_idx =
743 retrohtr_node( tree, child_iter_idx )->next_sibling;
744 continue;
745 }
746
747 if( MCSS_DISPLAY_BLOCK == child_style.DISPLAY ) {
748 /* Reset the line width counter for coming BLOCK node. */
749 this_line_w = 0;
750
751 if(
752 retrohtr_node( tree, child_iter_idx )->w >
753 retrohtr_node( tree, node_idx )->w
754 ) {
755 /* This BLOCK node is the longest yet! */
756 retrohtr_node( tree, node_idx )->w =
757 retrohtr_node( tree, child_iter_idx )->w;
758 }
759 } else {
760 /* Add inline node to this node line's width. */
761 this_line_w += retrohtr_node( tree, child_iter_idx )->w;
762
763 if( this_line_w > retrohtr_node( tree, node_idx )->w ) {
764 /* The line of nodes we've been adding up is the longest yet! */
765 retrohtr_node( tree, node_idx )->w = this_line_w;
766 }
767 }
768 child_iter_idx = retrohtr_node( tree, child_iter_idx )->next_sibling;
769 }
770 }
771
772 /* If our height is still zero, then size based on children. */
773 if( 0 == retrohtr_node( tree, node_idx )->h ) {
774 /* Cycle through children and add heights. */
775 child_iter_idx = retrohtr_node( tree, node_idx )->first_child;
776 while( 0 <= child_iter_idx ) {
777 assert( !mdata_vector_is_locked( &(parser->tags) ) );
779 parser, tree, &effect_style, &child_style,
780 retrohtr_node( tree, child_iter_idx )->tag );
781
782 /* Skip ABSOLUTE nodes. */
783 if( MCSS_POSITION_ABSOLUTE == child_style.POSITION ) {
784 child_iter_idx = retrohtr_node( tree, child_iter_idx )->next_sibling;
785 continue;
786 }
787
788 if( MCSS_DISPLAY_BLOCK == child_style.DISPLAY ) {
789 /* Add the last line to the running height. */
790 retrohtr_node( tree, node_idx )->h += this_line_h;
791
792 /* Start a new running line height with this BLOCK node. */
793 this_line_h = retrohtr_node( tree, child_iter_idx )->h;
794 } else {
795 /* Make sure this line is at least as tall as this INLINE node. */
796 if( this_line_h < retrohtr_node( tree, child_iter_idx )->h ) {
797 this_line_h = retrohtr_node( tree, child_iter_idx )->h;
798 }
799 }
800
801 child_iter_idx = retrohtr_node( tree, child_iter_idx )->next_sibling;
802 }
803
804 /* Add the last line height the node height. */
805 retrohtr_node( tree, node_idx )->h += this_line_h;
806 this_line_h = 0;
807 }
808
809 /* Apply additional modifiers (padding, etc) after children have all been
810 * calculated.
811 */
812
813 /* Try specific left padding first, then try general padding. */
814 if( mcss_prop_is_active_NOT_flag( effect_style.PADDING_LEFT, AUTO ) ) {
815 retrohtr_node( tree, node_idx )->w += effect_style.PADDING_LEFT;
816 } else if( mcss_prop_is_active_NOT_flag( effect_style.PADDING, AUTO ) ) {
817 retrohtr_node( tree, node_idx )->w += effect_style.PADDING;
818 }
819
820 /* Try specific right padding first, then try general padding. */
821 if( mcss_prop_is_active_NOT_flag( effect_style.PADDING_RIGHT, AUTO ) ) {
822 retrohtr_node( tree, node_idx )->w += effect_style.PADDING_RIGHT;
823 } else if( mcss_prop_is_active_NOT_flag( effect_style.PADDING, AUTO ) ) {
824 retrohtr_node( tree, node_idx )->w += effect_style.PADDING;
825 }
826
827 /* Try specific top padding first, then try general padding. */
828 if( mcss_prop_is_active_NOT_flag( effect_style.PADDING_TOP, AUTO ) ) {
829 retrohtr_node( tree, node_idx )->h += effect_style.PADDING_TOP;
830 } else if( mcss_prop_is_active_NOT_flag( effect_style.PADDING, AUTO ) ) {
831 retrohtr_node( tree, node_idx )->h += effect_style.PADDING;
832 }
833
834 /* Try specific bottom padding first, then try general padding. */
835 if( mcss_prop_is_active_NOT_flag( effect_style.PADDING_BOTTOM, AUTO ) ) {
836 retrohtr_node( tree, node_idx )->h += effect_style.PADDING_BOTTOM;
837 } else if( mcss_prop_is_active_NOT_flag( effect_style.PADDING, AUTO ) ) {
838 retrohtr_node( tree, node_idx )->h += effect_style.PADDING;
839 }
840
841 debug_printf( RETROHTR_TRACE_LVL,
842 "setting node " SIZE_T_FMT " dirty...", node_idx );
843 retrohtr_node( tree, node_idx )->flags |= RETROHTR_NODE_FLAG_DIRTY;
844
845cleanup:
846
847 if( mdata_vector_is_locked( &(parser->tags) ) ) {
848 mdata_vector_unlock( &(parser->tags) );
849 }
850
851 /* We're done with the prev_sibling_style for this iter, so prepare it for
852 * the next called by the parent!
853 */
854 if( NULL != prev_sibling_style ) {
855 maug_mcpy(
856 prev_sibling_style, &effect_style,
857 sizeof( struct MCSS_STYLE ) );
858 }
859
860 return retval;
861}
862
863/* TODO: See MCSS_POS_NOTE. */
865#define retrohtr_break_on_active_pos( iter_idx ) \
866 if( mcss_prop_is_active( retrohtr_node( tree, iter_idx )->pos ) ) { \
867 break; \
868 }
869
870static ssize_t retrohtr_find_prev_sibling_in_box_model(
871 struct RETROHTR_RENDER_TREE* tree,
872 ssize_t node_idx
873) {
874 ssize_t sibling_iter_idx = -1;
875 ssize_t sibling_found_idx = -1;
876
877 if( 0 > retrohtr_node( tree, node_idx )->parent ) {
878 /* Can't determine sibling! */
879 goto cleanup;
880 }
881
882 sibling_iter_idx = retrohtr_node_parent( tree, node_idx )->first_child;
883
884 if( sibling_iter_idx == node_idx ) {
885 /* No previous siblings! */
886 goto cleanup;
887 }
888
889 while( 0 <= sibling_iter_idx && node_idx != sibling_iter_idx ) {
890 if(
891 /* TODO: See MCSS_POS_NOTE. This is what we were talking about. */
892 MCSS_POSITION_ABSOLUTE != retrohtr_node( tree, sibling_iter_idx )->pos
893 ) {
894 sibling_found_idx = sibling_iter_idx;
895 }
896
897 /* TODO: Reset on <br />? */
898
899 sibling_iter_idx = retrohtr_node( tree, sibling_iter_idx )->next_sibling;
900 }
901
902cleanup:
903 return sibling_found_idx;
904}
905
906static MERROR_RETVAL retrohtr_mark_edge_child_nodes(
907 struct MHTML_PARSER* parser, struct RETROHTR_RENDER_TREE* tree,
908 ssize_t node_parent_idx
909) {
910 ssize_t node_sibling_idx = -1;
911 MERROR_RETVAL retval = MERROR_OK;
912 struct MCSS_STYLE effect_style;
913 size_t col_idx = 0; /* How many nodes right (X)? */
914 size_t row_idx = 0; /* How many nodes down (Y)? */
915 union MHTML_TAG* p_tag_iter = NULL;
916
917 node_sibling_idx = retrohtr_node( tree, node_parent_idx )->first_child;
918 while( 0 <= node_sibling_idx ) {
919 maug_mzero( &effect_style, sizeof( struct MCSS_STYLE ) );
921 parser, tree, NULL, &effect_style,
922 retrohtr_node( tree, node_sibling_idx )->tag );
923
924 if( MCSS_POSITION_ABSOLUTE == effect_style.POSITION ) {
925 /* Absolute nodes are never on the edge. */
926 retrohtr_node( tree, node_sibling_idx )->edge |= RETROHTR_EDGE_INSIDE;
927
928 } else if( MCSS_DISPLAY_INLINE == effect_style.DISPLAY ) {
929 /* Inline, or something that follows previous column. */
930 if( 0 == col_idx ) {
931 retrohtr_node( tree, node_sibling_idx )->edge |= RETROHTR_EDGE_LEFT;
932 }
933 if( 0 == row_idx ) {
934 retrohtr_node( tree, node_sibling_idx )->edge |= RETROHTR_EDGE_TOP;
935 }
936 if( 0 < row_idx && 0 < col_idx ) {
937 retrohtr_node( tree, node_sibling_idx )->edge |= RETROHTR_EDGE_INSIDE;
938 }
939 col_idx++;
940
941 } else {
942 /* Block element will be on the next line, so take that into account
943 * when deciding the edge below.
944 */
945 row_idx++;
946 col_idx = 0;
947
948 /* Block, or something else in a new row. */
949 if( 0 == row_idx ) {
950 retrohtr_node( tree, node_sibling_idx )->edge |= RETROHTR_EDGE_TOP;
951 }
952
953 /* Assume block is always on a new line. */
954 retrohtr_node( tree, node_sibling_idx )->edge |= RETROHTR_EDGE_LEFT;
955 }
956
957 assert( !mdata_vector_is_locked( &(parser->tags) ) );
958 mdata_vector_lock( &(parser->tags) );
959
960 p_tag_iter = mdata_vector_get( &(parser->tags),
961 retrohtr_node( tree, node_sibling_idx )->tag,
962 union MHTML_TAG );
963 assert( NULL != p_tag_iter );
964
965 debug_printf( 1, "marking node " SIZE_T_FMT " (%s) edge: %u",
966 node_sibling_idx,
967 gc_mhtml_tag_names[p_tag_iter->base.type],
968 retrohtr_node( tree, node_sibling_idx )->edge );
969
970 mdata_vector_unlock( &(parser->tags) );
971
972 node_sibling_idx =
973 retrohtr_node( tree, node_sibling_idx )->next_sibling;
974 }
975
976cleanup:
977
978 if( mdata_vector_is_locked( &(parser->tags) ) ) {
979 mdata_vector_unlock( &(parser->tags) );
980 }
981
982 return retval;
983}
984
985MERROR_RETVAL retrohtr_tree_pos(
986 struct MHTML_PARSER* parser, struct RETROHTR_RENDER_TREE* tree,
987 struct MCSS_STYLE* prev_sibling_style,
988 struct MCSS_STYLE* parent_style, ssize_t node_idx, size_t d
989) {
990 struct MCSS_STYLE child_prev_sibling_style;
991 struct MCSS_STYLE effect_style;
992 ssize_t child_iter_idx = -1;
993 ssize_t tag_idx = -1;
994 ssize_t node_iter_idx = -1;
995 ssize_t prev_sibling_idx = -1;
996 MERROR_RETVAL retval = MERROR_OK;
997 union MHTML_TAG* p_tag_iter = NULL;
998
999 if( NULL == retrohtr_node( tree, node_idx ) ) {
1000 goto cleanup;
1001 }
1002
1003 tag_idx = retrohtr_node( tree, node_idx )->tag;
1004
1006 parser, tree, parent_style, &effect_style, tag_idx );
1007
1008 prev_sibling_idx =
1009 retrohtr_find_prev_sibling_in_box_model( tree, node_idx );
1010
1011 /* x */
1012
1013 if( MCSS_POSITION_ABSOLUTE == effect_style.POSITION ) {
1014 /* This node is positioned absolutely. (Relatively) simple! */
1015
1016 if( mcss_prop_is_active_NOT_flag( effect_style.LEFT, AUTO ) ) {
1017
1018 child_iter_idx = retrohtr_node( tree, node_idx )->parent;
1019 while( 0 <= retrohtr_node( tree, child_iter_idx )->parent ) {
1020 retrohtr_break_on_active_pos( child_iter_idx );
1021 child_iter_idx = retrohtr_node( tree, child_iter_idx )->parent;
1022 }
1023
1024 /* Set X to highest non-explicit ancestor. */
1025 retrohtr_node( tree, node_idx )->x =
1026 retrohtr_node( tree, child_iter_idx )->x + effect_style.LEFT;
1027 }
1028 if( mcss_prop_is_active_NOT_flag( effect_style.RIGHT, AUTO ) ) {
1029
1030 child_iter_idx = retrohtr_node( tree, node_idx )->parent;
1031 while( 0 <= retrohtr_node( tree, child_iter_idx )->parent ) {
1032 retrohtr_break_on_active_pos( child_iter_idx );
1033 child_iter_idx = retrohtr_node( tree, child_iter_idx )->parent;
1034 }
1035
1036 /* Set X to highest non-explicit ancestor. */
1037 retrohtr_node( tree, node_idx )->x =
1038 retrohtr_node( tree, child_iter_idx )->w -
1039 retrohtr_node( tree, node_idx )->w -
1040 effect_style.RIGHT;
1041 }
1042
1043 } else if(
1044 MCSS_DISPLAY_INLINE == effect_style.DISPLAY &&
1045 MCSS_DISPLAY_INLINE == prev_sibling_style->DISPLAY &&
1046 0 <= prev_sibling_idx
1047 ) {
1048 /* Place to the right of the previous sibling. */
1049 retrohtr_node( tree, node_idx )->x =
1050 retrohtr_node( tree, prev_sibling_idx )->x +
1051 retrohtr_node( tree, prev_sibling_idx )->w;
1052
1053 } else if( 0 <= retrohtr_node( tree, node_idx )->parent ) {
1054 retrohtr_node( tree, node_idx )->x = retrohtr_node_parent( tree, node_idx )->x;
1055 }
1056
1057 /* y */
1058
1059 /* TODO: Add margins of children? */
1060
1061 if( MCSS_POSITION_ABSOLUTE == effect_style.POSITION ) {
1062 /* This node is positioned absolutely. (Relatively) simple! */
1063
1064 if( mcss_prop_is_active_NOT_flag( effect_style.TOP, AUTO ) ) {
1065
1066 child_iter_idx = retrohtr_node( tree, node_idx )->parent;
1067 while( 0 <= retrohtr_node( tree, child_iter_idx )->parent ) {
1068 retrohtr_break_on_active_pos( child_iter_idx );
1069 child_iter_idx = retrohtr_node( tree, child_iter_idx )->parent;
1070 }
1071
1072 /* Set Y to highest non-explicit ancestor. */
1073 retrohtr_node( tree, node_idx )->y =
1074 retrohtr_node( tree, child_iter_idx )->y + effect_style.TOP;
1075 }
1076 if( mcss_prop_is_active_NOT_flag( effect_style.BOTTOM, AUTO ) ) {
1077
1078 child_iter_idx = retrohtr_node( tree, node_idx )->parent;
1079 while( 0 <= retrohtr_node( tree, child_iter_idx )->parent ) {
1080 retrohtr_break_on_active_pos( child_iter_idx );
1081 child_iter_idx = retrohtr_node( tree, child_iter_idx )->parent;
1082 }
1083
1084 /* Set Y to highest non-explicit ancestor. */
1085 retrohtr_node( tree, node_idx )->y =
1086 retrohtr_node( tree, child_iter_idx )->h -
1087 retrohtr_node( tree, node_idx )->h -
1088 effect_style.BOTTOM;
1089 }
1090
1091 } else if(
1092 MCSS_DISPLAY_INLINE == effect_style.DISPLAY &&
1093 MCSS_DISPLAY_INLINE == prev_sibling_style->DISPLAY &&
1094 0 <= prev_sibling_idx
1095 ) {
1096 /* Place to the right of the previous sibling. */
1097 retrohtr_node( tree, node_idx )->y = retrohtr_node( tree, prev_sibling_idx )->y;
1098
1099 } else if( 0 <= prev_sibling_idx ) {
1100 /* Place below the previous block sibling. */
1101
1102 /* TODO: We should probably use the tallest element on the prev sibling's
1103 * line, but that seems hard...
1104 */
1105
1106 retrohtr_node( tree, node_idx )->y =
1107 retrohtr_node( tree, prev_sibling_idx )->y +
1108 retrohtr_node( tree, prev_sibling_idx )->h;
1109
1110 } else if( 0 <= retrohtr_node( tree, node_idx )->parent ) {
1111 /* Position relative to other nodes. */
1112
1113 retrohtr_node( tree, node_idx )->y = retrohtr_node_parent( tree, node_idx )->y;
1114 }
1115
1116 /* margin-left, margin-right */
1117
1118 if(
1119 MCSS_POSITION_ABSOLUTE != retrohtr_node( tree, node_idx )->pos &&
1120 0 <= retrohtr_node( tree, node_idx )->parent &&
1121 mcss_prop_is_active_flag( effect_style.MARGIN_LEFT, AUTO ) &&
1122 mcss_prop_is_active_flag( effect_style.MARGIN_RIGHT, AUTO )
1123 ) {
1124 /* Center */
1125 retrohtr_node( tree, node_idx )->x =
1126 retrohtr_node_parent( tree, node_idx )->x +
1127 (retrohtr_node_parent( tree, node_idx )->w >> 1) -
1128 (retrohtr_node( tree, node_idx )->w >> 1);
1129
1130 } else if(
1131 0 <= retrohtr_node( tree, node_idx )->parent &&
1132 mcss_prop_is_active_flag( effect_style.MARGIN_LEFT, AUTO ) &&
1133 mcss_prop_is_active_NOT_flag( effect_style.MARGIN_RIGHT, AUTO )
1134 ) {
1135 /* Justify right. */
1136 /* TODO: Subtract padding below, as well. */
1137 retrohtr_node( tree, node_idx )->x =
1138 retrohtr_node_parent( tree, node_idx )->w -
1139 retrohtr_node( tree, node_idx )->w;
1140
1141 } else if( mcss_prop_is_active( effect_style.MARGIN_LEFT ) ) {
1142 /* Justify left. */
1143 retrohtr_node( tree, node_idx )->x += effect_style.MARGIN_LEFT;
1144 }
1145
1146 /* padding */
1147
1148 /* TODO: Padding is still broken. Needs more involved understanding of
1149 * where elements are in their container.
1150 */
1151
1152 debug_printf( 1, "(d: " SIZE_T_FMT ") node " SIZE_T_FMT " is on edge: %u",
1153 d, node_idx, retrohtr_node( tree, node_idx )->edge );
1154
1155 assert(
1156 0 == node_idx ||
1157 RETROHTR_EDGE_UNKNOWN != retrohtr_node( tree, node_idx )->edge );
1158
1159 if(
1160 RETROHTR_EDGE_LEFT ==
1161 (RETROHTR_EDGE_LEFT & retrohtr_node( tree, node_idx )->edge)
1162 ) {
1163 /* Try specific left padding first, then try general padding. */
1164 if( mcss_prop_is_active_NOT_flag( parent_style->PADDING_LEFT, AUTO ) ) {
1165 retrohtr_node( tree, node_idx )->x += parent_style->PADDING_LEFT;
1166 } else if( mcss_prop_is_active_NOT_flag( parent_style->PADDING, AUTO ) ) {
1167 retrohtr_node( tree, node_idx )->x += parent_style->PADDING;
1168 }
1169 }
1170
1171 if(
1172 RETROHTR_EDGE_TOP ==
1173 (RETROHTR_EDGE_TOP & retrohtr_node( tree, node_idx )->edge) &&
1174 /* Only apply padding to first node in line. The rest will pick it up. */
1175 RETROHTR_EDGE_LEFT ==
1176 (RETROHTR_EDGE_LEFT & retrohtr_node( tree, node_idx )->edge)
1177 ) {
1178 /* Try specific top padding first, then try general padding. */
1179 if( mcss_prop_is_active_NOT_flag( parent_style->PADDING_TOP, AUTO ) ) {
1180 retrohtr_node( tree, node_idx )->y += parent_style->PADDING_TOP;
1181 } else if( mcss_prop_is_active_NOT_flag( parent_style->PADDING, AUTO ) ) {
1182 retrohtr_node( tree, node_idx )->y += parent_style->PADDING;
1183 }
1184 }
1185
1186 /* color */
1187
1188 if( mcss_prop_is_active( effect_style.COLOR ) ) {
1189 retrohtr_node( tree, node_idx )->fg = effect_style.COLOR;
1190 }
1191
1192 if( mcss_prop_is_active( effect_style.BACKGROUND_COLOR ) ) {
1193 retrohtr_node( tree, node_idx )->bg = effect_style.BACKGROUND_COLOR;
1194 }
1195
1196 /* Figure out child positions. */
1197
1198 retrohtr_mark_edge_child_nodes( parser, tree, node_idx );
1199
1200 maug_mzero( &child_prev_sibling_style, sizeof( struct MCSS_STYLE ) );
1201 node_iter_idx = retrohtr_node( tree, node_idx )->first_child;
1202 while( 0 <= node_iter_idx ) {
1203 /* Mark child nodes on the edge so applying padding can be done. */
1204
1205 /* Figure out child node positioning. */
1206 retrohtr_tree_pos(
1207 parser, tree, &child_prev_sibling_style, &effect_style,
1208 node_iter_idx, d + 1 );
1209
1210 node_iter_idx = retrohtr_node( tree, node_iter_idx )->next_sibling;
1211 }
1212
1213 assert( !mdata_vector_is_locked( &(parser->tags) ) );
1214 mdata_vector_lock( &(parser->tags) );
1215 p_tag_iter = mdata_vector_get( &(parser->tags), tag_idx, union MHTML_TAG );
1216 assert( NULL != p_tag_iter );
1217
1218 if( MHTML_TAG_TYPE_INPUT == p_tag_iter->base.type ) {
1219 /* Feed the position back to the GUI control created during tree_size. */
1220 retval = retrogui_pos_ctl( &(tree->gui), node_idx,
1221 retrohtr_node_screen_x( tree, node_idx ),
1222 retrohtr_node_screen_y( tree, node_idx ),
1223 retrohtr_node( tree, node_idx )->w,
1224 retrohtr_node( tree, node_idx )->h );
1225 maug_cleanup_if_not_ok();
1226 }
1227
1228 debug_printf( RETROHTR_TRACE_LVL,
1229 "setting node " SIZE_T_FMT " dirty...", node_idx );
1230 retrohtr_node( tree, node_idx )->flags |= RETROHTR_NODE_FLAG_DIRTY;
1231
1232cleanup:
1233
1234 if( mdata_vector_is_locked( &(parser->tags) ) ) {
1235 mdata_vector_unlock( &(parser->tags) );
1236 }
1237
1238 /* We're done with the prev_sibling_style for this iter, so prepare it for
1239 * the next called by the parent!
1240 */
1241 if( NULL != prev_sibling_style ) {
1242 maug_mcpy(
1243 prev_sibling_style, &effect_style,
1244 sizeof( struct MCSS_STYLE ) );
1245 }
1246
1247 return retval;
1248}
1249
1250MERROR_RETVAL retrohtr_tree_draw(
1251 struct MHTML_PARSER* parser, struct RETROHTR_RENDER_TREE* tree,
1252 ssize_t node_idx, size_t d
1253) {
1254 char* strpool = NULL;
1255 union MHTML_TAG* p_tag = NULL;
1256 struct RETROHTR_RENDER_NODE* node = NULL;
1257 MERROR_RETVAL retval = MERROR_OK;
1258
1259 node = retrohtr_node( tree, node_idx );
1260
1261 if( NULL == node ) {
1262 return MERROR_OK;
1263 }
1264
1265 /* TODO: Multi-pass, draw absolute pos afterwards. */
1266
1267 if( 0 > node->tag ) {
1268 goto cleanup;
1269 }
1270
1271 if( RETROHTR_NODE_FLAG_DIRTY != (RETROHTR_NODE_FLAG_DIRTY & node->flags) ) {
1272 goto cleanup;
1273 }
1274
1275 assert( !mdata_vector_is_locked( &(parser->tags) ) );
1276 mdata_vector_lock( &(parser->tags) );
1277
1278 p_tag = mdata_vector_get( &(parser->tags), node->tag, union MHTML_TAG );
1279 if( NULL == p_tag ) {
1280 goto cleanup;
1281 }
1282
1283 /* Perform drawing. */
1284 if( MHTML_TAG_TYPE_TEXT == p_tag->base.type ) {
1285
1286 if(
1287 0 > p_tag->TEXT.content_idx ||
1288#ifdef RETROGXC_PRESENT
1289 0 > node->font_idx
1290#else
1291 (MAUG_MHANDLE)NULL == node->font_h
1292#endif /* RETROGXC_PRESENT */
1293 ) {
1294 goto cleanup;
1295 }
1296
1297 mdata_strpool_lock( &(parser->strpool), strpool );
1298
1299#ifdef RETROGXC_PRESENT
1300 retrogxc_string(
1301#else
1303#endif /* RETROGXC_PRESENT */
1304 NULL, node->fg,
1305 &(strpool[p_tag->TEXT.content_idx]), p_tag->TEXT.content_sz,
1306#ifdef RETROGXC_PRESENT
1307 node->font_idx,
1308#else
1309 node->font_h,
1310#endif /* RETROGXC_PRESENT */
1311 retrohtr_node_screen_x( tree, node_idx ),
1312 retrohtr_node_screen_y( tree, node_idx ),
1313 node->w, node->h, 0 );
1314
1315 mdata_strpool_unlock( &(parser->strpool), strpool );
1316
1317 } else if( MHTML_TAG_TYPE_BODY == p_tag->base.type ) {
1318
1319 debug_printf(
1320 RETROHTR_TRACE_LVL, "drawing BODY node " SIZE_T_FMT "...", node_idx );
1321
1322 /* Draw body BG. */
1323 if( RETROFLAT_COLOR_NULL != node->bg ) {
1324 retroflat_rect(
1325 NULL, node->bg,
1326 retrohtr_node_screen_x( tree, node_idx ),
1327 retrohtr_node_screen_y( tree, node_idx ),
1328 retrohtr_node( tree, node_idx )->w,
1329 retrohtr_node( tree, node_idx )->h,
1330 RETROFLAT_FLAGS_FILL );
1331 }
1332
1333 } else if( MHTML_TAG_TYPE_IMG == p_tag->base.type ) {
1334 /* Blit the image. */
1335
1336 if( !retroflat_bitmap_ok( &(retrohtr_node( tree, node_idx )->bitmap) ) ) {
1337 goto cleanup;
1338 }
1339
1340 debug_printf(
1341 RETROHTR_TRACE_LVL, "drawing IMG node " SIZE_T_FMT "...", node_idx );
1342
1344 NULL, &(retrohtr_node( tree, node_idx )->bitmap),
1345 0, 0,
1346 retrohtr_node_screen_x( tree, node_idx ),
1347 retrohtr_node_screen_y( tree, node_idx ),
1348 retroflat_bitmap_w( &(retrohtr_node( tree, node_idx )->bitmap) ),
1349 retroflat_bitmap_h( &(retrohtr_node( tree, node_idx )->bitmap) ),
1351 /* retrohtr_node( tree, node_idx )->w,
1352 retrohtr_node( tree, node_idx )->h */ );
1353
1354 } else if( MHTML_TAG_TYPE_INPUT == p_tag->base.type ) {
1355
1356 debug_printf(
1357 RETROHTR_TRACE_LVL, "setting tree GUI dirty..." );
1358
1359 tree->gui.flags |= RETROGUI_FLAGS_DIRTY;
1360
1361 } else {
1362 if( RETROFLAT_COLOR_NULL == node->bg ) {
1363 goto cleanup;
1364 }
1365
1366 debug_printf(
1367 RETROHTR_TRACE_LVL, "drawing xs node " SIZE_T_FMT "...",
1368 /* gc_mhtml_tag_names[mhtml_tag( parser,
1369 retrohtr_node( tree, node_idx )->tag )->base.type], */
1370 node_idx );
1371
1373 NULL, node->bg,
1374 retrohtr_node_screen_x( tree, node_idx ),
1375 retrohtr_node_screen_y( tree, node_idx ),
1376 node->w, node->h,
1378 }
1379
1380 node->flags &= ~RETROHTR_NODE_FLAG_DIRTY;
1381
1382cleanup:
1383
1384 if( mdata_vector_is_locked( &(parser->tags) ) ) {
1385 mdata_vector_unlock( &(parser->tags) );
1386 }
1387
1388 if( MERROR_OK != retval ) {
1389 error_printf( "failed drawing node: " SIZE_T_FMT, node_idx );
1390 }
1391
1392 /* Keep trying to render children, tho. */
1393
1394 retrohtr_tree_draw( parser, tree, node->first_child, d + 1 );
1395
1396 retrohtr_tree_draw( parser, tree, node->next_sibling, d );
1397
1398 /* If this is the root redraw call, redraw GUI elements. */
1399 if(
1400 0 == d &&
1401 RETROHTR_TREE_FLAG_GUI_ACTIVE ==
1402 (tree->flags & RETROHTR_TREE_FLAG_GUI_ACTIVE)
1403 ) {
1404 retrogui_redraw_ctls( &(tree->gui) );
1405 }
1406
1407 return retval;
1408}
1409
1410retrogui_idc_t retrohtr_tree_poll_ctls(
1411 struct RETROHTR_RENDER_TREE* tree,
1412 RETROFLAT_IN_KEY* input,
1413 struct RETROFLAT_INPUT* input_evt
1414) {
1415 retrogui_idc_t idc = 0;
1416 MERROR_RETVAL retval = MERROR_OK;
1417
1418 assert( retrohtr_tree_is_locked( tree ) );
1419
1420 if(
1421 RETROHTR_TREE_FLAG_GUI_ACTIVE !=
1422 (RETROHTR_TREE_FLAG_GUI_ACTIVE & tree->flags)
1423 ) {
1424 /* No GUI, so exit without even unlocking. */
1425 return 0;
1426 }
1427
1428 idc = retrogui_poll_ctls( &(tree->gui), input, input_evt );
1429
1430 if( 0 < idc ) {
1431 debug_printf(
1432 RETROHTR_TRACE_LVL, "setting node " SIZE_T_FMT " dirty...", idc );
1433 retrohtr_node( tree, idc )->flags |= RETROHTR_NODE_FLAG_DIRTY;
1434 }
1435
1436 if( MERROR_OK != retval ) {
1437 idc = 0;
1438 }
1439
1440 return idc;
1441}
1442
1443MERROR_RETVAL retrohtr_tree_dump(
1444 struct RETROHTR_RENDER_TREE* tree, struct MHTML_PARSER* parser,
1445 ssize_t node_idx, size_t d
1446) {
1447 size_t i = 0;
1448 char indents[31];
1449 union MHTML_TAG* p_tag_iter = NULL;
1450 MERROR_RETVAL retval = MERROR_OK;
1451
1452 if( 0 > node_idx ) {
1453 return MERROR_OK;
1454 }
1455
1456 assert( !mdata_vector_is_locked( &(parser->tags) ) );
1457 mdata_vector_lock( &(parser->tags) );
1458
1459 p_tag_iter = mdata_vector_get(
1460 &(parser->tags), tree->nodes[node_idx].tag, union MHTML_TAG );
1461 if( NULL == p_tag_iter ) {
1462 goto cleanup;
1463 }
1464
1465 /* Generate the indentation. */
1466 maug_mzero( indents, 30 );
1467 for( i = 0 ; d > i ; i++ ) {
1468 if( maug_strlen( indents ) >= 30 ) {
1469 break;
1470 }
1471 strcat( indents, " " );
1472 }
1473
1474 /* Print the debug line. */
1475 debug_printf(
1476 1,
1477 "%s" SSIZE_T_FMT " (tag %s): x: " SSIZE_T_FMT ", y: " SSIZE_T_FMT
1478 " (" SSIZE_T_FMT " x " SSIZE_T_FMT ") f: "
1479#ifdef RETROGXC_PRESENT
1480 SSIZE_T_FMT,
1481#else
1482 "%p",
1483#endif /* RETROGXC_PRESENT */
1484 indents, node_idx,
1485 0 <= tree->nodes[node_idx].tag ?
1486 gc_mhtml_tag_names[p_tag_iter->base.type] : "ROOT",
1487 tree->nodes[node_idx].x, tree->nodes[node_idx].y,
1488 tree->nodes[node_idx].w, tree->nodes[node_idx].h,
1489#ifdef RETROGXC_PRESENT
1490 tree->nodes[node_idx].font_idx
1491#else
1492 tree->nodes[node_idx].font_h
1493#endif /* RETROGXC_PRESENT */
1494 );
1495
1496 mdata_vector_unlock( &(parser->tags) );
1497
1498 retval = retrohtr_tree_dump(
1499 tree, parser, tree->nodes[node_idx].first_child, d + 1 );
1500 maug_cleanup_if_not_ok();
1501
1502 retval = retrohtr_tree_dump(
1503 tree, parser, tree->nodes[node_idx].next_sibling, d );
1504 maug_cleanup_if_not_ok();
1505
1506cleanup:
1507
1508 return retval;
1509}
1510
1511void retrohtr_tree_free( struct RETROHTR_RENDER_TREE* tree ) {
1512
1513 debug_printf( RETROHTR_TRACE_LVL, "freeing render nodes..." );
1514
1515 /* TODO: Free bitmaps from img! */
1516
1517 /* TODO: Free node->font_h! */
1518
1519 /* Free GUI if present. */
1520 if(
1521 RETROHTR_TREE_FLAG_GUI_ACTIVE ==
1522 (tree->flags & RETROHTR_TREE_FLAG_GUI_ACTIVE)
1523 ) {
1524 retrogui_destroy( &(tree->gui) );
1525 }
1526
1527 /* Unlock nodes before trying to free them. */
1528 retrohtr_tree_unlock( tree );
1529
1530 if( (MAUG_MHANDLE)NULL != tree->nodes_h ) {
1531 maug_mfree( tree->nodes_h );
1532 }
1533}
1534
1535MERROR_RETVAL retrohtr_tree_init( struct RETROHTR_RENDER_TREE* tree ) {
1536 MERROR_RETVAL retval = MERROR_OK;
1537
1538 maug_mzero( tree, sizeof( struct RETROHTR_RENDER_TREE ) );
1539
1540 /* Perform initial node allocation. */
1541 tree->nodes_sz_max = MHTML_PARSER_TAGS_INIT_SZ;
1542 debug_printf( RETROHTR_TRACE_LVL,
1543 "allocating " SIZE_T_FMT " nodes...", tree->nodes_sz_max );
1544 tree->nodes_h = maug_malloc(
1545 tree->nodes_sz_max, sizeof( struct RETROHTR_RENDER_NODE ) );
1546 maug_cleanup_if_null_alloc( MAUG_MHANDLE, tree->nodes_h );
1547
1548 /* XXX
1549 r.w_max = retroflat_screen_w();
1550 r.h_max = retroflat_screen_h(); */
1551
1552cleanup:
1553
1554 return retval;
1555}
1556
1557#endif /* RETROHTR_C */
1558
1559#endif /* !RETROHTR_H */
1560
int 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