8 #include <QtCore/QStringList>
9 #include <QtGui/QFontMetrics>
10 #include <QtGui/QImage>
11 #include <QtGui/QPaintEngine>
12 #include <QtOpenGL/QGLWidget>
13 #include <QtGui/QPainterPath>
21 # define WIN32_LEAN_AND_MEAN 1
26 #include <OpenGL/gl.h>
27 #include <OpenGL/glu.h>
70 spec.
mSize = fontSize * zoomFactor;
73 QFont font = this->
getFont(spec);
95 std::map<CLFontSpec, QFont>::iterator pos;
102 styleString =
"Bold";
107 styleString +=
" Italic";
110 styleString = styleString.trimmed();
114 QString family(spec.
mFamily.c_str());
115 family = family.toLower();
117 if (family ==
"sans")
121 QString defaultFamily = defaultFont.defaultFamily();
124 tempSpec.
mFamily =
"helvetica";
126 if (defaultFamily.contains(tempSpec.
mFamily.c_str()))
132 font = this->
getFont(tempSpec);
134 if (font == defaultFont)
139 if (defaultFamily.contains(tempSpec.
mFamily.c_str()))
145 font = this->
getFont(tempSpec);
150 else if (family ==
"serif")
154 QString defaultFamily = defaultFont.defaultFamily();
159 if (defaultFamily.contains(tempSpec.
mFamily.c_str()))
165 font = this->
getFont(tempSpec);
167 if (font == defaultFont)
170 tempSpec.
mFamily =
"times new roman";
172 if (defaultFamily.contains(tempSpec.
mFamily.c_str()))
178 font = this->
getFont(tempSpec);
180 if (font == defaultFont)
185 if (defaultFamily.contains(tempSpec.
mFamily.c_str()))
191 font = this->
getFont(tempSpec);
198 else if (family ==
"monospaced")
202 QString defaultFamily = defaultFont.defaultFamily();
207 if (defaultFamily.contains(tempSpec.
mFamily.c_str()))
213 font = this->
getFont(tempSpec);
215 if (font == defaultFont)
218 tempSpec.
mFamily =
"courier new";
220 if (defaultFamily.contains(tempSpec.
mFamily.c_str()))
228 if (font == defaultFont)
233 if (defaultFamily.contains(tempSpec.
mFamily.c_str()))
239 font = this->
getFont(tempSpec);
248 std::list<std::string> familyList;
252 QString defaultFamily = defaultFont.defaultFamily();
255 if (!familyList.empty())
260 std::list<std::string>::const_iterator familyIt = familyList.begin(), familyEndit = familyList.end();
262 while (familyIt != familyEndit)
266 if (font != defaultFont)
281 this->
mFontMap.insert(std::pair<CLFontSpec, QFont>(spec, font));
297 QFontMetrics fontMetrics(font);
298 int textWidthInPixels = fontMetrics.width(text.c_str()) + 2;
299 int textHeightInPixels = fontMetrics.height() + 2;
300 return std::pair<double, double>(textWidthInPixels, textHeightInPixels);
308 QFont font = this->
getFont(spec);
321 font.setStyleStrategy(QFont::ForceOutline);
322 QFontMetrics fontMetrics(font);
323 std::pair<double, double> size = this->
getTextureSize(font, text);
326 unsigned int exponentWidth = (
unsigned int)ceil(log(size.first + 2) / log(2.0));
327 unsigned int exponentHeight = (
unsigned int)ceil(log(size.second + 2) / log(2.0));
328 unsigned int width = 1 << exponentWidth;
329 unsigned int height = 1 << exponentHeight;
331 QImage image(width, height, QImage::Format_RGB32);
332 QPainter painter(&image);
333 painter.setBackground(Qt::black);
334 painter.setPen(QPen(QColor(255, 255, 255, 255), 0.25, Qt::SolidLine, Qt::FlatCap, Qt::BevelJoin));
335 painter.setBrush(Qt::NoBrush);
336 painter.setFont(font);
337 painter.eraseRect(0, 0, width, height);
339 painter.translate(1.0, 1.0);
343 painter.drawText(0.0, fontMetrics.ascent() + 1, text.c_str());
346 image = QGLWidget::convertToGLFormat(image);
353 pSpec->
mScale = zoomFactor;
358 GLenum format = GL_ALPHA;
359 glTexImage2D(GL_PROXY_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, format, GL_UNSIGNED_BYTE, NULL);
360 glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w);
362 while (w == 0 && width > 1 && height > 1)
366 height = height >> 1;
367 glTexImage2D(GL_PROXY_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, format, GL_UNSIGNED_BYTE, NULL);
368 glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w);
371 GLubyte* textureData = NULL;
381 textureData =
new GLubyte[width * height];
382 pSpec->
mAscent = (double)fontMetrics.ascent();
383 unsigned int i, iMax = width * height;
385 for (i = 0; i < iMax; ++i)
387 textureData[i] = image.bits()[4 * i];
391 return std::pair<CLTextTextureSpec*, GLubyte*>(pSpec, textureData);
403 QString qname(name.c_str());
405 QStringList::const_iterator constIterator;
407 for (constIterator = familyList.constBegin(); constIterator != familyList.constEnd(); ++constIterator)
409 if ((*constIterator).contains(name.c_str(), Qt::CaseInsensitive))
411 families.insert((*constIterator).toLatin1().data());
422 std::set<std::string>::const_iterator it = familySet.begin();
423 std::set<std::string>::const_iterator endit = familySet.end();
436 familyList.push_back(*it);
443 QString name(family.c_str());
444 name = name.toLower();
445 name = name.trimmed();
447 std::set<std::string> familySet;
450 if (!familySet.empty())
458 return FONT_RENDERER(family, fontSize, text, weight, style, zoomFactor);
QFont getFont(const CLFontSpec &spec)
static std::pair< CLTextTextureSpec *, GLubyte * > createTexture(const std::string &family, double fontSize, const std::string &text, CLText::FONT_WEIGHT weight=CLText::WEIGHT_NORMAL, CLText::FONT_STYLE style=CLText::STYLE_NORMAL, double zoomFactor=1.0)
void findSimilarFamily(const std::string &name, std::set< std::string > &families) const
std::pair< CLTextTextureSpec *, GLubyte * > getTexture(QFont &font, const std::string &text, double zoomFactor)
void getFamilyList(const std::string &family, std::list< std::string > &list) const
virtual std::pair< CLTextTextureSpec *, GLubyte * > operator()(const std::string &family, double fontSize, const std::string &text, CLText::FONT_WEIGHT weight=CLText::WEIGHT_NORMAL, CLText::FONT_STYLE style=CLText::STYLE_NORMAL, double zoomFactor=1.0)
virtual std::pair< double, double > getTextureSize(const CLFontSpec &spec, const std::string &text)
CLText::FONT_WEIGHT mWeight
std::map< CLFontSpec, QFont > mFontMap
unsigned int mNumComponents
QFontDatabase * mpFontDatabase
CLText::FONT_STYLE mStyle
virtual ~CQFontRenderer()
static CQFontRenderer FONT_RENDERER
void orderFamilies(const std::string &name, const std::set< std::string > &familySet, std::list< std::string > &familyList) const