add function to get font file through fontconfig
This commit is contained in:
parent
e14956703c
commit
596c506307
11
api/basic_widgets/utils.hpp
Normal file
11
api/basic_widgets/utils.hpp
Normal file
|
@ -0,0 +1,11 @@
|
|||
#ifndef UTILS_HPP
|
||||
#define UTILS_HPP
|
||||
|
||||
#include <exception>
|
||||
#include <string>
|
||||
|
||||
struct NotFound : std::exception {};
|
||||
|
||||
std::string find_font(const std::string&);
|
||||
|
||||
#endif
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include <basic_widgets/button.hpp>
|
||||
#include <basic_widgets/horizontal_layout.hpp>
|
||||
#include <basic_widgets/utils.hpp>
|
||||
#include <basic_widgets/vertical_layout.hpp>
|
||||
|
||||
int main()
|
||||
|
@ -10,7 +11,7 @@ int main()
|
|||
int height = 480;
|
||||
|
||||
TTF_Init();
|
||||
TTF_Font* font = TTF_OpenFont("fonts/FiraMono-Bold.ttf", 20);
|
||||
TTF_Font* font = TTF_OpenFont(find_font("Monospace").c_str(), 20);
|
||||
|
||||
SDL_Init(SDL_INIT_VIDEO);
|
||||
SDL_Window* win = SDL_CreateWindow(
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include <basic_widgets/horizontal_layout.hpp>
|
||||
#include <basic_widgets/numeric_input.hpp>
|
||||
#include <basic_widgets/utils.hpp>
|
||||
#include <basic_widgets/vertical_layout.hpp>
|
||||
|
||||
int main()
|
||||
|
@ -10,7 +11,7 @@ int main()
|
|||
int height = 480;
|
||||
|
||||
TTF_Init();
|
||||
TTF_Font* font = TTF_OpenFont("fonts/FiraMono-Bold.ttf", 20);
|
||||
TTF_Font* font = TTF_OpenFont(find_font("Monospace").c_str(), 20);
|
||||
|
||||
SDL_Init(SDL_INIT_VIDEO);
|
||||
SDL_Window* win = SDL_CreateWindow(
|
||||
|
|
|
@ -6,6 +6,7 @@ sdl = [
|
|||
dependency('sdl2', version: '>=2.0.5'),
|
||||
dependency('SDL2_ttf')
|
||||
]
|
||||
fontconfig = dependency('fontconfig')
|
||||
|
||||
api = include_directories('api')
|
||||
|
||||
|
@ -15,16 +16,17 @@ libbasic_widgets = static_library('basic_widgets',
|
|||
'drawing_primitive.cpp',
|
||||
'horizontal_layout.cpp',
|
||||
'layout.cpp',
|
||||
'utils.cpp',
|
||||
'vertical_layout.cpp',
|
||||
'widget.cpp',
|
||||
dependencies : sdl,
|
||||
dependencies : [sdl, fontconfig],
|
||||
include_directories : api,
|
||||
install : true)
|
||||
|
||||
libbasic_widgets_dep = declare_dependency(
|
||||
include_directories : api,
|
||||
link_with: libbasic_widgets,
|
||||
dependencies : sdl)
|
||||
dependencies : [sdl, fontconfig])
|
||||
|
||||
executable('button_demo',
|
||||
'examples/button_example.cpp',
|
||||
|
|
37
utils.cpp
Normal file
37
utils.cpp
Normal file
|
@ -0,0 +1,37 @@
|
|||
#include <iostream>
|
||||
#include <fontconfig/fontconfig.h>
|
||||
|
||||
#include <basic_widgets/utils.hpp>
|
||||
|
||||
using namespace std;
|
||||
|
||||
string find_font(const string& pat)
|
||||
{
|
||||
FcConfig* conf = FcInitLoadConfigAndFonts();
|
||||
|
||||
FcPattern* pattern = FcNameParse((FcChar8*)pat.c_str());
|
||||
FcConfigSubstitute(conf, pattern, FcMatchPattern);
|
||||
FcDefaultSubstitute(pattern);
|
||||
|
||||
bool found = false;
|
||||
string file_path;
|
||||
FcResult res;
|
||||
FcPattern* font = FcFontMatch(conf, pattern, &res);
|
||||
if (font != nullptr)
|
||||
{
|
||||
FcChar8* file = nullptr;
|
||||
if (FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch)
|
||||
{
|
||||
file_path = (char*)file;
|
||||
found = true;
|
||||
}
|
||||
FcPatternDestroy(font);
|
||||
}
|
||||
FcConfigDestroy(conf);
|
||||
FcFini();
|
||||
|
||||
if (!found)
|
||||
throw NotFound();
|
||||
|
||||
return file_path;
|
||||
}
|
Loading…
Reference in a new issue