Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/rendering/StarFontTextureGroup.cpp
blob: 21f6d6aaa175252b0eabd08bea19513fdc2e86f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "StarFontTextureGroup.hpp"
#include "StarTime.hpp"
#include "StarImageProcessing.hpp"

namespace Star {

FontTextureGroup::FontTextureGroup(TextureGroupPtr textureGroup)
  : m_textureGroup(move(textureGroup)) {}

void FontTextureGroup::cleanup(int64_t timeout) {
  int64_t currentTime = Time::monotonicMilliseconds();
  eraseWhere(m_glyphs, [&](auto const& p) { return currentTime - p.second.time > timeout; });
}

void FontTextureGroup::switchFont(String const& font) {
  if (m_fontName != font) {
    m_fontName = font;
    auto find = m_fonts.find(font);
    m_font = find != m_fonts.end() ? find->second : m_defaultFont;
  }
}

void FontTextureGroup::addFont(FontPtr const& font, String const& name, bool default) {
  m_fonts[name] = font;
  if (default)
    m_defaultFont = m_font = font;
}

const FontTextureGroup::GlyphTexture& FontTextureGroup::glyphTexture(String::Char c, unsigned size, String const& processingDirectives)
{
  auto res = m_glyphs.insert(GlyphDescriptor{c, size, processingDirectives, m_font.get() }, GlyphTexture());

  if (res.second) {
    m_font->setPixelSize(size);
    Image image = m_font->render(c);
    if (!processingDirectives.empty()) {
      Vec2F preSize = Vec2F(image.size());
      auto imageOperations = parseImageOperations(processingDirectives);
      for (auto& imageOp : imageOperations) {
        if (auto border = imageOp.ptr<BorderImageOperation>())
          border->includeTransparent = true;
      }
      image = processImageOperations(imageOperations, image);
      res.first->second.processingOffset = preSize - Vec2F(image.size());
    }
    else
      res.first->second.processingOffset = Vec2F();

    res.first->second.texture = m_textureGroup->create(image);
  }

  res.first->second.time = Time::monotonicMilliseconds();
  return res.first->second;
}

TexturePtr FontTextureGroup::glyphTexturePtr(String::Char c, unsigned size) {
  return glyphTexture(c, size, "").texture;
}

TexturePtr FontTextureGroup::glyphTexturePtr(String::Char c, unsigned size, String const& processingDirectives) {
  return glyphTexture(c, size, processingDirectives).texture;
}

unsigned FontTextureGroup::glyphWidth(String::Char c, unsigned fontSize) {
  m_font->setPixelSize(fontSize);
  return m_font->width(c);
}

}