PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
PlatformFont Class Reference

#include <PlatformFont.h>

Inheritance diagram for PlatformFont:
PFont PFont PFont PEBLObjectBase PEBLObjectBase PEBLObjectBase

Public Member Functions

 PlatformFont (const std::string &filename)
 
 PlatformFont (const std::string &filename, int style, int size, PColor fgcolor, PColor bgcolor, bool aa)
 Convenience constructor of PlatformFont:
 
 PlatformFont (PlatformFont &font)
 Copy constructor of PlatformFont:
 
virtual ~PlatformFont ()
 Copy constructor.
 
virtual void SetFontColor (PColor color)
 Set*Color needs to be overridden because it doesn't change the SDL_Color data.
 
virtual void SetBackgroundColor (PColor color)
 Set*Color needs to be overridden because it doesn't change the SDL_Color data.
 
virtual void SetFontSize (const int size)
 Override SetFontSize to update TTF font and mark as changed.
 
virtual void SetFontStyle (const int style)
 Override SetFontStyle to update TTF font and mark as changed.
 
SDL_Surface * RenderText (const std::string &text)
 This takes care of all the busy work of rendering the text.
 
unsigned int GetTextWidth (const std::string &text)
 
unsigned int GetTextHeight (const std::string &text)
 
unsigned int GetPosition (const std::string &text, unsigned int x)
 
virtual std::string ObjectName () const
 
TTF_Font * GetTTFFont () const
 
bool HasChanged ()
 Check if font or its colors have changed.
 
void ClearChanged ()
 Clear all changed flags.
 
void UpdateSDLColors ()
 Update SDL color cache from PColor objects.
 
 PlatformFont (const std::string &filename)
 
 PlatformFont (const std::string &filename, int style, int size, PColor fgcolor, PColor bgcolor, bool aa)
 
 PlatformFont (PlatformFont &font)
 
virtual ~PlatformFont ()
 Copy constructor.
 
virtual void SetFontColor (PColor color)
 
virtual void SetBackgroundColor (PColor color)
 
SDL_Texture * RenderTexture (const std::wstring &text)
 This takes care of all the busy work of rendering the text.
 
SDL_Texture * RenderText (const std::wstring &text)
 
unsigned int GetTextWidth (const std::string &text)
 
unsigned int GetTextHeight (const std::string &text)
 
unsigned int GetPosition (const std::string &text, unsigned int x)
 
virtual std::string ObjectName () const
 
 PlatformFont (const std::string &filename)
 
 PlatformFont (const std::string &filename, int style, int size, PColor fgcolor, PColor bgcolor, bool aa)
 
 PlatformFont (PlatformFont &font)
 
virtual ~PlatformFont ()
 
unsigned int GetTextWidth (const std::string &text)
 
unsigned int GetTextHeight (const std::string &text)
 
- Public Member Functions inherited from PFont
 PFont ()
 Standard constructors:
 
 PFont (const std::string &filename)
 
 PFont (const std::string &filename, int style, int size, PColor fgcolor, PColor bgcolor, bool aa)
 Convenience constructor of PFont:
 
 PFont (const PFont &font)
 Copy constructor of PFont:
 
virtual ~PFont ()
 Copy constructor.
 
virtual bool SetProperty (std::string, Variant v)
 Standard Destructors.
 
virtual Variant GetProperty (std::string) const
 
virtual ObjectValidationError ValidateProperty (std::string, Variant v) const
 
virtual ObjectValidationError ValidateProperty (std::string) const
 
virtual void SetFontFileName (const std::string &name)
 Set methods for all of the data in font.
 
virtual void SetAntiAliased (const bool aa)
 
virtual std::string GetFontFileName () const
 
virtual int GetFontStyle () const
 
virtual int GetFontSize () const
 
virtual PColor GetFontColor () const
 
virtual PColor GetBackgroundColor () const
 
virtual bool GetAntiAliased () const
 
virtual bool IsNormalFont () const
 
virtual bool IsBoldFont () const
 
virtual bool IsItalicFont () const
 
virtual bool IsUnderlineFont () const
 
- Public Member Functions inherited from PEBLObjectBase
 PEBLObjectBase ()
 Standard Constructor.
 
 PEBLObjectBase (ComplexDataType cdt)
 Standard Constructor.
 
 PEBLObjectBase (const PEBLObjectBase &pob)
 
