| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | #include <osgQt/QFontImplementation> |
|---|
| 14 | |
|---|
| 15 | #include <osgDB/FileNameUtils> |
|---|
| 16 | #include <osgDB/Registry> |
|---|
| 17 | #include <osgText/Font> |
|---|
| 18 | |
|---|
| 19 | #include <QtGui/QFont> |
|---|
| 20 | #include <QtGui/QFontMetrics> |
|---|
| 21 | #include <QtGui/QImage> |
|---|
| 22 | #include <QtGui/QPainter> |
|---|
| 23 | |
|---|
| 24 | namespace osgQt { |
|---|
| 25 | |
|---|
| 26 | QFontImplementation::QFontImplementation(const QFont& font) : |
|---|
| 27 | _filename(font.toString().toStdString() + ".qfont"), |
|---|
| 28 | _font(font) |
|---|
| 29 | { |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | QFontImplementation::~QFontImplementation() |
|---|
| 33 | { |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | std::string |
|---|
| 37 | QFontImplementation::getFileName() const |
|---|
| 38 | { |
|---|
| 39 | return _filename; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | osgText::Glyph* |
|---|
| 43 | QFontImplementation::getGlyph(const osgText::FontResolution& fontRes, unsigned int charcode) |
|---|
| 44 | { |
|---|
| 45 | unsigned int fontSize = fontRes.second; |
|---|
| 46 | _font.setPixelSize(fontSize); |
|---|
| 47 | |
|---|
| 48 | float coord_scale = 1.0f/float(fontSize); |
|---|
| 49 | |
|---|
| 50 | QFontMetrics fontMetrics(_font); |
|---|
| 51 | QFontMetricsF fontMetricsF(_font); |
|---|
| 52 | |
|---|
| 53 | QRect rect = fontMetrics.boundingRect(QChar(charcode)); |
|---|
| 54 | QRectF rectF = fontMetricsF.boundingRect(QChar(charcode)); |
|---|
| 55 | |
|---|
| 56 | int margin = 1; |
|---|
| 57 | |
|---|
| 58 | int imageWidth = rect.width() + 2*margin; |
|---|
| 59 | int imageHeight = rect.height() + 2*margin; |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | QImage image(imageWidth, imageHeight, QImage::Format_ARGB32); |
|---|
| 63 | image.fill(0); |
|---|
| 64 | QPainter painter(&image); |
|---|
| 65 | painter.setRenderHint(QPainter::TextAntialiasing); |
|---|
| 66 | |
|---|
| 67 | painter.setFont(_font); |
|---|
| 68 | |
|---|
| 69 | painter.setBackgroundMode(Qt::TransparentMode); |
|---|
| 70 | painter.setBrush(Qt::white); |
|---|
| 71 | painter.setPen(Qt::white); |
|---|
| 72 | |
|---|
| 73 | painter.drawText(margin - rect.left(), imageHeight - 1 - (margin + rect.bottom()), QString(QChar(charcode))); |
|---|
| 74 | painter.end(); |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | osg::ref_ptr<osgText::Glyph> glyph = new osgText::Glyph(_facade, charcode); |
|---|
| 78 | |
|---|
| 79 | unsigned int dataSize = imageWidth*imageHeight; |
|---|
| 80 | unsigned char* data = new unsigned char[dataSize]; |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | for (int x = 0; x < imageWidth; ++x) |
|---|
| 84 | { |
|---|
| 85 | for (int y = 0; y < imageHeight; ++y) |
|---|
| 86 | { |
|---|
| 87 | data[x + y*imageWidth] = qAlpha(image.pixel(x, imageHeight - 1 - y)); |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | glyph->setImage(imageWidth, imageHeight, 1, |
|---|
| 93 | GL_ALPHA, |
|---|
| 94 | GL_ALPHA, GL_UNSIGNED_BYTE, |
|---|
| 95 | data, |
|---|
| 96 | osg::Image::USE_NEW_DELETE, |
|---|
| 97 | 1); |
|---|
| 98 | glyph->setInternalTextureFormat(GL_ALPHA); |
|---|
| 99 | |
|---|
| 100 | glyph->setWidth((float)imageWidth * coord_scale); |
|---|
| 101 | glyph->setHeight((float)imageHeight * coord_scale); |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | float leftBearing = fontMetricsF.leftBearing(QChar(charcode)); |
|---|
| 105 | float rightBearing = fontMetricsF.rightBearing(QChar(charcode)); |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | osg::Vec2 bottomLeft(leftBearing - margin, - rectF.bottom() - margin); |
|---|
| 109 | glyph->setHorizontalBearing(bottomLeft * coord_scale); |
|---|
| 110 | glyph->setHorizontalAdvance(fontMetricsF.width(QChar(charcode)) * coord_scale); |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | osg::Vec2 topMiddle(- margin + 0.5*(leftBearing - rect.width() - rightBearing), |
|---|
| 114 | rectF.top() - margin); |
|---|
| 115 | glyph->setVerticalBearing(topMiddle * coord_scale); |
|---|
| 116 | glyph->setVerticalAdvance((rectF.height() + fontMetricsF.overlinePos() - fontMetricsF.xHeight()) * coord_scale); |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | return glyph.release(); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | osg::Vec2 |
|---|
| 125 | QFontImplementation::getKerning(unsigned int leftcharcode, unsigned int rightcharcode, osgText::KerningType kerningType) |
|---|
| 126 | { |
|---|
| 127 | return osg::Vec2(0, 0); |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | bool |
|---|
| 131 | QFontImplementation::hasVertical() const |
|---|
| 132 | { |
|---|
| 133 | return true; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | } |
|---|