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

graphics/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 
00147 template<class StringType>
00148 int wordWrapTextImpl(const Font &font, const StringType &str, int maxWidth, Common::Array<StringType> &lines, int initWidth) {
00149     WordWrapper<StringType> wrapper(lines);
00150     StringType line;
00151     StringType tmpStr;
00152     int lineWidth = initWidth;
00153     int tmpWidth = 0;
00154 
00155     // The rough idea behind this algorithm is as follows:
00156     // We accumulate characters into the string tmpStr. Whenever a full word
00157     // has been gathered together this way, we 'commit' it to the line buffer
00158     // 'line', i.e. we add tmpStr to the end of line, then clear it. Before
00159     // we do that, we check whether it would cause 'line' to exceed maxWidth;
00160     // in that case, we first add line to lines, then reset it.
00161     //
00162     // If a newline character is read, then we also add line to lines and clear it.
00163     //
00164     // Special care has to be taken to account for 'words' that exceed the width
00165     // of a line. If we encounter such a word, we have to wrap it over multiple
00166     // lines.
00167 
00168     typename StringType::unsigned_type last = 0;
00169     for (typename StringType::const_iterator x = str.begin(); x != str.end(); ++x) {
00170         typename StringType::unsigned_type c = *x;
00171 
00172         // Convert Windows and Mac line breaks into plain \n
00173         if (c == '\r') {
00174             if (x != str.end() && *(x + 1) == '\n') {
00175                 ++x;
00176             }
00177             c = '\n';
00178         }
00179 
00180         const int currentCharWidth = font.getCharWidth(c);
00181         const int w = currentCharWidth + font.getKerningOffset(last, c);
00182         last = c;
00183         const bool wouldExceedWidth = (lineWidth + tmpWidth + w > maxWidth);
00184 
00185         // If this char is a whitespace, then it represents a potential
00186         // 'wrap point' where wrapping could take place. Everything that
00187         // came before it can now safely be added to the line, as we know
00188         // that it will not have to be wrapped.
00189         if (Common::isSpace(c)) {
00190             line += tmpStr;
00191             lineWidth += tmpWidth;
00192 
00193             tmpStr.clear();
00194             tmpWidth = 0;
00195 
00196             // If we encounter a line break (\n), or if the new space would
00197             // cause the line to overflow: start a new line
00198             if (c == '\n' || wouldExceedWidth) {
00199                 wrapper.add(line, lineWidth);
00200                 continue;
00201             }
00202         }
00203 
00204         // If the max line width would be exceeded by adding this char,
00205         // insert a line break.
00206         if (wouldExceedWidth) {
00207             // Commit what we have so far, *if* we have anything.
00208             // If line is empty, then we are looking at a word
00209             // which exceeds the maximum line width.
00210             if (lineWidth > 0) {
00211                 wrapper.add(line, lineWidth);
00212                 // Trim left side
00213                 while (tmpStr.size() && Common::isSpace(tmpStr[0])) {
00214                     tmpStr.deleteChar(0);
00215                     // This is not very fast, but it is the simplest way to
00216                     // assure we do not mess something up because of kerning.
00217                     tmpWidth = font.getStringWidth(tmpStr);
00218                 }
00219 
00220                 if (tmpStr.empty()) {
00221                     // If tmpStr is empty, we might have removed the space before 'c'.
00222                     // That means we have to recompute the kerning.
00223 
00224                     tmpWidth += currentCharWidth + font.getKerningOffset(0, c);
00225                     tmpStr += c;
00226                     continue;
00227                 }
00228             } else {
00229                 wrapper.add(tmpStr, tmpWidth);
00230             }
00231         }
00232 
00233         tmpWidth += w;
00234         tmpStr += c;
00235     }
00236 
00237     // If some text is left over, add it as the final line
00238     line += tmpStr;
00239     lineWidth += tmpWidth;
00240     if (lineWidth > 0) {
00241         wrapper.add(line, lineWidth);
00242     }
00243     return wrapper.actualMaxLineWidth;
00244 }
00245 
00246 } // End of anonymous namespace
00247 
00248 Common::Rect Font::getBoundingBox(const Common::String &input, int x, int y, const int w, TextAlign align, int deltax, bool useEllipsis) const {
00249     // In case no width was given we cannot use ellipsis or any alignment
00250     // apart from left alignment.
00251     if (w == 0) {
00252         if (useEllipsis) {
00253             warning("Font::getBoundingBox: Requested ellipsis when no width was specified");
00254         }
00255 
00256         if (align != kTextAlignLeft) {
00257             warning("Font::getBoundingBox: Requested text alignment when no width was specified");
00258         }
00259 
00260         useEllipsis = false;
00261         align = kTextAlignLeft;
00262     }
00263 
00264     const Common::String str = useEllipsis ? handleEllipsis(input, w) : input;
00265     return getBoundingBoxImpl(*this, str, x, y, w, align, deltax);
00266 }
00267 
00268 Common::Rect Font::getBoundingBox(const Common::U32String &str, int x, int y, const int w, TextAlign align) const {
00269     // In case no width was given we cannot any alignment apart from left
00270     // alignment.
00271     if (w == 0) {
00272         if (align != kTextAlignLeft) {
00273             warning("Font::getBoundingBox: Requested text alignment when no width was specified");
00274         }
00275 
00276         align = kTextAlignLeft;
00277     }
00278 
00279     return getBoundingBoxImpl(*this, str, x, y, w, align, 0);
00280 }
00281 
00282 int Font::getStringWidth(const Common::String &str) const {
00283     return getStringWidthImpl(*this, str);
00284 }
00285 
00286 int Font::getStringWidth(const Common::U32String &str) const {
00287     return getStringWidthImpl(*this, str);
00288 }
00289 
00290 void Font::drawChar(ManagedSurface *dst, uint32 chr, int x, int y, uint32 color) const {
00291     drawChar(&dst->_innerSurface, chr, x, y, color);
00292 
00293     Common::Rect charBox = getBoundingBox(chr);
00294     charBox.translate(x, y);
00295     dst->addDirtyRect(charBox);
00296 }
00297 
00298 void Font::drawString(Surface *dst, const Common::String &str, int x, int y, int w, uint32 color, TextAlign align, int deltax, bool useEllipsis) const {
00299     Common::String renderStr = useEllipsis ? handleEllipsis(str, w) : str;
00300     drawStringImpl(*this, dst, renderStr, x, y, w, color, align, deltax);
00301 }
00302 
00303 void Font::drawString(Surface *dst, const Common::U32String &str, int x, int y, int w, uint32 color, TextAlign align, int deltax) const {
00304     drawStringImpl(*this, dst, str, x, y, w, color, align, deltax);
00305 }
00306 
00307 void Font::drawString(ManagedSurface *dst, const Common::String &str, int x, int y, int w, uint32 color, TextAlign align, int deltax, bool useEllipsis) const {
00308     drawString(&dst->_innerSurface, str, x, y, w, color, align, deltax, useEllipsis);
00309     if (w != 0) {
00310         dst->addDirtyRect(getBoundingBox(str, x, y, w, align, deltax, useEllipsis));
00311     }
00312 }
00313 
00314 void Font::drawString(ManagedSurface *dst, const Common::U32String &str, int x, int y, int w, uint32 color, TextAlign align, int deltax) const {
00315     drawString(&dst->_innerSurface, str, x, y, w, color, align, deltax);
00316     if (w != 0) {
00317         dst->addDirtyRect(getBoundingBox(str, x, y, w, align));
00318     }
00319 }
00320 
00321 int Font::wordWrapText(const Common::String &str, int maxWidth, Common::Array<Common::String> &lines, int initWidth) const {
00322     return wordWrapTextImpl(*this, str, maxWidth, lines, initWidth);
00323 }
00324 
00325 int Font::wordWrapText(const Common::U32String &str, int maxWidth, Common::Array<Common::U32String> &lines, int initWidth) const {
00326     return wordWrapTextImpl(*this, str, maxWidth, lines, initWidth);
00327 }
00328 
00329 Common::String Font::handleEllipsis(const Common::String &input, int w) const {
00330     Common::String s = input;
00331     int width = getStringWidth(s);
00332 
00333     if (width > w && s.hasSuffix("...")) {
00334         // String is too wide. Check whether it ends in an ellipsis
00335         // ("..."). If so, remove that and try again!
00336         s.deleteLastChar();
00337         s.deleteLastChar();
00338         s.deleteLastChar();
00339         width = getStringWidth(s);
00340     }
00341 
00342     if (width > w) {
00343         Common::String str;
00344 
00345         // String is too wide. So we shorten it "intelligently" by
00346         // replacing parts of the string by an ellipsis. There are
00347         // three possibilities for this: replace the start, the end, or
00348         // the middle of the string. What is best really depends on the
00349         // context; but unless we want to make this configurable,
00350         // replacing the middle seems to be a good compromise.
00351 
00352         const int ellipsisWidth = getStringWidth("...");
00353 
00354         // SLOW algorithm to remove enough of the middle. But it is good enough
00355         // for now.
00356         const int halfWidth = (w - ellipsisWidth) / 2;
00357         int w2 = 0;
00358         Common::String::unsigned_type last = 0;
00359         uint i = 0;
00360 
00361         for (; i < s.size(); ++i) {
00362             const Common::String::unsigned_type cur = s[i];
00363             int charWidth = getCharWidth(cur) + getKerningOffset(last, cur);
00364             if (w2 + charWidth > halfWidth)
00365                 break;
00366             last = cur;
00367             w2 += charWidth;
00368             str += cur;
00369         }
00370 
00371         // At this point we know that the first 'i' chars are together 'w2'
00372         // pixels wide. We took the first i-1, and add "..." to them.
00373         str += "...";
00374         last = '.';
00375 
00376         // The original string is width wide. Of those we already skipped past
00377         // w2 pixels, which means (width - w2) remain.
00378         // The new str is (w2+ellipsisWidth) wide, so we can accommodate about
00379         // (w - (w2+ellipsisWidth)) more pixels.
00380         // Thus we skip ((width - w2) - (w - (w2+ellipsisWidth))) =
00381         // (width + ellipsisWidth - w)
00382         int skip = width + ellipsisWidth - w;
00383         for (; i < s.size() && skip > 0; ++i) {
00384             const Common::String::unsigned_type cur = s[i];
00385             skip -= getCharWidth(cur) + getKerningOffset(last, cur);
00386             last = cur;
00387         }
00388 
00389         // Append the remaining chars, if any
00390         for (; i < s.size(); ++i) {
00391             str += s[i];
00392         }
00393 
00394         return str;
00395     } else {
00396         return s;
00397     }
00398 }
00399 
00400 } // End of namespace Graphics


Generated on Sat Feb 23 2019 05:01:03 for ResidualVM by doxygen 1.7.1
curved edge   curved edge