Skip to content

Commit d1f198c

Browse files
api: OIIO::getattribute queries for available font families and styles (#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 #4503. --------- Signed-off-by: peter.horvath <[email protected]>
1 parent 3c3f715 commit d1f198c

File tree

4 files changed

+88
-1
lines changed

4 files changed

+88
-1
lines changed

src/include/OpenImageIO/imageio.h

+15
Original file line numberDiff line numberDiff line change
@@ -3039,6 +3039,21 @@ inline bool attribute (string_view name, string_view val) {
30393039
/// full paths), and all the directories that OpenImageIO will search for
30403040
/// fonts. (Added in OpenImageIO 2.5)
30413041
///
3042+
/// - `string font_family_list`
3043+
///
3044+
/// A semicolon-separated list of all the font family names that
3045+
/// OpenImageIO can find. (Added in OpenImageIO 3.0)
3046+
///
3047+
/// - `string font_style_list:family`
3048+
///
3049+
/// A semicolon-separated list of all the font style names that
3050+
/// belong to the given font family. (Added in OpenImageIO 3.0)
3051+
///
3052+
/// - `string font_filename:family:style`
3053+
///
3054+
/// The font file (with full path) that defines the given font
3055+
/// family and style. (Added in OpenImageIO 3.0)
3056+
///
30423057
/// - `string filter_list`
30433058
///
30443059
/// A semicolon-separated list of all built-in 2D filters. (Added in

src/include/imageio_pvt.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ OIIO_API const std::vector<std::string>&
5757
font_file_list();
5858
OIIO_API const std::vector<std::string>&
5959
font_list();
60-
60+
OIIO_API const std::vector<std::string>&
61+
font_family_list();
62+
OIIO_API const std::vector<std::string>
63+
font_style_list(string_view family);
64+
OIIO_API const std::string
65+
font_filename(string_view family, string_view style = "");
6166

6267

6368
// Make sure all plugins are inventoried. For internal use only.

src/libOpenImageIO/imagebufalgo_draw.cpp

+50
Original file line numberDiff line numberDiff line change
@@ -1318,4 +1318,54 @@ ImageBufAlgo::render_text(ImageBuf& R, int x, int y, string_view text,
13181318

13191319

13201320

1321+
const std::vector<std::string>&
1322+
pvt::font_family_list()
1323+
{
1324+
#ifdef USE_FREETYPE
1325+
lock_guard ft_lock(ft_mutex);
1326+
init_font_families();
1327+
#endif
1328+
return s_font_families;
1329+
}
1330+
1331+
1332+
const std::vector<std::string>
1333+
pvt::font_style_list(string_view family)
1334+
{
1335+
#ifdef USE_FREETYPE
1336+
lock_guard ft_lock(ft_mutex);
1337+
init_font_families();
1338+
#endif
1339+
auto it = s_font_styles.find(family);
1340+
if (it != s_font_styles.end())
1341+
return it->second;
1342+
else
1343+
return std::vector<std::string>();
1344+
}
1345+
1346+
1347+
const std::string
1348+
pvt::font_filename(string_view family, string_view style)
1349+
{
1350+
if (family.empty())
1351+
return std::string();
1352+
1353+
#ifdef USE_FREETYPE
1354+
lock_guard ft_lock(ft_mutex);
1355+
init_font_families();
1356+
#endif
1357+
1358+
std::string font = family;
1359+
if (!style.empty())
1360+
font = Strutil::fmt::format("{} {}", family, style);
1361+
1362+
auto it = s_font_filename_per_family.find(font);
1363+
if (it != s_font_filename_per_family.end())
1364+
return it->second;
1365+
else
1366+
return std::string();
1367+
}
1368+
1369+
1370+
13211371
OIIO_NAMESPACE_END

src/libOpenImageIO/imageio.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,23 @@ getattribute(string_view name, TypeDesc type, void* val)
505505
*(ustring*)val = ustring(Strutil::join(font_list(), ";"));
506506
return true;
507507
}
508+
if (name == "font_family_list" && type == TypeString) {
509+
*(ustring*)val = ustring(Strutil::join(font_family_list(), ";"));
510+
return true;
511+
}
512+
if (Strutil::starts_with(name, "font_style_list:") && type == TypeString) {
513+
string_view family = name.substr(strlen("font_style_list:"));
514+
*(ustring*)val = ustring(Strutil::join(font_style_list(family), ";"));
515+
return true;
516+
}
517+
if (Strutil::starts_with(name, "font_filename:") && type == TypeString) {
518+
std::vector<string_view> tokens;
519+
Strutil::split(name, tokens, ":");
520+
string_view family = tokens.size() >= 1 ? tokens[1] : string_view();
521+
string_view style = tokens.size() >= 2 ? tokens[2] : string_view();
522+
*(ustring*)val = ustring(font_filename(family, style));
523+
return true;
524+
}
508525
if (name == "filter_list" && type == TypeString) {
509526
std::vector<string_view> filternames;
510527
for (int i = 0, e = Filter2D::num_filters(); i < e; ++i)

0 commit comments

Comments
 (0)