From a81a336638000f1986f1bde56e909a07b2a6ae36 Mon Sep 17 00:00:00 2001 From: "Justin R. Miller" Date: Sat, 31 May 2014 11:32:42 -0700 Subject: [PATCH 1/2] programmatic styling API --- .gitignore | 7 +- README.md | 6 +- bin/build-style.js | 36 +-- bin/style.js | 214 ++++++++++++++++-- common/glfw_view.cpp | 2 +- include/llmr/map/map.hpp | 14 +- include/llmr/style/style.hpp | 4 +- ios/MBXViewController.mm | 6 +- ios/MVKMapKit | 2 +- ios/img/{toggleRaster.png => toggleStyle.png} | Bin ios/llmr-app.gyp | 9 +- linux/llmr-app.gyp | 1 + llmr.gyp | 13 +- macosx/llmr-app.gyp | 1 + macosx/main.mm | 10 +- setup-libraries.sh | 1 + src/map/map.cpp | 65 ++++-- src/map/tile_parser.cpp | 12 +- src/style/style.cpp | 16 +- 19 files changed, 301 insertions(+), 118 deletions(-) rename ios/img/{toggleRaster.png => toggleStyle.png} (100%) diff --git a/.gitignore b/.gitignore index 0b4742764..5295ff95f 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,9 @@ /ios/build /config.gypi /config.mk -/build \ No newline at end of file +/build +/bin/node_modules +/include/llmr/shader/shaders.hpp +/src/shader/shaders_gl.cpp +/src/shader/shaders_gles2.cpp +/bin/style.bin.js diff --git a/README.md b/README.md index 47c6a8d1d..bffa8167e 100644 --- a/README.md +++ b/README.md @@ -6,11 +6,13 @@ implemented in C++11, currently targeting iOS, OS X, and Ubuntu Linux. # Depends - Modern C++ compiler that supports `-std=c++11` + - Boost headers - `libpng` - `libuv` - - `libcurl` (depends on OpenSSL; Linux only) - `glfw3` - - Boost + - `libcurl` (depends on OpenSSL; Linux only) + - `libboost_regex` (Linux only) + - Homebrew (for build on OS X) - Python (for build only) - Node.js (for build only) diff --git a/bin/build-style.js b/bin/build-style.js index 932287b89..c69fcb8b4 100755 --- a/bin/build-style.js +++ b/bin/build-style.js @@ -7,36 +7,6 @@ var mkdirp = require('./mkdirp'); var name = 'style'; var data = JSON.stringify(require(path.join(process.cwd(), process.argv[2]))); - -var length = data.length; - -var header = '// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED.\n\n'; -header += '#ifndef LLMR_STYLE_RESOURCES\n'; -header += '#define LLMR_STYLE_RESOURCES\n'; -header += '\n'; -header += 'namespace llmr {\n'; -header += 'namespace resources {\n'; -header += '\n'; -header += 'extern const unsigned char ' + name + '[];\n'; -header += 'extern const unsigned long ' + name + '_size;\n'; -header += '\n'; -header += '}\n'; -header += '}\n'; -header += '\n'; -header += '#endif\n'; - -var header_path = path.join(process.argv[3], 'include/llmr/style/resources.hpp'); -mkdirp.sync(path.dirname(header_path)); -fs.writeFileSync(header_path, header); - -var code = '// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED.\n'; -code += '#include \n'; -code += '\n'; -code += 'using namespace llmr;\n'; -code += '\n'; -code += 'const unsigned char resources::' + name + '[] = R"JSON(' + data + ')JSON";\n'; -code += 'const unsigned long resources::' + name + '_size = sizeof(resources::' + name + ');\n'; - -var file_path = path.join(process.argv[3], 'src/style/resources.cpp'); -mkdirp.sync(path.dirname(file_path)); -fs.writeFileSync(file_path, code); +var out_path = path.join(process.argv[3], 'bin/style.min.js'); +mkdirp.sync(path.dirname(out_path)); +fs.writeFileSync(out_path, data); diff --git a/bin/style.js b/bin/style.js index 0a389f6be..ffa326525 100644 --- a/bin/style.js +++ b/bin/style.js @@ -661,15 +661,61 @@ module.exports = { "layer": "country_label_line", "type": "line" }, - "marine_label_line": { + "marine_label_line_1": { "source": "outdoors", "layer": "marine_label", "feature_type": "line", "type": "text", + "field": "labelrank", + "value": 1, "text_field": "{{name_en}}", "path": "curve", "font": "Open Sans Semibold Italic, Arial Unicode MS Bold", - "fontSize": 16 + "fontSize": 30, + "letterSpacing": 0.4, + "maxAngleDelta": 0.5 + }, + "marine_label_line_2": { + "source": "outdoors", + "layer": "marine_label", + "feature_type": "line", + "field": "labelrank", + "value": 2, + "type": "text", + "text_field": "{{name_en}}", + "path": "curve", + "font": "Open Sans Semibold Italic, Arial Unicode MS Bold", + "fontSize": 24, + "letterSpacing": 0.2, + "maxAngleDelta": 0.5 + }, + "marine_label_line_3": { + "source": "outdoors", + "layer": "marine_label", + "feature_type": "line", + "type": "text", + "field": "labelrank", + "value": 3, + "text_field": "{{name_en}}", + "path": "curve", + "font": "Open Sans Semibold Italic, Arial Unicode MS Bold", + "fontSize": 18, + "letterSpacing": 0.1, + "maxAngleDelta": 0.5 + }, + "marine_label_line_other": { + "source": "outdoors", + "layer": "marine_label", + "feature_type": "line", + "type": "text", + "field": "labelrank", + "value": [4, 5, 6], + "text_field": "{{name_en}}", + "path": "curve", + "font": "Open Sans Semibold Italic, Arial Unicode MS Bold", + "fontSize": 16, + "letterSpacing": 0.1, + "maxAngleDelta": 0.5 }, "marine_label_point_1": { "source": "outdoors", @@ -698,9 +744,24 @@ module.exports = { "font": "Open Sans Semibold Italic, Arial Unicode MS Bold", "fontSize": 24, "maxWidth": 8, - "letterSpacing": 0.4, + "letterSpacing": 0.2, "lineHeight": 1.5, }, + "marine_label_point_3": { + "source": "outdoors", + "layer": "marine_label", + "feature_type": "point", + "type": "text", + "text_field": "{{name_en}}", + "path": "horizontal", + "field": "labelrank", + "value": 3, + "font": "Open Sans Semibold Italic, Arial Unicode MS Bold", + "fontSize": 18, + "maxWidth": 8, + "letterSpacing": 0.1, + "lineHeight": 1.3, + }, "marine_label_point_other": { "source": "outdoors", "layer": "marine_label", @@ -709,9 +770,12 @@ module.exports = { "text_field": "{{name_en}}", "path": "horizontal", "field": "labelrank", - "value": [3,4,5,6], + "value": [4,5,6], "font": "Open Sans Semibold Italic, Arial Unicode MS Bold", - "fontSize": 18 + "fontSize": 16, + "maxWidth": 8, + "letterSpacing": 0.1, + "lineHeight": 1.2, }, "state_label": { "source": "outdoors", @@ -1363,8 +1427,20 @@ module.exports = { "bucket": "country_label" }, { - "name": "marine_label_line", - "bucket": "marine_label_line" + "name": "marine_label_line_1", + "bucket": "marine_label_line_1" + }, + { + "name": "marine_label_line_2", + "bucket": "marine_label_line_2" + }, + { + "name": "marine_label_line_3", + "bucket": "marine_label_line_3" + }, + { + "name": "marine_label_line_other", + "bucket": "marine_label_line_other" }, { "name": "marine_label_point_1", @@ -1374,6 +1450,10 @@ module.exports = { "name": "marine_label_point_2", "bucket": "marine_label_point_2" }, + { + "name": "marine_label_point_3", + "bucket": "marine_label_point_3" + }, { "name": "marine_label_point_other", "bucket": "marine_label_point_other" @@ -2309,12 +2389,8 @@ module.exports = { "width": 0.5, "opacity": 0.5 }, - "marine_label_line": { + "marine_label_line_1": { "color": "marine_text", - "stroke": "water" - }, - "marine_label_point_1": { - "color": "#a9c4c7", "size": ["stops", {"z": 3, "val": 20}, {"z": 4, "val": 25}, @@ -2323,8 +2399,8 @@ module.exports = { ], "stroke": "water" }, - "marine_label_point_2": { - "color": "#a9c4c7", + "marine_label_line_2": { + "color": "marine_text", "size": ["stops", {"z": 3, "val": 13}, {"z": 4, "val": 14}, @@ -2334,8 +2410,8 @@ module.exports = { ], "stroke": "water" }, - "marine_label_point_other": { - "color": "#a9c4c7", + "marine_label_line_3": { + "color": "marine_text", "size": ["stops", {"z": 3, "val": 12}, {"z": 4, "val": 13}, @@ -2345,6 +2421,58 @@ module.exports = { ], "stroke": "water" }, + "marine_label_line_other": { + "color": "marine_text", + "size": ["stops", + {"z": 4, "val": 12}, + {"z": 5, "val": 14}, + {"z": 6, "val": 16}, + {"z": 22, "val": 16} + ], + "stroke": "water" + }, + "marine_label_point_1": { + "color": "marine_text", + "size": ["stops", + {"z": 3, "val": 20}, + {"z": 4, "val": 25}, + {"z": 5, "val": 30}, + {"z": 22, "val": 30} + ], + "stroke": "water" + }, + "marine_label_point_2": { + "color": "marine_text", + "size": ["stops", + {"z": 3, "val": 13}, + {"z": 4, "val": 14}, + {"z": 5, "val": 20}, + {"z": 6, "val": 24}, + {"z": 22, "val": 24} + ], + "stroke": "water" + }, + "marine_label_point_3": { + "color": "marine_text", + "size": ["stops", + {"z": 3, "val": 12}, + {"z": 4, "val": 13}, + {"z": 5, "val": 15}, + {"z": 6, "val": 18}, + {"z": 22, "val": 18} + ], + "stroke": "water" + }, + "marine_label_point_other": { + "color": "marine_text", + "size": ["stops", + {"z": 4, "val": 12}, + {"z": 5, "val": 14}, + {"z": 6, "val": 16}, + {"z": 22, "val": 16} + ], + "stroke": "water" + }, "state_label": { "color": "#333", "strokeWidth": 0.4, @@ -2500,7 +2628,7 @@ module.exports = { } }, { - "name": "satellite", + "name": "night", "layers": { "background": { "color": "land_night" @@ -3067,8 +3195,46 @@ module.exports = { "width": 0.5, "opacity": 0.5 }, - "marine_label_line": { + "marine_label_line_1": { "color": "water_dark_night", + "size": ["stops", + {"z": 3, "val": 20}, + {"z": 4, "val": 25}, + {"z": 5, "val": 30}, + {"z": 22, "val": 30} + ], + "stroke": "water_night" + }, + "marine_label_line_2": { + "color": "water_dark_night", + "size": ["stops", + {"z": 3, "val": 13}, + {"z": 4, "val": 14}, + {"z": 5, "val": 20}, + {"z": 6, "val": 24}, + {"z": 22, "val": 24} + ], + "stroke": "water_night" + }, + "marine_label_line_3": { + "color": "water_dark_night", + "size": ["stops", + {"z": 3, "val": 12}, + {"z": 4, "val": 13}, + {"z": 5, "val": 15}, + {"z": 6, "val": 18}, + {"z": 22, "val": 18} + ], + "stroke": "water_night" + }, + "marine_label_line_other": { + "color": "water_dark_night", + "size": ["stops", + {"z": 4, "val": 12}, + {"z": 5, "val": 14}, + {"z": 6, "val": 16}, + {"z": 22, "val": 16} + ], "stroke": "water_night" }, "marine_label_point_1": { @@ -3092,7 +3258,7 @@ module.exports = { ], "stroke": "water_night" }, - "marine_label_point_other": { + "marine_label_point_3": { "color": "water_dark_night", "size": ["stops", {"z": 3, "val": 12}, @@ -3103,6 +3269,16 @@ module.exports = { ], "stroke": "water_night" }, + "marine_label_point_other": { + "color": "water_dark_night", + "size": ["stops", + {"z": 4, "val": 12}, + {"z": 5, "val": 14}, + {"z": 6, "val": 16}, + {"z": 22, "val": 16} + ], + "stroke": "water_night" + }, "state_label": { "color": "#fff", "strokeWidth": 0.4, diff --git a/common/glfw_view.cpp b/common/glfw_view.cpp index 3cc50b591..5c623d725 100644 --- a/common/glfw_view.cpp +++ b/common/glfw_view.cpp @@ -74,7 +74,7 @@ void GLFWView::key(GLFWwindow *window, int key, int /*scancode*/, int action, in break; case GLFW_KEY_R: if (!mods) - view->map->toggleRaster(); + view->map->toggleStyle(); break; case GLFW_KEY_N: if (!mods) diff --git a/include/llmr/map/map.hpp b/include/llmr/map/map.hpp index 8874172c3..d5d11dde8 100644 --- a/include/llmr/map/map.hpp +++ b/include/llmr/map/map.hpp @@ -52,6 +52,14 @@ public: void resize(uint16_t width, uint16_t height, float ratio = 1); void resize(uint16_t width, uint16_t height, float ratio, uint16_t fb_width, uint16_t fb_height); + // Styling + void toggleStyle(); + void setAppliedClasses(std::set appliedClasses); + std::set getAppliedClasses() const; + void setDefaultTransitionDuration(uint64_t duration_milliseconds = 0); + void setStyleJSON(std::string newStyleJSON); + std::string getStyleJSON() const; + // Transition void cancelTransitions(); @@ -92,9 +100,6 @@ public: void toggleDebug(); bool getDebug() const; - // TEMPORARY DEBUG API - void toggleRaster(); - public: inline const TransformState &getState() const { return state; } inline std::shared_ptr getStyle() const { return style; } @@ -115,7 +120,6 @@ private: // Setup void setup(); - void loadStyle(const uint8_t *const data, uint32_t bytes); void updateTiles(); void updateRenderState(); @@ -161,6 +165,8 @@ private: Sources sources; + std::string styleJSON = ""; + bool debug = false; time animationTime = 0; diff --git a/include/llmr/style/style.hpp b/include/llmr/style/style.hpp index 97abf586e..58202393d 100644 --- a/include/llmr/style/style.hpp +++ b/include/llmr/style/style.hpp @@ -30,7 +30,7 @@ public: void reset(); - void loadJSON(const uint8_t *const data, size_t bytes); + void loadJSON(const uint8_t *const data); size_t layerCount() const; void cascade(float z); @@ -39,7 +39,7 @@ public: void updateTransitions(time now); void cancelTransitions(); - void setDefaultTransitionDuration(uint64_t duration = 0); + void setDefaultTransitionDuration(uint64_t duration_milliseconds = 0); public: std::shared_ptr sprite; diff --git a/ios/MBXViewController.mm b/ios/MBXViewController.mm index 25fca0599..213fdceeb 100644 --- a/ios/MBXViewController.mm +++ b/ios/MBXViewController.mm @@ -85,7 +85,7 @@ llmr::Settings_NSUserDefaults *settings = nullptr; singleTap.numberOfTapsRequired = 1; [self.mapView addGestureRecognizer:singleTap]; - NSArray *selectorNames = @[ @"unrotate", @"resetPosition", @"toggleDebug", @"toggleRaster", @"locateUser" ]; + NSArray *selectorNames = @[ @"unrotate", @"resetPosition", @"toggleDebug", @"toggleStyle", @"locateUser" ]; CGFloat buttonSize = 40; CGFloat bufferSize = 20; CGFloat alpha = 0.75; @@ -166,9 +166,9 @@ llmr::Settings_NSUserDefaults *settings = nullptr; self.debug = ! self.debug; } -- (void)toggleRaster +- (void)toggleStyle { - [self.mapView toggleRaster]; + [self.mapView toggleStyle]; } - (void)locateUser diff --git a/ios/MVKMapKit b/ios/MVKMapKit index 7c7bb2310..fe12687eb 160000 --- a/ios/MVKMapKit +++ b/ios/MVKMapKit @@ -1 +1 @@ -Subproject commit 7c7bb2310216a8d18108db40b09db08732df1f88 +Subproject commit fe12687eb95b925b37a260596f309931a745d828 diff --git a/ios/img/toggleRaster.png b/ios/img/toggleStyle.png similarity index 100% rename from ios/img/toggleRaster.png rename to ios/img/toggleStyle.png diff --git a/ios/llmr-app.gyp b/ios/llmr-app.gyp index 5d7e1f8e3..89acc2d74 100644 --- a/ios/llmr-app.gyp +++ b/ios/llmr-app.gyp @@ -14,8 +14,10 @@ "./MBXAppDelegate.m", "./MBXViewController.h", "./MBXViewController.mm", - "./MVKMapKit/MVKMapKit/MVKMapView.h", - "./MVKMapKit/MVKMapKit/MVKMapView.mm", + ' + int main() { GLFWView view; llmr::Map map(view); @@ -10,7 +12,13 @@ int main() { map.setLonLatZoom(settings.longitude, settings.latitude, settings.zoom); map.setAngle(settings.angle); map.setDebug(settings.debug); - map.toggleRaster(); + + // Load style + NSString *path = [[NSBundle mainBundle] pathForResource:@"style.min" ofType:@"js"]; + NSString *json = [NSString stringWithContentsOfFile:path + encoding:[NSString defaultCStringEncoding] + error:nil]; + map.setStyleJSON((std::string)[json cStringUsingEncoding:[NSString defaultCStringEncoding]]); // fprintf(stderr, "lon: %f, lat: %f, zoom: %f, angle: %f, debug: %d\n", settings.l) diff --git a/setup-libraries.sh b/setup-libraries.sh index f37a67cb0..e0b03637e 100755 --- a/setup-libraries.sh +++ b/setup-libraries.sh @@ -95,6 +95,7 @@ source Linux.sh 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`/../../include/ `pwd`/../../linux/ `pwd`/../../common/ ; fi + if [ ! -d out/build-cpp11-libstdcpp-gcc-x86_64/lib/libboost_regex.a ] ; then ./scripts/build_boost.sh --with-regex ; fi cd ../../ ./configure \ diff --git a/src/map/map.cpp b/src/map/map.cpp index c4c736d1a..305b523c1 100644 --- a/src/map/map.cpp +++ b/src/map/map.cpp @@ -174,25 +174,20 @@ void Map::setup() { 14, true))); - sources.emplace("satellite", - std::unique_ptr(new Source(*this, - painter, - "https://a.tiles.mapbox.com/v3/justin.hh0gkdfm/%d/%d/%d%s.png256", - Source::Type::raster, - {{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21 }}, - 256, - 0, - 21, - false))); - - loadStyle(resources::style, resources::style_size); + setStyleJSON(styleJSON); } -void Map::loadStyle(const uint8_t *const data, uint32_t bytes) { - style->loadJSON(data, bytes); +void Map::setStyleJSON(std::string newStyleJSON) { + styleJSON.swap(newStyleJSON); + style->cancelTransitions(); + style->loadJSON((const uint8_t *)styleJSON.c_str()); update(); } +std::string Map::getStyleJSON() const { + return styleJSON; +} + #pragma mark - Size // Note: This function is called from another thread. Make sure you only call threadsafe functions! @@ -388,25 +383,45 @@ bool Map::getDebug() const { return debug; } -void Map::toggleRaster() { - style->setDefaultTransitionDuration(300); +void Map::toggleStyle() { + setDefaultTransitionDuration(300); style->cancelTransitions(); - auto it = sources.find("satellite"); - if (it != sources.end()) { - Source &satellite_source = *it->second; - if (satellite_source.enabled) { - satellite_source.enabled = false; - style->appliedClasses.erase("satellite"); - } else { - satellite_source.enabled = true; - style->appliedClasses.insert("satellite"); + if (style->appliedClasses.size() == 1) { + for (auto source_it = style->classes.begin(); source_it != style->classes.end(); source_it++) { + if (source_it->first != "default") { + style->appliedClasses.insert(source_it->first); + break; + } + } + } else { + for (auto source_it = style->classes.begin(); source_it != style->classes.end(); source_it++) { + if (source_it->first != "default") { + style->appliedClasses.erase(source_it->first); + break; + } } } update(); } +void Map::setAppliedClasses(std::set appliedClasses) { + style->cancelTransitions(); + + style->appliedClasses.swap(appliedClasses); + + update(); +} + +std::set Map::getAppliedClasses() const { + return style->appliedClasses; +} + +void Map::setDefaultTransitionDuration(uint64_t duration_milliseconds) { + style->setDefaultTransitionDuration(duration_milliseconds); +} + void Map::updateTiles() { for (auto &pair : sources) { const std::unique_ptr &source = pair.second; diff --git a/src/map/tile_parser.cpp b/src/map/tile_parser.cpp index ecd15adbb..ad0fb2d71 100644 --- a/src/map/tile_parser.cpp +++ b/src/map/tile_parser.cpp @@ -16,7 +16,13 @@ #include #include +#ifdef __linux__ +#include +namespace regex_impl = boost; +#else #include +namespace regex_impl = std; +#endif using namespace llmr; @@ -184,8 +190,8 @@ std::unique_ptr TileParser::createTextBucket(const VectorTileLayer &laye tile.textVertexBuffer, tile.triangleElementsBuffer, bucket_desc, placement); util::utf8_to_utf32 ucs4conv; - std::regex token_regex("\\{\\{(\\w+)\\}\\}"); - const auto tokens_end = std::sregex_token_iterator(); + regex_impl::regex token_regex("\\{\\{(\\w+)\\}\\}"); + const auto tokens_end = regex_impl::sregex_token_iterator(); std::vector> labels; @@ -205,7 +211,7 @@ std::unique_ptr TileParser::createTextBucket(const VectorTileLayer &laye source_string.reserve(field.size()); bool token = false; - for (auto token_it = std::sregex_token_iterator(field.begin(), field.end(), token_regex, {-1, 1}); token_it != tokens_end; ++token_it, token = !token) { + for (auto token_it = regex_impl::sregex_token_iterator(field.begin(), field.end(), token_regex, {-1, 1}); token_it != tokens_end; ++token_it, token = !token) { if (!token_it->matched) { continue; } diff --git a/src/style/style.cpp b/src/style/style.cpp index f1694694c..9ae0b273f 100644 --- a/src/style/style.cpp +++ b/src/style/style.cpp @@ -965,21 +965,15 @@ size_t Style::layerCount() const { return count; } -void Style::setDefaultTransitionDuration(uint64_t duration) { - default_transition_duration = duration; +void Style::setDefaultTransitionDuration(uint64_t duration_milliseconds) { + default_transition_duration = duration_milliseconds; } -void Style::loadJSON(const uint8_t *const data, size_t bytes) { +void Style::loadJSON(const uint8_t *const data) { + uv::writelock lock(mtx); + rapidjson::Document doc; - if (bytes <= 0) { - return; - } - - if (data[bytes - 1] != 0) { - throw exception("style JSON string is not 0-terminated"); - } - doc.Parse<0>((const char *const)data); const rapidjson::Document& document = doc; From eb35c0181d60011008672a96a83b4c61b6fa6c1c Mon Sep 17 00:00:00 2001 From: "Justin R. Miller" Date: Sat, 31 May 2014 11:49:01 -0700 Subject: [PATCH 2/2] bump MVK to squashed styling work --- ios/MVKMapKit | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ios/MVKMapKit b/ios/MVKMapKit index fe12687eb..113cec55d 160000 --- a/ios/MVKMapKit +++ b/ios/MVKMapKit @@ -1 +1 @@ -Subproject commit fe12687eb95b925b37a260596f309931a745d828 +Subproject commit 113cec55d080fc407d0c3d37f4aa36323749c1ec