mirror of
https://github.com/luanti-org/luanti.git
synced 2025-10-15 01:25:20 +02:00
IGUIFont / CGUITTFont code cleanups (#15581)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
/*
|
||||
CGUITTFont FreeType class for Irrlicht
|
||||
Copyright (c) 2009-2010 John Norman
|
||||
with changes from Luanti contributors:
|
||||
Copyright (c) 2016 Nathanaëlle Courant
|
||||
Copyright (c) 2023 Caleb Butler
|
||||
|
||||
@@ -31,14 +32,13 @@
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include "log.h"
|
||||
#include "filesys.h"
|
||||
#include "debug.h"
|
||||
|
||||
#include "CGUITTFont.h"
|
||||
#include "CMeshBuffer.h"
|
||||
#include "IFileSystem.h"
|
||||
#include "IGUIEnvironment.h"
|
||||
#include "IMeshManipulator.h"
|
||||
#include "IMeshSceneNode.h"
|
||||
#include "ISceneManager.h"
|
||||
#include "ISceneNode.h"
|
||||
|
||||
namespace irr
|
||||
{
|
||||
@@ -46,9 +46,9 @@ namespace gui
|
||||
{
|
||||
|
||||
// Manages the FT_Face cache.
|
||||
struct SGUITTFace : public virtual irr::IReferenceCounted
|
||||
struct SGUITTFace : public irr::IReferenceCounted
|
||||
{
|
||||
SGUITTFace() : face_buffer(0), face_buffer_size(0)
|
||||
SGUITTFace()
|
||||
{
|
||||
memset((void*)&face, 0, sizeof(FT_Face));
|
||||
}
|
||||
@@ -56,46 +56,29 @@ struct SGUITTFace : public virtual irr::IReferenceCounted
|
||||
~SGUITTFace()
|
||||
{
|
||||
FT_Done_Face(face);
|
||||
delete[] face_buffer;
|
||||
}
|
||||
|
||||
FT_Face face;
|
||||
FT_Byte* face_buffer;
|
||||
FT_Long face_buffer_size;
|
||||
std::string face_buffer;
|
||||
};
|
||||
|
||||
// Static variables.
|
||||
FT_Library CGUITTFont::c_library;
|
||||
std::map<io::path, SGUITTFace*> CGUITTFont::c_faces;
|
||||
bool CGUITTFont::c_libraryLoaded = false;
|
||||
scene::IMesh* CGUITTFont::shared_plane_ptr_ = 0;
|
||||
scene::SMesh CGUITTFont::shared_plane_;
|
||||
|
||||
//
|
||||
|
||||
/** Checks that no dimension of the FT_BitMap object is negative. If either is
|
||||
* negative, abort execution.
|
||||
*/
|
||||
inline void checkFontBitmapSize(const FT_Bitmap &bits)
|
||||
{
|
||||
if ((s32)bits.rows < 0 || (s32)bits.width < 0) {
|
||||
std::cout << "Insane font glyph size. File: "
|
||||
<< __FILE__ << " Line " << __LINE__
|
||||
<< std::endl;
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
video::IImage* SGUITTGlyph::createGlyphImage(const FT_Bitmap& bits, video::IVideoDriver* driver) const
|
||||
{
|
||||
// Make sure our casts to s32 in the loops below will not cause problems
|
||||
checkFontBitmapSize(bits);
|
||||
if ((s32)bits.rows < 0 || (s32)bits.width < 0)
|
||||
FATAL_ERROR("Insane font glyph size");
|
||||
|
||||
// Determine what our texture size should be.
|
||||
// Add 1 because textures are inclusive-exclusive.
|
||||
core::dimension2du d(bits.width + 1, bits.rows + 1);
|
||||
core::dimension2du texture_size;
|
||||
//core::dimension2du texture_size(bits.width + 1, bits.rows + 1);
|
||||
|
||||
// Create and load our image now.
|
||||
video::IImage* image = 0;
|
||||
@@ -147,36 +130,36 @@ video::IImage* SGUITTGlyph::createGlyphImage(const FT_Bitmap& bits, video::IVide
|
||||
for (s32 x = 0; x < (s32)bits.width; ++x)
|
||||
{
|
||||
image_data[y * image_pitch + x] |= static_cast<u32>(255.0f * (static_cast<float>(*row++) / gray_count)) << 24;
|
||||
//data[y * image_pitch + x] |= ((u32)(*bitsdata++) << 24);
|
||||
}
|
||||
glyph_data += bits.pitch;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
// TODO: error message?
|
||||
errorstream << "CGUITTFont: unknown pixel mode " << (int)bits.pixel_mode << std::endl;
|
||||
return 0;
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
void SGUITTGlyph::preload(u32 char_index, FT_Face face, video::IVideoDriver* driver, u32 font_size, const FT_Int32 loadFlags)
|
||||
void SGUITTGlyph::preload(u32 char_index, FT_Face face, CGUITTFont *parent, u32 font_size, const FT_Int32 loadFlags)
|
||||
{
|
||||
if (isLoaded) return;
|
||||
|
||||
// Set the size of the glyph.
|
||||
FT_Set_Pixel_Sizes(face, 0, font_size);
|
||||
|
||||
// Attempt to load the glyph.
|
||||
if (FT_Load_Glyph(face, char_index, loadFlags) != FT_Err_Ok)
|
||||
// TODO: error message?
|
||||
auto err = FT_Load_Glyph(face, char_index, loadFlags);
|
||||
if (err != FT_Err_Ok) {
|
||||
warningstream << "SGUITTGlyph: failed to load glyph " << char_index
|
||||
<< " with error: " << (int)err << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
FT_GlyphSlot glyph = face->glyph;
|
||||
FT_Bitmap bits = glyph->bitmap;
|
||||
const FT_Bitmap &bits = glyph->bitmap;
|
||||
|
||||
// Setup the glyph information here:
|
||||
advance = glyph->advance;
|
||||
advance = core::vector2di(glyph->advance.x, glyph->advance.y);
|
||||
offset = core::vector2di(glyph->bitmap_left, glyph->bitmap_top);
|
||||
|
||||
// Try to get the last page with available slots.
|
||||
@@ -187,7 +170,6 @@ void SGUITTGlyph::preload(u32 char_index, FT_Face face, video::IVideoDriver* dri
|
||||
{
|
||||
page = parent->createGlyphPage(bits.pixel_mode);
|
||||
if (!page)
|
||||
// TODO: add error message?
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -205,10 +187,7 @@ void SGUITTGlyph::preload(u32 char_index, FT_Face face, video::IVideoDriver* dri
|
||||
--page->available_slots;
|
||||
|
||||
// We grab the glyph bitmap here so the data won't be removed when the next glyph is loaded.
|
||||
surface = createGlyphImage(bits, driver);
|
||||
|
||||
// Set our glyph as loaded.
|
||||
isLoaded = true;
|
||||
surface = createGlyphImage(bits, parent->getDriver());
|
||||
}
|
||||
|
||||
void SGUITTGlyph::unload()
|
||||
@@ -218,7 +197,8 @@ void SGUITTGlyph::unload()
|
||||
surface->drop();
|
||||
surface = 0;
|
||||
}
|
||||
isLoaded = false;
|
||||
// reset isLoaded to false
|
||||
source_rect = core::recti();
|
||||
}
|
||||
|
||||
//////////////////////
|
||||
@@ -251,14 +231,13 @@ CGUITTFont* CGUITTFont::createTTFont(IGUIEnvironment *env, const io::path& filen
|
||||
//! Constructor.
|
||||
CGUITTFont::CGUITTFont(IGUIEnvironment *env)
|
||||
: use_monochrome(false), use_transparency(true), use_hinting(true), use_auto_hinting(true),
|
||||
batch_load_size(1), Device(0), Environment(env), Driver(0), GlobalKerningWidth(0), GlobalKerningHeight(0),
|
||||
batch_load_size(1), Driver(0), GlobalKerningWidth(0), GlobalKerningHeight(0),
|
||||
shadow_offset(0), shadow_alpha(0), fallback(0)
|
||||
{
|
||||
|
||||
if (Environment)
|
||||
{
|
||||
if (env) {
|
||||
// don't grab environment, to avoid circular references
|
||||
Driver = Environment->getVideoDriver();
|
||||
Driver = env->getVideoDriver();
|
||||
}
|
||||
|
||||
if (Driver)
|
||||
@@ -270,13 +249,10 @@ shadow_offset(0), shadow_alpha(0), fallback(0)
|
||||
bool CGUITTFont::load(const io::path& filename, const u32 size, const bool antialias, const bool transparency)
|
||||
{
|
||||
// Some sanity checks.
|
||||
if (Environment == 0 || Driver == 0) return false;
|
||||
if (!Driver) return false;
|
||||
if (size == 0) return false;
|
||||
if (filename.size() == 0) return false;
|
||||
if (filename.empty()) return false;
|
||||
|
||||
io::IFileSystem* filesystem = Environment->getFileSystem();
|
||||
irr::ILogger* logger = (Device != 0 ? Device->getLogger() : 0);
|
||||
// FIXME: this is always null ^
|
||||
this->size = size;
|
||||
this->filename = filename;
|
||||
|
||||
@@ -285,62 +261,33 @@ bool CGUITTFont::load(const io::path& filename, const u32 size, const bool antia
|
||||
this->use_transparency = transparency;
|
||||
update_load_flags();
|
||||
|
||||
// Log.
|
||||
if (logger)
|
||||
logger->log("CGUITTFont", (core::stringc(L"Creating new font: ") + filename + " " + core::stringc(size) + "pt " + (antialias ? "+antialias " : "-antialias ") + (transparency ? "+transparency" : "-transparency")).c_str(), irr::ELL_INFORMATION);
|
||||
infostream << "CGUITTFont: Creating new font: " << filename.c_str() << " "
|
||||
<< size << "pt " << (antialias ? "+antialias " : "-antialias ")
|
||||
<< (transparency ? "+transparency" : "-transparency") << std::endl;
|
||||
|
||||
// Grab the face.
|
||||
SGUITTFace* face = 0;
|
||||
SGUITTFace* face = nullptr;
|
||||
auto node = c_faces.find(filename);
|
||||
if (node == c_faces.end())
|
||||
{
|
||||
if (node == c_faces.end()) {
|
||||
face = new SGUITTFace();
|
||||
|
||||
if (!fs::ReadFile(filename.c_str(), face->face_buffer, true)) {
|
||||
delete face;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the face.
|
||||
if (FT_New_Memory_Face(c_library,
|
||||
reinterpret_cast<const FT_Byte*>(face->face_buffer.data()),
|
||||
face->face_buffer.size(), 0, &face->face))
|
||||
{
|
||||
errorstream << "CGUITTFont: FT_New_Memory_Face failed." << std::endl;
|
||||
delete face;
|
||||
return false;
|
||||
}
|
||||
|
||||
c_faces.emplace(filename, face);
|
||||
|
||||
if (filesystem)
|
||||
{
|
||||
// Read in the file data.
|
||||
io::IReadFile* file = filesystem->createAndOpenFile(filename);
|
||||
if (file == 0)
|
||||
{
|
||||
if (logger) logger->log("CGUITTFont", "Failed to open the file.", irr::ELL_INFORMATION);
|
||||
|
||||
c_faces.erase(filename);
|
||||
delete face;
|
||||
face = 0;
|
||||
return false;
|
||||
}
|
||||
face->face_buffer = new FT_Byte[file->getSize()];
|
||||
file->read(face->face_buffer, file->getSize());
|
||||
face->face_buffer_size = file->getSize();
|
||||
file->drop();
|
||||
|
||||
// Create the face.
|
||||
if (FT_New_Memory_Face(c_library, face->face_buffer, face->face_buffer_size, 0, &face->face))
|
||||
{
|
||||
if (logger) logger->log("CGUITTFont", "FT_New_Memory_Face failed.", irr::ELL_INFORMATION);
|
||||
|
||||
c_faces.erase(filename);
|
||||
delete face;
|
||||
face = 0;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (FT_New_Face(c_library, reinterpret_cast<const char*>(filename.c_str()), 0, &face->face))
|
||||
{
|
||||
if (logger) logger->log("CGUITTFont", "FT_New_Face failed.", irr::ELL_INFORMATION);
|
||||
|
||||
c_faces.erase(filename);
|
||||
delete face;
|
||||
face = 0;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// Using another instance of this face.
|
||||
face = node->second;
|
||||
face->grab();
|
||||
@@ -353,20 +300,12 @@ bool CGUITTFont::load(const io::path& filename, const u32 size, const bool antia
|
||||
FT_Set_Pixel_Sizes(tt_face, size, 0);
|
||||
font_metrics = tt_face->size->metrics;
|
||||
|
||||
verbosestream << tt_face->num_glyphs << " glyphs, ascender=" << font_metrics.ascender
|
||||
<< " height=" << font_metrics.height << std::endl;
|
||||
|
||||
// Allocate our glyphs.
|
||||
Glyphs.clear();
|
||||
Glyphs.reallocate(tt_face->num_glyphs);
|
||||
Glyphs.set_used(tt_face->num_glyphs);
|
||||
for (FT_Long i = 0; i < tt_face->num_glyphs; ++i)
|
||||
{
|
||||
Glyphs[i].isLoaded = false;
|
||||
Glyphs[i].glyph_page = 0;
|
||||
Glyphs[i].source_rect = core::recti();
|
||||
Glyphs[i].offset = core::vector2di();
|
||||
Glyphs[i].advance = FT_Vector();
|
||||
Glyphs[i].surface = 0;
|
||||
Glyphs[i].parent = this;
|
||||
}
|
||||
|
||||
// Cache the first 127 ascii characters.
|
||||
u32 old_size = batch_load_size;
|
||||
@@ -444,7 +383,7 @@ CGUITTGlyphPage* CGUITTFont::getLastGlyphPage() const
|
||||
return page;
|
||||
}
|
||||
|
||||
CGUITTGlyphPage* CGUITTFont::createGlyphPage(const u8& pixel_mode)
|
||||
CGUITTGlyphPage* CGUITTFont::createGlyphPage(const u8 pixel_mode)
|
||||
{
|
||||
CGUITTGlyphPage* page = 0;
|
||||
|
||||
@@ -481,7 +420,8 @@ CGUITTGlyphPage* CGUITTFont::createGlyphPage(const u8& pixel_mode)
|
||||
page_texture_size = max_texture_size;
|
||||
|
||||
if (!page->createPageTexture(pixel_mode, page_texture_size)) {
|
||||
// TODO: add error message?
|
||||
errorstream << "CGUITTGlyphPage: failed to create texture ("
|
||||
<< page_texture_size.Width << "x" << page_texture_size.Height << ")" << std::endl;
|
||||
delete page;
|
||||
return 0;
|
||||
}
|
||||
@@ -622,13 +562,12 @@ void CGUITTFont::draw(const EnrichedString &text, const core::rect<s32>& positio
|
||||
else if (fallback != 0)
|
||||
{
|
||||
// Let the fallback font draw it, this isn't super efficient but hopefully that doesn't matter
|
||||
wchar_t l1[] = { (wchar_t) currentChar, 0 }, l2 = (wchar_t) previousChar;
|
||||
wchar_t l1[] = { (wchar_t) currentChar, 0 };
|
||||
|
||||
if (visible)
|
||||
{
|
||||
// Apply kerning.
|
||||
offset.X += fallback->getKerningWidth(l1, &l2);
|
||||
offset.Y += fallback->getKerningHeight();
|
||||
offset += fallback->getKerning(*l1, (wchar_t) previousChar);
|
||||
|
||||
const u32 current_color = iter - utext.begin();
|
||||
fallback->draw(core::stringw(l1),
|
||||
@@ -688,11 +627,6 @@ void CGUITTFont::draw(const EnrichedString &text, const core::rect<s32>& positio
|
||||
}
|
||||
}
|
||||
|
||||
core::dimension2d<u32> CGUITTFont::getCharDimension(const wchar_t ch) const
|
||||
{
|
||||
return core::dimension2d<u32>(getWidthFromCharacter(ch), getHeightFromCharacter(ch));
|
||||
}
|
||||
|
||||
core::dimension2d<u32> CGUITTFont::getDimension(const wchar_t* text) const
|
||||
{
|
||||
return getDimension(convertWCharToU32String(text));
|
||||
@@ -759,21 +693,12 @@ core::dimension2d<u32> CGUITTFont::getDimension(const std::u32string& text) cons
|
||||
return text_dimension;
|
||||
}
|
||||
|
||||
inline u32 CGUITTFont::getWidthFromCharacter(wchar_t c) const
|
||||
{
|
||||
return getWidthFromCharacter((char32_t)c);
|
||||
}
|
||||
|
||||
inline u32 CGUITTFont::getWidthFromCharacter(char32_t c) const
|
||||
{
|
||||
// Set the size of the face.
|
||||
// This is because we cache faces and the face may have been set to a different size.
|
||||
//FT_Set_Pixel_Sizes(tt_face, 0, size);
|
||||
|
||||
u32 n = getGlyphIndexByChar(c);
|
||||
if (n > 0)
|
||||
{
|
||||
int w = Glyphs[n-1].advance.x / 64;
|
||||
int w = Glyphs[n-1].advance.X / 64;
|
||||
return w;
|
||||
}
|
||||
if (fallback != 0)
|
||||
@@ -787,17 +712,8 @@ inline u32 CGUITTFont::getWidthFromCharacter(char32_t c) const
|
||||
else return (font_metrics.ascender / 64) / 2;
|
||||
}
|
||||
|
||||
inline u32 CGUITTFont::getHeightFromCharacter(wchar_t c) const
|
||||
{
|
||||
return getHeightFromCharacter((char32_t)c);
|
||||
}
|
||||
|
||||
inline u32 CGUITTFont::getHeightFromCharacter(char32_t c) const
|
||||
{
|
||||
// Set the size of the face.
|
||||
// This is because we cache faces and the face may have been set to a different size.
|
||||
//FT_Set_Pixel_Sizes(tt_face, 0, size);
|
||||
|
||||
u32 n = getGlyphIndexByChar(c);
|
||||
if (n > 0)
|
||||
{
|
||||
@@ -816,11 +732,6 @@ inline u32 CGUITTFont::getHeightFromCharacter(char32_t c) const
|
||||
else return (font_metrics.ascender / 64) / 2;
|
||||
}
|
||||
|
||||
u32 CGUITTFont::getGlyphIndexByChar(wchar_t c) const
|
||||
{
|
||||
return getGlyphIndexByChar((char32_t)c);
|
||||
}
|
||||
|
||||
u32 CGUITTFont::getGlyphIndexByChar(char32_t c) const
|
||||
{
|
||||
// Get the glyph.
|
||||
@@ -831,7 +742,7 @@ u32 CGUITTFont::getGlyphIndexByChar(char32_t c) const
|
||||
return 0;
|
||||
|
||||
// If our glyph is already loaded, don't bother doing any batch loading code.
|
||||
if (glyph != 0 && Glyphs[glyph - 1].isLoaded)
|
||||
if (glyph != 0 && Glyphs[glyph - 1].isLoaded())
|
||||
return glyph;
|
||||
|
||||
// Determine our batch loading positions.
|
||||
@@ -850,9 +761,10 @@ u32 CGUITTFont::getGlyphIndexByChar(char32_t c) const
|
||||
if (char_index)
|
||||
{
|
||||
SGUITTGlyph& glyph = Glyphs[char_index - 1];
|
||||
if (!glyph.isLoaded)
|
||||
if (!glyph.isLoaded())
|
||||
{
|
||||
glyph.preload(char_index, tt_face, Driver, size, load_flags);
|
||||
auto *this2 = const_cast<CGUITTFont*>(this); // oh well
|
||||
glyph.preload(char_index, tt_face, this2, size, load_flags);
|
||||
Glyph_Pages[glyph.glyph_page]->pushGlyphToBePaged(&glyph);
|
||||
}
|
||||
}
|
||||
@@ -871,11 +783,10 @@ s32 CGUITTFont::getCharacterFromPos(const wchar_t* text, s32 pixel_x) const
|
||||
s32 CGUITTFont::getCharacterFromPos(const std::u32string& text, s32 pixel_x) const
|
||||
{
|
||||
s32 x = 0;
|
||||
//s32 idx = 0;
|
||||
|
||||
u32 character = 0;
|
||||
char32_t previousChar = 0;
|
||||
std::u32string::const_iterator iter = text.begin();
|
||||
auto iter = text.begin();
|
||||
while (iter != text.end())
|
||||
{
|
||||
char32_t c = *iter;
|
||||
@@ -906,28 +817,6 @@ void CGUITTFont::setKerningHeight(s32 kerning)
|
||||
GlobalKerningHeight = kerning;
|
||||
}
|
||||
|
||||
s32 CGUITTFont::getKerningWidth(const wchar_t* thisLetter, const wchar_t* previousLetter) const
|
||||
{
|
||||
if (tt_face == 0)
|
||||
return GlobalKerningWidth;
|
||||
if (thisLetter == 0 || previousLetter == 0)
|
||||
return 0;
|
||||
|
||||
return getKerningWidth((char32_t)*thisLetter, (char32_t)*previousLetter);
|
||||
}
|
||||
|
||||
s32 CGUITTFont::getKerningWidth(const char32_t thisLetter, const char32_t previousLetter) const
|
||||
{
|
||||
// Return only the kerning width.
|
||||
return getKerning(thisLetter, previousLetter).X;
|
||||
}
|
||||
|
||||
s32 CGUITTFont::getKerningHeight() const
|
||||
{
|
||||
// FreeType 2 currently doesn't return any height kerning information.
|
||||
return GlobalKerningHeight;
|
||||
}
|
||||
|
||||
core::vector2di CGUITTFont::getKerning(const wchar_t thisLetter, const wchar_t previousLetter) const
|
||||
{
|
||||
return getKerning((char32_t)thisLetter, (char32_t)previousLetter);
|
||||
@@ -949,11 +838,8 @@ core::vector2di CGUITTFont::getKerning(const char32_t thisLetter, const char32_t
|
||||
// If we don't have this glyph, ask fallback font
|
||||
if (n == 0)
|
||||
{
|
||||
if (fallback != 0) {
|
||||
wchar_t l1 = (wchar_t) thisLetter, l2 = (wchar_t) previousLetter;
|
||||
ret.X = fallback->getKerningWidth(&l1, &l2);
|
||||
ret.Y = fallback->getKerningHeight();
|
||||
}
|
||||
if (fallback)
|
||||
ret = fallback->getKerning((wchar_t) thisLetter, (wchar_t) previousLetter);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -1029,196 +915,6 @@ video::ITexture* CGUITTFont::getPageTextureByIndex(const u32& page_index) const
|
||||
return 0;
|
||||
}
|
||||
|
||||
void CGUITTFont::createSharedPlane()
|
||||
{
|
||||
/*
|
||||
2___3
|
||||
| /|
|
||||
| / | <-- plane mesh is like this, point 2 is (0,0), point 0 is (0, -1)
|
||||
|/ | <-- the texture coords of point 2 is (0,0, point 0 is (0, 1)
|
||||
0---1
|
||||
*/
|
||||
|
||||
using namespace core;
|
||||
using namespace video;
|
||||
using namespace scene;
|
||||
S3DVertex vertices[4];
|
||||
u16 indices[6] = {0,2,3,3,1,0};
|
||||
vertices[0] = S3DVertex(vector3df(0,-1,0), vector3df(0,0,-1), SColor(255,255,255,255), vector2df(0,1));
|
||||
vertices[1] = S3DVertex(vector3df(1,-1,0), vector3df(0,0,-1), SColor(255,255,255,255), vector2df(1,1));
|
||||
vertices[2] = S3DVertex(vector3df(0, 0,0), vector3df(0,0,-1), SColor(255,255,255,255), vector2df(0,0));
|
||||
vertices[3] = S3DVertex(vector3df(1, 0,0), vector3df(0,0,-1), SColor(255,255,255,255), vector2df(1,0));
|
||||
|
||||
SMeshBuffer* buf = new SMeshBuffer();
|
||||
buf->append(vertices, 4, indices, 6);
|
||||
|
||||
shared_plane_.addMeshBuffer( buf );
|
||||
shared_plane_.setHardwareMappingHint(EHM_STATIC);
|
||||
|
||||
shared_plane_ptr_ = &shared_plane_;
|
||||
buf->drop(); //the addMeshBuffer method will grab it, so we can drop this ptr.
|
||||
}
|
||||
|
||||
core::dimension2d<u32> CGUITTFont::getDimensionUntilEndOfLine(const wchar_t* p) const
|
||||
{
|
||||
core::stringw s;
|
||||
for (const wchar_t* temp = p; temp && *temp != '\0' && *temp != L'\r' && *temp != L'\n'; ++temp )
|
||||
s.append(*temp);
|
||||
|
||||
return getDimension(s.c_str());
|
||||
}
|
||||
|
||||
core::array<scene::ISceneNode*> CGUITTFont::addTextSceneNode(const wchar_t* text, scene::ISceneManager* smgr, scene::ISceneNode* parent, const video::SColor& color, bool center)
|
||||
{
|
||||
using namespace core;
|
||||
using namespace video;
|
||||
using namespace scene;
|
||||
|
||||
array<scene::ISceneNode*> container;
|
||||
|
||||
if (!Driver || !smgr) return container;
|
||||
if (!parent)
|
||||
parent = smgr->addEmptySceneNode(smgr->getRootSceneNode(), -1);
|
||||
// if you don't specify parent, then we add an empty node attached to the root node
|
||||
// this is generally undesirable.
|
||||
|
||||
if (!shared_plane_ptr_) //this points to a static mesh that contains the plane
|
||||
createSharedPlane(); //if it's not initialized, we create one.
|
||||
|
||||
dimension2d<s32> text_size(getDimension(text)); //convert from unsigned to signed.
|
||||
vector3df start_point(0, 0, 0), offset;
|
||||
|
||||
/** NOTICE:
|
||||
Because we are considering adding texts into 3D world, all Y axis vectors are inverted.
|
||||
**/
|
||||
|
||||
// There's currently no "vertical center" concept when you apply text scene node to the 3D world.
|
||||
if (center)
|
||||
{
|
||||
offset.X = start_point.X = -text_size.Width / 2.f;
|
||||
offset.Y = start_point.Y = +text_size.Height/ 2.f;
|
||||
offset.X += (text_size.Width - getDimensionUntilEndOfLine(text).Width) >> 1;
|
||||
}
|
||||
|
||||
// the default font material
|
||||
SMaterial mat;
|
||||
mat.ZWriteEnable = video::EZW_OFF;
|
||||
mat.MaterialType = use_transparency ? video::EMT_TRANSPARENT_ALPHA_CHANNEL : video::EMT_SOLID;
|
||||
mat.MaterialTypeParam = 0.01f;
|
||||
|
||||
wchar_t current_char = 0, previous_char = 0;
|
||||
u32 n = 0;
|
||||
|
||||
array<u32> glyph_indices;
|
||||
|
||||
while (*text)
|
||||
{
|
||||
current_char = *text;
|
||||
bool line_break=false;
|
||||
if (current_char == L'\r') // Mac or Windows breaks
|
||||
{
|
||||
line_break = true;
|
||||
if (*(text + 1) == L'\n') // Windows line breaks.
|
||||
current_char = *(++text);
|
||||
}
|
||||
else if (current_char == L'\n') // Unix breaks
|
||||
{
|
||||
line_break = true;
|
||||
}
|
||||
|
||||
if (line_break)
|
||||
{
|
||||
previous_char = 0;
|
||||
offset.Y -= tt_face->size->metrics.ascender / 64;
|
||||
offset.X = start_point.X;
|
||||
if (center)
|
||||
offset.X += (text_size.Width - getDimensionUntilEndOfLine(text+1).Width) >> 1;
|
||||
++text;
|
||||
}
|
||||
else
|
||||
{
|
||||
n = getGlyphIndexByChar(current_char);
|
||||
if (n > 0)
|
||||
{
|
||||
glyph_indices.push_back( n );
|
||||
|
||||
// Store glyph size and offset informations.
|
||||
SGUITTGlyph const& glyph = Glyphs[n-1];
|
||||
u32 texw = glyph.source_rect.getWidth();
|
||||
u32 texh = glyph.source_rect.getHeight();
|
||||
s32 offx = glyph.offset.X;
|
||||
s32 offy = (font_metrics.ascender / 64) - glyph.offset.Y;
|
||||
|
||||
// Apply kerning.
|
||||
vector2di k = getKerning(current_char, previous_char);
|
||||
offset.X += k.X;
|
||||
offset.Y += k.Y;
|
||||
|
||||
vector3df current_pos(offset.X + offx, offset.Y - offy, 0);
|
||||
dimension2d<u32> letter_size = dimension2d<u32>(texw, texh);
|
||||
|
||||
// Now we copy planes corresponding to the letter size.
|
||||
IMeshManipulator* mani = smgr->getMeshManipulator();
|
||||
IMesh* meshcopy = mani->createMeshCopy(shared_plane_ptr_);
|
||||
mani->scale(meshcopy, vector3df((f32)letter_size.Width, (f32)letter_size.Height, 1));
|
||||
|
||||
ISceneNode* current_node = smgr->addMeshSceneNode(meshcopy, parent, -1, current_pos);
|
||||
meshcopy->drop();
|
||||
|
||||
current_node->getMaterial(0) = mat;
|
||||
current_node->setAutomaticCulling(EAC_OFF);
|
||||
current_node->setIsDebugObject(true); //so the picking won't have any effect on individual letter
|
||||
//current_node->setDebugDataVisible(EDS_BBOX); //de-comment this when debugging
|
||||
|
||||
container.push_back(current_node);
|
||||
}
|
||||
offset.X += getWidthFromCharacter(current_char);
|
||||
// Note that fallback font handling is missing here (Minetest never uses this)
|
||||
|
||||
previous_char = current_char;
|
||||
++text;
|
||||
}
|
||||
}
|
||||
|
||||
update_glyph_pages();
|
||||
//only after we update the textures can we use the glyph page textures.
|
||||
|
||||
for (u32 i = 0; i < glyph_indices.size(); ++i)
|
||||
{
|
||||
u32 n = glyph_indices[i];
|
||||
SGUITTGlyph const& glyph = Glyphs[n-1];
|
||||
ITexture* current_tex = Glyph_Pages[glyph.glyph_page]->texture;
|
||||
f32 page_texture_size = (f32)current_tex->getSize().Width;
|
||||
//Now we calculate the UV position according to the texture size and the source rect.
|
||||
//
|
||||
// 2___3
|
||||
// | /|
|
||||
// | / | <-- plane mesh is like this, point 2 is (0,0), point 0 is (0, -1)
|
||||
// |/ | <-- the texture coords of point 2 is (0,0, point 0 is (0, 1)
|
||||
// 0---1
|
||||
//
|
||||
f32 u1 = glyph.source_rect.UpperLeftCorner.X / page_texture_size;
|
||||
f32 u2 = u1 + (glyph.source_rect.getWidth() / page_texture_size);
|
||||
f32 v1 = glyph.source_rect.UpperLeftCorner.Y / page_texture_size;
|
||||
f32 v2 = v1 + (glyph.source_rect.getHeight() / page_texture_size);
|
||||
|
||||
//we can be quite sure that this is IMeshSceneNode, because we just added them in the above loop.
|
||||
IMeshSceneNode* node = static_cast<IMeshSceneNode*>(container[i]);
|
||||
|
||||
S3DVertex* pv = static_cast<S3DVertex*>(node->getMesh()->getMeshBuffer(0)->getVertices());
|
||||
//pv[0].TCoords.Y = pv[1].TCoords.Y = (letter_size.Height - 1) / static_cast<f32>(letter_size.Height);
|
||||
//pv[1].TCoords.X = pv[3].TCoords.X = (letter_size.Width - 1) / static_cast<f32>(letter_size.Width);
|
||||
pv[0].TCoords = vector2df(u1, v2);
|
||||
pv[1].TCoords = vector2df(u2, v2);
|
||||
pv[2].TCoords = vector2df(u1, v1);
|
||||
pv[3].TCoords = vector2df(u2, v1);
|
||||
|
||||
container[i]->getMaterial(0).setTexture(0, current_tex);
|
||||
}
|
||||
|
||||
return container;
|
||||
}
|
||||
|
||||
std::u32string CGUITTFont::convertWCharToU32String(const wchar_t* const charArray) const
|
||||
{
|
||||
static_assert(sizeof(wchar_t) == 2 || sizeof(wchar_t) == 4, "unexpected wchar size");
|
||||
|
Reference in New Issue
Block a user