/[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 84 - (show annotations)
Thu Dec 28 00:24:58 2000 UTC (23 years, 3 months ago) by frodo
File MIME type: text/plain
File size: 14763 byte(s)
(Frodo) Fixed super/subscript generation, fixed border warnings in parsing

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

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