Ejemplo n.º 1
0
FontSourceHandle RpiPlatform::systemFont(const std::string& _name, const std::string& _weight,
        const std::string& _face) const {

    std::string fontFile = systemFontPath(m_fcConfig, _name, _weight, _face);

    if (fontFile.empty()) { return {}; }

    return FontSourceHandle(Url(fontFile));
}
Ejemplo n.º 2
0
FontID FontContext::addFont(const std::string& _family, const std::string& _weight,
                            const std::string& _style, bool _tryFallback) {

    unsigned int dataSize = 0;
    unsigned char* data = nullptr;
    int font = FONS_INVALID;

    std::string fontKey = _family + "_" + _weight + "_" + _style;

    auto it = m_fonts.find(fontKey);
    if (it != m_fonts.end()) {
        if (it->second < 0) {
            goto fallback;
        }
        return it->second;
    }

    {
        // Assuming bundled ttf file follows this convention
        auto bundledFontPath = "fonts/" + _family + "-" + _weight + _style + ".ttf";
        if (!(data = bytesFromFile(bundledFontPath.c_str(), PathType::resource, &dataSize)) &&
            !(data = bytesFromFile(bundledFontPath.c_str(), PathType::internal, &dataSize))) {
            const std::string sysFontPath = systemFontPath(_family, _weight, _style);
            if ( !(data = bytesFromFile(sysFontPath.c_str(), PathType::absolute, &dataSize)) ) {

                LOGE("Could not load font file %s", fontKey.c_str());
                m_fonts.emplace(std::move(fontKey), INVALID_FONT);
                goto fallback;
            }
        }
    }

    font = fonsAddFont(m_fsContext, fontKey.c_str(), data, dataSize);

    if (font == FONS_INVALID) {
        LOGE("Could not load font %s", fontKey.c_str());
        m_fonts.emplace(std::move(fontKey), INVALID_FONT);
        goto fallback;
    }

    m_fonts.emplace(std::move(fontKey), font);

    return font;

fallback:
    if (_tryFallback && m_fonts.size() > 0) {
        return 0;
    }
    return INVALID_FONT;
}