virtual ~PEBLObjectBase ()
 Standard Destructor.
 
virtual bool InitializeProperty (std::string name, Variant v)
 
Variant GetProperty (std::string) const
 
ComplexDataType GetType ()
 
virtual std::ostream & PrintProperties (std::ostream &out)
 
virtual Variant GetPropertyList ()
 

Protected Member Functions

virtual std::ostream & SendToStream (std::ostream &out) const
 This sends the font descriptions to the specified stream.
 
virtual std::ostream & SendToStream (std::ostream &out) const
 This sends the font descriptions to the specified stream.
 
virtual std::ostream & SendToStream (std::ostream &out) const
 This sends the font descriptions to the specified stream.
 
- Protected Member Functions inherited from PFont
PColorGetFontColorPtr () const
 
PColorGetBackgroundColorPtr () const
 

Additional Inherited Members

- Protected Attributes inherited from PFont
std::string mFontFileName
 
int mFontStyle
 
int mFontSize
 
bool mAntiAliased
 
- Protected Attributes inherited from PEBLObjectBase
ComplexDataType mCDT
 
std::map< std::string, VariantmProperties
 

Detailed Description

This is a platform-specific font class. Much of the information is held in the generic PFont class, but this class needs to hold onto an actual font structure from the font library, and handle the rendering.

Validator platform font - provides dummy text metrics Used only for compilation/linking, never executed

Definition at line 52 of file sdl/PlatformFont.h.

Constructor & Destructor Documentation

◆ PlatformFont() [1/9]

PlatformFont::PlatformFont ( const std::string &  filename)

Definition at line 27 of file validator/PlatformFont.cpp.

28 : PFont(filename, 0, 18, PColor(0,0,0,255), PColor(255,255,255,255), true) {
29}
PFont()
Standard constructors:
Definition PFont.cpp:39

◆ PlatformFont() [2/9]

PlatformFont::PlatformFont ( const std::string &  filename,
int  style,
int  size,
PColor  fgcolor,
PColor  bgcolor,
bool  aa 
)

Convenience constructor of PlatformFont:

Definition at line 117 of file sdl/PlatformFont.cpp.

117 :
118 PFont(filename, style, size, fgcolor, bgcolor, aa),
119 mBuffer(nullptr),
120 mChanged(false)
121
122{
123 string fontname = Evaluator::gPath.FindFile(mFontFileName);
124 if(fontname == "")
125 PError::SignalFatalError(string("Unable to find font file [") + mFontFileName + string("]."));
126
127 // Create cache key from base class properties (after PFont constructor)
128 mCacheKey.filename = mFontFileName;
129 mCacheKey.style = mFontStyle;
130 mCacheKey.size = mFontSize;
131
132 // Get font from cache (will load from disk if not cached)
133 mTTF_Font = FontCache::FontCacheManager::GetInstance().GetFont(mCacheKey, fontname);
134
135 if(!mTTF_Font)
136 {
137 printf("Oh My Goodness, an error : [%s]\n", TTF_GetError());
138 PError::SignalFatalError("Failed to create font\n");
139 }
140
141 //Translate PColor to SDLcolor for direct use in rendering.
142 PColor* fgColor = GetFontColorPtr();
143 PColor* bgColor = GetBackgroundColorPtr();
144
145 if(fgColor) mSDL_FGColor = SDLUtility::PColorToSDLColor(*fgColor);
146 if(bgColor) mSDL_BGColor = SDLUtility::PColorToSDLColor(*bgColor);
147}
static PEBLPath gPath
static FontCacheManager & GetInstance()
Get singleton instance.
Definition FontCache.cpp:33
TTF_Font * GetFont(const FontCacheKey &key, const std::string &full_path)
Definition FontCache.cpp:42
std::string FindFile(const string &filename)
Definition PEBLPath.cpp:368
PColor * GetBackgroundColorPtr() const
Definition PFont.cpp:280
PColor * GetFontColorPtr() const
Definition PFont.cpp:272
int mFontStyle
Definition PFont.h:105
int mFontSize
Definition PFont.h:106
std::string mFontFileName
Definition PFont.h:104
void SignalFatalError(const std::string &message)
SDL_Color PColorToSDLColor(PColor pcolor)
This converts between a PColor and an SDL color.

