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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 78 - (hide annotations)
Mon Dec 25 14:34:17 2000 UTC (23 years, 3 months ago) by frodo
File MIME type: text/plain
File size: 14670 byte(s)
(Frodo) Added compare functions for layout elements

1 frodo 72 /*
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 frodo 78 int res,i;
138 frodo 72 psiconv_buffer extra_buf;
139 frodo 78 psiconv_tab tab;
140 frodo 72
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 frodo 78 if (!base || psiconv_compare_color(base->back_color,value->back_color)) {
149 frodo 72 if ((res = psiconv_write_u8(extra_buf,0x01)))
150     goto ERROR;
151     if ((res = psiconv_write_color(extra_buf,value->back_color)))
152     goto ERROR;
153     }
154    
155     if (!base || (value->indent_left != base->indent_left)) {
156     if ((res = psiconv_write_u8(extra_buf,0x02)))
157     goto ERROR;
158     if ((res = psiconv_write_length(extra_buf,value->indent_left)))
159     goto ERROR;
160     }
161    
162     if (!base || (value->indent_right != base->indent_right)) {
163     if ((res = psiconv_write_u8(extra_buf,0x03)))
164     goto ERROR;
165     if ((res = psiconv_write_length(extra_buf,value->indent_right)))
166     goto ERROR;
167     }
168    
169     if (!base || (value->indent_first != base->indent_first)) {
170     if ((res = psiconv_write_u8(extra_buf,0x04)))
171     goto ERROR;
172     if ((res = psiconv_write_length(extra_buf,value->indent_first)))
173     goto ERROR;
174     }
175    
176     if (!base || (value->justify_hor != base->justify_hor)) {
177     if ((res = psiconv_write_u8(extra_buf,0x05)))
178     goto ERROR;
179     if ((value->justify_hor < psiconv_justify_left) ||
180     (value->justify_hor > psiconv_justify_full))
181     psiconv_warn(0,psiconv_list_length(buf),
182     "Unknown horizontal justify (%d); assuming left",
183     value->justify_hor);
184     if ((res = psiconv_write_u8(extra_buf,
185     value->justify_hor == psiconv_justify_centre?1:
186     value->justify_hor == psiconv_justify_right?2:
187     value->justify_hor == psiconv_justify_full?3:0)))
188     goto ERROR;
189     }
190    
191     if (!base || (value->justify_ver != base->justify_ver)) {
192     if ((res = psiconv_write_u8(extra_buf,0x06)))
193     goto ERROR;
194     if ((value->justify_ver < psiconv_justify_top) ||
195     (value->justify_ver > psiconv_justify_bottom))
196     psiconv_warn(0,psiconv_list_length(buf),
197     "Unknown vertical justify (%d); assuming middle",
198     value->justify_ver);
199     if ((res = psiconv_write_u8(extra_buf,
200     value->justify_ver == psiconv_justify_centre?1:
201     value->justify_ver == psiconv_justify_right?2:0)))
202     goto ERROR;
203     }
204    
205     if (!base || (value->linespacing != base->linespacing)) {
206     if ((res = psiconv_write_u8(extra_buf,0x07)))
207     goto ERROR;
208     if ((res = psiconv_write_size(extra_buf,value->linespacing)))
209     goto ERROR;
210     }
211    
212     if (!base || (value->linespacing_exact != base->linespacing_exact)) {
213     if ((res = psiconv_write_u8(extra_buf,0x08)))
214     goto ERROR;
215     if ((res = psiconv_write_bool(extra_buf,value->linespacing_exact)))
216     goto ERROR;
217     }
218    
219     if (!base || (value->space_above != base->space_above)) {
220     if ((res = psiconv_write_u8(extra_buf,0x09)))
221     goto ERROR;
222     if ((res = psiconv_write_size(extra_buf,value->space_above)))
223     goto ERROR;
224     }
225    
226     if (!base || (value->space_below != base->space_below)) {
227     if ((res = psiconv_write_u8(extra_buf,0x0a)))
228     goto ERROR;
229     if ((res = psiconv_write_size(extra_buf,value->space_below)))
230     goto ERROR;
231     }
232    
233     if (!base || (value->keep_together != base->keep_together)) {
234     if ((res = psiconv_write_u8(extra_buf,0x0b)))
235     goto ERROR;
236     if ((res = psiconv_write_bool(extra_buf,value->keep_together)))
237     goto ERROR;
238     }
239    
240     if (!base || (value->keep_with_next != base->keep_with_next)) {
241     if ((res = psiconv_write_u8(extra_buf,0x0c)))
242     goto ERROR;
243     if ((res = psiconv_write_bool(extra_buf,value->keep_with_next)))
244     goto ERROR;
245     }
246    
247     if (!base || (value->on_next_page != base->on_next_page)) {
248     if ((res = psiconv_write_u8(extra_buf,0x0d)))
249     goto ERROR;
250     if ((res = psiconv_write_bool(extra_buf,value->on_next_page)))
251     goto ERROR;
252     }
253    
254     if (!base || (value->no_widow_protection != base->no_widow_protection)) {
255     if ((res = psiconv_write_u8(extra_buf,0x0e)))
256     goto ERROR;
257     if ((res = psiconv_write_bool(extra_buf,value->no_widow_protection)))
258     goto ERROR;
259     }
260    
261     if (!base || (value->border_distance != base->border_distance)) {
262     if ((res = psiconv_write_u8(extra_buf,0x10)))
263     goto ERROR;
264     if ((res = psiconv_write_length(extra_buf,value->border_distance)))
265     goto ERROR;
266     }
267    
268 frodo 78 if (!base || psiconv_compare_border(value->top_border,base->top_border)) {
269 frodo 72 if ((res = psiconv_write_u8(extra_buf,0x11)))
270     goto ERROR;
271 frodo 76 if ((res = psiconv_write_border(extra_buf,value->top_border)))
272 frodo 72 goto ERROR;
273     }
274    
275 frodo 78 if (!base || psiconv_compare_border(value->bottom_border,
276     base->bottom_border)) {
277 frodo 72 if ((res = psiconv_write_u8(extra_buf,0x12)))
278     goto ERROR;
279 frodo 78 if ((res = psiconv_write_border(extra_buf,value->bottom_border)))
280 frodo 72 goto ERROR;
281     }
282    
283 frodo 78 if (!base || psiconv_compare_border(value->left_border,
284     base->left_border)) {
285 frodo 72 if ((res = psiconv_write_u8(extra_buf,0x13)))
286     goto ERROR;
287     if ((res = psiconv_write_border(extra_buf,value->left_border)))
288     goto ERROR;
289     }
290    
291 frodo 78 if (!base || psiconv_compare_border(value->right_border,
292     base->right_border)) {
293 frodo 72 if ((res = psiconv_write_u8(extra_buf,0x14)))
294     goto ERROR;
295     if ((res = psiconv_write_border(extra_buf,value->right_border)))
296     goto ERROR;
297     }
298    
299 frodo 78 if (!base || psiconv_compare_bullet(value->bullet,
300     base->bullet)) {
301 frodo 72 if ((res = psiconv_write_u8(extra_buf,0x15)))
302     goto ERROR;
303     if ((res = psiconv_write_bullet(extra_buf,value->bullet)))
304     goto ERROR;
305     }
306    
307     if (!value->tabs || !value->tabs->extras) {
308     psiconv_warn(0,psiconv_list_length(buf),"Null tabs");
309     res = -PSICONV_E_GENERATE;
310     goto ERROR;
311     }
312 frodo 78
313 frodo 72 /* It is not entirely clear how tabs are inherited. For now, I assume
314     if there is any difference at all, we will have to generate both
315     the normal tab-interval, and all specific tabs */
316 frodo 78 if (!base || psiconv_compare_all_tabs(value->tabs,base->tabs)) {
317 frodo 72 if ((res = psiconv_write_u8(extra_buf,0x16)))
318     goto ERROR;
319     if ((res = psiconv_write_length(extra_buf,value->tabs->normal)))
320     goto ERROR;
321     for (i = 0; i < psiconv_list_length(value->tabs->extras); i++) {
322 frodo 78 if (!(tab = psiconv_list_get(value->tabs->extras,i))) {
323 frodo 72 psiconv_warn(0,psiconv_list_length(buf),"Massive memory corruption");
324     res = -PSICONV_E_NOMEM;
325     goto ERROR;
326     }
327     if ((res = psiconv_write_u8(extra_buf,0x17)))
328     goto ERROR;
329 frodo 78 if ((res = psiconv_write_tab(extra_buf,tab)))
330 frodo 72 goto ERROR;
331     }
332     }
333    
334 frodo 76 if ((res = psiconv_write_u32(buf,psiconv_list_length(extra_buf))))
335     goto ERROR;
336    
337 frodo 72 res = psiconv_list_concat(buf,extra_buf);
338    
339     ERROR:
340     psiconv_free_buffer(extra_buf);
341     return res;
342     }
343    
344     int psiconv_write_character_layout_list(psiconv_buffer buf,
345     psiconv_character_layout value,
346     psiconv_character_layout base)
347     {
348     int res;
349     psiconv_buffer extra_buf;
350     if (!value) {
351     psiconv_warn(0,psiconv_list_length(buf),"Null character layout list");
352     return -PSICONV_E_GENERATE;
353     }
354     if (!(extra_buf = psiconv_new_buffer()))
355     return -PSICONV_E_NOMEM;
356    
357 frodo 78 if (!base || psiconv_compare_color(base->color,value->color)) {
358 frodo 72 if ((res = psiconv_write_u8(extra_buf,0x19)))
359     goto ERROR;
360     if ((res = psiconv_write_color(extra_buf,value->color)))
361     goto ERROR;
362     }
363    
364 frodo 78 if (!base || psiconv_compare_color(base->back_color,value->back_color)) {
365 frodo 72 if ((res = psiconv_write_u8(extra_buf,0x1a)))
366     goto ERROR;
367     if ((res = psiconv_write_color(extra_buf,value->back_color)))
368     goto ERROR;
369     }
370    
371     if (!base || (value->font_size != base->font_size)) {
372     if ((res = psiconv_write_u8(extra_buf,0x1c)))
373     goto ERROR;
374     if ((res = psiconv_write_size(extra_buf,value->font_size)))
375     goto ERROR;
376     }
377    
378     if (!base || (value->italic != base->italic)) {
379     if ((res = psiconv_write_u8(extra_buf,0x1d)))
380     goto ERROR;
381     if ((res = psiconv_write_bool(extra_buf,value->italic)))
382     goto ERROR;
383     }
384    
385     if (!base || (value->bold != base->bold)) {
386     if ((res = psiconv_write_u8(extra_buf,0x1e)))
387     goto ERROR;
388     if ((res = psiconv_write_bool(extra_buf,value->bold)))
389     goto ERROR;
390     }
391    
392     if (!base || (value->super_sub != base->super_sub)) {
393     if ((value->super_sub != psiconv_superscript) &&
394     (value->super_sub != psiconv_subscript) &&
395     (value->super_sub != psiconv_normalscript))
396     psiconv_warn(0,psiconv_list_length(buf),
397     "Unknown supersubscript (%d); assuming normal",
398     value->super_sub);
399     if ((res = psiconv_write_u8(extra_buf,0x1f)))
400     goto ERROR;
401     if ((res = psiconv_write_u8(extra_buf,
402     base->super_sub == psiconv_superscript?1:
403     base->super_sub == psiconv_superscript?2:0)))
404     goto ERROR;
405     }
406    
407     if (!base || (value->underline != base->underline)) {
408     if ((res = psiconv_write_u8(extra_buf,0x20)))
409     goto ERROR;
410     if ((res = psiconv_write_bool(extra_buf,value->underline)))
411     goto ERROR;
412     }
413    
414     if (!base || (value->strikethrough != base->strikethrough)) {
415     if ((res = psiconv_write_u8(extra_buf,0x21)))
416     goto ERROR;
417     if ((res = psiconv_write_bool(extra_buf,value->strikethrough)))
418     goto ERROR;
419     }
420    
421 frodo 78 if (!base || psiconv_compare_font(base->font,value->font)) {
422 frodo 72 if ((res = psiconv_write_u8(extra_buf,0x22)))
423     goto ERROR;
424     if ((res = psiconv_write_font(extra_buf,value->font)))
425     goto ERROR;
426     }
427    
428 frodo 76 if ((res = psiconv_write_u32(buf,psiconv_list_length(extra_buf))))
429     goto ERROR;
430    
431 frodo 72 res = psiconv_list_concat(buf,extra_buf);
432    
433     ERROR:
434     psiconv_free_buffer(extra_buf);
435     return res;
436     }

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