 |
Gorgon Game Engine
|
Go to the documentation of this file.
6 #include <unordered_map>
12 #include "../Utils/Assert.h"
13 #include "../Utils/Logging.h"
15 #define HR_REQ_SEMICOLON(statement) do { statement; } while(false)
16 #define HR_LOG(string, state) HR_REQ_SEMICOLON(HTMLRendererInternal::Logger.Log(string, state))
17 #define HR_LOG_ERROR(string) HR_LOG(string, Utils::Logger::State::Error)
18 #define HR_LOG_NOTICE(string) HR_LOG(string, Utils::Logger::State::Notice)
19 #define HR_LOG_MESSAGE(string) HR_LOG(string, Utils::Logger::State::Message)
21 #define HR_BTWSOR_TAG_ATTR_PAIR(tag, attribute) \
22 (static_cast<unsigned int>(tag) | static_cast<unsigned int>(attribute))
26 namespace HTMLRendererInternal {
45 return static_cast<unsigned int>(style);
49 FontFamily(
const std::unordered_map<Style, GlyphRenderer*, HashType> fonts): fonts(fonts)
54 if(fonts.count(style)) {
59 HR_LOG_NOTICE(
"could not find font style, will use the next available font");
63 const unsigned int start =
static_cast<unsigned int>(
Style::Normal);
64 const unsigned int end =
static_cast<unsigned int>(
Style::End);
65 for(
unsigned int i = start; i <
end; i++) {
66 if(fonts.count(
static_cast<Style>(i))) {
67 return fonts[
static_cast<Style>(i)];
71 HR_LOG_ERROR(
"couldn't find any font, about to crash!!!");
75 ASSERT(
false,
"empty font family map");
82 fonts[style] = renderer;
90 return static_cast<bool>(fonts.count(style));
94 std::unordered_map<Style, GlyphRenderer*, HashType> fonts;
101 fontfamily(fontfamily),
103 drawunderlined(
false),
119 parseandprint(target, text, x, y);
123 enum class Tag: unsigned int {
124 Underlined = 0x00000000,
125 Striked = 0x00000001,
129 Emphasized = 0x00000005,
135 enum class Attribute: unsigned int {
140 enum class LineType: unsigned int {
147 unsigned int operator()(Tag tag)
const {
148 return static_cast<unsigned int>(tag);
152 FontFamily &fontfamily;
153 StyledRenderer renderer;
154 bool drawunderlined, drawstriked;
155 unsigned int underlinedstart, strikedstart;
156 int baselineoffset, xx, yy, orgx, orgy;
159 TextureTarget *target;
161 static bool initialized;
162 static std::unordered_map<unsigned int, bool> attsupportmap;
163 static std::unordered_map<Tag, std::string, HashType> emptytagmap;
165 static void initialize() {
171 emptytagmap.emplace(Tag::Break,
"br");
174 static Tag string2tag(
const std::string &tag) {
175 if(tag ==
"u") {
return Tag::Underlined; }
176 else if(tag ==
"strike") {
return Tag::Striked; }
177 else if(tag ==
"b") {
return Tag::Bold; }
178 else if(tag ==
"strong") {
return Tag::Strong; }
179 else if(tag ==
"i") {
return Tag::Italic; }
180 else if(tag ==
"em") {
return Tag::Emphasized; }
181 else if(tag ==
"h1") {
return Tag::H1; }
182 else if(tag ==
"br") {
return Tag::Break; }
183 else {
ASSERT(
false,
"unsupported tag: " + tag);
return Tag::End; }
186 static Attribute string2attribute(
const std::string &attribute) {
187 if(attribute ==
"color") {
return Attribute::Color; }
188 else {
ASSERT(
false,
"unsupported attribute: " + attribute);
return Attribute::End; }
191 static RGBAf extractcolor(
const std::string color) {
192 if(color ==
"white") {
return RGBAf(1.f); }
193 else if(color ==
"black") {
return RGBAf(0.f); }
194 else if(color ==
"green") {
return RGBAf(0.f, 1.f, 0.f, 1.f); }
202 void parseandprint(TextureTarget &target,
const std::string &str,
int x,
int y);
204 void applytag(Tag tag) {
206 HR_LOG_NOTICE(
"applying tag: " + std::to_string(
static_cast<unsigned int>(tag)));
208 case Tag::Underlined:
209 underlinedstart = xx;
210 drawunderlined =
true;
221 case Tag::Emphasized:
228 if(drawunderlined) { drawline(LineType::Underline); }
229 if(drawstriked) { drawline(LineType::Strike); }
230 yy += (int)std::round(renderer.GetGlyphRenderer().GetHeight() * 1.2f);
235 ASSERT(
false,
"unsupported tag: " + std::to_string(
static_cast<unsigned int>(tag)));
240 void removetag(Tag tag) {
242 HR_LOG_NOTICE(
"removing tag: " + std::to_string(
static_cast<unsigned int>(tag)));
244 case Tag::Underlined:
245 drawunderlined =
false;
246 drawline(LineType::Underline);
250 drawline(LineType::Strike);
255 case Tag::Emphasized:
261 ASSERT(
false,
"attempting to remove tag Break" + std::to_string(
static_cast<unsigned int>(tag)));
265 ASSERT(
false,
"unsupported tag: " + std::to_string(
static_cast<unsigned int>(tag)));
270 void applyattributes(Tag tag,
const std::vector<std::pair<std::string, std::string>> &attributes) {
272 for(
const auto &attstr: attributes) {
273 attribute = string2attribute(attstr.first);
275 if(attsupportmap.count(mappedval) && attsupportmap.at(mappedval)) {
279 ucolor = extractcolor(attstr.second);
283 scolor = extractcolor(attstr.second);
286 ASSERT(
false,
"unsupported attribute: " + attstr.first);
292 HR_LOG_NOTICE(
"attribute is not part of tag (" + std::to_string(
static_cast<unsigned int>(tag)) +
")");
300 void clearattributes(Tag tag) {
302 case Tag::Underlined:
315 void drawline(LineType linetype) {
316 ASSERT(target,
"texture target is null");
318 if(linetype == LineType::Underline) {
320 target->Draw((
float)underlinedstart,
321 (
float)(yy + renderer.GetGlyphRenderer().GetUnderlineOffset() ),
322 (
float)(xx - underlinedstart),
323 (
float)renderer.GetGlyphRenderer().GetLineThickness(),
326 else if(linetype == LineType::Strike) {
328 target->Draw((
float)strikedstart,
329 (
float)(yy + renderer.GetStrikePosition() ),
330 (
float)(xx - strikedstart),
331 (
float)renderer.GetGlyphRenderer().GetLineThickness(),
335 ASSERT(
false,
"invalid line type");
342 int prevbaselineoffset = (int)renderer.GetGlyphRenderer().GetBaseLine();
344 renderer.SetGlyphRenderer(*fontfamily.GetGlyphRenderer(newstyle));
347 if((prevbaselineoffset - renderer.GetGlyphRenderer().GetBaseLine()) < 0) {
348 baselineoffset = (int)std::round(prevbaselineoffset - renderer.GetGlyphRenderer().GetBaseLine());
355 void print(TextureTarget &target,
const std::string &text,
int x,
int y) {
356 renderer.Print(target, text, x, y + baselineoffset);
Utils::Logger Logger
Definition: HTMLRenderer.cpp:10
PFNGLGENVERTEXARRAYSPROC glGenVertexArrays
Definition: OpenGL.cpp:43
PFNGLBINDBUFFERPROC glBindBuffer
Definition: OpenGL.cpp:27
void RemoveFont(Style style)
Definition: HTMLRenderer.h:85
PFNGLBINDVERTEXARRAYPROC glBindVertexArray
Definition: OpenGL.cpp:29
HTMLRenderer(FontFamily &fontfamily, RGBAf color=1.f, TextShadow shadow={})
Definition: HTMLRenderer.h:100
virtual float GetCursorAdvance(Glyph g) const =0
This function should return the number of pixels the cursor should advance after this glyph.
void ActivateQuadVertices()
Definition: Graphics.cpp:23
Tag
Tags define behavior of reflection objects.
Definition: Reflection.h:54
Definition: HTMLRenderer.h:32
Definition: HTMLRenderer.h:98
T_ Height
Height of this size object.
Definition: Size.h:261
PFNGLVERTEXATTRIBIPOINTERPROC glVertexAttribIPointer
Definition: OpenGL.cpp:68
Style
Definition: HTMLRenderer.h:34
FontFamily(const std::unordered_map< Style, GlyphRenderer *, HashType > fonts)
Definition: HTMLRenderer.h:49
Should be implemented by the systems aimed to render fonts on the screen.
Definition: Font.h:67
Represents a four channel 32 bit float per channel color information.
Definition: Color.h:373
void LoadFunctions()
Definition: OpenGL.cpp:281
GlyphRenderer * GetGlyphRenderer(Style style)
Definition: HTMLRenderer.h:52
Describes how a text shadow should be.
Definition: Font.h:432
basic_Size< int > Size
Definition: Size.h:385
Utils::Logger log
Definition: Animation.cpp:11
Glyph decode(std::string::const_iterator &it, std::string::const_iterator end)
Decodes a utf-8 character from the given iterator.
Definition: Font.cpp:9
#define HR_BTWSOR_TAG_ATTR_PAIR(tag, attribute)
Definition: HTMLRenderer.h:21
PFNGLENABLEVERTEXATTRIBARRAYPROC glEnableVertexAttribArray
Definition: OpenGL.cpp:40
PFNGLGENBUFFERSPROC glGenBuffers
Definition: OpenGL.cpp:41
bool isspace(Glyph g)
Definition: Font.cpp:96
GlyphRenderer & GetGlyphRenderer()
Definition: Font.h:472
#define HR_LOG_ERROR(string)
Definition: HTMLRenderer.h:17
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
#define ASSERT(expression, message,...)
Replaces regular assert to allow messages and backtrace.
Definition: Assert.h:161
Eases logging procedure by appending necessary information to the given data and streams to a standar...
Definition: Logging.h:19
void DrawQuadVertices()
Definition: Graphics.cpp:32
basic_Point< int > Point
Definition: Point.h:598
GLuint quadvbo
Definition: Graphics.cpp:20
basic_Point< Float > Pointf
Definition: Point.h:601
basic_Bounds< int > Bounds
Definition: Bounds.h:722
This interface defines a class that can be used as a common target for texture based drawing.
Definition: TextureTargets.h:12
GL::Texture LastTexture
Definition: Graphics.cpp:13
std::map< decltype(WindowManager::CurrentContext()), GLuint > vaos
Definition: Graphics.cpp:21
Definition: HTMLRenderer.h:43
void Initialize()
Initializes Graphics module, should be performed after an OpenGL context is created.
Definition: Graphics.cpp:37
unsigned char Byte
Represents smallest cell in memory.
Definition: Types.h:9
#define HR_LOG_NOTICE(string)
Definition: HTMLRenderer.h:18
bool HasFont(Style style) const
Definition: HTMLRenderer.h:89
@ Graphics
Definition: Template.h:164
T_ Width
Width of this size object.
Definition: Size.h:258
void AddFont(Style style, GlyphRenderer *renderer)
Definition: HTMLRenderer.h:81
std::vector< T_ >::const_iterator end(enum_type_id< T_ >)
Definition: Enum.h:288
PFNGLBUFFERDATAPROC glBufferData
Definition: OpenGL.cpp:30
unsigned int operator()(Style style) const
Definition: HTMLRenderer.h:44
intptr_t CurrentContext()
Returns an identifier for the current context.
Definition: WindowManager.cpp:34
Geometry::Size ScreenSize
Definition: Window.cpp:15
static const Geometry::Pointf fullcoordinates[4]
Coordinates that selects the entire texture to be used.
Definition: Graphics.h:501
int quadvertexindex[]
Definition: Graphics.cpp:15
void Print(TextureTarget &target, const std::string &text, int x, int y)
Definition: HTMLRenderer.h:118