References FontCache::FontCacheKey::filename, PEBLPath::FindFile(), PFont::GetBackgroundColorPtr(), FontCache::FontCacheManager::GetFont(), PFont::GetFontColorPtr(), FontCache::FontCacheManager::GetInstance(), Evaluator::gPath, PFont::mFontFileName, PFont::mFontSize, PFont::mFontStyle, SDLUtility::PColorToSDLColor(), PError::SignalFatalError(), FontCache::FontCacheKey::size, and FontCache::FontCacheKey::style.

◆ PlatformFont() [3/9]

PlatformFont::PlatformFont ( PlatformFont font)

Copy constructor of PlatformFont:

Definition at line 152 of file sdl/PlatformFont.cpp.

152 :
153 PFont(font), // Chain to base class copy constructor which handles colors
154 mBuffer(nullptr),
155 mChanged(false)
156
157{
159 mFontStyle = font.GetFontStyle();
160 mFontSize = font.GetFontSize();
162
163 // Create cache key from font properties
164 mCacheKey.filename = mFontFileName;
165 mCacheKey.style = mFontStyle;
166 mCacheKey.size = mFontSize;
167
168 // Get font from cache (may share TTF_Font with source font)
169 string fontname = Evaluator::gPath.FindFile(mFontFileName);
170 mTTF_Font = FontCache::FontCacheManager::GetInstance().GetFont(mCacheKey, fontname);
171
172 //Translate PColor to SDLcolor for direct use in rendering.
173 PColor* fgColor = GetFontColorPtr();
174 PColor* bgColor = GetBackgroundColorPtr();
175 if(fgColor) mSDL_FGColor = SDLUtility::PColorToSDLColor(*fgColor);
176 if(bgColor) mSDL_BGColor = SDLUtility::PColorToSDLColor(*bgColor);
177
178}
virtual int GetFontStyle() const
Definition PFont.h:84
virtual std::string GetFontFileName() const
Definition PFont.h:83
virtual bool GetAntiAliased() const
Definition PFont.h:88
bool mAntiAliased
Definition PFont.h:110
virtual int GetFontSize() const
Definition PFont.h:85

References FontCache::FontCacheKey::filename, PEBLPath::FindFile(), PFont::GetAntiAliased(), PFont::GetBackgroundColorPtr(), FontCache::FontCacheManager::GetFont(), PFont::GetFontColorPtr(), PFont::GetFontFileName(), PFont::GetFontSize(), PFont::GetFontStyle(), FontCache::FontCacheManager::GetInstance(), Evaluator::gPath, PFont::mAntiAliased, PFont::mFontFileName, PFont::mFontSize, PFont::mFontStyle, SDLUtility::PColorToSDLColor(), FontCache::FontCacheKey::size, and FontCache::FontCacheKey::style.

◆ ~PlatformFont() [1/3]

PlatformFont::~PlatformFont ( )
virtual

Copy constructor.

Fonts are not geting cleaned up, I think. Standard destructor of PlatformFont

Definition at line 183 of file sdl/PlatformFont.cpp.

184{
185 // Release font from cache (decrements ref count, closes if ref count reaches 0)
187 mTTF_Font = NULL;
188
189 // mBuffer is always nullptr with FontCache, but we keep it for binary compatibility
190 // free(nullptr) is safe and does nothing
191 free(mBuffer);
192 mBuffer = NULL;
193}
#define NULL
Definition BinReloc.cpp:317
void ReleaseFont(const FontCacheKey &key)
Definition FontCache.cpp:79

References FontCache::FontCacheManager::GetInstance(), NULL, and FontCache::FontCacheManager::ReleaseFont().

◆ PlatformFont() [4/9]

PlatformFont::PlatformFont ( const std::string &  filename)

◆ PlatformFont() [5/9]

PlatformFont::PlatformFont ( const std::string &  filename,
int  style,
int  size,
PColor  fgcolor,
PColor  bgcolor,
bool  aa 
)

◆ PlatformFont() [6/9]

PlatformFont::PlatformFont ( PlatformFont font)

◆ ~PlatformFont() [2/3]

virtual PlatformFont::~PlatformFont ( )
virtual

Copy constructor.

◆ PlatformFont() [7/9]

PlatformFont::PlatformFont ( const std::string &  filename)

◆ PlatformFont() [8/9]

