source: gaycko/trunk/src/render/table.c @ 4762

Last change on this file since 4762 was 4762, checked in by Jean-Yves Lamoureux, 10 years ago
  • Almost correct td width handling
File size: 3.8 KB
Line 
1/*
2 *  Gaycko        Text mode web browser
3 *  Copyright (c) 2011 Jean-Yves Lamoureux <jylam@lnxscene.org>
4 *                All Rights Reserved
5 *
6 *  This library is free software. It comes without any warranty, to
7 *  the extent permitted by applicable law. You can redistribute it
8 *  and/or modify it under the terms of the Do What The Fuck You Want
9 *  To Public License, Version 2, as published by Sam Hocevar. See
10 *  http://sam.zoy.org/wtfpl/COPYING for more details.
11 */
12
13#include "renderer.h"
14void gaycko_fix_table(gNode *table) {
15        unsigned int tr = 0;
16    unsigned int e  = 0;
17    gNode *last_tr = NULL;
18    table->specific.table->tr_count = 0;
19   
20    /* Count TD by TR, and TR by TABLE */
21    for(tr = 0 ; tr < table->children_count; tr++) {
22        if(table->children[tr]->type == ELEM_TR) {
23            gaycko_fix_tr(table->children[tr]);
24            table->specific.table->tr_count++;
25            last_tr = table->children[tr];
26        }
27    }
28   
29    /* Scan TD's and set each correspondants to the max width */
30    unsigned int td = 0;
31   
32    for(td = 0; td < last_tr->specific.tr->td_count; td++) {
33        unsigned int biggest_td = 0;
34       
35        /* Find widest at a given X offset */
36        for(e = 0 ; e < table->children_count; e++) {
37            if(table->children[e]->type == ELEM_TR) {
38                unsigned int td_width = gaycko_get_trtd_width(table->children[e], td);
39                if(biggest_td < td_width) {
40                    biggest_td = td_width;
41                }
42            }
43        }
44        /* Set the others's width to the widest one */
45        for(e = 0 ; e < table->children_count; e++) {
46            if(table->children[e]->type == ELEM_TR) {
47                gaycko_set_trtd_width(table->children[e], td, biggest_td);
48            }
49        }
50    }
51   
52    table->properties->width = gaycko_get_tr_width(last_tr);
53    printf("Table %d wide\n", table->properties->width);
54   
55}
56
57
58
59unsigned int gaycko_get_tr_width(gNode *tr) {
60        unsigned int w = 0, td = 0;
61   
62    for(td = 0 ; td < tr->children_count; td++) {
63        if(tr->children[td]->type == ELEM_TD) {
64            unsigned int tw = gaycko_get_node_width(tr->children[td]);
65            w +=  tw;
66            printf("TD width %d\n", tw);
67        }
68    }
69    printf("So TR width %d\n", w);
70    return w;
71}
72
73unsigned int gaycko_get_node_width(gNode *td) {
74    unsigned int e = 0;
75        unsigned int w = 0;
76    for(e = 0 ; e < td->children_count; e++) {
77        w+=td->properties->width;
78    }
79    return w;
80}
81
82
83void gaycko_fix_tr(gNode *tr) {
84        unsigned int td = 0;
85    tr->specific.tr->td_count = 0;
86   
87    for(td = 0 ; td < tr->children_count; td++) {
88        if(tr->children[td]->type == ELEM_TD) {
89            tr->specific.tr->td_count++;
90        }
91    }
92}   
93
94unsigned int gaycko_get_trtd_width(gNode *tr, unsigned int td_index) {
95    unsigned int e = 0;
96    unsigned int td = 0;
97    for(e = 0 ; e < tr->children_count; e++) {
98        if(tr->children[e]->type == ELEM_TD) {
99            if(td == td_index) return tr->children[e]->properties->width;
100            td++;
101        }
102    }
103    return 0;
104}
105
106void gaycko_set_trtd_width(gNode *tr, unsigned td_index, unsigned int width) {
107    unsigned int e = 0;
108    unsigned int td = 0;
109    unsigned int add_width = 0;
110   
111    for(e = 0 ; e < tr->children_count; e++) {
112        if(tr->children[e]->type == ELEM_TD) {
113            if(td == td_index) {
114                add_width = width - tr->children[e]->properties->width;
115            }
116            td++;
117        }
118    }
119        td = 0;
120    for(e = 0 ; e < tr->children_count; e++) {
121        if(tr->children[e]->type == ELEM_TD) {
122            if(td == td_index) {
123               
124            } else if(td > td_index){
125                tr->children[e]->properties->width = width;
126                gaycko_add_node_x(tr->children[e], add_width);
127            }
128            td++;
129        }
130    }
131   
132   
133}
134
Note: See TracBrowser for help on using the repository browser.