28#include "../../objects/PFont.h"
29#include "../../objects/PColor.h"
31#include "../../utility/PEBLUtility.h"
32#include "../../utility/PEBLPath.h"
33#include "../../utility/PError.h"
36#include "../../base/Evaluator-es.h"
38#include "../../base/Evaluator.h"
43#include <harfbuzz/hb.h>
44#include <harfbuzz/hb-ft.h>
48#if defined(PEBL_OSX) | defined(PEBL_EMSCRIPTEN)
67static const char* script_to_cstr(
const std::string & script) {
68 if (script.empty())
return NULL;
69 return script.c_str();
78 long unsigned int size;
79 if(fseek(*file, 0, SEEK_END) == -1){
return -1; }
81 fseek(*file, 0, SEEK_SET);
88 char *buffer = (
char*)malloc(fileSize + 1);
89 fread(buffer,
sizeof(
char),fileSize,*file);
95 unsigned long int fileSize;
96 FILE *file = fopen(path,
"rb");
118 PFont(filename, style, size, fgcolor, bgcolor, aa),
137 printf(
"Oh My Goodness, an error : [%s]\n", TTF_GetError());
238 mCacheKey.
size = size;
260 mCacheKey.
style = style;
323 cerr <<
"About to render text [" << text <<
"] with font " << *
this << endl;
326 while(i < text.length())
329 cerr <<
"[" << text[i] <<
"|" << (
unsigned int)(text[i]) <<
"]";
344 std::string toBeRendered = StripText(text);
345 SDL_Surface * tmpSurface =
NULL;
347 if(toBeRendered.length()>maxchars)
349 toBeRendered=toBeRendered.substr(0,maxchars);
352 if(toBeRendered.length() == 0) toBeRendered =
" ";
356 const char* script_cstr = script_to_cstr(script);
359 TTF_SetFontScriptName(mTTF_Font, script_cstr);
363 TTF_SetFontDirection(mTTF_Font, TTF_DIRECTION_RTL);
365 TTF_SetFontDirection(mTTF_Font, TTF_DIRECTION_LTR);
376 if(mSDL_BGColor.a < 255)
379 SDL_Surface* textSurface =
NULL;
382 textSurface = TTF_RenderUTF8_Blended(mTTF_Font, toBeRendered.c_str(), mSDL_FGColor);
384 textSurface = TTF_RenderText_Blended(mTTF_Font, toBeRendered.c_str(), mSDL_FGColor);
390 tmpSurface = SDL_CreateRGBSurface(0, textSurface->w, textSurface->h, 32,
391 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
396 SDL_FillRect(tmpSurface,
NULL, SDL_MapRGBA(tmpSurface->format,
397 mSDL_BGColor.r, mSDL_BGColor.g,
398 mSDL_BGColor.b, mSDL_BGColor.a));
400 SDL_BlitSurface(textSurface,
NULL, tmpSurface,
NULL);
402 SDL_FreeSurface(textSurface);
410 tmpSurface = TTF_RenderUTF8_Shaded(mTTF_Font, toBeRendered.c_str(), mSDL_FGColor, mSDL_BGColor);
412 tmpSurface = TTF_RenderText_Shaded(mTTF_Font, toBeRendered.c_str(), mSDL_FGColor, mSDL_BGColor);
422 tmpSurface = TTF_RenderUTF8_Blended(mTTF_Font,toBeRendered.c_str(), mSDL_FGColor);
426 tmpSurface = TTF_RenderText_Blended(mTTF_Font, toBeRendered.c_str(), mSDL_FGColor);
448 string message =
"Unable to render text [" + toBeRendered +
"] in PlatformFont::RenderText. Attempting to render '' instead\n";
453 std::string tmp =
"";
454 tmpSurface = TTF_RenderText_Blended(mTTF_Font, tmp.c_str(), mSDL_FGColor);
462 string message =
"Unable to render text in PlatformFont::RenderText";
471std::string PlatformFont::StripText(
const std::string & text)
477 std::string toBeRendered;
479 for(
unsigned int i = 0; i < text.size(); i++)
490 else if(text[i] == 9)
497 int x = 8*(((int)(toBeRendered.length())+1) /8 + 1 );
498 int diff = (int)x-(
int)(toBeRendered.length());
500 for(
int j = 0; j < diff; j++)
502 toBeRendered.push_back(tmp[0]);
508 toBeRendered.push_back(text[i]);
520 std::string toBeRendered = StripText(text.c_str());
524 const char* script_cstr = script_to_cstr(script);
526 TTF_SetFontScriptName(mTTF_Font, script_cstr);
528 TTF_SetFontDirection(mTTF_Font, TTF_DIRECTION_RTL);
530 TTF_SetFontDirection(mTTF_Font, TTF_DIRECTION_LTR);
536 TTF_SizeUTF8(mTTF_Font,toBeRendered.c_str(),&width,&height);
539 TTF_SizeText(mTTF_Font,toBeRendered.c_str(),&width,&height);
543 unsigned int uwidth = (
unsigned int)width;
552 TTF_SizeUTF8(mTTF_Font,toBeRendered.c_str(),&width,&height);
554 TTF_SizeText(mTTF_Font,toBeRendered.c_str(),&width,&height);
556 unsigned int uheight = (
unsigned int)height;
567 unsigned int lastcutoff = 0;
568 unsigned int lastwidth = 0;
570 unsigned int cutoff = 1;
572 std::string::const_iterator start;
573 std::string::const_iterator end;
575 start = text.begin();
578 while(cutoff < text.size())
589 unsigned int width =
GetTextWidth(text.substr(0,cutoff));
594 double prop = (x-lastwidth)/(width-lastwidth);
615 return (
unsigned int)(text.size());
621 out <<
"<SDL-Specific Font>" << std::flush;
static FontCacheManager & GetInstance()
Get singleton instance.
void ReleaseFont(const FontCacheKey &key)
TTF_Font * GetFont(const FontCacheKey &key, const std::string &full_path)
bool HasChanged() const
Change detection for nested property modifications.
std::string FindFile(const string &filename)
virtual void SetFontColor(PColor color)
PColor * GetBackgroundColorPtr() const
PColor * GetFontColorPtr() const
virtual void SetFontStyle(const int style)
virtual int GetFontStyle() const
virtual void SetBackgroundColor(PColor color)
virtual void SetFontSize(const int size)
virtual std::string GetFontFileName() const
std::string mFontFileName
virtual bool GetAntiAliased() const
virtual int GetFontSize() const
bool is_utf8(const std::string str)
std::string DetectScript(const std::string &text)
bool IsRTLScript(const std::string &script)
void SignalWarning(const std::string &message)
void SignalFatalError(const std::string &message)
SDL_Color PColorToSDLColor(PColor pcolor)
This converts between a PColor and an SDL color.