Add embolden, oblique font styles (NL3D)

This commit is contained in:
Nimetu 2015-03-29 17:28:33 +03:00
parent 25a399ae9e
commit b5b5dcd031
12 changed files with 122 additions and 16 deletions

View file

@ -54,11 +54,13 @@ public:
/** generate and return a bitmap
* \param c the unicode char
* \param size size of the generated font in ??? format
* \param embolden set embolden style (bold)
* \param oblique set oblique style (slanted, italic)
* \param width width of the generated bitmap, this value is set by this function
* \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, uint32 &width, uint32 &height, uint32 &pitch, sint32 &left, sint32 &top, sint32 &advx, uint32 &glyphIndex);
uint8 *getBitmap (ucchar 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
*

View file

@ -102,6 +102,8 @@ public:
* \param fontGen font generator
* \param color primitive blocks color
* \param fontSize font size
* \param embolden font style bold
* \param oblique font style slanted (italic)
* \param desc display descriptor (screen size, font ratio)
* \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...
@ -110,6 +112,8 @@ public:
CFontGenerator *fontGen,
const NLMISC::CRGBA &color,
uint32 fontSize,
bool embolden,
bool oblique,
IDriver *driver,
CComputedString& output,
bool keep800x600Ratio= true);
@ -121,6 +125,8 @@ public:
CFontGenerator *fontGen,
const NLMISC::CRGBA &color,
uint32 fontSize,
bool embolden,
bool oblique,
IDriver *driver,
CComputedString &output,
bool keep800x600Ratio= true);
@ -132,6 +138,8 @@ public:
CFontGenerator *fontGen,
const NLMISC::CRGBA &color,
uint32 fontSize,
bool embolden,
bool oblique,
IDriver *driver,
CComputedString &output,
bool keep800x600Ratio= true);

View file

@ -74,6 +74,10 @@ public:
void setFontSize (uint32 fontSize) { _FontSize = fontSize; }
void setEmbolden (bool b) { _Embolden = b; }
void setOblique (bool b) { _Oblique = b; }
void setHotSpot (CComputedString::THotSpot hotSpot) { _HotSpot = hotSpot; }
void setScaleX (float scaleX) { _ScaleX = scaleX; }
@ -101,6 +105,10 @@ public:
uint32 getFontSize () const { return _FontSize; }
bool getEmbolden () const { return _Embolden; }
bool getOblique () const { return _Oblique; }
CComputedString::THotSpot getHotSpot() const { return _HotSpot; }
float getScaleX() const { return _ScaleX; }
@ -240,7 +248,7 @@ public:
nlassert(_FontGen);
// compute the string just one time
_FontManager->computeString (ucstr, _FontGen, _Color, _FontSize, _Driver, _TempString, _Keep800x600Ratio);
_FontManager->computeString (ucstr, _FontGen, _Color, _FontSize, _Embolden, _Oblique, _Driver, _TempString, _Keep800x600Ratio);
// draw shaded
if (_Shaded)
@ -279,7 +287,7 @@ public:
// compute the string just one time
char *str;
NLMISC_CONVERT_VARGS (str, format, NLMISC::MaxCStringSize);
_FontManager->computeString (str, _FontGen, _Color, _FontSize, _Driver, _TempString, _Keep800x600Ratio);
_FontManager->computeString (str, _FontGen, _Color, _FontSize, _Embolden, _Oblique, _Driver, _TempString, _Keep800x600Ratio);
// draw shaded
if (_Shaded)
@ -334,7 +342,7 @@ public:
*/
void computeString (const std::string& s, CComputedString& output)
{
_FontManager->computeString (s, _FontGen, _Color, _FontSize, _Driver, output, _Keep800x600Ratio);
_FontManager->computeString (s, _FontGen, _Color, _FontSize, _Embolden, _Oblique, _Driver, output, _Keep800x600Ratio);
}
/**
@ -345,12 +353,12 @@ public:
*/
void computeString (const ucstring& s, CComputedString& output)
{
_FontManager->computeString (s, _FontGen, _Color, _FontSize, _Driver, output, _Keep800x600Ratio);
_FontManager->computeString (s, _FontGen, _Color, _FontSize, _Embolden, _Oblique, _Driver, output, _Keep800x600Ratio);
}
void computeStringInfo (const ucstring& s, CComputedString& output)
{
_FontManager->computeStringInfo (s, _FontGen, _Color, _FontSize, _Driver, output, _Keep800x600Ratio);
_FontManager->computeStringInfo (s, _FontGen, _Color, _FontSize, _Embolden, _Oblique, _Driver, output, _Keep800x600Ratio);
}
/// Debug : write to the disk the texture cache
@ -381,6 +389,10 @@ private:
/// Font size;
uint32 _FontSize;
bool _Embolden;
bool _Oblique;
/// Current text color
NLMISC::CRGBA _Color;

View file

