Skip to content

Commit

Permalink
api: OIIO::getattribute queries for available font families and styles (
Browse files Browse the repository at this point in the history
AcademySoftwareFoundation#4523)

Add an API to query available fonts based on family and style name. The
API can be used to build a UI where users can select a font from a list.
They consist of new queries for `OIIO::getattribute()`:

- `string font_family_list`

  A semicolon-separated list of all the font family names that
  OpenImageIO can find. 

- `string font_style_list:family`

  A semicolon-separated list of all the font style names that
  belong to the given font family. 

- `string font_filename:family:style`

  The font file (with full path) that defines the given font
  family and style. 

Fixes AcademySoftwareFoundation#4503.


---------

Signed-off-by: peter.horvath <[email protected]>
  • Loading branch information
peterhorvath111 authored and lgritz committed Nov 15, 2024
1 parent 09687d9 commit 8da9ce1
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
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

0 comments on commit 8da9ce1

Please sign in to comment.