Accept any UTF string in NL3D text rendering, support UTF-32 characters, ryzom/ryzomcore#335

develop
kaetemi 4 years ago
parent dca59073aa
commit 1a045e437e

@ -181,7 +181,7 @@ public:
CVertexBuffer Vertices;
CMaterial *Material;
CRGBA Color;
ucstring Text;
std::string Text;
uint32 CacheVersion;

@ -64,17 +64,17 @@ public:
* \param height height of the generated bitmap, this value is set by this function
* \param pitch pitch of the generated bitmap (+ or - the number of bytes per row), this value is set by this function
*/
uint8 *getBitmap (ucchar c, uint32 size, bool embolden, bool oblique, uint32 &width, uint32 &height, uint32 &pitch, sint32 &left, sint32 &top, sint32 &advx, uint32 &glyphIndex);
uint8 *getBitmap (u32char c, uint32 size, bool embolden, bool oblique, uint32 &width, uint32 &height, uint32 &pitch, sint32 &left, sint32 &top, sint32 &advx, uint32 &glyphIndex);
/** returns the width and height of a character using a specific size and
*
* \warning this function is not very fast (but faster than getBitmap()) because it has to load the char before.
*/
void getSizes (ucchar c, uint32 size, uint32 &width, uint32 &height);
void getSizes (u32char c, uint32 size, uint32 &width, uint32 &height);
void getKerning (ucchar left, ucchar right, sint32 &kernx);
void getKerning (u32char left, u32char right, sint32 &kernx);
uint32 getCharIndex (ucchar c);
uint32 getCharIndex (u32char c);
uint32 getUID() { return _UID; }

