[kgraphviewer-devel] [kgraphviewer] src/part: optimize: interpolate fontwidth and save calls to slow QFontMetrics::width

Milian Wolff mail at milianw.de
Fri Jul 15 16:04:30 UTC 2011


Git commit 863cda2aaf84d311e0ed5672b3a6c3125a1608c6 by Milian Wolff.
Committed on 15/07/2011 at 18:03.
Pushed by mwolff into branch 'master'.

optimize: interpolate fontwidth and save calls to slow QFontMetrics::width

already gives a 50% speedup (according to callgrind) for big graphs with lots of text,
as e.g. created by massif-visualizer

M  +9    -4    src/part/canvaselement.cpp     

http://commits.kde.org/kgraphviewer/863cda2aaf84d311e0ed5672b3a6c3125a1608c6

diff --git a/src/part/canvaselement.cpp b/src/part/canvaselement.cpp
index e3d6284..dcbb28e 100644
--- a/src/part/canvaselement.cpp
+++ b/src/part/canvaselement.cpp
@@ -451,13 +451,18 @@ QWidget *widget)
       int fontSize = element()->fontSize();
 //       kDebug() << element()->id() << " initial fontSize " << fontSize;
       m_font->setPointSize(fontSize);
+
       QFontMetrics fm(*m_font);
-      while (fm.width(dro.str) > stringWidthGoal && fontSize > 1)
+      int fontWidth = fm.width(dro.str);
+      while (fontWidth > stringWidthGoal && fontSize > 1)
       {
-        fontSize--;
+        // use floor'ed extrapolated font size
+        fontSize = double(stringWidthGoal) / fontWidth * fontSize;
         m_font->setPointSize(fontSize);
         fm = QFontMetrics(*m_font);
+        fontWidth = fm.width(dro.str);
       }
+
       p->save();
       p->setFont(*m_font);
       QPen pen(m_pen);
@@ -466,8 +471,8 @@ QWidget *widget)
       qreal x = (m_scaleX *
                        (
                          (dro.integers[0])
-                         + (((-dro.integers[2])*(fm.width(dro.str)))/2)
-                         - ( (fm.width(dro.str))/2 )
+                         + (((-dro.integers[2])*(fontWidth))/2)
+                         - ( (fontWidth)/2 )
                        )
                       )
                       + m_xMargin;


More information about the kgraphviewer-devel mailing list