/[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 182 - (show annotations)
Sun Jan 4 22:07:02 2004 UTC (20 years, 3 months ago) by frodo
File MIME type: text/plain
File size: 15905 byte(s)
(Frodo) Move fontnames to psiconv_string type

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

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