From 66d5d04c61fc17e5a652f7cf2f4758f3e5cac149 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Wed, 24 Jul 2024 17:13:28 -0400 Subject: [PATCH] Squashed Rust impl --- BUILD.bazel | 1 - CMakeLists.txt | 23 +- LICENSES.core.md | 29 -- docker/Dockerfile | 4 + docker/startup.sh | 15 + platform/android/LICENSE.md | 29 -- platform/android/android.cmake | 1 - platform/ios/LICENSE.md | 30 +- platform/linux/README.md | 3 + platform/qt/qt.cmake | 2 - rustutils/.gitignore | 1 + rustutils/Cargo.lock | 162 ++++++++++ rustutils/Cargo.toml | 19 ++ rustutils/src/lib.rs | 36 +++ src/mbgl/util/color.cpp | 13 +- vendor/BUILD.bazel | 14 - vendor/csscolorparser.cmake | 37 --- vendor/csscolorparser/LICENSE | 20 -- vendor/csscolorparser/csscolorparser.cpp | 288 ------------------ .../csscolorparser/csscolorparser.hpp | 56 ---- 20 files changed, 265 insertions(+), 518 deletions(-) create mode 100644 rustutils/.gitignore create mode 100644 rustutils/Cargo.lock create mode 100644 rustutils/Cargo.toml create mode 100644 rustutils/src/lib.rs delete mode 100644 vendor/csscolorparser.cmake delete mode 100644 vendor/csscolorparser/LICENSE delete mode 100644 vendor/csscolorparser/csscolorparser.cpp delete mode 100644 vendor/csscolorparser/csscolorparser/csscolorparser.hpp diff --git a/BUILD.bazel b/BUILD.bazel index ec5888a557b..0692289a0c2 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -124,7 +124,6 @@ cc_library( ":mbgl-core-generated-private-artifacts", ":mbgl-core-generated-public-artifacts", "//vendor:boost", - "//vendor:csscolorparser", "//vendor:earcut.hpp", "//vendor:eternal", "//vendor:mapbox-base", diff --git a/CMakeLists.txt b/CMakeLists.txt index 7aea21d4598..b287da082d9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1333,7 +1333,6 @@ target_include_directories( ) include(${PROJECT_SOURCE_DIR}/vendor/boost.cmake) -include(${PROJECT_SOURCE_DIR}/vendor/csscolorparser.cmake) include(${PROJECT_SOURCE_DIR}/vendor/earcut.hpp.cmake) include(${PROJECT_SOURCE_DIR}/vendor/eternal.cmake) include(${PROJECT_SOURCE_DIR}/vendor/mapbox-base.cmake) @@ -1355,6 +1354,19 @@ set_target_properties( INTERFACE_MAPBOX_LICENSE ${PROJECT_SOURCE_DIR}/vendor/unordered_dense/LICENSE ) +include(FetchContent) +FetchContent_Declare( + Corrosion + GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git + GIT_TAG v0.5 +) +FetchContent_MakeAvailable(Corrosion) + +corrosion_import_crate(MANIFEST_PATH rustutils/Cargo.toml) + +# rustutils_headers is a set of .h headers generated from rustutils crate +corrosion_add_cxxbridge(rustutils_headers CRATE rustutils FILES lib.rs) + target_link_libraries( mbgl-core PRIVATE @@ -1365,7 +1377,6 @@ target_link_libraries( Mapbox::Base::cheap-ruler-cpp mbgl-compiler-options mbgl-vendor-boost - mbgl-vendor-csscolorparser mbgl-vendor-earcut.hpp mbgl-vendor-eternal mbgl-vendor-parsedate @@ -1374,6 +1385,7 @@ target_link_libraries( mbgl-vendor-unique_resource mbgl-vendor-vector-tile mbgl-vendor-wagyu + rustutils_headers PUBLIC Mapbox::Base Mapbox::Base::Extras::expected-lite @@ -1383,11 +1395,13 @@ target_link_libraries( Mapbox::Base::variant TracyClient unordered_dense + rustutils ) export(TARGETS + rustutils_headers mbgl-core - + rustutils mapbox-base mapbox-base-cheap-ruler-cpp mapbox-base-extras-expected-lite @@ -1403,7 +1417,6 @@ export(TARGETS mapbox-base-variant mbgl-compiler-options mbgl-vendor-boost - mbgl-vendor-csscolorparser mbgl-vendor-earcut.hpp mbgl-vendor-eternal mbgl-vendor-parsedate @@ -1437,7 +1450,7 @@ if(MLN_WITH_CORE_ONLY) return() endif() -include(${PROJECT_SOURCE_DIR}/scripts/license.cmake) +#include(${PROJECT_SOURCE_DIR}/scripts/license.cmake) if(MLN_WITH_QT) include(${PROJECT_SOURCE_DIR}/platform/qt/qt.cmake) diff --git a/LICENSES.core.md b/LICENSES.core.md index 558d0e48945..86a5dd3fa4e 100644 --- a/LICENSES.core.md +++ b/LICENSES.core.md @@ -178,34 +178,6 @@ DEALINGS IN THE SOFTWARE. --- -### [csscolorparser](https://github.com/mapbox/css-color-parser-cpp) by Dean McNamee and Konstantin Käfer - -``` -(c) Dean McNamee , 2012. -(c) Konstantin Käfer , 2014. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to -deal in the Software without restriction, including without limitation the -rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -sell copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS -IN THE SOFTWARE. - -``` - ---- - ### [earcut.hpp](https://github.com/mapbox/earcut.hpp) by Mapbox ``` @@ -633,4 +605,3 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ``` --- - diff --git a/docker/Dockerfile b/docker/Dockerfile index 8bc737daf6d..decb7d243cf 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -49,5 +49,9 @@ RUN chmod +x /usr/local/bin/startup.sh # This allows users to `docker run` without specifying -u and -g USER $USERNAME +ENV RUSTUP_HOME=/home/$USERNAME/.cache/.rustup \ + CARGO_HOME=/home/$USERNAME/.cache/.cargo \ + PATH=/home/$USERNAME/.cache/.cargo/bin:$PATH + ENTRYPOINT ["/usr/local/bin/startup.sh"] CMD ["bash"] diff --git a/docker/startup.sh b/docker/startup.sh index 6bb1f9a17e6..81530303dc7 100644 --- a/docker/startup.sh +++ b/docker/startup.sh @@ -10,4 +10,19 @@ if [ ! -d /app/.github ] || [ ! -d /home/user/.cache ]; then exit 1 fi + +if [ ! -f "$CARGO_HOME/env" ]; then + echo "Downloading and installing Rust..." + curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --profile minimal +fi +. "$CARGO_HOME/env" + + + +if ! command -v cxxbridge > /dev/null; then + echo "Installing cxxbridge..." + cargo install cxxbridge-cmd +fi + + exec "$@" diff --git a/platform/android/LICENSE.md b/platform/android/LICENSE.md index 72b908825de..bf720b2ae78 100644 --- a/platform/android/LICENSE.md +++ b/platform/android/LICENSE.md @@ -456,34 +456,6 @@ DEALINGS IN THE SOFTWARE. --- -### [csscolorparser](https://github.com/mapbox/css-color-parser-cpp) by Dean McNamee and Konstantin Käfer - -``` -(c) Dean McNamee , 2012. -(c) Konstantin Käfer , 2014. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to -deal in the Software without restriction, including without limitation the -rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -sell copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS -IN THE SOFTWARE. - -``` - ---- - ### [earcut.hpp](https://github.com/mapbox/earcut.hpp) by Mapbox ``` @@ -942,4 +914,3 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ``` --- - diff --git a/platform/android/android.cmake b/platform/android/android.cmake index b28104d3a86..608cddb12cc 100644 --- a/platform/android/android.cmake +++ b/platform/android/android.cmake @@ -11,7 +11,6 @@ include(${PROJECT_SOURCE_DIR}/vendor/icu.cmake) include(${PROJECT_SOURCE_DIR}/vendor/sqlite.cmake) # cmake-format: off -target_compile_options(mbgl-vendor-csscolorparser PRIVATE $<$:-Oz> $<$:-Qunused-arguments> $<$:-flto>) target_compile_options(mbgl-vendor-icu PRIVATE $<$:-Oz> $<$:-Qunused-arguments> $<$:-flto>) target_compile_options(mbgl-vendor-parsedate PRIVATE $<$:-Oz> $<$:-Qunused-arguments> $<$:-flto>) target_compile_options(mbgl-vendor-sqlite PRIVATE $<$:-Oz> $<$:-Qunused-arguments> $<$:-flto>) diff --git a/platform/ios/LICENSE.md b/platform/ios/LICENSE.md index 5a9143de0ce..5378fb1fbf3 100644 --- a/platform/ios/LICENSE.md +++ b/platform/ios/LICENSE.md @@ -1026,34 +1026,6 @@ DEALINGS IN THE SOFTWARE. --- -### [csscolorparser](https://github.com/mapbox/css-color-parser-cpp) by Dean McNamee and Konstantin Käfer - -``` -(c) Dean McNamee , 2012. -(c) Konstantin Käfer , 2014. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to -deal in the Software without restriction, including without limitation the -rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -sell copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS -IN THE SOFTWARE. - -``` - ---- - ### [earcut.hpp](https://github.com/mapbox/earcut.hpp) by Mapbox ``` @@ -1687,4 +1659,4 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -``` \ No newline at end of file +``` diff --git a/platform/linux/README.md b/platform/linux/README.md index 371d4ae1358..b0a07646fa5 100644 --- a/platform/linux/README.md +++ b/platform/linux/README.md @@ -19,6 +19,9 @@ cd maplibre-native # Install build tools apt install build-essential clang cmake ccache ninja-build pkg-config +# Install rust toolchain (default options should be sufficient) +curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh + # Install system dependencies apt install libcurl4-openssl-dev libglfw3-dev libuv1-dev libpng-dev libicu-dev libjpeg-turbo8-dev libwebp-dev xvfb ``` diff --git a/platform/qt/qt.cmake b/platform/qt/qt.cmake index 71957dff527..8c1547819a4 100644 --- a/platform/qt/qt.cmake +++ b/platform/qt/qt.cmake @@ -138,7 +138,6 @@ target_link_libraries( PUBLIC $ $ - $ $<$,$>>:z> $,$,Qt${QT_VERSION_MAJOR}::Sql> PRIVATE @@ -162,7 +161,6 @@ if(MLN_QT_HAS_PARENT) set(MLN_QT_VENDOR_LIBRARIES mbgl-vendor-parsedate mbgl-vendor-nunicode - mbgl-vendor-csscolorparser $<$:$> $<$,$>:$> PARENT_SCOPE diff --git a/rustutils/.gitignore b/rustutils/.gitignore new file mode 100644 index 00000000000..ea8c4bf7f35 --- /dev/null +++ b/rustutils/.gitignore @@ -0,0 +1 @@ +/target diff --git a/rustutils/Cargo.lock b/rustutils/Cargo.lock new file mode 100644 index 00000000000..16003c0398f --- /dev/null +++ b/rustutils/Cargo.lock @@ -0,0 +1,162 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "cc" +version = "1.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2aba8f4e9906c7ce3c73463f62a7f0c65183ada1a2d47e397cc8810827f9694f" + +[[package]] +name = "csscolorparser" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb2a7d3066da2de787b7f032c736763eb7ae5d355f81a68bab2675a96008b0bf" +dependencies = [ + "phf", +] + +[[package]] +name = "cxx" +version = "1.0.124" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "273dcfd3acd4e1e276af13ed2a43eea7001318823e7a726a6b3ed39b4acc0b82" +dependencies = [ + "cc", + "cxxbridge-flags", + "cxxbridge-macro", + "link-cplusplus", +] + +[[package]] +name = "cxxbridge-flags" +version = "1.0.124" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "839fcd5e43464614ffaa989eaf1c139ef1f0c51672a1ed08023307fa1b909ccd" + +[[package]] +name = "cxxbridge-macro" +version = "1.0.124" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b2c1c1776b986979be68bb2285da855f8d8a35851a769fca8740df7c3d07877" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "link-cplusplus" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d240c6f7e1ba3a28b0249f774e6a9dd0175054b52dfbb61b16eb8505c3785c9" +dependencies = [ + "cc", +] + +[[package]] +name = "phf" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ade2d8b8f33c7333b51bcf0428d37e217e9f32192ae4772156f65063b8ce03dc" +dependencies = [ + "phf_macros", + "phf_shared", +] + +[[package]] +name = "phf_generator" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48e4cc64c2ad9ebe670cb8fd69dd50ae301650392e81c05f9bfcb2d5bdbc24b0" +dependencies = [ + "phf_shared", + "rand", +] + +[[package]] +name = "phf_macros" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3444646e286606587e49f3bcf1679b8cef1dc2c5ecc29ddacaffc305180d464b" +dependencies = [ + "phf_generator", + "phf_shared", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "phf_shared" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90fcb95eef784c2ac79119d1dd819e162b5da872ce6f3c3abe1e8ca1c082f72b" +dependencies = [ + "siphasher", +] + +[[package]] +name = "proc-macro2" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" + +[[package]] +name = "rustutils" +version = "0.1.0" +dependencies = [ + "csscolorparser", + "cxx", +] + +[[package]] +name = "siphasher" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d" + +[[package]] +name = "syn" +version = "2.0.72" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc4b9b9bf2add8093d3f2c0204471e951b2285580335de42f9d2534f3ae7a8af" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" diff --git a/rustutils/Cargo.toml b/rustutils/Cargo.toml new file mode 100644 index 00000000000..cea6240b2d6 --- /dev/null +++ b/rustutils/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "rustutils" +version = "0.1.0" +authors = ["Yuri Astrakhan ", "MapLibre contributors"] +description = "Core Rust utilities for MapLibre Native" +edition = "2021" +license = "MIT OR Apache-2.0" +repository = "https://github.com/maplibre/maplibre-native" + +[lib] +crate-type = ["staticlib"] + +[dependencies] +csscolorparser = "0.6.2" +cxx = "1" + +[profile.release] +lto = true +codegen-units = 1 diff --git a/rustutils/src/lib.rs b/rustutils/src/lib.rs new file mode 100644 index 00000000000..5841c4daab3 --- /dev/null +++ b/rustutils/src/lib.rs @@ -0,0 +1,36 @@ +use crate::ffi::ParsedColor; +use csscolorparser::Color; + +#[cxx::bridge(namespace = "rustutils")] +mod ffi { + struct ParsedColor { + pub success: bool, + pub r: f32, + pub g: f32, + pub b: f32, + pub a: f32, + } + + extern "Rust" { + fn parse_css_color(css_str: &str) -> ParsedColor; + } +} + +fn parse_css_color(css_str: &str) -> ParsedColor { + css_str + .parse::() + .map(|color| ParsedColor { + success: true, + r: color.r as f32, + g: color.g as f32, + b: color.b as f32, + a: color.a as f32, + }) + .unwrap_or_else(|_| ParsedColor { + success: false, + r: 0.0, + g: 0.0, + b: 0.0, + a: 0.0, + }) +} diff --git a/src/mbgl/util/color.cpp b/src/mbgl/util/color.cpp index 44fb65f732a..433e8b8ee96 100644 --- a/src/mbgl/util/color.cpp +++ b/src/mbgl/util/color.cpp @@ -1,17 +1,16 @@ +#include + #include #include -#include +#include namespace mbgl { std::optional Color::parse(const std::string& s) { - auto css_color = CSSColorParser::parse(s); - - // Premultiply the color. - if (css_color) { - const float factor = css_color->a / 255; - return {{css_color->r * factor, css_color->g * factor, css_color->b * factor, css_color->a}}; + auto css_color = rustutils::parse_css_color(s); + if (css_color.success) { + return {{css_color.r, css_color.g, css_color.b, css_color.a}}; } else { return {}; } diff --git a/vendor/BUILD.bazel b/vendor/BUILD.bazel index 66815d83b44..27441081bbc 100644 --- a/vendor/BUILD.bazel +++ b/vendor/BUILD.bazel @@ -56,20 +56,6 @@ cc_library( visibility = ["//visibility:public"], ) -# vendor/csscolorparser -cc_library( - name = "csscolorparser", - srcs = glob( - [ - "csscolorparser/**/*.hpp", - "csscolorparser/**/*.cpp", - ], - ), - copts = CPP_FLAGS, - includes = ["csscolorparser"], - visibility = ["//visibility:public"], -) - # vendor/wagyu-files.json cc_library( name = "wagyu", diff --git a/vendor/csscolorparser.cmake b/vendor/csscolorparser.cmake deleted file mode 100644 index 1a45f97af3f..00000000000 --- a/vendor/csscolorparser.cmake +++ /dev/null @@ -1,37 +0,0 @@ -if(TARGET mbgl-vendor-csscolorparser) - return() -endif() - -if(MLN_WITH_QT) - add_library(mbgl-vendor-csscolorparser OBJECT) -else() - add_library(mbgl-vendor-csscolorparser STATIC) -endif() - -target_sources( - mbgl-vendor-csscolorparser PRIVATE - ${CMAKE_CURRENT_LIST_DIR}/csscolorparser/csscolorparser.cpp -) - -target_link_libraries( - mbgl-vendor-csscolorparser - PRIVATE mbgl-compiler-options -) - -target_include_directories( - mbgl-vendor-csscolorparser SYSTEM - PUBLIC ${CMAKE_CURRENT_LIST_DIR}/csscolorparser -) - -if(MSVC) - target_compile_options(mbgl-vendor-csscolorparser PRIVATE /wd4244) -endif() - -set_target_properties( - mbgl-vendor-csscolorparser - PROPERTIES - INTERFACE_MAPBOX_NAME "csscolorparser" - INTERFACE_MAPBOX_URL "https://github.com/mapbox/css-color-parser-cpp" - INTERFACE_MAPBOX_AUTHOR "Dean McNamee and Konstantin Käfer" - INTERFACE_MAPBOX_LICENSE ${CMAKE_CURRENT_LIST_DIR}/csscolorparser/LICENSE -) diff --git a/vendor/csscolorparser/LICENSE b/vendor/csscolorparser/LICENSE deleted file mode 100644 index 2408b4a02e1..00000000000 --- a/vendor/csscolorparser/LICENSE +++ /dev/null @@ -1,20 +0,0 @@ -(c) Dean McNamee , 2012. -(c) Konstantin Käfer , 2014. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to -deal in the Software without restriction, including without limitation the -rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -sell copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS -IN THE SOFTWARE. diff --git a/vendor/csscolorparser/csscolorparser.cpp b/vendor/csscolorparser/csscolorparser.cpp deleted file mode 100644 index 74764a22e24..00000000000 --- a/vendor/csscolorparser/csscolorparser.cpp +++ /dev/null @@ -1,288 +0,0 @@ -// (c) Dean McNamee , 2012. -// C++ port by Mapbox, Konstantin Käfer , 2014-2017. -// -// https://github.com/deanm/css-color-parser-js -// https://github.com/kkaefer/css-color-parser-cpp -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to -// deal in the Software without restriction, including without limitation the -// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -// sell copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS -// IN THE SOFTWARE. - -#include "csscolorparser/csscolorparser.hpp" - -#include -#include -#include -#include - -namespace CSSColorParser { - -// http://www.w3.org/TR/css3-color/ -struct NamedColor { const char *const name; const Color color; }; -const NamedColor namedColors[] = { - { "transparent", { 0, 0, 0, 0 } }, { "aliceblue", { 240, 248, 255, 1 } }, - { "antiquewhite", { 250, 235, 215, 1 } }, { "aqua", { 0, 255, 255, 1 } }, - { "aquamarine", { 127, 255, 212, 1 } }, { "azure", { 240, 255, 255, 1 } }, - { "beige", { 245, 245, 220, 1 } }, { "bisque", { 255, 228, 196, 1 } }, - { "black", { 0, 0, 0, 1 } }, { "blanchedalmond", { 255, 235, 205, 1 } }, - { "blue", { 0, 0, 255, 1 } }, { "blueviolet", { 138, 43, 226, 1 } }, - { "brown", { 165, 42, 42, 1 } }, { "burlywood", { 222, 184, 135, 1 } }, - { "cadetblue", { 95, 158, 160, 1 } }, { "chartreuse", { 127, 255, 0, 1 } }, - { "chocolate", { 210, 105, 30, 1 } }, { "coral", { 255, 127, 80, 1 } }, - { "cornflowerblue", { 100, 149, 237, 1 } }, { "cornsilk", { 255, 248, 220, 1 } }, - { "crimson", { 220, 20, 60, 1 } }, { "cyan", { 0, 255, 255, 1 } }, - { "darkblue", { 0, 0, 139, 1 } }, { "darkcyan", { 0, 139, 139, 1 } }, - { "darkgoldenrod", { 184, 134, 11, 1 } }, { "darkgray", { 169, 169, 169, 1 } }, - { "darkgreen", { 0, 100, 0, 1 } }, { "darkgrey", { 169, 169, 169, 1 } }, - { "darkkhaki", { 189, 183, 107, 1 } }, { "darkmagenta", { 139, 0, 139, 1 } }, - { "darkolivegreen", { 85, 107, 47, 1 } }, { "darkorange", { 255, 140, 0, 1 } }, - { "darkorchid", { 153, 50, 204, 1 } }, { "darkred", { 139, 0, 0, 1 } }, - { "darksalmon", { 233, 150, 122, 1 } }, { "darkseagreen", { 143, 188, 143, 1 } }, - { "darkslateblue", { 72, 61, 139, 1 } }, { "darkslategray", { 47, 79, 79, 1 } }, - { "darkslategrey", { 47, 79, 79, 1 } }, { "darkturquoise", { 0, 206, 209, 1 } }, - { "darkviolet", { 148, 0, 211, 1 } }, { "deeppink", { 255, 20, 147, 1 } }, - { "deepskyblue", { 0, 191, 255, 1 } }, { "dimgray", { 105, 105, 105, 1 } }, - { "dimgrey", { 105, 105, 105, 1 } }, { "dodgerblue", { 30, 144, 255, 1 } }, - { "firebrick", { 178, 34, 34, 1 } }, { "floralwhite", { 255, 250, 240, 1 } }, - { "forestgreen", { 34, 139, 34, 1 } }, { "fuchsia", { 255, 0, 255, 1 } }, - { "gainsboro", { 220, 220, 220, 1 } }, { "ghostwhite", { 248, 248, 255, 1 } }, - { "gold", { 255, 215, 0, 1 } }, { "goldenrod", { 218, 165, 32, 1 } }, - { "gray", { 128, 128, 128, 1 } }, { "green", { 0, 128, 0, 1 } }, - { "greenyellow", { 173, 255, 47, 1 } }, { "grey", { 128, 128, 128, 1 } }, - { "honeydew", { 240, 255, 240, 1 } }, { "hotpink", { 255, 105, 180, 1 } }, - { "indianred", { 205, 92, 92, 1 } }, { "indigo", { 75, 0, 130, 1 } }, - { "ivory", { 255, 255, 240, 1 } }, { "khaki", { 240, 230, 140, 1 } }, - { "lavender", { 230, 230, 250, 1 } }, { "lavenderblush", { 255, 240, 245, 1 } }, - { "lawngreen", { 124, 252, 0, 1 } }, { "lemonchiffon", { 255, 250, 205, 1 } }, - { "lightblue", { 173, 216, 230, 1 } }, { "lightcoral", { 240, 128, 128, 1 } }, - { "lightcyan", { 224, 255, 255, 1 } }, { "lightgoldenrodyellow", { 250, 250, 210, 1 } }, - { "lightgray", { 211, 211, 211, 1 } }, { "lightgreen", { 144, 238, 144, 1 } }, - { "lightgrey", { 211, 211, 211, 1 } }, { "lightpink", { 255, 182, 193, 1 } }, - { "lightsalmon", { 255, 160, 122, 1 } }, { "lightseagreen", { 32, 178, 170, 1 } }, - { "lightskyblue", { 135, 206, 250, 1 } }, { "lightslategray", { 119, 136, 153, 1 } }, - { "lightslategrey", { 119, 136, 153, 1 } }, { "lightsteelblue", { 176, 196, 222, 1 } }, - { "lightyellow", { 255, 255, 224, 1 } }, { "lime", { 0, 255, 0, 1 } }, - { "limegreen", { 50, 205, 50, 1 } }, { "linen", { 250, 240, 230, 1 } }, - { "magenta", { 255, 0, 255, 1 } }, { "maroon", { 128, 0, 0, 1 } }, - { "mediumaquamarine", { 102, 205, 170, 1 } }, { "mediumblue", { 0, 0, 205, 1 } }, - { "mediumorchid", { 186, 85, 211, 1 } }, { "mediumpurple", { 147, 112, 219, 1 } }, - { "mediumseagreen", { 60, 179, 113, 1 } }, { "mediumslateblue", { 123, 104, 238, 1 } }, - { "mediumspringgreen", { 0, 250, 154, 1 } }, { "mediumturquoise", { 72, 209, 204, 1 } }, - { "mediumvioletred", { 199, 21, 133, 1 } }, { "midnightblue", { 25, 25, 112, 1 } }, - { "mintcream", { 245, 255, 250, 1 } }, { "mistyrose", { 255, 228, 225, 1 } }, - { "moccasin", { 255, 228, 181, 1 } }, { "navajowhite", { 255, 222, 173, 1 } }, - { "navy", { 0, 0, 128, 1 } }, { "oldlace", { 253, 245, 230, 1 } }, - { "olive", { 128, 128, 0, 1 } }, { "olivedrab", { 107, 142, 35, 1 } }, - { "orange", { 255, 165, 0, 1 } }, { "orangered", { 255, 69, 0, 1 } }, - { "orchid", { 218, 112, 214, 1 } }, { "palegoldenrod", { 238, 232, 170, 1 } }, - { "palegreen", { 152, 251, 152, 1 } }, { "paleturquoise", { 175, 238, 238, 1 } }, - { "palevioletred", { 219, 112, 147, 1 } }, { "papayawhip", { 255, 239, 213, 1 } }, - { "peachpuff", { 255, 218, 185, 1 } }, { "peru", { 205, 133, 63, 1 } }, - { "pink", { 255, 192, 203, 1 } }, { "plum", { 221, 160, 221, 1 } }, - { "powderblue", { 176, 224, 230, 1 } }, { "purple", { 128, 0, 128, 1 } }, - { "red", { 255, 0, 0, 1 } }, { "rosybrown", { 188, 143, 143, 1 } }, - { "royalblue", { 65, 105, 225, 1 } }, { "saddlebrown", { 139, 69, 19, 1 } }, - { "salmon", { 250, 128, 114, 1 } }, { "sandybrown", { 244, 164, 96, 1 } }, - { "seagreen", { 46, 139, 87, 1 } }, { "seashell", { 255, 245, 238, 1 } }, - { "sienna", { 160, 82, 45, 1 } }, { "silver", { 192, 192, 192, 1 } }, - { "skyblue", { 135, 206, 235, 1 } }, { "slateblue", { 106, 90, 205, 1 } }, - { "slategray", { 112, 128, 144, 1 } }, { "slategrey", { 112, 128, 144, 1 } }, - { "snow", { 255, 250, 250, 1 } }, { "springgreen", { 0, 255, 127, 1 } }, - { "steelblue", { 70, 130, 180, 1 } }, { "tan", { 210, 180, 140, 1 } }, - { "teal", { 0, 128, 128, 1 } }, { "thistle", { 216, 191, 216, 1 } }, - { "tomato", { 255, 99, 71, 1 } }, { "turquoise", { 64, 224, 208, 1 } }, - { "violet", { 238, 130, 238, 1 } }, { "wheat", { 245, 222, 179, 1 } }, - { "white", { 255, 255, 255, 1 } }, { "whitesmoke", { 245, 245, 245, 1 } }, - { "yellow", { 255, 255, 0, 1 } }, { "yellowgreen", { 154, 205, 50, 1 } } -}; - -template -uint8_t clamp_css_byte(T i) { // Clamp to integer 0 .. 255. - i = ::round(i); // Seems to be what Chrome does (vs truncation). - return i < 0 ? 0 : i > 255 ? 255 : uint8_t(i); -} - -template -float clamp_css_float(T f) { // Clamp to float 0.0 .. 1.0. - return f < 0 ? 0 : f > 1 ? 1 : float(f); -} - -float parseFloat(const std::string& str) { - return strtof(str.c_str(), nullptr); -} - -int64_t parseInt(const std::string& str, uint8_t base = 10) { - return strtoll(str.c_str(), nullptr, base); -} - -uint8_t parse_css_int(const std::string& str) { // int or percentage. - if (str.length() && str.back() == '%') { - return clamp_css_byte(parseFloat(str) / 100.0f * 255.0f); - } else { - return clamp_css_byte(parseInt(str)); - } -} - -float parse_css_float(const std::string& str) { // float or percentage. - if (str.length() && str.back() == '%') { - return clamp_css_float(parseFloat(str) / 100.0f); - } else { - return clamp_css_float(parseFloat(str)); - } -} - -float css_hue_to_rgb(float m1, float m2, float h) { - if (h < 0.0f) { - h += 1.0f; - } else if (h > 1.0f) { - h -= 1.0f; - } - - if (h * 6.0f < 1.0f) { - return m1 + (m2 - m1) * h * 6.0f; - } - if (h * 2.0f < 1.0f) { - return m2; - } - if (h * 3.0f < 2.0f) { - return m1 + (m2 - m1) * (2.0f / 3.0f - h) * 6.0f; - } - return m1; -} - - - -std::vector split(const std::string& s, char delim) { - std::vector elems; - std::stringstream ss(s); - std::string item; - while (std::getline(ss, item, delim)) { - elems.push_back(item); - } - return elems; -} - -std::optional parse(const std::string& css_str) { - std::string str = css_str; - - // Remove all whitespace, not compliant, but should just be more accepting. - str.erase(std::remove(str.begin(), str.end(), ' '), str.end()); - - // Convert to lowercase. - std::transform(str.begin(), str.end(), str.begin(), ::tolower); - - for (const auto& namedColor : namedColors) { - if (str == namedColor.name) { - return { namedColor.color }; - } - } - - // #abc and #abc123 syntax. - if (str.length() && str.front() == '#') { - if (str.length() == 4) { - int64_t iv = parseInt(str.substr(1), 16); // TODO(deanm): Stricter parsing. - if (!(iv >= 0 && iv <= 0xfff)) { - return {}; - } else { - return {{ - static_cast(((iv & 0xf00) >> 4) | ((iv & 0xf00) >> 8)), - static_cast((iv & 0xf0) | ((iv & 0xf0) >> 4)), - static_cast((iv & 0xf) | ((iv & 0xf) << 4)), - 1 - }}; - } - } else if (str.length() == 7) { - int64_t iv = parseInt(str.substr(1), 16); // TODO(deanm): Stricter parsing. - if (!(iv >= 0 && iv <= 0xffffff)) { - return {}; // Covers NaN. - } else { - return {{ - static_cast((iv & 0xff0000) >> 16), - static_cast((iv & 0xff00) >> 8), - static_cast(iv & 0xff), - 1 - }}; - } - } - - return {}; - } - - size_t op = str.find_first_of('('), ep = str.find_first_of(')'); - if (op != std::string::npos && ep + 1 == str.length()) { - const std::string fname = str.substr(0, op); - const std::vector params = split(str.substr(op + 1, ep - (op + 1)), ','); - - float alpha = 1.0f; - - if (fname == "rgba" || fname == "rgb") { - if (fname == "rgba") { - if (params.size() != 4) { - return {}; - } - alpha = parse_css_float(params.back()); - } else { - if (params.size() != 3) { - return {}; - } - } - - return {{ - parse_css_int(params[0]), - parse_css_int(params[1]), - parse_css_int(params[2]), - alpha - }}; - - } else if (fname == "hsla" || fname == "hsl") { - if (fname == "hsla") { - if (params.size() != 4) { - return {}; - } - alpha = parse_css_float(params.back()); - } else { - if (params.size() != 3) { - return {}; - } - } - - float h = parseFloat(params[0]) / 360.0f; - float i; - // Normalize the hue to [0..1[ - h = std::modf(h, &i); - - // NOTE(deanm): According to the CSS spec s/l should only be - // percentages, but we don't bother and let float or percentage. - float s = parse_css_float(params[1]); - float l = parse_css_float(params[2]); - - float m2 = l <= 0.5f ? l * (s + 1.0f) : l + s - l * s; - float m1 = l * 2.0f - m2; - - return {{ - clamp_css_byte(css_hue_to_rgb(m1, m2, h + 1.0f / 3.0f) * 255.0f), - clamp_css_byte(css_hue_to_rgb(m1, m2, h) * 255.0f), - clamp_css_byte(css_hue_to_rgb(m1, m2, h - 1.0f / 3.0f) * 255.0f), - alpha - }}; - } - } - - return {}; -} - -} // namespace CSSColorParser diff --git a/vendor/csscolorparser/csscolorparser/csscolorparser.hpp b/vendor/csscolorparser/csscolorparser/csscolorparser.hpp deleted file mode 100644 index 7b41bfa5de2..00000000000 --- a/vendor/csscolorparser/csscolorparser/csscolorparser.hpp +++ /dev/null @@ -1,56 +0,0 @@ -// (c) Dean McNamee , 2012. -// C++ port by Mapbox, Konstantin Käfer , 2014-2017. -// -// https://github.com/deanm/css-color-parser-js -// https://github.com/kkaefer/css-color-parser-cpp -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to -// deal in the Software without restriction, including without limitation the -// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -// sell copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS -// IN THE SOFTWARE. - -#ifndef CSS_COLOR_PARSER_CPP -#define CSS_COLOR_PARSER_CPP - -#include -#include -#include - -namespace CSSColorParser { - -struct Color { - inline Color() { - } - inline Color(unsigned char r_, unsigned char g_, unsigned char b_, float a_) - : r(r_), g(g_), b(b_), a(a_ > 1 ? 1 : a_ < 0 ? 0 : a_) { - } - unsigned char r = 0, g = 0, b = 0; - float a = 1.0f; -}; - -inline bool operator==(const Color& lhs, const Color& rhs) { - return lhs.r == rhs.r && lhs.g == rhs.g && lhs.b == rhs.b && ::fabs(lhs.a - rhs.a) < 0.0001f; -} - -inline bool operator!=(const Color& lhs, const Color& rhs) { - return !(lhs == rhs); -} - -std::optional parse(const std::string& css_str); - -} // namespace CSSColorParser - -#endif