@ -66,6 +66,10 @@ public:
void setColor(NLMISC::CRGBA color);
void setFontSize(uint32 fontSize);
uint32 getFontSize() const;
void setEmbolden(bool b);
bool getEmbolden() const;
void setOblique(bool b);
bool getOblique() const;
void setHotSpot(THotSpot hotSpot);
THotSpot getHotSpot() const;
void setScaleX(float scaleX);

View file

@ -43,6 +43,8 @@ public:
ucchar Char;
CFontGenerator *FontGenerator;
sint Size;
bool Embolden;
bool Oblique;
// The less recently used infos
@ -66,6 +68,8 @@ public:
ucchar Char;
CFontGenerator *FontGenerator;
sint Size;
bool Embolden;
bool Oblique;
uint32 getVal();
//bool operator < (const SLetterKey&k) const;

View file

@ -137,6 +137,24 @@ public:
* \return the font size
*/
virtual uint32 getFontSize () const = 0;
/**
* set embolden (bold) state
* \param embolden the embbolden state
*/
virtual void setEmbolden (bool b) = 0;
/**
* \return the embolden state
*/
virtual bool getEmbolden () const = 0;
/**
* set oblique (italic) state
* \param oblique the oblique state
*/
virtual void setOblique (bool b) = 0;
/**
* \return the oblique state
*/
virtual bool getOblique () const = 0;
/**
* set the hot spot
* \param fonSize the font size

View file

@ -31,6 +31,7 @@ using namespace std;
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_SYNTHESIS_H
// for freetype 2.0
#ifdef FTERRORS_H
@ -171,7 +172,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, uint32 &width, uint32 &height, uint32 &pitch, sint32 &left, sint32 &top, sint32 &advx, uint32 &glyphIndex)
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)
{
FT_Error error;
@ -209,6 +210,16 @@ uint8 *CFontGenerator::getBitmap (ucchar c, uint32 size, uint32 &width, uint32 &
return NULL;
}
if (embolden)
{
FT_GlyphSlot_Embolden(_Face->glyph);
}
if (oblique)
{
FT_GlyphSlot_Oblique(_Face->glyph);
}
// convert to an anti-aliased bitmap
error = FT_Render_Glyph (_Face->glyph, ft_render_mode_normal);
if (error)
@ -389,7 +400,7 @@ void CFontGenerator::getSizes (ucchar c, uint32 size, uint32 &width, uint32 &hei
HFONT hFont = NULL;
uint32 CurrentFontSize = 0;
uint8 *CFontGenerator::getBitmap (ucchar c, uint32 size, uint32 &width, uint32 &height, uint32 &pitch, sint32 &left, sint32 &top, sint32 &advx, uint32 &glyphIndex)
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)
{
/* FT_Error error;

View file

@ -64,6 +64,8 @@ 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)
@ -71,7 +73,7 @@ void CFontManager::computeString (const std::string &s,
// static to avoid reallocation
static ucstring ucs;
ucs= s;
computeString(ucs, fontGen, color, fontSize, driver, output, keep800x600Ratio);
computeString(ucs, fontGen, color, fontSize, embolden, oblique, driver, output, keep800x600Ratio);
}
@ -80,6 +82,8 @@ void CFontManager::computeString (const ucstring &s,
CFontGenerator *fontGen,
const NLMISC::CRGBA &color,
uint32 fontSize,
bool embolden,
bool oblique,
IDriver *driver,
CComputedString &output,
bool keep800x600Ratio)
@ -147,6 +151,8 @@ void CFontManager::computeString (const ucstring &s,
k.Char = s[i];
k.FontGenerator = fontGen;
k.Size = fontSize;
k.Embolden = embolden;
k.Oblique = oblique;
CTextureFont::SLetterInfo *pLI = pTexFont->getLetterInfo (k);
if(pLI != NULL)
{
@ -227,6 +233,8 @@ void CFontManager::computeStringInfo ( const ucstring &s,
CFontGenerator *fontGen,
const NLMISC::CRGBA &color,
uint32 fontSize,
bool embolden,
bool oblique,
IDriver *driver,
CComputedString &output,
bool keep800x600Ratio )
@ -259,6 +267,8 @@ void CFontManager::computeStringInfo ( const ucstring &s,
k.Char = s[i];
k.FontGenerator = fontGen;
k.Size = fontSize;
k.Embolden = embolden;
k.Oblique = oblique;
pLI = pTexFont->getLetterInfo (k);
if(pLI != NULL)
{

View file

@ -277,6 +277,8 @@ void CPSUtil::print(IDriver *driver, const std::string &text, CFontGenerator &fg
&fg,
col,
16,
false,
false,
driver,
cptedString);

View file

@ -31,6 +31,8 @@ CTextContext::CTextContext()
_FontGen = NULL;
_FontSize = 12;
_Embolden = false;
_Oblique = false;
_Color = NLMISC::CRGBA(0,0,0);
@ -81,7 +83,7 @@ uint32 CTextContext::textPush (const char *format, ...)
// compute the string.
uint32 index = _CacheFreePlaces[_CacheNbFreePlaces-1];
CComputedString &strToFill = _CacheStrings[index];
_FontManager->computeString (str, _FontGen, _Color, _FontSize, _Driver, strToFill, _Keep800x600Ratio);
_FontManager->computeString (str, _FontGen, _Color, _FontSize, _Embolden, _Oblique, _Driver, strToFill, _Keep800x600Ratio);
_CacheNbFreePlaces--;
@ -109,7 +111,7 @@ uint32 CTextContext::textPush (const ucstring &str)
nlassert (index < _CacheStrings.size());
CComputedString &strToFill = _CacheStrings[index];
_FontManager->computeString (str, _FontGen, _Color
, _FontSize, _Driver, strToFill, _Keep800x600Ratio);
, _FontSize, _Embolden, _Oblique, _Driver, strToFill, _Keep800x600Ratio);
_CacheNbFreePlaces--;

View file

@ -100,6 +100,30 @@ uint32 CTextContextUser::getFontSize() const
return _TextContext.getFontSize();
}
void CTextContextUser::setEmbolden(bool b)
{
H_AUTO2;
_TextContext.setEmbolden(b);
}
bool CTextContextUser::getEmbolden() const
{
H_AUTO2;
return _TextContext.getEmbolden();
}
void CTextContextUser::setOblique(bool b)
{
H_AUTO2;
_TextContext.setOblique(b);
}
bool CTextContextUser::getOblique() const
{
H_AUTO2;
return _TextContext.getOblique();
}
void CTextContextUser::setHotSpot(THotSpot hotSpot)
{
H_AUTO2;

View file

@ -47,11 +47,14 @@ const int NbLine[TEXTUREFONT_NBCATEGORY] = { 4, 6, 4, 1 }; // Based on textsize
// ---------------------------------------------------------------------------
inline uint32 CTextureFont::SLetterKey::getVal()
{
// this limits Size to 6bits
// Large sizes already render wrong when many
// different glyphs are used due to limited texture atlas
uint8 eb = ((uint)Embolden) + ((uint)Oblique << 1);
if (FontGenerator == NULL)
return Char + ((Size&255)<<16);
return Char + ((Size&255)<<16) + (eb << 22);
else
return Char + ((Size&255)<<16) + ((FontGenerator->getUID()&0xFF)<<24);
return Char + ((Size&255)<<16) + (eb << 22) + ((FontGenerator->getUID()&0xFF)<<24);
}
// ---------------------------------------------------------------------------
@ -86,6 +89,8 @@ CTextureFont::CTextureFont()
rLetter.Char = 0xffff;
rLetter.FontGenerator = NULL;
rLetter.Size= 0;
rLetter.Embolden = false;
rLetter.Oblique = false;
// The less recently used infos
if (j < Letters[i].size()-1)
@ -164,7 +169,7 @@ void CTextureFont::rebuildLetter (sint cat, sint x, sint y)
sint posy = catTopY + y * Categories[cat];
uint32 pitch = 0;
uint8 *bitmap = rLetter.FontGenerator->getBitmap ( rLetter.Char, rLetter.Size,
uint8 *bitmap = rLetter.FontGenerator->getBitmap ( rLetter.Char, rLetter.Size, rLetter.Embolden, rLetter.Oblique,
rLetter.CharWidth, rLetter.CharHeight,
pitch, rLetter.Left, rLetter.Top,
rLetter.AdvX, rLetter.GlyphIndex );
@ -303,7 +308,7 @@ CTextureFont::SLetterInfo* CTextureFont::getLetterInfo (SLetterKey& k)
// \todo mat : Temp !!! Try to use freetype cache
uint32 nPitch, nGlyphIndex;
sint32 nLeft, nTop, nAdvX;
k.FontGenerator->getBitmap (k.Char, k.Size, width, height, nPitch, nLeft, nTop,
k.FontGenerator->getBitmap (k.Char, k.Size, k.Embolden, k.Oblique, width, height, nPitch, nLeft, nTop,
nAdvX, nGlyphIndex );
// Add 1 pixel space for black border to get correct category
@ -323,6 +328,8 @@ CTextureFont::SLetterInfo* CTextureFont::getLetterInfo (SLetterKey& k)
k2.Char = Back[cat]->Char;
k2.FontGenerator = Back[cat]->FontGenerator;
k2.Size = Back[cat]->Size;
k2.Embolden = Back[cat]->Embolden;
k2.Oblique = Back[cat]->Oblique;
itAccel = Accel.find (k2.getVal());
if (itAccel != Accel.end())
@ -336,6 +343,8 @@ CTextureFont::SLetterInfo* CTextureFont::getLetterInfo (SLetterKey& k)
Back[cat]->Char = k.Char;
Back[cat]->FontGenerator = k.FontGenerator;
Back[cat]->Size = k.Size;
Back[cat]->Embolden = k.Embolden;
Back[cat]->Oblique = k.Oblique;
Back[cat]->CharWidth = width;
Back[cat]->CharHeight = height;
Back[cat]->Top = nTop;