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 0e7f28204c7..b287da082d9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -135,9 +135,6 @@ set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_MINSIZEREL ON) set(CMAKE_POSITION_INDEPENDENT_CODE ON) set(CMAKE_VISIBILITY_INLINES_HIDDEN 1) -add_subdirectory(${PROJECT_SOURCE_DIR}/rustutils) - - if(MLN_WITH_QT) add_library(mbgl-core OBJECT) else() @@ -1336,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) @@ -1358,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 @@ -1368,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 @@ -1377,7 +1385,7 @@ target_link_libraries( mbgl-vendor-unique_resource mbgl-vendor-vector-tile mbgl-vendor-wagyu - rustutils + rustutils_headers PUBLIC Mapbox::Base Mapbox::Base::Extras::expected-lite @@ -1387,9 +1395,11 @@ target_link_libraries( Mapbox::Base::variant TracyClient unordered_dense + rustutils ) export(TARGETS + rustutils_headers mbgl-core rustutils mapbox-base @@ -1407,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 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/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/startup.sh b/platform/linux/startup.sh index 547f6cfb69e..ba1c51b0bfb 100644 --- a/platform/linux/startup.sh +++ b/platform/linux/startup.sh @@ -18,4 +18,11 @@ fi . "$CARGO_HOME/env" + +if ! command -v cxxbridge > /dev/null; then + echo "Installing cxxbridge..." + cargo install cxxbridge-cmd +fi + + exec "$@" 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/CMakeLists.txt b/rustutils/CMakeLists.txt deleted file mode 100644 index 07544af9c96..00000000000 --- a/rustutils/CMakeLists.txt +++ /dev/null @@ -1,34 +0,0 @@ -project(RustUtils) - -if (CMAKE_BUILD_TYPE STREQUAL "Debug") - set(CARGO_CMD cargo build --verbose) - set(TARGET_DIR "debug") -else () - set(CARGO_CMD cargo build --release --verbose) - set(TARGET_DIR "release") -endif () - -if (ENABLE_LTO) - set(RUST_FLAGS "-Clinker-plugin-lto" "-Clinker=clang-17" "-Clink-arg=-fuse-ld=lld-17") -endif () - -set(RUST_UTILS_LIB "${CMAKE_CURRENT_BINARY_DIR}/${TARGET_DIR}/librustutils.a") -set(RUST_UTILS_CXX "${CMAKE_CURRENT_BINARY_DIR}/rustutils.cpp") - -add_library(rustutils STATIC ${RUST_UTILS_CXX}) - -add_custom_command( - OUTPUT ${RUST_UTILS_CXX} - COMMAND CARGO_TARGET_DIR=${CMAKE_CURRENT_BINARY_DIR} RUSTFLAGS="${RUST_FLAGS}" ${CARGO_CMD} - COMMAND cp ${CMAKE_CURRENT_BINARY_DIR}/cxxbridge/rustutils/src/lib.rs.cc ${RUST_UTILS_CXX} - COMMAND cp ${CMAKE_CURRENT_BINARY_DIR}/cxxbridge/rustutils/src/lib.rs.h ${CMAKE_CURRENT_BINARY_DIR}/rustutils.h - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} -) - -#file(INSTALL "${SOURCE_PATH}/LICENSE" DESTINATION "${CURRENT_PACKAGES_DIR}/share/${PORT}" RENAME copyright) - -target_link_libraries(rustutils pthread dl ${RUST_UTILS_LIB}) - -add_test(NAME rustutils_test - COMMAND cargo test - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/rustutils/Cargo.lock b/rustutils/Cargo.lock index e6afe11094b..e31e75a6b32 100644 --- a/rustutils/Cargo.lock +++ b/rustutils/Cargo.lock @@ -19,26 +19,12 @@ dependencies = [ ] [[package]] -name = "cssparser" -version = "0.34.0" +name = "csscolorparser" +version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7c66d1cd8ed61bf80b38432613a7a2f09401ab8d0501110655f8b341484a3e3" +checksum = "eb2a7d3066da2de787b7f032c736763eb7ae5d355f81a68bab2675a96008b0bf" dependencies = [ - "cssparser-macros", - "dtoa-short", - "itoa", "phf", - "smallvec", -] - -[[package]] -name = "cssparser-macros" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13b588ba4ac1a99f7f2964d24b3d896ddc6bf847ee3855dbd4366f058cfcd331" -dependencies = [ - "quote", - "syn", ] [[package]] @@ -85,27 +71,6 @@ dependencies = [ "syn", ] -[[package]] -name = "dtoa" -version = "1.0.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dcbb2bf8e87535c23f7a8a321e364ce21462d0ff10cb6407820e8e96dfff6653" - -[[package]] -name = "dtoa-short" -version = "0.3.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd1511a7b6a56299bd043a9c167a6d2bfb37bf84a6dfceaba651168adfb43c87" -dependencies = [ - "dtoa", -] - -[[package]] -name = "itoa" -version = "1.0.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" - [[package]] name = "link-cplusplus" version = "1.0.9" @@ -200,7 +165,7 @@ checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" name = "rustutils" version = "0.1.0" dependencies = [ - "cssparser", + "csscolorparser", "cxx", "cxx-build", ] @@ -217,12 +182,6 @@ version = "0.3.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d" -[[package]] -name = "smallvec" -version = "1.13.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" - [[package]] name = "syn" version = "2.0.72" diff --git a/rustutils/Cargo.toml b/rustutils/Cargo.toml index 3084213936d..b917a64c30a 100644 --- a/rustutils/Cargo.toml +++ b/rustutils/Cargo.toml @@ -5,7 +5,7 @@ edition = "2021" license = "MIT" [dependencies] -cssparser = "0.34.0" +csscolorparser = "0.6.2" cxx = "1" [build-dependencies] diff --git a/rustutils/src/lib.rs b/rustutils/src/lib.rs index 1d20932439b..5841c4daab3 100644 --- a/rustutils/src/lib.rs +++ b/rustutils/src/lib.rs @@ -1,21 +1,36 @@ -#[cxx::bridge] +use crate::ffi::ParsedColor; +use csscolorparser::Color; + +#[cxx::bridge(namespace = "rustutils")] mod ffi { - extern "Rust" { - fn add(left: usize, right: usize) -> usize; + struct ParsedColor { + pub success: bool, + pub r: f32, + pub g: f32, + pub b: f32, + pub a: f32, } -} -pub fn add(left: usize, right: usize) -> usize { - left + right + extern "Rust" { + fn parse_css_color(css_str: &str) -> ParsedColor; + } } -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); - } +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