aboutsummaryrefslogtreecommitdiff
path: root/engines/draci/font.cpp
diff options
context:
space:
mode:
authorDenis Kasak2009-08-04 19:23:59 +0000
committerDenis Kasak2009-08-04 19:23:59 +0000
commit71dbb75031622432f94c38c4024c4253033d3747 (patch)
tree7d180f5a94b902de6e5eef7d576ac58a133be35e /engines/draci/font.cpp
parentd3412ea3a433cee5dce9d520142110eea5713ea7 (diff)
downloadscummvm-rg350-71dbb75031622432f94c38c4024c4253033d3747.tar.gz
scummvm-rg350-71dbb75031622432f94c38c4024c4253033d3747.tar.bz2
scummvm-rg350-71dbb75031622432f94c38c4024c4253033d3747.zip
* Added Font::getLineWidth()
* Changed Font::getStringWidth() and Font::getStringHeight() to return uint instead of int. * Made the Font::drawString() overload which accepts a Common::String the "default" one. The overload accepting a (byte *) now calls that one (it was the other way around before). * Added proper line centering to the Font::drawString() routine. svn-id: r43053
Diffstat (limited to 'engines/draci/font.cpp')
-rw-r--r--engines/draci/font.cpp71
1 files changed, 48 insertions, 23 deletions
diff --git a/engines/draci/font.cpp b/engines/draci/font.cpp
index 4902965aa3..2a9185e725 100644
--- a/engines/draci/font.cpp
+++ b/engines/draci/font.cpp
@@ -209,20 +209,37 @@ void Font::drawChar(Surface *dst, uint8 chr, int tx, int ty, bool markDirty) con
void Font::drawString(Surface *dst, const byte *str, uint len,
int x, int y, int spacing, bool markDirty) const {
+ drawString(dst, Common::String((const char *)str, len), x, y, spacing, markDirty);
+}
+
+/**
+ * @brief Draw a string to a Draci::Surface
+ *
+ * @param dst Pointer to the destination surface
+ * @param str String to draw
+ * @param x Horizontal offset on the surface
+ * @param y Vertical offset on the surface
+ * @param spacing Space to leave between individual characters. Defaults to 0.
+ */
+
+void Font::drawString(Surface *dst, const Common::String &str,
+ int x, int y, int spacing, bool markDirty) const {
assert(dst != NULL);
assert(x >= 0);
assert(y >= 0);
- int curx = x;
- int cury = y;
+ uint widest = getStringWidth(str, spacing);
- for (unsigned int i = 0; i < len; ++i) {
+ int curx = x + (widest - getLineWidth(str, 0, spacing)) / 2;
+ int cury = y;
+
+ for (uint i = 0; i < str.size(); ++i) {
// If we encounter the '|' char (newline and end of string marker),
// skip it and go to the start of the next line
if (str[i] == '|') {
cury += getFontHeight();
- curx = x;
+ curx = x + (widest - getLineWidth(str, i+1, spacing) - 1) / 2;
continue;
}
@@ -237,22 +254,6 @@ void Font::drawString(Surface *dst, const byte *str, uint len,
}
/**
- * @brief Draw a string to a Draci::Surface
- *
- * @param dst Pointer to the destination surface
- * @param str String to draw
- * @param x Horizontal offset on the surface
- * @param y Vertical offset on the surface
- * @param spacing Space to leave between individual characters. Defaults to 0.
- */
-
-void Font::drawString(Surface *dst, const Common::String &str,
- int x, int y, int spacing, bool markDirty) const {
-
- drawString(dst, (byte *) str.c_str(), str.size(), x, y, spacing, markDirty);
-}
-
-/**
* @brief Calculate the width of a string when drawn in the current font
*
* @param str String to draw
@@ -261,7 +262,7 @@ void Font::drawString(Surface *dst, const Common::String &str,
* @return The calculated width of the string
*/
-int Font::getStringWidth(const Common::String &str, int spacing) const {
+uint Font::getStringWidth(const Common::String &str, int spacing) const {
unsigned int width = 0;
// Real length, including '|' separators
@@ -290,6 +291,30 @@ int Font::getStringWidth(const Common::String &str, int spacing) const {
return width + 1;
}
+uint Font::getLineWidth(const Common::String &str, uint startIndex, int spacing) const {
+
+ uint width = 0;
+
+ // If the index is greater or equal to the string size,
+ // the width of the line is 0
+ if (startIndex >= str.size())
+ return 0;
+
+ for (uint i = startIndex; i < str.size(); ++i) {
+
+ // EOL encountered
+ if (str[i] == '|')
+ break;
+
+ // Add width of the current char
+ uint8 charIndex = str[i] - kCharIndexOffset;
+ width += _charWidths[charIndex];
+ width += spacing;
+ }
+
+ return width;
+}
+
/**
* @brief Calculate the height of a string by counting the number of '|' chars (which
* are used as newline characters and end-of-string markers)
@@ -301,7 +326,7 @@ int Font::getStringWidth(const Common::String &str, int spacing) const {
*/
-int Font::getStringHeight(const Common::String &str) const {
+uint Font::getStringHeight(const Common::String &str) const {
uint len = str.size();
int separators = 0;
@@ -314,6 +339,6 @@ int Font::getStringHeight(const Common::String &str) const {
}
return separators * getFontHeight();
-}
+}
} // End of namespace Draci