AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
AFont.h
    1/*
    2 * AUI Framework - Declarative UI toolkit for modern C++20
    3 * Copyright (C) 2020-2025 Alex2772 and Contributors
    4 *
    5 * SPDX-License-Identifier: MPL-2.0
    6 *
    7 * This Source Code Form is subject to the terms of the Mozilla Public
    8 * License, v. 2.0. If a copy of the MPL was not distributed with this
    9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
   10 */
   11
   12#pragma once
   13
   14#include <string>
   15#include <glm/glm.hpp>
   16#include <AUI/Url/AUrl.h>
   17
   18#include "AUI/Render/FontRendering.h"
   19#include "AUI/GL/Texture2D.h"
   20
   21#include "AUI/Render/SimpleTexturePacker.h"
   22
   23#include "AUI/Common/AStringVector.h"
   24#include "AFontFamily.h"
   25#include <AUI/Common/AByteBuffer.h>
   26
   27class AString;
   28
   29class AFontManager;
   30
   31class FreeType;
   32
   33
   34struct FT_FaceRec_;
   35
   36class AFont {
   37public:
   38    struct Character {
   39        _<AImage> image;
   40        int advanceX, advanceY;
   41        int bearingX;
   42
   43        [[nodiscard]]
   44        bool empty() const {
   45            return image == nullptr;
   46        }
   47
   48        void* rendererData = nullptr;
   49    };
   50
   51    struct FontKey {
   52        unsigned size;
   53        FontRendering fr;
   54
   55        int hash() const {
   56            return (size << 2) | int(fr);
   57        }
   58
   59        bool operator<(const FontKey& f) const {
   60            return hash() < f.hash();
   61        }
   62    };
   63
   64    struct FontData {
   65        AVector<AOptional<Character>> characters;
   66        void* rendererData = nullptr;
   67    };
   68
   69
   70    using FontEntry = std::pair<FontKey, FontData&>;
   71
   72
   73private:
   74    _<FreeType> ft;
   75    AByteBuffer mFontDataBuffer;
   76    FT_FaceRec_* mFace;
   77
   78    AMap<FontKey, FontData> mCharData;
   79
   80    FontData& getFontEntry(unsigned size, FontRendering fr) {
   81        return mCharData[FontKey{size, fr}];
   82    }
   83
   84    Character renderGlyph(const FontEntry& fs, long glyph);
   85
   86public:
   87    AFont(AFontManager* fm, const AString& path);
   88
   89    AFont(AFontManager* fm, const AUrl& url);
   90
   91    FontEntry getFontEntry(const FontKey& key) {
   92        return {key, mCharData[key]};
   93    }
   94
   95    glm::vec2 getKerning(wchar_t left, wchar_t right);
   96
   97    AFont(const AFont&) = delete;
   98
   99    Character& getCharacter(const FontEntry& charset, long glyph);
  100
  101    float length(const FontEntry& charset, const AString& text);
  102
  103    template<class Iterator>
  104    float length(const FontEntry& charset, Iterator begin, Iterator end) {
  105        int size = charset.first.size;
  106        int advance = 0;
  107
  108        for (Iterator i = begin; i != end; i++) {
  109            if (*i == ' ')
  110                advance += getSpaceWidth(size);
  111            else if (*i == '\n')
  112                advance = 0;
  113            else {
  114                Character& ch = getCharacter(charset, *i);
  115                if (!ch.empty()) {
  116                    advance += ch.advanceX;
  117                    advance = glm::floor(advance);
  118                } else
  119                    advance += getSpaceWidth(size);
  120            }
  121        }
  122        return advance;
  123    }
  124
  125    AString
  126    trimStringToWidth(const FontEntry& charset, AString::iterator begin, AString::iterator end, float maxWidth) {
  127        float width = 0;
  128        for (auto i = begin; i != end; i++) {
  129            if (*i == '\n') {
  130                return AString(begin, i);
  131            }
  132            float charWidth = length(charset, i, std::next(i));
  133            if (width + charWidth > maxWidth) {
  134                return AString(begin, i);
  135            }
  136            width += charWidth;
  137        }
  138        return AString(begin, end);
  139    }
  140
  141
  142    bool isHasKerning();
  143
  144    [[nodiscard]]
  145    AString getFontFamilyName() const;
  146
  147    [[nodiscard]]
  148    AFontFamily::Weight getFontWeight() const;
  149
  150    int getAscenderHeight(unsigned size) const;
  151
  152    int getDescenderHeight(unsigned size) const;
  153
  154    int getSpaceWidth(unsigned size) {
  155        return size * 10 / 23;
  156    }
  157
  158    [[nodiscard]]
  159    bool isItalic() const;
  160};
std::vector-like growing array for byte storage.
Definition AByteBuffer.h:31
Definition AFontManager.h:19
A std::map with AUI extensions.
Definition AMap.h:218
Represents a Unicode character string.
Definition AString.h:38
A std::vector with AUI extensions.
Definition AVector.h:39
Definition FreeType.h:16
An std::weak_ptr with AUI extensions.
Definition SharedPtrTypes.h:179
FontRendering
Controls the expanding of AView.
Definition FontRendering.h:19
Definition AFont.h:38
Definition AFont.h:64
Definition AFont.h:51