Cleanup: minor changes to BKE_appdir_font_folder_default

- Ensure a trailing slash in BKE_blendfile_userdef_from_defaults
  instead of in-line platform specific checks.
- Take a buffer size argument for the directory.
- Use the documented size for SHGetSpecialFolderPathW wchar_t argument.
- Remove use of unsafe `wcscat` on WIN32
This commit is contained in:
Campbell Barton 2023-06-22 13:39:17 +10:00
parent 944663723a
commit 7d7ac591c8
4 changed files with 14 additions and 9 deletions

@ -32,6 +32,8 @@ const UserDef U_default = {
.pref_flag = USER_PREF_FLAG_SAVE,
.savetime = 2,
.tempdir = "",
/* Overwritten by #BKE_appdir_font_folder_default(..)
* unless the system font's cannot be found. */
.fontdir = "//",
.renderdir = "//",
.render_cachedir = "",

@ -131,7 +131,7 @@ const char *BKE_appdir_program_dir(void) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NO
/**
* Gets a good default directory for fonts.
*/
bool BKE_appdir_font_folder_default(char *dir);
bool BKE_appdir_font_folder_default(char *dir, size_t dir_maxncpy);
/**
* Find Python executable.

@ -227,26 +227,24 @@ bool BKE_appdir_folder_caches(char *path, const size_t path_maxncpy)
return true;
}
bool BKE_appdir_font_folder_default(char *dir)
bool BKE_appdir_font_folder_default(char *dir, size_t dir_maxncpy)
{
char test_dir[FILE_MAXDIR];
test_dir[0] = '\0';
#ifdef WIN32
wchar_t wpath[FILE_MAXDIR];
wchar_t wpath[MAX_PATH];
if (SHGetSpecialFolderPathW(0, wpath, CSIDL_FONTS, 0)) {
wcscat(wpath, L"\\");
BLI_strncpy_wchar_as_utf8(test_dir, wpath, sizeof(test_dir));
}
#elif defined(__APPLE__)
STRNCPY(test_dir, BLI_expand_tilde("~/Library/Fonts/"));
BLI_path_slash_ensure(test_dir, sizeof(test_dir));
STRNCPY(test_dir, BLI_expand_tilde("~/Library/Fonts"));
#else
STRNCPY(test_dir, "/usr/share/fonts");
#endif
if (test_dir[0] && BLI_exists(test_dir)) {
BLI_strncpy(dir, test_dir, FILE_MAXDIR);
BLI_strncpy(dir, test_dir, dir_maxncpy);
return true;
}
return false;

@ -1214,8 +1214,13 @@ UserDef *BKE_blendfile_userdef_from_defaults(void)
userdef->flag &= ~USER_SCRIPT_AUTOEXEC_DISABLE;
#endif
/* System-specific fonts directory. */
BKE_appdir_font_folder_default(userdef->fontdir);
/* System-specific fonts directory.
* NOTE: when not found, leaves as-is (`//` for the blend-file directory). */
if (BKE_appdir_font_folder_default(userdef->fontdir, sizeof(userdef->fontdir))) {
/* Not actually needed, just a convention that directory selection
* adds a trailing slash. */
BLI_path_slash_ensure(userdef->fontdir, sizeof(userdef->fontdir));
}
userdef->memcachelimit = min_ii(BLI_system_memory_max_in_megabytes_int() / 2,
userdef->memcachelimit);