- bugfixes for font display:

- workaround for a bug in freetype in some cases
  - better handling on different resolutions
  - new commandline option to override screen resolution settings
This commit is contained in:
Mark Vejvoda
2011-06-11 08:52:49 +00:00
parent 4aca4af404
commit f1aa231e4b
9 changed files with 314 additions and 68 deletions

View File

@@ -50,8 +50,10 @@ class TextRenderer3DGl: public TextRenderer3D {
private:
Font3DGl *font;
bool rendering;
int currentFTGLErrorCount;
void internalRender(const string &text, float x, float y, bool centered, Vec3f *color);
void specialFTGLErrorCheckWorkaround(string text);
public:
TextRenderer3DGl();

View File

@@ -185,6 +185,29 @@ void TextFTGL::Render(const char* str, const int len) {
float TextFTGL::Advance(const char* str, const int len) {
return ftFont->Advance(str, len);
//FTBBox box = ftFont->BBox(str);
//float urx = box.Upper().X();
//float llx = box.Lower().X();
//float llx, lly, llz, urx, ury, urz;
//ftFont->BBox(str, llx, lly, llz, urx, ury, urz);
//Short_t halign = fTextAlign/10;
//Short_t valign = fTextAlign - 10*halign;
//Float_t dx = 0, dy = 0;
// switch (halign) {
// case 1 : dx = 0 ; break;
// case 2 : dx = -urx/2; break;
// case 3 : dx = -urx ; break;
// }
// switch (valign) {
// case 1 : dy = 0 ; break;
// case 2 : dy = -ury/2; break;
// case 3 : dy = -ury ; break;
// }
//printf("For str [%s] advance = %f, urx = %f, llx = %f\n",str, ftFont->Advance(str, len),urx,llx);
//return urx;
}
float TextFTGL::LineHeight(const char* str, const int len) {

View File

@@ -351,6 +351,7 @@ void TextRenderer2DGl::end() {
TextRenderer3DGl::TextRenderer3DGl() : TextRenderer3D() {
rendering= false;
this->font = NULL;
currentFTGLErrorCount = 0;
}
TextRenderer3DGl::~TextRenderer3DGl() {
@@ -367,70 +368,118 @@ void TextRenderer3DGl::begin(Font3D *font) {
//load color
glPushAttrib(GL_TRANSFORM_BIT);
assertGl();
//assertGl();
}
void TextRenderer3DGl::render(const string &text, float x, float y, bool centered, Vec3f *color) {
assert(rendering);
string renderText = text;
if(Font::fontIsMultibyte == true) {
if(font->getTextHandler() != NULL) {
if(Font::fontIsRightToLeft == true) {
//printf("\n\n#A [%s]\n",renderText.c_str());
//bool isRLM = utf8::starts_with_rlm(text.begin(), text.end() + text.size());
if(text.empty() == false) {
string renderText = text;
if(Font::fontIsMultibyte == true) {
if(font->getTextHandler() != NULL) {
if(Font::fontIsRightToLeft == true) {
//printf("\n\n#A [%s]\n",renderText.c_str());
//bool isRLM = utf8::starts_with_rlm(text.begin(), text.end() + text.size());
//printf("\n\nORIGINAL TEXT [%s] isRLM = %d\n\n",text.c_str(),isRLM);
//for(int i = 0; i < renderText.size(); ++i) {
// printf("i = %d c [%c][%d][%X]\n",i,renderText[i],renderText[i],renderText[i]);
//}
//if(isRLM == true) {
if(is_string_all_ascii(renderText) == false) {
strrev_utf8(renderText);
//printf("\n\nORIGINAL TEXT [%s] isRLM = %d\n\n",text.c_str(),isRLM);
//for(int i = 0; i < renderText.size(); ++i) {
// printf("i = %d c [%c][%d][%X]\n",i,renderText[i],renderText[i],renderText[i]);
//}
//if(isRLM == true) {
if(is_string_all_ascii(renderText) == false) {
strrev_utf8(renderText);
}
}
}
}
}
internalRender(renderText, x, y, centered, color);
internalRender(renderText, x, y, centered, color);
}
}
void TextRenderer3DGl::specialFTGLErrorCheckWorkaround(string text) {
GLenum error = glGetError();
if(error) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\n\nIn [%s::%s Line: %d] error = %d for text [%s]\n\n",__FILE__,__FUNCTION__,__LINE__,error,text.c_str());
if(currentFTGLErrorCount > 0) {
printf("error = %d for text [%s]\n",error,text.c_str());
assertGlWithErrorNumber(error);
}
currentFTGLErrorCount++;
}
}
void TextRenderer3DGl::internalRender(const string &text, float x, float y, bool centered, Vec3f *color) {
//assert(rendering);
if(color != NULL) {
//assertGl();
glPushAttrib(GL_CURRENT_BIT);
//assertGl();
glColor3fv(color->ptr());
//assertGl();
}
const unsigned char *utext= NULL;
assertGl();
//assertGl();
glMatrixMode(GL_MODELVIEW);
//assertGl();
glPushMatrix();
//assertGl();
glLoadIdentity();
//assertGl();
//glPushAttrib(GL_POLYGON_BIT);
int size = font->getSize();
//float scale= size / 15.f;
float scale= 1.0f;
//float scale= size;
Vec3f translatePos;
FontMetrics *metrics= font->getMetrics();
if(font->getTextHandler() != NULL) {
//centered = false;
if(centered) {
translatePos.x = x - scale * font->getTextHandler()->Advance(text.c_str()) / 2.f;
translatePos.y = y - scale * font->getTextHandler()->LineHeight(text.c_str()) / font->getYOffsetFactor();
//printf("3d text to center [%s] advance = %f, x = %f\n",text.c_str(),font->getTextHandler()->Advance(text.c_str()), x);
//printf("3d text to center [%s] lineheight = %f, y = %f\n",text.c_str(),font->getTextHandler()->LineHeight(text.c_str()), y);
// translatePos.x = x - scale * font->getTextHandler()->Advance(text.c_str()) / 2.f;
// translatePos.y = y - scale * font->getTextHandler()->LineHeight(text.c_str()) / font->getYOffsetFactor();
//assertGl();
translatePos.x = x - (font->getTextHandler()->Advance(text.c_str()) / 2.f);
//assertGl();
//translatePos.y = y - (font->getTextHandler()->LineHeight(text.c_str()) / font->getYOffsetFactor());
translatePos.y = y - (font->getTextHandler()->LineHeight(text.c_str()) / 2.f);
//assertGl();
translatePos.z = 0;
}
else {
translatePos.x = x-scale;
translatePos.y = y-scale;
//printf("3d text [%s] advance = %f, x = %f\n",text.c_str(),font->getTextHandler()->Advance(text.c_str()), x);
// translatePos.x = x-scale;
// translatePos.y = y-scale;
translatePos.x = x;
translatePos.y = y;
translatePos.z = 0;
}
}
else {
float scale= 1.0f;
//float scale= size;
utext= reinterpret_cast<const unsigned char*>(text.c_str());
if(centered) {
//glTranslatef(x-scale*metrics->getTextWidth(text)/2.f, y-scale*metrics->getHeight()/2.f, 0);
@@ -446,24 +495,35 @@ void TextRenderer3DGl::internalRender(const string &text, float x, float y, boo
}
}
float scaleX = 0.65;
float scaleY = 0.75;
float scaleZ = 1.0;
//float scaleX = 0.65;
//float scaleY = 0.75;
//float scaleZ = 1.0;
//float scaleX = 1;
//float scaleY = 1;
//float scaleZ = 1;
float yScaleFactor = (metrics->getHeight() * (1.0 - scaleY));
translatePos.y += yScaleFactor;
//float yScaleFactor = (metrics->getHeight() * (1.0 - scaleY));
//translatePos.y += yScaleFactor;
//assertGl();
glTranslatef(translatePos.x, translatePos.y, translatePos.z);
glScalef(scaleX, scaleY, scaleZ);
//font->getTextHandler()->Render(text.c_str());
//assertGl();
//glScalef(scaleX, scaleY, scaleZ);
//assertGl();
// font->getTextHandler()->Render(text.c_str());
// specialFTGLErrorCheckWorkaround(text);
if(font->getTextHandler() != NULL) {
if(text.find("\n") == text.npos && text.find("\t") == text.npos) {
//assertGl();
font->getTextHandler()->Render(text.c_str());
specialFTGLErrorCheckWorkaround(text);
}
else {
int line=0;
@@ -479,18 +539,12 @@ void TextRenderer3DGl::internalRender(const string &text, float x, float y, boo
case '\t':
parts.push_back(szBuf);
lastCharacterWasSpecial = true;
//rasterPos= Vec2f((rasterPos.x / size + 3.f) * size, y-(size + 1.f) * line);
//glRasterPos2f(rasterPos.x, rasterPos.y);
break;
case '\n':
parts.push_back(szBuf);
lastCharacterWasSpecial = true;
//line++;
//rasterPos= Vec2f(static_cast<float>(x), y - (fontFTGL->LineHeight(text.c_str()) * 2.f) * line);
//glRasterPos2f(rasterPos.x, rasterPos.y);
break;
default:
//glCallList(font->getHandle()+utext[i]);
if(lastCharacterWasSpecial == true) {
parts.push_back(szBuf);
}
@@ -512,18 +566,30 @@ void TextRenderer3DGl::internalRender(const string &text, float x, float y, boo
case '\n':
{
line++;
//assertGl();
float yLineValue = font->getTextHandler()->LineHeight(parts[i].c_str());
//assertGl();
translatePos= Vec3f(translatePos.x, translatePos.y - yLineValue, translatePos.z);
needsRecursiveRender = true;
}
break;
default:
if(needsRecursiveRender == true) {
internalRender(parts[i], translatePos.x, translatePos.y, false, color);
//internalRender(parts[i], translatePos.x, translatePos.y, false, color);
glPushMatrix();
glLoadIdentity();
glTranslatef(translatePos.x, translatePos.y, translatePos.z);
font->getTextHandler()->Render(parts[i].c_str());
specialFTGLErrorCheckWorkaround(parts[i]);
glPopMatrix();
needsRecursiveRender = false;
}
else {
//assertGl();
font->getTextHandler()->Render(parts[i].c_str());
specialFTGLErrorCheckWorkaround(parts[i]);
}
}
}
@@ -560,8 +626,12 @@ void TextRenderer3DGl::internalRender(const string &text, float x, float y, boo
}
}
//assertGl();
glPopMatrix();
//assertGl();
if(color != NULL) {
glPopAttrib();
}
@@ -573,7 +643,7 @@ void TextRenderer3DGl::end() {
assert(rendering);
rendering= false;
assertGl();
//assertGl();
glPopAttrib();

View File

@@ -17,6 +17,7 @@
#include "model.h"
#include "conversion.h"
#include "util.h"
#include <stdexcept>
#include "leak_dumper.h"
using namespace std;
@@ -76,7 +77,11 @@ void InterpolationData::updateVertices(float t, bool cycle) {
if(t <0.0f || t>1.0f) {
printf("ERROR t = [%f] for cycle [%d] f [%d] v [%d]\n",t,cycle,mesh->getFrameCount(),mesh->getVertexCount());
}
assert(t>=0.0f && t<=1.0f);
//assert(t>=0.0f && t<=1.0f);
if(t < 0.0f || t > 1.0f) {
throw runtime_error("t < 0.0f || t > 1.0f t = [" + floatToStr(t) + "]");
}
assert(t >= 0.f && t <= 1.f);
uint32 frameCount= mesh->getFrameCount();
uint32 vertexCount= mesh->getVertexCount();