/[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 72 - (show annotations)
Sat Dec 23 20:21:40 2000 UTC (19 years ago) by frodo
File MIME type: text/plain
File size: 18101 byte(s)
(Frodo) New generation routines in generate_layout

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

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