ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

font.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "graphics/font.h"
00024 #include "graphics/managed_surface.h"
00025 
00026 #include "common/array.h"
00027 #include "common/util.h"
00028 
00029 namespace Graphics {
00030 
00031 int Font::getKerningOffset(uint32 left, uint32 right) const {
00032     return 0;
00033 }
00034 
00035 Common::Rect Font::getBoundingBox(uint32 chr) const {
00036     return Common::Rect(getCharWidth(chr), getFontHeight());
00037 }
00038 
00039 namespace {
00040 
00041 template<class StringType>
00042 Common::Rect getBoundingBoxImpl(const Font &font, const StringType &str, int x, int y, int w, TextAlign align, int deltax) {
00043     // We follow the logic of drawStringImpl here. The only exception is
00044     // that we do allow an empty width to be specified here. This allows us
00045     // to obtain the complete bounding box of a string.
00046     const int leftX = x, rightX = w ? (x + w) : 0x7FFFFFFF;
00047     int width = font.getStringWidth(str);
00048 
00049     if (align == kTextAlignCenter)
00050         x = x + (w - width)/2;
00051     else if (align == kTextAlignRight)
00052         x = x + w - width;
00053     x += deltax;
00054 
00055     bool first = true;
00056     Common::Rect bbox;
00057 
00058     typename StringType::unsigned_type last = 0;
00059     for (typename StringType::const_iterator i = str.begin(), end = str.end(); i != end; ++i) {
00060         const typename StringType::unsigned_type cur = *i;
00061         x += font.getKerningOffset(last, cur);
00062         last = cur;
00063 
00064         Common::Rect charBox = font.getBoundingBox(cur);
00065         if (x + charBox.right > rightX)
00066             break;
00067         if (x + charBox.right >= leftX) {
00068             charBox.translate(x, y);
00069             if (first) {
00070                 bbox = charBox;
00071                 first = false;
00072             } else {
00073                 bbox.extend(charBox);
00074             }
00075         }
00076 
00077         x += font.getCharWidth(cur);
00078     }
00079 
00080     return bbox;
00081 }
00082 
00083 template<class StringType>
00084 int getStringWidthImpl(const Font &font, const StringType &str) {
00085     int space = 0;
00086     typename StringType::unsigned_type last = 0;
00087 
00088     for (uint i = 0; i < str.size(); ++i) {
00089         const typename StringType::unsigned_type cur = str[i];
00090         space += font.getCharWidth(cur) + font.getKerningOffset(last, cur);
00091         last = cur;
00092     }
00093 
00094     return space;
00095 }
00096 
00097 template<class StringType>
00098 void drawStringImpl(const Font &font, Surface *dst, const StringType &str, int x, int y, int w, uint32 color, TextAlign align, int deltax) {
00099     // The logic in getBoundingImpl is the same as we use here. In case we
00100     // ever change something here we will need to change it there too.
00101     assert(dst != 0);
00102 
00103     const int leftX = x, rightX = x + w;
00104     int width = font.getStringWidth(str);
00105 
00106     if (align == kTextAlignCenter)
00107         x = x + (w - width)/2;
00108     else if (align == kTextAlignRight)
00109         x = x + w - width;
00110     x += deltax;
00111 
00112     typename StringType::unsigned_type last = 0;
00113     for (typename StringType::const_iterator i = str.begin(), end = str.end(); i != end; ++i) {
00114         const typename StringType::unsigned_type cur = *i;
00115         x += font.getKerningOffset(last, cur);
00116         last = cur;
00117 
00118         Common::Rect charBox = font.getBoundingBox(cur);
00119         if (x + charBox.right > rightX)
00120             break;
00121         if (x + charBox.right >= leftX)
00122             font.drawChar(dst, cur, x, y, color);
00123 
00124         x += font.getCharWidth(cur);
00125     }
00126 }
00127 
00128 template<class StringType>
00129 struct WordWrapper {
00130     Common::Array<StringType> &lines;
00131     int actualMaxLineWidth;
00132 
00133     WordWrapper(Common::Array<StringType> &l) : lines(l), actualMaxLineWidth(0) {
00134     }
00135 
00136     void add(StringType &line, int &w) {
00137         if (actualMaxLineWidth < w)
00138             actualMaxLineWidth = w;
00139 
00140         lines.push_back(line);
00141 
00142         line.clear();
00143         w = 0;
00144     }
00145 
00146     void clear() {
00147         lines.clear();
00148         actualMaxLineWidth = 0;
00149     }
00150 };
00151 
00152 template<class StringType>
00153 int wordWrapTextImpl(const Font &font, const StringType &str, int maxWidth, Common::Array<StringType> &lines, int initWidth, bool evenWidthLinesModeEnabled, bool wrapOnExplicitNewLines) {
00154     WordWrapper<StringType> wrapper(lines);
00155     StringType line;
00156     StringType tmpStr;
00157     int lineWidth = initWidth;
00158     int tmpWidth = 0;
00159     int fullTextWidthEWL = initWidth; // this replaces new line characters (if any) with single spaces - it is used in Even Width Lines mode
00160 
00161     // The rough idea behind this algorithm is as follows:
00162     // We accumulate characters into the string tmpStr. Whenever a full word
00163     // has been gathered together this way, we 'commit' it to the line buffer
00164     // 'line', i.e. we add tmpStr to the end of line, then clear it. Before
00165     // we do that, we check whether it would cause 'line' to exceed maxWidth;
00166     // in that case, we first add line to lines, then reset it.
00167     //
00168     // If a newline character is read, then we also add line to lines and clear it.
00169     //
00170     // Special care has to be taken to account for 'words' that exceed the width
00171     // of a line. If we encounter such a word, we have to wrap it over multiple
00172     // lines.
00173 
00174     typename StringType::unsigned_type last = 0;
00175 
00176     // When EvenWidthLines mode is enabled then we require an early loop over the entire string
00177     // in order to get the full width of the text
00178     //
00179     // "Wrap On Explicit New Lines" and "Even Width Lines" modes are mutually exclusive,
00180     // If both are set to true and there are new line characters in the text,
00181     // then "Even Width Lines" mode is disabled.
00182     //
00183     if (evenWidthLinesModeEnabled) {
00184         // Early loop to get the full width of the text
00185         for (typename StringType::const_iterator x = str.begin(); x != str.end(); ++x) {
00186             typename StringType::unsigned_type c = *x;
00187 
00188             // Check for Windows and Mac line breaks
00189             if (c == '\r') {
00190                 if (x != str.end() && *(x + 1) == '\n') {
00191                     ++x;
00192                 }
00193                 c = '\n';
00194             }
00195 
00196             if (c == '\n') {
00197                 if (!wrapOnExplicitNewLines) {
00198                     c = ' ';
00199                 } else {
00200                     evenWidthLinesModeEnabled = false;
00201                     break;
00202                 }
00203             }
00204 
00205             const int w = font.getCharWidth(c) + font.getKerningOffset(last, c);
00206             last = c;
00207             fullTextWidthEWL += w;
00208         }
00209     }
00210 
00211     int targetTotalLinesNumberEWL = 0;
00212     int targetMaxLineWidth = 0;
00213     do {
00214         if (evenWidthLinesModeEnabled) {
00215             wrapper.clear();
00216             targetTotalLinesNumberEWL += 1;
00217             // We add +2 to the fullTextWidthEWL to account for possible shadow pixels
00218             // We add +10 * font.getCharWidth(' ') to the quotient since we want to allow some extra margin (about an extra wprd's length)
00219             // since that yields better looking results
00220             targetMaxLineWidth = ((fullTextWidthEWL + 2) / targetTotalLinesNumberEWL) + 10 * font.getCharWidth(' ');
00221             if (targetMaxLineWidth > maxWidth) {
00222                 // repeat the loop with increased targetTotalLinesNumberEWL
00223                 continue;
00224             }
00225         } else {
00226             targetMaxLineWidth = maxWidth;
00227         }
00228 
00229         last = 0;
00230         tmpWidth = 0;
00231 
00232         for (typename StringType::const_iterator x = str.begin(); x != str.end(); ++x) {
00233             typename StringType::unsigned_type c = *x;
00234 
00235             // Convert Windows and Mac line breaks into plain \n
00236             if (c == '\r') {
00237                 if (x != str.end() && *(x + 1) == '\n') {
00238                     ++x;
00239                 }
00240                 c = '\n';
00241             }
00242             // if wrapping on explicit new lines is disabled, then new line characters should be treated as a single white space char
00243             if (!wrapOnExplicitNewLines && c == '\n')  {
00244                 c = ' ';
00245             }
00246 
00247             const int currentCharWidth = font.getCharWidth(c);
00248             const int w = currentCharWidth + font.getKerningOffset(last, c);
00249             last = c;
00250             const bool wouldExceedWidth = (lineWidth + tmpWidth + w > targetMaxLineWidth);
00251 
00252             // If this char is a whitespace, then it represents a potential
00253             // 'wrap point' where wrapping could take place. Everything that
00254             // came before it can now safely be added to the line, as we know
00255             // that it will not have to be wrapped.
00256             if (Common::isSpace(c)) {
00257                 line += tmpStr;
00258                 lineWidth += tmpWidth;
00259 
00260                 tmpStr.clear();
00261                 tmpWidth = 0;
00262 
00263                 // If we encounter a line break (\n), or if the new space would
00264                 // cause the line to overflow: start a new line
00265                 if ((wrapOnExplicitNewLines && c == '\n') || wouldExceedWidth) {
00266                     wrapper.add(line, lineWidth);
00267                     continue;
00268                 }
00269             }
00270 
00271             // If the max line width would be exceeded by adding this char,
00272             // insert a line break.
00273             if (wouldExceedWidth) {
00274                 // Commit what we have so far, *if* we have anything.
00275                 // If line is empty, then we are looking at a word
00276                 // which exceeds the maximum line width.
00277                 if (lineWidth > 0) {
00278                     wrapper.add(line, lineWidth);
00279                     // Trim left side
00280                     while (tmpStr.size() && Common::isSpace(tmpStr[0])) {
00281                         tmpStr.deleteChar(0);
00282                         // This is not very fast, but it is the simplest way to
00283                         // assure we do not mess something up because of kerning.
00284                         tmpWidth = font.getStringWidth(tmpStr);
00285                     }
00286 
00287                     if (tmpStr.empty()) {
00288                         // If tmpStr is empty, we might have removed the space before 'c'.
00289                         // That means we have to recompute the kerning.
00290 
00291                         tmpWidth += currentCharWidth + font.getKerningOffset(0, c);
00292                         tmpStr += c;
00293                         continue;
00294                     }
00295                 } else {
00296                     wrapper.add(tmpStr, tmpWidth);
00297                 }
00298             }
00299 
00300             tmpWidth += w;
00301             tmpStr += c;
00302         }
00303 
00304         // If some text is left over, add it as the final line
00305         line += tmpStr;
00306         lineWidth += tmpWidth;
00307         if (lineWidth > 0) {
00308             wrapper.add(line, lineWidth);
00309         }
00310     } while (evenWidthLinesModeEnabled
00311              && (targetMaxLineWidth > maxWidth));
00312     return wrapper.actualMaxLineWidth;
00313 }
00314 
00315 } // End of anonymous namespace
00316 
00317 Common::Rect Font::getBoundingBox(const Common::String &input, int x, int y, const int w, TextAlign align, int deltax, bool useEllipsis) const {
00318     // In case no width was given we cannot use ellipsis or any alignment
00319     // apart from left alignment.
00320     if (w == 0) {
00321         if (useEllipsis) {
00322             warning("Font::getBoundingBox: Requested ellipsis when no width was specified");
00323         }
00324 
00325         if (align != kTextAlignLeft) {
00326             warning("Font::getBoundingBox: Requested text alignment when no width was specified");
00327         }
00328 
00329         useEllipsis = false;
00330         align = kTextAlignLeft;
00331     }
00332 
00333     const Common::String str = useEllipsis ? handleEllipsis(input, w) : input;
00334     return getBoundingBoxImpl(*this, str, x, y, w, align, deltax);
00335 }
00336 
00337 Common::Rect Font::getBoundingBox(const Common::U32String &str, int x, int y, const int w, TextAlign align) const {
00338     // In case no width was given we cannot any alignment apart from left
00339     // alignment.
00340     if (w == 0) {
00341         if (align != kTextAlignLeft) {
00342             warning("Font::getBoundingBox: Requested text alignment when no width was specified");
00343         }
00344 
00345         align = kTextAlignLeft;
00346     }
00347 
00348     return getBoundingBoxImpl(*this, str, x, y, w, align, 0);
00349 }
00350 
00351 int Font::getStringWidth(const Common::String &str) const {
00352     return getStringWidthImpl(*this, str);
00353 }
00354 
00355 int Font::getStringWidth(const Common::U32String &str) const {
00356     return getStringWidthImpl(*this, str);
00357 }
00358 
00359 void Font::drawChar(ManagedSurface *dst, uint32 chr, int x, int y, uint32 color) const {
00360     drawChar(&dst->_innerSurface, chr, x, y, color);
00361 
00362     Common::Rect charBox = getBoundingBox(chr);
00363     charBox.translate(x, y);
00364     dst->addDirtyRect(charBox);
00365 }
00366 
00367 void Font::drawString(Surface *dst, const Common::String &str, int x, int y, int w, uint32 color, TextAlign align, int deltax, bool useEllipsis) const {
00368     Common::String renderStr = useEllipsis ? handleEllipsis(str, w) : str;
00369     drawStringImpl(*this, dst, renderStr, x, y, w, color, align, deltax);
00370 }
00371 
00372 void Font::drawString(Surface *dst, const Common::U32String &str, int x, int y, int w, uint32 color, TextAlign align, int deltax) const {
00373     drawStringImpl(*this, dst, str, x, y, w, color, align, deltax);
00374 }
00375 
00376 void Font::drawString(ManagedSurface *dst, const Common::String &str, int x, int y, int w, uint32 color, TextAlign align, int deltax, bool useEllipsis) const {
00377     drawString(&dst->_innerSurface, str, x, y, w, color, align, deltax, useEllipsis);
00378     if (w != 0) {
00379         dst->addDirtyRect(getBoundingBox(str, x, y, w, align, deltax, useEllipsis));
00380     }
00381 }
00382 
00383 void Font::drawString(ManagedSurface *dst, const Common::U32String &str, int x, int y, int w, uint32 color, TextAlign align, int deltax) const {
00384     drawString(&dst->_innerSurface, str, x, y, w, color, align, deltax);
00385     if (w != 0) {
00386         dst->addDirtyRect(getBoundingBox(str, x, y, w, align));
00387     }
00388 }
00389 
00390 int Font::wordWrapText(const Common::String &str, int maxWidth, Common::Array<Common::String> &lines, int initWidth, bool evenWidthLinesModeEnabled, bool wrapOnExplicitNewLines) const {
00391     return wordWrapTextImpl(*this, str, maxWidth, lines, initWidth, evenWidthLinesModeEnabled, wrapOnExplicitNewLines);
00392 }
00393 
00394 int Font::wordWrapText(const Common::U32String &str, int maxWidth, Common::Array<Common::U32String> &lines, int initWidth, bool evenWidthLinesModeEnabled, bool wrapOnExplicitNewLines) const {
00395     return wordWrapTextImpl(*this, str, maxWidth, lines, initWidth, evenWidthLinesModeEnabled, wrapOnExplicitNewLines);
00396 }
00397 
00398 Common::String Font::handleEllipsis(const Common::String &input, int w) const {
00399     Common::String s = input;
00400     int width = getStringWidth(s);
00401 
00402     if (width > w && s.hasSuffix("...")) {
00403         // String is too wide. Check whether it ends in an ellipsis
00404         // ("..."). If so, remove that and try again!
00405         s.deleteLastChar();
00406         s.deleteLastChar();
00407         s.deleteLastChar();
00408         width = getStringWidth(s);
00409     }
00410 
00411     if (width > w) {
00412         Common::String str;
00413 
00414         // String is too wide. So we shorten it "intelligently" by
00415         // replacing parts of the string by an ellipsis. There are
00416         // three possibilities for this: replace the start, the end, or
00417         // the middle of the string. What is best really depends on the
00418         // context; but unless we want to make this configurable,
00419         // replacing the middle seems to be a good compromise.
00420 
00421         const int ellipsisWidth = getStringWidth("...");
00422 
00423         // SLOW algorithm to remove enough of the middle. But it is good enough
00424         // for now.
00425         const int halfWidth = (w - ellipsisWidth) / 2;
00426         int w2 = 0;
00427         Common::String::unsigned_type last = 0;
00428         uint i = 0;
00429 
00430         for (; i < s.size(); ++i) {
00431             const Common::String::unsigned_type cur = s[i];
00432             int charWidth = getCharWidth(cur) + getKerningOffset(last, cur);
00433             if (w2 + charWidth > halfWidth)
00434                 break;
00435             last = cur;
00436             w2 += charWidth;
00437             str += cur;
00438         }
00439 
00440         // At this point we know that the first 'i' chars are together 'w2'
00441         // pixels wide. We took the first i-1, and add "..." to them.
00442         str += "...";
00443         last = '.';
00444 
00445         // The original string is width wide. Of those we already skipped past
00446         // w2 pixels, which means (width - w2) remain.
00447         // The new str is (w2+ellipsisWidth) wide, so we can accommodate about
00448         // (w - (w2+ellipsisWidth)) more pixels.
00449         // Thus we skip ((width - w2) - (w - (w2+ellipsisWidth))) =
00450         // (width + ellipsisWidth - w)
00451         int skip = width + ellipsisWidth - w;
00452         for (; i < s.size() && skip > 0; ++i) {
00453             const Common::String::unsigned_type cur = s[i];
00454             skip -= getCharWidth(cur) + getKerningOffset(last, cur);
00455             last = cur;
00456         }
00457 
00458         // Append the remaining chars, if any
00459         for (; i < s.size(); ++i) {
00460             str += s[i];
00461         }
00462 
00463         return str;
00464     } else {
00465         return s;
00466     }
00467 }
00468 
00469 } // End of namespace Graphics


Generated on Sat May 30 2020 05:00:45 for ResidualVM by doxygen 1.7.1
curved edge   curved edge