From 4fd28ba87492f2f4725e48f1a3e30ed8cd099081 Mon Sep 17 00:00:00 2001 From: Ariya Hidayat Date: Wed, 2 Sep 2009 11:50:45 +0200 Subject: Faster SVG color parsing by tackling the #rrggbb color early. If the color starts with '#', let's parse it ourselves rather than waiting for the (fall-back) QColor-from-QString which even requires us to create a QString out of the QStringRef. All widely used illustration programs output SVG with #rrggbb format to specify the color. Loading tiger.svg (tests/benchmarks/qsvgrenderer) enjoys 2.4% speed-up. Reviewed-by: Kim --- src/svg/qsvghandler.cpp | 78 +++++++++++++++++++++++++++++-------------------- 1 file changed, 47 insertions(+), 31 deletions(-) diff --git a/src/svg/qsvghandler.cpp b/src/svg/qsvghandler.cpp index 6620707..38ffe3e 100644 --- a/src/svg/qsvghandler.cpp +++ b/src/svg/qsvghandler.cpp @@ -65,6 +65,7 @@ #include "qmath.h" #include "qnumeric.h" #include "qvarlengtharray.h" +#include "private/qcolor_p.h" #include "private/qmath_p.h" #include "float.h" @@ -718,40 +719,55 @@ static bool resolveColor(const QStringRef &colorStr, QColor &color, QSvgHandler return false; switch(colorStrTr.at(0).unicode()) { - case 'r': { - // starts with "rgb(" - if (colorStrTr == QLatin1String("rgb(")) { - const QChar *s = colorStrTr.constData() + 4; - QVector compo = parseNumbersList(s); - //1 means that it failed after reaching non-parsable - //character which is going to be "%" - if (compo.size() == 1) { - const QChar *s = colorStrTr.constData() + 4; - compo = parsePercentageList(s); - compo[0] *= (qreal)2.55; - compo[1] *= (qreal)2.55; - compo[2] *= (qreal)2.55; + + case '#': + { + // #rrggbb is very very common, so let's tackle it here + // rather than falling back to QColor + QRgb rgb; + bool ok = qt_get_hex_rgb(colorStrTr.unicode(), colorStrTr.length(), &rgb); + if (ok) + color.setRgb(rgb); + return ok; } + break; + + case 'r': + { + // starts with "rgb(" + if (colorStrTr == QLatin1String("rgb(")) { + const QChar *s = colorStrTr.constData() + 4; + QVector compo = parseNumbersList(s); + //1 means that it failed after reaching non-parsable + //character which is going to be "%" + if (compo.size() == 1) { + const QChar *s = colorStrTr.constData() + 4; + compo = parsePercentageList(s); + compo[0] *= (qreal)2.55; + compo[1] *= (qreal)2.55; + compo[2] *= (qreal)2.55; + } - color = QColor(int(compo[0]), - int(compo[1]), - int(compo[2])); - return true; + color = QColor(int(compo[0]), + int(compo[1]), + int(compo[2])); + return true; + } } - } - break; - case 'c': - if (colorStrTr == QLatin1String("currentColor")) { - color = handler->currentColor(); - return true; - } - break; - case 'i': - if (colorStrTr == QT_INHERIT) - return false; - break; - default: - break; + break; + + case 'c': + if (colorStrTr == QLatin1String("currentColor")) { + color = handler->currentColor(); + return true; + } + break; + case 'i': + if (colorStrTr == QT_INHERIT) + return false; + break; + default: + break; } color = QColor(colorStrTr.toString()); -- cgit v0.12