Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: Add API to query available font families and styles #4523

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/include/OpenImageIO/imageio.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/include/imageio_pvt.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ OIIO_API const std::vector<std::string>&
font_file_list();
OIIO_API const std::vector<std::string>&
font_list();

OIIO_API const std::vector<std::string>&
font_family_list();
OIIO_API const std::vector<std::string>
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.
Expand Down
50 changes: 50 additions & 0 deletions src/libOpenImageIO/imagebufalgo_draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1318,4 +1318,54 @@ ImageBufAlgo::render_text(ImageBuf& R, int x, int y, string_view text,



const std::vector<std::string>&
pvt::font_family_list()
{
#ifdef USE_FREETYPE
lock_guard ft_lock(ft_mutex);
init_font_families();
#endif
return s_font_families;
}


const std::vector<std::string>
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<std::string>();
}


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
17 changes: 17 additions & 0 deletions src/libOpenImageIO/imageio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<string_view> 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<string_view> filternames;
for (int i = 0, e = Filter2D::num_filters(); i < e; ++i)
Expand Down
Loading