diff --git a/.travis.yml b/.travis.yml index 5d46212f1..789083764 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,6 +28,7 @@ install: before_script: - if [[ ${CXX} == "g++" ]]; then export CXX="g++-4.8" ; fi +- if [[ $BUILDTYPE} == "Debug" ]]; then export CXXFLAGS="-fsanitize=address -fsanitize=thread";export LDFLAGS="-fsanitize=address -fsanitize=thread" ; fi script: - make llmr BUILDTYPE=${BUILDTYPE} diff --git a/README.md b/README.md index c94627b47..a8d3c31c9 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ Target devices: iPhone 4 and above (4S, 5, 5c, 5s) and iPad 2 and above (3, 4, m Ensure you have git and other build essentials: sudo apt-get update - sudo apt-get install git cmake make pkg-config curl automake libtool xutils-dev + sudo apt-get install git build-essential zlib1g-dev automake libtool xutils-dev make cmake pkg-config nodejs-legacy Install a `-std=c++11` capable compiler diff --git a/bin/style.js b/bin/style.js index a3c312f54..c831fd414 100644 --- a/bin/style.js +++ b/bin/style.js @@ -267,6 +267,7 @@ module.exports = { "value": 10, "path": "curve", "text_field": "ele", + "font": "Open Sans Regular, Arial Unicode MS Regular", "fontSize": 10, "feature_type": "line", "type": "text" @@ -648,6 +649,7 @@ module.exports = { "layer": "country_label", "text_field": "name", "path": "horizontal", + "font": "Open Sans Bold, Arial Unicode MS Bold", "fontSize": 24, "feature_type": "point", "type": "text" @@ -664,6 +666,7 @@ module.exports = { "type": "text", "text_field": "name", "path": "curve", + "font": "Open Sans Italic, Arial Unicode MS Regular", "fontSize": 16 }, "marine_label_point_1": { @@ -675,6 +678,7 @@ module.exports = { "path": "horizontal", "field": "labelrank", "value": 1, + "font": "Open Sans Italic, Arial Unicode MS Regular", "fontSize": 30 }, "marine_label_point_2": { @@ -686,6 +690,7 @@ module.exports = { "path": "horizontal", "field": "labelrank", "value": 2, + "font": "Open Sans Italic, Arial Unicode MS Regular", "fontSize": 24 }, "marine_label_point_other": { @@ -697,6 +702,7 @@ module.exports = { "path": "horizontal", "field": "labelrank", "value": [3,4,5,6], + "font": "Open Sans Italic, Arial Unicode MS Regular", "fontSize": 18 }, "state_label": { @@ -704,6 +710,7 @@ module.exports = { "layer": "state_label", "text_field": "name", "path": "horizontal", + "font": "Open Sans Regular, Arial Unicode MS Regular", "fontSize": 16, "feature_type": "point", "type": "text", @@ -716,6 +723,7 @@ module.exports = { "value": "city", "text_field": "name", "path": "horizontal", + "font": "Open Sans Semibold, Arial Unicode MS Bold", "fontSize": 20, "feature_type": "point", "type": "text" @@ -727,6 +735,7 @@ module.exports = { "value": "town", "text_field": "name", "path": "horizontal", + "font": "Open Sans Semibold, Arial Unicode MS Bold", "fontSize": 24, "feature_type": "point", "type": "text" @@ -738,6 +747,7 @@ module.exports = { "value": "village", "text_field": "name", "path": "horizontal", + "font": "Open Sans Semibold, Arial Unicode MS Bold", "fontSize": 22, "feature_type": "point", "type": "text" @@ -753,6 +763,7 @@ module.exports = { ], "text_field": "name", "path": "horizontal", + "font": "Open Sans Semibold, Arial Unicode MS Bold", "fontSize": 18, "feature_type": "point", "type": "text" @@ -765,6 +776,7 @@ module.exports = { "text_field": "name", "path": "curve", "padding": 2, + "font": "Open Sans Regular, Arial Unicode MS Regular", "fontSize": 18, "feature_type": "line", "type": "text", @@ -778,6 +790,7 @@ module.exports = { "text_field": "name", "path": "curve", "padding": 2, + "font": "Open Sans Regular, Arial Unicode MS Regular", "fontSize": 16, "feature_type": "line", "type": "text", @@ -791,6 +804,7 @@ module.exports = { "text_field": "name", "path": "curve", "padding": 2, + "font": "Open Sans Regular, Arial Unicode MS Regular", "fontSize": 14, "feature_type": "line", "type": "text", @@ -801,6 +815,7 @@ module.exports = { "layer": "water_label", "text_field": "name", "path": "horizontal", + "font": "Open Sans Semibold Italic, Arial Unicode MS Bold", "fontSize": 12, "feature_type": "point", "type": "text" @@ -810,6 +825,7 @@ module.exports = { "layer": "waterway_label", "text_field": "name", "path": "curve", + "font": "Open Sans Semibold Italic, Arial Unicode MS Bold", "fontSize": 12, "textMinDistance": 15, "feature_type": "line", @@ -862,6 +878,7 @@ module.exports = { "text_field": "name", "path": "horizontal", "padding": 2, + "font": "Open Sans Semibold, Arial Unicode MS Bold", "fontSize": 11, "feature_type": "point", "type": "text" @@ -874,6 +891,7 @@ module.exports = { "text_field": "name", "path": "horizontal", "padding": 2, + "font": "Open Sans Semibold, Arial Unicode MS Bold", "fontSize": 10, "feature_type": "point", "type": "text" @@ -886,6 +904,7 @@ module.exports = { "text_field": "name", "path": "horizontal", "padding": 2, + "font": "Open Sans Semibold, Arial Unicode MS Bold", "fontSize": 10, "feature_type": "point", "type": "text" @@ -2232,7 +2251,7 @@ module.exports = { "country_label": { "color": "country_text", "stroke": "rgba(255,255,255,0.5)", - "strokeWidth": 0.4, + "strokeWidth": 0.5, "size": "country_label_size" }, "country_label_line": { @@ -2981,7 +3000,7 @@ module.exports = { "country_label": { "color": "text_night", "stroke": "text2_stroke_night", - "strokeWidth": 0.4, + "strokeWidth": 0.5, "size": "country_label_size" }, "country_label_line": { diff --git a/common.gypi b/common.gypi index b1ad46015..8e6919a10 100644 --- a/common.gypi +++ b/common.gypi @@ -5,6 +5,11 @@ 'CLANG_CXX_LIBRARY': 'libc++', 'CLANG_CXX_LANGUAGE_STANDARD':'c++11', 'GCC_VERSION': 'com.apple.compilers.llvm.clang.1_0', + 'GCC_ENABLE_CPP_EXCEPTIONS': 'YES', + 'GCC_ENABLE_CPP_RTTI':'YES', + 'OTHER_CPLUSPLUSFLAGS': [ '-Wall', '-Wextra' ], + 'GCC_WARN_PEDANTIC': 'YES', + 'GCC_WARN_UNINITIALIZED_AUTOS': 'YES_AGGRESSIVE' }, 'cflags_cc': ['-std=c++11'], 'configurations': { @@ -12,9 +17,11 @@ 'cflags': [ '-g', '-O0' ], 'defines': [ 'DEBUG' ], 'xcode_settings': { - 'GCC_OPTIMIZATION_LEVEL': '0', + 'GCC_OPTIMIZATION_LEVEL': '1', 'GCC_GENERATE_DEBUGGING_SYMBOLS': 'YES', - 'OTHER_CPLUSPLUSFLAGS': [ '-Wall', '-Wextra', '-pedantic' ] + 'DEAD_CODE_STRIPPING': 'NO', + 'GCC_INLINES_ARE_PRIVATE_EXTERN': 'NO', + 'OTHER_CPLUSPLUSFLAGS': [ '-fno-omit-frame-pointer','-fwrapv', '-fstack-protector-all', '-fno-common'] } }, 'Release': { @@ -24,8 +31,7 @@ 'GCC_OPTIMIZATION_LEVEL': '3', 'GCC_GENERATE_DEBUGGING_SYMBOLS': 'NO', 'DEAD_CODE_STRIPPING': 'YES', - 'GCC_INLINES_ARE_PRIVATE_EXTERN': 'YES', - 'OTHER_CPLUSPLUSFLAGS': [ '-Wall', '-Wextra', '-pedantic' ] + 'GCC_INLINES_ARE_PRIVATE_EXTERN': 'YES' } } } diff --git a/include/llmr/geometry/glyph_atlas.hpp b/include/llmr/geometry/glyph_atlas.hpp index 312572f5e..5bcd573f5 100644 --- a/include/llmr/geometry/glyph_atlas.hpp +++ b/include/llmr/geometry/glyph_atlas.hpp @@ -2,6 +2,7 @@ #define LLMR_GEOMETRY_GLYPH_ATLAS #include +#include #include #include @@ -30,7 +31,7 @@ public: Rect addGlyph(uint64_t tile_id, const std::string& face_name, - const VectorTileGlyph& glyph); + const SDFGlyph& glyph); void removeGlyphs(uint64_t tile_id); void bind(); diff --git a/include/llmr/map/map.hpp b/include/llmr/map/map.hpp index ab1738f96..49050540c 100644 --- a/include/llmr/map/map.hpp +++ b/include/llmr/map/map.hpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -98,6 +99,7 @@ public: inline const TransformState &getState() const { return state; } inline const Style &getStyle() const { return style; } inline GlyphAtlas &getGlyphAtlas() { return glyphAtlas; } + inline GlyphStore &getGlyphStore() { return glyphStore; } inline SpriteAtlas &getSpriteAtlas() { return spriteAtlas; } inline uv_loop_t *getLoop() { return loop; } inline time getAnimationTime() const { return animationTime; } @@ -152,6 +154,7 @@ private: Texturepool texturepool; Style style; GlyphAtlas glyphAtlas; + GlyphStore glyphStore; SpriteAtlas spriteAtlas; Painter painter; diff --git a/include/llmr/map/tile.hpp b/include/llmr/map/tile.hpp index 0f43f9df2..9681c3637 100644 --- a/include/llmr/map/tile.hpp +++ b/include/llmr/map/tile.hpp @@ -9,6 +9,7 @@ #include #include #include +#include namespace llmr { diff --git a/include/llmr/map/tile_parser.hpp b/include/llmr/map/tile_parser.hpp index 86545e4f7..5785c29b3 100644 --- a/include/llmr/map/tile_parser.hpp +++ b/include/llmr/map/tile_parser.hpp @@ -4,11 +4,15 @@ #include #include #include +#include +#include +#include namespace llmr { class Style; class GlyphAtlas; +class GlyphStore; class SpriteAtlas; class LayerDescription; @@ -16,12 +20,13 @@ class Bucket; class TileParser { public: - TileParser(const std::string& data, VectorTileData& tile, const Style& style, GlyphAtlas& glyphAtlas, SpriteAtlas &spriteAtlas); + TileParser(const std::string& data, VectorTileData& tile, const Style& style, GlyphAtlas& glyphAtlas, GlyphStore &glyphStore, SpriteAtlas &spriteAtlas); private: bool obsolete() const; void parseGlyphs(); void parseStyleLayers(const std::vector& layers); + void addGlyph(uint64_t tileid, const std::string stackname, const std::u32string &string, const FontStack &fontStack, GlyphAtlas &glyphAtlas, GlyphPositions &face); std::unique_ptr createBucket(const BucketDescription& bucket_desc); std::unique_ptr createFillBucket(const VectorTileLayer& layer, const BucketDescription& bucket_desc); std::unique_ptr createLineBucket(const VectorTileLayer& layer, const BucketDescription& bucket_desc); @@ -35,6 +40,7 @@ private: VectorTileData& tile; const Style& style; GlyphAtlas& glyphAtlas; + GlyphStore &glyphStore; SpriteAtlas &spriteAtlas; Faces faces; Placement placement; diff --git a/include/llmr/platform/platform.hpp b/include/llmr/platform/platform.hpp index 972e7db6b..d516378c2 100644 --- a/include/llmr/platform/platform.hpp +++ b/include/llmr/platform/platform.hpp @@ -25,9 +25,11 @@ struct Response { // Makes an HTTP request of a URL, preferrably on a background thread, and calls a function with the // results in the original thread (which runs the libuv loop). +// If the loop pointer is NULL, the callback function will be called on an arbitrary thread. // Returns a cancellable request. std::shared_ptr request_http(const std::string &url, - std::function callback, uv_loop_t *loop); + std::function callback, + uv_loop_t *loop = nullptr); // Cancels an HTTP request. void cancel_request_http(const std::shared_ptr &req); diff --git a/include/llmr/renderer/bucket.hpp b/include/llmr/renderer/bucket.hpp index f8447be89..43bd96e3f 100644 --- a/include/llmr/renderer/bucket.hpp +++ b/include/llmr/renderer/bucket.hpp @@ -2,6 +2,7 @@ #define LLMR_RENDERER_BUCKET #include +#include #include #include #include diff --git a/include/llmr/renderer/prerendered_texture.hpp b/include/llmr/renderer/prerendered_texture.hpp index e35cc0321..4b163ac70 100644 --- a/include/llmr/renderer/prerendered_texture.hpp +++ b/include/llmr/renderer/prerendered_texture.hpp @@ -26,7 +26,6 @@ private: GLint previous_fbo = 0; GLuint fbo = 0; GLuint texture = 0; - bool mipmapped = false; }; } diff --git a/include/llmr/renderer/text_bucket.hpp b/include/llmr/renderer/text_bucket.hpp index 2b73aa74d..ad282bdd0 100644 --- a/include/llmr/renderer/text_bucket.hpp +++ b/include/llmr/renderer/text_bucket.hpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -36,7 +37,7 @@ public: PlacementRange placementRange, float zoom); void addFeature(const VectorTileFeature &feature, - const IndexedFaces &faces, + const GlyphPositions &face, const std::map &shapings); void drawGlyphs(TextShader &shader); diff --git a/include/llmr/text/glyph.hpp b/include/llmr/text/glyph.hpp index 91531e24f..e6138e712 100644 --- a/include/llmr/text/glyph.hpp +++ b/include/llmr/text/glyph.hpp @@ -9,6 +9,11 @@ namespace llmr { +typedef std::pair GlyphRange; + +// Note: this only works for the BMP +GlyphRange getGlyphRange(uint32_t glyph); + struct GlyphMetrics { operator bool() const { return width == 0 && height == 0 && advance == 0; diff --git a/include/llmr/text/glyph_store.hpp b/include/llmr/text/glyph_store.hpp new file mode 100644 index 000000000..eb5d6038f --- /dev/null +++ b/include/llmr/text/glyph_store.hpp @@ -0,0 +1,80 @@ +#ifndef LLMR_TEXT_GLYPH_STORE +#define LLMR_TEXT_GLYPH_STORE + +#include +#include + +#include +#include +#include +#include +#include +#include + +namespace llmr { + + +class SDFGlyph { +public: + uint32_t id = 0; + + // A signed distance field of the glyph with a border of 3 pixels. + std::string bitmap; + + // Glyph metrics + GlyphMetrics metrics; +}; + +class FontStack { +public: + void insert(uint32_t id, const SDFGlyph &glyph); + const std::map &getMetrics() const; + const std::map &getSDFs() const; + const Shaping getShaping(const std::u32string &string) const; + +private: + std::map bitmaps; + std::map metrics; + std::map sdfs; + mutable std::mutex mtx; +}; + +class GlyphPBF { +public: + GlyphPBF(const std::string &fontStack, GlyphRange glyphRange); + + void parse(FontStack &stack); + + std::shared_future getFuture(); + +private: + std::string data; + std::promise promise; + std::shared_future future; + std::mutex mtx; +}; + +// Manages Glyphrange PBF loading. +class GlyphStore { +public: + // Block until all specified GlyphRanges of the specified font stack are loaded. + void waitForGlyphRanges(const std::string &fontStack, const std::set &glyphRanges); + + FontStack &getFontStack(const std::string &fontStack); + +private: + // Loads an individual glyph range from the font stack and adds it to rangeSets + std::shared_future loadGlyphRange(const std::string &fontStack, std::map> &rangeSets, GlyphRange range); + + FontStack &createFontStack(const std::string &fontStack); + +private: + std::unordered_map>> ranges; + std::unordered_map> stacks; + std::mutex mtx; +}; + + +} + +#endif diff --git a/include/llmr/text/placement.hpp b/include/llmr/text/placement.hpp index 8ec7df5e4..87c47345e 100644 --- a/include/llmr/text/placement.hpp +++ b/include/llmr/text/placement.hpp @@ -18,9 +18,9 @@ public: void addFeature(TextBucket &bucket, const std::vector &line, const BucketGeometryDescription &info, - const IndexedFaces &faces, + const GlyphPositions &face, const Shaping &shaping); - float measureText(const IndexedFaces &faces, + float measureText(const GlyphPositions &face, const Shaping &shaping); private: diff --git a/include/llmr/util/constants.hpp b/include/llmr/util/constants.hpp index 70befcb37..f315e5171 100644 --- a/include/llmr/util/constants.hpp +++ b/include/llmr/util/constants.hpp @@ -19,6 +19,11 @@ extern const bool styleParseWarnings; extern const bool spriteWarnings; extern const bool renderWarnings; extern const bool renderTree; +extern const bool labelTextMissingWarning; +extern const bool missingFontStackWarning; +extern const bool missingFontFaceWarning; +extern const bool glyphWarning; +extern const bool shapingWarning; } diff --git a/include/llmr/util/utf.hpp b/include/llmr/util/utf.hpp new file mode 100644 index 000000000..92da93f01 --- /dev/null +++ b/include/llmr/util/utf.hpp @@ -0,0 +1,45 @@ +#ifndef LLMR_UTIL_UTF +#define LLMR_UTIL_UTF + +#include + +// g++/libstdc++ is missing c++11 codecvt support +#ifdef __linux__ +#include +#else +#include +#include +#endif + +namespace llmr { + +namespace util { + +#ifdef __linux__ + +class utf8_to_utf32 { + public: + explicit utf8_to_utf32() {} + std::u32string convert(std::string const& utf8) { + return boost::locale::conv::utf_to_utf(utf8); + } +}; + +#else + +class utf8_to_utf32 { + public: + explicit utf8_to_utf32() + : utf32conv_() {} + std::u32string convert(std::string const& utf8) { + return utf32conv_.from_bytes(utf8); + } + private: + std::wstring_convert, char32_t> utf32conv_; +}; + +#endif + +}} + +#endif diff --git a/ios/MVKMapKit b/ios/MVKMapKit index 9c3aa2d91..5821b22b4 160000 --- a/ios/MVKMapKit +++ b/ios/MVKMapKit @@ -1 +1 @@ -Subproject commit 9c3aa2d919f2a76d5fef2500a3ee624b57a11383 +Subproject commit 5821b22b47c475562dc474cb75a28ac7320a1c95 diff --git a/ios/llmr-app.gyp b/ios/llmr-app.gyp index e42e914fc..5d7e1f8e3 100644 --- a/ios/llmr-app.gyp +++ b/ios/llmr-app.gyp @@ -6,7 +6,7 @@ 'targets': [ { "target_name": "iosapp", - "product_name": "llmr", + "product_name": "Mapbox GL", "type": "executable", "sources": [ "./main.m", diff --git a/linux/llmr-app.gyp b/linux/llmr-app.gyp index d979f00b4..75fd0dfe3 100644 --- a/linux/llmr-app.gyp +++ b/linux/llmr-app.gyp @@ -6,7 +6,7 @@ 'targets': [ { 'target_name': 'linuxapp', - 'product_name': 'llmr', + 'product_name': 'mapbox-gl', 'type': 'executable', 'sources': [ './main.cpp', diff --git a/macosx/llmr-app.gyp b/macosx/llmr-app.gyp index 560b22300..83bcd0f03 100644 --- a/macosx/llmr-app.gyp +++ b/macosx/llmr-app.gyp @@ -6,7 +6,7 @@ 'targets': [ { 'target_name': 'osxapp', - 'product_name': 'llmr', + 'product_name': 'Mapbox GL', 'type': 'executable', 'sources': [ './main.mm', diff --git a/proto/glyphs.proto b/proto/glyphs.proto new file mode 100644 index 000000000..606050f7a --- /dev/null +++ b/proto/glyphs.proto @@ -0,0 +1,33 @@ +// Protocol Version 1 + +package llmr.glyphs; + +option optimize_for = LITE_RUNTIME; + +// Stores a glyph with metrics and optional SDF bitmap information. +message glyph { + required uint32 id = 1; + + // A signed distance field of the glyph with a border of 3 pixels. + optional bytes bitmap = 2; + + // Glyph metrics. + required uint32 width = 3; + required uint32 height = 4; + required sint32 left = 5; + required sint32 top = 6; + required uint32 advance = 7; +} + +// Stores fontstack information and a list of faces. +message fontstack { + required string name = 1; + required string range = 2; + repeated glyph glyphs = 3; +} + +message glyphs { + repeated fontstack stacks = 1; + + extensions 16 to 8191; +} diff --git a/setup-libraries.sh b/setup-libraries.sh index 954c98070..710562bf1 100755 --- a/setup-libraries.sh +++ b/setup-libraries.sh @@ -32,7 +32,7 @@ if [[ $MISSING_DEPS != "" ]]; then echo 'and then re-run ./setup-libraries.sh' elif [ ${UNAME} = 'Linux' ]; then echo "Missing build deps: ${MISSING_DEPS}" - echo 'Please run "sudo apt-get install automake libtool xutils-dev cmake pkg-config libxi-dev libglu1-mesa-dev x11proto-randr-dev x11proto-xext-dev libxrandr-dev x11proto-xf86vidmode-dev libxxf86vm-dev libxcursor-dev nodejs-legacy"' + echo 'Please run "sudo apt-get install git build-essential zlib1g-dev automake libtool xutils-dev make cmake pkg-config nodejs-legacy libxi-dev libglu1-mesa-dev x11proto-randr-dev x11proto-xext-dev libxrandr-dev x11proto-xf86vidmode-dev libxxf86vm-dev libxcursor-dev"' echo 'and then re-run ./setup-libraries.sh' fi exit 1 @@ -74,7 +74,7 @@ source MacOSX.sh if [ ! -f out/build-cpp11-libcpp-x86_64/lib/libuv.a ] ; then ./scripts/build_libuv.sh ; fi if [ ! -f out/build-cpp11-libcpp-x86_64/lib/libssl.a ] ; then ./scripts/build_openssl.sh ; fi if [ ! -f out/build-cpp11-libcpp-x86_64/lib/libcurl.a ] ; then ./scripts/build_curl.sh ; fi - if [ ! -d out/build-cpp11-libcpp-x86_64/include/boost ] ; then ./scripts/build_boost.sh `pwd`/../../src/ `pwd`/../../linux/ `pwd`/../../common/ ; fi + if [ ! -d out/build-cpp11-libcpp-x86_64/include/boost ] ; then ./scripts/build_boost.sh `pwd`/../../src/ `pwd`/../../include/ `pwd`/../../linux/ `pwd`/../../common/ ; fi echo ' ...done' ./scripts/make_universal.sh @@ -92,7 +92,7 @@ source Linux.sh if [ ! -f out/build-cpp11-libstdcpp-gcc-x86_64/lib/libuv.a ] ; then ./scripts/build_libuv.sh ; fi if [ ! -f out/build-cpp11-libstdcpp-gcc-x86_64/lib/libssl.a ] ; then ./scripts/build_openssl.sh ; fi if [ ! -f out/build-cpp11-libstdcpp-gcc-x86_64/lib/libcurl.a ] ; then ./scripts/build_curl.sh ; fi - if [ ! -d out/build-cpp11-libstdcpp-gcc-x86_64/include/boost ] ; then ./scripts/build_boost.sh `pwd`/../../src/ `pwd`/../../linux/ `pwd`/../../common/ ; fi + if [ ! -d out/build-cpp11-libstdcpp-gcc-x86_64/include/boost ] ; then ./scripts/build_boost.sh `pwd`/../../src/ `pwd`/../../include/ `pwd`/../../linux/ `pwd`/../../common/ ; fi cd ../../ ./configure \ diff --git a/src/clipper/clipper.cpp b/src/clipper/clipper.cpp index 95975450e..56ad58416 100755 --- a/src/clipper/clipper.cpp +++ b/src/clipper/clipper.cpp @@ -2152,13 +2152,13 @@ void Clipper::IntersectEdges(TEdge *e1, TEdge *e2, else if (e1->PolyTyp != e2->PolyTyp) { //toggle subj open path OutIdx on/off when Abs(clip.WndCnt) == 1 ... - if ((e1->WindDelta == 0) && abs(e2->WindCnt) == 1 && + if ((e1->WindDelta == 0) && std::abs(e2->WindCnt) == 1 && (m_ClipType != ctUnion || e2->WindCnt2 == 0)) { AddOutPt(e1, Pt); if (e1Contributing) e1->OutIdx = Unassigned; } - else if ((e2->WindDelta == 0) && (abs(e1->WindCnt) == 1) && + else if ((e2->WindDelta == 0) && (std::abs(e1->WindCnt) == 1) && (m_ClipType != ctUnion || e1->WindCnt2 == 0)) { AddOutPt(e2, Pt); diff --git a/src/geometry/glyph_atlas.cpp b/src/geometry/glyph_atlas.cpp index 200d281f7..f4defac69 100644 --- a/src/geometry/glyph_atlas.cpp +++ b/src/geometry/glyph_atlas.cpp @@ -22,7 +22,7 @@ GlyphAtlas::~GlyphAtlas() { } Rect GlyphAtlas::addGlyph(uint64_t tile_id, const std::string& face_name, - const VectorTileGlyph& glyph) { + const SDFGlyph& glyph) { std::lock_guard lock(mtx); // Use constant value for now. diff --git a/src/map/map.cpp b/src/map/map.cpp index 835edac0b..7ac6dbe0e 100644 --- a/src/map/map.cpp +++ b/src/map/map.cpp @@ -163,7 +163,7 @@ void Map::setup() { sources.emplace("outdoors", std::unique_ptr(new Source(*this, painter, - "http://api-maps-gl.tilestream.net/v3/mapbox.mapbox-terrain-v1,mapbox.mapbox-streets-v5/%d/%d/%d.gl.pbf", + "http://a.tiles.mapbox.com/v3/mapbox.mapbox-terrain-v1,mapbox.mapbox-streets-v5/%d/%d/%d.vector.pbf", Source::Type::vector, {{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }}, 512, diff --git a/src/map/tile_parser.cpp b/src/map/tile_parser.cpp index 49444428a..ff5d61a5e 100644 --- a/src/map/tile_parser.cpp +++ b/src/map/tile_parser.cpp @@ -9,20 +9,22 @@ #include #include #include +#include +#include #include +#include using namespace llmr; - -TileParser::TileParser(const std::string& data, VectorTileData& tile, const Style& style, GlyphAtlas& glyphAtlas, SpriteAtlas &spriteAtlas) +TileParser::TileParser(const std::string& data, VectorTileData& tile, const Style& style, GlyphAtlas& glyphAtlas, GlyphStore &glyphStore, SpriteAtlas &spriteAtlas) : vector_data(pbf((const uint8_t *)data.data(), data.size())), tile(tile), style(style), glyphAtlas(glyphAtlas), + glyphStore(glyphStore), spriteAtlas(spriteAtlas), placement(tile.id.z) { - parseGlyphs(); parseStyleLayers(style.layers); } @@ -30,17 +32,13 @@ bool TileParser::obsolete() const { return tile.state == TileData::State::obsolete; } -void TileParser::parseGlyphs() { - for (const std::pair pair : vector_data.faces) { - const std::string &name = pair.first; - const VectorTileFace &face = pair.second; - - GlyphPositions &glyphs = faces[name]; - for (const VectorTileGlyph &glyph : face.glyphs) { - const Rect rect = - glyphAtlas.addGlyph(tile.id.to_uint64(), name, glyph); - glyphs.emplace(glyph.id, Glyph{rect, glyph.metrics}); - } +void TileParser::addGlyph(uint64_t tileid, const std::string stackname, const std::u32string &string, const FontStack &fontStack, GlyphAtlas &glyphAtlas, GlyphPositions &face) { + std::map sdfs = fontStack.getSDFs(); + // Loop through all characters and add glyph to atlas, positions. + for (uint32_t chr : string) { + const SDFGlyph sdf = sdfs[chr]; + const Rect rect = glyphAtlas.addGlyph(tileid, stackname, sdf); + face.emplace(chr, Glyph{rect, sdf.metrics}); } } @@ -162,30 +160,79 @@ std::unique_ptr TileParser::createIconBucket(const VectorTileLayer& laye return obsolete() ? nullptr : std::move(bucket); } +typedef std::pair GlyphRange; + std::unique_ptr TileParser::createTextBucket(const VectorTileLayer& layer, const BucketDescription& bucket_desc) { - - // Determine the correct text stack. - if (!layer.shaping.size()) { - return nullptr; - } - - // TODO: currently hardcoded to use the first font stack. - const std::map& shaping = layer.shaping.begin()->second; - - const Faces& const_faces = faces; - - IndexedFaces faces; - for (const std::string& face : layer.faces) { - auto it = const_faces.find(face); - if (it == const_faces.end()) { - // This layer references an unknown face. - return nullptr; - } - faces.push_back(&it->second); - } - std::unique_ptr bucket = std::make_unique( tile.textVertexBuffer, tile.triangleElementsBuffer, bucket_desc, placement); - addBucketFeatures(bucket, layer, bucket_desc, faces, shaping); + + util::utf8_to_utf32 ucs4conv; + + // Determine and load glyph ranges + { + std::set ranges; + + FilteredVectorTileLayer filtered_layer(layer, bucket_desc); + for (const pbf& feature_pbf : filtered_layer) { + if (obsolete()) return nullptr; + VectorTileFeature feature { feature_pbf, layer }; + + auto it_prop = feature.properties.find(bucket_desc.geometry.field); + if (it_prop == feature.properties.end()) { + // feature does not have the correct property + if (debug::labelTextMissingWarning) { + fprintf(stderr, "[WARNING] feature doesn't have property '%s' required for labelling\n", bucket_desc.geometry.field.c_str()); + } + continue; + } + + const std::u32string string = ucs4conv.convert(toString(it_prop->second)); + + // Loop through all characters of this text and collect unique codepoints. + for (uint32_t chr : string) { + ranges.insert(getGlyphRange(chr)); + } + } + + glyphStore.waitForGlyphRanges(bucket_desc.geometry.font, ranges); + } + + // Create a copy! + const FontStack &fontStack = glyphStore.getFontStack(bucket_desc.geometry.font); + std::map shaping; + GlyphPositions face; + + // Shape and place all labels. + { + FilteredVectorTileLayer filtered_layer(layer, bucket_desc); + for (const pbf& feature_pbf : filtered_layer) { + if (obsolete()) return nullptr; + VectorTileFeature feature { feature_pbf, layer }; + + auto it_prop = feature.properties.find(bucket_desc.geometry.field); + if (it_prop == feature.properties.end()) { + // feature does not have the correct property + if (debug::labelTextMissingWarning) { + fprintf(stderr, "[WARNING] feature doesn't have property '%s' required for labelling\n", bucket_desc.geometry.field.c_str()); + } + continue; + } + + const std::u32string string = ucs4conv.convert(toString(it_prop->second)); + + // Shape labels. + const Shaping shaped = fontStack.getShaping(string); + shaping.emplace(toString(it_prop->second), shaped); + + // Place labels. + addGlyph(tile.id.to_uint64(), bucket_desc.geometry.font, string, fontStack, glyphAtlas, face); + } + } + + // It looks like nearly the same interface through the rest + // of the stack. + addBucketFeatures(bucket, layer, bucket_desc, face, shaping); + return std::move(bucket); } + diff --git a/src/map/transform.cpp b/src/map/transform.cpp index 996117bfa..d567a586c 100644 --- a/src/map/transform.cpp +++ b/src/map/transform.cpp @@ -60,8 +60,8 @@ void Transform::_moveBy(const double dx, const double dy, const time duration) { // Un-rotate when rotated and panning far enough to show off-world in corners. double w = final.scale * util::tileSize / 2; - double m = std::sqrt(pow((current.width / 2), 2) + pow((current.height / 2), 2)); - double x = std::abs(sqrt(pow(final.x, 2) + pow(final.y, 2))); + double m = std::sqrt(std::pow((current.width / 2), 2) + pow((current.height / 2), 2)); + double x = std::abs(sqrt(std::pow(final.x, 2) + std::pow(final.y, 2))); if (current.angle && w - x < m) _setAngle(0, 500_milliseconds, true); if (duration == 0) { diff --git a/src/map/vector_tile_data.cpp b/src/map/vector_tile_data.cpp index 337e2fcc3..3c972aa31 100644 --- a/src/map/vector_tile_data.cpp +++ b/src/map/vector_tile_data.cpp @@ -22,7 +22,7 @@ void VectorTileData::parse() { // Parsing creates state that is encapsulated in TileParser. While parsing, // the TileParser object writes results into this objects. All other state // is going to be discarded afterwards. - TileParser parser(data, *this, map.getStyle(), map.getGlyphAtlas(), map.getSpriteAtlas()); + TileParser parser(data, *this, map.getStyle(), map.getGlyphAtlas(), map.getGlyphStore(), map.getSpriteAtlas()); } catch (const std::exception& ex) { fprintf(stderr, "[%p] exception [%d/%d/%d]... failed: %s\n", this, id.z, id.x, id.y, ex.what()); cancel(); diff --git a/src/platform/request.cpp b/src/platform/request.cpp index aadf83dea..f689517ab 100644 --- a/src/platform/request.cpp +++ b/src/platform/request.cpp @@ -12,25 +12,33 @@ Request::Request(const std::string &url, res(std::make_unique(callback)), cancelled(false), loop(loop) { - // Add a check handle without a callback to keep the default loop running. - // We don't have a real handler attached to the default loop right from the - // beginning, because we're using asynchronous messaging to perform the actual - // request in the request thread. Only after the request is complete, we - // create an actual work request that is attached to the default loop. - async = new uv_async_t(); - async->data = new std::unique_ptr(); - uv_async_init(loop, async, complete); + if (loop) { + // Add a check handle without a callback to keep the default loop running. + // We don't have a real handler attached to the default loop right from the + // beginning, because we're using asynchronous messaging to perform the actual + // request in the request thread. Only after the request is complete, we + // create an actual work request that is attached to the default loop. + async = new uv_async_t(); + async->data = new std::unique_ptr(); + uv_async_init(loop, async, complete); + } } Request::~Request() { } void Request::complete() { - // We're scheduling the response callback to be invoked in the event loop. - // Since the Request object will be deleted before the callback is invoked, - // we move over the Response object to be owned by the async handle. - ((std::unique_ptr *)async->data)->swap(res); - uv_async_send(async); + if (loop) { + // We're scheduling the response callback to be invoked in the event loop. + // Since the Request object will be deleted before the callback is invoked, + // we move over the Response object to be owned by the async handle. + ((std::unique_ptr *)async->data)->swap(res); + uv_async_send(async); + } else { + // We're calling the response callback immediately. We're currently on an + // arbitrary thread, but that's okay. + res->callback(res.get()); + } } void Request::complete(uv_async_t *async) { diff --git a/src/renderer/text_bucket.cpp b/src/renderer/text_bucket.cpp index 0fd351343..5823607ac 100644 --- a/src/renderer/text_bucket.cpp +++ b/src/renderer/text_bucket.cpp @@ -7,6 +7,8 @@ #include #include #include +#include +#include #include #include @@ -98,17 +100,23 @@ void TextBucket::addGlyphs(const PlacedGlyphs &glyphs, float placementZoom, }; void TextBucket::addFeature(const VectorTileFeature &feature, - const IndexedFaces &faces, + const GlyphPositions &face, const std::map &shapings) { auto it_prop = feature.properties.find(geom_desc.field); if (it_prop == feature.properties.end()) { // feature does not have the correct property + if (debug::labelTextMissingWarning) { + fprintf(stderr, "[WARNING] feature doesn't have property '%s' required for labelling\n", geom_desc.field.c_str()); + } return; } const Value &value = it_prop->second; - auto it_shaping = shapings.find(value); + auto it_shaping = shapings.find(toString(value)); if (it_shaping == shapings.end()) { + if (debug::shapingWarning) { + fprintf(stderr, "[WARNING] missing shaping for '%s'\n", toString(value).c_str()); + } // we lack shaping information for this label return; } @@ -127,14 +135,14 @@ void TextBucket::addFeature(const VectorTileFeature &feature, while ((cmd = geometry.next(x, y)) != Geometry::end) { if (cmd == Geometry::move_to) { if (!line.empty()) { - placement.addFeature(*this, line, geom_desc, faces, shaping); + placement.addFeature(*this, line, geom_desc, face, shaping); line.clear(); } } line.emplace_back(x, y); } if (line.size()) { - placement.addFeature(*this, line, geom_desc, faces, shaping); + placement.addFeature(*this, line, geom_desc, face, shaping); } } diff --git a/src/style/properties.cpp b/src/style/properties.cpp index 0e4841830..b39f20c51 100644 --- a/src/style/properties.cpp +++ b/src/style/properties.cpp @@ -29,9 +29,11 @@ float functions::stops(float z, const std::vector& stops) { if (stops.size() % 2 != 0) return 0; bool smaller = false; - float smaller_z, smaller_val; + float smaller_z = 0.0; + float smaller_val = 0.0; bool larger = false; - float larger_z, larger_val; + float larger_z = 0.0; + float larger_val = 0.0; for (uint32_t i = 0; i < stops.size(); i += 2) { float stop_z = stops[i]; diff --git a/src/text/glyph.cpp b/src/text/glyph.cpp new file mode 100644 index 000000000..882a8c493 --- /dev/null +++ b/src/text/glyph.cpp @@ -0,0 +1,17 @@ +#include + +namespace llmr { + +// Note: this only works for the BMP +// Note: we could use a binary lookup table to get averaged constant time lookups, however, +// most of our lookups are going to be within the first 3 ranges listed here, so this is +// likely faster. +GlyphRange getGlyphRange(uint32_t glyph) { + unsigned start = (glyph/256) * 256; + unsigned end = (start + 255); + if (start > 65280) start = 65280; + if (end > 65533) end = 65533; + return { start, end }; +} + +} diff --git a/src/text/glyph_store.cpp b/src/text/glyph_store.cpp new file mode 100644 index 000000000..3f741236f --- /dev/null +++ b/src/text/glyph_store.cpp @@ -0,0 +1,185 @@ +#include + +#include +#include +#include +#include +#include +#include +#include + +namespace llmr { + + +void FontStack::insert(uint32_t id, const SDFGlyph &glyph) { + std::lock_guard lock(mtx); + metrics.emplace(id, glyph.metrics); + bitmaps.emplace(id, glyph.bitmap); + sdfs.emplace(id, glyph); +} + +const std::map &FontStack::getMetrics() const { + std::lock_guard lock(mtx); + return metrics; +} + +const std::map &FontStack::getSDFs() const { + std::lock_guard lock(mtx); + return sdfs; +} + +const Shaping FontStack::getShaping(const std::u32string &string) const { + std::lock_guard lock(mtx); + uint32_t i = 0; + uint32_t x = 0; + Shaping shaped; + // Loop through all characters of this label and shape. + for (uint32_t chr : string) { + GlyphPlacement glyph = GlyphPlacement(0, chr, x, 0); + shaped.push_back(glyph); + i++; + x += metrics.find(chr)->second.advance; + } + return shaped; +} + +GlyphPBF::GlyphPBF(const std::string &fontStack, GlyphRange glyphRange) + : future(promise.get_future().share()) +{ + // Load the glyph set URL + std::string url = util::sprintf<255>("http://mapbox.s3.amazonaws.com/gl-glyphs-256/%s/%d-%d.pbf", fontStack.c_str(), glyphRange.first, glyphRange.second); + + // TODO: Find more reliable URL normalization function + std::replace(url.begin(), url.end(), ' ', '+'); + + fprintf(stderr, "%s\n", url.c_str()); + + platform::request_http(url, [&](platform::Response *res) { + if (res->code != 200) { + // Something went wrong with loading the glyph pbf. Pass on the error to the future listeners. + const std::string msg = util::sprintf<255>("[ERROR] failed to load glyphs (%d): %s\n", res->code, res->error_message.c_str()); + promise.set_exception(std::make_exception_ptr(std::runtime_error(msg))); + } else { + // Transfer the data to the GlyphSet and signal its availability. + // Once it is available, the caller will need to call parse() to actually + // parse the data we received. We are not doing this here since this callback is being + // called from another (unknown) thread. + data.swap(res->body); + promise.set_value(*this); + } + }); +} + +std::shared_future GlyphPBF::getFuture() { + return future; +} + +void GlyphPBF::parse(FontStack &stack) { + std::lock_guard lock(mtx); + + if (!data.size()) { + // If there is no data, this means we either haven't received any data, or + // we have already parsed the data. + return; + } + + // Parse the glyph PBF + pbf glyphs_pbf(reinterpret_cast(data.data()), data.size()); + + while (glyphs_pbf.next()) { + if (glyphs_pbf.tag == 1) { // stacks + pbf fontstack_pbf = glyphs_pbf.message(); + while (fontstack_pbf.next()) { + if (fontstack_pbf.tag == 3) { // glyphs + pbf glyph_pbf = fontstack_pbf.message(); + + SDFGlyph glyph; + + while (glyph_pbf.next()) { + if (glyph_pbf.tag == 1) { // id + glyph.id = glyph_pbf.varint(); + } else if (glyph_pbf.tag == 2) { // bitmap + glyph.bitmap = glyph_pbf.string(); + } else if (glyph_pbf.tag == 3) { // width + glyph.metrics.width = glyph_pbf.varint(); + } else if (glyph_pbf.tag == 4) { // height + glyph.metrics.height = glyph_pbf.varint(); + } else if (glyph_pbf.tag == 5) { // left + glyph.metrics.left = glyph_pbf.svarint(); + } else if (glyph_pbf.tag == 6) { // top + glyph.metrics.top = glyph_pbf.svarint(); + } else if (glyph_pbf.tag == 7) { // advance + glyph.metrics.advance = glyph_pbf.varint(); + } else { + glyph_pbf.skip(); + } + } + + stack.insert(glyph.id, glyph); + } else { + fontstack_pbf.skip(); + } + } + } else { + glyphs_pbf.skip(); + } + } + + data.clear(); +} + + +void GlyphStore::waitForGlyphRanges(const std::string &fontStack, const std::set &glyphRanges) { + // We are implementing a blocking wait with futures: Every GlyphSet has a future that we are + // waiting for until it is loaded. + + FontStack *stack = nullptr; + + std::vector> futures; + futures.reserve(glyphRanges.size()); + { + std::lock_guard lock(mtx); + auto &rangeSets = ranges[fontStack]; + + stack = &createFontStack(fontStack); + + // Attempt to load the glyph range. If the GlyphSet already exists, we are getting back + // the same shared_future. + for (GlyphRange range : glyphRanges) { + futures.emplace_back(loadGlyphRange(fontStack, rangeSets, range)); + } + } + + // Now that we potentially created all GlyphSets, we are waiting for the results, one by one. + // When we get a result (or the GlyphSet is aready loaded), we are attempting to parse the + // GlyphSet. + for (std::shared_future &future : futures) { + future.get().parse(*stack); + } +} + +std::shared_future GlyphStore::loadGlyphRange(const std::string &name, std::map> &rangeSets, const GlyphRange range) { + auto range_it = rangeSets.find(range); + if (range_it == rangeSets.end()) { + // We don't have this glyph set yet for this font stack. + range_it = rangeSets.emplace(range, std::make_unique(name, range)).first; + } + + return range_it->second->getFuture(); +} + +FontStack &GlyphStore::createFontStack(const std::string &fontStack) { + auto stack_it = stacks.find(fontStack); + if (stack_it == stacks.end()) { + stack_it = stacks.emplace(fontStack, std::make_unique()).first; + } + return *stack_it->second.get(); +} + +FontStack &GlyphStore::getFontStack(const std::string &fontStack) { + std::lock_guard lock(mtx); + return createFontStack(fontStack); +} + + +} diff --git a/src/text/placement.cpp b/src/text/placement.cpp index e1d7b55bc..f57436bcb 100644 --- a/src/text/placement.cpp +++ b/src/text/placement.cpp @@ -35,21 +35,12 @@ bool byScale(const Anchor &a, const Anchor &b) { return a.scale < b.scale; } static const Glyph null_glyph; inline const Glyph &getGlyph(const GlyphPlacement &placed, - const IndexedFaces &faces) { - if (placed.face < faces.size()) { - const GlyphPositions &face = *faces[placed.face]; - if (&face) { - auto it = face.find(placed.glyph); - if (it != face.end()) { - return it->second; - } else { - fprintf(stderr, "glyph %d does not exist\n", placed.glyph); - } - } else { - fprintf(stderr, "face pointer is null\n"); - } + const GlyphPositions &face) { + auto it = face.find(placed.glyph); + if (it != face.end()) { + return it->second; } else { - fprintf(stderr, "face does not exist\n"); + fprintf(stderr, "glyph %d does not exist\n", placed.glyph); } return null_glyph; @@ -147,7 +138,7 @@ void getSegmentGlyphs(std::back_insert_iterator glyphs, void getGlyphs(PlacedGlyphs &glyphs, GlyphBoxes &boxes, Anchor &anchor, float advance, const Shaping &shaping, - const IndexedFaces &faces, float fontScale, + const GlyphPositions &face, float fontScale, bool horizontal, const std::vector &line, float maxAngleDelta, float rotate) { // The total text advance is the width of this label. @@ -166,7 +157,7 @@ void getGlyphs(PlacedGlyphs &glyphs, GlyphBoxes &boxes, const uint32_t buffer = 3; for (const GlyphPlacement &placed : shaping) { - const Glyph &glyph = getGlyph(placed, faces); + const Glyph &glyph = getGlyph(placed, face); if (!glyph) { // This glyph is empty and doesn't have any pixels that we'd need to // show. @@ -259,7 +250,7 @@ void getGlyphs(PlacedGlyphs &glyphs, GlyphBoxes &boxes, void Placement::addFeature(TextBucket& bucket, const std::vector &line, const BucketGeometryDescription &info, - const IndexedFaces &faces, + const GlyphPositions &face, const Shaping &shaping) { const bool horizontal = info.path == TextPathType::Horizontal; @@ -270,7 +261,7 @@ void Placement::addFeature(TextBucket& bucket, const float fontScale = (tileExtent / util::tileSize) / (glyphSize / info.size); - const float advance = measureText(faces, shaping); + const float advance = measureText(face, shaping); Anchors anchors; // fprintf(stderr, "adding feature with advance %f\n", advance); @@ -294,7 +285,7 @@ void Placement::addFeature(TextBucket& bucket, PlacedGlyphs glyphs; GlyphBoxes boxes; - getGlyphs(glyphs, boxes, anchor, advance, shaping, faces, fontScale, horizontal, + getGlyphs(glyphs, boxes, anchor, advance, shaping, face, fontScale, horizontal, line, maxAngleDelta, rotate); PlacementProperty place = collision.place(boxes, anchor, anchor.scale, maxPlacementScale, @@ -305,14 +296,14 @@ void Placement::addFeature(TextBucket& bucket, } } -float Placement::measureText(const IndexedFaces &faces, +float Placement::measureText(const GlyphPositions &face, const Shaping &shaping) { float advance = 0; // TODO: advance is not calculated correctly. we should instead use the // bounding box of the glyph placement. for (const GlyphPlacement &shape : shaping) { - advance += getGlyph(shape, faces).metrics.advance; + advance += getGlyph(shape, face).metrics.advance; } return advance; diff --git a/src/util/constants.cpp b/src/util/constants.cpp index 2a4ecd374..f67464926 100644 --- a/src/util/constants.cpp +++ b/src/util/constants.cpp @@ -7,3 +7,8 @@ const bool llmr::debug::styleParseWarnings = false; const bool llmr::debug::spriteWarnings = false; const bool llmr::debug::renderWarnings = false; const bool llmr::debug::renderTree = false; +const bool llmr::debug::labelTextMissingWarning = true; +const bool llmr::debug::missingFontStackWarning = true; +const bool llmr::debug::missingFontFaceWarning = true; +const bool llmr::debug::glyphWarning = true; +const bool llmr::debug::shapingWarning = true; diff --git a/src/util/threadpool.cpp b/src/util/threadpool.cpp index 3b69302b1..29ab11f1c 100644 --- a/src/util/threadpool.cpp +++ b/src/util/threadpool.cpp @@ -1,6 +1,7 @@ #include #include #include +#include using namespace llmr::util;