PlatformFont::PlatformFont ( const std::string &  filename,
int  style,
int  size,
PColor  fgcolor,
PColor  bgcolor,
bool  aa 
)

◆ PlatformFont() [9/9]

PlatformFont::PlatformFont ( PlatformFont font)

◆ ~PlatformFont() [3/3]

virtual PlatformFont::~PlatformFont ( )
virtual

Member Function Documentation

◆ ClearChanged()

void PlatformFont::ClearChanged ( )

Clear all changed flags.

Definition at line 290 of file sdl/PlatformFont.cpp.

291{
292 mChanged = false;
293 PColor* fgColor = GetFontColorPtr();
294 PColor* bgColor = GetBackgroundColorPtr();
295 if(fgColor) fgColor->ClearChanged();
296 if(bgColor) bgColor->ClearChanged();
297}
void ClearChanged()
Definition PColor.h:79

References PColor::ClearChanged(), PFont::GetBackgroundColorPtr(), and PFont::GetFontColorPtr().

Referenced by PlatformLabel::Draw(), and PlatformTextBox::Draw().

◆ GetPosition() [1/2]

unsigned int PlatformFont::GetPosition ( const std::string &  text,
unsigned int  x 
)

Definition at line 562 of file sdl/PlatformFont.cpp.

563{
564
565 //Start at 0 and check until the width of the rendered text is bigger than x
566
567 unsigned int lastcutoff = 0;
568 unsigned int lastwidth = 0;
569
570 unsigned int cutoff = 1;
571
572 std::string::const_iterator start;
573 std::string::const_iterator end;
574
575 start = text.begin();
576 end = start+1;
577
578 while(cutoff < text.size())
579 {
580
581 //the bytes don't align with the glyphs. Be sure we only
582 //permit the cutoff to occur between valid utf8 glyphs.
583 while(!PEBLUtility::is_utf8(text.substr(0,cutoff)))
584 {
585 //end++;
586 cutoff++;
587 }
588 //If the width of the rendered text is larger than the x argument,
589 unsigned int width = GetTextWidth(text.substr(0,cutoff));
590 if(width > x)
591 {
592 //width is greater that the x position. now, round to the
593 //best cutoff.
594 double prop = (x-lastwidth)/(width-lastwidth);
595 if(prop>.5)
596 {
597 return cutoff;
598 }
599 else
600 {
601 return lastcutoff;
602 }
603
604
605 }
606 lastcutoff = cutoff;
607 lastwidth = width;
608
609 cutoff++;
610 //end++;
611 }
612
613 //If we make it this far, we have run out of letters, so return the last character.
614
615 return (unsigned int)(text.size());
616}
unsigned int GetTextWidth(const std::string &text)
bool is_utf8(const std::string str)

References GetTextWidth(), and PEBLUtility::is_utf8().

Referenced by PlatformTextBox::FindCursorPosition().

◆ GetPosition() [2/2]

unsigned int PlatformFont::GetPosition ( const std::string &  text,
unsigned int  x 
)

◆ GetTextHeight() [1/3]

unsigned int PlatformFont::GetTextHeight ( const std::string &  text)

Definition at line 547 of file sdl/PlatformFont.cpp.

548{
549 int height, width;
550
551 if(PEBLUtility::is_utf8(toBeRendered))
552 TTF_SizeUTF8(mTTF_Font,toBeRendered.c_str(),&width,&height); // should work on all utf8
553 else
554 TTF_SizeText(mTTF_Font,toBeRendered.c_str(),&width,&height); //breaks on non-ascii text
555
556 unsigned int uheight = (unsigned int)height;
557 return uheight;
558}

References PEBLUtility::is_utf8().

Referenced by PlatformTextBox::FindCursorPosition(), PlatformTextBox::HandleKeyPress(), and PlatformTextBox::RenderText().

◆ GetTextHeight() [2/3]

unsigned int PlatformFont::GetTextHeight ( const std::string &  text)

◆ GetTextHeight() [3/3]

unsigned int PlatformFont::GetTextHeight ( const std::string &  text)

◆ GetTextWidth() [1/3]

unsigned int PlatformFont::GetTextWidth ( const std::string &  text)

Definition at line 517 of file sdl/PlatformFont.cpp.

