… | |
… | |
90 | if (!value) { |
90 | if (!value) { |
91 | psiconv_warn(0,psiconv_buffer_length(buf),"Null text section"); |
91 | psiconv_warn(0,psiconv_buffer_length(buf),"Null text section"); |
92 | return -PSICONV_E_GENERATE; |
92 | return -PSICONV_E_GENERATE; |
93 | } |
93 | } |
94 | |
94 | |
|
|
95 | if (psiconv_list_length(value)) { |
95 | if (!(extra_buf = psiconv_buffer_new())) |
96 | if (!(extra_buf = psiconv_buffer_new())) |
96 | return -PSICONV_E_NOMEM; |
97 | return -PSICONV_E_NOMEM; |
97 | for (i = 0; i < psiconv_list_length(value); i++) { |
98 | for (i = 0; i < psiconv_list_length(value); i++) { |
98 | if (!(paragraph = psiconv_list_get(value,i))) { |
99 | if (!(paragraph = psiconv_list_get(value,i))) { |
99 | psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption"); |
100 | psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption"); |
100 | res = -PSICONV_E_OTHER; |
101 | res = -PSICONV_E_OTHER; |
|
|
102 | goto ERROR; |
|
|
103 | } |
|
|
104 | for (j = 0; j < strlen(paragraph->text); j++) |
|
|
105 | if ((res = psiconv_write_u8(extra_buf,paragraph->text[j]))) |
|
|
106 | goto ERROR; |
|
|
107 | psiconv_write_u8(extra_buf,0x06); |
|
|
108 | } |
|
|
109 | if ((res = psiconv_write_X(buf,psiconv_buffer_length(extra_buf)))) |
101 | goto ERROR; |
110 | goto ERROR; |
102 | } |
|
|
103 | for (j = 0; j < strlen(paragraph->text); j++) |
|
|
104 | if ((res = psiconv_write_u8(extra_buf,paragraph->text[j]))) |
|
|
105 | goto ERROR; |
|
|
106 | psiconv_write_u8(extra_buf,0x06); |
|
|
107 | } |
|
|
108 | if ((res = psiconv_write_X(buf,psiconv_buffer_length(extra_buf)))) |
|
|
109 | goto ERROR; |
|
|
110 | res = psiconv_buffer_concat(buf,extra_buf); |
111 | res = psiconv_buffer_concat(buf,extra_buf); |
|
|
112 | } else |
|
|
113 | /* Hack: empty text sections are just not allowed */ |
|
|
114 | return psiconv_write_u16(buf,0x0602); |
111 | |
115 | |
112 | ERROR: |
116 | ERROR: |
113 | psiconv_buffer_free(extra_buf); |
117 | psiconv_buffer_free(extra_buf); |
114 | return res; |
118 | return res; |
115 | } |
119 | } |
… | |
… | |
131 | struct psiconv_paragraph_type_list_s new_type; |
135 | struct psiconv_paragraph_type_list_s new_type; |
132 | psiconv_buffer buf_types,buf_elements,buf_inlines; |
136 | psiconv_buffer buf_types,buf_elements,buf_inlines; |
133 | psiconv_paragraph paragraph; |
137 | psiconv_paragraph paragraph; |
134 | psiconv_in_line_layout in_line; |
138 | psiconv_in_line_layout in_line; |
135 | psiconv_word_style style; |
139 | psiconv_word_style style; |
136 | int i,j,para_type,nr_of_inlines=0,res; |
140 | psiconv_character_layout para_charlayout; |
|
|
141 | int i,j,para_type,nr_of_inlines=0,res,ptl_length,pel_length,thislen,paralen; |
137 | |
142 | |
138 | if (!value) { |
143 | if (!value) { |
139 | psiconv_warn(0,psiconv_buffer_length(buf),"Null text section"); |
144 | psiconv_warn(0,psiconv_buffer_length(buf),"Null text section"); |
140 | return -PSICONV_E_GENERATE; |
145 | return -PSICONV_E_GENERATE; |
141 | } |
146 | } |
… | |
… | |
167 | goto ERROR5; |
172 | goto ERROR5; |
168 | } |
173 | } |
169 | if ((res = psiconv_write_u32(buf_elements,strlen(paragraph->text)+1))) |
174 | if ((res = psiconv_write_u32(buf_elements,strlen(paragraph->text)+1))) |
170 | goto ERROR5; |
175 | goto ERROR5; |
171 | |
176 | |
172 | if (psiconv_list_length(paragraph->in_lines)) { |
177 | if (psiconv_list_length(paragraph->in_lines) > 1) { |
173 | /* Inline layouts, so we generate a paragraph element and inline |
178 | /* Inline layouts, so we generate a paragraph element and inline |
174 | elements */ |
179 | elements */ |
175 | if ((res = psiconv_write_u8(buf_elements,0x00))) |
180 | if ((res = psiconv_write_u8(buf_elements,0x00))) |
176 | goto ERROR5; |
181 | goto ERROR5; |
177 | if (!(style = psiconv_get_style(styles,paragraph->base_style))) { |
182 | if (!(style = psiconv_get_style(styles,paragraph->base_style))) { |
… | |
… | |
189 | if ((res = psiconv_write_u32(buf_elements, |
194 | if ((res = psiconv_write_u32(buf_elements, |
190 | psiconv_list_length(paragraph->in_lines)))) |
195 | psiconv_list_length(paragraph->in_lines)))) |
191 | goto ERROR5; |
196 | goto ERROR5; |
192 | |
197 | |
193 | /* Generate the inlines. NB: Against what are all settings relative?!? */ |
198 | /* Generate the inlines. NB: Against what are all settings relative?!? */ |
|
|
199 | paralen = 0; |
194 | for (j = 0; j < psiconv_list_length(paragraph->in_lines); j++) { |
200 | for (j = 0; j < psiconv_list_length(paragraph->in_lines); j++) { |
195 | nr_of_inlines ++; |
201 | nr_of_inlines ++; |
196 | if (!(in_line = psiconv_list_get(paragraph->in_lines,j))) { |
202 | if (!(in_line = psiconv_list_get(paragraph->in_lines,j))) { |
197 | psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption"); |
203 | psiconv_warn(0,psiconv_buffer_length(buf), |
|
|
204 | "Massive memory corruption"); |
198 | res = -PSICONV_E_OTHER; |
205 | res = -PSICONV_E_OTHER; |
199 | goto ERROR5; |
206 | goto ERROR5; |
200 | } |
207 | } |
201 | if ((res = psiconv_write_u8(buf_inlines,0x00))) |
208 | if ((res = psiconv_write_u8(buf_inlines,0x00))) |
202 | goto ERROR5; |
209 | goto ERROR5; |
|
|
210 | thislen = in_line->length; |
|
|
211 | paralen += thislen; |
|
|
212 | /* If this is the last in_line, we need to make sure that the |
|
|
213 | complete length of all inlines equals the text length */ |
|
|
214 | if (j == psiconv_list_length(paragraph->in_lines)-1) { |
|
|
215 | if (paralen > strlen(paragraph->text)+1) { |
|
|
216 | res = -PSICONV_E_GENERATE; |
|
|
217 | goto ERROR5; |
|
|
218 | } |
|
|
219 | thislen += strlen(paragraph->text)+1-paralen; |
|
|
220 | } |
203 | if ((res = psiconv_write_u32(buf_inlines,in_line->length))) |
221 | if ((res = psiconv_write_u32(buf_inlines,thislen))) |
204 | goto ERROR5; |
222 | goto ERROR5; |
205 | if ((res = psiconv_write_character_layout_list(buf_inlines, |
223 | if ((res = psiconv_write_character_layout_list(buf_inlines, |
206 | in_line->layout, |
224 | in_line->layout, |
207 | style->character))) |
225 | style->character))) |
208 | goto ERROR5; |
226 | goto ERROR5; |
209 | } |
227 | } |
210 | } else { |
228 | } else { |
211 | /* No inline layouts, so we generate a paragraph type list */ |
229 | /* No inline layouts (or only 1), so we generate a paragraph type list */ |
212 | para_type = 0; |
230 | para_type = 0; |
|
|
231 | /* Set para_charlayout to the correct character-level layout */ |
|
|
232 | if (psiconv_list_length(paragraph->in_lines) == 0) |
|
|
233 | para_charlayout = paragraph->base_character; |
|
|
234 | else { |
|
|
235 | if (!(in_line = psiconv_list_get(paragraph->in_lines,0))) { |
|
|
236 | psiconv_warn(0,psiconv_buffer_length(buf), |
|
|
237 | "Massive memory corruption"); |
|
|
238 | res = -PSICONV_E_OTHER; |
|
|
239 | goto ERROR5; |
|
|
240 | } |
|
|
241 | para_charlayout = in_line->layout; |
|
|
242 | } |
213 | for (j = 0; j < psiconv_list_length(paragraph_type_list); j++) { |
243 | for (j = 0; j < psiconv_list_length(paragraph_type_list); j++) { |
214 | if (!(paragraph_type = psiconv_list_get(paragraph_type_list,j))) { |
244 | if (!(paragraph_type = psiconv_list_get(paragraph_type_list,j))) { |
215 | psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption"); |
245 | psiconv_warn(0,psiconv_buffer_length(buf), |
|
|
246 | "Massive memory corruption"); |
216 | res = -PSICONV_E_OTHER; |
247 | res = -PSICONV_E_OTHER; |
217 | goto ERROR5; |
248 | goto ERROR5; |
218 | } |
249 | } |
219 | if ((paragraph->base_style == paragraph_type->style) && |
250 | if ((paragraph->base_style == paragraph_type->style) && |
220 | !psiconv_compare_character_layout(paragraph->base_character, |
251 | !psiconv_compare_character_layout(para_charlayout, |
221 | paragraph_type->character) && |
252 | paragraph_type->character) && |
222 | !psiconv_compare_paragraph_layout(paragraph->base_paragraph, |
253 | !psiconv_compare_paragraph_layout(paragraph->base_paragraph, |
223 | paragraph_type->paragraph)) { |
254 | paragraph_type->paragraph)) { |
224 | para_type = paragraph_type->nr; |
255 | para_type = paragraph_type->nr; |
225 | break; |
256 | break; |
… | |
… | |
228 | if (!para_type) { |
259 | if (!para_type) { |
229 | /* We need to add a new entry */ |
260 | /* We need to add a new entry */ |
230 | para_type = new_type.nr = j+1; |
261 | para_type = new_type.nr = j+1; |
231 | /* No need to copy them, we won't change them anyway */ |
262 | /* No need to copy them, we won't change them anyway */ |
232 | new_type.paragraph = paragraph->base_paragraph; |
263 | new_type.paragraph = paragraph->base_paragraph; |
233 | new_type.character = paragraph->base_character; |
264 | new_type.character = para_charlayout; |
234 | new_type.style = paragraph->base_style; |
265 | new_type.style = paragraph->base_style; |
235 | paragraph_type = &new_type; |
266 | paragraph_type = &new_type; |
236 | if ((res = psiconv_list_add(paragraph_type_list,paragraph_type))) |
267 | if ((res = psiconv_list_add(paragraph_type_list,paragraph_type))) |
237 | goto ERROR5; |
268 | goto ERROR5; |
238 | if ((res = psiconv_write_u32(buf_types,paragraph_type->nr))) |
269 | if ((res = psiconv_write_u32(buf_types,paragraph_type->nr))) |
… | |
… | |
255 | if ((res = psiconv_write_u8(buf_elements,para_type))) |
286 | if ((res = psiconv_write_u8(buf_elements,para_type))) |
256 | goto ERROR5; |
287 | goto ERROR5; |
257 | } |
288 | } |
258 | } |
289 | } |
259 | |
290 | |
|
|
291 | /* HACK: special case: no paragraphs at all. We need to improvize. */ |
|
|
292 | if (!psiconv_list_length(value)) { |
|
|
293 | if ((res = psiconv_write_u32(buf_types,1))) |
|
|
294 | goto ERROR5; |
|
|
295 | if ((res = psiconv_write_u32(buf_types,0))) |
|
|
296 | goto ERROR5; |
|
|
297 | if (with_styles) |
|
|
298 | if ((res = psiconv_write_u8(buf_types,0))) |
|
|
299 | goto ERROR5; |
|
|
300 | if ((res = psiconv_write_u32(buf_types,0))) |
|
|
301 | goto ERROR5; |
|
|
302 | |
|
|
303 | if ((res = psiconv_write_u32(buf_elements,1))) |
|
|
304 | goto ERROR5; |
|
|
305 | if ((res = psiconv_write_u8(buf_elements,1))) |
|
|
306 | goto ERROR5; |
|
|
307 | pel_length = 1; |
|
|
308 | ptl_length = 1; |
|
|
309 | } else { |
|
|
310 | pel_length = psiconv_list_length(value); |
|
|
311 | ptl_length = psiconv_list_length(paragraph_type_list); |
|
|
312 | } |
|
|
313 | |
260 | /* Now append everything */ |
314 | /* Now append everything */ |
261 | if ((res = psiconv_write_u16(buf,with_styles?0x0001:0x0000))) |
315 | if ((res = psiconv_write_u16(buf,with_styles?0x0001:0x0000))) |
262 | goto ERROR5; |
316 | goto ERROR5; |
263 | if ((res = psiconv_write_u8(buf,psiconv_list_length(paragraph_type_list)))) |
317 | if ((res = psiconv_write_u8(buf, ptl_length))) |
264 | goto ERROR5; |
318 | goto ERROR5; |
265 | if ((res = psiconv_buffer_concat(buf,buf_types))) |
319 | if ((res = psiconv_buffer_concat(buf,buf_types))) |
266 | goto ERROR5; |
320 | goto ERROR5; |
267 | if ((res = psiconv_write_u32(buf,psiconv_list_length(value)))) |
321 | if ((res = psiconv_write_u32(buf,pel_length))) |
268 | goto ERROR5; |
322 | goto ERROR5; |
269 | if ((res = psiconv_buffer_concat(buf,buf_elements))) |
323 | if ((res = psiconv_buffer_concat(buf,buf_elements))) |
270 | goto ERROR5; |
324 | goto ERROR5; |
271 | if ((res = psiconv_write_u32(buf,nr_of_inlines))) |
325 | if ((res = psiconv_write_u32(buf,nr_of_inlines))) |
272 | goto ERROR5; |
326 | goto ERROR5; |