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

summaryrefslogtreecommitdiff
path: root/source/rendering/StarTextPainter.hpp
blob: 05f16f257e6d84fbc2706d1cea52b5652685151d (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#pragma once

#include "StarFontTextureGroup.hpp"
#include "StarAnchorTypes.hpp"
#include "StarRoot.hpp"
#include "StarStringView.hpp"
#include "StarText.hpp"

namespace Star {

// deprecated in favor of explicit shadow color
enum class FontMode : uint8_t {
  Normal,
  Shadow
};

inline Color const& fontModeToColor(FontMode mode) {
  return mode == FontMode::Shadow ? Color::Black : Color::Clear;
}

STAR_CLASS(TextPainter);

struct TextPositioning {
  TextPositioning();

  TextPositioning(Vec2F pos,
      HorizontalAnchor hAnchor = HorizontalAnchor::LeftAnchor,
      VerticalAnchor vAnchor = VerticalAnchor::BottomAnchor,
      Maybe<unsigned> wrapWidth = {},
      Maybe<unsigned> charLimit = {});

  TextPositioning(Json const& v);
  Json toJson() const;

  TextPositioning translated(Vec2F translation) const;

  Vec2F pos;
  HorizontalAnchor hAnchor;
  VerticalAnchor vAnchor;
  Maybe<unsigned> wrapWidth;
  Maybe<unsigned> charLimit;
};

// Renders text while caching individual glyphs for fast rendering but with *no
// kerning*.
class TextPainter {
public:
  TextPainter(RendererPtr renderer, TextureGroupPtr textureGroup);

  RectF renderText(StringView s, TextPositioning const& position);
  RectF renderLine(StringView s, TextPositioning const& position);
  RectF renderGlyph(String::Char c, TextPositioning const& position);

  RectF determineTextSize(StringView s, TextPositioning const& position);
  RectF determineLineSize(StringView s, TextPositioning const& position);
  RectF determineGlyphSize(String::Char c, TextPositioning const& position);

  int glyphWidth(String::Char c);
  int stringWidth(StringView s, unsigned charLimit = 0);

    
  typedef function<bool(StringView, unsigned)> WrapTextCallback;
  bool processWrapText(StringView s, unsigned* wrapWidth, WrapTextCallback textFunc);

  List<StringView> wrapTextViews(StringView s, Maybe<unsigned> wrapWidth);
  StringList wrapText(StringView s, Maybe<unsigned> wrapWidth);

  unsigned fontSize() const;
  void setFontSize(unsigned size);
  void setLineSpacing(float lineSpacing);
  void setMode(FontMode mode);
  void setFontColor(Vec4B color);
  void setProcessingDirectives(StringView directives, bool back = false);
  void setFont(String const& font);
  TextStyle& setTextStyle(TextStyle const& textStyle);
  void clearTextStyle();
  void addFont(FontPtr const& font, String const& name);
  void reloadFonts();

  void cleanup(int64_t textureTimeout);
  void applyCommands(StringView unsplitCommands);
private:
  void modifyDirectives(Directives& directives);
  RectF doRenderText(StringView s, TextPositioning const& position, bool reallyRender, unsigned* charLimit);
  RectF doRenderLine(StringView s, TextPositioning const& position, bool reallyRender, unsigned* charLimit);
  RectF doRenderGlyph(String::Char c, TextPositioning const& position, bool reallyRender);

  void renderPrimitives();
  void renderGlyph(String::Char c, Vec2F const& screenPos, List<RenderPrimitive>& out, unsigned fontSize, float scale, Vec4B color, Directives const* processingDirectives = nullptr);
  static FontPtr loadFont(String const& fontPath, Maybe<String> fontName = {});

  RendererPtr m_renderer;
  List<RenderPrimitive> m_shadowPrimitives;
  List<RenderPrimitive> m_backPrimitives;
  List<RenderPrimitive> m_frontPrimitives;
  FontTextureGroup m_fontTextureGroup;

  TextStyle m_defaultRenderSettings;
  TextStyle m_renderSettings;
  TextStyle m_savedRenderSettings;

  String m_nonRenderedCharacters;
  TrackerListenerPtr m_reloadTracker;
};

}