aboutsummaryrefslogtreecommitdiff
path: root/src/cguittfont
diff options
context:
space:
mode:
authorEkdohibs <nathanael.courant@laposte.net>2016-05-31 17:30:11 +0200
committerEkdohibs <nathanael.courant@laposte.net>2016-05-31 17:34:29 +0200
commit14ef2b445adcec770defe1abf83af9d22ccf39d8 (patch)
treebe434ea35d6134f4e7b90a74283a21815ed079ee /src/cguittfont
parent1d40385d4aacf0cbea4b19ff06940e8c9bebaf47 (diff)
downloadminetest-14ef2b445adcec770defe1abf83af9d22ccf39d8.tar.gz
minetest-14ef2b445adcec770defe1abf83af9d22ccf39d8.tar.bz2
minetest-14ef2b445adcec770defe1abf83af9d22ccf39d8.zip
Add colored text (not only colored chat).
Add documentation, move files to a proper place and avoid memory leaks. Make it work with most kind of texts, and allow backgrounds too.
Diffstat (limited to 'src/cguittfont')
-rw-r--r--src/cguittfont/CGUITTFont.cpp30
-rw-r--r--src/cguittfont/CGUITTFont.h7
2 files changed, 32 insertions, 5 deletions
diff --git a/src/cguittfont/CGUITTFont.cpp b/src/cguittfont/CGUITTFont.cpp
index 2342eb748..c2d37c6c0 100644
--- a/src/cguittfont/CGUITTFont.cpp
+++ b/src/cguittfont/CGUITTFont.cpp
@@ -1,6 +1,7 @@
/*
CGUITTFont FreeType class for Irrlicht
Copyright (c) 2009-2010 John Norman
+ Copyright (c) 2016 Nathanaƫl Courant
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
@@ -545,6 +546,13 @@ void CGUITTFont::setFontHinting(const bool enable, const bool enable_auto_hintin
void CGUITTFont::draw(const core::stringw& text, const core::rect<s32>& position, video::SColor color, bool hcenter, bool vcenter, const core::rect<s32>* clip)
{
+ draw(EnrichedString(std::wstring(text.c_str()), color), position, color, hcenter, vcenter, clip);
+}
+
+void CGUITTFont::draw(const EnrichedString &text, const core::rect<s32>& position, video::SColor color, bool hcenter, bool vcenter, const core::rect<s32>* clip)
+{
+ std::vector<video::SColor> colors = text.getColors();
+
if (!Driver)
return;
@@ -572,7 +580,7 @@ void CGUITTFont::draw(const core::stringw& text, const core::rect<s32>& position
}
// Convert to a unicode string.
- core::ustring utext(text);
+ core::ustring utext = text.getString();
// Set up our render map.
core::map<u32, CGUITTGlyphPage*> Render_Map;
@@ -581,6 +589,7 @@ void CGUITTFont::draw(const core::stringw& text, const core::rect<s32>& position
u32 n;
uchar32_t previousChar = 0;
core::ustring::const_iterator iter(utext);
+ std::vector<video::SColor> applied_colors;
while (!iter.atEnd())
{
uchar32_t currentChar = *iter;
@@ -590,7 +599,7 @@ void CGUITTFont::draw(const core::stringw& text, const core::rect<s32>& position
if (currentChar == L'\r') // Mac or Windows breaks
{
lineBreak = true;
- if (*(iter + 1) == (uchar32_t)'\n') // Windows line breaks.
+ if (*(iter + 1) == (uchar32_t)'\n') // Windows line breaks.
currentChar = *(++iter);
}
else if (currentChar == (uchar32_t)'\n') // Unix breaks
@@ -627,6 +636,9 @@ void CGUITTFont::draw(const core::stringw& text, const core::rect<s32>& position
page->render_positions.push_back(core::position2di(offset.X + offx, offset.Y + offy));
page->render_source_rects.push_back(glyph.source_rect);
Render_Map.set(glyph.glyph_page, page);
+ u32 current_color = iter.getPos();
+ if (current_color < colors.size())
+ applied_colors.push_back(colors[current_color]);
}
offset.X += getWidthFromCharacter(currentChar);
@@ -645,8 +657,6 @@ void CGUITTFont::draw(const core::stringw& text, const core::rect<s32>& position
CGUITTGlyphPage* page = n->getValue();
- if (!use_transparency) color.color |= 0xff000000;
-
if (shadow_offset) {
for (size_t i = 0; i < page->render_positions.size(); ++i)
page->render_positions[i] += core::vector2di(shadow_offset, shadow_offset);
@@ -654,7 +664,17 @@ void CGUITTFont::draw(const core::stringw& text, const core::rect<s32>& position
for (size_t i = 0; i < page->render_positions.size(); ++i)
page->render_positions[i] -= core::vector2di(shadow_offset, shadow_offset);
}
- Driver->draw2DImageBatch(page->texture, page->render_positions, page->render_source_rects, clip, color, true);
+ for (size_t i = 0; i < page->render_positions.size(); ++i) {
+ irr::video::SColor col;
+ if (!applied_colors.empty()) {
+ col = applied_colors[i < applied_colors.size() ? i : 0];
+ } else {
+ col = irr::video::SColor(255, 255, 255, 255);
+ }
+ if (!use_transparency)
+ col.color |= 0xff000000;
+ Driver->draw2DImage(page->texture, page->render_positions[i], page->render_source_rects[i], clip, col, true);
+ }
}
}
diff --git a/src/cguittfont/CGUITTFont.h b/src/cguittfont/CGUITTFont.h
index e24d8f18b..0aa540c5c 100644
--- a/src/cguittfont/CGUITTFont.h
+++ b/src/cguittfont/CGUITTFont.h
@@ -1,6 +1,7 @@
/*
CGUITTFont FreeType class for Irrlicht
Copyright (c) 2009-2010 John Norman
+ Copyright (c) 2016 Nathanaƫl Courant
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
@@ -33,6 +34,8 @@
#include <irrlicht.h>
#include <ft2build.h>
+#include <vector>
+#include "util/enriched_string.h"
#include FT_FREETYPE_H
namespace irr
@@ -258,6 +261,10 @@ namespace gui
virtual void draw(const core::stringw& text, const core::rect<s32>& position,
video::SColor color, bool hcenter=false, bool vcenter=false,
const core::rect<s32>* clip=0);
+
+ virtual void draw(const EnrichedString& text, const core::rect<s32>& position,
+ video::SColor color, bool hcenter=false, bool vcenter=false,
+ const core::rect<s32>* clip=0);
//! Returns the dimension of a character produced by this font.
virtual core::dimension2d<u32> getCharDimension(const wchar_t ch) const;