44#include < pybind11/stl.h>
55
66#include " ft2font.h"
7+ #include " mplutils.h"
78#include " _enums.h"
89
910#include < set>
@@ -18,23 +19,20 @@ using double_or_ = std::variant<double, T>;
1819
1920template <typename T>
2021static T
21- _double_to_ (const char *name, double_or_<T> & var)
22+ _double_to_ (const char *name, double_or_<T> var)
2223{
23- if (auto value = std::get_if<double >(&var)) {
24- auto api = py::module_::import (" matplotlib._api" );
25- auto warn = api.attr (" warn_deprecated" );
26- warn (" since" _a=" 3.10" , " name" _a=name, " obj_type" _a=" parameter as float" ,
27- " alternative" _a=" int({})" _s.format (name));
28- return static_cast <T>(*value);
29- } else if (auto value = std::get_if<T>(&var)) {
30- return *value;
31- } else {
32- // pybind11 will have only allowed types that match the variant, so this `else`
33- // can't happen. We only have this case because older macOS doesn't support
34- // `std::get` and using the conditional `std::get_if` means an `else` to silence
35- // compiler warnings about "unhandled" cases.
36- throw std::runtime_error (" Should not happen" );
37- }
24+ return std::visit (overloaded {
25+ [&](double value) {
26+ auto api = py::module_::import (" matplotlib._api" );
27+ auto warn = api.attr (" warn_deprecated" );
28+ warn (" since" _a=" 3.10" , " name" _a=name, " obj_type" _a=" parameter as float" ,
29+ " alternative" _a=" int({})" _s.format (name));
30+ return static_cast <T>(value);
31+ },
32+ [](T value) {
33+ return value;
34+ }
35+ }, var);
3836}
3937
4038/* *********************************************************************
@@ -603,22 +601,18 @@ static int
603601PyFT2Font_get_kerning (PyFT2Font *self, FT_UInt left, FT_UInt right,
604602 std::variant<FT_Kerning_Mode, FT_UInt> mode_or_int)
605603{
606- FT_Kerning_Mode mode;
607-
608- if (auto value = std::get_if<FT_UInt>(&mode_or_int)) {
609- auto api = py::module_::import (" matplotlib._api" );
610- auto warn = api.attr (" warn_deprecated" );
611- warn (" since" _a=" 3.10" , " name" _a=" mode" , " obj_type" _a=" parameter as int" ,
612- " alternative" _a=" Kerning enum values" );
613- mode = static_cast <FT_Kerning_Mode>(*value);
614- } else if (auto value = std::get_if<FT_Kerning_Mode>(&mode_or_int)) {
615- mode = *value;
616- } else {
617- // NOTE: this can never happen as pybind11 would have checked the type in the
618- // Python wrapper before calling this function, but we need to keep the
619- // std::get_if instead of std::get for macOS 10.12 compatibility.
620- throw py::type_error (" mode must be Kerning or int" );
621- }
604+ FT_Kerning_Mode mode = std::visit (overloaded {
605+ [&](FT_UInt value) {
606+ auto api = py::module_::import (" matplotlib._api" );
607+ auto warn = api.attr (" warn_deprecated" );
608+ warn (" since" _a=" 3.10" , " name" _a=" mode" , " obj_type" _a=" parameter as int" ,
609+ " alternative" _a=" Kerning enum values" );
610+ return static_cast <FT_Kerning_Mode>(value);
611+ },
612+ [](FT_Kerning_Mode value) {
613+ return value;
614+ }
615+ }, mode_or_int);
622616
623617 return self->get_kerning (left, right, mode);
624618}
@@ -708,36 +702,28 @@ PyFT2Font_set_text(PyFT2Font *self, std::u32string_view text, double angle = 0.0
708702 std::variant<FT2Font::LanguageType, std::string> languages_or_str = nullptr )
709703{
710704 std::vector<double > xys;
711- LoadFlags flags;
712-
713- if (auto value = std::get_if<FT_Int32>(&flags_or_int)) {
714- auto api = py::module_::import (" matplotlib._api" );
715- auto warn = api.attr (" warn_deprecated" );
716- warn (" since" _a=" 3.10" , " name" _a=" flags" , " obj_type" _a=" parameter as int" ,
717- " alternative" _a=" LoadFlags enum values" );
718- flags = static_cast <LoadFlags>(*value);
719- } else if (auto value = std::get_if<LoadFlags>(&flags_or_int)) {
720- flags = *value;
721- } else {
722- // NOTE: this can never happen as pybind11 would have checked the type in the
723- // Python wrapper before calling this function, but we need to keep the
724- // std::get_if instead of std::get for macOS 10.12 compatibility.
725- throw py::type_error (" flags must be LoadFlags or int" );
726- }
727-
728- FT2Font::LanguageType languages;
729- if (auto value = std::get_if<FT2Font::LanguageType>(&languages_or_str)) {
730- languages = std::move (*value);
731- } else if (auto value = std::get_if<std::string>(&languages_or_str)) {
732- languages = std::vector<FT2Font::LanguageRange>{
733- FT2Font::LanguageRange{*value, 0 , text.size ()}
734- };
735- } else {
736- // NOTE: this can never happen as pybind11 would have checked the type in the
737- // Python wrapper before calling this function, but we need to keep the
738- // std::get_if instead of std::get for macOS 10.12 compatibility.
739- throw py::type_error (" languages must be str or list of tuple" );
740- }
705+ LoadFlags flags = std::visit (overloaded {
706+ [&](FT_Int32 value) {
707+ auto api = py::module_::import (" matplotlib._api" );
708+ auto warn = api.attr (" warn_deprecated" );
709+ warn (" since" _a=" 3.10" , " name" _a=" flags" , " obj_type" _a=" parameter as int" ,
710+ " alternative" _a=" LoadFlags enum values" );
711+ return static_cast <LoadFlags>(value);
712+ },
713+ [](LoadFlags value) {
714+ return value;
715+ }
716+ }, flags_or_int);
717+
718+ FT2Font::LanguageType languages = std::visit (overloaded {
719+ [](FT2Font::LanguageType languages) {
720+ return languages;
721+ },
722+ [&](std::string value) {
723+ return FT2Font::LanguageType{{
724+ FT2Font::LanguageRange{value, 0 , text.size ()}}};
725+ }
726+ }, languages_or_str);
741727
742728 self->set_text (text, angle, static_cast <FT_Int32>(flags), features, languages, xys);
743729
@@ -783,22 +769,18 @@ PyFT2Font_load_char(PyFT2Font *self, long charcode,
783769{
784770 bool fallback = true ;
785771 FT2Font *ft_object = nullptr ;
786- LoadFlags flags;
787-
788- if (auto value = std::get_if<FT_Int32>(&flags_or_int)) {
789- auto api = py::module_::import (" matplotlib._api" );
790- auto warn = api.attr (" warn_deprecated" );
791- warn (" since" _a=" 3.10" , " name" _a=" flags" , " obj_type" _a=" parameter as int" ,
792- " alternative" _a=" LoadFlags enum values" );
793- flags = static_cast <LoadFlags>(*value);
794- } else if (auto value = std::get_if<LoadFlags>(&flags_or_int)) {
795- flags = *value;
796- } else {
797- // NOTE: this can never happen as pybind11 would have checked the type in the
798- // Python wrapper before calling this function, but we need to keep the
799- // std::get_if instead of std::get for macOS 10.12 compatibility.
800- throw py::type_error (" flags must be LoadFlags or int" );
801- }
772+ LoadFlags flags = std::visit (overloaded {
773+ [&](FT_Int32 value) {
774+ auto api = py::module_::import (" matplotlib._api" );
775+ auto warn = api.attr (" warn_deprecated" );
776+ warn (" since" _a=" 3.10" , " name" _a=" flags" , " obj_type" _a=" parameter as int" ,
777+ " alternative" _a=" LoadFlags enum values" );
778+ return static_cast <LoadFlags>(value);
779+ },
780+ [](LoadFlags value) {
781+ return value;
782+ }
783+ }, flags_or_int);
802784
803785 self->load_char (charcode, static_cast <FT_Int32>(flags), ft_object, fallback);
804786
@@ -835,22 +817,18 @@ static PyGlyph *
835817PyFT2Font_load_glyph (PyFT2Font *self, FT_UInt glyph_index,
836818 std::variant<LoadFlags, FT_Int32> flags_or_int = LoadFlags::FORCE_AUTOHINT)
837819{
838- LoadFlags flags;
839-
840- if (auto value = std::get_if<FT_Int32>(&flags_or_int)) {
841- auto api = py::module_::import (" matplotlib._api" );
842- auto warn = api.attr (" warn_deprecated" );
843- warn (" since" _a=" 3.10" , " name" _a=" flags" , " obj_type" _a=" parameter as int" ,
844- " alternative" _a=" LoadFlags enum values" );
845- flags = static_cast <LoadFlags>(*value);
846- } else if (auto value = std::get_if<LoadFlags>(&flags_or_int)) {
847- flags = *value;
848- } else {
849- // NOTE: this can never happen as pybind11 would have checked the type in the
850- // Python wrapper before calling this function, but we need to keep the
851- // std::get_if instead of std::get for macOS 10.12 compatibility.
852- throw py::type_error (" flags must be LoadFlags or int" );
853- }
820+ LoadFlags flags = std::visit (overloaded {
821+ [&](FT_Int32 value) {
822+ auto api = py::module_::import (" matplotlib._api" );
823+ auto warn = api.attr (" warn_deprecated" );
824+ warn (" since" _a=" 3.10" , " name" _a=" flags" , " obj_type" _a=" parameter as int" ,
825+ " alternative" _a=" LoadFlags enum values" );
826+ return static_cast <LoadFlags>(value);
827+ },
828+ [](LoadFlags value) {
829+ return value;
830+ }
831+ }, flags_or_int);
854832
855833 self->load_glyph (glyph_index, static_cast <FT_Int32>(flags));
856834
0 commit comments