@mapwhit/tilerenderer 0.47.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.txt +87 -0
- package/README.md +25 -0
- package/build/min/.dir +0 -0
- package/build/min/package.json +3 -0
- package/build/min/src/shaders/_prelude.fragment.glsl.txt +13 -0
- package/build/min/src/shaders/_prelude.vertex.glsl.txt +14 -0
- package/build/min/src/shaders/background.fragment.glsl.txt +5 -0
- package/build/min/src/shaders/background.vertex.glsl.txt +1 -0
- package/build/min/src/shaders/background_pattern.fragment.glsl.txt +5 -0
- package/build/min/src/shaders/background_pattern.vertex.glsl.txt +1 -0
- package/build/min/src/shaders/circle.fragment.glsl.txt +20 -0
- package/build/min/src/shaders/circle.vertex.glsl.txt +17 -0
- package/build/min/src/shaders/clipping_mask.fragment.glsl.txt +1 -0
- package/build/min/src/shaders/clipping_mask.vertex.glsl.txt +1 -0
- package/build/min/src/shaders/collision_box.fragment.glsl.txt +1 -0
- package/build/min/src/shaders/collision_box.vertex.glsl.txt +1 -0
- package/build/min/src/shaders/collision_circle.fragment.glsl.txt +1 -0
- package/build/min/src/shaders/collision_circle.vertex.glsl.txt +1 -0
- package/build/min/src/shaders/debug.fragment.glsl.txt +1 -0
- package/build/min/src/shaders/debug.vertex.glsl.txt +1 -0
- package/build/min/src/shaders/fill.fragment.glsl.txt +10 -0
- package/build/min/src/shaders/fill.vertex.glsl.txt +7 -0
- package/build/min/src/shaders/fill_extrusion.fragment.glsl.txt +13 -0
- package/build/min/src/shaders/fill_extrusion.vertex.glsl.txt +9 -0
- package/build/min/src/shaders/fill_extrusion_pattern.fragment.glsl.txt +15 -0
- package/build/min/src/shaders/fill_extrusion_pattern.vertex.glsl.txt +11 -0
- package/build/min/src/shaders/fill_outline.fragment.glsl.txt +10 -0
- package/build/min/src/shaders/fill_outline.vertex.glsl.txt +7 -0
- package/build/min/src/shaders/fill_outline_pattern.fragment.glsl.txt +13 -0
- package/build/min/src/shaders/fill_outline_pattern.vertex.glsl.txt +9 -0
- package/build/min/src/shaders/fill_pattern.fragment.glsl.txt +13 -0
- package/build/min/src/shaders/fill_pattern.vertex.glsl.txt +9 -0
- package/build/min/src/shaders/heatmap.fragment.glsl.txt +10 -0
- package/build/min/src/shaders/heatmap.vertex.glsl.txt +8 -0
- package/build/min/src/shaders/heatmap_texture.fragment.glsl.txt +5 -0
- package/build/min/src/shaders/heatmap_texture.vertex.glsl.txt +1 -0
- package/build/min/src/shaders/hillshade.fragment.glsl.txt +7 -0
- package/build/min/src/shaders/hillshade.vertex.glsl.txt +1 -0
- package/build/min/src/shaders/hillshade_prepare.fragment.glsl.txt +8 -0
- package/build/min/src/shaders/hillshade_prepare.vertex.glsl.txt +1 -0
- package/build/min/src/shaders/line.fragment.glsl.txt +12 -0
- package/build/min/src/shaders/line.vertex.glsl.txt +17 -0
- package/build/min/src/shaders/line_gradient.fragment.glsl.txt +10 -0
- package/build/min/src/shaders/line_gradient.vertex.glsl.txt +16 -0
- package/build/min/src/shaders/line_pattern.fragment.glsl.txt +15 -0
- package/build/min/src/shaders/line_pattern.vertex.glsl.txt +20 -0
- package/build/min/src/shaders/line_sdf.fragment.glsl.txt +17 -0
- package/build/min/src/shaders/line_sdf.vertex.glsl.txt +20 -0
- package/build/min/src/shaders/raster.fragment.glsl.txt +5 -0
- package/build/min/src/shaders/raster.vertex.glsl.txt +1 -0
- package/build/min/src/shaders/symbol_icon.fragment.glsl.txt +9 -0
- package/build/min/src/shaders/symbol_icon.vertex.glsl.txt +5 -0
- package/build/min/src/shaders/symbol_sdf.fragment.glsl.txt +19 -0
- package/build/min/src/shaders/symbol_sdf.vertex.glsl.txt +13 -0
- package/package.json +44 -0
- package/src/css/mapbox-gl.css +506 -0
- package/src/css/svg/mapboxgl-ctrl-attrib.svg +3 -0
- package/src/css/svg/mapboxgl-ctrl-compass.svg +4 -0
- package/src/css/svg/mapboxgl-ctrl-fullscreen.svg +3 -0
- package/src/css/svg/mapboxgl-ctrl-geolocate-background.svg +3 -0
- package/src/css/svg/mapboxgl-ctrl-geolocate.svg +3 -0
- package/src/css/svg/mapboxgl-ctrl-logo-compact.svg +2 -0
- package/src/css/svg/mapboxgl-ctrl-logo.svg +1 -0
- package/src/css/svg/mapboxgl-ctrl-shrink.svg +3 -0
- package/src/css/svg/mapboxgl-ctrl-zoom-in.svg +3 -0
- package/src/css/svg/mapboxgl-ctrl-zoom-out.svg +3 -0
- package/src/data/array_types.js +1138 -0
- package/src/data/bucket/circle_attributes.js +5 -0
- package/src/data/bucket/circle_bucket.js +118 -0
- package/src/data/bucket/fill_attributes.js +5 -0
- package/src/data/bucket/fill_bucket.js +166 -0
- package/src/data/bucket/fill_extrusion_attributes.js +11 -0
- package/src/data/bucket/fill_extrusion_bucket.js +247 -0
- package/src/data/bucket/heatmap_bucket.js +12 -0
- package/src/data/bucket/line_attributes.js +11 -0
- package/src/data/bucket/line_bucket.js +625 -0
- package/src/data/bucket/pattern_attributes.js +9 -0
- package/src/data/bucket/pattern_bucket_features.js +44 -0
- package/src/data/bucket/symbol_attributes.js +95 -0
- package/src/data/bucket/symbol_bucket.js +697 -0
- package/src/data/bucket.js +53 -0
- package/src/data/dem_data.js +126 -0
- package/src/data/extent.js +17 -0
- package/src/data/feature_index.js +254 -0
- package/src/data/index_array_type.js +14 -0
- package/src/data/load_geometry.js +42 -0
- package/src/data/pos_attributes.js +3 -0
- package/src/data/program_configuration.js +782 -0
- package/src/data/raster_bounds_attributes.js +6 -0
- package/src/data/segment.js +63 -0
- package/src/geo/coordinate.js +78 -0
- package/src/geo/lng_lat.js +129 -0
- package/src/geo/lng_lat_bounds.js +253 -0
- package/src/geo/transform.js +605 -0
- package/src/gl/color_mode.js +21 -0
- package/src/gl/context.js +193 -0
- package/src/gl/cull_face_mode.js +22 -0
- package/src/gl/depth_mode.js +18 -0
- package/src/gl/framebuffer.js +28 -0
- package/src/gl/index_buffer.js +52 -0
- package/src/gl/stencil_mode.js +17 -0
- package/src/gl/types.js +0 -0
- package/src/gl/value.js +676 -0
- package/src/gl/vertex_buffer.js +101 -0
- package/src/index.js +50 -0
- package/src/render/draw_background.js +60 -0
- package/src/render/draw_circle.js +55 -0
- package/src/render/draw_collision_debug.js +45 -0
- package/src/render/draw_debug.js +429 -0
- package/src/render/draw_fill.js +143 -0
- package/src/render/draw_fill_extrusion.js +101 -0
- package/src/render/draw_heatmap.js +159 -0
- package/src/render/draw_hillshade.js +144 -0
- package/src/render/draw_line.js +99 -0
- package/src/render/draw_raster.js +151 -0
- package/src/render/draw_symbol.js +231 -0
- package/src/render/glyph_atlas.js +55 -0
- package/src/render/glyph_manager.js +145 -0
- package/src/render/image_atlas.js +97 -0
- package/src/render/image_manager.js +183 -0
- package/src/render/line_atlas.js +139 -0
- package/src/render/painter.js +483 -0
- package/src/render/program/background_program.js +46 -0
- package/src/render/program/circle_program.js +40 -0
- package/src/render/program/clipping_mask_program.js +11 -0
- package/src/render/program/collision_program.js +28 -0
- package/src/render/program/debug_program.js +13 -0
- package/src/render/program/fill_extrusion_program.js +76 -0
- package/src/render/program/fill_program.js +60 -0
- package/src/render/program/heatmap_program.js +46 -0
- package/src/render/program/hillshade_program.js +77 -0
- package/src/render/program/line_program.js +119 -0
- package/src/render/program/pattern.js +57 -0
- package/src/render/program/program_uniforms.js +46 -0
- package/src/render/program/raster_program.js +50 -0
- package/src/render/program/symbol_program.js +112 -0
- package/src/render/program.js +133 -0
- package/src/render/texture.js +88 -0
- package/src/render/tile_mask.js +108 -0
- package/src/render/uniform_binding.js +129 -0
- package/src/render/vertex_array_object.js +155 -0
- package/src/shaders/README.md +42 -0
- package/src/shaders/_prelude.fragment.glsl +17 -0
- package/src/shaders/_prelude.vertex.glsl +73 -0
- package/src/shaders/background.fragment.glsl +10 -0
- package/src/shaders/background.vertex.glsl +7 -0
- package/src/shaders/background_pattern.fragment.glsl +28 -0
- package/src/shaders/background_pattern.vertex.glsl +20 -0
- package/src/shaders/circle.fragment.glsl +39 -0
- package/src/shaders/circle.vertex.glsl +63 -0
- package/src/shaders/clipping_mask.fragment.glsl +3 -0
- package/src/shaders/clipping_mask.vertex.glsl +7 -0
- package/src/shaders/collision_box.fragment.glsl +21 -0
- package/src/shaders/collision_box.vertex.glsl +26 -0
- package/src/shaders/collision_circle.fragment.glsl +34 -0
- package/src/shaders/collision_circle.vertex.glsl +36 -0
- package/src/shaders/debug.fragment.glsl +5 -0
- package/src/shaders/debug.vertex.glsl +7 -0
- package/src/shaders/encode_attribute.js +19 -0
- package/src/shaders/fill.fragment.glsl +13 -0
- package/src/shaders/fill.vertex.glsl +13 -0
- package/src/shaders/fill_extrusion.fragment.glsl +16 -0
- package/src/shaders/fill_extrusion.vertex.glsl +66 -0
- package/src/shaders/fill_extrusion_pattern.fragment.glsl +41 -0
- package/src/shaders/fill_extrusion_pattern.vertex.glsl +76 -0
- package/src/shaders/fill_outline.fragment.glsl +17 -0
- package/src/shaders/fill_outline.vertex.glsl +17 -0
- package/src/shaders/fill_outline_pattern.fragment.glsl +43 -0
- package/src/shaders/fill_outline_pattern.vertex.glsl +41 -0
- package/src/shaders/fill_pattern.fragment.glsl +36 -0
- package/src/shaders/fill_pattern.vertex.glsl +36 -0
- package/src/shaders/heatmap.fragment.glsl +21 -0
- package/src/shaders/heatmap.vertex.glsl +53 -0
- package/src/shaders/heatmap_texture.fragment.glsl +14 -0
- package/src/shaders/heatmap_texture.vertex.glsl +11 -0
- package/src/shaders/hillshade.fragment.glsl +52 -0
- package/src/shaders/hillshade.vertex.glsl +11 -0
- package/src/shaders/hillshade_prepare.fragment.glsl +72 -0
- package/src/shaders/hillshade_prepare.vertex.glsl +15 -0
- package/src/shaders/index.js +194 -0
- package/src/shaders/line.fragment.glsl +28 -0
- package/src/shaders/line.vertex.glsl +84 -0
- package/src/shaders/line_gradient.fragment.glsl +34 -0
- package/src/shaders/line_gradient.vertex.glsl +84 -0
- package/src/shaders/line_pattern.fragment.glsl +69 -0
- package/src/shaders/line_pattern.vertex.glsl +88 -0
- package/src/shaders/line_sdf.fragment.glsl +44 -0
- package/src/shaders/line_sdf.vertex.glsl +95 -0
- package/src/shaders/raster.fragment.glsl +52 -0
- package/src/shaders/raster.vertex.glsl +21 -0
- package/src/shaders/symbol_icon.fragment.glsl +17 -0
- package/src/shaders/symbol_icon.vertex.glsl +91 -0
- package/src/shaders/symbol_sdf.fragment.glsl +50 -0
- package/src/shaders/symbol_sdf.vertex.glsl +117 -0
- package/src/source/geojson_source.js +267 -0
- package/src/source/geojson_worker_source.js +210 -0
- package/src/source/geojson_wrapper.js +67 -0
- package/src/source/image_source.js +213 -0
- package/src/source/load_tilejson.js +40 -0
- package/src/source/pixels_to_tile_units.js +17 -0
- package/src/source/query_features.js +198 -0
- package/src/source/raster_dem_tile_source.js +140 -0
- package/src/source/raster_dem_tile_worker_source.js +26 -0
- package/src/source/raster_tile_source.js +126 -0
- package/src/source/rtl_text_plugin.js +63 -0
- package/src/source/source.js +75 -0
- package/src/source/source_cache.js +794 -0
- package/src/source/source_state.js +55 -0
- package/src/source/tile.js +332 -0
- package/src/source/tile_bounds.js +40 -0
- package/src/source/tile_cache.js +122 -0
- package/src/source/tile_id.js +150 -0
- package/src/source/vector_tile_source.js +144 -0
- package/src/source/vector_tile_worker_source.js +126 -0
- package/src/source/worker.js +175 -0
- package/src/source/worker_source.js +14 -0
- package/src/source/worker_tile.js +199 -0
- package/src/style/create_style_layer.js +25 -0
- package/src/style/evaluation_parameters.js +45 -0
- package/src/style/light.js +112 -0
- package/src/style/load_glyph_range.js +17 -0
- package/src/style/load_sprite.js +26 -0
- package/src/style/parse_glyph_pbf.js +45 -0
- package/src/style/pauseable_placement.js +88 -0
- package/src/style/properties.js +691 -0
- package/src/style/query_utils.js +39 -0
- package/src/style/style.js +955 -0
- package/src/style/style_layer/background_style_layer.js +11 -0
- package/src/style/style_layer/background_style_layer_properties.js +25 -0
- package/src/style/style_layer/circle_style_layer.js +93 -0
- package/src/style/style_layer/circle_style_layer_properties.js +76 -0
- package/src/style/style_layer/fill_extrusion_style_layer.js +194 -0
- package/src/style/style_layer/fill_extrusion_style_layer_properties.js +56 -0
- package/src/style/style_layer/fill_style_layer.js +46 -0
- package/src/style/style_layer/fill_style_layer_properties.js +45 -0
- package/src/style/style_layer/heatmap_style_layer.js +51 -0
- package/src/style/style_layer/heatmap_style_layer_properties.js +52 -0
- package/src/style/style_layer/hillshade_style_layer.js +15 -0
- package/src/style/style_layer/hillshade_style_layer_properties.js +43 -0
- package/src/style/style_layer/line_style_layer.js +129 -0
- package/src/style/style_layer/line_style_layer_properties.js +104 -0
- package/src/style/style_layer/raster_style_layer.js +11 -0
- package/src/style/style_layer/raster_style_layer_properties.js +55 -0
- package/src/style/style_layer/symbol_style_layer.js +66 -0
- package/src/style/style_layer/symbol_style_layer_properties.js +288 -0
- package/src/style/style_layer.js +183 -0
- package/src/style/style_layer_index.js +61 -0
- package/src/style/zoom_history.js +36 -0
- package/src/style-spec/deref.js +51 -0
- package/src/style-spec/error/parsing_error.js +8 -0
- package/src/style-spec/error/validation_error.js +10 -0
- package/src/style-spec/expression/compound_expression.js +118 -0
- package/src/style-spec/expression/definitions/array.js +82 -0
- package/src/style-spec/expression/definitions/assertion.js +69 -0
- package/src/style-spec/expression/definitions/at.js +57 -0
- package/src/style-spec/expression/definitions/case.js +73 -0
- package/src/style-spec/expression/definitions/coalesce.js +68 -0
- package/src/style-spec/expression/definitions/coercion.js +96 -0
- package/src/style-spec/expression/definitions/collator.js +102 -0
- package/src/style-spec/expression/definitions/equals.js +93 -0
- package/src/style-spec/expression/definitions/index.js +407 -0
- package/src/style-spec/expression/definitions/interpolate.js +235 -0
- package/src/style-spec/expression/definitions/length.js +54 -0
- package/src/style-spec/expression/definitions/let.js +60 -0
- package/src/style-spec/expression/definitions/literal.js +64 -0
- package/src/style-spec/expression/definitions/match.js +142 -0
- package/src/style-spec/expression/definitions/step.js +116 -0
- package/src/style-spec/expression/definitions/var.js +38 -0
- package/src/style-spec/expression/evaluation_context.js +35 -0
- package/src/style-spec/expression/index.js +329 -0
- package/src/style-spec/expression/is_constant.js +63 -0
- package/src/style-spec/expression/parsing_context.js +213 -0
- package/src/style-spec/expression/parsing_error.js +9 -0
- package/src/style-spec/expression/runtime_error.js +12 -0
- package/src/style-spec/expression/scope.js +34 -0
- package/src/style-spec/expression/stops.js +37 -0
- package/src/style-spec/expression/types.js +77 -0
- package/src/style-spec/expression/values.js +126 -0
- package/src/style-spec/feature_filter/README.md +55 -0
- package/src/style-spec/feature_filter/index.js +158 -0
- package/src/style-spec/function/convert.js +256 -0
- package/src/style-spec/function/index.js +299 -0
- package/src/style-spec/group_by_layout.js +68 -0
- package/src/style-spec/reference/v8.json +5356 -0
- package/src/style-spec/util/color.js +73 -0
- package/src/style-spec/util/color_spaces.js +128 -0
- package/src/style-spec/util/eval_support.js +8 -0
- package/src/style-spec/util/get_type.js +18 -0
- package/src/style-spec/util/interpolate.js +21 -0
- package/src/style-spec/util/properties.js +17 -0
- package/src/style-spec/util/ref_properties.js +1 -0
- package/src/style-spec/util/result.js +19 -0
- package/src/symbol/anchor.js +21 -0
- package/src/symbol/check_max_angle.js +75 -0
- package/src/symbol/clip_line.js +73 -0
- package/src/symbol/collision_feature.js +230 -0
- package/src/symbol/collision_index.js +379 -0
- package/src/symbol/cross_tile_symbol_index.js +270 -0
- package/src/symbol/get_anchors.js +177 -0
- package/src/symbol/grid_index.js +318 -0
- package/src/symbol/mergelines.js +75 -0
- package/src/symbol/opacity_state.js +21 -0
- package/src/symbol/placement.js +563 -0
- package/src/symbol/projection.js +601 -0
- package/src/symbol/quads.js +173 -0
- package/src/symbol/shaping.js +347 -0
- package/src/symbol/symbol_layout.js +519 -0
- package/src/symbol/symbol_size.js +110 -0
- package/src/symbol/transform_text.js +16 -0
- package/src/ui/anchor.js +24 -0
- package/src/ui/bind_handlers.js +199 -0
- package/src/ui/camera.js +954 -0
- package/src/ui/events.js +210 -0
- package/src/ui/handler/box_zoom.js +151 -0
- package/src/ui/handler/dblclick_zoom.js +91 -0
- package/src/ui/handler/drag_pan.js +285 -0
- package/src/ui/handler/drag_rotate.js +290 -0
- package/src/ui/handler/frame.js +28 -0
- package/src/ui/handler/inertia.js +45 -0
- package/src/ui/handler/keyboard.js +148 -0
- package/src/ui/handler/scroll_zoom.js +284 -0
- package/src/ui/handler/touch_zoom_rotate.js +263 -0
- package/src/ui/map.js +1645 -0
- package/src/util/actor.js +104 -0
- package/src/util/async.js +23 -0
- package/src/util/browser.js +61 -0
- package/src/util/callback.js +26 -0
- package/src/util/classify_rings.js +43 -0
- package/src/util/color_ramp.js +24 -0
- package/src/util/config.js +24 -0
- package/src/util/dictionary_coder.js +25 -0
- package/src/util/dispatcher.js +68 -0
- package/src/util/dom.js +102 -0
- package/src/util/evented.js +182 -0
- package/src/util/find_pole_of_inaccessibility.js +129 -0
- package/src/util/global_worker_pool.js +15 -0
- package/src/util/image.js +124 -0
- package/src/util/interpolate.js +5 -0
- package/src/util/intersection_tests.js +207 -0
- package/src/util/is_char_in_unicode_block.js +287 -0
- package/src/util/loader/image.js +32 -0
- package/src/util/object.js +178 -0
- package/src/util/script_detection.js +337 -0
- package/src/util/struct_array.js +197 -0
- package/src/util/task_queue.js +57 -0
- package/src/util/throttle.js +26 -0
- package/src/util/tile_cover.js +114 -0
- package/src/util/token.js +13 -0
- package/src/util/unique_id.js +12 -0
- package/src/util/util.js +192 -0
- package/src/util/vectortile_to_geojson.js +44 -0
- package/src/util/verticalize_punctuation.js +112 -0
- package/src/util/warn.js +21 -0
- package/src/util/web_worker.js +5 -0
- package/src/util/web_worker_transfer.js +228 -0
- package/src/util/worker_pool.js +41 -0
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
const {
|
|
2
|
+
NumberType,
|
|
3
|
+
StringType,
|
|
4
|
+
BooleanType,
|
|
5
|
+
ColorType,
|
|
6
|
+
ObjectType,
|
|
7
|
+
ValueType,
|
|
8
|
+
ErrorType,
|
|
9
|
+
CollatorType,
|
|
10
|
+
array,
|
|
11
|
+
toString
|
|
12
|
+
} = require('../types');
|
|
13
|
+
|
|
14
|
+
const { typeOf, Color, validateRGBA } = require('../values');
|
|
15
|
+
const CompoundExpression = require('../compound_expression');
|
|
16
|
+
const RuntimeError = require('../runtime_error');
|
|
17
|
+
const Let = require('./let');
|
|
18
|
+
const Var = require('./var');
|
|
19
|
+
const Literal = require('./literal');
|
|
20
|
+
const Assertion = require('./assertion');
|
|
21
|
+
const ArrayAssertion = require('./array');
|
|
22
|
+
const Coercion = require('./coercion');
|
|
23
|
+
const At = require('./at');
|
|
24
|
+
const Match = require('./match');
|
|
25
|
+
const Case = require('./case');
|
|
26
|
+
const Step = require('./step');
|
|
27
|
+
const Interpolate = require('./interpolate');
|
|
28
|
+
const Coalesce = require('./coalesce');
|
|
29
|
+
const { Equals, NotEquals } = require('./equals');
|
|
30
|
+
const { CollatorExpression } = require('./collator');
|
|
31
|
+
const Length = require('./length');
|
|
32
|
+
|
|
33
|
+
const expressions = {
|
|
34
|
+
// special forms
|
|
35
|
+
'==': Equals,
|
|
36
|
+
'!=': NotEquals,
|
|
37
|
+
array: ArrayAssertion,
|
|
38
|
+
at: At,
|
|
39
|
+
boolean: Assertion,
|
|
40
|
+
case: Case,
|
|
41
|
+
coalesce: Coalesce,
|
|
42
|
+
collator: CollatorExpression,
|
|
43
|
+
interpolate: Interpolate,
|
|
44
|
+
length: Length,
|
|
45
|
+
let: Let,
|
|
46
|
+
literal: Literal,
|
|
47
|
+
match: Match,
|
|
48
|
+
number: Assertion,
|
|
49
|
+
object: Assertion,
|
|
50
|
+
step: Step,
|
|
51
|
+
string: Assertion,
|
|
52
|
+
'to-color': Coercion,
|
|
53
|
+
'to-number': Coercion,
|
|
54
|
+
var: Var
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
function rgba(ctx, [r, g, b, a]) {
|
|
58
|
+
r = r.evaluate(ctx);
|
|
59
|
+
g = g.evaluate(ctx);
|
|
60
|
+
b = b.evaluate(ctx);
|
|
61
|
+
const alpha = a ? a.evaluate(ctx) : 1;
|
|
62
|
+
const error = validateRGBA(r, g, b, alpha);
|
|
63
|
+
if (error) throw new RuntimeError(error);
|
|
64
|
+
return new Color((r / 255) * alpha, (g / 255) * alpha, (b / 255) * alpha, alpha);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function has(key, obj) {
|
|
68
|
+
return key in obj;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function get(key, obj) {
|
|
72
|
+
const v = obj[key];
|
|
73
|
+
return typeof v === 'undefined' ? null : v;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function lt(ctx, [a, b]) {
|
|
77
|
+
return a.evaluate(ctx) < b.evaluate(ctx);
|
|
78
|
+
}
|
|
79
|
+
function gt(ctx, [a, b]) {
|
|
80
|
+
return a.evaluate(ctx) > b.evaluate(ctx);
|
|
81
|
+
}
|
|
82
|
+
function lteq(ctx, [a, b]) {
|
|
83
|
+
return a.evaluate(ctx) <= b.evaluate(ctx);
|
|
84
|
+
}
|
|
85
|
+
function gteq(ctx, [a, b]) {
|
|
86
|
+
return a.evaluate(ctx) >= b.evaluate(ctx);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function ltCollate(ctx, [a, b, c]) {
|
|
90
|
+
return c.evaluate(ctx).compare(a.evaluate(ctx), b.evaluate(ctx)) < 0;
|
|
91
|
+
}
|
|
92
|
+
function gtCollate(ctx, [a, b, c]) {
|
|
93
|
+
return c.evaluate(ctx).compare(a.evaluate(ctx), b.evaluate(ctx)) > 0;
|
|
94
|
+
}
|
|
95
|
+
function lteqCollate(ctx, [a, b, c]) {
|
|
96
|
+
return c.evaluate(ctx).compare(a.evaluate(ctx), b.evaluate(ctx)) <= 0;
|
|
97
|
+
}
|
|
98
|
+
function gteqCollate(ctx, [a, b, c]) {
|
|
99
|
+
return c.evaluate(ctx).compare(a.evaluate(ctx), b.evaluate(ctx)) >= 0;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function binarySearch(v, a, i, j) {
|
|
103
|
+
while (i <= j) {
|
|
104
|
+
const m = (i + j) >> 1;
|
|
105
|
+
if (a[m] === v) return true;
|
|
106
|
+
if (a[m] > v) j = m - 1;
|
|
107
|
+
else i = m + 1;
|
|
108
|
+
}
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function varargs(type) {
|
|
113
|
+
return { type };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
CompoundExpression.register(expressions, {
|
|
117
|
+
error: [
|
|
118
|
+
ErrorType,
|
|
119
|
+
[StringType],
|
|
120
|
+
(ctx, [v]) => {
|
|
121
|
+
throw new RuntimeError(v.evaluate(ctx));
|
|
122
|
+
}
|
|
123
|
+
],
|
|
124
|
+
typeof: [StringType, [ValueType], (ctx, [v]) => toString(typeOf(v.evaluate(ctx)))],
|
|
125
|
+
'to-string': [
|
|
126
|
+
StringType,
|
|
127
|
+
[ValueType],
|
|
128
|
+
(ctx, [v]) => {
|
|
129
|
+
v = v.evaluate(ctx);
|
|
130
|
+
const type = typeof v;
|
|
131
|
+
if (v === null) {
|
|
132
|
+
return '';
|
|
133
|
+
}
|
|
134
|
+
if (type === 'string' || type === 'number' || type === 'boolean') {
|
|
135
|
+
return String(v);
|
|
136
|
+
}
|
|
137
|
+
if (v instanceof Color) {
|
|
138
|
+
return v.toString();
|
|
139
|
+
}
|
|
140
|
+
return JSON.stringify(v);
|
|
141
|
+
}
|
|
142
|
+
],
|
|
143
|
+
'to-boolean': [BooleanType, [ValueType], (ctx, [v]) => Boolean(v.evaluate(ctx))],
|
|
144
|
+
'to-rgba': [
|
|
145
|
+
array(NumberType, 4),
|
|
146
|
+
[ColorType],
|
|
147
|
+
(ctx, [v]) => {
|
|
148
|
+
return v.evaluate(ctx).toArray();
|
|
149
|
+
}
|
|
150
|
+
],
|
|
151
|
+
rgb: [ColorType, [NumberType, NumberType, NumberType], rgba],
|
|
152
|
+
rgba: [ColorType, [NumberType, NumberType, NumberType, NumberType], rgba],
|
|
153
|
+
has: {
|
|
154
|
+
type: BooleanType,
|
|
155
|
+
overloads: [
|
|
156
|
+
[[StringType], (ctx, [key]) => has(key.evaluate(ctx), ctx.properties())],
|
|
157
|
+
[[StringType, ObjectType], (ctx, [key, obj]) => has(key.evaluate(ctx), obj.evaluate(ctx))]
|
|
158
|
+
]
|
|
159
|
+
},
|
|
160
|
+
get: {
|
|
161
|
+
type: ValueType,
|
|
162
|
+
overloads: [
|
|
163
|
+
[[StringType], (ctx, [key]) => get(key.evaluate(ctx), ctx.properties())],
|
|
164
|
+
[[StringType, ObjectType], (ctx, [key, obj]) => get(key.evaluate(ctx), obj.evaluate(ctx))]
|
|
165
|
+
]
|
|
166
|
+
},
|
|
167
|
+
'feature-state': [ValueType, [StringType], (ctx, [key]) => get(key.evaluate(ctx), ctx.featureState || {})],
|
|
168
|
+
properties: [ObjectType, [], ctx => ctx.properties()],
|
|
169
|
+
'geometry-type': [StringType, [], ctx => ctx.geometryType()],
|
|
170
|
+
id: [ValueType, [], ctx => ctx.id()],
|
|
171
|
+
zoom: [NumberType, [], ctx => ctx.globals.zoom],
|
|
172
|
+
'heatmap-density': [NumberType, [], ctx => ctx.globals.heatmapDensity || 0],
|
|
173
|
+
'line-progress': [NumberType, [], ctx => ctx.globals.lineProgress || 0],
|
|
174
|
+
'+': [
|
|
175
|
+
NumberType,
|
|
176
|
+
varargs(NumberType),
|
|
177
|
+
(ctx, args) => {
|
|
178
|
+
let result = 0;
|
|
179
|
+
for (const arg of args) {
|
|
180
|
+
result += arg.evaluate(ctx);
|
|
181
|
+
}
|
|
182
|
+
return result;
|
|
183
|
+
}
|
|
184
|
+
],
|
|
185
|
+
'*': [
|
|
186
|
+
NumberType,
|
|
187
|
+
varargs(NumberType),
|
|
188
|
+
(ctx, args) => {
|
|
189
|
+
let result = 1;
|
|
190
|
+
for (const arg of args) {
|
|
191
|
+
result *= arg.evaluate(ctx);
|
|
192
|
+
}
|
|
193
|
+
return result;
|
|
194
|
+
}
|
|
195
|
+
],
|
|
196
|
+
'-': {
|
|
197
|
+
type: NumberType,
|
|
198
|
+
overloads: [
|
|
199
|
+
[[NumberType, NumberType], (ctx, [a, b]) => a.evaluate(ctx) - b.evaluate(ctx)],
|
|
200
|
+
[[NumberType], (ctx, [a]) => -a.evaluate(ctx)]
|
|
201
|
+
]
|
|
202
|
+
},
|
|
203
|
+
'/': [NumberType, [NumberType, NumberType], (ctx, [a, b]) => a.evaluate(ctx) / b.evaluate(ctx)],
|
|
204
|
+
'%': [NumberType, [NumberType, NumberType], (ctx, [a, b]) => a.evaluate(ctx) % b.evaluate(ctx)],
|
|
205
|
+
ln2: [NumberType, [], () => Math.LN2],
|
|
206
|
+
pi: [NumberType, [], () => Math.PI],
|
|
207
|
+
e: [NumberType, [], () => Math.E],
|
|
208
|
+
'^': [NumberType, [NumberType, NumberType], (ctx, [b, e]) => b.evaluate(ctx) ** e.evaluate(ctx)],
|
|
209
|
+
sqrt: [NumberType, [NumberType], (ctx, [x]) => Math.sqrt(x.evaluate(ctx))],
|
|
210
|
+
log10: [NumberType, [NumberType], (ctx, [n]) => Math.log10(n.evaluate(ctx))],
|
|
211
|
+
ln: [NumberType, [NumberType], (ctx, [n]) => Math.log(n.evaluate(ctx))],
|
|
212
|
+
log2: [NumberType, [NumberType], (ctx, [n]) => Math.log2(n.evaluate(ctx))],
|
|
213
|
+
sin: [NumberType, [NumberType], (ctx, [n]) => Math.sin(n.evaluate(ctx))],
|
|
214
|
+
cos: [NumberType, [NumberType], (ctx, [n]) => Math.cos(n.evaluate(ctx))],
|
|
215
|
+
tan: [NumberType, [NumberType], (ctx, [n]) => Math.tan(n.evaluate(ctx))],
|
|
216
|
+
asin: [NumberType, [NumberType], (ctx, [n]) => Math.asin(n.evaluate(ctx))],
|
|
217
|
+
acos: [NumberType, [NumberType], (ctx, [n]) => Math.acos(n.evaluate(ctx))],
|
|
218
|
+
atan: [NumberType, [NumberType], (ctx, [n]) => Math.atan(n.evaluate(ctx))],
|
|
219
|
+
min: [NumberType, varargs(NumberType), (ctx, args) => Math.min(...args.map(arg => arg.evaluate(ctx)))],
|
|
220
|
+
max: [NumberType, varargs(NumberType), (ctx, args) => Math.max(...args.map(arg => arg.evaluate(ctx)))],
|
|
221
|
+
abs: [NumberType, [NumberType], (ctx, [n]) => Math.abs(n.evaluate(ctx))],
|
|
222
|
+
round: [
|
|
223
|
+
NumberType,
|
|
224
|
+
[NumberType],
|
|
225
|
+
(ctx, [n]) => {
|
|
226
|
+
const v = n.evaluate(ctx);
|
|
227
|
+
// Javascript's Math.round() rounds towards +Infinity for halfway
|
|
228
|
+
// values, even when they're negative. It's more common to round
|
|
229
|
+
// away from 0 (e.g., this is what python and C++ do)
|
|
230
|
+
return v < 0 ? -Math.round(-v) : Math.round(v);
|
|
231
|
+
}
|
|
232
|
+
],
|
|
233
|
+
floor: [NumberType, [NumberType], (ctx, [n]) => Math.floor(n.evaluate(ctx))],
|
|
234
|
+
ceil: [NumberType, [NumberType], (ctx, [n]) => Math.ceil(n.evaluate(ctx))],
|
|
235
|
+
'filter-==': [BooleanType, [StringType, ValueType], (ctx, [k, v]) => ctx.properties()[k.value] === v.value],
|
|
236
|
+
'filter-id-==': [BooleanType, [ValueType], (ctx, [v]) => ctx.id() === v.value],
|
|
237
|
+
'filter-type-==': [BooleanType, [StringType], (ctx, [v]) => ctx.geometryType() === v.value],
|
|
238
|
+
'filter-<': [
|
|
239
|
+
BooleanType,
|
|
240
|
+
[StringType, ValueType],
|
|
241
|
+
(ctx, [k, v]) => {
|
|
242
|
+
const a = ctx.properties()[k.value];
|
|
243
|
+
const b = v.value;
|
|
244
|
+
return typeof a === typeof b && a < b;
|
|
245
|
+
}
|
|
246
|
+
],
|
|
247
|
+
'filter-id-<': [
|
|
248
|
+
BooleanType,
|
|
249
|
+
[ValueType],
|
|
250
|
+
(ctx, [v]) => {
|
|
251
|
+
const a = ctx.id();
|
|
252
|
+
const b = v.value;
|
|
253
|
+
return typeof a === typeof b && a < b;
|
|
254
|
+
}
|
|
255
|
+
],
|
|
256
|
+
'filter->': [
|
|
257
|
+
BooleanType,
|
|
258
|
+
[StringType, ValueType],
|
|
259
|
+
(ctx, [k, v]) => {
|
|
260
|
+
const a = ctx.properties()[k.value];
|
|
261
|
+
const b = v.value;
|
|
262
|
+
return typeof a === typeof b && a > b;
|
|
263
|
+
}
|
|
264
|
+
],
|
|
265
|
+
'filter-id->': [
|
|
266
|
+
BooleanType,
|
|
267
|
+
[ValueType],
|
|
268
|
+
(ctx, [v]) => {
|
|
269
|
+
const a = ctx.id();
|
|
270
|
+
const b = v.value;
|
|
271
|
+
return typeof a === typeof b && a > b;
|
|
272
|
+
}
|
|
273
|
+
],
|
|
274
|
+
'filter-<=': [
|
|
275
|
+
BooleanType,
|
|
276
|
+
[StringType, ValueType],
|
|
277
|
+
(ctx, [k, v]) => {
|
|
278
|
+
const a = ctx.properties()[k.value];
|
|
279
|
+
const b = v.value;
|
|
280
|
+
return typeof a === typeof b && a <= b;
|
|
281
|
+
}
|
|
282
|
+
],
|
|
283
|
+
'filter-id-<=': [
|
|
284
|
+
BooleanType,
|
|
285
|
+
[ValueType],
|
|
286
|
+
(ctx, [v]) => {
|
|
287
|
+
const a = ctx.id();
|
|
288
|
+
const b = v.value;
|
|
289
|
+
return typeof a === typeof b && a <= b;
|
|
290
|
+
}
|
|
291
|
+
],
|
|
292
|
+
'filter->=': [
|
|
293
|
+
BooleanType,
|
|
294
|
+
[StringType, ValueType],
|
|
295
|
+
(ctx, [k, v]) => {
|
|
296
|
+
const a = ctx.properties()[k.value];
|
|
297
|
+
const b = v.value;
|
|
298
|
+
return typeof a === typeof b && a >= b;
|
|
299
|
+
}
|
|
300
|
+
],
|
|
301
|
+
'filter-id->=': [
|
|
302
|
+
BooleanType,
|
|
303
|
+
[ValueType],
|
|
304
|
+
(ctx, [v]) => {
|
|
305
|
+
const a = ctx.id();
|
|
306
|
+
const b = v.value;
|
|
307
|
+
return typeof a === typeof b && a >= b;
|
|
308
|
+
}
|
|
309
|
+
],
|
|
310
|
+
'filter-has': [BooleanType, [ValueType], (ctx, [k]) => k.value in ctx.properties()],
|
|
311
|
+
'filter-has-id': [BooleanType, [], ctx => ctx.id() !== null],
|
|
312
|
+
'filter-type-in': [BooleanType, [array(StringType)], (ctx, [v]) => v.value.indexOf(ctx.geometryType()) >= 0],
|
|
313
|
+
'filter-id-in': [BooleanType, [array(ValueType)], (ctx, [v]) => v.value.indexOf(ctx.id()) >= 0],
|
|
314
|
+
'filter-in-small': [
|
|
315
|
+
BooleanType,
|
|
316
|
+
[StringType, array(ValueType)],
|
|
317
|
+
// assumes v is an array literal
|
|
318
|
+
(ctx, [k, v]) => v.value.indexOf(ctx.properties()[k.value]) >= 0
|
|
319
|
+
],
|
|
320
|
+
'filter-in-large': [
|
|
321
|
+
BooleanType,
|
|
322
|
+
[StringType, array(ValueType)],
|
|
323
|
+
// assumes v is a array literal with values sorted in ascending order and of a single type
|
|
324
|
+
(ctx, [k, v]) => binarySearch(ctx.properties()[k.value], v.value, 0, v.value.length - 1)
|
|
325
|
+
],
|
|
326
|
+
'>': {
|
|
327
|
+
type: BooleanType,
|
|
328
|
+
overloads: [
|
|
329
|
+
[[NumberType, NumberType], gt],
|
|
330
|
+
[[StringType, StringType], gt],
|
|
331
|
+
[[StringType, StringType, CollatorType], gtCollate]
|
|
332
|
+
]
|
|
333
|
+
},
|
|
334
|
+
'<': {
|
|
335
|
+
type: BooleanType,
|
|
336
|
+
overloads: [
|
|
337
|
+
[[NumberType, NumberType], lt],
|
|
338
|
+
[[StringType, StringType], lt],
|
|
339
|
+
[[StringType, StringType, CollatorType], ltCollate]
|
|
340
|
+
]
|
|
341
|
+
},
|
|
342
|
+
'>=': {
|
|
343
|
+
type: BooleanType,
|
|
344
|
+
overloads: [
|
|
345
|
+
[[NumberType, NumberType], gteq],
|
|
346
|
+
[[StringType, StringType], gteq],
|
|
347
|
+
[[StringType, StringType, CollatorType], gteqCollate]
|
|
348
|
+
]
|
|
349
|
+
},
|
|
350
|
+
'<=': {
|
|
351
|
+
type: BooleanType,
|
|
352
|
+
overloads: [
|
|
353
|
+
[[NumberType, NumberType], lteq],
|
|
354
|
+
[[StringType, StringType], lteq],
|
|
355
|
+
[[StringType, StringType, CollatorType], lteqCollate]
|
|
356
|
+
]
|
|
357
|
+
},
|
|
358
|
+
all: {
|
|
359
|
+
type: BooleanType,
|
|
360
|
+
overloads: [
|
|
361
|
+
[[BooleanType, BooleanType], (ctx, [a, b]) => a.evaluate(ctx) && b.evaluate(ctx)],
|
|
362
|
+
[
|
|
363
|
+
varargs(BooleanType),
|
|
364
|
+
(ctx, args) => {
|
|
365
|
+
for (const arg of args) {
|
|
366
|
+
if (!arg.evaluate(ctx)) return false;
|
|
367
|
+
}
|
|
368
|
+
return true;
|
|
369
|
+
}
|
|
370
|
+
]
|
|
371
|
+
]
|
|
372
|
+
},
|
|
373
|
+
any: {
|
|
374
|
+
type: BooleanType,
|
|
375
|
+
overloads: [
|
|
376
|
+
[[BooleanType, BooleanType], (ctx, [a, b]) => a.evaluate(ctx) || b.evaluate(ctx)],
|
|
377
|
+
[
|
|
378
|
+
varargs(BooleanType),
|
|
379
|
+
(ctx, args) => {
|
|
380
|
+
for (const arg of args) {
|
|
381
|
+
if (arg.evaluate(ctx)) return true;
|
|
382
|
+
}
|
|
383
|
+
return false;
|
|
384
|
+
}
|
|
385
|
+
]
|
|
386
|
+
]
|
|
387
|
+
},
|
|
388
|
+
'!': [BooleanType, [BooleanType], (ctx, [b]) => !b.evaluate(ctx)],
|
|
389
|
+
'is-supported-script': [
|
|
390
|
+
BooleanType,
|
|
391
|
+
[StringType],
|
|
392
|
+
// At parse time this will always return true, so we need to exclude this expression with isGlobalPropertyConstant
|
|
393
|
+
(ctx, [s]) => {
|
|
394
|
+
const isSupportedScript = ctx.globals?.isSupportedScript;
|
|
395
|
+
if (isSupportedScript) {
|
|
396
|
+
return isSupportedScript(s.evaluate(ctx));
|
|
397
|
+
}
|
|
398
|
+
return true;
|
|
399
|
+
}
|
|
400
|
+
],
|
|
401
|
+
upcase: [StringType, [StringType], (ctx, [s]) => s.evaluate(ctx).toUpperCase()],
|
|
402
|
+
downcase: [StringType, [StringType], (ctx, [s]) => s.evaluate(ctx).toLowerCase()],
|
|
403
|
+
concat: [StringType, varargs(StringType), (ctx, args) => args.map(arg => arg.evaluate(ctx)).join('')],
|
|
404
|
+
'resolved-locale': [StringType, [CollatorType], (ctx, [collator]) => collator.evaluate(ctx).resolvedLocale()]
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
module.exports = expressions;
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
const UnitBezier = require('@mapbox/unitbezier');
|
|
2
|
+
|
|
3
|
+
const interpolate = require('../../util/interpolate');
|
|
4
|
+
const { toString, NumberType } = require('../types');
|
|
5
|
+
const { findStopLessThanOrEqualTo } = require('../stops');
|
|
6
|
+
|
|
7
|
+
class Interpolate {
|
|
8
|
+
constructor(type, interpolation, input, stops) {
|
|
9
|
+
this.type = type;
|
|
10
|
+
this.interpolation = interpolation;
|
|
11
|
+
this.input = input;
|
|
12
|
+
|
|
13
|
+
this.labels = [];
|
|
14
|
+
this.outputs = [];
|
|
15
|
+
for (const [label, expression] of stops) {
|
|
16
|
+
this.labels.push(label);
|
|
17
|
+
this.outputs.push(expression);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
static interpolationFactor(interpolation, input, lower, upper) {
|
|
22
|
+
let t = 0;
|
|
23
|
+
if (interpolation.name === 'exponential') {
|
|
24
|
+
t = exponentialInterpolation(input, interpolation.base, lower, upper);
|
|
25
|
+
} else if (interpolation.name === 'linear') {
|
|
26
|
+
t = exponentialInterpolation(input, 1, lower, upper);
|
|
27
|
+
} else if (interpolation.name === 'cubic-bezier') {
|
|
28
|
+
const c = interpolation.controlPoints;
|
|
29
|
+
const ub = new UnitBezier(c[0], c[1], c[2], c[3]);
|
|
30
|
+
t = ub.solve(exponentialInterpolation(input, 1, lower, upper));
|
|
31
|
+
}
|
|
32
|
+
return t;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
static parse(args, context) {
|
|
36
|
+
let [, interpolation, input, ...rest] = args;
|
|
37
|
+
|
|
38
|
+
if (!Array.isArray(interpolation) || interpolation.length === 0) {
|
|
39
|
+
return context.error('Expected an interpolation type expression.', 1);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (interpolation[0] === 'linear') {
|
|
43
|
+
interpolation = { name: 'linear' };
|
|
44
|
+
} else if (interpolation[0] === 'exponential') {
|
|
45
|
+
const base = interpolation[1];
|
|
46
|
+
if (typeof base !== 'number') return context.error('Exponential interpolation requires a numeric base.', 1, 1);
|
|
47
|
+
interpolation = {
|
|
48
|
+
name: 'exponential',
|
|
49
|
+
base
|
|
50
|
+
};
|
|
51
|
+
} else if (interpolation[0] === 'cubic-bezier') {
|
|
52
|
+
const controlPoints = interpolation.slice(1);
|
|
53
|
+
if (controlPoints.length !== 4 || controlPoints.some(t => typeof t !== 'number' || t < 0 || t > 1)) {
|
|
54
|
+
return context.error(
|
|
55
|
+
'Cubic bezier interpolation requires four numeric arguments with values between 0 and 1.',
|
|
56
|
+
1
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
interpolation = {
|
|
61
|
+
name: 'cubic-bezier',
|
|
62
|
+
controlPoints: controlPoints
|
|
63
|
+
};
|
|
64
|
+
} else {
|
|
65
|
+
return context.error(`Unknown interpolation type ${String(interpolation[0])}`, 1, 0);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (args.length - 1 < 4) {
|
|
69
|
+
return context.error(`Expected at least 4 arguments, but found only ${args.length - 1}.`);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if ((args.length - 1) % 2 !== 0) {
|
|
73
|
+
return context.error('Expected an even number of arguments.');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
input = context.parse(input, 2, NumberType);
|
|
77
|
+
if (!input) return null;
|
|
78
|
+
|
|
79
|
+
const stops = [];
|
|
80
|
+
|
|
81
|
+
let outputType = null;
|
|
82
|
+
if (context.expectedType && context.expectedType.kind !== 'value') {
|
|
83
|
+
outputType = context.expectedType;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
for (let i = 0; i < rest.length; i += 2) {
|
|
87
|
+
const label = rest[i];
|
|
88
|
+
const value = rest[i + 1];
|
|
89
|
+
|
|
90
|
+
const labelKey = i + 3;
|
|
91
|
+
const valueKey = i + 4;
|
|
92
|
+
|
|
93
|
+
if (typeof label !== 'number') {
|
|
94
|
+
return context.error(
|
|
95
|
+
'Input/output pairs for "interpolate" expressions must be defined using literal numeric values (not computed expressions) for the input values.',
|
|
96
|
+
labelKey
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (stops.length && stops[stops.length - 1][0] >= label) {
|
|
101
|
+
return context.error(
|
|
102
|
+
'Input/output pairs for "interpolate" expressions must be arranged with input values in strictly ascending order.',
|
|
103
|
+
labelKey
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const parsed = context.parse(value, valueKey, outputType);
|
|
108
|
+
if (!parsed) return null;
|
|
109
|
+
outputType = outputType || parsed.type;
|
|
110
|
+
stops.push([label, parsed]);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (
|
|
114
|
+
outputType.kind !== 'number' &&
|
|
115
|
+
outputType.kind !== 'color' &&
|
|
116
|
+
!(outputType.kind === 'array' && outputType.itemType.kind === 'number' && typeof outputType.N === 'number')
|
|
117
|
+
) {
|
|
118
|
+
return context.error(`Type ${toString(outputType)} is not interpolatable.`);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return new Interpolate(outputType, interpolation, input, stops);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
evaluate(ctx) {
|
|
125
|
+
const labels = this.labels;
|
|
126
|
+
const outputs = this.outputs;
|
|
127
|
+
|
|
128
|
+
if (labels.length === 1) {
|
|
129
|
+
return outputs[0].evaluate(ctx);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const value = this.input.evaluate(ctx);
|
|
133
|
+
if (value <= labels[0]) {
|
|
134
|
+
return outputs[0].evaluate(ctx);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const stopCount = labels.length;
|
|
138
|
+
if (value >= labels[stopCount - 1]) {
|
|
139
|
+
return outputs[stopCount - 1].evaluate(ctx);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const index = findStopLessThanOrEqualTo(labels, value);
|
|
143
|
+
const lower = labels[index];
|
|
144
|
+
const upper = labels[index + 1];
|
|
145
|
+
const t = Interpolate.interpolationFactor(this.interpolation, value, lower, upper);
|
|
146
|
+
|
|
147
|
+
const outputLower = outputs[index].evaluate(ctx);
|
|
148
|
+
const outputUpper = outputs[index + 1].evaluate(ctx);
|
|
149
|
+
|
|
150
|
+
return interpolate[this.type.kind.toLowerCase()](outputLower, outputUpper, t);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
eachChild(fn) {
|
|
154
|
+
fn(this.input);
|
|
155
|
+
for (const expression of this.outputs) {
|
|
156
|
+
fn(expression);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
possibleOutputs() {
|
|
161
|
+
return [].concat(...this.outputs.map(output => output.possibleOutputs()));
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
serialize() {
|
|
165
|
+
let interpolation;
|
|
166
|
+
if (this.interpolation.name === 'linear') {
|
|
167
|
+
interpolation = ['linear'];
|
|
168
|
+
} else if (this.interpolation.name === 'exponential') {
|
|
169
|
+
if (this.interpolation.base === 1) {
|
|
170
|
+
interpolation = ['linear'];
|
|
171
|
+
} else {
|
|
172
|
+
interpolation = ['exponential', this.interpolation.base];
|
|
173
|
+
}
|
|
174
|
+
} else {
|
|
175
|
+
interpolation = ['cubic-bezier'].concat(this.interpolation.controlPoints);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const serialized = ['interpolate', interpolation, this.input.serialize()];
|
|
179
|
+
|
|
180
|
+
for (let i = 0; i < this.labels.length; i++) {
|
|
181
|
+
serialized.push(this.labels[i], this.outputs[i].serialize());
|
|
182
|
+
}
|
|
183
|
+
return serialized;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Returns a ratio that can be used to interpolate between exponential function
|
|
189
|
+
* stops.
|
|
190
|
+
* How it works: Two consecutive stop values define a (scaled and shifted) exponential function `f(x) = a * base^x + b`, where `base` is the user-specified base,
|
|
191
|
+
* and `a` and `b` are constants affording sufficient degrees of freedom to fit
|
|
192
|
+
* the function to the given stops.
|
|
193
|
+
*
|
|
194
|
+
* Here's a bit of algebra that lets us compute `f(x)` directly from the stop
|
|
195
|
+
* values without explicitly solving for `a` and `b`:
|
|
196
|
+
*
|
|
197
|
+
* First stop value: `f(x0) = y0 = a * base^x0 + b`
|
|
198
|
+
* Second stop value: `f(x1) = y1 = a * base^x1 + b`
|
|
199
|
+
* => `y1 - y0 = a(base^x1 - base^x0)`
|
|
200
|
+
* => `a = (y1 - y0)/(base^x1 - base^x0)`
|
|
201
|
+
*
|
|
202
|
+
* Desired value: `f(x) = y = a * base^x + b`
|
|
203
|
+
* => `f(x) = y0 + a * (base^x - base^x0)`
|
|
204
|
+
*
|
|
205
|
+
* From the above, we can replace the `a` in `a * (base^x - base^x0)` and do a
|
|
206
|
+
* little algebra:
|
|
207
|
+
* ```
|
|
208
|
+
* a * (base^x - base^x0) = (y1 - y0)/(base^x1 - base^x0) * (base^x - base^x0)
|
|
209
|
+
* = (y1 - y0) * (base^x - base^x0) / (base^x1 - base^x0)
|
|
210
|
+
* ```
|
|
211
|
+
*
|
|
212
|
+
* If we let `(base^x - base^x0) / (base^x1 base^x0)`, then we have
|
|
213
|
+
* `f(x) = y0 + (y1 - y0) * ratio`. In other words, `ratio` may be treated as
|
|
214
|
+
* an interpolation factor between the two stops' output values.
|
|
215
|
+
*
|
|
216
|
+
* (Note: a slightly different form for `ratio`,
|
|
217
|
+
* `(base^(x-x0) - 1) / (base^(x1-x0) - 1) `, is equivalent, but requires fewer
|
|
218
|
+
* expensive `Math.pow()` operations.)
|
|
219
|
+
*
|
|
220
|
+
* @private
|
|
221
|
+
*/
|
|
222
|
+
function exponentialInterpolation(input, base, lowerValue, upperValue) {
|
|
223
|
+
const difference = upperValue - lowerValue;
|
|
224
|
+
const progress = input - lowerValue;
|
|
225
|
+
|
|
226
|
+
if (difference === 0) {
|
|
227
|
+
return 0;
|
|
228
|
+
}
|
|
229
|
+
if (base === 1) {
|
|
230
|
+
return progress / difference;
|
|
231
|
+
}
|
|
232
|
+
return (base ** progress - 1) / (base ** difference - 1);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
module.exports = Interpolate;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
const { NumberType, toString } = require('../types');
|
|
2
|
+
|
|
3
|
+
const { typeOf } = require('../values');
|
|
4
|
+
const RuntimeError = require('../runtime_error');
|
|
5
|
+
|
|
6
|
+
class Length {
|
|
7
|
+
constructor(input) {
|
|
8
|
+
this.type = NumberType;
|
|
9
|
+
this.input = input;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
static parse(args, context) {
|
|
13
|
+
if (args.length !== 2) return context.error(`Expected 1 argument, but found ${args.length - 1} instead.`);
|
|
14
|
+
|
|
15
|
+
const input = context.parse(args[1], 1);
|
|
16
|
+
if (!input) return null;
|
|
17
|
+
|
|
18
|
+
if (input.type.kind !== 'array' && input.type.kind !== 'string' && input.type.kind !== 'value')
|
|
19
|
+
return context.error(`Expected argument of type string or array, but found ${toString(input.type)} instead.`);
|
|
20
|
+
|
|
21
|
+
return new Length(input);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
evaluate(ctx) {
|
|
25
|
+
const input = this.input.evaluate(ctx);
|
|
26
|
+
if (typeof input === 'string') {
|
|
27
|
+
return input.length;
|
|
28
|
+
}
|
|
29
|
+
if (Array.isArray(input)) {
|
|
30
|
+
return input.length;
|
|
31
|
+
}
|
|
32
|
+
throw new RuntimeError(
|
|
33
|
+
`Expected value to be of type string or array, but found ${toString(typeOf(input))} instead.`
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
eachChild(fn) {
|
|
38
|
+
fn(this.input);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
possibleOutputs() {
|
|
42
|
+
return [undefined];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
serialize() {
|
|
46
|
+
const serialized = ['length'];
|
|
47
|
+
this.eachChild(child => {
|
|
48
|
+
serialized.push(child.serialize());
|
|
49
|
+
});
|
|
50
|
+
return serialized;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
module.exports = Length;
|