2007-05-30 21:56:52 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
2003-11-19 23:46:39 +00:00
|
|
|
*
|
2021-12-26 17:47:58 +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 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2003-11-19 23:46:39 +00:00
|
|
|
*
|
|
|
|
* 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
|
2021-12-26 17:47:58 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2014-02-18 01:34:20 +00:00
|
|
|
*
|
2003-11-19 23:46:39 +00:00
|
|
|
*/
|
|
|
|
|
2006-04-15 21:29:41 +00:00
|
|
|
#ifndef GRAPHICS_FONT_H
|
|
|
|
#define GRAPHICS_FONT_H
|
2003-11-19 23:46:39 +00:00
|
|
|
|
2004-03-13 13:03:25 +00:00
|
|
|
#include "common/str.h"
|
2013-11-23 20:34:54 +00:00
|
|
|
#include "common/ustr.h"
|
2014-06-09 16:54:49 +00:00
|
|
|
#include "common/rect.h"
|
2004-03-13 13:03:25 +00:00
|
|
|
|
2006-04-14 02:16:55 +00:00
|
|
|
namespace Common {
|
2011-04-24 08:34:27 +00:00
|
|
|
template<class T> class Array;
|
2006-04-14 02:16:55 +00:00
|
|
|
}
|
|
|
|
|
2004-03-21 21:20:25 +00:00
|
|
|
namespace Graphics {
|
2003-11-19 23:46:39 +00:00
|
|
|
|
2020-11-25 00:52:40 +00:00
|
|
|
/**
|
|
|
|
* @defgroup graphics_font Fonts
|
|
|
|
* @ingroup graphics
|
|
|
|
*
|
|
|
|
* @brief API for representing and managing fonts on the screen.
|
|
|
|
*
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2011-04-24 08:34:27 +00:00
|
|
|
struct Surface;
|
2016-03-11 02:49:42 +00:00
|
|
|
class ManagedSurface;
|
2011-04-24 08:34:27 +00:00
|
|
|
|
2020-11-25 00:52:40 +00:00
|
|
|
/** Text alignment modes. */
|
2008-11-12 14:30:16 +00:00
|
|
|
enum TextAlign {
|
2020-11-25 00:52:40 +00:00
|
|
|
kTextAlignInvalid, ///< Indicates invalid alignment.
|
|
|
|
kTextAlignStart, ///< Align the text to start of line (virtual).
|
|
|
|
kTextAlignLeft, ///< Align the text to the left.
|
|
|
|
kTextAlignCenter, ///< Center the text.
|
|
|
|
kTextAlignEnd, ///< Align the text to end of line (virtual).
|
|
|
|
kTextAlignRight ///< Align the text to the right.
|
2004-03-13 13:03:25 +00:00
|
|
|
};
|
|
|
|
|
2020-11-25 00:52:40 +00:00
|
|
|
/** Word wrapping modes. */
|
2020-10-16 21:51:46 +00:00
|
|
|
enum WordWrapMode {
|
2020-11-25 00:52:40 +00:00
|
|
|
kWordWrapDefault = 0, ///< Default wrapping mode.
|
|
|
|
kWordWrapEvenWidthLines = 1 << 0, ///< Make the resulting line segments close to the same width.
|
2020-12-09 16:53:42 +00:00
|
|
|
kWordWrapOnExplicitNewLines = 1 << 1 ///< Text is wrapped on new lines. Otherwise, treats them as single whitespace.
|
2020-10-16 21:51:46 +00:00
|
|
|
};
|
|
|
|
|
2020-06-21 21:22:56 +00:00
|
|
|
/**
|
2020-11-25 00:52:40 +00:00
|
|
|
* Convert virtual text alignments (start + end)
|
|
|
|
* to actual text alignment (left + right + center) for drawing.
|
|
|
|
*
|
|
|
|
* If actual text alignment is provided, it is returned as-is.
|
|
|
|
*
|
|
|
|
* @param alignH The horizontal alignment to convert.
|
|
|
|
* @param rtl Indicates whether this is an RTL (right-to-left) language (such as Hebrew),
|
|
|
|
* or a left-to-right language (such as English).
|
2020-06-21 21:22:56 +00:00
|
|
|
*/
|
|
|
|
TextAlign convertTextAlignH(TextAlign alignH, bool rtl);
|
|
|
|
|
2004-03-15 18:44:14 +00:00
|
|
|
/**
|
|
|
|
* Instances of this class represent a distinct font, with a built-in renderer.
|
2020-11-25 00:52:40 +00:00
|
|
|
*
|
2005-01-06 19:09:34 +00:00
|
|
|
* @todo Maybe move the high-level methods (drawString etc.) to a separate
|
2004-03-15 18:44:14 +00:00
|
|
|
* FontRenderer class? That way, we could have different variants... ?
|
|
|
|
*/
|
2004-03-13 13:03:25 +00:00
|
|
|
class Font {
|
|
|
|
public:
|
2005-06-20 17:59:00 +00:00
|
|
|
Font() {}
|
|
|
|
virtual ~Font() {}
|
|
|
|
|
2011-01-07 12:43:00 +00:00
|
|
|
/**
|
2020-11-25 00:52:40 +00:00
|
|
|
* Return the height of the font.
|
2011-01-07 12:43:00 +00:00
|
|
|
*
|
2020-11-25 00:52:40 +00:00
|
|
|
* @return Font height in pixels.
|
2011-01-07 12:43:00 +00:00
|
|
|
*/
|
2004-03-13 13:03:25 +00:00
|
|
|
virtual int getFontHeight() const = 0;
|
2011-01-07 12:43:00 +00:00
|
|
|
|
2022-03-15 04:57:14 +00:00
|
|
|
/**
|
|
|
|
* Returns the font's name, if one is available
|
|
|
|
*/
|
|
|
|
virtual Common::String getFontName() const { return ""; }
|
|
|
|
|
2021-03-23 03:33:45 +00:00
|
|
|
/**
|
|
|
|
* Return the ascent of the font.
|
|
|
|
*
|
|
|
|
* @return Font ascent in pixels. If it is unknown
|
|
|
|
* a value of -1 is returned.
|
|
|
|
*/
|
|
|
|
virtual int getFontAscent() const;
|
|
|
|
|
2022-11-05 00:29:36 +00:00
|
|
|
/**
|
|
|
|
* Return the descent of the font.
|
|
|
|
*
|
|
|
|
* @return Font descent in pixels. If it is unknown
|
|
|
|
* a value of -1 is returned.
|
|
|
|
*/
|
|
|
|
virtual int getFontDescent() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the leading of the font.
|
|
|
|
* This is the distance between the descent line
|
|
|
|
* and the ascent line below it.
|
|
|
|
*
|
|
|
|
* @return Font leading in pixels. If it is unknown
|
|
|
|
* a value of -1 is returned.
|
|
|
|
*/
|
|
|
|
virtual int getFontLeading() const;
|
|
|
|
|
2011-01-07 12:43:00 +00:00
|
|
|
/**
|
2020-11-25 00:52:40 +00:00
|
|
|
* Return the maximum width of the font.
|
2011-01-07 12:43:00 +00:00
|
|
|
*
|
2020-11-25 00:52:40 +00:00
|
|
|
* @return Maximum font width in pixels.
|
2011-01-07 12:43:00 +00:00
|
|
|
*/
|
2004-03-13 13:03:25 +00:00
|
|
|
virtual int getMaxCharWidth() const = 0;
|
|
|
|
|
2011-01-07 12:43:00 +00:00
|
|
|
/**
|
2020-11-25 00:52:40 +00:00
|
|
|
* Return the width of a specific character.
|
|
|
|
*
|
|
|
|
* @param chr The character to query the width of.
|
2011-01-07 12:43:00 +00:00
|
|
|
*
|
2020-11-25 00:52:40 +00:00
|
|
|
* @return The width of the character in pixels.
|
2011-01-07 12:43:00 +00:00
|
|
|
*/
|
2013-11-23 20:34:54 +00:00
|
|
|
virtual int getCharWidth(uint32 chr) const = 0;
|
2011-01-07 12:43:00 +00:00
|
|
|
|
2012-01-08 01:33:34 +00:00
|
|
|
/**
|
|
|
|
* Query the kerning offset between two characters.
|
|
|
|
*
|
2020-11-25 00:52:40 +00:00
|
|
|
* @param left Left character. Can be 0.
|
|
|
|
* @param right Right character. Can be 0.
|
|
|
|
*
|
2012-01-08 01:33:34 +00:00
|
|
|
* @return The horizontal displacement.
|
|
|
|
*/
|
2013-11-23 20:34:54 +00:00
|
|
|
virtual int getKerningOffset(uint32 left, uint32 right) const;
|
2012-01-08 01:33:34 +00:00
|
|
|
|
2014-06-09 16:54:49 +00:00
|
|
|
/**
|
2020-11-25 00:52:40 +00:00
|
|
|
* Calculate the bounding box of a character.
|
|
|
|
*
|
|
|
|
* It is assumed that the character shall be drawn at position (0, 0).
|
2014-06-09 16:54:49 +00:00
|
|
|
*
|
|
|
|
* The idea here is that the character might be drawn outside the
|
|
|
|
* rect (0, 0) to (getCharWidth(chr), getFontHeight()) for some fonts.
|
|
|
|
* This is common among TTF fonts.
|
|
|
|
*
|
|
|
|
* The default implementation simply returns the rect with a width
|
|
|
|
* of getCharWidth(chr) and height of getFontHeight().
|
|
|
|
*
|
2020-11-25 00:52:40 +00:00
|
|
|
* @param chr The character to draw.
|
|
|
|
*
|
2014-06-09 16:54:49 +00:00
|
|
|
* @return The bounding box of the drawn glyph.
|
|
|
|
*/
|
|
|
|
virtual Common::Rect getBoundingBox(uint32 chr) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the bounding box of a string drawn with drawString.
|
|
|
|
*
|
2020-11-25 00:52:40 +00:00
|
|
|
* @param str The drawn string.
|
|
|
|
* @param x The x position where to start drawing.
|
|
|
|
* @param y The y position where to start drawing.
|
|
|
|
* @param w Width of the text area. This can be 0 to allow for
|
|
|
|
* obtaining the whole bounding box for a string. Note that this
|
|
|
|
* does not work with an align different than kTextAlignLeft or
|
|
|
|
* with @p useEllipsis.
|
|
|
|
* @param align Text alignment. This can be used to center a string
|
|
|
|
* in the given area or to align it to the right.
|
|
|
|
* @param deltax Offset to the x starting position of the string.
|
|
|
|
* @param useEllipsis Try to fit the string in the area by inserting an
|
|
|
|
* ellipsis. Note that the default value is false for this
|
|
|
|
* argument, unlike for drawString.
|
2021-05-04 08:45:03 +00:00
|
|
|
*
|
2014-06-09 16:54:49 +00:00
|
|
|
* @return The actual area where the string is drawn.
|
|
|
|
*/
|
|
|
|
Common::Rect getBoundingBox(const Common::String &str, int x = 0, int y = 0, const int w = 0, TextAlign align = kTextAlignLeft, int deltax = 0, bool useEllipsis = false) const;
|
2020-11-25 00:52:40 +00:00
|
|
|
/** @overload */
|
2020-06-11 19:34:38 +00:00
|
|
|
Common::Rect getBoundingBox(const Common::U32String &str, int x = 0, int _y = 0, const int w = 0, TextAlign align = kTextAlignLeft, int deltax = 0, bool useEllipsis = false) const;
|
2014-06-09 16:54:49 +00:00
|
|
|
|
2011-01-07 12:43:00 +00:00
|
|
|
/**
|
2020-11-25 00:52:40 +00:00
|
|
|
* Draw a character at a specific point on the surface.
|
2011-01-07 12:43:00 +00:00
|
|
|
*
|
2014-06-09 16:54:49 +00:00
|
|
|
* Note that the point describes the top left edge point where to draw
|
2020-11-25 00:52:40 +00:00
|
|
|
* the character. This can be different from the top left edge point of the
|
|
|
|
* character's bounding box. For example, TTF fonts sometimes move
|
|
|
|
* characters like 't' by one (or more) pixels to the left to create better
|
|
|
|
* visual results. To query the actual bounding box of a character, use
|
2014-06-09 16:54:49 +00:00
|
|
|
* getBoundingBox.
|
|
|
|
* @see getBoundingBox
|
2011-01-07 12:43:00 +00:00
|
|
|
*
|
2020-11-25 00:52:40 +00:00
|
|
|
* The Font implementation should take care of not drawing outside of the
|
2011-01-07 12:43:00 +00:00
|
|
|
* specified surface.
|
|
|
|
*
|
2020-11-25 00:52:40 +00:00
|
|
|
* @param dst The surface to draw on.
|
|
|
|
* @param chr The character to draw.
|
|
|
|
* @param x The x coordinate where to draw the character.
|
|
|
|
* @param y The y coordinate where to draw the character.
|
2011-01-07 12:43:00 +00:00
|
|
|
* @param color The color of the character.
|
|
|
|
*/
|
2013-11-23 20:34:54 +00:00
|
|
|
virtual void drawChar(Surface *dst, uint32 chr, int x, int y, uint32 color) const = 0;
|
2021-02-24 03:56:55 +00:00
|
|
|
virtual void drawChar(ManagedSurface *dst, uint32 chr, int x, int y, uint32 color) const;
|
|
|
|
|
2020-11-25 00:52:40 +00:00
|
|
|
/** @overload */
|
2004-03-13 13:03:25 +00:00
|
|
|
|
2020-11-25 00:52:40 +00:00
|
|
|
/**
|
|
|
|
* Draw the given @p str string to the given @p dst surface.
|
|
|
|
*
|
|
|
|
* @param dst The surface on which to draw the string.
|
|
|
|
* @param str The string to draw.
|
|
|
|
* @param x The x position where to start drawing.
|
|
|
|
* @param y The y position where to start drawing.
|
|
|
|
* @param w Width of the text area.
|
|
|
|
* @param color The color with which to draw the string.
|
|
|
|
* @param align Text alignment. This can be used to center the string in the given area or to align it to the right.
|
|
|
|
* @param deltax Offset to the x starting position of the string.
|
|
|
|
* @param useEllipsis Use ellipsis if needed to fit the string in the area.
|
|
|
|
*
|
|
|
|
*/
|
2021-07-31 17:42:38 +00:00
|
|
|
void drawString(Surface *dst, const Common::String &str, int x, int y, int w, uint32 color, TextAlign align = kTextAlignLeft, int deltax = 0, bool useEllipsis = false) const;
|
2020-11-25 00:52:40 +00:00
|
|
|
/** @overload */
|
2021-07-31 17:42:38 +00:00
|
|
|
void drawString(Surface *dst, const Common::U32String &str, int x, int y, int w, uint32 color, TextAlign align = kTextAlignLeft, int deltax = 0, bool useEllipsis = false) const;
|
2020-11-25 00:52:40 +00:00
|
|
|
/** @overload */
|
2021-07-31 17:42:38 +00:00
|
|
|
void drawString(ManagedSurface *dst, const Common::String &str, int x, int _y, int w, uint32 color, TextAlign align = kTextAlignLeft, int deltax = 0, bool useEllipsis = false) const;
|
2020-11-25 00:52:40 +00:00
|
|
|
/** @overload */
|
2021-07-31 17:42:38 +00:00
|
|
|
void drawString(ManagedSurface *dst, const Common::U32String &str, int x, int y, int w, uint32 color, TextAlign align = kTextAlignLeft, int deltax = 0, bool useEllipsis = false) const;
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-05-15 16:13:52 +00:00
|
|
|
/**
|
2020-11-25 00:52:40 +00:00
|
|
|
* Compute and return the width of the string @p str when rendered using this font.
|
|
|
|
*
|
2014-06-09 16:54:49 +00:00
|
|
|
* This describes the logical width of the string when drawn at (0, 0).
|
|
|
|
* This can be different from the actual bounding box of the string. Use
|
|
|
|
* getBoundingBox when you need the bounding box of a drawn string.
|
|
|
|
* @see getBoundingBox
|
|
|
|
* @see drawChar
|
2005-05-15 16:13:52 +00:00
|
|
|
*/
|
2004-03-13 13:03:25 +00:00
|
|
|
int getStringWidth(const Common::String &str) const;
|
2020-11-25 00:52:40 +00:00
|
|
|
/** @overload */
|
2013-11-23 20:34:54 +00:00
|
|
|
int getStringWidth(const Common::U32String &str) const;
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-05-15 16:13:52 +00:00
|
|
|
/**
|
2020-11-25 00:52:40 +00:00
|
|
|
* Word-wrap a text (that can contain newline characters) so that
|
|
|
|
* no text line is wider than @p maxWidth pixels.
|
|
|
|
*
|
|
|
|
* If necessary, additional line breaks are generated, preferably between
|
|
|
|
* words, where whitespace is. The resulting lines are appended
|
|
|
|
* to the @p lines string list. This returns the maximal width of any of the new
|
|
|
|
* lines (i.e. a value that is smaller or equal to maxWidth).
|
|
|
|
*
|
|
|
|
* @param str The string to word-wrap.
|
|
|
|
* @param maxWidth Maximum width that a line can have.
|
|
|
|
* @param lines The string list to which the text lines from @p str are appended.
|
|
|
|
* @param initWidth Starting width of the first line, for partially filled lines (optional).
|
|
|
|
* @param mode Wrapping mode. A bitfield of @c WordWrapMode values.
|
|
|
|
*
|
|
|
|
* @return The maximal width of any of the lines added to @p lines.
|
2005-05-15 16:13:52 +00:00
|
|
|
*/
|
2020-10-16 21:51:46 +00:00
|
|
|
int wordWrapText(const Common::String &str, int maxWidth, Common::Array<Common::String> &lines, int initWidth = 0, uint32 mode = kWordWrapOnExplicitNewLines) const;
|
2020-11-25 00:52:40 +00:00
|
|
|
/** @overload */
|
2020-10-16 21:51:46 +00:00
|
|
|
int wordWrapText(const Common::U32String &str, int maxWidth, Common::Array<Common::U32String> &lines, int initWidth = 0, uint32 mode = kWordWrapOnExplicitNewLines) const;
|
2024-03-13 16:39:21 +00:00
|
|
|
/**
|
|
|
|
* @overload
|
|
|
|
* Word-wrap a text, and returns in lineCountination a list of lines where a word has
|
|
|
|
* been splitted into the next line.
|
|
|
|
*
|
|
|
|
* @param lineContinuation Bool list. If the ith element of the list is true, then the ith
|
|
|
|
* line in lines contains a splitted word.
|
|
|
|
*/
|
|
|
|
int wordWrapText(const Common::U32String &str, int maxWidth, Common::Array<Common::U32String> &lines, Common::Array<bool> &lineContinuation, int initWidth = 0, uint32 mode = kWordWrapOnExplicitNewLines) const;
|
2023-04-16 14:33:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Scales the single gylph at @p chr the given the @p scale and the pointer @p grayScaleMap to the grayscale array. It fills @p scaleSurface surface
|
|
|
|
* and then we draw the character on @p scaleSurface surface. The @p scaleSUrface is magnified to grayScale array and then we change the @p scaleSurface using the
|
|
|
|
* grayScale array and @p grayLevel.
|
|
|
|
*
|
|
|
|
* @param grayScaleMap The pointer to the grayScale array.
|
|
|
|
* @param grayScaleMapSize The size of the grayScale array.
|
|
|
|
* @param scaleSurface The surface to where character is scaled and drawn.
|
|
|
|
* @param width The width of the bouding box for scaled glyph.
|
|
|
|
* @param height The height of the bounding box for scaled glyph.
|
|
|
|
* @param grayLevel The graylevel is the threshold value above which the pixel is considered
|
|
|
|
* @param chr The character to scale.
|
|
|
|
* @param xOffset The x offset of the bounding box for scaled glyph.
|
|
|
|
* @param yOffset The y offset of the bounding box for scaled glyph.
|
|
|
|
* @param srcheight The height of the source glyph.
|
|
|
|
* @param srcwidth The width of the source glyph bounding box.
|
|
|
|
* @param scale The scale factor that is equal to @p newSize / @p srcSize of initializeScaling.
|
|
|
|
*/
|
|
|
|
void scaleSingleGlyph(Surface *scaleSurface, int *grayScaleMap, int grayScaleMapSize, int width, int height, int xOffset, int yOffset, int grayLevel, int chr, int srcheight, int srcwidth, float scale) const;
|
|
|
|
|
2004-03-13 13:03:25 +00:00
|
|
|
};
|
2020-11-25 00:52:40 +00:00
|
|
|
/** @} */
|
2004-03-21 21:20:25 +00:00
|
|
|
} // End of namespace Graphics
|
2003-11-19 23:46:39 +00:00
|
|
|
|
|
|
|
#endif
|