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