Text2D.h
1 /*
2 
3 Copyright (c) 2018, Johnathan Corkery. (jcorkery@umich.edu)
4 All rights reserved.
5 
6 This file is part of the Dynacoe project (https://github.com/jcorks/Dynacoe)
7 Dynacoe was released under the MIT License, as detailed below.
8 
9 
10 
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is furnished
16 to do so, subject to the following conditions:
17 
18 The above copyright notice and this permission notice shall
19 be included in all copies or substantial portions of the Software.
20 
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 DEALINGS IN THE SOFTWARE.
28 
29 
30 
31 */
32 
33 
34 #ifndef H_DC_TEXT_2D_INCLUDED
35 #define H_DC_TEXT_2D_INCLUDED
36 
37 #include <Dynacoe/Util/Chain.h>
38 #include <Dynacoe/Components/Render2D.h>
39 #include <Dynacoe/AssetID.h>
40 #include <Dynacoe/Color.h>
41 
42 #include <map>
43 
44 
45 namespace Dynacoe {
46 
47 
48 
49 class TextState;
50 
60 class Text2D : public Render2D {
61  public:
62  Text2D(const std::string & str = std::string(""), const Color & color = Color("white"));
63 
64  ~Text2D();
65 
68  enum class SpacingMode {
69  Kerned,
70  Monospace,
71  Bitmap
72 
73  };
74 
78  void SetFont(AssetID font);
79 
83  void SetFontSize(int size);
84 
87  std::string text;
88 
89 
94 
95 
103  void SetTextColor(int charBegin, int charEnd, const Color & color);
104  void SetTextColor(const Color & color);
105 
106 
114  Vector GetCharPosition(int i);
115 
116 
121 
126  Vector GetDimensions(const std::string &);
127 
128  void OnDraw();
129  std::string GetInfo();
130 
131  private:
132 
133 
134 
135  void ReRender();
136 
137 
138  TextState * modeInst;
139 
140  void * fontFace;
141  int fontSize;
142  SpacingMode fontSpacing;
143  std::string srcText;
144 
145  std::map<Color, uint32_t> colorLookup;
146  std::vector<Color> colorStorage;
147  std::vector<uint32_t> colorIndex;
148 
149  Renderer::Vertex2D * vertices;
150  std::vector<Vector> offsets;
151  uint32_t numVertices;
152  uint32_t numVerticesAllocd;
153  Vector dimensions;
154  void Initialize(const std::string &, const Color &);
155 };
156 }
157 
158 #endif
A standard object representing an RGBA color.
Definition: Color.h:47
void SetFont(AssetID font)
Sets the active font to be the AssetID given.
Aspect2D class that handles text rendering.
Definition: Text2D.h:60
Spcifies space to be determined by the actual character extents only. This is at times useful...
3D and 2D positional vector.
Definition: Vector.h:55
void SetTextColor(int charBegin, int charEnd, const Color &color)
Sets the character indices to be the colors specified. This setting persists across strings...
RenderMode mode
The visual mode for rendered vertices.
Definition: Render2D.h:59
Base component to render a 2D object.
Definition: Render2D.h:42
Definition: AssetID.h:37
Attempts to produce the most natural spacing between text by taking into account any special characte...
void SetFontSize(int size)
Sets the render resolution of the font.
Space among characters is distributed uniformly regardless of input text. This is typically preferred...
void OnDraw()
Function that is called upon each Draw iteration. Component Draw()s are run before the entity's draw ...
Vector GetDimensions()
Returns the bounding box dimensions of the rendered text in pixels.
std::string GetInfo()
Returns a string containing human-readable information on the state of the component.
Assets are referred to by an AssetID. The AssetID uniquely refers to an Asset stored within memory...
Definition: AssetID.h:42
void SetSpacingMode(SpacingMode mode)
Sets the mode to render text against when drawn.
std::string text
The text that should be drawn.
Definition: Text2D.h:87
Vector GetCharPosition(int i)
Returns the pixel position of the i'th character in the stored string.
SpacingMode
The mode by which text should be rendered.
Definition: Text2D.h:68