Maxim Integrated / Mbed OS MAXREFDES155#

Dependencies:   MaximInterface

Revision:
8:a0d75dff3c9b
Parent:
6:0c9050b02876
Child:
10:71359af61af8
diff -r 66c5dedc750b -r a0d75dff3c9b Text.cpp
--- a/Text.cpp	Thu Mar 09 11:38:33 2017 -0600
+++ b/Text.cpp	Thu Apr 06 15:16:30 2017 -0500
@@ -39,8 +39,10 @@
 
 using std::string;
 
-static const unsigned int characterWidth = 5;
-static const unsigned int characterHeight = 7;
+static const int invalidWidthHeight = -1;
+
+static const int characterWidth = 5;
+static const int characterHeight = 7;
 
 static const unsigned char printableCharBegin = 0x20;
 static const unsigned char printableCharEnd = 0x7E;
@@ -220,22 +222,16 @@
     return toSplitLines;
 }
 
-Text::Text(const string & text, Graphic * parent) :
-    Graphic(parent), m_text(text), m_wordWrap(false), m_charSpacing(1), m_lineSpacing(1),
-    m_textLines(), m_preferredWidth(0), m_preferredHeight(0)
-{
-    // Default to the preferred width and height.
-    update();
-    setWidth(preferredWidth());
-    setHeight(preferredHeight());
-}
+Text::Text() : m_text(), m_wordWrap(false), m_charSpacing(1), m_lineSpacing(1),
+    m_textLines(), m_preferredWidth(invalidWidthHeight), m_preferredHeight(invalidWidthHeight) { }
 
 void Text::setText(const string & text)
 {
     if (m_text != text)
     {
         m_text = text;
-        invalidateRegion();
+        invalidate();
+        m_preferredWidth = m_preferredHeight = invalidWidthHeight;
     }
 }
 
@@ -244,34 +240,91 @@
     if (m_wordWrap != wordWrap)
     {
         m_wordWrap = wordWrap;
-        invalidateRegion();
+        invalidate();
+        m_preferredWidth = m_preferredHeight = invalidWidthHeight;
     }
 }
 
-void Text::setLineSpacing(unsigned int lineSpacing)
+void Text::setLineSpacing(int lineSpacing)
 {
-    if (m_lineSpacing != lineSpacing)
+    if (lineSpacing > 0 && m_lineSpacing != lineSpacing)
     {
         m_lineSpacing = lineSpacing;
-        invalidateRegion();
+        invalidate();
+        m_preferredHeight = invalidWidthHeight;
+    }
+}
+
+void Text::setCharSpacing(int charSpacing)
+{
+    if (charSpacing > 0 && m_charSpacing != charSpacing)
+    {
+        m_charSpacing = charSpacing;
+        invalidate();
+        m_preferredWidth = invalidWidthHeight;
+        if (wordWrap())
+        {
+            m_preferredHeight = invalidWidthHeight;
+        }
     }
 }
 
-void Text::setCharSpacing(unsigned int charSpacing)
+int Text::preferredWidth() const
+{
+    if (m_preferredWidth == invalidWidthHeight)
+    {
+        calculateLayout();
+    }
+    return m_preferredWidth;
+}
+
+int Text::preferredHeight() const
 {
-    if (m_charSpacing != charSpacing)
+    if (m_preferredHeight == invalidWidthHeight)
     {
-        m_charSpacing = charSpacing;
-        invalidateRegion();
+        calculateLayout();
+    }
+    return m_preferredHeight;
+}
+
+void Text::resized()
+{
+    m_preferredWidth = invalidWidthHeight;
+    if (wordWrap())
+    {
+        m_preferredHeight = invalidWidthHeight;
     }
 }
+
+void Text::doRender(Bitmap & bitmap) const
+{   
+    using std::list;
     
-void Text::doPostLayout()
+    // Ensure layout is up to date.
+    if (m_preferredWidth == invalidWidthHeight || m_preferredHeight == invalidWidthHeight)
+    {
+        calculateLayout();
+    }
+    
+    // Render each line.
+    int lineNum = 0;
+    for (list<string>::const_iterator lineIt = m_textLines.begin(); lineIt != m_textLines.end(); lineIt++)
+    {
+        // Render each character.
+        for (string::size_type charNum = 0; charNum < lineIt->length(); charNum++)
+        {
+            Bitmap character(characterWidth, characterHeight,
+                &characterMap[(*lineIt)[charNum] - printableCharBegin][0]);
+            bitmap.overlay(character, charNum * (characterWidth + m_charSpacing),
+                lineNum * (characterHeight + m_lineSpacing));
+        }
+        lineNum++;
+    }
+}
+
+void Text::calculateLayout() const
 {
     using std::list;
-    
-    if (regionValid())
-        return;
                 
     // Split string into lines.
     m_textLines = tokenizeString(m_text, '\n');
@@ -279,10 +332,10 @@
     // Remove non-printable characters.
     std::for_each(m_textLines.begin(), m_textLines.end(), removeNonprintableChars);
         
-    const unsigned int lineLen =
+    const int lineLen =
         (width() / (characterWidth + m_charSpacing)) +
         (((width() % (characterWidth + m_charSpacing)) >= characterWidth) ? 1 : 0);
-    const unsigned int numLines =
+    const int numLines =
         (height() / (characterHeight + m_lineSpacing)) +
         (((height() % (characterHeight + m_lineSpacing)) >= characterHeight) ? 1 : 0);
         
@@ -314,29 +367,7 @@
         (m_textLines.size() * characterHeight) + ((m_textLines.size() - 1) * m_lineSpacing);
     
     // Remove clipped text.
-    if (m_textLines.size() > numLines)
+    if (m_textLines.size() > static_cast<list<string>::size_type>(numLines))
         m_textLines.resize(numLines);
     std::for_each(m_textLines.begin(), m_textLines.end(), TrimString(lineLen));
 }
-
-Bitmap Text::render() const
-{   
-    using std::list;
-    
-    // Render each line.
-    Bitmap bitmap(width(), height());
-    unsigned int lineNum = 0;
-    for (list<string>::const_iterator lineIt = m_textLines.begin(); lineIt != m_textLines.end(); lineIt++)
-    {
-        // Render each character.
-        for (string::size_type charNum = 0; charNum < lineIt->length(); charNum++)
-        {
-            Bitmap character(characterWidth, characterHeight,
-                &characterMap[(*lineIt)[charNum] - printableCharBegin][0]);
-            bitmap.overlay(character, charNum * (characterWidth + m_charSpacing),
-                lineNum * (characterHeight + m_lineSpacing));
-        }
-        lineNum++;
-    }
-    return bitmap;
-}