/[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 231 - (show annotations)
Wed Feb 25 20:57:10 2004 UTC (20 years, 1 month ago) by frodo
File MIME type: text/plain
File size: 18271 byte(s)
(Frodo) Progress information while generating update

Much better progress information now, and better error reporting.

1 /*
2 generate_layout.c - Part of psiconv, a PSION 5 file formats converter
3 Copyright (c) 2000-2004 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 "config.h"
21 #include "compat.h"
22
23 #include <string.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,
34 int lev, const psiconv_color value)
35 {
36 int res;
37
38 psiconv_progress(config,lev,0,"Writing color");
39
40 if (!value) {
41 psiconv_error(config,lev,0,"Null color");
42 res = -PSICONV_E_GENERATE;
43 goto ERROR;
44 }
45 if ((res = psiconv_write_u8(config,buf,lev+1,value->red)))
46 goto ERROR;
47 if ((res = psiconv_write_u8(config,buf,lev+1,value->green)))
48 goto ERROR;
49 if ((res = psiconv_write_u8(config,buf,lev+1,value->blue)))
50 goto ERROR;
51 psiconv_progress(config,lev,0,"End of color");
52 return 0;
53
54 ERROR:
55 psiconv_error(config,lev,0,"Writing of color failed");
56 return res;
57 }
58
59 int psiconv_write_font(const psiconv_config config, psiconv_buffer buf,
60 int lev, const psiconv_font value)
61 {
62 int res,len;
63
64 psiconv_progress(config,lev,0,"Writing font");
65 if (!value) {
66 psiconv_error(config,0,psiconv_buffer_length(buf),"Null font");
67 res = -PSICONV_E_GENERATE;
68 goto ERROR;
69 }
70 len = psiconv_unicode_strlen(value->name);
71 if ((res = psiconv_write_u8(config,buf,lev+1,len+1)))
72 goto ERROR;
73 if ((res = psiconv_write_charlist(config,buf,lev+1,value->name)))
74 goto ERROR;
75 if ((res = psiconv_write_u8(config,buf,lev+1,value->screenfont)))
76 goto ERROR;
77 psiconv_progress(config,lev,0,"End of font");
78 return 0;
79
80 ERROR:
81 psiconv_error(config,lev,0,"Writing of font failed");
82 return res;
83 }
84
85 int psiconv_write_border(const psiconv_config config, psiconv_buffer buf,int lev, const psiconv_border value)
86 {
87 int res;
88
89 psiconv_progress(config,lev,0,"Writing border");
90
91 if (!value) {
92 psiconv_error(config,lev,0,"Null border");
93 res = -PSICONV_E_GENERATE;
94 goto ERROR;
95 }
96 if (value->kind > psiconv_border_dotdotdashed)
97 psiconv_warn(config,lev,0,
98 "Unknown border kind (%d); assuming none",value->kind);
99 if ((res =psiconv_write_u8(config,buf,lev+1,
100 value->kind == psiconv_border_none?0:
101 value->kind == psiconv_border_solid?1:
102 value->kind == psiconv_border_double?2:
103 value->kind == psiconv_border_dotted?3:
104 value->kind == psiconv_border_dashed?4:
105 value->kind == psiconv_border_dotdashed?5:
106 value->kind == psiconv_border_dotdotdashed?6:
107 0)))
108 goto ERROR;
109 if ((res = psiconv_write_size(config,buf,lev+1,
110 (value->kind == psiconv_border_solid) ||
111 (value->kind == psiconv_border_double) ?
112 value->thickness:1.0/20.0)))
113 goto ERROR;
114 if ((res = psiconv_write_color(config,buf,lev+1,value->color)))
115 goto ERROR;
116 /* Unknown byte */
117 if ((res = psiconv_write_u8(config,buf,lev+1,1)))
118 goto ERROR;
119 psiconv_progress(config,lev,0,"End of border");
120 return 0;
121
122 ERROR:
123 psiconv_error(config,lev,0,"Writing of border failed");
124 return res;
125 }
126
127 int psiconv_write_bullet(const psiconv_config config, psiconv_buffer buf,int lev, const psiconv_bullet value)
128 {
129 int res;
130 psiconv_buffer extra_buf;
131
132 psiconv_progress(config,lev,0,"Writing bullet");
133
134 if (!value) {
135 psiconv_error(config,0,psiconv_buffer_length(buf),"Null bullet");
136 res = -PSICONV_E_GENERATE;
137 goto ERROR1;
138 }
139
140 if (!(extra_buf = psiconv_buffer_new())) {
141 res = -PSICONV_E_NOMEM;
142 goto ERROR1;
143 }
144 if ((res = psiconv_write_size(config,extra_buf,lev+1,value->font_size)))
145 goto ERROR2;
146 if ((res = psiconv_unicode_write_char(config,extra_buf,lev+1,
147 value->character)))
148 goto ERROR2;
149 if ((res = psiconv_write_bool(config,extra_buf,lev+1,value->indent)))
150 goto ERROR2;
151 if ((res = psiconv_write_color(config,extra_buf,lev+1,value->color)))
152 goto ERROR2;
153 if ((res = psiconv_write_font(config,extra_buf,lev+1,value->font)))
154 goto ERROR2;
155
156 if ((res = psiconv_write_u8(config,buf,lev+1,psiconv_buffer_length(extra_buf))))
157 goto ERROR2;
158 res = psiconv_buffer_concat(buf,extra_buf);
159
160 ERROR2:
161 psiconv_buffer_free(extra_buf);
162 ERROR1:
163 if (res)
164 psiconv_error(config,lev,0,"Writing of bullet failed");
165 else
166 psiconv_progress(config,lev,0,"End of bullet");
167 return res;
168 }
169
170 int psiconv_write_tab(const psiconv_config config, psiconv_buffer buf,int lev, psiconv_tab value)
171 {
172 int res;
173
174 psiconv_progress(config,lev,0,"Writing tab");
175
176 if (!value) {
177 psiconv_error(config,lev,0,"Null tab");
178 res = -PSICONV_E_GENERATE;
179 goto ERROR;
180 }
181 if ((res = psiconv_write_length(config,buf,lev+1,value->location)))
182 goto ERROR;
183 if ((value->kind != psiconv_tab_left) &&
184 (value->kind != psiconv_tab_right) &&
185 (value->kind != psiconv_tab_centre))
186 psiconv_warn(config,lev,0,
187 "Unknown tab kind (%d); assuming left",value->kind);
188 if ((res = psiconv_write_u8(config,buf,lev+1,
189 value->kind == psiconv_tab_right?2:
190 value->kind == psiconv_tab_centre?3:1)))
191 goto ERROR;
192 psiconv_progress(config,lev,0,"End of tab");
193 return 0;
194 ERROR:
195 psiconv_error(config,lev,0,"Writing of tab failed");
196 return res;
197 }
198
199 int psiconv_write_paragraph_layout_list(const psiconv_config config,
200 psiconv_buffer buf,int lev,
201 psiconv_paragraph_layout value,
202 psiconv_paragraph_layout base)
203 {
204 int res,i;
205 psiconv_buffer extra_buf;
206 psiconv_tab tab;
207
208 psiconv_progress(config,lev,0,"Writing paragraph layout list");
209
210 if (!value) {
211 psiconv_error(config,lev,0,"Null paragraph layout list");
212 res = -PSICONV_E_GENERATE;
213 goto ERROR1;
214 }
215 if (!(extra_buf = psiconv_buffer_new())) {
216 res = -PSICONV_E_NOMEM;
217 goto ERROR1;
218 }
219
220 if (!base || psiconv_compare_color(base->back_color,value->back_color)) {
221 if ((res = psiconv_write_u8(config,extra_buf,lev+1,0x01)))
222 goto ERROR2;
223 if ((res = psiconv_write_color(config,extra_buf,lev+1,value->back_color)))
224 goto ERROR2;
225 }
226
227 if (!base || (value->indent_left != base->indent_left)) {
228 if ((res = psiconv_write_u8(config,extra_buf,lev+1,0x02)))
229 goto ERROR2;
230 if ((res = psiconv_write_length(config,extra_buf,lev+1,value->indent_left)))
231 goto ERROR2;
232 }
233
234 if (!base || (value->indent_right != base->indent_right)) {
235 if ((res = psiconv_write_u8(config,extra_buf,lev+1,0x03)))
236 goto ERROR2;
237 if ((res = psiconv_write_length(config,extra_buf,lev+1,value->indent_right)))
238 goto ERROR2;
239 }
240
241 if (!base || (value->indent_first != base->indent_first)) {
242 if ((res = psiconv_write_u8(config,extra_buf,lev+1,0x04)))
243 goto ERROR2;
244 if ((res = psiconv_write_length(config,extra_buf,lev+1,value->indent_first)))
245 goto ERROR2;
246 }
247
248 if (!base || (value->justify_hor != base->justify_hor)) {
249 if ((res = psiconv_write_u8(config,extra_buf,lev+1,0x05)))
250 goto ERROR2;
251 if ((value->justify_hor < psiconv_justify_left) ||
252 (value->justify_hor > psiconv_justify_full))
253 psiconv_warn(config,lev,0,
254 "Unknown horizontal justify (%d); assuming left",
255 value->justify_hor);
256 if ((res = psiconv_write_u8(config,extra_buf,lev+1,
257 value->justify_hor == psiconv_justify_centre?1:
258 value->justify_hor == psiconv_justify_right?2:
259 value->justify_hor == psiconv_justify_full?3:0)))
260 goto ERROR2;
261 }
262
263 if (!base || (value->justify_ver != base->justify_ver)) {
264 if ((res = psiconv_write_u8(config,extra_buf,lev+1,0x06)))
265 goto ERROR2;
266 if ((value->justify_ver < psiconv_justify_top) ||
267 (value->justify_ver > psiconv_justify_bottom))
268 psiconv_warn(config,0,psiconv_buffer_length(buf),
269 "Unknown vertical justify (%d); assuming middle",
270 value->justify_ver);
271 if ((res = psiconv_write_u8(config,extra_buf,lev+1,
272 value->justify_ver == psiconv_justify_centre?1:
273 value->justify_ver == psiconv_justify_right?2:0)))
274 goto ERROR2;
275 }
276
277 if (!base || (value->linespacing != base->linespacing)) {
278 if ((res = psiconv_write_u8(config,extra_buf,lev+1,0x07)))
279 goto ERROR2;
280 if ((res = psiconv_write_size(config,extra_buf,lev+1,value->linespacing)))
281 goto ERROR2;
282 }
283
284 if (!base || (value->linespacing_exact != base->linespacing_exact)) {
285 if ((res = psiconv_write_u8(config,extra_buf,lev+1,0x08)))
286 goto ERROR2;
287 if ((res = psiconv_write_bool(config,extra_buf,lev+1,value->linespacing_exact)))
288 goto ERROR2;
289 }
290
291 if (!base || (value->space_above != base->space_above)) {
292 if ((res = psiconv_write_u8(config,extra_buf,lev+1,0x09)))
293 goto ERROR2;
294 if ((res = psiconv_write_size(config,extra_buf,lev+1,value->space_above)))
295 goto ERROR2;
296 }
297
298 if (!base || (value->space_below != base->space_below)) {
299 if ((res = psiconv_write_u8(config,extra_buf,lev+1,0x0a)))
300 goto ERROR2;
301 if ((res = psiconv_write_size(config,extra_buf,lev+1,value->space_below)))
302 goto ERROR2;
303 }
304
305 if (!base || (value->keep_together != base->keep_together)) {
306 if ((res = psiconv_write_u8(config,extra_buf,lev+1,0x0b)))
307 goto ERROR2;
308 if ((res = psiconv_write_bool(config,extra_buf,lev+1,value->keep_together)))
309 goto ERROR2;
310 }
311
312 if (!base || (value->keep_with_next != base->keep_with_next)) {
313 if ((res = psiconv_write_u8(config,extra_buf,lev+1,0x0c)))
314 goto ERROR2;
315 if ((res = psiconv_write_bool(config,extra_buf,lev+1,value->keep_with_next)))
316 goto ERROR2;
317 }
318
319 if (!base || (value->on_next_page != base->on_next_page)) {
320 if ((res = psiconv_write_u8(config,extra_buf,lev+1,0x0d)))
321 goto ERROR2;
322 if ((res = psiconv_write_bool(config,extra_buf,lev+1,value->on_next_page)))
323 goto ERROR2;
324 }
325
326 if (!base || (value->no_widow_protection != base->no_widow_protection)) {
327 if ((res = psiconv_write_u8(config,extra_buf,lev+1,0x0e)))
328 goto ERROR2;
329 if ((res = psiconv_write_bool(config,extra_buf,lev+1,value->no_widow_protection)))
330 goto ERROR2;
331 }
332
333 if (!base || (value->wrap_to_fit_cell != base->wrap_to_fit_cell)) {
334 if ((res = psiconv_write_u8(config,extra_buf,lev+1,0x0f)))
335 goto ERROR2;
336 if ((res = psiconv_write_bool(config,extra_buf,lev+1,value->wrap_to_fit_cell)))
337 goto ERROR2;
338 }
339
340 if (!base || (value->border_distance != base->border_distance)) {
341 if ((res = psiconv_write_u8(config,extra_buf,lev+1,0x10)))
342 goto ERROR2;
343 if ((res = psiconv_write_length(config,extra_buf,lev+1,value->border_distance)))
344 goto ERROR2;
345 }
346
347 if (!base || psiconv_compare_border(value->top_border,base->top_border)) {
348 if ((res = psiconv_write_u8(config,extra_buf,lev+1,0x11)))
349 goto ERROR2;
350 if ((res = psiconv_write_border(config,extra_buf,lev+1,value->top_border)))
351 goto ERROR2;
352 }
353
354 if (!base || psiconv_compare_border(value->bottom_border,
355 base->bottom_border)) {
356 if ((res = psiconv_write_u8(config,extra_buf,lev+1,0x12)))
357 goto ERROR2;
358 if ((res = psiconv_write_border(config,extra_buf,lev+1,value->bottom_border)))
359 goto ERROR2;
360 }
361
362 if (!base || psiconv_compare_border(value->left_border,
363 base->left_border)) {
364 if ((res = psiconv_write_u8(config,extra_buf,lev+1,0x13)))
365 goto ERROR2;
366 if ((res = psiconv_write_border(config,extra_buf,lev+1,value->left_border)))
367 goto ERROR2;
368 }
369
370 if (!base || psiconv_compare_border(value->right_border,
371 base->right_border)) {
372 if ((res = psiconv_write_u8(config,extra_buf,lev+1,0x14)))
373 goto ERROR2;
374 if ((res = psiconv_write_border(config,extra_buf,lev+1,value->right_border)))
375 goto ERROR2;
376 }
377
378 if (!base || psiconv_compare_bullet(value->bullet,
379 base->bullet)) {
380 if ((res = psiconv_write_u8(config,extra_buf,lev+1,0x15)))
381 goto ERROR2;
382 if ((res = psiconv_write_bullet(config,extra_buf,lev+1,value->bullet)))
383 goto ERROR2;
384 }
385
386 if (!value->tabs || !value->tabs->extras) {
387 psiconv_error(config,0,psiconv_buffer_length(buf),"Null tabs");
388 res = -PSICONV_E_GENERATE;
389 goto ERROR2;
390 }
391
392 /* It is not entirely clear how tabs are inherited. For now, I assume
393 if there is any difference at all, we will have to generate both
394 the normal tab-interval, and all specific tabs */
395 if (!base || psiconv_compare_all_tabs(value->tabs,base->tabs)) {
396 if ((res = psiconv_write_u8(config,extra_buf,lev+1,0x16)))
397 goto ERROR2;
398 if ((res = psiconv_write_length(config,extra_buf,lev+1,value->tabs->normal)))
399 goto ERROR2;
400 for (i = 0; i < psiconv_list_length(value->tabs->extras); i++) {
401 if (!(tab = psiconv_list_get(value->tabs->extras,i))) {
402 psiconv_error(config,lev+1,0,"Massive memory corruption");
403 res = -PSICONV_E_NOMEM;
404 goto ERROR2;
405 }
406 if ((res = psiconv_write_u8(config,extra_buf,lev+1,0x17)))
407 goto ERROR2;
408 if ((res = psiconv_write_tab(config,extra_buf,lev+1,tab)))
409 goto ERROR2;
410 }
411 }
412
413 if ((res = psiconv_write_u32(config,buf,lev+1,psiconv_buffer_length(extra_buf))))
414 goto ERROR2;
415
416 res = psiconv_buffer_concat(buf,extra_buf);
417
418 ERROR2:
419 psiconv_buffer_free(extra_buf);
420 ERROR1:
421 if (res)
422 psiconv_error(config,lev,0,"Writing of paragraph layout list failed");
423 else
424 psiconv_progress(config,lev,0,"End of paragraph layout list");
425 return res;
426 }
427
428 int psiconv_write_character_layout_list(const psiconv_config config,
429 psiconv_buffer buf,int lev,
430 psiconv_character_layout value,
431 psiconv_character_layout base)
432 {
433 int res;
434 psiconv_buffer extra_buf;
435
436 psiconv_progress(config,lev,0,"Writing character layout list");
437
438 if (!value) {
439 psiconv_error(config,lev,0,"Null character layout list");
440 res = -PSICONV_E_GENERATE;
441 goto ERROR1;
442 }
443 if (!(extra_buf = psiconv_buffer_new())) {
444 res = -PSICONV_E_NOMEM;
445 goto ERROR1;
446 }
447
448 if (!base || psiconv_compare_color(base->color,value->color)) {
449 if ((res = psiconv_write_u8(config,extra_buf,lev+1,0x19)))
450 goto ERROR2;
451 if ((res = psiconv_write_color(config,extra_buf,lev+1,value->color)))
452 goto ERROR2;
453 }
454
455 if (!base || psiconv_compare_color(base->back_color,value->back_color)) {
456 if ((res = psiconv_write_u8(config,extra_buf,lev+1,0x1a)))
457 goto ERROR2;
458 if ((res = psiconv_write_color(config,extra_buf,lev+1,value->back_color)))
459 goto ERROR2;
460 }
461
462 if (!base || (value->font_size != base->font_size)) {
463 if ((res = psiconv_write_u8(config,extra_buf,lev+1,0x1c)))
464 goto ERROR2;
465 if ((res = psiconv_write_size(config,extra_buf,lev+1,value->font_size)))
466 goto ERROR2;
467 }
468
469 if (!base || (value->italic != base->italic)) {
470 if ((res = psiconv_write_u8(config,extra_buf,lev+1,0x1d)))
471 goto ERROR2;
472 if ((res = psiconv_write_bool(config,extra_buf,lev+1,value->italic)))
473 goto ERROR2;
474 }
475
476 if (!base || (value->bold != base->bold)) {
477 if ((res = psiconv_write_u8(config,extra_buf,lev+1,0x1e)))
478 goto ERROR2;
479 if ((res = psiconv_write_bool(config,extra_buf,lev+1,value->bold)))
480 goto ERROR2;
481 }
482
483 if (!base || (value->super_sub != base->super_sub)) {
484 if ((value->super_sub != psiconv_superscript) &&
485 (value->super_sub != psiconv_subscript) &&
486 (value->super_sub != psiconv_normalscript))
487 psiconv_warn(config,lev,0,"Unknown supersubscript (%d); assuming normal",
488 value->super_sub);
489 if ((res = psiconv_write_u8(config,extra_buf,lev+1,0x1f)))
490 goto ERROR2;
491 if ((res = psiconv_write_u8(config,extra_buf,lev+1,
492 value->super_sub == psiconv_superscript?1:
493 value->super_sub == psiconv_subscript?2:0)))
494 goto ERROR2;
495 }
496
497 if (!base || (value->underline != base->underline)) {
498 if ((res = psiconv_write_u8(config,extra_buf,lev+1,0x20)))
499 goto ERROR2;
500 if ((res = psiconv_write_bool(config,extra_buf,lev+1,value->underline)))
501 goto ERROR2;
502 }
503
504 if (!base || (value->strikethrough != base->strikethrough)) {
505 if ((res = psiconv_write_u8(config,extra_buf,lev+1,0x21)))
506 goto ERROR2;
507 if ((res = psiconv_write_bool(config,extra_buf,lev+1,value->strikethrough)))
508 goto ERROR2;
509 }
510
511 if (!base || psiconv_compare_font(base->font,value->font)) {
512 if ((res = psiconv_write_u8(config,extra_buf,lev+1,0x22)))
513 goto ERROR2;
514 if ((res = psiconv_write_font(config,extra_buf,lev+1,value->font)))
515 goto ERROR2;
516 }
517
518 if ((res = psiconv_write_u32(config,buf,lev+1,psiconv_buffer_length(extra_buf))))
519 goto ERROR2;
520
521 res = psiconv_buffer_concat(buf,extra_buf);
522
523 ERROR2:
524 psiconv_buffer_free(extra_buf);
525 ERROR1:
526 if (res)
527 psiconv_error(config,lev,0,"Writing of character layout list failed");
528 else
529 psiconv_progress(config,lev,0,"End of character layout list");
530 return res;
531 }

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