/[public]/psiconv/trunk/lib/psiconv/generate_layout.c
ViewVC logotype

Contents of /psiconv/trunk/lib/psiconv/generate_layout.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 168 - (show annotations)
Tue Nov 25 17:57:05 2003 UTC (20 years, 4 months ago) by frodo
File MIME type: text/plain
File size: 16030 byte(s)
(Frodo) config stuff and image generation stuff

* All parse and generate functions have a new config parameter
* New files configuration.[ch] in the psiconv lib
* Some image generation stuff (not ready, but won't do any harm)

1 /*
2 generate_layout.c - Part of psiconv, a PSION 5 file formats converter
3 Copyright (c) 2000 Frodo Looijaard <frodol@dds.nl>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <string.h>
21
22 #include "config.h"
23 #include "compat.h"
24
25 #include "generate_routines.h"
26 #include "error.h"
27
28 #ifdef DMALLOC
29 #include <dmalloc.h>
30 #endif
31
32
33 int psiconv_write_color(const psiconv_config config, psiconv_buffer buf, const psiconv_color value)
34 {
35 int res;
36 if (!value) {
37 psiconv_warn(config,0,psiconv_buffer_length(buf),"Null color");
38 return -PSICONV_E_GENERATE;
39 }
40 if ((res = psiconv_write_u8(config,buf,value->red)))
41 return res;
42 if ((res = psiconv_write_u8(config,buf,value->green)))
43 return res;
44 return psiconv_write_u8(config,buf,value->blue);
45 }
46
47 int psiconv_write_font(const psiconv_config config, psiconv_buffer buf, const psiconv_font value)
48 {
49 int res,i;
50 if (!value) {
51 psiconv_warn(config,0,psiconv_buffer_length(buf),"Null font");
52 return -PSICONV_E_GENERATE;
53 }
54 if ((res = psiconv_write_u8(config,buf,strlen(value->name)+1)))
55 return res;
56 for (i = 0; i < strlen(value->name); i++)
57 if ((res = psiconv_write_u8(config,buf,value->name[i])))
58 return res;
59 return psiconv_write_u8(config,buf,value->screenfont);
60 }
61
62 int psiconv_write_border(const psiconv_config config, psiconv_buffer buf, const psiconv_border value)
63 {
64 int res;
65
66 if (!value) {
67 psiconv_warn(config,0,psiconv_buffer_length(buf),"Null border");
68 return -PSICONV_E_GENERATE;
69 }
70 if (value->kind > psiconv_border_dotdotdashed)
71 psiconv_warn(config,0,psiconv_buffer_length(buf),
72 "Unknown border kind (%d); assuming none",value->kind);
73 if ((res =psiconv_write_u8(config,buf,value->kind == psiconv_border_none?0:
74 value->kind == psiconv_border_solid?1:
75 value->kind == psiconv_border_double?2:
76 value->kind == psiconv_border_dotted?3:
77 value->kind == psiconv_border_dashed?4:
78 value->kind == psiconv_border_dotdashed?5:
79 value->kind == psiconv_border_dotdotdashed?6:
80 0)))
81 return res;
82 if ((res = psiconv_write_size(config,buf,(value->kind == psiconv_border_solid) ||
83 (value->kind == psiconv_border_double) ?
84 value->thickness:1.0/20.0)))
85 return res;
86 if ((res = psiconv_write_color(config,buf,value->color)))
87 return res;
88 // Unknown byte
89 return psiconv_write_u8(config,buf,1);
90 }
91
92 int psiconv_write_bullet(const psiconv_config config, psiconv_buffer buf, const psiconv_bullet value)
93 {
94 int res;
95 psiconv_buffer extra_buf;
96 if (!value) {
97 psiconv_warn(config,0,psiconv_buffer_length(buf),"Null bullet");
98 return -PSICONV_E_GENERATE;
99 }
100
101 if (!(extra_buf = psiconv_buffer_new()))
102 return -PSICONV_E_NOMEM;
103 if ((res = psiconv_write_size(config,extra_buf,value->font_size)))
104 goto ERROR;
105 if ((res = psiconv_write_u8(config,extra_buf,value->character)))
106 goto ERROR;
107 if ((res = psiconv_write_bool(config,extra_buf,value->indent)))
108 goto ERROR;
109 if ((res = psiconv_write_color(config,extra_buf,value->color)))
110 goto ERROR;
111 if ((res = psiconv_write_font(config,extra_buf,value->font)))
112 goto ERROR;
113
114 if ((res = psiconv_write_u8(config,buf,psiconv_buffer_length(extra_buf))))
115 goto ERROR;
116 res = psiconv_buffer_concat(buf,extra_buf);
117
118 ERROR:
119 psiconv_buffer_free(extra_buf);
120 return res;
121 }
122
123 int psiconv_write_tab(const psiconv_config config, psiconv_buffer buf,psiconv_tab value)
124 {
125 int res;
126 if (!value) {
127 psiconv_warn(config,0,psiconv_buffer_length(buf),"Null tab");
128 return -PSICONV_E_GENERATE;
129 }
130 if ((res = psiconv_write_length(config,buf,value->location)))
131 return res;
132 if ((value->kind != psiconv_tab_left) &&
133 (value->kind != psiconv_tab_right) &&
134 (value->kind != psiconv_tab_centre))
135 psiconv_warn(config,0,psiconv_buffer_length(buf),
136 "Unknown tab kind (%d); assuming left",value->kind);
137 return psiconv_write_u8(config,buf, value->kind == psiconv_tab_right?2:
138 value->kind == psiconv_tab_centre?3:1);
139 }
140
141 int psiconv_write_paragraph_layout_list(const psiconv_config config,
142 psiconv_buffer buf,
143 psiconv_paragraph_layout value,
144 psiconv_paragraph_layout base)
145 {
146 int res,i;
147 psiconv_buffer extra_buf;
148 psiconv_tab tab;
149
150 if (!value) {
151 psiconv_warn(config,0,psiconv_buffer_length(buf),"Null paragraph layout list");
152 return -PSICONV_E_GENERATE;
153 }
154 if (!(extra_buf = psiconv_buffer_new()))
155 return -PSICONV_E_NOMEM;
156
157 if (!base || psiconv_compare_color(base->back_color,value->back_color)) {
158 if ((res = psiconv_write_u8(config,extra_buf,0x01)))
159 goto ERROR;
160 if ((res = psiconv_write_color(config,extra_buf,value->back_color)))
161 goto ERROR;
162 }
163
164 if (!base || (value->indent_left != base->indent_left)) {
165 if ((res = psiconv_write_u8(config,extra_buf,0x02)))
166 goto ERROR;
167 if ((res = psiconv_write_length(config,extra_buf,value->indent_left)))
168 goto ERROR;
169 }
170
171 if (!base || (value->indent_right != base->indent_right)) {
172 if ((res = psiconv_write_u8(config,extra_buf,0x03)))
173 goto ERROR;
174 if ((res = psiconv_write_length(config,extra_buf,value->indent_right)))
175 goto ERROR;
176 }
177
178 if (!base || (value->indent_first != base->indent_first)) {
179 if ((res = psiconv_write_u8(config,extra_buf,0x04)))
180 goto ERROR;
181 if ((res = psiconv_write_length(config,extra_buf,value->indent_first)))
182 goto ERROR;
183 }
184
185 if (!base || (value->justify_hor != base->justify_hor)) {
186 if ((res = psiconv_write_u8(config,extra_buf,0x05)))
187 goto ERROR;
188 if ((value->justify_hor < psiconv_justify_left) ||
189 (value->justify_hor > psiconv_justify_full))
190 psiconv_warn(config,0,psiconv_buffer_length(buf),
191 "Unknown horizontal justify (%d); assuming left",
192 value->justify_hor);
193 if ((res = psiconv_write_u8(config,extra_buf,
194 value->justify_hor == psiconv_justify_centre?1:
195 value->justify_hor == psiconv_justify_right?2:
196 value->justify_hor == psiconv_justify_full?3:0)))
197 goto ERROR;
198 }
199
200 if (!base || (value->justify_ver != base->justify_ver)) {
201 if ((res = psiconv_write_u8(config,extra_buf,0x06)))
202 goto ERROR;
203 if ((value->justify_ver < psiconv_justify_top) ||
204 (value->justify_ver > psiconv_justify_bottom))
205 psiconv_warn(config,0,psiconv_buffer_length(buf),
206 "Unknown vertical justify (%d); assuming middle",
207 value->justify_ver);
208 if ((res = psiconv_write_u8(config,extra_buf,
209 value->justify_ver == psiconv_justify_centre?1:
210 value->justify_ver == psiconv_justify_right?2:0)))
211 goto ERROR;
212 }
213
214 if (!base || (value->linespacing != base->linespacing)) {
215 if ((res = psiconv_write_u8(config,extra_buf,0x07)))
216 goto ERROR;
217 if ((res = psiconv_write_size(config,extra_buf,value->linespacing)))
218 goto ERROR;
219 }
220
221 if (!base || (value->linespacing_exact != base->linespacing_exact)) {
222 if ((res = psiconv_write_u8(config,extra_buf,0x08)))
223 goto ERROR;
224 if ((res = psiconv_write_bool(config,extra_buf,value->linespacing_exact)))
225 goto ERROR;
226 }
227
228 if (!base || (value->space_above != base->space_above)) {
229 if ((res = psiconv_write_u8(config,extra_buf,0x09)))
230 goto ERROR;
231 if ((res = psiconv_write_size(config,extra_buf,value->space_above)))
232 goto ERROR;
233 }
234
235 if (!base || (value->space_below != base->space_below)) {
236 if ((res = psiconv_write_u8(config,extra_buf,0x0a)))
237 goto ERROR;
238 if ((res = psiconv_write_size(config,extra_buf,value->space_below)))
239 goto ERROR;
240 }
241
242 if (!base || (value->keep_together != base->keep_together)) {
243 if ((res = psiconv_write_u8(config,extra_buf,0x0b)))
244 goto ERROR;
245 if ((res = psiconv_write_bool(config,extra_buf,value->keep_together)))
246 goto ERROR;
247 }
248
249 if (!base || (value->keep_with_next != base->keep_with_next)) {
250 if ((res = psiconv_write_u8(config,extra_buf,0x0c)))
251 goto ERROR;
252 if ((res = psiconv_write_bool(config,extra_buf,value->keep_with_next)))
253 goto ERROR;
254 }
255
256 if (!base || (value->on_next_page != base->on_next_page)) {
257 if ((res = psiconv_write_u8(config,extra_buf,0x0d)))
258 goto ERROR;
259 if ((res = psiconv_write_bool(config,extra_buf,value->on_next_page)))
260 goto ERROR;
261 }
262
263 if (!base || (value->no_widow_protection != base->no_widow_protection)) {
264 if ((res = psiconv_write_u8(config,extra_buf,0x0e)))
265 goto ERROR;
266 if ((res = psiconv_write_bool(config,extra_buf,value->no_widow_protection)))
267 goto ERROR;
268 }
269
270 if (!base || (value->wrap_to_fit_cell != base->wrap_to_fit_cell)) {
271 if ((res = psiconv_write_u8(config,extra_buf,0x0f)))
272 goto ERROR;
273 if ((res = psiconv_write_bool(config,extra_buf,value->wrap_to_fit_cell)))
274 goto ERROR;
275 }
276
277 if (!base || (value->border_distance != base->border_distance)) {
278 if ((res = psiconv_write_u8(config,extra_buf,0x10)))
279 goto ERROR;
280 if ((res = psiconv_write_length(config,extra_buf,value->border_distance)))
281 goto ERROR;
282 }
283
284 if (!base || psiconv_compare_border(value->top_border,base->top_border)) {
285 if ((res = psiconv_write_u8(config,extra_buf,0x11)))
286 goto ERROR;
287 if ((res = psiconv_write_border(config,extra_buf,value->top_border)))
288 goto ERROR;
289 }
290
291 if (!base || psiconv_compare_border(value->bottom_border,
292 base->bottom_border)) {
293 if ((res = psiconv_write_u8(config,extra_buf,0x12)))
294 goto ERROR;
295 if ((res = psiconv_write_border(config,extra_buf,value->bottom_border)))
296 goto ERROR;
297 }
298
299 if (!base || psiconv_compare_border(value->left_border,
300 base->left_border)) {
301 if ((res = psiconv_write_u8(config,extra_buf,0x13)))
302 goto ERROR;
303 if ((res = psiconv_write_border(config,extra_buf,value->left_border)))
304 goto ERROR;
305 }
306
307 if (!base || psiconv_compare_border(value->right_border,
308 base->right_border)) {
309 if ((res = psiconv_write_u8(config,extra_buf,0x14)))
310 goto ERROR;
311 if ((res = psiconv_write_border(config,extra_buf,value->right_border)))
312 goto ERROR;
313 }
314
315 if (!base || psiconv_compare_bullet(value->bullet,
316 base->bullet)) {
317 if ((res = psiconv_write_u8(config,extra_buf,0x15)))
318 goto ERROR;
319 if ((res = psiconv_write_bullet(config,extra_buf,value->bullet)))
320 goto ERROR;
321 }
322
323 if (!value->tabs || !value->tabs->extras) {
324 psiconv_warn(config,0,psiconv_buffer_length(buf),"Null tabs");
325 res = -PSICONV_E_GENERATE;
326 goto ERROR;
327 }
328
329 /* It is not entirely clear how tabs are inherited. For now, I assume
330 if there is any difference at all, we will have to generate both
331 the normal tab-interval, and all specific tabs */
332 if (!base || psiconv_compare_all_tabs(value->tabs,base->tabs)) {
333 if ((res = psiconv_write_u8(config,extra_buf,0x16)))
334 goto ERROR;
335 if ((res = psiconv_write_length(config,extra_buf,value->tabs->normal)))
336 goto ERROR;
337 for (i = 0; i < psiconv_list_length(value->tabs->extras); i++) {
338 if (!(tab = psiconv_list_get(value->tabs->extras,i))) {
339 psiconv_warn(config,0,psiconv_buffer_length(buf),"Massive memory corruption");
340 res = -PSICONV_E_NOMEM;
341 goto ERROR;
342 }
343 if ((res = psiconv_write_u8(config,extra_buf,0x17)))
344 goto ERROR;
345 if ((res = psiconv_write_tab(config,extra_buf,tab)))
346 goto ERROR;
347 }
348 }
349
350 if ((res = psiconv_write_u32(config,buf,psiconv_buffer_length(extra_buf))))
351 goto ERROR;
352
353 res = psiconv_buffer_concat(buf,extra_buf);
354
355 ERROR:
356 psiconv_buffer_free(extra_buf);
357 return res;
358 }
359
360 int psiconv_write_character_layout_list(const psiconv_config config,
361 psiconv_buffer buf,
362 psiconv_character_layout value,
363 psiconv_character_layout base)
364 {
365 int res;
366 psiconv_buffer extra_buf;
367 if (!value) {
368 psiconv_warn(config,0,psiconv_buffer_length(buf),"Null character layout list");
369 return -PSICONV_E_GENERATE;
370 }
371 if (!(extra_buf = psiconv_buffer_new()))
372 return -PSICONV_E_NOMEM;
373
374 if (!base || psiconv_compare_color(base->color,value->color)) {
375 if ((res = psiconv_write_u8(config,extra_buf,0x19)))
376 goto ERROR;
377 if ((res = psiconv_write_color(config,extra_buf,value->color)))
378 goto ERROR;
379 }
380
381 if (!base || psiconv_compare_color(base->back_color,value->back_color)) {
382 if ((res = psiconv_write_u8(config,extra_buf,0x1a)))
383 goto ERROR;
384 if ((res = psiconv_write_color(config,extra_buf,value->back_color)))
385 goto ERROR;
386 }
387
388 if (!base || (value->font_size != base->font_size)) {
389 if ((res = psiconv_write_u8(config,extra_buf,0x1c)))
390 goto ERROR;
391 if ((res = psiconv_write_size(config,extra_buf,value->font_size)))
392 goto ERROR;
393 }
394
395 if (!base || (value->italic != base->italic)) {
396 if ((res = psiconv_write_u8(config,extra_buf,0x1d)))
397 goto ERROR;
398 if ((res = psiconv_write_bool(config,extra_buf,value->italic)))
399 goto ERROR;
400 }
401
402 if (!base || (value->bold != base->bold)) {
403 if ((res = psiconv_write_u8(config,extra_buf,0x1e)))
404 goto ERROR;
405 if ((res = psiconv_write_bool(config,extra_buf,value->bold)))
406 goto ERROR;
407 }
408
409 if (!base || (value->super_sub != base->super_sub)) {
410 if ((value->super_sub != psiconv_superscript) &&
411 (value->super_sub != psiconv_subscript) &&
412 (value->super_sub != psiconv_normalscript))
413 psiconv_warn(config,0,psiconv_buffer_length(buf),
414 "Unknown supersubscript (%d); assuming normal",
415 value->super_sub);
416 if ((res = psiconv_write_u8(config,extra_buf,0x1f)))
417 goto ERROR;
418 if ((res = psiconv_write_u8(config,extra_buf,
419 value->super_sub == psiconv_superscript?1:
420 value->super_sub == psiconv_subscript?2:0)))
421 goto ERROR;
422 }
423
424 if (!base || (value->underline != base->underline)) {
425 if ((res = psiconv_write_u8(config,extra_buf,0x20)))
426 goto ERROR;
427 if ((res = psiconv_write_bool(config,extra_buf,value->underline)))
428 goto ERROR;
429 }
430
431 if (!base || (value->strikethrough != base->strikethrough)) {
432 if ((res = psiconv_write_u8(config,extra_buf,0x21)))
433 goto ERROR;
434 if ((res = psiconv_write_bool(config,extra_buf,value->strikethrough)))
435 goto ERROR;
436 }
437
438 if (!base || psiconv_compare_font(base->font,value->font)) {
439 if ((res = psiconv_write_u8(config,extra_buf,0x22)))
440 goto ERROR;
441 if ((res = psiconv_write_font(config,extra_buf,value->font)))
442 goto ERROR;
443 }
444
445 if ((res = psiconv_write_u32(config,buf,psiconv_buffer_length(extra_buf))))
446 goto ERROR;
447
448 res = psiconv_buffer_concat(buf,extra_buf);
449
450 ERROR:
451 psiconv_buffer_free(extra_buf);
452 return res;
453 }

frodo@frodo.looijaard.name
ViewVC Help
Powered by ViewVC 1.1.26