diff --git a/src/include/OpenImageIO/imageio.h b/src/include/OpenImageIO/imageio.h index 56bdeadda5..14b677f936 100644 --- a/src/include/OpenImageIO/imageio.h +++ b/src/include/OpenImageIO/imageio.h @@ -3039,6 +3039,21 @@ inline bool attribute (string_view name, string_view val) { /// full paths), and all the directories that OpenImageIO will search for /// fonts. (Added in OpenImageIO 2.5) /// +/// - `string font_family_list` +/// +/// A semicolon-separated list of all the font family names that +/// OpenImageIO can find. (Added in OpenImageIO 3.0) +/// +/// - `string font_style_list:family` +/// +/// A semicolon-separated list of all the font style names that +/// belong to the given font family. (Added in OpenImageIO 3.0) +/// +/// - `string font_filename:family:style` +/// +/// The font file (with full path) that defines the given font +/// family and style. (Added in OpenImageIO 3.0) +/// /// - `string filter_list` /// /// A semicolon-separated list of all built-in 2D filters. (Added in diff --git a/src/include/imageio_pvt.h b/src/include/imageio_pvt.h index 4b854bcded..1d961731da 100644 --- a/src/include/imageio_pvt.h +++ b/src/include/imageio_pvt.h @@ -57,7 +57,12 @@ OIIO_API const std::vector& font_file_list(); OIIO_API const std::vector& font_list(); - +OIIO_API const std::vector& +font_family_list(); +OIIO_API const std::vector +font_style_list(string_view family); +OIIO_API const std::string +font_filename(string_view family, string_view style = ""); // Make sure all plugins are inventoried. For internal use only. diff --git a/src/libOpenImageIO/imagebufalgo_draw.cpp b/src/libOpenImageIO/imagebufalgo_draw.cpp index e5be580adb..39460bd04f 100644 --- a/src/libOpenImageIO/imagebufalgo_draw.cpp +++ b/src/libOpenImageIO/imagebufalgo_draw.cpp @@ -1318,4 +1318,54 @@ ImageBufAlgo::render_text(ImageBuf& R, int x, int y, string_view text, +const std::vector& +pvt::font_family_list() +{ +#ifdef USE_FREETYPE + lock_guard ft_lock(ft_mutex); + init_font_families(); +#endif + return s_font_families; +} + + +const std::vector +pvt::font_style_list(string_view family) +{ +#ifdef USE_FREETYPE + lock_guard ft_lock(ft_mutex); + init_font_families(); +#endif + auto it = s_font_styles.find(family); + if (it != s_font_styles.end()) + return it->second; + else + return std::vector(); +} + + +const std::string +pvt::font_filename(string_view family, string_view style) +{ + if (family.empty()) + return std::string(); + +#ifdef USE_FREETYPE + lock_guard ft_lock(ft_mutex); + init_font_families(); +#endif + + std::string font = family; + if (!style.empty()) + font = Strutil::fmt::format("{} {}", family, style); + + auto it = s_font_filename_per_family.find(font); + if (it != s_font_filename_per_family.end()) + return it->second; + else + return std::string(); +} + + + OIIO_NAMESPACE_END diff --git a/src/libOpenImageIO/imageio.cpp b/src/libOpenImageIO/imageio.cpp index 084540a108..fde80ba8b7 100644 --- a/src/libOpenImageIO/imageio.cpp +++ b/src/libOpenImageIO/imageio.cpp @@ -505,6 +505,23 @@ getattribute(string_view name, TypeDesc type, void* val) *(ustring*)val = ustring(Strutil::join(font_list(), ";")); return true; } + if (name == "font_family_list" && type == TypeString) { + *(ustring*)val = ustring(Strutil::join(font_family_list(), ";")); + return true; + } + if (Strutil::starts_with(name, "font_style_list:") && type == TypeString) { + string_view family = name.substr(strlen("font_style_list:")); + *(ustring*)val = ustring(Strutil::join(font_style_list(family), ";")); + return true; + } + if (Strutil::starts_with(name, "font_filename:") && type == TypeString) { + std::vector tokens; + Strutil::split(name, tokens, ":"); + string_view family = tokens.size() >= 1 ? tokens[1] : string_view(); + string_view style = tokens.size() >= 2 ? tokens[2] : string_view(); + *(ustring*)val = ustring(font_filename(family, style)); + return true; + } if (name == "filter_list" && type == TypeString) { std::vector filternames; for (int i = 0, e = Filter2D::num_filters(); i < e; ++i)