⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qtexthtmlparser.cpp

📁 qt-x11-opensource-src-4.1.4.tar.gz源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
    else if (s == QLatin1String("pre"))        return QTextHtmlParserNode::WhiteSpacePre;    else if (s == QLatin1String("nowrap"))        return QTextHtmlParserNode::WhiteSpaceNoWrap;    else if (s == QLatin1String("pre-wrap"))        return QTextHtmlParserNode::WhiteSpacePreWrap;    return QTextHtmlParserNode::WhiteSpaceModeUndefined;}static bool parseFontSize(QTextHtmlParserNode *node, QString value){    bool parsed = false;    value = value.trimmed();    if (value.endsWith(QLatin1String("pt"))) {        value = value.left(value.length() - 2);        qreal real = 0;        if (setFloatAttribute(&real, value)) {            node->hasFontPointSize = true;            node->fontPointSize = qRound(real);            parsed = true;        }    } else if (value.endsWith(QLatin1String("px"))) {        value = value.left(value.length() - 2);        if (setIntAttribute(&node->fontPixelSize, value)) {            node->hasFontPixelSize = true;            parsed = true;        }    } else if (value == QLatin1String("small")) {        node->hasFontSizeAdjustment = true;        node->fontSizeAdjustment = -1;        parsed = true;    } else if (value == QLatin1String("medium")) {        node->hasFontSizeAdjustment = true;        node->fontSizeAdjustment = 0;        parsed = true;    } else if (value == QLatin1String("large")) {        node->hasFontSizeAdjustment = true;        node->fontSizeAdjustment = 1;        parsed = true;    } else if (value == QLatin1String("x-large")) {        node->hasFontSizeAdjustment = true;        node->fontSizeAdjustment = 2;        parsed = true;    } else if (value == QLatin1String("xx-large")) {        node->hasFontSizeAdjustment = true;        node->fontSizeAdjustment = 3;        parsed = true;    }    return parsed;}static bool parseFontStyle(QTextHtmlParserNode *node, const QString &value){    bool parsed = false;    if (value == QLatin1String("normal")) {        node->fontItalic = Off;        parsed = true;    } else if (value == QLatin1String("italic") || value == QLatin1String("oblique")) {        node->fontItalic = On;        parsed = true;    }    return parsed;}static bool parseFontWeight(QTextHtmlParserNode *node, const QString &value){    bool parsed = false;    if (value == QLatin1String("normal")) {        node->fontWeight = QFont::Normal;        parsed = true;    } else if (value == QLatin1String("bold")) {        node->fontWeight = QFont::Bold;        parsed = true;    } else {        bool ok = false;        int n = value.toInt(&ok);        if (ok) {            node->fontWeight = n/8;            parsed = true;        }    }    return parsed;}static bool parseFontFamily(QTextHtmlParserNode *node, const QString &value){    node->fontFamily = value.trimmed();    if ((node->fontFamily.startsWith(QLatin1Char('\''))         && node->fontFamily.endsWith(QLatin1Char('\'')))        ||        (node->fontFamily.startsWith(QLatin1Char('\"'))         && node->fontFamily.endsWith(QLatin1Char('\"')))       ) {        node->fontFamily.remove(0, 1);        node->fontFamily.chop(1);    }    return true;}static QColor parseColor(QString value){    QColor color;    if (value.startsWith(QLatin1String("rgb("))        && value.endsWith(QLatin1Char(')'))) {        value.chop(1);        value.remove(0, 4);        const QStringList rgb = value.split(',');        if (rgb.count() == 3)            color.setRgb(rgb[0].toInt(), rgb[1].toInt(), rgb[2].toInt());    } else {        color.setNamedColor(value);    }    return color;}// style parser taken from Qt 3static void parseStyleAttribute(QTextHtmlParserNode *node, const QString &value){    QString a = value;    int count = a.count(';')+1;    for (int s = 0; s < count; s++) {        QString style = a.section(';', s, s).trimmed();        if (style.startsWith(QLatin1String("font-size:"))) {            parseFontSize(node, style.mid(10));        } if (style.startsWith(QLatin1String("font-style:"))) {            parseFontStyle(node, style.mid(11).trimmed());        } else if (style.startsWith(QLatin1String("font-weight:"))) {            parseFontWeight(node, style.mid(12));        } else if (style.startsWith(QLatin1String("font-family:"))) {            parseFontFamily(node, style.mid(12));        } else if (style.startsWith(QLatin1String("text-decoration:"))) {            QString s = style.mid(16);            node->fontUnderline = static_cast<bool>(s.contains("underline")) ? On : Off;            node->fontOverline = static_cast<bool>(s.contains("overline")) ? On : Off;            node->fontStrikeOut = static_cast<bool>(s.contains("line-through")) ? On : Off;#if 0        } else if (style.startsWith(QLatin1String("vertical-align:"))) {            QString s = style.mid(15).trimmed();            if (s == QLatin1String("sub"))                format.setVAlign(QTextFormat::AlignSubScript);            else if (s == QLatin1String("super"))                format.setVAlign(QTextFormat::AlignSuperScript);            else                format.setVAlign(QTextFormat::AlignNormal);#endif        } else if (style.startsWith(QLatin1String("color:"))) {            node->color = parseColor(style.mid(6).trimmed());        } else if (style.startsWith(QLatin1String("background-color:"))) {            node->bgColor = parseColor(style.mid(17).trimmed());        } else if (style.startsWith(QLatin1String("float:"))) {            QString s = style.mid(6).trimmed();            node->cssFloat = QTextFrameFormat::InFlow;            if (s == QLatin1String("left"))                node->cssFloat = QTextFrameFormat::FloatLeft;            else if (s == QLatin1String("right"))                node->cssFloat = QTextFrameFormat::FloatRight;        } else if (style.startsWith(QLatin1String("-qt-block-indent:"))) {            const QString s = style.mid(17).trimmed();            if (setIntAttribute(&node->cssBlockIndent, s))                node->hasCssBlockIndent = true;        } else if (style.startsWith(QLatin1String("text-indent:")) && style.endsWith(QLatin1String("px"))) {            node->text_indent = style.mid(12, style.length() - 14).trimmed().toDouble();        } else if (style.startsWith(QLatin1String("-qt-list-indent:"))) {            const QString s = style.mid(16).trimmed();            if (setIntAttribute(&node->cssListIndent, s)) {                node->hasCssListIndent = true;            }        } else if (style.startsWith(QLatin1String("-qt-paragraph-type:"))) {            const QString s = style.mid(19).trimmed().toLower();            if (s == QLatin1String("empty"))                node->isEmptyParagraph = true;        } else if (style.startsWith(QLatin1String("-qt-table-type:"))) {            const QString s = style.mid(15).trimmed().toLower();            if (s == QLatin1String("frame"))                node->isTextFrame = true;        } else if (style.startsWith(QLatin1String("white-space:"))) {            const QString s = style.mid(12).trimmed().toLower();            QTextHtmlParserNode::WhiteSpaceMode ws = stringToWhiteSpaceMode(s);            if (ws != QTextHtmlParserNode::WhiteSpaceModeUndefined)                node->wsm = ws;        } else if (style.startsWith(QLatin1String("margin-top:")) && style.endsWith("px")) {            const QString s = style.mid(11, style.length() - 13).trimmed();            setIntAttribute(&node->margin[QTextHtmlParser::MarginTop], s);        } else if (style.startsWith(QLatin1String("margin-bottom:")) && style.endsWith("px")) {            const QString s = style.mid(14, style.length() - 16).trimmed();            setIntAttribute(&node->margin[QTextHtmlParser::MarginBottom], s);        } else if (style.startsWith(QLatin1String("margin-left:")) && style.endsWith("px")) {            const QString s = style.mid(12, style.length() - 14).trimmed();            setIntAttribute(&node->margin[QTextHtmlParser::MarginLeft], s);        } else if (style.startsWith(QLatin1String("margin-right:")) && style.endsWith("px")) {            const QString s = style.mid(13, style.length() - 15).trimmed();            setIntAttribute(&node->margin[QTextHtmlParser::MarginRight], s);        } else if (style.startsWith(QLatin1String("vertical-align:"))) {            const QString s = style.mid(15, style.length() - 15).trimmed();            if (s == "sub")                node->verticalAlignment = QTextCharFormat::AlignSubScript;            else if (s == "super")                node->verticalAlignment = QTextCharFormat::AlignSuperScript;            else                node->verticalAlignment = QTextCharFormat::AlignNormal;        } else if (style.startsWith(QLatin1String("font:"))) { // shorthand font property            // first reset to initial values            node->fontItalic = false;            node->fontWeight = QFont::Normal;            node->hasFontPointSize = false;            node->fontSizeAdjustment = 0;            node->fontFamily.clear();            style = style.mid(5).trimmed();            QStringList values;            for (int i = 0; i < style.count(); ++i) {                if (style.at(i) == QLatin1Char('\"')) {                    ++i;                    while (i < style.count() && style.at(i) != QLatin1Char('\"'))                        ++i;                } else if (style.at(i) == QLatin1Char('\'')) {                    ++i;                    while (i < style.count() && style.at(i) != QLatin1Char('\''))                        ++i;                } else if (style.at(i).isSpace()) {                    values << style.left(i);                    style.remove(0, i + 1);                    i = -1;                }            }            if (!style.isEmpty()) {                values << style;                style.clear();            }            if (values.isEmpty())                continue;            bool foundStyleOrWeight = false;            do {                const QString val = values.first();                foundStyleOrWeight = parseFontStyle(node, val);                if (!foundStyleOrWeight)                    foundStyleOrWeight = parseFontWeight(node, val);                if (foundStyleOrWeight)                    values.removeFirst();            } while (foundStyleOrWeight && !values.isEmpty());            if (values.isEmpty())                continue;            parseFontSize(node, values.takeFirst());            if (values.isEmpty())                continue;            parseFontFamily(node, values.takeFirst());        }    }}void QTextHtmlParser::parseAttributes(){    // local state variable for qt3 textedit mode    bool seenQt3Richtext = false;    QTextHtmlParserNode *node = &nodes.last();    while (pos < len) {        eatSpace();        if (hasPrefix(QLatin1Char('>')) || hasPrefix(QLatin1Char('/')))            break;        QString key = parseWord().toLower();        QString value = QLatin1String("1");        if (key.size() == 0)            break;        eatSpace();        if (hasPrefix(QLatin1Char('='))){            pos++;            eatSpace();            value = parseWord();        }        if (value.size() == 0)            continue;        switch (node->id) {            case Html_font:                // the infamous font tag                if (key == QLatin1String("size") && value.size()) {                    int n = value.toInt();                    if (value.at(0) != QLatin1Char('+') && value.at(0) != QLatin1Char('-'))                        n -= 3;                    node->fontSizeAdjustment = n;                    node->hasFontSizeAdjustment = true;                } else if (key == QLatin1String("face")) {                    node->fontFamily = value;                } else if (key == QLatin1String("color")) {                    node->color.setNamedColor(value);                }                break;            case Html_ol:            case Html_ul:                if (key == QLatin1String("type")) {                    node->hasOwnListStyle = true;                    if (value == QLatin1String("1")) {                        node->listStyle = QTextListFormat::ListDecimal;                    } else if (value == QLatin1String("a")) {                        node->listStyle = QTextListFormat::ListLowerAlpha;                    } else if (value == QLatin1String("A")) {                        node->listStyle = QTextListFormat::ListUpperAlpha;                    } else {                        value = value.toLower();                        if (value == QLatin1String("square"))                            node->listStyle = QTextListFormat::ListSquare;                        else if (value == QLatin1String("disc"))                            node->listStyle = QTextListFormat::ListDisc;                        else if (value == QLatin1String("circle"))                            node->listStyle = QTextListFormat::ListCircle;                    }                }                break;            case Html_a:                if (key == QLatin1String("href"))                    node->anchorHref = value;                else if (key == QLatin1String("name"))                    node->anchorName = value;                break;            case Html_img:                if (key == QLatin1String("src") || key == QLatin1String("source")) {                    node->imageName = value;                } else if (key == QLatin1String("width")) {                    setFloatAttribute(&node->imageWidth, value);                } else if (key == QLatin1String("height")) {                    setFloatAttribute(&node->imageHeight, value);                }                break;            case Html_tr:            case Html_body:                if (key == QLatin1String("bgcolor"))                    node->bgColor.setNamedColor(value);                break;            case Html_th:            case Html_td:                if (key == QLatin1String("width")) {                    setWidthAttribute(&node->width, value);                } else if (key == QLatin1String("bgcolor")) {                    node->bgColor.setNamedColor(value);                } else if (key == QLatin1String("rowspan")) {                    setIntAttribute(&node->tableCellRowSpan, value);                } else if (key == QLatin1String("colspan")) {                    setIntAttribute(&node->tableCellColSpan, value);                }                break;            case Html_table:                if (key == QLatin1String("border")) {                    setFloatAttribute(&node->tableBorder, value);                } else if (key == QLatin1String("bgcolor")) {                    node->bgColor.setNamedColor(value);                } else if (key == QLatin1String("cellspacing")) {                    setFloatAttribute(&node->tableCellSpacing, value);                } else if (key == QLatin1String("cellpadding")) {                    setFloatAttribute(&node->tableCellPadding, value);                } else if (key == QLatin1String("width")) {                    setWidthAttribute(&node->width, value);                } else if (key == QLatin1String("height")) {                    setWidthAttribute(&node->height, value);                }                break;            case Html_meta:                if (key == QLatin1String("name")                    && value == QLatin1String("qrichtext")) {                    seenQt3Richtext = true;                }                if (key == QLatin1String("content")                    && value == QLatin1String("1")                    && seenQt3Richtext) {                    textEditMode = true;                }                break;            case Html_hr:                if (key == QLatin1String("width"))                    setWidthAttribute(&node->width, value);                break;            default:                break;        }        if (key == QLatin1String("style")) {            parseStyleAttribute(node, value);        } else if (key == QLatin1String("align")) {            if (value == QLatin1String("left"))                node->alignment = Qt::AlignLeft|Qt::AlignAbsolute;            else if (value == QLatin1String("right"))                node->alignment = Qt::AlignRight|Qt::AlignAbsolute;            else if (value == QLatin1String("center"))                node->alignment = Qt::AlignHCenter;            else if (value == QLatin1String("justify"))                node->alignment = Qt::AlignJustify;            // HTML4 compat            if (node->id == Html_img) {                if (node->alignment == Qt::AlignLeft)                    node->cssFloat = QTextFrameFormat::FloatLeft;                else if (node->alignment == Qt::AlignRight)                    node->cssFloat = QTextFrameFormat::FloatRight;            }        } else if (key == QLatin1String("dir")) {            if (value == QLatin1String("ltr"))                node->direction = Qt::LeftToRight;            else if (value == QLatin1String("rtl"))                node->direction = Qt::RightToLeft;        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -