[Qt] Implement LocalGlyphRasterizer

This commit is contained in:
Bruno de Oliveira Abinader
2019-01-03 09:54:13 -04:00
parent 76b3bb9d20
commit 513b50eee8
4 changed files with 82 additions and 3 deletions

View File

@@ -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

View File

@@ -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

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

View File

@@ -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,