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 | |
parent | 76b3bb9d20259db6d5ba7c39e00bf7d13922d0a4 (diff) | |
download | qtlocation-mapboxgl-513b50eee80cb72fe38fd42dd99527c3b6ef7800.tar.gz |
[Qt] Implement LocalGlyphRasterizer
-rw-r--r-- | platform/qt/qt.cmake | 2 | ||||
-rw-r--r-- | platform/qt/src/local_glyph_rasterizer.cpp | 74 | ||||
-rw-r--r-- | test/fixtures/local_glyphs/ping_fang_qt/expected.png | bin | 0 -> 25026 bytes | |||
-rw-r--r-- | test/text/local_glyph_rasterizer.test.cpp | 9 |
4 files changed, 82 insertions, 3 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 diff --git a/test/fixtures/local_glyphs/ping_fang_qt/expected.png b/test/fixtures/local_glyphs/ping_fang_qt/expected.png Binary files differnew file mode 100644 index 0000000000..465bce5b77 --- /dev/null +++ b/test/fixtures/local_glyphs/ping_fang_qt/expected.png diff --git a/test/text/local_glyph_rasterizer.test.cpp b/test/text/local_glyph_rasterizer.test.cpp index 84c685d66f..0dfec1689a 100644 --- a/test/text/local_glyph_rasterizer.test.cpp +++ b/test/text/local_glyph_rasterizer.test.cpp @@ -52,7 +52,8 @@ public: } // end namespace -#ifdef __APPLE__ +// Enabling Qt requires adding a CJK-compatible font in the CI image. +#if defined(__APPLE__) TEST(LocalGlyphRasterizer, PingFang) { LocalGlyphRasterizerTest test(std::string("PingFang")); @@ -64,10 +65,14 @@ TEST(LocalGlyphRasterizer, PingFang) { return response; }; test.map.getStyle().loadJSON(util::read_file("test/fixtures/local_glyphs/mixed.json")); +#if defined(__APPLE__) test.checkRendering("ping_fang"); +#elif defined(__QT__) + test.checkRendering("ping_fang_qt"); +#endif // defined(__APPLE__) } -#endif +#endif // defined(__APPLE__) TEST(LocalGlyphRasterizer, NoLocal) { // Expectation: without any local fonts set, and without any CJK glyphs provided, |