/[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 104 - (show annotations)
Wed Jan 31 00:57:17 2001 UTC (23 years, 2 months ago) by frodo
File MIME type: text/plain
File size: 14995 byte(s)
(Frodo) Applied Decsi's patch for wraps

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->wrap_to_fit_cell != base->wrap_to_fit_cell)) {
263 if ((res = psiconv_write_u8(extra_buf,0x0f)))
264 goto ERROR;
265 if ((res = psiconv_write_bool(extra_buf,value->wrap_to_fit_cell)))
266 goto ERROR;
267 }
268
269 if (!base || (value->border_distance != base->border_distance)) {
270 if ((res = psiconv_write_u8(extra_buf,0x10)))
271 goto ERROR;
272 if ((res = psiconv_write_length(extra_buf,value->border_distance)))
273 goto ERROR;
274 }
275
276 if (!base || psiconv_compare_border(value->top_border,base->top_border)) {
277 if ((res = psiconv_write_u8(extra_buf,0x11)))
278 goto ERROR;
279 if ((res = psiconv_write_border(extra_buf,value->top_border)))
280 goto ERROR;
281 }
282
283 if (!base || psiconv_compare_border(value->bottom_border,
284 base->bottom_border)) {
285 if ((res = psiconv_write_u8(extra_buf,0x12)))
286 goto ERROR;
287 if ((res = psiconv_write_border(extra_buf,value->bottom_border)))
288 goto ERROR;
289 }
290
291 if (!base || psiconv_compare_border(value->left_border,
292 base->left_border)) {
293 if ((res = psiconv_write_u8(extra_buf,0x13)))
294 goto ERROR;
295 if ((res = psiconv_write_border(extra_buf,value->left_border)))
296 goto ERROR;
297 }
298
299 if (!base || psiconv_compare_border(value->right_border,
300 base->right_border)) {
301 if ((res = psiconv_write_u8(extra_buf,0x14)))
302 goto ERROR;
303 if ((res = psiconv_write_border(extra_buf,value->right_border)))
304 goto ERROR;
305 }
306
307 if (!base || psiconv_compare_bullet(value->bullet,
308 base->bullet)) {
309 if ((res = psiconv_write_u8(extra_buf,0x15)))
310 goto ERROR;
311 if ((res = psiconv_write_bullet(extra_buf,value->bullet)))
312 goto ERROR;
313 }
314
315 if (!value->tabs || !value->tabs->extras) {
316 psiconv_warn(0,psiconv_buffer_length(buf),"Null tabs");
317 res = -PSICONV_E_GENERATE;
318 goto ERROR;
319 }
320
321 /* It is not entirely clear how tabs are inherited. For now, I assume
322 if there is any difference at all, we will have to generate both
323 the normal tab-interval, and all specific tabs */
324 if (!base || psiconv_compare_all_tabs(value->tabs,base->tabs)) {
325 if ((res = psiconv_write_u8(extra_buf,0x16)))
326 goto ERROR;
327 if ((res = psiconv_write_length(extra_buf,value->tabs->normal)))
328 goto ERROR;
329 for (i = 0; i < psiconv_list_length(value->tabs->extras); i++) {
330 if (!(tab = psiconv_list_get(value->tabs->extras,i))) {
331 psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption");
332 res = -PSICONV_E_NOMEM;
333 goto ERROR;
334 }
335 if ((res = psiconv_write_u8(extra_buf,0x17)))
336 goto ERROR;
337 if ((res = psiconv_write_tab(extra_buf,tab)))
338 goto ERROR;
339 }
340 }
341
342 if ((res = psiconv_write_u32(buf,psiconv_buffer_length(extra_buf))))
343 goto ERROR;
344
345 res = psiconv_buffer_concat(buf,extra_buf);
346
347 ERROR:
348 psiconv_buffer_free(extra_buf);
349 return res;
350 }
351
352 int psiconv_write_character_layout_list(psiconv_buffer buf,
353 psiconv_character_layout value,
354 psiconv_character_layout base)
355 {
356 int res;
357 psiconv_buffer extra_buf;
358 if (!value) {
359 psiconv_warn(0,psiconv_buffer_length(buf),"Null character layout list");
360 return -PSICONV_E_GENERATE;
361 }
362 if (!(extra_buf = psiconv_buffer_new()))
363 return -PSICONV_E_NOMEM;
364
365 if (!base || psiconv_compare_color(base->color,value->color)) {
366 if ((res = psiconv_write_u8(extra_buf,0x19)))
367 goto ERROR;
368 if ((res = psiconv_write_color(extra_buf,value->color)))
369 goto ERROR;
370 }
371
372 if (!base || psiconv_compare_color(base->back_color,value->back_color)) {
373 if ((res = psiconv_write_u8(extra_buf,0x1a)))
374 goto ERROR;
375 if ((res = psiconv_write_color(extra_buf,value->back_color)))
376 goto ERROR;
377 }
378
379 if (!base || (value->font_size != base->font_size)) {
380 if ((res = psiconv_write_u8(extra_buf,0x1c)))
381 goto ERROR;
382 if ((res = psiconv_write_size(extra_buf,value->font_size)))
383 goto ERROR;
384 }
385
386 if (!base || (value->italic != base->italic)) {
387 if ((res = psiconv_write_u8(extra_buf,0x1d)))
388 goto ERROR;
389 if ((res = psiconv_write_bool(extra_buf,value->italic)))
390 goto ERROR;
391 }
392
393 if (!base || (value->bold != base->bold)) {
394 if ((res = psiconv_write_u8(extra_buf,0x1e)))
395 goto ERROR;
396 if ((res = psiconv_write_bool(extra_buf,value->bold)))
397 goto ERROR;
398 }
399
400 if (!base || (value->super_sub != base->super_sub)) {
401 if ((value->super_sub != psiconv_superscript) &&
402 (value->super_sub != psiconv_subscript) &&
403 (value->super_sub != psiconv_normalscript))
404 psiconv_warn(0,psiconv_buffer_length(buf),
405 "Unknown supersubscript (%d); assuming normal",
406 value->super_sub);
407 if ((res = psiconv_write_u8(extra_buf,0x1f)))
408 goto ERROR;
409 if ((res = psiconv_write_u8(extra_buf,
410 value->super_sub == psiconv_superscript?1:
411 value->super_sub == psiconv_subscript?2:0)))
412 goto ERROR;
413 }
414
415 if (!base || (value->underline != base->underline)) {
416 if ((res = psiconv_write_u8(extra_buf,0x20)))
417 goto ERROR;
418 if ((res = psiconv_write_bool(extra_buf,value->underline)))
419 goto ERROR;
420 }
421
422 if (!base || (value->strikethrough != base->strikethrough)) {
423 if ((res = psiconv_write_u8(extra_buf,0x21)))
424 goto ERROR;
425 if ((res = psiconv_write_bool(extra_buf,value->strikethrough)))
426 goto ERROR;
427 }
428
429 if (!base || psiconv_compare_font(base->font,value->font)) {
430 if ((res = psiconv_write_u8(extra_buf,0x22)))
431 goto ERROR;
432 if ((res = psiconv_write_font(extra_buf,value->font)))
433 goto ERROR;
434 }
435
436 if ((res = psiconv_write_u32(buf,psiconv_buffer_length(extra_buf))))
437 goto ERROR;
438
439 res = psiconv_buffer_concat(buf,extra_buf);
440
441 ERROR:
442 psiconv_buffer_free(extra_buf);
443 return res;
444 }

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