2004-03-13 13:03:25 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
2006-01-18 17:39:49 +00:00
|
|
|
* Copyright (C) 2002-2006 The ScummVM project
|
2004-03-13 13:03:25 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2005-10-18 01:30:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2004-03-13 13:03:25 +00:00
|
|
|
*
|
|
|
|
* $Header$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common/stdafx.h"
|
2004-03-21 21:20:25 +00:00
|
|
|
#include "graphics/font.h"
|
2003-11-19 23:46:39 +00:00
|
|
|
|
2004-03-21 21:20:25 +00:00
|
|
|
namespace Graphics {
|
2003-11-19 23:46:39 +00:00
|
|
|
|
2004-03-13 13:03:25 +00:00
|
|
|
int NewFont::getCharWidth(byte chr) const {
|
|
|
|
// If no width table is specified, return the maximum width
|
2004-08-15 14:05:28 +00:00
|
|
|
if (!desc.width)
|
|
|
|
return desc.maxwidth;
|
2004-03-13 13:03:25 +00:00
|
|
|
// If this character is not included in the font, use the default char.
|
2004-08-15 14:05:28 +00:00
|
|
|
if (chr < desc.firstchar || desc.firstchar + desc.size < chr) {
|
|
|
|
chr = desc.defaultchar;
|
2004-03-13 13:03:25 +00:00
|
|
|
}
|
2004-08-15 14:05:28 +00:00
|
|
|
return desc.width[chr - desc.firstchar];
|
2004-03-13 13:03:25 +00:00
|
|
|
}
|
|
|
|
|
2005-05-02 18:00:05 +00:00
|
|
|
void NewFont::drawChar(Surface *dst, byte chr, int tx, int ty, uint32 color) const {
|
2004-03-13 13:03:25 +00:00
|
|
|
assert(dst != 0);
|
2004-11-27 00:55:48 +00:00
|
|
|
byte *ptr = (byte *)dst->getBasePtr(tx, ty);
|
2004-03-13 13:03:25 +00:00
|
|
|
|
2006-01-31 03:11:40 +00:00
|
|
|
assert(desc.bits != 0 && desc.maxwidth <= 17);
|
2004-03-13 13:03:25 +00:00
|
|
|
assert(dst->bytesPerPixel == 1 || dst->bytesPerPixel == 2);
|
|
|
|
|
|
|
|
// If this character is not included in the font, use the default char.
|
2004-08-15 14:05:28 +00:00
|
|
|
if (chr < desc.firstchar || chr >= desc.firstchar + desc.size) {
|
|
|
|
chr = desc.defaultchar;
|
2004-03-13 13:03:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const int w = getCharWidth(chr);
|
2004-08-15 14:05:28 +00:00
|
|
|
chr -= desc.firstchar;
|
|
|
|
const bitmap_t *tmp = desc.bits + (desc.offset ? desc.offset[chr] : (chr * desc.height));
|
2004-03-13 13:03:25 +00:00
|
|
|
|
2005-01-06 22:48:42 +00:00
|
|
|
for (int y = 0; y < desc.height; y++, ptr += dst->pitch) {
|
2005-01-08 18:11:29 +00:00
|
|
|
const bitmap_t buffer = *tmp++;
|
2004-03-13 13:03:25 +00:00
|
|
|
bitmap_t mask = 0x8000;
|
2004-11-27 00:55:48 +00:00
|
|
|
if (ty + y < 0 || ty + y >= dst->h)
|
|
|
|
continue;
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-01-08 18:11:29 +00:00
|
|
|
for (int x = 0; x < w; x++, mask >>= 1) {
|
2004-11-27 00:55:48 +00:00
|
|
|
if (tx + x < 0 || tx + x >= dst->w)
|
|
|
|
continue;
|
2005-01-08 18:11:29 +00:00
|
|
|
if ((buffer & mask) != 0) {
|
2004-03-13 13:03:25 +00:00
|
|
|
if (dst->bytesPerPixel == 1)
|
|
|
|
ptr[x] = color;
|
|
|
|
else if (dst->bytesPerPixel == 2)
|
|
|
|
((uint16 *)ptr)[x] = color;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-15 18:44:14 +00:00
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
|
2004-03-13 13:03:25 +00:00
|
|
|
int Font::getStringWidth(const Common::String &str) const {
|
|
|
|
int space = 0;
|
|
|
|
|
|
|
|
for (uint i = 0; i < str.size(); ++i)
|
|
|
|
space += getCharWidth(str[i]);
|
|
|
|
return space;
|
|
|
|
}
|
|
|
|
|
2005-05-02 18:00:05 +00:00
|
|
|
void Font::drawString(Surface *dst, const Common::String &s, int x, int y, int w, uint32 color, TextAlignment align, int deltax, bool useEllipsis) const {
|
2004-03-13 13:03:25 +00:00
|
|
|
assert(dst != 0);
|
|
|
|
const int leftX = x, rightX = x + w;
|
|
|
|
uint i;
|
|
|
|
int width = getStringWidth(s);
|
|
|
|
Common::String str;
|
2005-01-06 19:09:34 +00:00
|
|
|
|
2004-03-13 13:03:25 +00:00
|
|
|
if (useEllipsis && width > w) {
|
|
|
|
// String is too wide. So we shorten it "intellegently", by replacing
|
|
|
|
// parts of it by an ellipsis ("..."). There are three possibilities
|
|
|
|
// for this: replace the start, the end, or the middle of the string.
|
|
|
|
// What is best really depends on the context; but unless we want to
|
|
|
|
// make this configurable, replacing the middle probably is a good
|
|
|
|
// compromise.
|
|
|
|
const int ellipsisWidth = getStringWidth("...");
|
2005-01-06 19:09:34 +00:00
|
|
|
|
2004-03-13 13:03:25 +00:00
|
|
|
// SLOW algorithm to remove enough of the middle. But it is good enough
|
|
|
|
// for now.
|
|
|
|
const int halfWidth = (w - ellipsisWidth) / 2;
|
|
|
|
int w2 = 0;
|
2005-01-06 19:09:34 +00:00
|
|
|
|
2004-03-13 13:03:25 +00:00
|
|
|
for (i = 0; i < s.size(); ++i) {
|
|
|
|
int charWidth = getCharWidth(s[i]);
|
|
|
|
if (w2 + charWidth > halfWidth)
|
|
|
|
break;
|
|
|
|
w2 += charWidth;
|
|
|
|
str += s[i];
|
|
|
|
}
|
|
|
|
// At this point we know that the first 'i' chars are together 'w2'
|
|
|
|
// pixels wide. We took the first i-1, and add "..." to them.
|
|
|
|
str += "...";
|
2005-01-06 19:09:34 +00:00
|
|
|
|
2004-03-13 13:03:25 +00:00
|
|
|
// The original string is width wide. Of those we already skipped past
|
|
|
|
// w2 pixels, which means (width - w2) remain.
|
|
|
|
// The new str is (w2+ellipsisWidth) wide, so we can accomodate about
|
|
|
|
// (w - (w2+ellipsisWidth)) more pixels.
|
|
|
|
// Thus we skip ((width - w2) - (w - (w2+ellipsisWidth))) =
|
|
|
|
// (width + ellipsisWidth - w)
|
|
|
|
int skip = width + ellipsisWidth - w;
|
|
|
|
for (; i < s.size() && skip > 0; ++i) {
|
|
|
|
skip -= getCharWidth(s[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append the remaining chars, if any
|
|
|
|
for (; i < s.size(); ++i) {
|
|
|
|
str += s[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
width = getStringWidth(str);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
str = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (align == kTextAlignCenter)
|
|
|
|
x = x + (w - width - 1)/2;
|
|
|
|
else if (align == kTextAlignRight)
|
|
|
|
x = x + w - width;
|
|
|
|
x += deltax;
|
|
|
|
|
|
|
|
for (i = 0; i < str.size(); ++i) {
|
|
|
|
w = getCharWidth(str[i]);
|
|
|
|
if (x+w > rightX)
|
|
|
|
break;
|
|
|
|
if (x >= leftX)
|
2005-01-06 22:48:42 +00:00
|
|
|
drawChar(dst, str[i], x, y, color);
|
2004-03-13 13:03:25 +00:00
|
|
|
x += w;
|
|
|
|
}
|
|
|
|
}
|
2003-11-19 23:46:39 +00:00
|
|
|
|
|
|
|
|
2005-05-15 16:13:52 +00:00
|
|
|
struct WordWrapper {
|
|
|
|
Common::StringList &lines;
|
|
|
|
int actualMaxLineWidth;
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-05-15 16:13:52 +00:00
|
|
|
WordWrapper(Common::StringList &l) : lines(l), actualMaxLineWidth(0) {
|
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-05-15 16:13:52 +00:00
|
|
|
void add(Common::String &line, int &w) {
|
|
|
|
if (actualMaxLineWidth < w)
|
|
|
|
actualMaxLineWidth = w;
|
|
|
|
|
|
|
|
lines.push_back(line);
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-05-15 16:13:52 +00:00
|
|
|
line.clear();
|
|
|
|
w = 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int Font::wordWrapText(const Common::String &str, int maxWidth, Common::StringList &lines) const {
|
|
|
|
WordWrapper wrapper(lines);
|
|
|
|
Common::String line;
|
|
|
|
Common::String tmpStr;
|
|
|
|
int lineWidth = 0;
|
|
|
|
int tmpWidth = 0;
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-05-15 16:13:52 +00:00
|
|
|
// The rough idea behind this algorithm is as follows:
|
|
|
|
// We accumulate characters into the string tmpStr. Whenever a full word
|
|
|
|
// has been gathered together this way, we 'commit' it to the line buffer
|
|
|
|
// 'line', i.e. we add tmpStr to the end of line, then clear it. Before
|
|
|
|
// we do that, we check whether it would cause 'line' to exceed maxWidth;
|
|
|
|
// in that case, we first add line to lines, then reset it.
|
|
|
|
//
|
|
|
|
// If a newline character is read, then we also add line to lines and clear it.
|
|
|
|
//
|
|
|
|
// Special care has to be taken to account for 'words' that exceed the width
|
|
|
|
// of a line. If we encounter such a word, we have to wrap it over multiple
|
|
|
|
// lines.
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-05-15 16:13:52 +00:00
|
|
|
for (Common::String::const_iterator x = str.begin(); x != str.end(); ++x) {
|
|
|
|
const char c = *x;
|
|
|
|
const int w = getCharWidth(c);
|
2005-05-17 23:14:13 +00:00
|
|
|
const bool wouldExceedWidth = (lineWidth + tmpWidth + w > maxWidth);
|
2005-05-15 16:13:52 +00:00
|
|
|
|
|
|
|
// If this char is a whitespace, then it represents a potential
|
|
|
|
// 'wrap point' where wrapping could take place. Everything that
|
|
|
|
// came before it can now safely be added to the line, as we know
|
|
|
|
// that it will not have to be wrapped.
|
|
|
|
if (isspace(c)) {
|
|
|
|
line += tmpStr;
|
|
|
|
lineWidth += tmpWidth;
|
|
|
|
|
|
|
|
tmpStr.clear();
|
|
|
|
tmpWidth = 0;
|
|
|
|
|
2005-05-17 23:14:13 +00:00
|
|
|
// If we encounter a line break (\n), or if the new space would
|
|
|
|
// cause the line to overflow: start a new line
|
|
|
|
if (c == '\n' || wouldExceedWidth) {
|
|
|
|
wrapper.add(line, lineWidth);
|
|
|
|
continue;
|
|
|
|
}
|
2005-05-15 16:13:52 +00:00
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-05-15 16:13:52 +00:00
|
|
|
// If the max line width would be exceeded by adding this char,
|
|
|
|
// insert a line break.
|
2005-05-17 23:14:13 +00:00
|
|
|
if (wouldExceedWidth) {
|
2005-05-15 16:13:52 +00:00
|
|
|
// Commit what we have so far, *if* we have anything.
|
|
|
|
// If line is empty, then we are looking at a word
|
|
|
|
// which exceeds the maximum line width.
|
|
|
|
if (lineWidth > 0) {
|
|
|
|
wrapper.add(line, lineWidth);
|
2005-05-17 23:14:13 +00:00
|
|
|
// Trim left side
|
|
|
|
while (tmpStr.size() && isspace(tmpStr[0])) {
|
|
|
|
tmpWidth -= getCharWidth(tmpStr[0]);
|
|
|
|
tmpStr.deleteChar(0);
|
|
|
|
}
|
2005-05-15 16:13:52 +00:00
|
|
|
} else {
|
|
|
|
wrapper.add(tmpStr, tmpWidth);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-05-15 16:13:52 +00:00
|
|
|
tmpWidth += w;
|
|
|
|
tmpStr += c;
|
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-05-15 16:13:52 +00:00
|
|
|
// If some text is left over, add it as the final line
|
|
|
|
line += tmpStr;
|
|
|
|
lineWidth += tmpWidth;
|
|
|
|
if (lineWidth > 0) {
|
|
|
|
wrapper.add(line, lineWidth);
|
|
|
|
}
|
|
|
|
return wrapper.actualMaxLineWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-21 21:20:25 +00:00
|
|
|
} // End of namespace Graphics
|