/[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 193 - (show annotations)
Mon Feb 2 21:56:48 2004 UTC (15 years, 11 months ago) by frodo
File MIME type: text/plain
File size: 17119 byte(s)
(Frodo) Make the formats stuff work again

  * Amend the configure.in to select the right targets
  * Remove RTF support (it never worked anyway)
  * Update the links creation scripts

And in the psiconv program and library:
  * Some fixes to reduce compiler warnings
  * Emit tabs as spaces in the HTML4/XHTML generators

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

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