@ -19,6 +19,7 @@
#include "nel/misc/types_nl.h"
#include "nel/misc/smart_ptr.h"
#include "nel/misc/utf_string_view.h"
#include "nel/3d/texture.h"
#include "nel/3d/material.h"
#include "nel/3d/texture_font.h"
@ -111,6 +112,7 @@ public:
* \param output computed string
* \param keep800x600Ratio true if you want that CFontManager look at Driver window size, and resize fontSize so it keeps same size...
*/
/*
void computeString (const std::string& s,
CFontGenerator *fontGen,
const NLMISC::CRGBA &color,
@ -120,11 +122,12 @@ public:
IDriver *driver,
CComputedString& output,
bool keep800x600Ratio= true);
*/
/**
* Same as computeString but works with a unicode string (ucstring)
*/
void computeString (const ucstring &s,
void computeString (NLMISC::CUtfStringView sv,
CFontGenerator *fontGen,
const NLMISC::CRGBA &color,
uint32 fontSize,
@ -137,7 +140,7 @@ public:
/**
* Same as computeString but do not make vertex buffers and primitives
*/
void computeStringInfo (const ucstring &s,
void computeStringInfo (NLMISC::CUtfStringView sv,
CFontGenerator *fontGen,
const NLMISC::CRGBA &color,
uint32 fontSize,

@ -134,7 +134,7 @@ public:
uint32 textPush (const char *format, ...);
/// computes an ucstring and adds the result to the cache (return the index)
uint32 textPush (const ucstring &str);
uint32 textPush (NLMISC::CUtfStringView sv);
/// remove a string from the cache
void erase (uint32 index);
@ -262,12 +262,12 @@ public:
}
/// Directly print a string
void printAt (float x, float z, const ucstring &ucstr)
void printAt (float x, float z, NLMISC::CUtfStringView sv)
{
nlassert(_FontGen);
// compute the string just one time
_FontManager->computeString (ucstr, _FontGen, _Color, _FontSize, _Embolden, _Oblique, _Driver, _TempString, _Keep800x600Ratio);
_FontManager->computeString (sv, _FontGen, _Color, _FontSize, _Embolden, _Oblique, _Driver, _TempString, _Keep800x600Ratio);
// draw shaded
if (_Shaded)
@ -372,14 +372,14 @@ public:
* \param an ucstring
* \param the computed string
*/
void computeString (const ucstring& s, CComputedString& output)
void computeString (NLMISC::CUtfStringView sv, CComputedString& output)
{
_FontManager->computeString (s, _FontGen, _Color, _FontSize, _Embolden, _Oblique, _Driver, output, _Keep800x600Ratio);
_FontManager->computeString (sv, _FontGen, _Color, _FontSize, _Embolden, _Oblique, _Driver, output, _Keep800x600Ratio);
}
void computeStringInfo (const ucstring& s, CComputedString& output)
void computeStringInfo (NLMISC::CUtfStringView sv, CComputedString& output)
{
_FontManager->computeStringInfo (s, _FontGen, _Color, _FontSize, _Embolden, _Oblique, _Driver, output, _Keep800x600Ratio);
_FontManager->computeStringInfo (sv, _FontGen, _Color, _FontSize, _Embolden, _Oblique, _Driver, output, _Keep800x600Ratio);
}
/// Debug : write to the disk the texture cache

@ -97,13 +97,13 @@ public:
*/
// @{
uint32 textPush(const char *format, ...) ;
uint32 textPush(const ucstring &str) ;
uint32 textPush(NLMISC::CUtfStringView sv) ;
void setStringColor(uint32 i, CRGBA newCol);
void setStringSelection(uint32 i, uint32 selectStart, uint32 selectSize);
void resetStringSelection(uint32 i);
void erase(uint32 i) ;
virtual CStringInfo getStringInfo (uint32 i);
virtual CStringInfo getStringInfo (const ucstring &ucstr);
virtual CStringInfo getStringInfo (NLMISC::CUtfStringView sv);
void clear() ;
void printAt(float x, float y, uint32 i) ;
@ -111,10 +111,10 @@ public:
void printClipAtUnProjected(URenderStringBuffer &renderBuffer, class NL3D::CFrustum &frustum, const NLMISC::CMatrix &scaleMatrix, float x, float y, float depth, uint32 i, float xmin, float ymin, float xmax, float ymax);
void printClipAtOld (float x, float y, uint32 i, float xmin, float ymin, float xmax, float ymax);
void printAt(float x, float y, const ucstring &ucstr) ;
void printAt(float x, float y, NLMISC::CUtfStringView sv) ;
void printfAt(float x, float y, const char * format, ...) ;
void render3D(const CMatrix &mat, const ucstring &ucstr) ;
void render3D(const CMatrix &mat, NLMISC::CUtfStringView sv) ;
void render3D(const CMatrix &mat, const char *format, ...) ;
float getLastXBound() const ;

@ -69,7 +69,7 @@ public:
// Holds info for glyphs displayed on screen
struct SLetterInfo
{
ucchar Char;
u32char Char;
sint Size;
bool Embolden;
bool Oblique;
@ -94,7 +94,7 @@ public:
struct SLetterKey
{
ucchar Char;
u32char Char;
sint Size;
bool Embolden;
bool Oblique;

@ -24,7 +24,7 @@
#include "nel/misc/rgba.h"
#include "nel/misc/ucstring.h"
#include "nel/misc/matrix.h"
#include "nel/misc/utf_string_view.h"
namespace NL3D {
@ -248,7 +248,7 @@ public:
* \param an ucstring
* \return the index where computed string has been inserted
*/
virtual uint32 textPush (const ucstring &str) = 0;
virtual uint32 textPush (NLMISC::CUtfStringView sv) = 0;
/**
* set the color of a string.
*/
@ -274,7 +274,7 @@ public:
* Get a string information from the ucstring
* The returned string info is in pixel size per default.
*/
virtual CStringInfo getStringInfo (const ucstring &ucstr) = 0;
virtual CStringInfo getStringInfo (NLMISC::CUtfStringView sv) = 0;
/**
* empty the map
*/
@ -299,7 +299,7 @@ public:
/**
* compute and print a ucstring at the location (2D method) x/y E [0,1]
*/
virtual void printAt (float x, float y, const ucstring &ucstr) = 0;
virtual void printAt (float x, float y, NLMISC::CUtfStringView sv) = 0;
/**
* compute and print a string at the location (2D method) x/y E [0,1]
*/
@ -309,7 +309,7 @@ public:
* compute and render a ucstring at the location (3D method)
* render3D() use UDriver Matrix context for Frustum/ViewMatrix, but use its own modelmatrix (mat).
*/
virtual void render3D (const NLMISC::CMatrix &mat, const ucstring &ucstr) = 0;
virtual void render3D (const NLMISC::CMatrix &mat, NLMISC::CUtfStringView sv) = 0;
/**
* compute and render a string at the location (3D method)
* render3D() use UDriver Matrix context for Frustum/ViewMatrix, but use its own modelmatrix (mat).

@ -91,6 +91,13 @@ public:
iterator begin() const { return iterator(*this, m_Str); }
inline iterator end() const { return iterator(*this, NULL); }
/// Largest possible number of characters in this string.
/// Number of actual characters may be less or equal.
inline size_t largestSize() const { return m_Size; }
inline bool empty() const { return !m_Size; }
const void *ptr() const { return m_Str; }
private:
typedef u32char (*TIterator)(const void **addr);
static u32char utf8Iterator(const void **addr);

@ -256,7 +256,7 @@ CFontGenerator::~CFontGenerator ()
}
}
void CFontGenerator::getSizes (ucchar c, uint32 size, uint32 &width, uint32 &height)
void CFontGenerator::getSizes (u32char c, uint32 size, uint32 &width, uint32 &height)
{
FT_Error error;
@ -272,7 +272,7 @@ void CFontGenerator::getSizes (ucchar c, uint32 size, uint32 &width, uint32 &hei
if (glyph_index == 0)
{
// no glyph available, replace with a dot
glyph_index = FT_Get_Char_Index (_Face, ucchar('.'));
glyph_index = FT_Get_Char_Index (_Face, u32char('.'));
}
// load glyph image into the slot (erase previous one)
@ -294,7 +294,7 @@ void CFontGenerator::getSizes (ucchar c, uint32 size, uint32 &width, uint32 &hei
height = _Face->glyph->metrics.height >> 6;
}
uint8 *CFontGenerator::getBitmap (ucchar c, uint32 size, bool embolden, bool oblique, uint32 &width, uint32 &height, uint32 &pitch, sint32 &left, sint32 &top, sint32 &advx, uint32 &glyphIndex)
uint8 *CFontGenerator::getBitmap (u32char c, uint32 size, bool embolden, bool oblique, uint32 &width, uint32 &height, uint32 &pitch, sint32 &left, sint32 &top, sint32 &advx, uint32 &glyphIndex)
{
FT_Error error;
@ -311,7 +311,7 @@ uint8 *CFontGenerator::getBitmap (ucchar c, uint32 size, bool embolden, bool obl
if (glyph_index == 0)
{
// no glyph available, replace with a dot
glyph_index = FT_Get_Char_Index (_Face, ucchar('.'));
glyph_index = FT_Get_Char_Index (_Face, u32char('.'));
}
*/
@ -374,7 +374,7 @@ uint8 *CFontGenerator::getBitmap (ucchar c, uint32 size, bool embolden, bool obl
void CFontGenerator::getKerning (ucchar left, ucchar right, sint32 &kernx)
void CFontGenerator::getKerning (u32char left, u32char right, sint32 &kernx)
{
if (!FT_HAS_KERNING(_Face))
{
@ -394,14 +394,14 @@ void CFontGenerator::getKerning (ucchar left, ucchar right, sint32 &kernx)
uint32 CFontGenerator::getCharIndex (ucchar c)
uint32 CFontGenerator::getCharIndex (u32char c)
{
uint32 ret = FT_Get_Char_Index(_Face, c);
if (ret == 0)
{
// no glyph available, replace with a dot
ret = FT_Get_Char_Index (_Face, ucchar('.'));
ret = FT_Get_Char_Index (_Face, u32char('.'));
}
return ret;
@ -475,14 +475,14 @@ CFontGenerator::~CFontGenerator ()
DeleteDC (hdcDib);
}
void CFontGenerator::getSizes (ucchar c, uint32 size, uint32 &width, uint32 &height)
void CFontGenerator::getSizes (u32char c, uint32 size, uint32 &width, uint32 &height)
{
}
HFONT hFont = NULL;
uint32 CurrentFontSize = 0;
uint8 *CFontGenerator::getBitmap (ucchar c, uint32 size, bool embolden, bool oblique, uint32 &width, uint32 &height, uint32 &pitch, sint32 &left, sint32 &top, sint32 &advx, uint32 &glyphIndex)
uint8 *CFontGenerator::getBitmap (u32char c, uint32 size, bool embolden, bool oblique, uint32 &width, uint32 &height, uint32 &pitch, sint32 &left, sint32 &top, sint32 &advx, uint32 &glyphIndex)
{
if (size == 0)
{
@ -529,7 +529,7 @@ uint8 *CFontGenerator::getBitmap (ucchar c, uint32 size, bool embolden, bool obl
SelectObject (hdcDib, hFont);
SelectObject (hdcDib, Dib);
const ucchar cc = /*(char)*/ c;
const u32char cc = /*(char)*/ c;
// prevent outputing white glyph if char is not available in font
DWORD glyphIndex;
@ -639,13 +639,13 @@ uint8 *CFontGenerator::getBitmap (ucchar c, uint32 size, bool embolden, bool obl
void CFontGenerator::getKerning (ucchar left, ucchar right, sint32 &kernx)
void CFontGenerator::getKerning (u32char left, u32char right, sint32 &kernx)
{
}
uint32 CFontGenerator::getCharIndex (ucchar c)
uint32 CFontGenerator::getCharIndex (u32char c)
{
return 0;
}

@ -63,27 +63,8 @@ CMaterial* CFontManager::getFontMaterial()
return _MatFont;
}
// ***************************************************************************
void CFontManager::computeString (const std::string &s,
CFontGenerator *fontGen,
const NLMISC::CRGBA &color,
uint32 fontSize,
bool embolden,
bool oblique,
IDriver *driver,
CComputedString &output,
bool keep800x600Ratio)
{
// static to avoid reallocation
static ucstring ucs;
ucs= s;
computeString(ucs, fontGen, color, fontSize, embolden, oblique, driver, output, keep800x600Ratio);
}
// ***************************************************************************
void CFontManager::computeString (const ucstring &s,
void CFontManager::computeString (NLMISC::CUtfStringView sv,
CFontGenerator *fontGen,
const NLMISC::CRGBA &color,
uint32 fontSize,
@ -109,7 +90,7 @@ void CFontManager::computeString (const ucstring &s,
}
// Setting vertices format
output.Vertices.setNumVertices (4 * (uint32)s.size());
output.Vertices.setNumVertices (4 * (uint32)sv.largestSize());
// 1 character <-> 1 quad
sint32 penx = 0, dx;
@ -144,7 +125,8 @@ void CFontManager::computeString (const ucstring &s,
output.StringHeight = 0;
// save string info for later rebuild as needed
output.Text = s;
if (sv.ptr() != output.Text.c_str()) // don't resave if rebuilding
output.Text = sv.toUtf8();
output.CacheVersion = getCacheVersion();
uint j = 0;
@ -156,10 +138,11 @@ void CFontManager::computeString (const ucstring &s,
hlfPixScrH = 0.f;
// For all chars
for (uint i = 0; i < s.size(); i++)
//for (uint i = 0; i < s.size(); i++)
for (NLMISC::CUtfStringView::iterator it(sv.begin()), end(sv.end()); it != end; ++it)
{
// Creating font
k.Char = s[i];
k.Char = *it;
k.FontGenerator = fontGen;
k.Size = fontSize;
k.Embolden = embolden;
@ -245,7 +228,7 @@ void CFontManager::computeString (const ucstring &s,
// ***************************************************************************
void CFontManager::computeStringInfo ( const ucstring &s,
void CFontManager::computeStringInfo ( NLMISC::CUtfStringView sv,
CFontGenerator *fontGen,
const NLMISC::CRGBA &color,
uint32 fontSize,
@ -258,10 +241,11 @@ void CFontManager::computeStringInfo ( const ucstring &s,
output.Color = color;
// save string info for later rebuild as needed
output.Text = s;
if (sv.ptr() != output.Text.c_str()) // don't resave if rebuilding
output.Text = sv.toUtf8();
output.CacheVersion = 0;
if (s.empty())
if (sv.empty())
{
output.StringWidth = 0.f;
output.StringHeight = 0;
@ -290,10 +274,10 @@ void CFontManager::computeStringInfo ( const ucstring &s,
CTextureFont::SLetterKey k;
CTextureFont::SLetterInfo *pLI;
for (uint i = 0; i < s.size(); i++)
for (NLMISC::CUtfStringView::iterator it(sv.begin()), end(sv.end()); it != end; ++it)
{
// Creating font
k.Char = s[i];
k.Char = *it;
k.FontGenerator = fontGen;
k.Size = fontSize;
k.Embolden = embolden;

@ -77,13 +77,11 @@ uint32 CTextContext::textPush (const char *format, ...)
char *str;
NLMISC_CONVERT_VARGS (str, format, NLMISC::MaxCStringSize);
ucstring uc;
uc.fromUtf8((const char *)str);
return textPush(uc);
return textPush(str);
}
// ------------------------------------------------------------------------------------------------
uint32 CTextContext::textPush (const ucstring &str)
uint32 CTextContext::textPush (NLMISC::CUtfStringView sv)
{
nlassert(_FontGen);
@ -103,7 +101,7 @@ uint32 CTextContext::textPush (const ucstring &str)
nlassert (index < _CacheStrings.size());
CComputedString &strToFill = _CacheStrings[index];
_FontManager->computeString (str, _FontGen, _Color, _FontSize, _Embolden, _Oblique, _Driver, strToFill, _Keep800x600Ratio);
_FontManager->computeString (sv, _FontGen, _Color, _FontSize, _Embolden, _Oblique, _Driver, strToFill, _Keep800x600Ratio);
// just compute letters, glyphs are rendered on demand before first draw
//_FontManager->computeStringInfo(str, _FontGen, _Color, _FontSize, _Embolden, _Oblique, _Driver, strToFill, _Keep800x600Ratio);

@ -231,11 +231,11 @@ uint32 CTextContextUser::textPush(const char *format, ...)
return _TextContext.textPush(ucstring(str)) ;
}
uint32 CTextContextUser::textPush(const ucstring &str)
uint32 CTextContextUser::textPush(NLMISC::CUtfStringView sv)
{
H_AUTO2;
return _TextContext.textPush(str) ;
return _TextContext.textPush(sv) ;
}
void CTextContextUser::setStringColor(uint32 i, CRGBA newCol)
{
@ -279,11 +279,11 @@ UTextContext::CStringInfo CTextContextUser::getStringInfo(uint32 i)
else
return CStringInfo(cstr->StringWidth, cstr->StringHeight, cstr->StringLine);
}
UTextContext::CStringInfo CTextContextUser::getStringInfo(const ucstring &str)
UTextContext::CStringInfo CTextContextUser::getStringInfo(NLMISC::CUtfStringView sv)
{
H_AUTO2;
_TextContext.computeStringInfo(str, _CacheString);
_TextContext.computeStringInfo(sv, _CacheString);
return CStringInfo (_CacheString.StringWidth, _CacheString.StringHeight, _CacheString.StringLine);
}
void CTextContextUser::clear()
@ -319,11 +319,11 @@ void CTextContextUser::printClipAtOld (float x, float y, uint32 i, float xmin, f
printClipAt(rdrBuffer, x, y ,i, xmin, ymin, xmax, ymax);
flushRenderBuffer(&rdrBuffer);
}
void CTextContextUser::printAt(float x, float y, const ucstring &ucstr)
void CTextContextUser::printAt(float x, float y, NLMISC::CUtfStringView sv)
{
H_AUTO2;
_TextContext.printAt(x, y, ucstr);
_TextContext.printAt(x, y, sv);
_DriverUser->restoreMatrixContext();
}
void CTextContextUser::printfAt(float x, float y, const char * format, ...)
@ -337,12 +337,12 @@ void CTextContextUser::printfAt(float x, float y, const char * format, ...)
_DriverUser->restoreMatrixContext();
}
void CTextContextUser::render3D(const CMatrix &mat, const ucstring &ucstr)
void CTextContextUser::render3D(const CMatrix &mat, NLMISC::CUtfStringView sv)
{
NL3D_HAUTO_RENDER_3D_TEXTCONTEXT;
CComputedString computedStr;
_TextContext.computeString(ucstr,computedStr);
_TextContext.computeString(sv,computedStr);
computedStr.render3D(*_Driver,mat);
@ -355,7 +355,7 @@ void CTextContextUser::render3D(const CMatrix &mat, const char *format, ...)
char *str;
NLMISC_CONVERT_VARGS (str, format, NLMISC::MaxCStringSize);
render3D(mat, ucstring(str));
render3D(mat, str);
_DriverUser->restoreMatrixContext();
}

Loading…
Cancel
Save