Rev 439 | Rev 456 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
415 | Franz | 1 | /*************************************************************************** |
2 | * Copyright (C) 2004 by Riku Leino * |
||
3 | * tsoots@welho.com * |
||
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 * |
||
17 | * Free Software Foundation, Inc., * |
||
18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
||
19 | ***************************************************************************/ |
||
20 | |||
21 | #include "stylereader.h" |
||
22 | |||
23 | #ifdef HAVE_XML |
||
24 | |||
25 | #include <gtmeasure.h> |
||
26 | #include <gtparagraphstyle.h> |
||
27 | #include <gtframestyle.h> |
||
28 | #include <gtfont.h> |
||
29 | |||
30 | StyleReader* StyleReader::sreader = NULL; |
||
31 | |||
32 | extern xmlSAXHandlerPtr sSAXHandler; |
||
33 | |||
34 | StyleReader::StyleReader(QString documentName, gtWriter *w, bool textOnly, bool prefix) |
||
411 | Franz | 35 | { |
415 | Franz | 36 | sreader = this; |
37 | docname = documentName; |
||
38 | readProperties = false; |
||
39 | writer = w; |
||
40 | importTextOnly = textOnly; |
||
41 | usePrefix = prefix; |
||
42 | currentStyle = NULL; |
||
43 | parentStyle = NULL; |
||
44 | inList = false; |
||
45 | currentList = ""; |
||
420 | Franz | 46 | defaultStyleCreated = false; |
411 | Franz | 47 | } |
415 | Franz | 48 | |
49 | bool StyleReader::startElement(const QString&, const QString&, const QString &name, const QXmlAttributes &attrs) |
||
50 | { |
||
51 | if (name == "style:default-style") |
||
52 | defaultStyle(attrs); |
||
53 | else if (name == "style:properties") |
||
54 | styleProperties(attrs); |
||
55 | else if (name == "style:style") |
||
56 | { |
||
420 | Franz | 57 | if (!defaultStyleCreated) |
58 | { |
||
59 | currentStyle = new gtParagraphStyle(*(writer->getDefaultStyle())); |
||
60 | currentStyle->setName("default-style"); |
||
61 | defaultStyleCreated = true; |
||
62 | } |
||
415 | Franz | 63 | styleStyle(attrs); |
64 | } |
||
65 | else if (name == "style:tab-stop") |
||
66 | tabStop(attrs); |
||
67 | else if (name == "text:list-style") |
||
68 | { |
||
69 | for (int i = 0; i < attrs.count(); ++i) |
||
70 | if (attrs.localName(i) == "style:name") |
||
71 | currentList = attrs.value(i); |
||
72 | inList = true; |
||
73 | } |
||
74 | else if (((name == "text:list-level-style-bullet") || |
||
439 | tsoots | 75 | (name == "text:list-level-style-number") || |
76 | (name == "text:list-level-style-image")) && (inList)) |
||
415 | Franz | 77 | { |
78 | QString level = ""; |
||
79 | for (int i = 0; i < attrs.count(); ++i) |
||
80 | { |
||
81 | if (attrs.localName(i) == "text:level") |
||
82 | { |
||
83 | gtStyle *plist; |
||
84 | if (attrs.value(i) == "1") |
||
85 | { |
||
86 | plist = listParents[currentList]; |
||
87 | } |
||
88 | else |
||
89 | { |
||
90 | int level = (attrs.value(i)).toInt(); |
||
91 | --level; |
||
92 | plist = styles[QString(currentList + "_%1").arg(level)]; |
||
93 | } |
||
94 | gtParagraphStyle *pstyle; |
||
95 | if (plist == NULL) |
||
96 | plist = new gtStyle(*(styles["default-style"])); |
||
97 | |||
98 | if (plist->target() == "paragraph") |
||
99 | { |
||
100 | pstyle = dynamic_cast<gtParagraphStyle*>(plist); |
||
101 | gtParagraphStyle* tmp = new gtParagraphStyle(*pstyle); |
||
102 | currentStyle = tmp; |
||
103 | } |
||
104 | else |
||
105 | { |
||
106 | gtParagraphStyle* tmp = new gtParagraphStyle(*plist); |
||
107 | currentStyle = tmp; |
||
108 | } |
||
109 | currentStyle->setName(currentList + "_" + attrs.value(i)); |
||
110 | } |
||
111 | } |
||
112 | readProperties = true; |
||
113 | } |
||
114 | else if ((name == "style:drop-cap") && (readProperties)) |
||
115 | { |
||
116 | if (currentStyle->target() == "paragraph") |
||
117 | { |
||
118 | for (int i = 0; i < attrs.count(); ++i) |
||
119 | { |
||
120 | if (attrs.localName(i) == "style:lines") |
||
121 | { |
||
122 | bool ok = false; |
||
123 | QString sd = attrs.value(i); |
||
124 | int dh = sd.toInt(&ok); |
||
125 | if (ok) |
||
126 | { |
||
127 | gtParagraphStyle* s = dynamic_cast<gtParagraphStyle*>(currentStyle); |
||
128 | s->setDropCapHeight(dh); |
||
129 | s->setDropCap(true); |
||
130 | } |
||
131 | } |
||
132 | } |
||
133 | } |
||
134 | } |
||
135 | else if (name == "style:font-decl") |
||
136 | { |
||
137 | QString key = ""; |
||
138 | QString family = ""; |
||
139 | QString style = ""; |
||
140 | for (int i = 0; i < attrs.count(); ++i) |
||
141 | { |
||
142 | if (attrs.localName(i) == "style:name") |
||
143 | key = attrs.value(i); |
||
144 | else if (attrs.localName(i) == "fo:font-family") |
||
145 | { |
||
146 | family = attrs.value(i); |
||
147 | family = family.remove("'"); |
||
148 | } |
||
149 | else if (attrs.localName(i) == "style:font-style-name") |
||
150 | style += attrs.value(i) + " "; |
||
151 | } |
||
152 | QString name = family + " " + style; |
||
153 | name = name.simplifyWhiteSpace(); |
||
154 | fonts[key] = name; |
||
155 | } |
||
156 | return true; |
||
157 | } |
||
158 | |||
159 | void StyleReader::defaultStyle(const QXmlAttributes& attrs) |
||
160 | { |
||
161 | currentStyle = NULL; |
||
162 | for (int i = 0; i < attrs.count(); ++i) |
||
163 | if (attrs.localName(i) == "style:family") |
||
164 | if (attrs.value(i) == "paragraph") |
||
165 | { |
||
420 | Franz | 166 | currentStyle = new gtParagraphStyle(*(writer->getDefaultStyle())); |
167 | currentStyle->setName("default-style"); |
||
415 | Franz | 168 | readProperties = true; |
420 | Franz | 169 | defaultStyleCreated = true; |
415 | Franz | 170 | } |
171 | } |
||
172 | |||
173 | void StyleReader::styleProperties(const QXmlAttributes& attrs) |
||
174 | { |
||
175 | if ((currentStyle == NULL) || (!readProperties)) |
||
176 | return; |
||
177 | gtParagraphStyle* pstyle = NULL; |
||
178 | if (currentStyle->target() == "paragraph") |
||
179 | pstyle = dynamic_cast<gtParagraphStyle*>(currentStyle); |
||
180 | else |
||
181 | pstyle = NULL; |
||
182 | QString align = NULL; |
||
183 | QString force = NULL; |
||
184 | for (int i = 0; i < attrs.count(); ++i) |
||
185 | { |
||
186 | if ((attrs.localName(i) == "style:font-name") && (!inList)) |
||
187 | currentStyle->getFont()->setName(getFont(attrs.value(i))); |
||
188 | else if (attrs.localName(i) == "fo:font-size") |
||
189 | { |
||
190 | double size = 0; |
||
191 | double psize = 0; |
||
192 | if (parentStyle != NULL) |
||
193 | psize = static_cast<double>(parentStyle->getFont()->getSize()); |
||
194 | else if (styles.contains("default-style")) |
||
195 | psize = static_cast<double>(styles["default-style"]->getFont()->getSize()); |
||
196 | |||
197 | psize = psize / 10; |
||
198 | size = getSize(attrs.value(i), psize); |
||
199 | int nsize = static_cast<int>(size * 10); |
||
200 | currentStyle->getFont()->setSize(nsize); |
||
201 | if ((currentStyle->getName() == "default-style") && (pstyle != NULL)) |
||
202 | pstyle->setLineSpacing(writer->getPreferredLineSpacing(nsize)); |
||
203 | } |
||
204 | else if ((attrs.localName(i) == "fo:line-height") && (parentStyle != NULL)) |
||
205 | { |
||
206 | gtParagraphStyle* ppstyle; |
||
207 | if (parentStyle->target() == "paragraph") |
||
208 | { |
||
209 | ppstyle = dynamic_cast<gtParagraphStyle*>(parentStyle); |
||
210 | pstyle->setLineSpacing(getSize(attrs.value(i), writer->getPreferredLineSpacing(currentStyle->getFont()->getSize()))); |
||
211 | } |
||
212 | } |
||
213 | else if ((attrs.localName(i) == "fo:font-weight") && (attrs.value(i) == "bold")) |
||
214 | currentStyle->getFont()->setWeight(BOLD); |
||
215 | else if ((attrs.localName(i) == "fo:font-style") && (attrs.value(i) == "italic")) |
||
216 | currentStyle->getFont()->setSlant(ITALIC); |
||
217 | else if ((attrs.localName(i) == "style:text-underline") && (attrs.value(i) != "none")) |
||
218 | currentStyle->getFont()->toggleEffect(UNDERLINE); |
||
219 | else if ((attrs.localName(i) == "style:text-crossing-out") && (attrs.value(i) != "none")) |
||
220 | currentStyle->getFont()->toggleEffect(STRIKETHROUGH); |
||
221 | else if ((attrs.localName(i) == "fo:font-variant") && (attrs.value(i) == "small-caps")) |
||
222 | currentStyle->getFont()->toggleEffect(SMALL_CAPS); |
||
223 | else if ((attrs.localName(i) == "style:text-outline") && (attrs.value(i) == "true")) |
||
224 | { |
||
225 | currentStyle->getFont()->toggleEffect(OUTLINE); |
||
226 | currentStyle->getFont()->setStrokeColor("Black"); |
||
227 | currentStyle->getFont()->setColor("White"); |
||
228 | } |
||
229 | else if (attrs.localName(i) == "fo:letter-spacing") |
||
230 | currentStyle->getFont()->setKerning(getSize(attrs.value(i))); |
||
231 | else if (attrs.localName(i) == "style:text-scale") |
||
232 | currentStyle->getFont()->setHscale(static_cast<int>(getSize(attrs.value(i), -1.0))); |
||
233 | else if ((attrs.localName(i) == "style:text-position") && |
||
234 | (((attrs.value(i)).find("sub") != -1) || |
||
235 | (((attrs.value(i)).left(1) == "-") && ((attrs.value(i)).left(1) != "0")))) |
||
236 | currentStyle->getFont()->toggleEffect(SUBSCRIPT); |
||
237 | else if ((attrs.localName(i) == "style:text-position") && |
||
238 | (((attrs.value(i)).find("super") != -1) || |
||
239 | (((attrs.value(i)).left(1) != "-") && ((attrs.value(i)).left(1) != "0")))) |
||
240 | currentStyle->getFont()->toggleEffect(SUPERSCRIPT); |
||
241 | else if ((attrs.localName(i) == "fo:margin-top") && (pstyle != NULL)) |
||
242 | pstyle->setSpaceAbove(getSize(attrs.value(i))); |
||
243 | else if ((attrs.localName(i) == "fo:margin-bottom") && (pstyle != NULL)) |
||
244 | pstyle->setSpaceBelow(getSize(attrs.value(i))); |
||
245 | else if ((attrs.localName(i) == "fo:margin-left") && (pstyle != NULL)) |
||
246 | { |
||
247 | if (inList) |
||
248 | pstyle->setIndent(pstyle->getIndent() + getSize(attrs.value(i))); |
||
249 | else |
||
250 | pstyle->setIndent(getSize(attrs.value(i))); |
||
251 | } |
||
252 | else if ((attrs.localName(i) == "text:space-before") && (pstyle != NULL)) |
||
253 | { |
||
254 | if (inList) |
||
255 | pstyle->setIndent(pstyle->getIndent() + getSize(attrs.value(i))); |
||
256 | else |
||
257 | pstyle->setIndent(getSize(attrs.value(i))); |
||
258 | } |
||
259 | else if ((attrs.localName(i) == "fo:text-indent") && (pstyle != NULL)) |
||
260 | pstyle->setFirstLineIndent(getSize(attrs.value(i))); |
||
261 | else if ((attrs.localName(i) == "fo:text-align") && (pstyle != NULL)) |
||
262 | align = attrs.value(i); |
||
263 | else if ((attrs.localName(i) == "style:justify-single-word") && (pstyle != NULL)) |
||
264 | force = attrs.value(i); |
||
265 | } |
||
266 | if (align != NULL) |
||
267 | { |
||
268 | if (align == "end") |
||
269 | pstyle->setAlignment(RIGHT); |
||
270 | else if (align == "center") |
||
271 | pstyle->setAlignment(CENTER); |
||
272 | else if (align == "justify") |
||
273 | { |
||
274 | if (force == "false") |
||
275 | pstyle->setAlignment(BLOCK); |
||
276 | else |
||
277 | pstyle->setAlignment(FORCED); |
||
278 | } |
||
279 | } |
||
280 | } |
||
281 | |||
282 | void StyleReader::styleStyle(const QXmlAttributes& attrs) |
||
283 | { |
||
284 | QString name = ""; |
||
285 | QString listName = NULL; |
||
286 | bool isParaStyle = false; |
||
287 | bool create = true; |
||
288 | for (int i = 0; i < attrs.count(); ++i) |
||
289 | { |
||
290 | if (attrs.localName(i) == "style:family") |
||
291 | { |
||
292 | if (attrs.value(i) == "paragraph") |
||
293 | { |
||
294 | isParaStyle = true; |
||
295 | readProperties = true; |
||
296 | } |
||
297 | else if (attrs.value(i) == "text") |
||
298 | { |
||
299 | isParaStyle = false; |
||
300 | readProperties = true; |
||
301 | } |
||
302 | else |
||
303 | { |
||
304 | readProperties = false; |
||
305 | return; |
||
306 | } |
||
307 | } |
||
308 | else if (attrs.localName(i) == "style:name") |
||
309 | name = attrs.value(i); |
||
310 | else if (attrs.localName(i) == "style:parent-style-name") |
||
311 | { |
||
312 | if (styles.contains(attrs.value(i))) |
||
313 | parentStyle = styles[attrs.value(i)]; |
||
314 | else |
||
315 | parentStyle = NULL; |
||
316 | } |
||
317 | else if (attrs.localName(i) == "style:list-style-name") |
||
318 | listName = attrs.value(i); |
||
319 | } |
||
320 | if ((parentStyle == NULL) && (styles.contains("default-style"))) |
||
321 | parentStyle = styles["default-style"]; |
||
322 | |||
323 | if (create) |
||
324 | { |
||
325 | if (parentStyle == NULL) |
||
326 | { |
||
327 | parentStyle = new gtStyle("tmp-parent"); |
||
328 | } |
||
329 | if (isParaStyle) |
||
330 | { |
||
331 | gtParagraphStyle *tmpP; |
||
332 | if (parentStyle->target() == "paragraph") |
||
333 | { |
||
334 | tmpP = dynamic_cast<gtParagraphStyle*>(parentStyle); |
||
335 | gtParagraphStyle* tmp = new gtParagraphStyle(*tmpP); |
||
336 | // tmp->setAutoLineSpacing(true); |
||
337 | currentStyle = tmp; |
||
338 | } |
||
339 | else |
||
340 | { |
||
341 | gtParagraphStyle* tmp = new gtParagraphStyle(*parentStyle); |
||
342 | // tmp->setAutoLineSpacing(true); |
||
343 | currentStyle = tmp; |
||
344 | } |
||
345 | if (listName != NULL) |
||
346 | { |
||
347 | listParents[listName] = currentStyle; |
||
348 | } |
||
349 | } |
||
350 | else |
||
351 | currentStyle = new gtStyle(*parentStyle); |
||
352 | |||
353 | currentStyle->setName(name); |
||
354 | } |
||
355 | else |
||
356 | currentStyle = NULL; |
||
357 | } |
||
358 | |||
359 | void StyleReader::tabStop(const QXmlAttributes& attrs) |
||
360 | { |
||
361 | if (currentStyle->target() == "paragraph") |
||
362 | { |
||
363 | gtParagraphStyle* pstyle = dynamic_cast<gtParagraphStyle*>(currentStyle); |
||
364 | QString pos = NULL; |
||
365 | QString type = NULL; |
||
366 | for (int i = 0; i < attrs.count(); ++i) |
||
367 | { |
||
368 | if (attrs.localName(i) == "style:position") |
||
369 | pos = attrs.value(i); |
||
370 | else if (attrs.localName(i) == "style:type") |
||
371 | type = attrs.value(i); |
||
372 | |||
373 | } |
||
374 | if (pos != NULL) |
||
375 | { |
||
376 | if (type == NULL) |
||
377 | type = "left"; |
||
378 | double posd = getSize(pos); |
||
379 | if (type == "left") |
||
380 | pstyle->setTabValue(posd, LEFT_T); |
||
381 | else if (type == "right") |
||
382 | pstyle->setTabValue(posd, RIGHT_T); |
||
383 | else if (type == "center") |
||
384 | pstyle->setTabValue(posd, CENTER_T); |
||
385 | else |
||
386 | pstyle->setTabValue(posd, CENTER_T); |
||
387 | } |
||
388 | } |
||
389 | } |
||
390 | |||
391 | bool StyleReader::endElement(const QString&, const QString&, const QString &name) |
||
392 | { |
||
393 | if ((name == "style:default-style") && (currentStyle != NULL) && (readProperties)) |
||
394 | { |
||
395 | setStyle(currentStyle->getName(), currentStyle); |
||
396 | currentStyle = NULL; |
||
397 | parentStyle = NULL; |
||
398 | readProperties = false; |
||
399 | } |
||
400 | else if (((name == "style:style") || |
||
401 | (name == "text:list-level-style-bullet") || |
||
439 | tsoots | 402 | (name == "text:list-level-style-number") || |
403 | (name == "text:list-level-style-image")) && (currentStyle != NULL)) |
||
415 | Franz | 404 | { |
405 | setStyle(currentStyle->getName(), currentStyle); |
||
406 | currentStyle = NULL; |
||
407 | parentStyle = NULL; |
||
408 | readProperties = false; |
||
409 | } |
||
410 | else if (name == "text:list-style") |
||
411 | { |
||
412 | inList = false; |
||
413 | } |
||
414 | return true; |
||
415 | } |
||
416 | |||
417 | void StyleReader::parse(QString fileName) |
||
418 | { |
||
419 | xmlSAXParseFile(sSAXHandler, fileName.ascii(), XML_PARSE_RECOVER); |
||
420 | } |
||
421 | |||
422 | gtStyle* StyleReader::getStyle(const QString& name) |
||
423 | { |
||
424 | if (styles.contains(name)) |
||
425 | { |
||
426 | gtStyle* tmp = styles[name]; |
||
427 | QString tname = tmp->getName(); |
||
428 | if ((tname.find(docname) == -1) && (usePrefix)) |
||
429 | tmp->setName(docname + "_" + tname); |
||
430 | |||
431 | return tmp; |
||
432 | } |
||
433 | else |
||
439 | tsoots | 434 | return styles["default-style"]; |
435 | |||
415 | Franz | 436 | } |
437 | |||
438 | void StyleReader::setStyle(const QString& name, gtStyle* style) |
||
439 | { |
||
440 | gtParagraphStyle *s; |
||
441 | QString tname = style->getName(); |
||
442 | if (style->target() == "paragraph") |
||
443 | { |
||
444 | s = dynamic_cast<gtParagraphStyle*>(style); |
||
445 | QString nameByAttrs = QString("%1-").arg(s->getSpaceAbove()); |
||
446 | nameByAttrs += QString("%1-").arg(s->getSpaceBelow()); |
||
447 | nameByAttrs += QString("%1-").arg(s->getLineSpacing()); |
||
448 | nameByAttrs += QString("%1-").arg(s->getIndent()); |
||
449 | nameByAttrs += QString("%1-").arg(s->getFirstLineIndent()); |
||
450 | nameByAttrs += QString("%1-").arg(s->getAlignment()); |
||
451 | nameByAttrs += QString("%1-").arg(s->hasDropCap()); |
||
452 | QValueList<double>* tmp = s->getTabValues(); |
||
453 | for (uint i = 0; i < tmp->count(); ++i) |
||
454 | { |
||
455 | double td = (*tmp)[i]; |
||
456 | nameByAttrs += QString("%1-").arg(td); |
||
457 | } |
||
458 | if (attrsStyles.contains(nameByAttrs)) |
||
459 | { |
||
460 | tname = attrsStyles[nameByAttrs]->getName(); |
||
461 | ++pstyleCounts[nameByAttrs]; |
||
462 | style->setName(tname); |
||
463 | } |
||
464 | else |
||
465 | { |
||
466 | attrsStyles[nameByAttrs] = style; |
||
467 | pstyleCounts[nameByAttrs] = 1; |
||
468 | tname = style->getName(); |
||
469 | } |
||
470 | } |
||
471 | if (!styles.contains(name)) |
||
472 | { |
||
473 | if ((tname.find(docname) == -1) && (usePrefix)) |
||
474 | style->setName(docname + "_" + tname); |
||
475 | styles[name] = style; |
||
476 | } |
||
477 | } |
||
478 | |||
479 | QString StyleReader::getFont(const QString& key) |
||
480 | { |
||
481 | if (fonts.contains(key)) |
||
482 | return fonts[key]; |
||
483 | else |
||
484 | return key; |
||
485 | } |
||
486 | |||
487 | void StyleReader::setupFrameStyle() |
||
488 | { |
||
489 | QString fstyleName = ""; |
||
490 | int count = 0; |
||
491 | CounterMap::Iterator it; |
||
492 | for (it = pstyleCounts.begin(); it != pstyleCounts.end(); ++it) |
||
493 | { |
||
494 | if (it.data() > count) |
||
495 | { |
||
496 | count = it.data(); |
||
497 | fstyleName = it.key(); |
||
498 | } |
||
499 | } |
||
500 | gtFrameStyle* fstyle; |
||
501 | gtParagraphStyle* pstyle = dynamic_cast<gtParagraphStyle*>(attrsStyles[fstyleName]); |
||
502 | fstyle = new gtFrameStyle(*pstyle); |
||
503 | |||
504 | if (!importTextOnly) |
||
505 | writer->setFrameStyle(fstyle); |
||
506 | delete fstyle; |
||
507 | } |
||
508 | |||
509 | bool StyleReader::updateStyle(gtStyle* style, gtStyle* parent2Style, const QString& key, const QString& value) |
||
510 | { |
||
511 | gtParagraphStyle* pstyle = NULL; |
||
512 | if (style->target() == "paragraph") |
||
513 | pstyle = dynamic_cast<gtParagraphStyle*>(style); |
||
514 | else |
||
515 | pstyle = NULL; |
||
516 | QString align = NULL; |
||
517 | QString force = NULL; |
||
518 | |||
519 | if (key == "style:font-name") |
||
520 | style->getFont()->setName(getFont(value)); |
||
521 | else if (key == "fo:font-size") |
||
522 | { |
||
523 | double size = 0; |
||
524 | double psize = 0; |
||
525 | if (parent2Style != NULL) |
||
526 | psize = static_cast<double>(parent2Style->getFont()->getSize()); |
||
527 | else if (styles.contains("default-style")) |
||
528 | psize = static_cast<double>(styles["default-style"]->getFont()->getSize()); |
||
529 | psize = psize / 10; |
||
530 | size = getSize(value, psize); |
||
531 | int nsize = static_cast<int>(size * 10); |
||
532 | style->getFont()->setSize(nsize); |
||
533 | if ((style->getName() == "default-style") && (pstyle != NULL)) |
||
534 | pstyle->setLineSpacing(writer->getPreferredLineSpacing(nsize)); |
||
535 | } |
||
536 | else if ((key == "fo:line-height") && (parent2Style != NULL)) |
||
537 | { |
||
538 | gtParagraphStyle* ppstyle; |
||
539 | if (parent2Style->target() == "paragraph") |
||
540 | { |
||
541 | ppstyle = dynamic_cast<gtParagraphStyle*>(parent2Style); |
||
542 | pstyle->setLineSpacing(getSize(value, writer->getPreferredLineSpacing(style->getFont()->getSize()))); |
||
543 | } |
||
544 | } |
||
545 | else if ((key == "fo:font-weight") && (value == "bold")) |
||
546 | style->getFont()->setWeight(BOLD); |
||
547 | else if ((key == "fo:font-style") && (value == "italic")) |
||
548 | style->getFont()->setSlant(ITALIC); |
||
549 | else if ((key == "style:text-underline") && (value != "none")) |
||
550 | style->getFont()->toggleEffect(UNDERLINE); |
||
551 | else if ((key == "style:text-crossing-out") && (value != "none")) |
||
552 | style->getFont()->toggleEffect(STRIKETHROUGH); |
||
553 | else if ((key == "fo:font-variant") && (value == "small-caps")) |
||
554 | style->getFont()->toggleEffect(SMALL_CAPS); |
||
555 | else if ((key == "style:text-outline") && (value == "true")) |
||
556 | { |
||
557 | style->getFont()->toggleEffect(OUTLINE); |
||
558 | style->getFont()->setStrokeColor("Black"); |
||
559 | style->getFont()->setColor("White"); |
||
560 | } |
||
561 | else if (key == "fo:letter-spacing") |
||
562 | style->getFont()->setKerning(getSize(value)); |
||
563 | else if (key == "style:text-scale") |
||
564 | style->getFont()->setHscale(static_cast<int>(getSize(value, -1.0))); |
||
565 | else if ((key == "style:text-position") && |
||
566 | (((value).find("sub") != -1) || |
||
567 | (((value).left(1) == "-") && ((value).left(1) != "0")))) |
||
568 | style->getFont()->toggleEffect(SUBSCRIPT); |
||
569 | else if ((key == "style:text-position") && |
||
570 | (((value).find("super") != -1) || |
||
571 | (((value).left(1) != "-") && ((value).left(1) != "0")))) |
||
572 | style->getFont()->toggleEffect(SUPERSCRIPT); |
||
573 | else if ((key == "fo:margin-top") && (pstyle != NULL)) |
||
574 | pstyle->setSpaceAbove(getSize(value)); |
||
575 | else if ((key == "fo:margin-bottom") && (pstyle != NULL)) |
||
576 | pstyle->setSpaceBelow(getSize(value)); |
||
577 | else if ((key == "fo:margin-left") && (pstyle != NULL)) |
||
578 | { |
||
579 | if (inList) |
||
580 | pstyle->setIndent(pstyle->getIndent() + getSize(value)); |
||
581 | else |
||
582 | pstyle->setIndent(getSize(value)); |
||
583 | } |
||
584 | else if ((key == "text:space-before") && (pstyle != NULL)) |
||
585 | { |
||
586 | if (inList) |
||
587 | pstyle->setIndent(pstyle->getIndent() + getSize(value)); |
||
588 | else |
||
589 | pstyle->setIndent(getSize(value)); |
||
590 | } |
||
591 | else if ((key == "fo:text-indent") && (pstyle != NULL)) |
||
592 | pstyle->setFirstLineIndent(getSize(value)); |
||
593 | else if ((key == "fo:text-align") && (pstyle != NULL)) |
||
594 | align = value; |
||
595 | else if ((key == "style:justify-single-word") && (pstyle != NULL)) |
||
596 | force = value; |
||
597 | |||
598 | if (align != NULL) |
||
599 | { |
||
600 | if (align == "end") |
||
601 | pstyle->setAlignment(RIGHT); |
||
602 | else if (align == "center") |
||
603 | pstyle->setAlignment(CENTER); |
||
604 | else if (align == "justify") |
||
605 | { |
||
606 | if (force != "false") |
||
607 | pstyle->setAlignment(FORCED); |
||
608 | else |
||
609 | pstyle->setAlignment(BLOCK); |
||
610 | } |
||
611 | } |
||
612 | |||
613 | return true; |
||
614 | } |
||
615 | |||
616 | double StyleReader::getSize(QString s, double parentSize) |
||
617 | { |
||
618 | QString dbl = "0.0"; |
||
619 | QString lowerValue = s.lower(); |
||
620 | double ret = 0.0; |
||
621 | if (lowerValue.find("pt") != -1) |
||
622 | { |
||
623 | dbl = lowerValue.remove("pt"); |
||
624 | ret = gtMeasure::d2d(dbl.toDouble(), PT); |
||
625 | } |
||
626 | else if (lowerValue.find("mm") != -1) |
||
627 | { |
||
628 | dbl = lowerValue.remove("mm"); |
||
629 | ret = gtMeasure::d2d(dbl.toDouble(), MM); |
||
630 | } |
||
631 | else if (lowerValue.find("cm") != -1) |
||
632 | { |
||
633 | dbl = lowerValue.remove("cm"); |
||
634 | ret = gtMeasure::d2d(dbl.toDouble() * 10, MM); |
||
635 | } |
||
636 | else if (lowerValue.find("in") != -1) |
||
637 | { |
||
638 | dbl = lowerValue.remove("inch"); |
||
639 | dbl = lowerValue.remove("in"); |
||
640 | ret = gtMeasure::d2d(dbl.toDouble(), IN); |
||
641 | } |
||
642 | else if (lowerValue.find("pi") != -1) |
||
643 | { |
||
644 | dbl = lowerValue.remove("pica"); |
||
645 | dbl = lowerValue.remove("pi"); |
||
646 | ret = gtMeasure::d2d(dbl.toDouble(), P); |
||
647 | } |
||
648 | else if (lowerValue.find("%") != -1) |
||
649 | { |
||
650 | dbl = lowerValue.remove("%"); |
||
651 | double factor = dbl.toDouble(); |
||
652 | if (parentSize != -1.0) |
||
653 | { |
||
654 | factor = factor / 100; |
||
655 | ret = factor * parentSize; |
||
656 | } |
||
657 | else |
||
658 | ret = factor; |
||
659 | } |
||
660 | return ret; |
||
661 | } |
||
662 | |||
663 | StyleReader::~StyleReader() |
||
664 | { |
||
665 | sreader = NULL; |
||
666 | StyleMap::Iterator it; |
||
667 | for (it = styles.begin(); it != styles.end(); ++it) |
||
668 | { |
||
669 | if (it.data()) |
||
670 | { |
||
671 | delete it.data(); |
||
672 | it.data() = NULL; |
||
673 | } |
||
674 | } |
||
675 | } |
||
676 | |||
677 | xmlSAXHandler sSAXHandlerStruct = { |
||
678 | NULL, // internalSubset, |
||
679 | NULL, // isStandalone, |
||
680 | NULL, // hasInternalSubset, |
||
681 | NULL, // hasExternalSubset, |
||
682 | NULL, // resolveEntity, |
||
683 | NULL, // getEntity, |
||
684 | NULL, // entityDecl, |
||
685 | NULL, // notationDecl, |
||
686 | NULL, // attributeDecl, |
||
687 | NULL, // elementDecl, |
||
688 | NULL, // unparsedEntityDecl, |
||
689 | NULL, // setDocumentLocator, |
||
690 | NULL, // startDocument, |
||
691 | NULL, // endDocument, |
||
692 | StyleReader::startElement, |
||
693 | StyleReader::endElement, |
||
694 | NULL, // reference, |
||
695 | NULL, // characters |
||
696 | NULL, // ignorableWhitespace, |
||
697 | NULL, // processingInstruction, |
||
698 | NULL, // comment, |
||
699 | NULL, // warning, |
||
700 | NULL, // error, |
||
701 | NULL, // fatalError, |
||
702 | NULL, // getParameterEntity, |
||
703 | NULL, // cdata, |
||
704 | NULL, |
||
705 | 1 |
||
706 | #ifdef HAVE_XML26 |
||
707 | , |
||
708 | NULL, |
||
709 | NULL, |
||
710 | NULL, |
||
711 | NULL |
||
712 | #endif |
||
713 | }; |
||
714 | |||
715 | xmlSAXHandlerPtr sSAXHandler = &sSAXHandlerStruct; |
||
716 | |||
717 | void StyleReader::startElement(void *user_data, const xmlChar * fullname, const xmlChar ** atts) |
||
718 | { |
||
719 | QString* name = new QString((const char*) fullname); |
||
720 | name = new QString(name->lower()); |
||
721 | QXmlAttributes* attrs = new QXmlAttributes(); |
||
722 | if (atts) |
||
723 | { |
||
724 | for(const xmlChar** cur = atts; cur && *cur; cur += 2) |
||
725 | attrs->append(QString((char*)*cur), NULL, QString((char*)*cur), QString((char*)*(cur + 1))); |
||
726 | } |
||
727 | sreader->startElement(NULL, NULL, *name, *attrs); |
||
728 | } |
||
729 | |||
730 | void StyleReader::endElement(void *user_data, const xmlChar * name) |
||
731 | { |
||
732 | QString *nname = new QString((const char*) name); |
||
733 | nname = new QString(nname->lower()); |
||
734 | sreader->endElement(NULL, NULL, *nname); |
||
735 | } |
||
736 | |||
418 | Franz | 737 | #endif |