518{
519 int height, width;
520 std::string toBeRendered = StripText(text.c_str());
521
522 // Auto-detect script and set direction for accurate width measurement
523 std::string script = PEBLUtility::DetectScript(toBeRendered);
524 const char* script_cstr = script_to_cstr(script);
525
526 TTF_SetFontScriptName(mTTF_Font, script_cstr);
527 if (PEBLUtility::IsRTLScript(script)) {
528 TTF_SetFontDirection(mTTF_Font, TTF_DIRECTION_RTL);
529 } else {
530 TTF_SetFontDirection(mTTF_Font, TTF_DIRECTION_LTR);
531 }
532
533 if(PEBLUtility::is_utf8(toBeRendered))
534 {
535
536 TTF_SizeUTF8(mTTF_Font,toBeRendered.c_str(),&width,&height); // should work on all utf8
537 }
538 else
539 TTF_SizeText(mTTF_Font,toBeRendered.c_str(),&width,&height); //breaks on non-ascii text
540
541 // TTF_SizeUNICODE(mTTF_Font,toBeRendered.c_str(),&width,&height); //requires conversion to Uint16 array.
542
543 unsigned int uwidth = (unsigned int)width;
544 return uwidth;
545}
std::string DetectScript(const std::string &text)
bool IsRTLScript(const std::string &script)

References PEBLUtility::DetectScript(), PEBLUtility::is_utf8(), and PEBLUtility::IsRTLScript().

Referenced by PlatformTextBox::FindCursorPosition(), GetPosition(), PlatformTextBox::HandleKeyPress(), and PlatformTextBox::RenderText().

◆ GetTextWidth() [2/3]

unsigned int PlatformFont::GetTextWidth ( const std::string &  text)

◆ GetTextWidth() [3/3]

unsigned int PlatformFont::GetTextWidth ( const std::string &  text)

◆ GetTTFFont()

TTF_Font * PlatformFont::GetTTFFont ( ) const
inline

Definition at line 78 of file sdl/PlatformFont.h.

78{ return mTTF_Font; }

Referenced by PlatformLabel::RenderText(), and PlatformTextBox::RenderText().

◆ HasChanged()

bool PlatformFont::HasChanged ( )

Check if font or its colors have changed.

Definition at line 270 of file sdl/PlatformFont.cpp.

271{
272 // Check if colors changed and update SDL cache if needed
273 PColor* fgColor = GetFontColorPtr();
274 PColor* bgColor = GetBackgroundColorPtr();
275
276 if(fgColor && fgColor->HasChanged())
277 {
279 return true;
280 }
281 if(bgColor && bgColor->HasChanged())
282 {
284 return true;
285 }
286 return mChanged;
287}
bool HasChanged() const
Change detection for nested property modifications.
Definition PColor.h:78
void UpdateSDLColors()
Update SDL color cache from PColor objects.

References PFont::GetBackgroundColorPtr(), PFont::GetFontColorPtr(), PColor::HasChanged(), and UpdateSDLColors().

◆ ObjectName() [1/2]

virtual std::string PlatformFont::ObjectName ( ) const
inlinevirtual

Reimplemented from PFont.

Definition at line 75 of file sdl/PlatformFont.h.

75{return "Platform Font";};

◆ ObjectName() [2/2]

virtual std::string PlatformFont::ObjectName ( ) const
inlinevirtual

Reimplemented from PFont.

Definition at line 87 of file PlatformFont_RTL.h.

87{return "Platform Font";};

◆ RenderText() [1/2]

SDL_Surface * PlatformFont::RenderText ( const std::string &  text)

This takes care of all the busy work of rendering the text.

Definition at line 318 of file sdl/PlatformFont.cpp.

