1 |
/* |
2 |
parse_layout.c - Part of psiconv, a PSION 5 file formats converter |
3 |
Copyright (c) 1999-2014 Frodo Looijaard <frodo@frodo.looijaard.name> |
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 <stdlib.h> |
24 |
#include <math.h> |
25 |
|
26 |
#include "parse_routines.h" |
27 |
#include "error.h" |
28 |
|
29 |
#ifdef DMALLOC |
30 |
#include <dmalloc.h> |
31 |
#endif |
32 |
|
33 |
|
34 |
int psiconv_parse_color(const psiconv_config config, |
35 |
const psiconv_buffer buf, int lev, psiconv_u32 off, |
36 |
int *length, psiconv_color *result) |
37 |
{ |
38 |
int res = 0; |
39 |
int len = 0; |
40 |
|
41 |
psiconv_progress(config,lev+1,off,"Going to parse color"); |
42 |
if (!(*result = malloc(sizeof(**result)))) |
43 |
goto ERROR1; |
44 |
|
45 |
(*result)->red = psiconv_read_u8(config,buf,lev+2,off+len,&res); |
46 |
if (res) |
47 |
goto ERROR2; |
48 |
(*result)->green = psiconv_read_u8(config,buf,lev+2,off+len+1,&res); |
49 |
if (res) |
50 |
goto ERROR2; |
51 |
(*result)->blue = psiconv_read_u8(config,buf,lev+2,off+len+2,&res); |
52 |
if (res) |
53 |
goto ERROR2; |
54 |
len += 3; |
55 |
|
56 |
psiconv_debug(config,lev+2,off,"Color: red %02x, green %02x, blue %02x", |
57 |
(*result)->red, (*result)->green, (*result)->blue); |
58 |
if (length) |
59 |
*length = len; |
60 |
|
61 |
psiconv_progress(config,lev+1,off+len-1,"End of color (total length: %08x)",len); |
62 |
return 0; |
63 |
|
64 |
ERROR2: |
65 |
free(*result); |
66 |
ERROR1: |
67 |
psiconv_error(config,lev+1,off,"Reading of Color failed"); |
68 |
if (length) |
69 |
*length = 0; |
70 |
if (res == 0) |
71 |
return -PSICONV_E_NOMEM; |
72 |
else |
73 |
return res; |
74 |
} |
75 |
|
76 |
|
77 |
|
78 |
int psiconv_parse_font(const psiconv_config config, |
79 |
const psiconv_buffer buf, int lev, psiconv_u32 off, |
80 |
int *length, psiconv_font *result) |
81 |
{ |
82 |
int res = 0; |
83 |
char *str_copy; |
84 |
int len=0; |
85 |
int fontlen; |
86 |
|
87 |
psiconv_progress(config,lev+1,off,"Going to parse font"); |
88 |
if (!(*result = malloc(sizeof(**result)))) |
89 |
goto ERROR1; |
90 |
|
91 |
fontlen = psiconv_read_u8(config,buf,lev+2,off,&res); |
92 |
if (res) |
93 |
goto ERROR2; |
94 |
len = 1; |
95 |
|
96 |
(*result)->name = psiconv_read_charlist(config,buf,lev+2,off+len, fontlen-1,&res); |
97 |
if (res) |
98 |
goto ERROR2; |
99 |
len += fontlen - 1; |
100 |
|
101 |
(*result)->screenfont = psiconv_read_u8(config,buf,lev+2,off+len,&res); |
102 |
if (res) |
103 |
goto ERROR3; |
104 |
|
105 |
if (!(str_copy = psiconv_make_printable(config,(*result)->name))) |
106 |
goto ERROR3; |
107 |
|
108 |
psiconv_debug(config,lev+2,off+len, |
109 |
"Found font `%s', displayed with screen font %02x", |
110 |
str_copy,(*result)->screenfont); |
111 |
free(str_copy); |
112 |
len ++; |
113 |
|
114 |
if (length) |
115 |
*length = len; |
116 |
|
117 |
psiconv_progress(config,lev+1,off + len - 1, |
118 |
"End of font (total length: %08x)",len); |
119 |
return 0; |
120 |
|
121 |
ERROR3: |
122 |
free ((*result)->name); |
123 |
ERROR2: |
124 |
free (*result); |
125 |
ERROR1: |
126 |
psiconv_error(config,lev+1,off,"Reading of Font failed"); |
127 |
if (length) |
128 |
*length = 0; |
129 |
if (!res) |
130 |
return -PSICONV_E_NOMEM; |
131 |
else |
132 |
return res; |
133 |
} |
134 |
|
135 |
int psiconv_parse_border(const psiconv_config config, |
136 |
const psiconv_buffer buf,int lev,psiconv_u32 off, |
137 |
int *length, psiconv_border *result) |
138 |
{ |
139 |
int res = 0; |
140 |
int len = 0; |
141 |
psiconv_u32 temp; |
142 |
int leng; |
143 |
|
144 |
psiconv_progress(config,lev+1,off,"Going to parse border data"); |
145 |
if (!(*result = malloc(sizeof(**result)))) { |
146 |
goto ERROR1; |
147 |
} |
148 |
|
149 |
psiconv_progress(config,lev+2,off+len,"Going to read border kind"); |
150 |
temp = psiconv_read_u8(config,buf,lev+2,off+len,&res); |
151 |
if (res) |
152 |
goto ERROR2; |
153 |
if (temp == 0x00) |
154 |
(*result)->kind = psiconv_border_none; |
155 |
else if (temp == 0x01) |
156 |
(*result)->kind = psiconv_border_solid; |
157 |
else if (temp == 0x02) |
158 |
(*result)->kind = psiconv_border_double; |
159 |
else if (temp == 0x03) |
160 |
(*result)->kind = psiconv_border_dotted; |
161 |
else if (temp == 0x04) |
162 |
(*result)->kind = psiconv_border_dashed; |
163 |
else if (temp == 0x05) |
164 |
(*result)->kind = psiconv_border_dotdashed; |
165 |
else if (temp == 0x06) |
166 |
(*result)->kind = psiconv_border_dotdotdashed; |
167 |
else { |
168 |
psiconv_warn(config,lev+2,off,"Unknown border kind (defaults to `none')"); |
169 |
(*result)->kind = psiconv_border_none; |
170 |
} |
171 |
psiconv_debug(config,lev+2,off+len,"Kind: %02x",temp); |
172 |
len ++; |
173 |
|
174 |
psiconv_progress(config,lev+2,off+len,"Going to read border thickness"); |
175 |
(*result)->thickness = psiconv_read_size(config,buf,lev+2,off+len,&leng,&res); |
176 |
if (res) |
177 |
goto ERROR2; |
178 |
#if 0 |
179 |
/* This seems no longer necessary to test? */ |
180 |
if (((*result)->kind != psiconv_border_solid) && |
181 |
((*result)->kind != psiconv_border_double) && |
182 |
((*result)->thickness != 0.0) && |
183 |
(fabs((*result)->thickness - 1/20) >= 1/1000)) { |
184 |
psiconv_warn(config,lev+2,off, |
185 |
"Border thickness specified for unlikely border type"); |
186 |
} |
187 |
#endif |
188 |
psiconv_debug(config,lev+2,off+len,"Thickness: %f",(*result)->thickness); |
189 |
len += leng; |
190 |
|
191 |
psiconv_progress(config,lev+2,off+len,"Going to read the border color"); |
192 |
if ((psiconv_parse_color(config,buf,lev+2,off+len,&leng,&(*result)->color))) |
193 |
goto ERROR2; |
194 |
len += leng; |
195 |
|
196 |
psiconv_progress(config,lev+2,off+len,"Going to read the final unknown byte " |
197 |
"(0x00 or 0x01 expected)"); |
198 |
temp = psiconv_read_u8(config,buf,lev+2,off + len,&res); |
199 |
if (res) |
200 |
goto ERROR3; |
201 |
if ((temp != 0x01) && (temp != 0x00)) { |
202 |
psiconv_warn(config,lev+2,off,"Unknown last byte in border specification"); |
203 |
psiconv_debug(config,lev+2,off+len, "Last byte: read %02x, expected %02x or %02x", |
204 |
temp,0x00,0x01); |
205 |
} |
206 |
len ++; |
207 |
|
208 |
if (length) |
209 |
*length = len; |
210 |
|
211 |
psiconv_progress(config,lev+1,off + len - 1, |
212 |
"End of border (total length: %08x)",len); |
213 |
|
214 |
return 0; |
215 |
|
216 |
ERROR3: |
217 |
psiconv_free_color((*result)->color); |
218 |
ERROR2: |
219 |
free (result); |
220 |
ERROR1: |
221 |
psiconv_error(config,lev+1,off,"Reading of Border failed"); |
222 |
if (length) |
223 |
*length = 0; |
224 |
if (!res) |
225 |
return -PSICONV_E_NOMEM; |
226 |
else |
227 |
return res; |
228 |
} |
229 |
|
230 |
int psiconv_parse_bullet(const psiconv_config config, |
231 |
const psiconv_buffer buf,int lev,psiconv_u32 off, |
232 |
int *length, psiconv_bullet *result) |
233 |
{ |
234 |
int res = 0; |
235 |
int len = 0; |
236 |
int leng; |
237 |
int bullet_length; |
238 |
|
239 |
if (!(*result = malloc(sizeof(**result)))) |
240 |
goto ERROR1; |
241 |
(*result)->on = psiconv_bool_true; |
242 |
|
243 |
psiconv_progress(config,lev+1,off,"Going to parse bullet data"); |
244 |
psiconv_progress(config,lev+2,off+len,"Going to read bullet length"); |
245 |
bullet_length = psiconv_read_u8(config,buf,lev+2,off+len,&res); |
246 |
if (res) |
247 |
goto ERROR2; |
248 |
psiconv_debug(config,lev+2,off+len,"Length: %02x",bullet_length); |
249 |
len ++; |
250 |
|
251 |
psiconv_progress(config,lev+2,off+len,"Going to read bullet font size"); |
252 |
(*result)->font_size = psiconv_read_size(config,buf,lev+2,off+len, &leng,&res); |
253 |
if (res) |
254 |
goto ERROR2; |
255 |
len +=leng; |
256 |
|
257 |
psiconv_progress(config,lev+2,off+len,"Going to read bullet character"); |
258 |
(*result)->character = psiconv_unicode_read_char(config,buf,lev+2, |
259 |
off+len,&leng,&res); |
260 |
if (res) |
261 |
goto ERROR2; |
262 |
psiconv_debug(config,lev+2,off+len,"Character: %02x",(*result)->character); |
263 |
len += leng; |
264 |
|
265 |
psiconv_progress(config,lev+2,off+len,"Going to read indent on/off"); |
266 |
if ((res = psiconv_parse_bool(config,buf,lev+2,off+len,&leng,&(*result)->indent))) |
267 |
goto ERROR2; |
268 |
psiconv_debug(config,lev+2,off+len,"Indent on: %02x",(*result)->indent); |
269 |
len += leng; |
270 |
|
271 |
psiconv_progress(config,lev+2,off+len,"Going to read bullet color"); |
272 |
if ((res = psiconv_parse_color(config,buf,lev+2,off+len,&leng,&(*result)->color))) |
273 |
goto ERROR2; |
274 |
len += leng; |
275 |
|
276 |
psiconv_progress(config,lev+2,off+len,"Going to read bullet font"); |
277 |
if ((res = psiconv_parse_font(config,buf,lev+2,off+len,&leng,&(*result)->font))) |
278 |
goto ERROR3; |
279 |
len += leng; |
280 |
|
281 |
if (len != bullet_length + 1) { |
282 |
psiconv_warn(config,lev+2,off,"Bullet data structure length mismatch"); |
283 |
psiconv_debug(config,lev+2,off,"Length: specified %02x, found %02x", |
284 |
bullet_length,len-1); |
285 |
} |
286 |
|
287 |
psiconv_progress(config,lev+1,off + len - 1, |
288 |
"End of bullet data (total length: %08x)",len); |
289 |
|
290 |
if (length) |
291 |
*length = len; |
292 |
return 0; |
293 |
|
294 |
ERROR3: |
295 |
psiconv_free_color((*result)->color); |
296 |
ERROR2: |
297 |
free (result); |
298 |
ERROR1: |
299 |
psiconv_error(config,lev+1,off,"Reading of Bullet failed"); |
300 |
if (length) |
301 |
*length = 0; |
302 |
if (!res) |
303 |
return -PSICONV_E_NOMEM; |
304 |
else |
305 |
return res; |
306 |
} |
307 |
|
308 |
int psiconv_parse_tab(const psiconv_config config, |
309 |
const psiconv_buffer buf, int lev, psiconv_u32 off, |
310 |
int *length, psiconv_tab *result) |
311 |
{ |
312 |
int res = 0; |
313 |
int len = 0; |
314 |
int leng; |
315 |
psiconv_u8 temp; |
316 |
|
317 |
psiconv_progress(config,lev+1,off,"Going to parse tab"); |
318 |
if (!(*result = malloc(sizeof(**result)))) |
319 |
goto ERROR1; |
320 |
|
321 |
psiconv_progress(config,lev+2,off,"Going to read tab location"); |
322 |
(*result)->location = psiconv_read_length(config,buf,lev+2,off+len,&leng,&res); |
323 |
if (res) |
324 |
goto ERROR2; |
325 |
len += leng; |
326 |
|
327 |
psiconv_progress(config,lev+2,off+len,"Going to read the tab kind"); |
328 |
temp = psiconv_read_u8(config,buf,lev+2,off+len,&res); |
329 |
if (res) |
330 |
goto ERROR2; |
331 |
if (temp == 1) |
332 |
(*result)->kind = psiconv_tab_left; |
333 |
else if (temp == 2) |
334 |
(*result)->kind = psiconv_tab_centre; |
335 |
else if (temp == 3) |
336 |
(*result)->kind = psiconv_tab_right; |
337 |
else { |
338 |
psiconv_warn(config,lev+2,off+len,"Unknown tab kind argument"); |
339 |
psiconv_debug(config,lev+2,off+len,"Kind found: %02x (defaulted to left tab)", |
340 |
temp); |
341 |
(*result)->kind = psiconv_tab_left; |
342 |
} |
343 |
psiconv_debug(config,lev+2,off+len,"Kind: %02x",temp); |
344 |
len ++; |
345 |
|
346 |
if (length) |
347 |
*length = len; |
348 |
|
349 |
psiconv_progress(config,lev+1,off+len-1,"End of tab (total length: %08x)",len); |
350 |
return 0; |
351 |
|
352 |
ERROR2: |
353 |
free (result); |
354 |
ERROR1: |
355 |
psiconv_error(config,lev+1,off,"Reading of Tab failed"); |
356 |
if (length) |
357 |
*length = 0; |
358 |
if (!res) |
359 |
return -PSICONV_E_NOMEM; |
360 |
else |
361 |
return res; |
362 |
} |
363 |
|
364 |
int psiconv_parse_paragraph_layout_list(const psiconv_config config, |
365 |
const psiconv_buffer buf, int lev, |
366 |
psiconv_u32 off, int *length, |
367 |
psiconv_paragraph_layout result) |
368 |
{ |
369 |
int res=0; |
370 |
int len=0; |
371 |
int list_length,leng,nr; |
372 |
psiconv_u8 id; |
373 |
psiconv_u32 temp; |
374 |
psiconv_tab temp_tab; |
375 |
psiconv_color temp_color; |
376 |
psiconv_border temp_border; |
377 |
psiconv_bullet temp_bullet; |
378 |
|
379 |
psiconv_progress(config,lev+1,off,"Going to read paragraph layout list"); |
380 |
|
381 |
psiconv_progress(config,lev+2,off,"Going to read the list length"); |
382 |
list_length = psiconv_read_u32(config,buf,lev+2,off + len,&res); |
383 |
if (res) |
384 |
goto ERROR1; |
385 |
psiconv_debug(config,lev+2,off,"Length in bytes: %08x",list_length); |
386 |
len += 4; |
387 |
|
388 |
nr = 0; |
389 |
while(len - 4 < list_length) { |
390 |
psiconv_progress(config,lev+2,off+len,"Going to read element %d",nr); |
391 |
psiconv_progress(config,lev+3,off+len,"Going to read the element id"); |
392 |
id = psiconv_read_u8(config,buf,lev+2,off+len,&res); |
393 |
if (res) |
394 |
goto ERROR1; |
395 |
psiconv_debug(config,lev+3,off+len,"Id: %02x",id); |
396 |
len ++; |
397 |
switch(id) { |
398 |
case 0x01: |
399 |
psiconv_progress(config,lev+3,off+len,"Going to read background color"); |
400 |
if ((res = psiconv_parse_color(config,buf,lev+3,off+len,&leng,&temp_color))) |
401 |
goto ERROR1; |
402 |
psiconv_free_color(result->back_color); |
403 |
result->back_color = temp_color; |
404 |
len += leng; |
405 |
break; |
406 |
case 0x02: |
407 |
psiconv_progress(config,lev+3,off+len ,"Going to read indent left"); |
408 |
result->indent_left = psiconv_read_length(config,buf,lev+3,off+len,&leng,&res); |
409 |
if (res) |
410 |
goto ERROR1; |
411 |
len += leng; |
412 |
break; |
413 |
case 0x03: |
414 |
psiconv_progress(config,lev+3,off+len,"Going to read indent right"); |
415 |
result->indent_right = psiconv_read_length(config,buf,lev+2,off+len,&leng, |
416 |
&res); |
417 |
if (res) |
418 |
goto ERROR1; |
419 |
len += leng; |
420 |
break; |
421 |
case 0x04: |
422 |
psiconv_progress(config,lev+3,off+len,"Going to read indent left first line"); |
423 |
result->indent_first = psiconv_read_length(config,buf,lev+2,off+len, &leng, |
424 |
&res); |
425 |
if (res) |
426 |
goto ERROR1; |
427 |
len += leng; |
428 |
break; |
429 |
case 0x05: |
430 |
psiconv_progress(config,lev+3,off+len,"Going to read horizontal justify"); |
431 |
temp = psiconv_read_u8(config,buf,lev+3,off+len,&res); |
432 |
if (res) |
433 |
goto ERROR1; |
434 |
if (temp == 0x00) |
435 |
result->justify_hor = psiconv_justify_left; |
436 |
else if (temp == 0x01) |
437 |
result->justify_hor = psiconv_justify_centre; |
438 |
else if (temp == 0x02) |
439 |
result->justify_hor = psiconv_justify_right; |
440 |
else if (temp == 0x03) |
441 |
result->justify_hor = psiconv_justify_full; |
442 |
else { |
443 |
psiconv_warn(config,lev+3,off+len, "Unknown horizontal justify argument " |
444 |
"in paragraph layout codes list"); |
445 |
result->justify_hor = psiconv_justify_left; |
446 |
} |
447 |
psiconv_debug(config,lev+3,off+len,"Justify: %02x",temp); |
448 |
len ++; |
449 |
break; |
450 |
case 0x06: |
451 |
psiconv_progress(config,lev+3,off+len,"Going to read vertical justify"); |
452 |
temp = psiconv_read_u8(config,buf,lev+3,off+len,&res); |
453 |
if (res) |
454 |
goto ERROR1; |
455 |
if (temp == 0x00) |
456 |
result->justify_ver = psiconv_justify_top; |
457 |
else if (temp == 0x01) |
458 |
result->justify_ver = psiconv_justify_middle; |
459 |
else if (temp == 0x02) |
460 |
result->justify_ver = psiconv_justify_bottom; |
461 |
else { |
462 |
psiconv_warn(config,lev+3,off+len, "Unknown vertical justify argument " |
463 |
"in paragraph layout codes list"); |
464 |
result->justify_ver = psiconv_justify_bottom; |
465 |
} |
466 |
psiconv_debug(config,lev+3,off+len,"Justify: %02x",temp); |
467 |
len ++; |
468 |
break; |
469 |
case 0x07: |
470 |
psiconv_progress(config,lev+3,off+len,"Going to read linespacing distance"); |
471 |
result->linespacing = psiconv_read_size(config,buf,lev+3,off+len,&leng,&res); |
472 |
if (res) |
473 |
goto ERROR1; |
474 |
len += leng; |
475 |
break; |
476 |
case 0x08: |
477 |
psiconv_progress(config,lev+3,off+len,"Going to read linespacing exact"); |
478 |
if ((res = psiconv_parse_bool(config,buf,lev+3,off+len,&leng, |
479 |
&result->linespacing_exact))) |
480 |
goto ERROR1; |
481 |
len += leng; |
482 |
break; |
483 |
case 0x09: |
484 |
psiconv_progress(config,lev+3,off+len,"Going to read top space"); |
485 |
result->space_above = psiconv_read_size(config,buf,lev+3,off+len,&leng,&res); |
486 |
if (res) |
487 |
goto ERROR1; |
488 |
len += leng; |
489 |
break; |
490 |
case 0x0a: |
491 |
psiconv_progress(config,lev+3,off+len,"Going to read bottom space"); |
492 |
result->space_below = psiconv_read_size(config,buf,lev+3,off+len,&leng,&res); |
493 |
if (res) |
494 |
goto ERROR1; |
495 |
len += leng; |
496 |
break; |
497 |
case 0x0b: |
498 |
psiconv_progress(config,lev+3,off+len,"Going to read on one page"); |
499 |
if ((res = psiconv_parse_bool(config,buf,lev+3,off+len,&leng, |
500 |
&result->keep_together))) |
501 |
goto ERROR1; |
502 |
len += leng; |
503 |
break; |
504 |
case 0x0c: |
505 |
psiconv_progress(config,lev+3,off+len,"Going to read together with"); |
506 |
if ((res = psiconv_parse_bool(config,buf,lev+3,off+len,&leng, |
507 |
&result->keep_with_next))) |
508 |
goto ERROR1; |
509 |
len += leng; |
510 |
break; |
511 |
case 0x0d: |
512 |
psiconv_progress(config,lev+3,off+len,"Going to read on next page"); |
513 |
if ((res = psiconv_parse_bool(config,buf,lev+3,off+len,&leng, |
514 |
&result->on_next_page))) |
515 |
goto ERROR1; |
516 |
len += leng; |
517 |
break; |
518 |
case 0x0e: |
519 |
psiconv_progress(config,lev+3,off+len,"Going to read no widow protection"); |
520 |
if ((res = psiconv_parse_bool(config,buf,lev+3,off+len,&leng, |
521 |
&result->no_widow_protection))) |
522 |
goto ERROR1; |
523 |
len += leng; |
524 |
break; |
525 |
case 0x0f: |
526 |
psiconv_progress(config,lev+3,off+len,"Going to read wrap to fit cell limits"); |
527 |
if ((res = psiconv_parse_bool(config,buf,lev+3,off+len,&leng, |
528 |
&result->wrap_to_fit_cell))) |
529 |
goto ERROR1; |
530 |
len += leng; |
531 |
break; |
532 |
case 0x10: |
533 |
psiconv_progress(config,lev+3,off+len,"Going to read border distance to text"); |
534 |
result->border_distance = psiconv_read_length(config,buf,lev+3, |
535 |
off+len,&leng,&res); |
536 |
if (res) |
537 |
goto ERROR1; |
538 |
len += leng; |
539 |
break; |
540 |
case 0x11: |
541 |
psiconv_progress(config,lev+3,off+len,"Going to read top border"); |
542 |
if ((res = psiconv_parse_border(config,buf,lev+3,off+len,&leng,&temp_border))) |
543 |
goto ERROR1; |
544 |
psiconv_free_border(result->top_border); |
545 |
result->top_border = temp_border; |
546 |
len += leng; |
547 |
break; |
548 |
case 0x12: |
549 |
psiconv_progress(config,lev+3,off+len,"Going to read bottom border"); |
550 |
if ((res = psiconv_parse_border(config,buf,lev+3,off+len,&leng,&temp_border))) |
551 |
goto ERROR1; |
552 |
psiconv_free_border(result->bottom_border); |
553 |
result->bottom_border = temp_border; |
554 |
len += leng; |
555 |
break; |
556 |
case 0x13: |
557 |
psiconv_progress(config,lev+3,off+len,"Going to read left border"); |
558 |
if ((res = psiconv_parse_border(config,buf,lev+3,off+len,&leng,&temp_border))) |
559 |
goto ERROR1; |
560 |
psiconv_free_border(result->left_border); |
561 |
result->left_border = temp_border; |
562 |
len += leng; |
563 |
break; |
564 |
case 0x14: |
565 |
psiconv_progress(config,lev+3,off+len,"Going to read right border"); |
566 |
if ((res = psiconv_parse_border(config,buf,lev+3,off+len,&leng,&temp_border))) |
567 |
goto ERROR1; |
568 |
psiconv_free_border(result->right_border); |
569 |
result->right_border = temp_border; |
570 |
len += leng; |
571 |
break; |
572 |
case 0x15: |
573 |
psiconv_progress(config,lev+3,off+len,"Going to read bullet"); |
574 |
if ((res = psiconv_parse_bullet(config,buf,lev+3,off+len,&leng,&temp_bullet))) |
575 |
goto ERROR1; |
576 |
psiconv_free_bullet(result->bullet); |
577 |
result->bullet = temp_bullet; |
578 |
len += leng; |
579 |
break; |
580 |
case 0x16: |
581 |
psiconv_progress(config,lev+3,off+len,"Going to read standard tabs"); |
582 |
result->tabs->normal = psiconv_read_length(config,buf,lev+3,off+len,&leng, |
583 |
&res); |
584 |
if (res) |
585 |
goto ERROR1; |
586 |
len += leng; |
587 |
break; |
588 |
case 0x17: |
589 |
psiconv_progress(config,lev+3,off+len,"Going to read extra tab"); |
590 |
if ((res = psiconv_parse_tab(config,buf,lev+3,off+len,&leng,&temp_tab))) |
591 |
goto ERROR1; |
592 |
if ((res = psiconv_list_add(result->tabs->extras,temp_tab))) { |
593 |
psiconv_free_tab(temp_tab); |
594 |
goto ERROR1; |
595 |
} |
596 |
psiconv_free_tab(temp_tab); |
597 |
len += leng; |
598 |
break; |
599 |
default: |
600 |
psiconv_warn(config,lev+3,off+len, |
601 |
"Unknown code in paragraph layout codes list"); |
602 |
psiconv_debug(config,lev+3,off+len,"Code: %02x",id); |
603 |
len ++; |
604 |
break; |
605 |
} |
606 |
nr ++; |
607 |
} |
608 |
|
609 |
if (len - 4 != list_length) { |
610 |
psiconv_error(config,lev+2,off+len, |
611 |
"Read past end of paragraph layout codes list. I probably lost track " |
612 |
"somewhere!"); |
613 |
psiconv_debug(config,lev+2,off+len,"Read %d characters instead of %d", |
614 |
len-4,list_length); |
615 |
res = PSICONV_E_PARSE; |
616 |
goto ERROR1; |
617 |
} |
618 |
|
619 |
len = list_length + 4; |
620 |
|
621 |
psiconv_progress(config,lev+1,off+len, |
622 |
"End of paragraph layout list (total length: %08x)",len); |
623 |
|
624 |
if (length) |
625 |
*length = len; |
626 |
return 0; |
627 |
|
628 |
ERROR1: |
629 |
psiconv_error(config,lev+1,off,"Reading of paragraph_layout_list failed"); |
630 |
if (length) |
631 |
*length = 0; |
632 |
if (!res) |
633 |
return -PSICONV_E_NOMEM; |
634 |
else |
635 |
return res; |
636 |
} |
637 |
|
638 |
int psiconv_parse_character_layout_list(const psiconv_config config, |
639 |
const psiconv_buffer buf, int lev, |
640 |
psiconv_u32 off, int *length, |
641 |
psiconv_character_layout result) |
642 |
{ |
643 |
int res=0; |
644 |
int len=0; |
645 |
int list_length,leng,nr; |
646 |
psiconv_u8 id; |
647 |
psiconv_u32 temp; |
648 |
psiconv_color temp_color; |
649 |
psiconv_font temp_font; |
650 |
|
651 |
psiconv_progress(config,lev+1,off,"Going to read character layout codes"); |
652 |
|
653 |
psiconv_progress(config,lev+2,off,"Going to read the list length"); |
654 |
list_length = psiconv_read_u32(config,buf,lev+2,off + len,&res); |
655 |
if (res) |
656 |
goto ERROR1; |
657 |
psiconv_debug(config,lev+2,off,"Length in bytes: %08x",list_length); |
658 |
len += 4; |
659 |
|
660 |
nr = 0; |
661 |
while(len-4 < list_length) { |
662 |
psiconv_progress(config,lev+2,off+len,"Going to read element %d",nr); |
663 |
psiconv_progress(config,lev+3,off+len,"Going to read the element id"); |
664 |
id = psiconv_read_u8(config,buf,lev+2,off+len,&res); |
665 |
if (res) |
666 |
goto ERROR1; |
667 |
psiconv_debug(config,lev+3,off+len,"Id: %02x",id); |
668 |
len ++; |
669 |
switch(id) { |
670 |
case 0x18: |
671 |
psiconv_progress(config,lev+3,off+len,"Going to skip an unknown setting"); |
672 |
len ++; |
673 |
break; |
674 |
case 0x19: |
675 |
psiconv_progress(config,lev+3,off+len,"Going to read text color"); |
676 |
if ((res = psiconv_parse_color(config,buf,lev+3,off+len, &leng,&temp_color))) |
677 |
goto ERROR1; |
678 |
psiconv_free_color(result->color); |
679 |
result->color = temp_color; |
680 |
len += leng; |
681 |
break; |
682 |
case 0x1a: |
683 |
psiconv_progress(config,lev+3,off+len,"Going to read background color (?)"); |
684 |
if ((res = psiconv_parse_color(config,buf,lev+2,off+len, &leng,&temp_color))) |
685 |
goto ERROR1; |
686 |
psiconv_free_color(result->back_color); |
687 |
result->back_color = temp_color; |
688 |
len += leng; |
689 |
break; |
690 |
case 0x1b: |
691 |
psiconv_progress(config,lev+3,off+len,"Going to skip an unknown setting"); |
692 |
len ++; |
693 |
break; |
694 |
case 0x1c: |
695 |
psiconv_progress(config,lev+3,off+len,"Going to read font size"); |
696 |
result->font_size = psiconv_read_size(config,buf,lev+3,off+len,&leng,&res); |
697 |
if (res) |
698 |
goto ERROR1; |
699 |
len += leng; |
700 |
break; |
701 |
case 0x1d: |
702 |
psiconv_progress(config,lev+3,off+len,"Going to read italic"); |
703 |
if ((res = psiconv_parse_bool(config,buf,lev+3,off+len,&leng,&result->italic))) |
704 |
goto ERROR1; |
705 |
len += leng; |
706 |
break; |
707 |
case 0x1e: |
708 |
psiconv_progress(config,lev+3,off+len,"Going to read bold"); |
709 |
if ((res = psiconv_parse_bool(config,buf,lev+3,off+len,&leng,&result->bold))) |
710 |
goto ERROR1; |
711 |
len += leng; |
712 |
break; |
713 |
case 0x1f: |
714 |
psiconv_progress(config,lev+3,off+len,"Going to read super_sub"); |
715 |
temp = psiconv_read_u8(config,buf,lev+3,off+len,&res); |
716 |
if (res) |
717 |
goto ERROR1; |
718 |
if (temp == 0x00) |
719 |
result->super_sub = psiconv_normalscript; |
720 |
else if (temp == 0x01) |
721 |
result->super_sub = psiconv_superscript; |
722 |
else if (temp == 0x02) |
723 |
result->super_sub = psiconv_subscript; |
724 |
else { |
725 |
psiconv_warn(config,lev+3,off+len, |
726 |
"Unknown super_sub argument in character layout codes list"); |
727 |
} |
728 |
psiconv_debug(config,lev+3,off+len,"Super_sub: %02x",temp); |
729 |
len ++; |
730 |
break; |
731 |
case 0x20: |
732 |
psiconv_progress(config,lev+3,off+len,"Going to read underline"); |
733 |
if ((res = psiconv_parse_bool(config,buf,lev+3,off+len,&leng, |
734 |
&result->underline))) |
735 |
goto ERROR1; |
736 |
len += leng; |
737 |
break; |
738 |
case 0x21: |
739 |
psiconv_progress(config,lev+3,off+len,"Going to read strikethrough"); |
740 |
if ((res = psiconv_parse_bool(config,buf,lev+3,off+len,&leng, |
741 |
&result->strikethrough))) |
742 |
goto ERROR1; |
743 |
len += leng; |
744 |
break; |
745 |
case 0x22: |
746 |
psiconv_progress(config,lev+3,off+len,"Going to read font"); |
747 |
if ((res = psiconv_parse_font(config,buf,lev+3,off+len, &leng, &temp_font))) |
748 |
goto ERROR1; |
749 |
psiconv_free_font(result->font); |
750 |
result->font = temp_font; |
751 |
len += leng; |
752 |
break; |
753 |
case 0x23: |
754 |
psiconv_progress(config,lev+3,off+len,"Going to skip an unknown setting"); |
755 |
len ++; |
756 |
break; |
757 |
case 0x24: |
758 |
psiconv_progress(config,lev+3,off+len, |
759 |
"Going to read unknown code 0x24 (%02x expected)", 0); |
760 |
temp = psiconv_read_u8(config,buf,lev+3,off+len,&res); |
761 |
if (res) |
762 |
goto ERROR1; |
763 |
if (temp != 0) { |
764 |
psiconv_warn(config,lev+3,off+len, |
765 |
"Unknown code 0x24 value != 0x0 (0x%02x)", temp); |
766 |
} |
767 |
len ++; |
768 |
break; |
769 |
default: |
770 |
psiconv_warn(config,lev+3,off+len,"Unknown code in character layout list"); |
771 |
psiconv_debug(config,lev+3,off+len,"Code: %02x",id); |
772 |
len ++; |
773 |
break; |
774 |
} |
775 |
nr ++; |
776 |
} |
777 |
|
778 |
if (len - 4 != list_length) { |
779 |
psiconv_error(config,lev+2,off+len, |
780 |
"Read past end of character layout codes list. I probably lost track " |
781 |
"somewhere!"); |
782 |
psiconv_debug(config,lev+2,off+len,"Read %d characters instead of %d", |
783 |
len-4,list_length); |
784 |
res = PSICONV_E_PARSE; |
785 |
goto ERROR1; |
786 |
} |
787 |
|
788 |
len = list_length + 4; |
789 |
|
790 |
psiconv_progress(config,lev+1,off+len, |
791 |
"End of character layout list (total length: %08x)",len); |
792 |
|
793 |
if (length) |
794 |
*length = len; |
795 |
return res; |
796 |
|
797 |
ERROR1: |
798 |
psiconv_error(config,lev+1,off,"Reading of character_layout_list failed"); |
799 |
if (length) |
800 |
*length = 0; |
801 |
if (!res) |
802 |
return -PSICONV_E_NOMEM; |
803 |
else |
804 |
return res; |
805 |
} |