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.