Update of /cvsroot/super-tux/supertux/lib/video
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16591/lib/video
Modified Files:
font.cpp font.h
Log Message:
Added support for break lines for get_text_width() - returns the size of the largest paragraph.
Also added a get_text_height() that also supports break lines - returns the size of all the paragraphs.
Index: font.cpp
===================================================================
RCS file: /cvsroot/super-tux/supertux/lib/video/font.cpp,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- font.cpp 28 Jul 2004 14:57:24 -0000 1.5
+++ font.cpp 29 Jul 2004 11:24:41 -0000 1.6
@@ -71,15 +71,46 @@
}
float
-Font::get_height() const
+Font::get_text_width(const std::string& text) const
{
- return h;
+ /** Let's calculate the size of the biggest paragraph */
+ int l, hl;
+ hl = 0; l = -1;
+ while(true)
+ {
+ l = text.find("\n", l+1);
+ if(l == (int)std::string::npos)
+ break;
+ if(hl < l)
+ hl = l;
+ }
+ if(hl == 0)
+ hl = text.size();
+
+ return hl * w;
}
float
-Font::get_text_width(const std::string& text) const
+Font::get_text_height(const std::string& text) const
{
- return text.size() * w;
+ /** Let's calculate height of the text */
+ int l, hh;
+ hh = h; l = -1;
+ while(true)
+ {
+ l = text.find("\n", l+1);
+ if(l == (int)std::string::npos)
+ break;
+ hh += h + 2;
+ }
+
+ return hh;
+}
+
+float
+Font::get_height() const
+{
+ return h;
}
void
Index: font.h
===================================================================
RCS file: /cvsroot/super-tux/supertux/lib/video/font.h,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- font.h 28 Jul 2004 14:57:24 -0000 1.6
+++ font.h 29 Jul 2004 11:24:41 -0000 1.7
@@ -42,14 +42,24 @@
Font(const std::string& file, FontType type, int w, int h, int shadowsize=2);
~Font();
- /// returns the height of the font.
- float get_height() const;
/** returns the width of a given text. (Note that I won't add a normal
* get_width function here, as we might switch to variable width fonts in the
- * future.
+ * future.)
+ * Supports breaklines.
*/
float get_text_width(const std::string& text) const;
+ /** returns the height of a given text. (Note that I won't add a normal
+ * get_width function here, as we might switch to variable width fonts in the
+ * future.)
+ * Supports breaklines.
+ * In case, you are positive that your text doesn't use break lines, you can
+ * just use get_height().
+ */
+ float get_text_height(const std::string& text) const;
+ /// returns the height of the font.
+ float get_height() const;
+
private:
friend class DrawingContext;
|