diff options
author | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2019-01-03 09:54:13 -0400 |
---|---|---|
committer | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2019-01-04 17:27:41 -0400 |
commit | 513b50eee80cb72fe38fd42dd99527c3b6ef7800 (patch) | |
tree | c1e7fb8661475febd784451e3aff28f0d36c2586 /platform/qt | |
parent | 76b3bb9d20259db6d5ba7c39e00bf7d13922d0a4 (diff) | |
download | qtlocation-mapboxgl-513b50eee80cb72fe38fd42dd99527c3b6ef7800.tar.gz |
[Qt] Implement LocalGlyphRasterizer
Diffstat (limited to 'platform/qt')
-rw-r--r-- | platform/qt/qt.cmake | 2 | ||||
-rw-r--r-- | platform/qt/src/local_glyph_rasterizer.cpp | 74 |
2 files changed, 75 insertions, 1 deletions
diff --git a/platform/qt/qt.cmake b/platform/qt/qt.cmake index a7b48ac4b1..e651b8c0c7 100644 --- a/platform/qt/qt.cmake +++ b/platform/qt/qt.cmake @@ -29,6 +29,7 @@ set(MBGL_QT_CORE_FILES # Platform integration PRIVATE platform/qt/src/async_task.cpp PRIVATE platform/qt/src/async_task_impl.hpp + PRIVATE platform/qt/src/local_glyph_rasterizer.cpp PRIVATE platform/qt/src/qt_logging.cpp PRIVATE platform/qt/src/qt_image.cpp PRIVATE platform/qt/src/run_loop.cpp @@ -38,7 +39,6 @@ set(MBGL_QT_CORE_FILES PRIVATE platform/qt/src/timer_impl.hpp PRIVATE platform/qt/src/utf.cpp - PRIVATE platform/default/src/mbgl/text/local_glyph_rasterizer.cpp PRIVATE platform/default/src/mbgl/text/collator.cpp PRIVATE platform/default/src/mbgl/text/unaccent.cpp PRIVATE platform/default/include/mbgl/text/unaccent.hpp diff --git a/platform/qt/src/local_glyph_rasterizer.cpp b/platform/qt/src/local_glyph_rasterizer.cpp new file mode 100644 index 0000000000..3f8df5d876 --- /dev/null +++ b/platform/qt/src/local_glyph_rasterizer.cpp @@ -0,0 +1,74 @@ +#include <mbgl/text/local_glyph_rasterizer.hpp> +#include <mbgl/util/i18n.hpp> + +#include <QtCore/QFile> +#include <QtGui/QFont> +#include <QtGui/QImage> +#include <QtGui/QPainter> +#include <qglobal.h> + +namespace mbgl { + +class LocalGlyphRasterizer::Impl { +public: + Impl(const optional<std::string> fontFamily_); + + bool isConfigured() const; + + optional<std::string> fontFamily; + QFont font; +}; + +LocalGlyphRasterizer::Impl::Impl(const optional<std::string> fontFamily_) + : fontFamily(fontFamily_) { + if (isConfigured()) { + font.setFamily(QString::fromStdString(*fontFamily)); + font.setPixelSize(24); + } +} + +bool LocalGlyphRasterizer::Impl::isConfigured() const { + return fontFamily.operator bool(); +} + +LocalGlyphRasterizer::LocalGlyphRasterizer(const optional<std::string> fontFamily) + : impl(std::make_unique<Impl>(fontFamily)) { +} + +LocalGlyphRasterizer::~LocalGlyphRasterizer() { +} + +bool LocalGlyphRasterizer::canRasterizeGlyph(const FontStack&, GlyphID glyphID) { + return util::i18n::allowsFixedWidthGlyphGeneration(glyphID) && impl->isConfigured(); +} + +Glyph LocalGlyphRasterizer::rasterizeGlyph(const FontStack&, GlyphID glyphID) { + Glyph glyph; + glyph.id = glyphID; + + QFontMetrics metrics(impl->font); + Size size(metrics.width(glyphID), metrics.height()); + + glyph.metrics.width = size.width; + glyph.metrics.height = size.height; + glyph.metrics.left = 3; + glyph.metrics.top = -8; + glyph.metrics.advance = metrics.width(glyphID); + + QImage image(QSize(size.width, size.height), QImage::Format_Alpha8); + image.fill(qRgba(0, 0, 0, 0)); + + QPainter painter(&image); + painter.setFont(impl->font); + painter.setRenderHints(QPainter::TextAntialiasing); + painter.drawText(QPointF(0, metrics.ascent()), QString(QChar(glyphID))); + + auto img = std::make_unique<uint8_t[]>(image.byteCount()); + memcpy(img.get(), image.constBits(), image.byteCount()); + + glyph.bitmap = AlphaImage { size, std::move(img) }; + + return glyph; +} + +} // namespace mbgl |