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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 166 Revision 167
132 psiconv_character_layout character; 132 psiconv_character_layout character;
133 psiconv_paragraph_layout paragraph; 133 psiconv_paragraph_layout paragraph;
134 psiconv_u8 style; 134 psiconv_u8 style;
135 psiconv_u8 nr; 135 psiconv_u8 nr;
136 } *psiconv_paragraph_type_list; 136 } *psiconv_paragraph_type_list;
137 psiconv_u32 obj_id;
137 psiconv_list paragraph_type_list; /* Of psiconv_paragraph_type_list_s */ 138 psiconv_list paragraph_type_list; /* Of psiconv_paragraph_type_list_s */
138 psiconv_paragraph_type_list paragraph_type; 139 psiconv_paragraph_type_list paragraph_type;
139 struct psiconv_paragraph_type_list_s new_type; 140 struct psiconv_paragraph_type_list_s new_type;
140 psiconv_buffer buf_types,buf_elements,buf_inlines; 141 psiconv_buffer buf_types,buf_elements,buf_inlines,buf_objects;
141 psiconv_paragraph paragraph; 142 psiconv_paragraph paragraph;
142 psiconv_in_line_layout in_line; 143 psiconv_in_line_layout in_line = NULL;
143 psiconv_word_style style; 144 psiconv_word_style style;
144 psiconv_character_layout para_charlayout; 145 psiconv_character_layout para_charlayout;
145 int i,j,para_type,nr_of_inlines=0,res,ptl_length,pel_length,thislen,paralen; 146 int i,j,para_type,nr_of_inlines=0,res,ptl_length,pel_length,thislen,paralen;
146 147
147 if (!value) { 148 if (!value) {
165 } 166 }
166 167
167 if (!(buf_inlines = psiconv_buffer_new())) { 168 if (!(buf_inlines = psiconv_buffer_new())) {
168 res = -PSICONV_E_NOMEM; 169 res = -PSICONV_E_NOMEM;
169 goto ERROR4; 170 goto ERROR4;
171 }
172
173 if (!(buf_objects = psiconv_buffer_new())) {
174 res = -PSICONV_E_NOMEM;
175 goto ERROR5;
170 } 176 }
171 177
172 for (i = 0; i < psiconv_list_length(value); i++) { 178 for (i = 0; i < psiconv_list_length(value); i++) {
173 if (!(paragraph = psiconv_list_get(value,i))) { 179 if (!(paragraph = psiconv_list_get(value,i))) {
174 psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption"); 180 psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption");
175 res = -PSICONV_E_OTHER; 181 res = -PSICONV_E_OTHER;
176 goto ERROR5; 182 goto ERROR6;
177 } 183 }
178 if ((res = psiconv_write_u32(buf_elements,strlen(paragraph->text)+1))) 184 if ((res = psiconv_write_u32(buf_elements,strlen(paragraph->text)+1)))
179 goto ERROR5; 185 goto ERROR6;
180 186
187 /* We need it for the next if-statement */
188 if (psiconv_list_length(paragraph->in_lines) == 1)
189 if (!(in_line = psiconv_list_get(paragraph->in_lines,1)))
190 goto ERROR6;
191
181 if (psiconv_list_length(paragraph->in_lines) > 1) { 192 if ((psiconv_list_length(paragraph->in_lines) > 1) ||
193 ((psiconv_list_length(paragraph->in_lines) == 1) &&
194 (in_line->object != NULL))) {
182 /* Inline layouts, so we generate a paragraph element and inline 195 /* Inline layouts, or an object, so we generate a paragraph element
183 elements */ 196 and inline elements */
184 if ((res = psiconv_write_u8(buf_elements,0x00))) 197 if ((res = psiconv_write_u8(buf_elements,0x00)))
185 goto ERROR5; 198 goto ERROR6;
186 if (!(style = psiconv_get_style(styles,paragraph->base_style))) { 199 if (!(style = psiconv_get_style(styles,paragraph->base_style))) {
187 psiconv_warn(0,psiconv_buffer_length(buf),"Unknown style"); 200 psiconv_warn(0,psiconv_buffer_length(buf),"Unknown style");
188 res = -PSICONV_E_GENERATE; 201 res = -PSICONV_E_GENERATE;
189 goto ERROR5; 202 goto ERROR6;
190 } 203 }
191 if ((res = psiconv_write_paragraph_layout_list(buf_elements, 204 if ((res = psiconv_write_paragraph_layout_list(buf_elements,
192 paragraph->base_paragraph, 205 paragraph->base_paragraph,
193 style->paragraph))) 206 style->paragraph)))
194 goto ERROR5; 207 goto ERROR6;
195 if (with_styles) 208 if (with_styles)
196 if ((res = psiconv_write_u8(buf_elements,paragraph->base_style))) 209 if ((res = psiconv_write_u8(buf_elements,paragraph->base_style)))
197 goto ERROR5; 210 goto ERROR6;
198 if ((res = psiconv_write_u32(buf_elements, 211 if ((res = psiconv_write_u32(buf_elements,
199 psiconv_list_length(paragraph->in_lines)))) 212 psiconv_list_length(paragraph->in_lines))))
200 goto ERROR5; 213 goto ERROR6;
201 214
202 /* Generate the inlines. NB: Against what are all settings relative?!? */ 215 /* Generate the inlines. NB: Against what are all settings relative?!? */
203 paralen = 0; 216 paralen = 0;
204 for (j = 0; j < psiconv_list_length(paragraph->in_lines); j++) { 217 for (j = 0; j < psiconv_list_length(paragraph->in_lines); j++) {
205 nr_of_inlines ++; 218 nr_of_inlines ++;
206 if (!(in_line = psiconv_list_get(paragraph->in_lines,j))) { 219 if (!(in_line = psiconv_list_get(paragraph->in_lines,j))) {
207 psiconv_warn(0,psiconv_buffer_length(buf), 220 psiconv_warn(0,psiconv_buffer_length(buf),
208 "Massive memory corruption"); 221 "Massive memory corruption");
209 res = -PSICONV_E_OTHER; 222 res = -PSICONV_E_OTHER;
210 goto ERROR5; 223 goto ERROR6;
211 } 224 }
212 if ((res = psiconv_write_u8(buf_inlines,0x00))) 225 if ((res = psiconv_write_u8(buf_inlines,in_line->object?0x01:0x00)))
213 goto ERROR5; 226 goto ERROR6;
214 thislen = in_line->length; 227 thislen = in_line->length;
215 paralen += thislen; 228 paralen += thislen;
216 /* If this is the last in_line, we need to make sure that the 229 /* If this is the last in_line, we need to make sure that the
217 complete length of all inlines equals the text length */ 230 complete length of all inlines equals the text length */
218 if (j == psiconv_list_length(paragraph->in_lines)-1) { 231 if (j == psiconv_list_length(paragraph->in_lines)-1) {
219 if (paralen > strlen(paragraph->text)+1) { 232 if (paralen > strlen(paragraph->text)+1) {
220 res = -PSICONV_E_GENERATE; 233 res = -PSICONV_E_GENERATE;
221 goto ERROR5; 234 goto ERROR6;
222 } 235 }
223 thislen += strlen(paragraph->text)+1-paralen; 236 thislen += strlen(paragraph->text)+1-paralen;
224 } 237 }
225 if ((res = psiconv_write_u32(buf_inlines,thislen))) 238 if ((res = psiconv_write_u32(buf_inlines,thislen)))
226 goto ERROR5; 239 goto ERROR6;
227 if ((res = psiconv_write_character_layout_list(buf_inlines, 240 if ((res = psiconv_write_character_layout_list(buf_inlines,
228 in_line->layout, 241 in_line->layout,
229 style->character))) 242 style->character)))
230 goto ERROR5; 243 goto ERROR6;
244 if (in_line->object) {
245 if ((res = psiconv_write_u32(buf_inlines,PSICONV_ID_OBJECT)))
246 goto ERROR6;
247 obj_id = psiconv_buffer_unique_id();
248 if ((res = psiconv_buffer_add_reference(buf_inlines,obj_id)))
249 goto ERROR6;
250 if ((res = psiconv_buffer_add_target(buf_objects,obj_id)))
251 goto ERROR6;
252 if ((res = psiconv_write_embedded_object_section(buf_objects,
253 in_line->object)))
254 goto ERROR6;
255 if ((res = psiconv_write_length(buf_inlines,in_line->object_width)))
256 goto ERROR6;
257 if ((res = psiconv_write_length(buf_inlines,in_line->object_height)))
258 goto ERROR6;
259 }
231 } 260 }
232 } else { 261 } else {
233 /* No inline layouts (or only 1), so we generate a paragraph type list */ 262 /* No inline layouts (or only 1), so we generate a paragraph type list */
234 para_type = 0; 263 para_type = 0;
235 /* Set para_charlayout to the correct character-level layout */ 264 /* Set para_charlayout to the correct character-level layout */
238 else { 267 else {
239 if (!(in_line = psiconv_list_get(paragraph->in_lines,0))) { 268 if (!(in_line = psiconv_list_get(paragraph->in_lines,0))) {
240 psiconv_warn(0,psiconv_buffer_length(buf), 269 psiconv_warn(0,psiconv_buffer_length(buf),
241 "Massive memory corruption"); 270 "Massive memory corruption");
242 res = -PSICONV_E_OTHER; 271 res = -PSICONV_E_OTHER;
243 goto ERROR5; 272 goto ERROR6;
244 } 273 }
245 para_charlayout = in_line->layout; 274 para_charlayout = in_line->layout;
246 } 275 }
247 for (j = 0; j < psiconv_list_length(paragraph_type_list); j++) { 276 for (j = 0; j < psiconv_list_length(paragraph_type_list); j++) {
248 if (!(paragraph_type = psiconv_list_get(paragraph_type_list,j))) { 277 if (!(paragraph_type = psiconv_list_get(paragraph_type_list,j))) {
249 psiconv_warn(0,psiconv_buffer_length(buf), 278 psiconv_warn(0,psiconv_buffer_length(buf),
250 "Massive memory corruption"); 279 "Massive memory corruption");
251 res = -PSICONV_E_OTHER; 280 res = -PSICONV_E_OTHER;
252 goto ERROR5; 281 goto ERROR6;
253 } 282 }
254 if ((paragraph->base_style == paragraph_type->style) && 283 if ((paragraph->base_style == paragraph_type->style) &&
255 !psiconv_compare_character_layout(para_charlayout, 284 !psiconv_compare_character_layout(para_charlayout,
256 paragraph_type->character) && 285 paragraph_type->character) &&
257 !psiconv_compare_paragraph_layout(paragraph->base_paragraph, 286 !psiconv_compare_paragraph_layout(paragraph->base_paragraph,
267 new_type.paragraph = paragraph->base_paragraph; 296 new_type.paragraph = paragraph->base_paragraph;
268 new_type.character = para_charlayout; 297 new_type.character = para_charlayout;
269 new_type.style = paragraph->base_style; 298 new_type.style = paragraph->base_style;
270 paragraph_type = &new_type; 299 paragraph_type = &new_type;
271 if ((res = psiconv_list_add(paragraph_type_list,paragraph_type))) 300 if ((res = psiconv_list_add(paragraph_type_list,paragraph_type)))
272 goto ERROR5; 301 goto ERROR6;
273 if ((res = psiconv_write_u32(buf_types,paragraph_type->nr))) 302 if ((res = psiconv_write_u32(buf_types,paragraph_type->nr)))
274 goto ERROR5; 303 goto ERROR6;
275 if (!(style = psiconv_get_style(styles,paragraph_type->style))) { 304 if (!(style = psiconv_get_style(styles,paragraph_type->style))) {
276 psiconv_warn(0,psiconv_buffer_length(buf),"Unknown style"); 305 psiconv_warn(0,psiconv_buffer_length(buf),"Unknown style");
277 res = -PSICONV_E_GENERATE; 306 res = -PSICONV_E_GENERATE;
278 goto ERROR5; 307 goto ERROR6;
279 } 308 }
280 if ((res = psiconv_write_paragraph_layout_list(buf_types, 309 if ((res = psiconv_write_paragraph_layout_list(buf_types,
281 paragraph_type->paragraph,style->paragraph))) 310 paragraph_type->paragraph,style->paragraph)))
282 goto ERROR5; 311 goto ERROR6;
283 if (with_styles) 312 if (with_styles)
284 if ((res = psiconv_write_u8(buf_types,paragraph_type->style))) 313 if ((res = psiconv_write_u8(buf_types,paragraph_type->style)))
285 goto ERROR5; 314 goto ERROR6;
286 if ((res = psiconv_write_character_layout_list(buf_types, 315 if ((res = psiconv_write_character_layout_list(buf_types,
287 paragraph_type->character,style->character))) 316 paragraph_type->character,style->character)))
288 goto ERROR5; 317 goto ERROR6;
289 } 318 }
290 if ((res = psiconv_write_u8(buf_elements,para_type))) 319 if ((res = psiconv_write_u8(buf_elements,para_type)))
291 goto ERROR5; 320 goto ERROR6;
292 } 321 }
293 } 322 }
294 323
295 /* HACK: special case: no paragraphs at all. We need to improvize. */ 324 /* HACK: special case: no paragraphs at all. We need to improvize. */
296 if (!psiconv_list_length(value)) { 325 if (!psiconv_list_length(value)) {
297 if ((res = psiconv_write_u32(buf_types,1))) 326 if ((res = psiconv_write_u32(buf_types,1)))
298 goto ERROR5; 327 goto ERROR6;
299 if ((res = psiconv_write_u32(buf_types,0))) 328 if ((res = psiconv_write_u32(buf_types,0)))
300 goto ERROR5; 329 goto ERROR6;
301 if (with_styles) 330 if (with_styles)
302 if ((res = psiconv_write_u8(buf_types,0))) 331 if ((res = psiconv_write_u8(buf_types,0)))
303 goto ERROR5; 332 goto ERROR6;
304 if ((res = psiconv_write_u32(buf_types,0))) 333 if ((res = psiconv_write_u32(buf_types,0)))
305 goto ERROR5; 334 goto ERROR6;
306 335
307 if ((res = psiconv_write_u32(buf_elements,1))) 336 if ((res = psiconv_write_u32(buf_elements,1)))
308 goto ERROR5; 337 goto ERROR6;
309 if ((res = psiconv_write_u8(buf_elements,1))) 338 if ((res = psiconv_write_u8(buf_elements,1)))
310 goto ERROR5; 339 goto ERROR6;
311 pel_length = 1; 340 pel_length = 1;
312 ptl_length = 1; 341 ptl_length = 1;
313 } else { 342 } else {
314 pel_length = psiconv_list_length(value); 343 pel_length = psiconv_list_length(value);
315 ptl_length = psiconv_list_length(paragraph_type_list); 344 ptl_length = psiconv_list_length(paragraph_type_list);
316 } 345 }
317 346
318 /* Now append everything */ 347 /* Now append everything */
319 if ((res = psiconv_write_u16(buf,with_styles?0x0001:0x0000))) 348 if ((res = psiconv_write_u16(buf,with_styles?0x0001:0x0000)))
320 goto ERROR5; 349 goto ERROR6;
321 if ((res = psiconv_write_u8(buf, ptl_length))) 350 if ((res = psiconv_write_u8(buf, ptl_length)))
322 goto ERROR5; 351 goto ERROR6;
323 if ((res = psiconv_buffer_concat(buf,buf_types))) 352 if ((res = psiconv_buffer_concat(buf,buf_types)))
324 goto ERROR5; 353 goto ERROR6;
325 if ((res = psiconv_write_u32(buf,pel_length))) 354 if ((res = psiconv_write_u32(buf,pel_length)))
326 goto ERROR5; 355 goto ERROR6;
327 if ((res = psiconv_buffer_concat(buf,buf_elements))) 356 if ((res = psiconv_buffer_concat(buf,buf_elements)))
328 goto ERROR5; 357 goto ERROR6;
329 if ((res = psiconv_write_u32(buf,nr_of_inlines))) 358 if ((res = psiconv_write_u32(buf,nr_of_inlines)))
330 goto ERROR5; 359 goto ERROR6;
331 res = psiconv_buffer_concat(buf,buf_inlines); 360 if ((res = psiconv_buffer_concat(buf,buf_inlines)))
361 goto ERROR6;
362 if ((res = psiconv_buffer_concat(buf,buf_objects)))
363 goto ERROR6;
332 364
365ERROR6:
366 psiconv_buffer_free(buf_objects);
333ERROR5: 367ERROR5:
334 psiconv_buffer_free(buf_inlines); 368 psiconv_buffer_free(buf_inlines);
335ERROR4: 369ERROR4:
336 psiconv_buffer_free(buf_elements); 370 psiconv_buffer_free(buf_elements);
337ERROR3: 371ERROR3:
392 if (!res) 426 if (!res)
393 return -PSICONV_E_NOMEM; 427 return -PSICONV_E_NOMEM;
394 else 428 else
395 return res; 429 return res;
396} 430}
431
432
433int psiconv_write_embedded_object_section(psiconv_buffer buf,
434 const psiconv_embedded_object_section value)
435{
436 int res;
437 psiconv_u32 display_id,icon_id,table_id;
438 psiconv_buffer extra_buf;
439
440 if (!value) {
441 psiconv_warn(0,psiconv_buffer_length(buf),"Null Object");
442 res = -PSICONV_E_GENERATE;
443 goto ERROR1;
444 }
445
446 if (!(extra_buf = psiconv_buffer_new())) {
447 res = -PSICONV_E_NOMEM;
448 goto ERROR1;
449 }
450
451 display_id = psiconv_buffer_unique_id();
452 icon_id = psiconv_buffer_unique_id();
453 table_id = psiconv_buffer_unique_id();
454 if ((res = psiconv_write_u8(buf,0x06)))
455 goto ERROR2;
456 if ((res = psiconv_write_u32(buf,PSICONV_ID_OBJECT_DISPLAY_SECTION)))
457 goto ERROR2;
458 if ((res = psiconv_buffer_add_reference(buf,display_id)))
459 goto ERROR2;
460 if ((res = psiconv_write_u32(buf,PSICONV_ID_OBJECT_ICON_SECTION)))
461 goto ERROR2;
462 if ((res = psiconv_buffer_add_reference(buf,icon_id)))
463 goto ERROR2;
464 if ((res = psiconv_write_u32(buf,PSICONV_ID_OBJECT_SECTION_TABLE_SECTION)))
465 goto ERROR2;
466 if ((res = psiconv_buffer_add_reference(buf,table_id)))
467 goto ERROR2;
468
469 if ((res = psiconv_buffer_add_target(buf,display_id)))
470 goto ERROR2;
471 if ((res = psiconv_write_object_display_section(buf,value->display)))
472 goto ERROR2;
473 if ((res = psiconv_buffer_add_target(buf,icon_id)))
474 goto ERROR2;
475 if ((res = psiconv_write_object_icon_section(buf,value->icon)))
476 goto ERROR2;
477 if ((res = psiconv_buffer_add_target(buf,table_id)))
478 goto ERROR2;
479 switch(value->object->type) {
480 case psiconv_word_file:
481 if ((res = psiconv_write_word_file(extra_buf,
482 (psiconv_word_f) value->object->file)))
483 goto ERROR2;
484 break;
485/*
486 case psiconv_sketch_file:
487 if ((res = psiconv_write_sketch_file(extra_buf,
488 (psiconv_sketch_f) value->object->file)))
489 goto ERROR2;
490 break;
491 case psiconv_sheet_file:
492 if ((res = psiconv_write_sheet_file(extra_buf,
493 (psiconv_sheet_f) value->object->file)))
494 goto ERROR2;
495 break;
496*/
497 default:
498 psiconv_warn(0,psiconv_buffer_length(buf),
499 "Unknown or unsupported object type");
500 res = -PSICONV_E_GENERATE;
501 goto ERROR2;
502 }
503
504 if ((res = psiconv_buffer_resolve(extra_buf)))
505 goto ERROR2;
506 if ((res = psiconv_buffer_concat(buf,extra_buf)))
507 goto ERROR2;
508
509 return 0;
510
511ERROR2:
512 psiconv_buffer_free(extra_buf);
513ERROR1:
514 return res;
515}
516
517
518int psiconv_write_object_display_section(psiconv_buffer buf,
519 const psiconv_object_display_section value)
520{
521 int res;
522
523 if (!value) {
524 psiconv_warn(0,psiconv_buffer_length(buf),"Null Object Display Section");
525 res = -PSICONV_E_GENERATE;
526 goto ERROR1;
527 }
528
529 if ((res = psiconv_write_u8(buf,value->show_icon?0x00:0x01)))
530 goto ERROR1;
531 if ((res = psiconv_write_length(buf,value->width)))
532 goto ERROR1;
533 if ((res = psiconv_write_length(buf,value->height)))
534 goto ERROR1;
535 if ((res = psiconv_write_u32(buf,0x00000000)))
536 goto ERROR1;
537
538 return 0;
539
540ERROR1:
541 return res;
542}
543
544int psiconv_write_object_icon_section(psiconv_buffer buf,
545 const psiconv_object_icon_section value)
546{
547 int res;
548
549 if (!value) {
550 psiconv_warn(0,psiconv_buffer_length(buf),"Null Object Icon Section");
551 res = -PSICONV_E_GENERATE;
552 goto ERROR1;
553 }
554
555 if ((res = psiconv_write_string(buf,value->icon_name)))
556 goto ERROR1;
557 if ((res = psiconv_write_length(buf,value->icon_width)))
558 goto ERROR1;
559 if ((res = psiconv_write_length(buf,value->icon_height)))
560 goto ERROR1;
561
562 return 0;
563
564ERROR1:
565 return res;
566}

Legend:
Removed from v.166  
changed lines
  Added in v.167

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