|
| 1 | +#include "FontFinder.hpp" |
| 2 | +#include "../../helpers/Log.hpp" |
| 3 | +#include "../Fuzzy.hpp" |
| 4 | + |
| 5 | +#include <hyprutils/os/Process.hpp> |
| 6 | +#include <fontconfig/fontconfig.h> |
| 7 | + |
| 8 | +#include <algorithm> |
| 9 | + |
| 10 | +using namespace Hyprutils::OS; |
| 11 | + |
| 12 | +class CFontEntry : public IFinderResult { |
| 13 | + public: |
| 14 | + CFontEntry() = default; |
| 15 | + virtual ~CFontEntry() = default; |
| 16 | + |
| 17 | + virtual const std::string& fuzzable() { |
| 18 | + return m_fuzzable; |
| 19 | + } |
| 20 | + |
| 21 | + virtual eFinderTypes type() { |
| 22 | + return FINDER_MATH; |
| 23 | + } |
| 24 | + |
| 25 | + virtual const std::string& name() { |
| 26 | + return m_font; |
| 27 | + } |
| 28 | + |
| 29 | + virtual void run() { |
| 30 | + Debug::log(TRACE, "Copying {} with wl-copy", m_font); |
| 31 | + |
| 32 | + CProcess proc("wl-copy", {m_font}); |
| 33 | + proc.runAsync(); |
| 34 | + } |
| 35 | + |
| 36 | + std::string m_font, m_fuzzable; |
| 37 | +}; |
| 38 | + |
| 39 | +CFontFinder::CFontFinder() : m_valid(FcInit()) { |
| 40 | + if (!m_valid) |
| 41 | + Debug::log(ERR, "font: FcInit failed, fonts unavailable"); |
| 42 | + |
| 43 | + refreshFonts(); |
| 44 | +} |
| 45 | + |
| 46 | +CFontFinder::~CFontFinder() { |
| 47 | + FcFini(); |
| 48 | +} |
| 49 | + |
| 50 | +void CFontFinder::refreshFonts() { |
| 51 | + if (!m_valid) |
| 52 | + return; |
| 53 | + |
| 54 | + m_entries.clear(); |
| 55 | + m_entriesGeneric.clear(); |
| 56 | + |
| 57 | + FcPattern* pattern = FcPatternCreate(); |
| 58 | + FcObjectSet* objectSet = FcObjectSetBuild(FC_FAMILY, FC_STYLE, FC_FILE, nullptr); |
| 59 | + FcFontSet* fontSet = FcFontList(nullptr, pattern, objectSet); |
| 60 | + |
| 61 | + if (fontSet) { |
| 62 | + for (int i = 0; i < fontSet->nfont; i++) { |
| 63 | + FcPattern* font = fontSet->fonts[i]; |
| 64 | + FcChar8 * family = nullptr, *style = nullptr; |
| 65 | + |
| 66 | + if (FcPatternGetString(font, FC_FAMILY, 0, &family) != FcResultMatch || FcPatternGetString(font, FC_STYLE, 0, &style) != FcResultMatch) |
| 67 | + continue; |
| 68 | + |
| 69 | + if (!family || !style) |
| 70 | + continue; |
| 71 | + |
| 72 | + auto e = m_entries.emplace_back(makeShared<CFontEntry>()); |
| 73 | + e->m_font = std::format("{} {}", (char*)family, (char*)style); |
| 74 | + e->m_fuzzable = e->m_font; |
| 75 | + std::ranges::transform(e->m_fuzzable, e->m_fuzzable.begin(), ::tolower); |
| 76 | + m_entriesGeneric.emplace_back(std::move(e)); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + FcFontSetDestroy(fontSet); |
| 81 | + FcObjectSetDestroy(objectSet); |
| 82 | + FcPatternDestroy(pattern); |
| 83 | +} |
| 84 | + |
| 85 | +std::vector<SFinderResult> CFontFinder::getResultsForQuery(const std::string& query) { |
| 86 | + std::vector<SFinderResult> results; |
| 87 | + |
| 88 | + std::vector<SP<IFinderResult>> fuzzed; |
| 89 | + if (!query.empty()) |
| 90 | + fuzzed = Fuzzy::getNResults(m_entriesGeneric, query, MAX_RESULTS_PER_FINDER); |
| 91 | + else |
| 92 | + fuzzed = std::vector<SP<IFinderResult>>{m_entriesGeneric.begin(), m_entriesGeneric.begin() + std::min(m_entriesGeneric.size(), MAX_RESULTS_PER_FINDER)}; |
| 93 | + |
| 94 | + results.reserve(fuzzed.size()); |
| 95 | + |
| 96 | + for (const auto& f : fuzzed) { |
| 97 | + const auto p = reinterpretPointerCast<CFontEntry>(f); |
| 98 | + if (!p) |
| 99 | + continue; |
| 100 | + results.emplace_back(SFinderResult{ |
| 101 | + .label = p->m_font, |
| 102 | + .icon = "", |
| 103 | + .result = p, |
| 104 | + .overrideFont = p->m_font, |
| 105 | + }); |
| 106 | + } |
| 107 | + |
| 108 | + return results; |
| 109 | +} |
0 commit comments