Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Fix for issue #243  (Read 2066 times)

0 Members and 1 Guest are viewing this topic.

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Fix for issue #243
« on: February 13, 2013, 10:43:03 pm »
Hello!
There is an issue on the tracker, which I was interested in for a long time. I finally found some time to investigate it and noticed it is very easy to implement. Basically in the same spots a line for the underline style is drawn, I check for the strike through style and draw a line. That's pretty much it. I tested the results with different font and it works as expected. The patch doesn't break the current API.
Also in your comment next to the italic variable you say 12 degree, but 2 * pi * (12/360) is 0.2094... instead of 0.208. I "fixed" that (it's not like anybody is gonna notice it, just for the mathematical correctness).
I don't really know how to apply the patch to github and I thought I should get some feedback first anyway. So I'm just gonna present the patch file here.
--- C:\Librarys\SFML Source\SFML\src\SFML\Graphics\Text.cpp     Thu Jan  3 12:39:24 2013
+++ C:\Users\User\Programs\Strikethrough Text\Text.cpp  Wed Feb 13 21:12:38 2013
@@ -238,11 +238,13 @@
         return;
 
     // Compute values related to the text style
-    bool  bold               = (m_style & Bold) != 0;
-    bool  underlined         = (m_style & Underlined) != 0;
-    float italic             = (m_style & Italic) ? 0.208f : 0.f; // 12 degrees
-    float underlineOffset    = m_characterSize * 0.1f;
-    float underlineThickness = m_characterSize * (bold ? 0.1f : 0.07f);
+    bool  bold                = (m_style & Bold) != 0;
+    bool  underlined          = (m_style & Underlined) != 0;
+    bool  strikethrough       = (m_style & Strikethrough) != 0;
+    float italic              = (m_style & Italic) ? 0.209f : 0.f; // 12 degrees
+    float lineThickness       = m_characterSize * (bold ? 0.1f : 0.07f);
+    float underlineOffset     = m_characterSize * 0.1f;
+    float strikethroughOffset = m_characterSize * 0.4f ;
 
     // Precompute the variables needed by the algorithm
     float hspace = static_cast<float>(m_font->getGlyph(L' ', m_characterSize, bold).advance);
@@ -260,24 +262,40 @@
         x += static_cast<float>(m_font->getKerning(prevChar, curChar, m_characterSize));
         prevChar = curChar;
 
-        // If we're using the underlined style and there's a new line, draw a line
-        if (underlined && (curChar == L'\n'))
-        {
-            float top = y + underlineOffset;
-            float bottom = top + underlineThickness;
-
-            m_vertices.append(Vertex(Vector2f(0, top),    m_color, Vector2f(1, 1)));
-            m_vertices.append(Vertex(Vector2f(x, top),    m_color, Vector2f(1, 1)));
-            m_vertices.append(Vertex(Vector2f(x, bottom), m_color, Vector2f(1, 1)));
-            m_vertices.append(Vertex(Vector2f(0, bottom), m_color, Vector2f(1, 1)));
-        }
-
         // Handle special characters
         switch (curChar)
         {
-            case L' ' :  x += hspace;        continue;
-            case L'\t' : x += hspace * 4;    continue;
-            case L'\n' : y += vspace; x = 0; continue;
+            case L' '  : x += hspace;     continue;
+            case L'\t' : x += hspace * 4; continue;
+            case L'\n' :
+                // If we're using the underlined style and there's a new line, draw a line
+                if(underlined)
+                {
+                    float top = y + underlineOffset;
+                    float bottom = top + lineThickness;
+
+                    m_vertices.append(Vertex(sf::Vector2f(0, top),    m_color, Vector2f(1, 1)));
+                    m_vertices.append(Vertex(sf::Vector2f(x, top),    m_color, Vector2f(1, 1)));
+                    m_vertices.append(Vertex(sf::Vector2f(x, bottom), m_color, Vector2f(1, 1)));
+                    m_vertices.append(Vertex(sf::Vector2f(0, bottom), m_color, Vector2f(1, 1)));
+                }
+
+                // If we're using the strikethrough style and there's a new line, draw a line across all charcters
+                if (strikethrough)
+                {
+                    float top = y - strikethroughOffset;
+                    float bottom = top + lineThickness;
+
+                    m_vertices.append(Vertex(sf::Vector2f(0, top),    m_color, Vector2f(1, 1)));
+                    m_vertices.append(Vertex(sf::Vector2f(x, top),    m_color, Vector2f(1, 1)));
+                    m_vertices.append(Vertex(sf::Vector2f(x, bottom), m_color, Vector2f(1, 1)));
+                    m_vertices.append(Vertex(sf::Vector2f(0, bottom), m_color, Vector2f(1, 1)));
+                }
+
+                y += vspace;
+                x = 0;
+                continue;
+
             case L'\v' : y += vspace * 4;    continue;
         }
 
@@ -308,7 +326,19 @@
     if (underlined)
     {
         float top = y + underlineOffset;
-        float bottom = top + underlineThickness;
+        float bottom = top + lineThickness;
+
+        m_vertices.append(Vertex(sf::Vector2f(0, top),    m_color, Vector2f(1, 1)));
+        m_vertices.append(Vertex(sf::Vector2f(x, top),    m_color, Vector2f(1, 1)));
+        m_vertices.append(Vertex(sf::Vector2f(x, bottom), m_color, Vector2f(1, 1)));
+        m_vertices.append(Vertex(sf::Vector2f(0, bottom), m_color, Vector2f(1, 1)));
+    }
+
+    // If we're using the strikethrough style, add the a line across all charcters
+    if (strikethrough)
+    {
+        float top = y - strikethroughOffset;
+        float bottom = top + lineThickness;
 
         m_vertices.append(Vertex(Vector2f(0, top),    m_color, Vector2f(1, 1)));
         m_vertices.append(Vertex(Vector2f(x, top),    m_color, Vector2f(1, 1)));
 
For the header file it's simply a matter of adding Strikethrough = 1 << 3 ///< Strikedthough characters to the enum.
If I should post this somewhere else, please tell me.

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Fix for issue #243
« Reply #1 on: February 13, 2013, 11:00:40 pm »
Damn it! I just noticed that my code can be optimized to:
    if (underlined || strikethrough)
    {
        float top = 0.f;

        if(underline)
            top = y + underlineOffset;
        else
            top = y - strikethroughOffset;

        float bottom = top + lineThickness;

        m_vertices.append(sf::Vertex(sf::Vector2f(0, top), m_color, sf::Vector2f(1, 1)));
        m_vertices.append(sf::Vertex(sf::Vector2f(x, top), m_color, sf::Vector2f(1, 1)));
        m_vertices.append(sf::Vertex(sf::Vector2f(x, bottom), m_color, sf::Vector2f(1, 1)));
        m_vertices.append(sf::Vertex(sf::Vector2f(0, bottom), m_color, sf::Vector2f(1, 1)));
    }
 
Oh well. It's too late now. I will make a improved patch tomorrow.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Fix for issue #243
« Reply #2 on: February 13, 2013, 11:14:49 pm »
It's not optimized, it's wrong ;) It fails if you have both styles applied. Your first version was the correct one.

I prefer pull requests than patches that I can't apply directly. It's really easy: fork the repository on github (there's a "fork" button on the SFML repository), do your modifications in your forked repository, and then create a pull request (there's also a button for it).
Laurent Gomila - SFML developer

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Fix for issue #243
« Reply #3 on: February 14, 2013, 08:45:26 am »
Haha see that's what I meant. It was to late to write any good code...  ;D
Ok I'll try to make a pull request. Although I have worked with git/github before I am really shitty at it. But I'll see if I can get it to work.

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Fix for issue #243
« Reply #4 on: February 14, 2013, 07:06:59 pm »
Ok I made a pull request. I hope I got everything right :D

Edit: oh shit i messed something up… I'll try to fix it.
« Last Edit: February 14, 2013, 07:32:33 pm by Foaly »

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Fix for issue #243
« Reply #5 on: February 17, 2013, 06:17:47 pm »
I clean up the commit and uplaoded it again.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Fix for issue #243
« Reply #6 on: February 17, 2013, 07:13:05 pm »
Thanks for the efforts :)

I may not review it right now, but don't worry it won't be ignored.
Laurent Gomila - SFML developer

 

anything