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" |
---|
14 | |
---|
15 | void gaycko_get_position(gNode *node, gNode *parent) { |
---|
16 | unsigned int c = 0; |
---|
17 | unsigned int w = 0; |
---|
18 | unsigned int h = 0; |
---|
19 | unsigned int x = node->properties->x; |
---|
20 | unsigned int y = node->properties->y; |
---|
21 | |
---|
22 | for(c = 0; c < node->children_count; c++) { |
---|
23 | node->children[c]->properties->x = x; |
---|
24 | node->children[c]->properties->y = y; |
---|
25 | switch(node->children[c]->type) { |
---|
26 | case ELEM_TABLE: |
---|
27 | gaycko_get_position(node->children[c], parent); |
---|
28 | x+=node->children[c]->properties->width; |
---|
29 | if(node->children[c]->properties->width>w) { |
---|
30 | w = (node->children[c]->properties->width); |
---|
31 | } |
---|
32 | gaycko_fix_table(node->children[c]); |
---|
33 | break; |
---|
34 | |
---|
35 | case ELEM_TR: |
---|
36 | gaycko_get_position(node->children[c], parent); |
---|
37 | node->children[c]->properties->height+=1; |
---|
38 | x = 0; |
---|
39 | w+=node->children[c]->properties->width+1; |
---|
40 | break; |
---|
41 | |
---|
42 | case ELEM_TD: |
---|
43 | gaycko_get_position(node->children[c], parent); |
---|
44 | x+=node->children[c]->properties->width; |
---|
45 | if(node->children[c]->properties->width>w) { |
---|
46 | w = (node->children[c]->properties->width); |
---|
47 | } |
---|
48 | break; |
---|
49 | |
---|
50 | case ELEM_BR: |
---|
51 | node->children[c]->properties->height+=1; |
---|
52 | x = 0; |
---|
53 | w+=node->children[c]->properties->width+1; |
---|
54 | break; |
---|
55 | |
---|
56 | case ELEM_HR: |
---|
57 | node->children[c]->properties->height+=1; |
---|
58 | x = 0; |
---|
59 | w+=node->children[c]->properties->width+1; |
---|
60 | break; |
---|
61 | |
---|
62 | case ELEM_H1: |
---|
63 | case ELEM_H2: |
---|
64 | case ELEM_H3: |
---|
65 | node->children[c]->properties->height+=1; |
---|
66 | x = 0; |
---|
67 | w+=node->children[c]->properties->width+1; |
---|
68 | break; |
---|
69 | |
---|
70 | |
---|
71 | case ELEM_TEXT: |
---|
72 | if(parent && |
---|
73 | (parent->type!=ELEM_TR && parent->type!=ELEM_TABLE)) { |
---|
74 | if(node->children[c]->text) { |
---|
75 | node->children[c]->properties->width = strlen(node->children[c]->text); |
---|
76 | } else { |
---|
77 | node->children[c]->properties->width = 0; |
---|
78 | } |
---|
79 | } else { |
---|
80 | node->children[c]->properties->width = 0; |
---|
81 | node->children[c]->properties->height = 0; |
---|
82 | } |
---|
83 | x+=node->children[c]->properties->width; |
---|
84 | if(node->children[c]->properties->width>w) { |
---|
85 | w = (node->children[c]->properties->width); |
---|
86 | } |
---|
87 | break; |
---|
88 | |
---|
89 | |
---|
90 | default: |
---|
91 | gaycko_get_position(node->children[c], node); |
---|
92 | x+=node->children[c]->properties->width; |
---|
93 | if(node->children[c]->properties->width>w) { |
---|
94 | w = (node->children[c]->properties->width); |
---|
95 | } |
---|
96 | |
---|
97 | break; |
---|
98 | } |
---|
99 | |
---|
100 | y+=node->children[c]->properties->height; |
---|
101 | h+=node->children[c]->properties->height; |
---|
102 | |
---|
103 | } |
---|
104 | |
---|
105 | node->properties->width = w; |
---|
106 | node->properties->height = h; |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | void gaycko_fix_table(gNode *table) { |
---|
111 | unsigned int tr = 0; |
---|
112 | unsigned int e = 0; |
---|
113 | gNode *last_tr = NULL; |
---|
114 | table->specific.table->tr_count = 0; |
---|
115 | |
---|
116 | /* Count TD by TR, and TR by TABLE */ |
---|
117 | for(tr = 0 ; tr < table->children_count; tr++) { |
---|
118 | if(table->children[tr]->type == ELEM_TR) { |
---|
119 | gaycko_fix_tr(table->children[tr]); |
---|
120 | table->specific.table->tr_count++; |
---|
121 | last_tr = table->children[tr]; |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | |
---|
126 | /* Scan TD's and set each correspondants to the max width */ |
---|
127 | unsigned int td = 0; |
---|
128 | for(td = 0; td < last_tr->specific.tr->td_count; td++) { |
---|
129 | unsigned int biggest_td = 0; |
---|
130 | |
---|
131 | for(e = 0 ; e < table->children_count; e++) { |
---|
132 | if(table->children[e]->type == ELEM_TR) { |
---|
133 | unsigned int td_width = gaycko_get_td_width(table->children[e], td); |
---|
134 | if(biggest_td < td_width) { |
---|
135 | biggest_td = td_width; |
---|
136 | } |
---|
137 | } |
---|
138 | } |
---|
139 | for(e = 0 ; e < table->children_count; e++) { |
---|
140 | if(table->children[e]->type == ELEM_TR) { |
---|
141 | gaycko_set_td_width(table->children[e], td, biggest_td); |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | } |
---|
146 | |
---|
147 | } |
---|
148 | |
---|
149 | void gaycko_fix_tr(gNode *tr) { |
---|
150 | unsigned int td = 0; |
---|
151 | tr->specific.tr->td_count = 0; |
---|
152 | |
---|
153 | for(td = 0 ; td < tr->children_count; td++) { |
---|
154 | if(tr->children[td]->type == ELEM_TD) { |
---|
155 | tr->specific.tr->td_count++; |
---|
156 | } |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | unsigned int gaycko_get_td_width(gNode *tr, unsigned int td_index) { |
---|
161 | unsigned int e = 0; |
---|
162 | unsigned int td = 0; |
---|
163 | for(e = 0 ; e < tr->children_count; e++) { |
---|
164 | if(tr->children[e]->type == ELEM_TD) { |
---|
165 | if(td == td_index) return tr->children[e]->properties->width; |
---|
166 | td++; |
---|
167 | } |
---|
168 | } |
---|
169 | return 0; |
---|
170 | } |
---|
171 | |
---|
172 | void gaycko_set_td_width(gNode *tr, unsigned td_index, unsigned int width) { |
---|
173 | unsigned int e = 0; |
---|
174 | unsigned int td = 0; |
---|
175 | unsigned int add_width = 0; |
---|
176 | |
---|
177 | for(e = 0 ; e < tr->children_count; e++) { |
---|
178 | if(tr->children[e]->type == ELEM_TD) { |
---|
179 | if(td == td_index) { |
---|
180 | add_width = width - tr->children[e]->properties->width; |
---|
181 | } else { |
---|
182 | gaycko_add_node_x(tr->children[e], add_width); |
---|
183 | } |
---|
184 | td++; |
---|
185 | } |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | |
---|
190 | void gaycko_add_node_x(gNode *node, unsigned int add) { |
---|
191 | |
---|
192 | unsigned int e = 0; |
---|
193 | for(e = 0 ; e < node->children_count; e++) { |
---|
194 | node->children[e]->properties->x +=add; |
---|
195 | } |
---|
196 | } |
---|
197 | |
---|
198 | |
---|
199 | |
---|
200 | |
---|
201 | |
---|
202 | |
---|