319{
320
321 int maxchars = 1000;
322#if 0
323 cerr << "About to render text [" << text << "] with font " << *this << endl;
324
325 int i = 0;
326 while(i < text.length())
327 {
328
329 cerr << "[" << text[i] << "|" << (unsigned int)(text[i]) << "]";
330 i++;
331 }
332
333 cerr << endl;
334#endif
335
336 //If there is no text, return a null surface.
337 // if(text=="") return NULL; don't do this;render anyway because this causes updates
338 // to blank labels to fail in 2.0
339
340
341
342
343 //Get a temporary pointer that we return
344 std::string toBeRendered = StripText(text);
345 SDL_Surface * tmpSurface = NULL;
346
347 if(toBeRendered.length()>maxchars)
348 {
349 toBeRendered=toBeRendered.substr(0,maxchars);
350 }
351 //The text renderer doesn't like to render empty text.
352 if(toBeRendered.length() == 0) toBeRendered = " ";
353
354 // Auto-detect script and set both script and direction for proper HarfBuzz shaping
355 std::string script = PEBLUtility::DetectScript(toBeRendered);
356 const char* script_cstr = script_to_cstr(script);
357
358 // Set the script for proper shaping (ligatures, contextual forms, etc.)
359 TTF_SetFontScriptName(mTTF_Font, script_cstr);
360
361 // Set direction based on script
362 if (PEBLUtility::IsRTLScript(script)) {
363 TTF_SetFontDirection(mTTF_Font, TTF_DIRECTION_RTL);
364 } else {
365 TTF_SetFontDirection(mTTF_Font, TTF_DIRECTION_LTR);
366 }
367
368 //Using the RenderUTF8 stuff below has a hard time with 'foreign' characters; possibly because
369 //the toberendered needs to be converted to UTF-8????
370
371 //Note: Modern Emscripten (2.0+) DOES support TTF_RenderUTF8 functions
372 if(mAntiAliased)
373 {
374 // If background alpha is < 255, we need transparency support
375 // SDL's Shaded mode doesn't support alpha properly, so use Blended + manual background
376 if(mSDL_BGColor.a < 255)
377 {
378 // Use Blended mode for text (transparent background)
379 SDL_Surface* textSurface = NULL;
380 if(PEBLUtility::is_utf8(toBeRendered))
381 {
382 textSurface = TTF_RenderUTF8_Blended(mTTF_Font, toBeRendered.c_str(), mSDL_FGColor);
383 } else {
384 textSurface = TTF_RenderText_Blended(mTTF_Font, toBeRendered.c_str(), mSDL_FGColor);
385 }
386
387 if(textSurface)
388 {
389 // Create a new RGBA surface for compositing
390 tmpSurface = SDL_CreateRGBSurface(0, textSurface->w, textSurface->h, 32,
391 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
392 if(tmpSurface)
393 {
394 // Fill with background color INCLUDING its alpha value
395 // This encodes transparency directly in the pixel data
396 SDL_FillRect(tmpSurface, NULL, SDL_MapRGBA(tmpSurface->format,
397 mSDL_BGColor.r, mSDL_BGColor.g,
398 mSDL_BGColor.b, mSDL_BGColor.a));
399 // Blit the opaque text on top (will replace background pixels)
400 SDL_BlitSurface(textSurface, NULL, tmpSurface, NULL);
401 }
402 SDL_FreeSurface(textSurface);
403 }
404 }
405 else
406 {
407 // Use Shaded mode for opaque backgrounds (more efficient)
408 if(PEBLUtility::is_utf8(toBeRendered))
409 {
410 tmpSurface = TTF_RenderUTF8_Shaded(mTTF_Font, toBeRendered.c_str(), mSDL_FGColor, mSDL_BGColor);
411 } else {
412 tmpSurface = TTF_RenderText_Shaded(mTTF_Font, toBeRendered.c_str(), mSDL_FGColor, mSDL_BGColor);
413 }
414 }
415
416 }
417 else
418 {
419 // tmpSurface = TTF_RenderText_Blended(mTTF_Font,toBeRendered.c_str(), mSDL_FGColor);
420 if(PEBLUtility::is_utf8(toBeRendered) )
421 {
422 tmpSurface = TTF_RenderUTF8_Blended(mTTF_Font,toBeRendered.c_str(), mSDL_FGColor);
423 }
424 else
425 {
426 tmpSurface = TTF_RenderText_Blended(mTTF_Font, toBeRendered.c_str(), mSDL_FGColor);
427 }
428 }
429
430 //
431 //TTF_RenderText_Blended(
432 //TTF_Font *font, // This is the TTF_Font to use.
433 //char *cstr, // This is the text to render.
434 // SDL_Color &clr, // This is the color to use.
435 // );
436
437
438
439
440 if(tmpSurface)
441 {
442 return tmpSurface;
443 //we need to copy the surface to the texture, render it, and return I think.
444
445 }
446 else
447 {
448 string message = "Unable to render text [" + toBeRendered + "] in PlatformFont::RenderText. Attempting to render '' instead\n";
449 PError::SignalWarning(message);
450
451 //We have a problem, probably because we are trying to render garbage. Let's try to render "" instead, and
452 //signal a waring. If there is still an error, we will signal a fatal error.
453 std::string tmp = "";
454 tmpSurface = TTF_RenderText_Blended(mTTF_Font, tmp.c_str(), mSDL_FGColor);
455
456 if(tmpSurface)
457 {
458 return tmpSurface;
459 }
460 else
461 {
462 string message = "Unable to render text in PlatformFont::RenderText";
464 }
465 }
466 return NULL;
467}
void SignalWarning(const std::string &message)
Definition PError.cpp:119

References PEBLUtility::DetectScript(), PEBLUtility::is_utf8(), PEBLUtility::IsRTLScript(), PFont::mAntiAliased, NULL, PError::SignalFatalError(), and PError::SignalWarning().

Referenced by PlatformLabel::RenderText(), and PlatformTextBox::RenderText().

◆ RenderText() [2/2]

SDL_Surface * PlatformFont::RenderText ( const std::wstring &  text)

Definition at line 626 of file PlatformFont_RTL.cpp.

627{
628
629 int maxchars = 1000;
630#if 0
631 cerr << "About to render text [" << text << "] with font " << *this << endl;
632
633 int i = 0;
634 while(i < text.length())
635 {
636
637 cerr << "[" << text[i] << "|" << (unsigned int)(text[i]) << "]";
638 i++;
639 }
640
641 cerr << endl;
642#endif
643
644
645
646
647 //If there is no text, return a null surface.
648 // if(text=="") return NULL; don't do this;render anyway because this causes updates
649 // to blank labels to fail in 2.0
650
651
652 //Get a temporary pointer that we return
653 std::string toBeRendered = StripText(text);
654 SDL_Surface * tmpSurface = NULL;
655
656 if(toBeRendered.length()>maxchars)
657 {
658 toBeRendered=toBeRendered.substr(0,maxchars);
659 }
660 //The text renderer doesn't like to render empty text.
661 if(toBeRendered.length() == 0) toBeRendered = " ";
662
663 //Using the RenderUTF8 stuff below has a hard time with 'foreign' characters; possibly because
664 //the toberendered needs to be converted to UTF-8????
665 mTexture = DrawTexture(toBeRendered);
666 return tmpSurface;
667}

References NULL.

◆ RenderTexture()

SDL_Texture * PlatformFont::RenderTexture ( const std::wstring &  text)

This takes care of all the busy work of rendering the text.

◆ SendToStream() [1/3]

std::ostream & PlatformFont::SendToStream ( std::ostream &  out) const
protectedvirtual

This sends the font descriptions to the specified stream.

Reimplemented from PFont.

Definition at line 619 of file sdl/PlatformFont.cpp.

620{
621 out << "<SDL-Specific Font>" << std::flush;
622 return out;
623}

◆ SendToStream() [2/3]

virtual std::ostream & PlatformFont::SendToStream ( std::ostream &  out) const
protectedvirtual

This sends the font descriptions to the specified stream.

Reimplemented from PFont.

◆ SendToStream() [3/3]

virtual std::ostream & PlatformFont::SendToStream ( std::ostream &  out) const
protectedvirtual

This sends the font descriptions to the specified stream.

Reimplemented from PFont.

◆ SetBackgroundColor() [1/2]

void PlatformFont::SetBackgroundColor ( PColor  color)
virtual

Set*Color needs to be overridden because it doesn't change the SDL_Color data.

Reimplemented from PFont.

Definition at line 212 of file sdl/PlatformFont.cpp.

213{
214 //Chain up to parent method
216
217 //Set child member data - retrieve from property map
218 PColor* bgColor = GetBackgroundColorPtr();
219 if(bgColor) mSDL_BGColor = SDLUtility::PColorToSDLColor(*bgColor);
220 mChanged = true; // Mark font as changed to trigger re-rendering
221}
virtual void SetBackgroundColor(PColor color)
Definition PFont.cpp:315

References PFont::GetBackgroundColorPtr(), SDLUtility::PColorToSDLColor(), and PFont::SetBackgroundColor().

◆ SetBackgroundColor() [2/2]

virtual void PlatformFont::SetBackgroundColor ( PColor  color)
virtual

Reimplemented from PFont.

◆ SetFontColor() [1/2]

void PlatformFont::SetFontColor ( PColor  color)
virtual

Set*Color needs to be overridden because it doesn't change the SDL_Color data.

Reimplemented from PFont.

Definition at line 198 of file sdl/PlatformFont.cpp.

199{
200 //Chain up to parent method
201 PFont::SetFontColor(color);
202
203 //Set child member data - retrieve from property map
204 PColor* fgColor = GetFontColorPtr();
205 if(fgColor) mSDL_FGColor = SDLUtility::PColorToSDLColor(*fgColor);
206 mChanged = true; // Mark font as changed to trigger re-rendering
207}
virtual void SetFontColor(PColor color)
Definition PFont.cpp:303

References PFont::GetFontColorPtr(), SDLUtility::PColorToSDLColor(), and PFont::SetFontColor().

◆ SetFontColor() [2/2]

virtual void PlatformFont::SetFontColor ( PColor  color)
virtual

Reimplemented from PFont.

◆ SetFontSize()

void PlatformFont::SetFontSize ( const int  size)
virtual

Override SetFontSize to update TTF font and mark as changed.

Reimplemented from PFont.

Definition at line 226 of file sdl/PlatformFont.cpp.

227{
228 // If size unchanged, nothing to do
229 if (size == mFontSize) return;
230
231 //Chain up to parent method
232 PFont::SetFontSize(size);
233
234 // Release old cached font
236
237 // Update cache key with new size
238 mCacheKey.size = size;
239
240 // Get new cached font with new size
241 string fontname = Evaluator::gPath.FindFile(mFontFileName);
242 mTTF_Font = FontCache::FontCacheManager::GetInstance().GetFont(mCacheKey, fontname);
243
244 mChanged = true; // Mark font as changed to trigger re-rendering
245}
virtual void SetFontSize(const int size)
Definition PFont.cpp:265

References PEBLPath::FindFile(), FontCache::FontCacheManager::GetFont(), FontCache::FontCacheManager::GetInstance(), Evaluator::gPath, PFont::mFontFileName, PFont::mFontSize, FontCache::FontCacheManager::ReleaseFont(), PFont::SetFontSize(), and FontCache::FontCacheKey::size.

◆ SetFontStyle()

void PlatformFont::SetFontStyle ( const int  style)
virtual

Override SetFontStyle to update TTF font and mark as changed.

Reimplemented from PFont.

Definition at line 248 of file sdl/PlatformFont.cpp.

249{
250 // If style unchanged, nothing to do
251 if (style == mFontStyle) return;
252
253 //Chain up to parent method
254 PFont::SetFontStyle(style);
255
256 // Release old cached font
258
259 // Update cache key with new style
260 mCacheKey.style = style;
261
262 // Get new cached font with new style
263 string fontname = Evaluator::gPath.FindFile(mFontFileName);
264 mTTF_Font = FontCache::FontCacheManager::GetInstance().GetFont(mCacheKey, fontname);
265
266 mChanged = true; // Mark font as changed to trigger re-rendering
267}
virtual void SetFontStyle(const int style)
Definition PFont.cpp:257

References PEBLPath::FindFile(), FontCache::FontCacheManager::GetFont(), FontCache::FontCacheManager::GetInstance(), Evaluator::gPath, PFont::mFontFileName, PFont::mFontStyle, FontCache::FontCacheManager::ReleaseFont(), PFont::SetFontStyle(), and FontCache::FontCacheKey::style.

◆ UpdateSDLColors()

void PlatformFont::UpdateSDLColors ( )

Update SDL color cache from PColor objects.

Definition at line 300 of file sdl/PlatformFont.cpp.

301{
302 PColor* fgColor = GetFontColorPtr();
303 PColor* bgColor = GetBackgroundColorPtr();
304
305 if(fgColor)
306 {
307 mSDL_FGColor = SDLUtility::PColorToSDLColor(*fgColor);
308 }
309 if(bgColor)
310 {
311 mSDL_BGColor = SDLUtility::PColorToSDLColor(*bgColor);
312 }
313 mChanged = true; // Mark font as changed so label re-renders
314}

References PFont::GetBackgroundColorPtr(), PFont::GetFontColorPtr(), and SDLUtility::PColorToSDLColor().

Referenced by HasChanged().


The documentation for this class was generated from the following files: