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

Contents of /psiconv/trunk/lib/psiconv/generate_common.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 170 - (show annotations)
Tue Nov 25 22:08:48 2003 UTC (20 years, 4 months ago) by frodo
File MIME type: text/plain
File size: 19909 byte(s)
(Frodo) Generating of Sketch files, both stand-alone and as objects, works!

No RLE encoding yet, we have to test higher colordepths.

1 /*
2 generate_common.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 #include <stdlib.h>
23 #include <string.h>
24
25 #include "generate_routines.h"
26 #include "error.h"
27
28 #ifdef DMALLOC
29 #include <dmalloc.h>
30 #endif
31
32 static int psiconv_write_layout_section(const psiconv_config config,
33 psiconv_buffer buf,
34 const psiconv_text_and_layout value,
35 const psiconv_word_styles_section styles,
36 int with_styles);
37
38 /* Maybe use a psiconv_header_section variable instead? */
39 int psiconv_write_header_section(const psiconv_config config,
40 psiconv_buffer buf,psiconv_u32 uid1,
41 psiconv_u32 uid2, psiconv_u32 uid3)
42 {
43 int res;
44 if ((res = psiconv_write_u32(config,buf,uid1)))
45 return res;
46 if ((res = psiconv_write_u32(config,buf,uid2)))
47 return res;
48 if ((res = psiconv_write_u32(config,buf,uid3)))
49 return res;
50 return psiconv_write_u32(config,buf,psiconv_checkuid(uid1,uid2,uid3));
51 }
52
53 int psiconv_write_section_table_section(const psiconv_config config,
54 psiconv_buffer buf,
55 const psiconv_section_table_section value)
56 {
57 int res,i;
58 psiconv_section_table_entry entry;
59
60 if (!value) {
61 psiconv_warn(config,0,psiconv_buffer_length(buf),"Null section table section");
62 return -PSICONV_E_GENERATE;
63 }
64
65 if ((res = psiconv_write_u8(config,buf,2 * psiconv_list_length(value))))
66 return res;
67 for (i = 0; i < psiconv_list_length(value); i++) {
68 if (!(entry = psiconv_list_get(value,i))) {
69 psiconv_warn(config,0,psiconv_buffer_length(buf),"Massive memory corruption");
70 return -PSICONV_E_NOMEM;
71 }
72 if ((res = psiconv_write_u32(config,buf,entry->id)))
73 return res;
74 if ((res = psiconv_write_offset(config,buf,entry->offset)))
75 return res;
76 }
77 return -PSICONV_E_OK;
78 }
79
80 int psiconv_write_application_id_section(const psiconv_config config,
81 psiconv_buffer buf,psiconv_u32 id,
82 const psiconv_string_t text)
83 {
84 int res;
85 if ((res = psiconv_write_u32(config,buf,id)))
86 return res;
87 return psiconv_write_string(config,buf,text);
88 }
89
90 int psiconv_write_text_section(const psiconv_config config,
91 psiconv_buffer buf,
92 const psiconv_text_and_layout value)
93 {
94 int res;
95 psiconv_buffer extra_buf;
96 int i,j;
97 psiconv_paragraph paragraph;
98
99 if (!value) {
100 psiconv_warn(config,0,psiconv_buffer_length(buf),"Null text section");
101 return -PSICONV_E_GENERATE;
102 }
103
104 if (psiconv_list_length(value)) {
105 if (!(extra_buf = psiconv_buffer_new()))
106 return -PSICONV_E_NOMEM;
107 for (i = 0; i < psiconv_list_length(value); i++) {
108 if (!(paragraph = psiconv_list_get(value,i))) {
109 psiconv_warn(config,0,psiconv_buffer_length(buf),"Massive memory corruption");
110 res = -PSICONV_E_OTHER;
111 goto ERROR;
112 }
113 for (j = 0; j < strlen(paragraph->text); j++)
114 if ((res = psiconv_write_u8(config,extra_buf,paragraph->text[j])))
115 goto ERROR;
116 psiconv_write_u8(config,extra_buf,0x06);
117 }
118 if ((res = psiconv_write_X(config,buf,psiconv_buffer_length(extra_buf))))
119 goto ERROR;
120 res = psiconv_buffer_concat(buf,extra_buf);
121 } else
122 /* Hack: empty text sections are just not allowed */
123 return psiconv_write_u16(config,buf,0x0602);
124
125 ERROR:
126 psiconv_buffer_free(extra_buf);
127 return res;
128 }
129
130 int psiconv_write_layout_section(const psiconv_config config,
131 psiconv_buffer buf,
132 const psiconv_text_and_layout value,
133 const psiconv_word_styles_section styles,
134 int with_styles)
135 {
136 typedef struct psiconv_paragraph_type_list_s
137 {
138 psiconv_character_layout character;
139 psiconv_paragraph_layout paragraph;
140 psiconv_u8 style;
141 psiconv_u8 nr;
142 } *psiconv_paragraph_type_list;
143 psiconv_u32 obj_id;
144 psiconv_list paragraph_type_list; /* Of psiconv_paragraph_type_list_s */
145 psiconv_paragraph_type_list paragraph_type;
146 struct psiconv_paragraph_type_list_s new_type;
147 psiconv_buffer buf_types,buf_elements,buf_inlines,buf_objects;
148 psiconv_paragraph paragraph;
149 psiconv_in_line_layout in_line = NULL;
150 psiconv_word_style style;
151 psiconv_character_layout para_charlayout;
152 int i,j,para_type,nr_of_inlines=0,res,ptl_length,pel_length,thislen,paralen;
153
154 if (!value) {
155 psiconv_warn(config,0,psiconv_buffer_length(buf),"Null text section");
156 return -PSICONV_E_GENERATE;
157 }
158
159 if (!(paragraph_type_list = psiconv_list_new(sizeof(new_type)))) {
160 res = -PSICONV_E_NOMEM;
161 goto ERROR1;
162 }
163
164 if (!(buf_types = psiconv_buffer_new())) {
165 res = -PSICONV_E_NOMEM;
166 goto ERROR2;
167 }
168
169 if (!(buf_elements = psiconv_buffer_new())) {
170 res = -PSICONV_E_NOMEM;
171 goto ERROR3;
172 }
173
174 if (!(buf_inlines = psiconv_buffer_new())) {
175 res = -PSICONV_E_NOMEM;
176 goto ERROR4;
177 }
178
179 if (!(buf_objects = psiconv_buffer_new())) {
180 res = -PSICONV_E_NOMEM;
181 goto ERROR5;
182 }
183
184 for (i = 0; i < psiconv_list_length(value); i++) {
185 if (!(paragraph = psiconv_list_get(value,i))) {
186 psiconv_warn(config,0,psiconv_buffer_length(buf),"Massive memory corruption");
187 res = -PSICONV_E_OTHER;
188 goto ERROR6;
189 }
190 if ((res = psiconv_write_u32(config,buf_elements,strlen(paragraph->text)+1)))
191 goto ERROR6;
192
193 /* We need it for the next if-statement */
194 if (psiconv_list_length(paragraph->in_lines) == 1)
195 if (!(in_line = psiconv_list_get(paragraph->in_lines,1)))
196 goto ERROR6;
197
198 if ((psiconv_list_length(paragraph->in_lines) > 1) ||
199 ((psiconv_list_length(paragraph->in_lines) == 1) &&
200 (in_line->object != NULL))) {
201 /* Inline layouts, or an object, so we generate a paragraph element
202 and inline elements */
203 if ((res = psiconv_write_u8(config,buf_elements,0x00)))
204 goto ERROR6;
205 if (!(style = psiconv_get_style(styles,paragraph->base_style))) {
206 psiconv_warn(config,0,psiconv_buffer_length(buf),"Unknown style");
207 res = -PSICONV_E_GENERATE;
208 goto ERROR6;
209 }
210 if ((res = psiconv_write_paragraph_layout_list(config,buf_elements,
211 paragraph->base_paragraph,
212 style->paragraph)))
213 goto ERROR6;
214 if (with_styles)
215 if ((res = psiconv_write_u8(config,buf_elements,paragraph->base_style)))
216 goto ERROR6;
217 if ((res = psiconv_write_u32(config,buf_elements,
218 psiconv_list_length(paragraph->in_lines))))
219 goto ERROR6;
220
221 /* Generate the inlines. NB: Against what are all settings relative?!? */
222 paralen = 0;
223 for (j = 0; j < psiconv_list_length(paragraph->in_lines); j++) {
224 nr_of_inlines ++;
225 if (!(in_line = psiconv_list_get(paragraph->in_lines,j))) {
226 psiconv_warn(config,0,psiconv_buffer_length(buf),
227 "Massive memory corruption");
228 res = -PSICONV_E_OTHER;
229 goto ERROR6;
230 }
231 if ((res = psiconv_write_u8(config,buf_inlines,in_line->object?0x01:0x00)))
232 goto ERROR6;
233 thislen = in_line->length;
234 paralen += thislen;
235 /* If this is the last in_line, we need to make sure that the
236 complete length of all inlines equals the text length */
237 if (j == psiconv_list_length(paragraph->in_lines)-1) {
238 if (paralen > strlen(paragraph->text)+1) {
239 res = -PSICONV_E_GENERATE;
240 goto ERROR6;
241 }
242 thislen += strlen(paragraph->text)+1-paralen;
243 }
244 if ((res = psiconv_write_u32(config,buf_inlines,thislen)))
245 goto ERROR6;
246 if ((res = psiconv_write_character_layout_list(config,buf_inlines,
247 in_line->layout,
248 style->character)))
249 goto ERROR6;
250 if (in_line->object) {
251 if ((res = psiconv_write_u32(config,buf_inlines,PSICONV_ID_OBJECT)))
252 goto ERROR6;
253 obj_id = psiconv_buffer_unique_id();
254 if ((res = psiconv_buffer_add_reference(buf_inlines,obj_id)))
255 goto ERROR6;
256 if ((res = psiconv_buffer_add_target(buf_objects,obj_id)))
257 goto ERROR6;
258 if ((res = psiconv_write_embedded_object_section(config,buf_objects,
259 in_line->object)))
260 goto ERROR6;
261 if ((res = psiconv_write_length(config,buf_inlines,in_line->object_width)))
262 goto ERROR6;
263 if ((res = psiconv_write_length(config,buf_inlines,in_line->object_height)))
264 goto ERROR6;
265 }
266 }
267 } else {
268 /* No inline layouts (or only 1), so we generate a paragraph type list */
269 para_type = 0;
270 /* Set para_charlayout to the correct character-level layout */
271 if (psiconv_list_length(paragraph->in_lines) == 0)
272 para_charlayout = paragraph->base_character;
273 else {
274 if (!(in_line = psiconv_list_get(paragraph->in_lines,0))) {
275 psiconv_warn(config,0,psiconv_buffer_length(buf),
276 "Massive memory corruption");
277 res = -PSICONV_E_OTHER;
278 goto ERROR6;
279 }
280 para_charlayout = in_line->layout;
281 }
282 for (j = 0; j < psiconv_list_length(paragraph_type_list); j++) {
283 if (!(paragraph_type = psiconv_list_get(paragraph_type_list,j))) {
284 psiconv_warn(config,0,psiconv_buffer_length(buf),
285 "Massive memory corruption");
286 res = -PSICONV_E_OTHER;
287 goto ERROR6;
288 }
289 if ((paragraph->base_style == paragraph_type->style) &&
290 !psiconv_compare_character_layout(para_charlayout,
291 paragraph_type->character) &&
292 !psiconv_compare_paragraph_layout(paragraph->base_paragraph,
293 paragraph_type->paragraph)) {
294 para_type = paragraph_type->nr;
295 break;
296 }
297 }
298 if (!para_type) {
299 /* We need to add a new entry */
300 para_type = new_type.nr = j+1;
301 /* No need to copy them, we won't change them anyway */
302 new_type.paragraph = paragraph->base_paragraph;
303 new_type.character = para_charlayout;
304 new_type.style = paragraph->base_style;
305 paragraph_type = &new_type;
306 if ((res = psiconv_list_add(paragraph_type_list,paragraph_type)))
307 goto ERROR6;
308 if ((res = psiconv_write_u32(config,buf_types,paragraph_type->nr)))
309 goto ERROR6;
310 if (!(style = psiconv_get_style(styles,paragraph_type->style))) {
311 psiconv_warn(config,0,psiconv_buffer_length(buf),"Unknown style");
312 res = -PSICONV_E_GENERATE;
313 goto ERROR6;
314 }
315 if ((res = psiconv_write_paragraph_layout_list(config,buf_types,
316 paragraph_type->paragraph,style->paragraph)))
317 goto ERROR6;
318 if (with_styles)
319 if ((res = psiconv_write_u8(config,buf_types,paragraph_type->style)))
320 goto ERROR6;
321 if ((res = psiconv_write_character_layout_list(config,buf_types,
322 paragraph_type->character,style->character)))
323 goto ERROR6;
324 }
325 if ((res = psiconv_write_u8(config,buf_elements,para_type)))
326 goto ERROR6;
327 }
328 }
329
330 /* HACK: special case: no paragraphs at all. We need to improvize. */
331 if (!psiconv_list_length(value)) {
332 if ((res = psiconv_write_u32(config,buf_types,1)))
333 goto ERROR6;
334 if ((res = psiconv_write_u32(config,buf_types,0)))
335 goto ERROR6;
336 if (with_styles)
337 if ((res = psiconv_write_u8(config,buf_types,0)))
338 goto ERROR6;
339 if ((res = psiconv_write_u32(config,buf_types,0)))
340 goto ERROR6;
341
342 if ((res = psiconv_write_u32(config,buf_elements,1)))
343 goto ERROR6;
344 if ((res = psiconv_write_u8(config,buf_elements,1)))
345 goto ERROR6;
346 pel_length = 1;
347 ptl_length = 1;
348 } else {
349 pel_length = psiconv_list_length(value);
350 ptl_length = psiconv_list_length(paragraph_type_list);
351 }
352
353 /* Now append everything */
354 if ((res = psiconv_write_u16(config,buf,with_styles?0x0001:0x0000)))
355 goto ERROR6;
356 if ((res = psiconv_write_u8(config,buf, ptl_length)))
357 goto ERROR6;
358 if ((res = psiconv_buffer_concat(buf,buf_types)))
359 goto ERROR6;
360 if ((res = psiconv_write_u32(config,buf,pel_length)))
361 goto ERROR6;
362 if ((res = psiconv_buffer_concat(buf,buf_elements)))
363 goto ERROR6;
364 if ((res = psiconv_write_u32(config,buf,nr_of_inlines)))
365 goto ERROR6;
366 if ((res = psiconv_buffer_concat(buf,buf_inlines)))
367 goto ERROR6;
368 if ((res = psiconv_buffer_concat(buf,buf_objects)))
369 goto ERROR6;
370
371 ERROR6:
372 psiconv_buffer_free(buf_objects);
373 ERROR5:
374 psiconv_buffer_free(buf_inlines);
375 ERROR4:
376 psiconv_buffer_free(buf_elements);
377 ERROR3:
378 psiconv_buffer_free(buf_types);
379 ERROR2:
380 psiconv_list_free(paragraph_type_list);
381 ERROR1:
382 return res;
383 }
384
385 int psiconv_write_styled_layout_section(const psiconv_config config,
386 psiconv_buffer buf,
387 psiconv_text_and_layout result,
388 psiconv_word_styles_section styles)
389 {
390 return psiconv_write_layout_section(config,buf,result,styles,1);
391 }
392
393 int psiconv_write_styleless_layout_section(const psiconv_config config,
394 psiconv_buffer buf,
395 const psiconv_text_and_layout value,
396 const psiconv_character_layout base_char,
397 const psiconv_paragraph_layout base_para)
398 {
399 int res = 0;
400 psiconv_word_styles_section styles_section;
401
402 if (!(styles_section = malloc(sizeof(*styles_section))))
403 goto ERROR1;
404 if (!(styles_section->normal = malloc(sizeof(*styles_section->normal))))
405 goto ERROR2;
406 if (!(styles_section->normal->character =
407 psiconv_clone_character_layout(base_char)))
408 goto ERROR3;
409 if (!(styles_section->normal->paragraph =
410 psiconv_clone_paragraph_layout(base_para)))
411 goto ERROR4;
412 styles_section->normal->hotkey = 0;
413 if (!(styles_section->normal->name = strdup("")))
414 goto ERROR5;
415 if (!(styles_section->styles = psiconv_list_new(sizeof(
416 struct psiconv_word_style_s))))
417 goto ERROR6;
418
419 res = psiconv_write_layout_section(config,buf,value,styles_section,0);
420 psiconv_free_word_styles_section(styles_section);
421 return res;
422
423 ERROR6:
424 free(styles_section->normal->name);
425 ERROR5:
426 psiconv_free_paragraph_layout(styles_section->normal->paragraph);
427 ERROR4:
428 psiconv_free_character_layout(styles_section->normal->character);
429 ERROR3:
430 free(styles_section->normal);
431 ERROR2:
432 free(styles_section);
433 ERROR1:
434 if (!res)
435 return -PSICONV_E_NOMEM;
436 else
437 return res;
438 }
439
440
441 int psiconv_write_embedded_object_section(const psiconv_config config,
442 psiconv_buffer buf,
443 const psiconv_embedded_object_section value)
444 {
445 int res;
446 psiconv_u32 display_id,icon_id,table_id;
447 psiconv_buffer extra_buf;
448
449 if (!value) {
450 psiconv_warn(config,0,psiconv_buffer_length(buf),"Null Object");
451 res = -PSICONV_E_GENERATE;
452 goto ERROR1;
453 }
454
455 if (!(extra_buf = psiconv_buffer_new())) {
456 res = -PSICONV_E_NOMEM;
457 goto ERROR1;
458 }
459
460 display_id = psiconv_buffer_unique_id();
461 icon_id = psiconv_buffer_unique_id();
462 table_id = psiconv_buffer_unique_id();
463 if ((res = psiconv_write_u8(config,buf,0x06)))
464 goto ERROR2;
465 if ((res = psiconv_write_u32(config,buf,PSICONV_ID_OBJECT_DISPLAY_SECTION)))
466 goto ERROR2;
467 if ((res = psiconv_buffer_add_reference(buf,display_id)))
468 goto ERROR2;
469 if ((res = psiconv_write_u32(config,buf,PSICONV_ID_OBJECT_ICON_SECTION)))
470 goto ERROR2;
471 if ((res = psiconv_buffer_add_reference(buf,icon_id)))
472 goto ERROR2;
473 if ((res = psiconv_write_u32(config,buf,PSICONV_ID_OBJECT_SECTION_TABLE_SECTION)))
474 goto ERROR2;
475 if ((res = psiconv_buffer_add_reference(buf,table_id)))
476 goto ERROR2;
477
478 if ((res = psiconv_buffer_add_target(buf,display_id)))
479 goto ERROR2;
480 if ((res = psiconv_write_object_display_section(config,buf,value->display)))
481 goto ERROR2;
482 if ((res = psiconv_buffer_add_target(buf,icon_id)))
483 goto ERROR2;
484 if ((res = psiconv_write_object_icon_section(config,buf,value->icon)))
485 goto ERROR2;
486 if ((res = psiconv_buffer_add_target(buf,table_id)))
487 goto ERROR2;
488 switch(value->object->type) {
489 case psiconv_word_file:
490 if ((res = psiconv_write_word_file(config,extra_buf,
491 (psiconv_word_f) value->object->file)))
492 goto ERROR2;
493 break;
494 case psiconv_sketch_file:
495 if ((res = psiconv_write_sketch_file(config,extra_buf,
496 (psiconv_sketch_f) value->object->file)))
497 goto ERROR2;
498 break;
499 /*
500 case psiconv_sheet_file:
501 if ((res = psiconv_write_sheet_file(config,extra_buf,
502 (psiconv_sheet_f) value->object->file)))
503 goto ERROR2;
504 break;
505 */
506 default:
507 psiconv_warn(config,0,psiconv_buffer_length(buf),
508 "Unknown or unsupported object type");
509 res = -PSICONV_E_GENERATE;
510 goto ERROR2;
511 }
512
513 if ((res = psiconv_buffer_resolve(extra_buf)))
514 goto ERROR2;
515 if ((res = psiconv_buffer_concat(buf,extra_buf)))
516 goto ERROR2;
517
518 return 0;
519
520 ERROR2:
521 psiconv_buffer_free(extra_buf);
522 ERROR1:
523 return res;
524 }
525
526
527 int psiconv_write_object_display_section(const psiconv_config config,
528 psiconv_buffer buf,
529 const psiconv_object_display_section value)
530 {
531 int res;
532
533 if (!value) {
534 psiconv_warn(config,0,psiconv_buffer_length(buf),"Null Object Display Section");
535 res = -PSICONV_E_GENERATE;
536 goto ERROR1;
537 }
538
539 if ((res = psiconv_write_u8(config,buf,value->show_icon?0x00:0x01)))
540 goto ERROR1;
541 if ((res = psiconv_write_length(config,buf,value->width)))
542 goto ERROR1;
543 if ((res = psiconv_write_length(config,buf,value->height)))
544 goto ERROR1;
545 if ((res = psiconv_write_u32(config,buf,0x00000000)))
546 goto ERROR1;
547
548 return 0;
549
550 ERROR1:
551 return res;
552 }
553
554 int psiconv_write_object_icon_section(const psiconv_config config,
555 psiconv_buffer buf,
556 const psiconv_object_icon_section value)
557 {
558 int res;
559
560 if (!value) {
561 psiconv_warn(config,0,psiconv_buffer_length(buf),"Null Object Icon Section");
562 res = -PSICONV_E_GENERATE;
563 goto ERROR1;
564 }
565
566 if ((res = psiconv_write_string(config,buf,value->icon_name)))
567 goto ERROR1;
568 if ((res = psiconv_write_length(config,buf,value->icon_width)))
569 goto ERROR1;
570 if ((res = psiconv_write_length(config,buf,value->icon_height)))
571 goto ERROR1;
572
573 return 0;
574
575 ERROR1:
576 return res;
577 }

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