| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- #ifndef FONT_H_
- #define FONT_H_
- #include "SpriteBatch.h"
- namespace gameplay
- {
- /**
- * Defines a font for text rendering.
- */
- class Font : public Ref
- {
- friend class Bundle;
- friend class TextBox;
- public:
- /**
- * Defines the set of allowable font styles.
- */
- enum Style
- {
- PLAIN = 0,
- BOLD = 1,
- ITALIC = 2,
- BOLD_ITALIC = 4
- };
- /**
- * Defines the set of allowable alignments when drawing text.
- */
- enum Justify
- {
- // Specify horizontal alignment, use default vertical alignment (ALIGN_TOP).
- ALIGN_LEFT = 0x01,
- ALIGN_HCENTER = 0x02,
- ALIGN_RIGHT = 0x04,
-
- // Specify vertical alignment, use default horizontal alignment (ALIGN_LEFT).
- ALIGN_TOP = 0x10,
- ALIGN_VCENTER = 0x20,
- ALIGN_BOTTOM = 0x40,
- // Specify both vertical and horizontal alignment.
- ALIGN_TOP_LEFT = ALIGN_TOP | ALIGN_LEFT,
- ALIGN_VCENTER_LEFT = ALIGN_VCENTER | ALIGN_LEFT,
- ALIGN_BOTTOM_LEFT = ALIGN_BOTTOM | ALIGN_LEFT,
- ALIGN_TOP_HCENTER = ALIGN_TOP | ALIGN_HCENTER,
- ALIGN_VCENTER_HCENTER = ALIGN_VCENTER | ALIGN_HCENTER,
- ALIGN_BOTTOM_HCENTER = ALIGN_BOTTOM | ALIGN_HCENTER,
- ALIGN_TOP_RIGHT = ALIGN_TOP | ALIGN_RIGHT,
- ALIGN_VCENTER_RIGHT = ALIGN_VCENTER | ALIGN_RIGHT,
- ALIGN_BOTTOM_RIGHT = ALIGN_BOTTOM | ALIGN_RIGHT
- };
- /**
- * Defines a font glyph within the texture map for a font.
- */
- class Glyph
- {
- public:
- /**
- * Glyph character code (decimal value).
- */
- unsigned int code;
- /**
- * Glyph width (in pixels).
- */
- unsigned int width;
- /**
- * Glyph texture coordinates.
- */
- float uvs[4];
- };
- /**
- * Vertex coordinates, UVs and indices can be computed and stored in a Text object.
- * For static text labels that do not change frequently, this means these computations
- * need not be performed every frame.
- */
- class Text
- {
- friend class Font;
- public:
- /**
- * Constructor.
- */
- Text(const char* text);
- /**
- * Destructor.
- */
- ~Text();
- /**
- * Get the string that will be drawn from this Text object.
- */
- const char* getText();
- private:
- std::string _text;
- unsigned int _vertexCount;
- SpriteBatch::SpriteVertex* _vertices;
- unsigned int _indexCount;
- unsigned short* _indices;
- Vector4 _color;
- };
- /**
- * Creates a font from the given bundle.
- *
- * If the 'id' parameter is NULL, it is assumed that the Bundle at 'path'
- * contains exactly one Font resource. If the Bundle does not meet this criteria,
- * NULL is returned.
- *
- * If a font for the given path has already been loaded, the existing font will be
- * returned with its reference count increased.
- *
- * @param path The path to a bundle file containing a font resource.
- * @param id An optional ID of the font resource within the bundle (NULL for the first/only resource).
- *
- * @return The specified font.
- */
- static Font* create(const char* path, const char* id = NULL);
- /**
- * Creates a font with the given characteristics from the specified glyph array and texture map.
- *
- * This method will create a new Font object regardless of whether another Font is already
- * created with the same attributes.
- *
- * @param family The font family name.
- * @param style The font style.
- * @param size The font size.
- * @param glyphs An array of font glyphs, defining each character in the font within the texture map.
- * @param glyphCount The number of items in the glyph array.
- * @param texture A texture map containing rendered glyphs.
- *
- * @return The new Font.
- */
- static Font* create(const char* family, Style style, unsigned int size, Glyph* glyphs, int glyphCount, Texture* texture);
- /**
- * Returns the font size (max height of glyphs) in pixels.
- */
- unsigned int getSize();
- /**
- * Begins text drawing for this font.
- */
- void begin();
- /**
- * Ends text batching for this font and renders all drawn text.
- */
- void end();
- /**
- * Draws the specified text in a solid color, with a scaling factor.
- *
- * @param text The text to draw.
- * @param x The viewport x position to draw text at.
- * @param y The viewport y position to draw text at.
- * @param color The color of text.
- * @param size The size to draw text (0 for default size).
- * @param rightToLeft Whether to draw text from right to left.
- */
- void drawText(const char* text, int x, int y, const Vector4& color, unsigned int size = 0, bool rightToLeft = false);
- /**
- * Draws the specified text within a rectangular area, with a specified alignment and scale.
- * Clips text outside the viewport. Optionally wraps text to fit within the width of the viewport.
- *
- * @param text The text to draw.
- * @param area The viewport area to draw within. Text will be clipped outside this rectangle.
- * @param color The color of text.
- * @param size The size to draw text (0 for default size).
- * @param justify Justification of text within the viewport.
- * @param wrap Wraps text to fit within the width of the viewport if true.
- * @param rightToLeft Whether to draw text from right to left.
- * @param clip A region to clip text within after applying justification to the viewport area.
- */
- void drawText(const char* text, const Rectangle& area, const Vector4& color, unsigned int size = 0,
- Justify justify = ALIGN_TOP_LEFT, bool wrap = true, bool rightToLeft = false, const Rectangle* clip = NULL);
- /**
- * Draw a string from a precomputed Text object.
- *
- * @param text The text to draw.
- */
- void drawText(Text* text);
- /**
- * Create an immutable Text object from a given string.
- * Vertex coordinates, UVs and indices will be computed and stored in the Text object.
- * For static text labels that do not change frequently, this means these computations
- * need not be performed every frame.
- *
- * @param text The text to draw.
- * @param area The viewport area to draw within. Text will be clipped outside this rectangle.
- * @param color The color of text.
- * @param size The size to draw text (0 for default size).
- * @param justify Justification of text within the viewport.
- * @param wrap Wraps text to fit within the width of the viewport if true.
- * @param rightToLeft Whether to draw text from right to left.
- * @param clip A region to clip text within after applying justification to the viewport area.
- *
- * @return A Text object.
- */
- Text* createText(const char* text, const Rectangle& area, const Vector4& color, unsigned int size = 0,
- Justify justify = ALIGN_TOP_LEFT, bool wrap = true, bool rightToLeft = false, const Rectangle* clip = NULL);
- /**
- * Measures a string's width and height without alignment, wrapping or clipping.
- *
- * @param text The text to measure.
- * @param size The font height to scale to.
- * @param widthOut Destination for the text's width.
- * @param heightOut Destination for the text's height.
- */
- void measureText(const char* text, unsigned int size, unsigned int* widthOut, unsigned int* heightOut);
- /**
- * Measures a string's bounding box after alignment, wrapping and clipping within a viewport.
- *
- * @param text The text to measure.
- * @param clip The clip rectangle.
- * @param size The font height to scale to.
- * @param out Destination rectangle to store the bounds in.
- * @param justify Justification of text within the viewport.
- * @param wrap Whether to measure text with wrapping applied.
- * @param ignoreClip Whether to clip 'out' to the viewport. Set false for the bounds of what would actually be drawn
- * within the given viewport; true for bounds that are guaranteed to fit the entire string of text.
- */
- void measureText(const char* text, const Rectangle& clip, unsigned int size, Rectangle* out,
- Justify justify = ALIGN_TOP_LEFT, bool wrap = true, bool ignoreClip = false);
- /**
- * Get an character index into a string corresponding to the character nearest the given location within the clip region.
- */
- int getIndexAtLocation(const char* text, const Rectangle& clip, unsigned int size, const Vector2& inLocation, Vector2* outLocation,
- Justify justify = ALIGN_TOP_LEFT, bool wrap = true, bool rightToLeft = false);
- /**
- * Get the location of the character at the given index.
- */
- void getLocationAtIndex(const char* text, const Rectangle& clip, unsigned int size, Vector2* outLocation, const unsigned int destIndex,
- Justify justify = ALIGN_TOP_LEFT, bool wrap = true, bool rightToLeft = false);
- /**
- * Gets the sprite batch for this Font.
- *
- * @return The sprite batch for this Font.
- */
- SpriteBatch* getSpriteBatch() const;
- /**
- * Gets the Justify value from the given string.
- * Returns ALIGN_TOP_LEFT if the string is unrecognized.
- *
- * @param justify The string such as "ALIGN_HCENTER" or "ALIGN_VCENTER_RIGHT".
- *
- * @return The Justify value.
- */
- static Justify getJustify(const char* justify);
- private:
- /**
- * Constructor.
- */
- Font();
- /**
- * Constructor.
- */
- Font(const Font& copy);
- /**
- * Destructor.
- */
- ~Font();
- void getMeasurementInfo(const char* text, const Rectangle& area, unsigned int size, Justify justify, bool wrap, bool rightToLeft,
- std::vector<int>* xPositions, int* yPosition, std::vector<unsigned int>* lineLengths);
- int getIndexOrLocation(const char* text, const Rectangle& clip, unsigned int size, const Vector2& inLocation, Vector2* outLocation,
- const int destIndex = -1, Justify justify = ALIGN_TOP_LEFT, bool wrap = true, bool rightToLeft = false);
- unsigned int getTokenWidth(const char* token, unsigned int length, unsigned int size, float scale);
- unsigned int getReversedTokenLength(const char* token, const char* bufStart);
- int handleDelimiters(const char** token, const unsigned int size, const int iteration, const int areaX, int* xPos, int* yPos, unsigned int* lineLength,
- std::vector<int>::const_iterator* xPositionsIt, std::vector<int>::const_iterator xPositionsEnd, unsigned int* charIndex = NULL,
- const Vector2* stopAtPosition = NULL, const int currentIndex = -1, const int destIndex = -1);
- void addLineInfo(const Rectangle& area, int lineWidth, int lineLength, Justify hAlign,
- std::vector<int>* xPositions, std::vector<unsigned int>* lineLengths, bool rightToLeft);
- std::string _path;
- std::string _id;
- std::string _family;
- Style _style;
- unsigned int _size;
- Glyph* _glyphs;
- unsigned int _glyphCount;
- Texture* _texture;
- SpriteBatch* _batch;
- Rectangle _viewport;
- };
- }
- #endif
|