From c9e955091937f0a4b24c3f3e1ec5c6547c8f4c39 Mon Sep 17 00:00:00 2001 From: Jan Zeleny Date: Fri, 28 Sep 2012 22:45:08 +0000 Subject: Create only styles for element types Up until now a new style was created for every start tag in the HTML code. Now there are only a handful of styles are created, one for each element type (h1, h2, code, div, ...). Other styles are applied directly to characters. --- scribus/plugins/gettext/htmlim/htmlreader.cpp | 157 +++++++++++--------------- scribus/plugins/gettext/htmlim/htmlreader.h | 6 +- 2 files changed, 74 insertions(+), 89 deletions(-) diff --git a/scribus/plugins/gettext/htmlim/htmlreader.cpp b/scribus/plugins/gettext/htmlim/htmlreader.cpp index 6f0fcca..9fb9029 100644 --- a/scribus/plugins/gettext/htmlim/htmlreader.cpp +++ b/scribus/plugins/gettext/htmlim/htmlreader.cpp @@ -39,7 +39,7 @@ extern htmlSAXHandlerPtr mySAXHandler; HTMLReader::HTMLReader(gtParagraphStyle *ps, gtWriter *w, bool textOnly): href(""), extLinks(""), extIndex(1), writer(w), pstyle(ps), lineBrokenByBlock(true), -lineBroken(true) +lineBroken(true), paragraphSet(false) { inElement.li = false; inElement.pre = false; @@ -63,6 +63,8 @@ void HTMLReader::initPStyles() styleStack = new QStack(); styleStack->push(pstyle); + elementStack = new QStack(); + elementStack->push(QString("-")); styleNumber = 0; blockElements = new QVector(); @@ -71,74 +73,49 @@ void HTMLReader::initPStyles() blockElements->append(element); } - elements = new QMap(); - style = QString("font-size: +2.5px; font-weight: bold; margin-top: 2.5px; margin-bottom: 1.25px"); - element = QString("h6"); - elements->insert(element, style); - - style = QString("font-size: +5px; font-weight: bold; margin-top: 5px; margin-bottom: 2.5px"); - element = QString("h5"); - elements->insert(element, style); - - style = QString("font-size: +10px; font-weight: bold; margin-top: 10px; margin-bottom: 5px"); - element = QString("h4"); - elements->insert(element, style); - - style = QString("font-size: +20px; font-weight: bold; margin-top: 20px; margin-bottom: 10px"); - element = QString("h3"); - elements->insert(element, style); - - style = QString("font-size: +40px; font-weight: bold; margin-top: 30px; margin-bottom: 20px"); - element = QString("h2"); - elements->insert(element, style); - - style = QString("font-size: +60px; font-weight: bold; margin-top: 40px; margin-bottom: 30px"); - element = QString("h1"); - elements->insert(element, style); - - style = QString("font-family: Courier Regular"); - element = QString("code"); - elements->insert(element, style); - - style = QString("margin-bottom: %1").arg(gtMeasure::i2d(5, SC_MM)); - element = QString("p"); - elements->insert(element, style); - - style = QString("font-style: italic"); - element = QString("em"); - elements->insert(element, style); - element = QString("i"); - elements->insert(element, style); - - style = QString("font-weight: bold"); - element = QString("strong"); - elements->insert(element, style); - element = QString("b"); - elements->insert(element, style); - - style = QString("text-align: center"); - element = QString("center"); - elements->insert(element, style); - - style = QString("color: Blue; text-decoration: underline"); - element = QString("a"); - elements->insert(element, style); - - style = QString("text-decoration: underline"); - element = QString("ins"); - elements->insert(element, style); - element = QString("u"); - elements->insert(element, style); - - style = QString("text-decoration: strikethrough"); - element = QString("del"); - elements->insert(element, style); - - style = QString("margin-left: +25px"); - element = QString("ul"); - elements->insert(element, style); - element = QString("ol"); - elements->insert(element, style); + elements = new QMap(); + addElementToMap("h6", "font-size: +2.5px; font-weight: bold; margin-top: 2.5px; margin-bottom: 1.25px"); + addElementToMap("h5", "font-size: +5px; font-weight: bold; margin-top: 5px; margin-bottom: 2.5px"); + addElementToMap("h4", "font-size: +10px; font-weight: bold; margin-top: 10px; margin-bottom: 5px"); + addElementToMap("h3", "font-size: +20px; font-weight: bold; margin-top: 20px; margin-bottom: 10px"); + addElementToMap("h2", "font-size: +40px; font-weight: bold; margin-top: 30px; margin-bottom: 20px"); + addElementToMap("h1", "font-size: +60px; font-weight: bold; margin-top: 40px; margin-bottom: 30px"); + addElementToMap("code", "font-family: Courier Regular"); + addElementToMap("p", QString("margin-bottom: %1").arg(gtMeasure::i2d(5, SC_MM)).toAscii().data()); + addElementToMap("em", "font-style: italic"); + addElementToMap("i", "font-style: italic"); + addElementToMap("strong", "font-weight: bold"); + addElementToMap("b", "font-weight: bold"); + addElementToMap("center", "text-align: center"); + addElementToMap("a", "color: Blue; text-decoration: underline"); + addElementToMap("ins", "text-decoration: underline"); + addElementToMap("u", "text-decoration: underline"); + addElementToMap("del", "text-decoration: strikethrough"); + addElementToMap("ul", "margin-left: +25px"); + addElementToMap("ol", "margin-left: +25px"); + addElementToMap("div", ""); + addElementToMap("span", ""); + addElementToMap("li", ""); + addElementToMap("pre", ""); + addElementToMap("sub", ""); + addElementToMap("sup", ""); + // FIXME: verify if img should be in the map (does img create create new paragraph?) + // addElementToMap("img", ""); +} + +void HTMLReader::addElementToMap(const char *name, const char *style) +{ + QString element = QString(name); + QString elementStyle = QString(style); + gtParagraphStyle *paragraphStyle = new gtParagraphStyle(QString()); + + setStyle(paragraphStyle, elementStyle); + paragraphStyle->setName(QString("style_")+element); + + if (elements->contains(element)) { + delete elements->value(element); + } + elements->insert(element, paragraphStyle); } void HTMLReader::startElement(void*, const xmlChar * fullname, const xmlChar ** atts) @@ -245,21 +222,15 @@ bool HTMLReader::startElement(const QString&, const QString&, const QString &nam } } - if (elements->contains(name)) { - style = elements->value(name); - } else { - style = ""; + if (elements->contains(name) && paragraphSet == false && lineBrokenByBlock == true) { + writer->setParagraphStyle(elements->value(name)); + paragraphSet = true; } - if (!styleAttr.isEmpty()) { - style += ";"+styleAttr; - } - - if (setStyle(newStyle, style)) { + if (setStyle(newStyle, styleAttr)) { styleNumber++; - newStyle->setName(QString("style")+QString::number(styleNumber)); styleStack->push(newStyle); - writer->setParagraphStyle(newStyle); + elementStack->push(name); } else { /* An error occurred */ } @@ -326,6 +297,7 @@ bool HTMLReader::characters(const QString &ch) } lineBroken = false; lineBrokenByBlock = false; + paragraphSet = false; return true; } @@ -338,6 +310,9 @@ void HTMLReader::endElement(void*, const xmlChar * name) bool HTMLReader::endElement(const QString&, const QString&, const QString &name) { + gtParagraphStyle *newStyle; + QString newElement; + for (int i = 0; i < blockElements->count(); i++) { if (name == blockElements->at(i)) { writer->append("\n"); @@ -346,6 +321,7 @@ bool HTMLReader::endElement(const QString&, const QString&, const QString &name) break; } } + paragraphSet = false; if (name == "ul" || name == "ol") { if (listStyles.count()) { @@ -368,6 +344,7 @@ bool HTMLReader::endElement(const QString&, const QString&, const QString &name) if (styleStack->count() > 1) { styleStack->pop(); + elementStack->pop(); } if (name == "a") { @@ -384,7 +361,14 @@ bool HTMLReader::endElement(const QString&, const QString&, const QString &name) href = ""; } - writer->setParagraphStyle(styleStack->top()); + if (lineBrokenByBlock) { + newElement = elementStack->top(); + if (elements->contains(newElement)) { + newStyle = elements->value(newElement); + std::cout << "2 - " << newElement.toAscii().data() << std::endl; + writer->setParagraphStyle(newStyle); + } + } return true; } @@ -631,15 +615,12 @@ htmlSAXHandlerPtr mySAXHandler = &mySAXHandlerStruct; HTMLReader::~HTMLReader() { gtParagraphStyle *style; - QString styleString; - if (!extLinks.isEmpty()) { - style = new gtParagraphStyle(*pstyle); - styleString = elements->value(QString("h4")); - setStyle(style, styleString); - writer->append(QObject::tr("\nExternal Links\n"), style); + writer->append("\n"); + writer->setParagraphStyle(elements->value(QString("h4"))); + writer->append(QObject::tr("External Links"), pstyle); + writer->append("\n"); writer->append(extLinks, pstyle); - delete style; } delete elements; diff --git a/scribus/plugins/gettext/htmlim/htmlreader.h b/scribus/plugins/gettext/htmlim/htmlreader.h index b28300d..6333a5e 100644 --- a/scribus/plugins/gettext/htmlim/htmlreader.h +++ b/scribus/plugins/gettext/htmlim/htmlreader.h @@ -59,9 +59,10 @@ private: gtParagraphStyle *pstyle; QMap alignments; - QMap *elements; + QMap *elements; QVector *blockElements; QStack *styleStack; + QStack *elementStack; int styleNumber; /* This indicates that one line break has just been done. * Without this, two following block elements would create @@ -72,6 +73,8 @@ private: * filter spaces on the beginning of lines after line breaks */ bool lineBroken; + bool paragraphSet; + QStack listStyles; QStack nextItemNumbers; @@ -83,6 +86,7 @@ private: bool noFormatting; void initPStyles(); + void addElementToMap(const char *elementName, const char *elementStyle); double getSize(double currentFontSize, QString styleValue, bool isFontSize); static HTMLReader* hreader; public: -- cgit