Overte C++ Documentation
Font.h
1 //
2 // Created by Bradley Austin Davis on 2015/07/16
3 // Copyright 2013 High Fidelity, Inc.
4 //
5 // Distributed under the Apache License, Version 2.0.
6 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
7 //
8 
9 #pragma once
10 #ifndef hifi_Font_h
11 #define hifi_Font_h
12 
13 #include <QObject>
14 
15 #include "Glyph.h"
16 #include "TextEffect.h"
17 #include "TextAlignment.h"
18 #include "TextVerticalAlignment.h"
19 #include <gpu/Batch.h>
20 #include <gpu/Pipeline.h>
21 
22 class Font : public QObject {
23  Q_OBJECT
24 
25 public:
26  using Pointer = std::shared_ptr<Font>;
27 
28  Font(const QString& family);
29 
30  void read(QIODevice& path);
31 
32  struct DrawParams {
33  vec4 bounds { 0.0f };
34  vec4 color { 0.0f };
35 
36  vec2 unitRange { 1.0f };
37 
38  int effect { 0 };
39  float effectThickness { 0.0f };
40 
41  vec3 effectColor { 0.0f };
42 
43 #if defined(__clang__)
44  __attribute__((unused))
45 #endif
46  float _spare;
47  };
48 
49  struct DrawInfo {
50  gpu::BufferPointer verticesBuffer { nullptr };
51  gpu::BufferPointer indicesBuffer { nullptr };
52  gpu::BufferPointer paramsBuffer { nullptr };
53  uint32_t indexCount;
54 
55  QString string;
56  glm::vec2 origin;
57  glm::vec2 bounds;
58  DrawParams params;
59 
60  float scale { 0.0f };
61  TextAlignment alignment { TextAlignment::LEFT };
62  TextVerticalAlignment verticalAlignment { TextVerticalAlignment::TOP };
63  };
64 
65  glm::vec2 computeExtent(const QString& str) const;
66  float getFontHeight() const { return _fontHeight; }
67 
68  struct DrawProps {
69  DrawProps(const QString& str, const glm::vec4& color, const glm::vec3& effectColor, const glm::vec2& origin, const glm::vec2& bounds,
70  float scale, float effectThickness, TextEffect effect, TextAlignment alignment, TextVerticalAlignment verticalAlignment, bool unlit,
71  bool forward, bool mirror, bool fading) :
72  str(str), color(color), effectColor(effectColor), origin(origin), bounds(bounds), scale(scale), effectThickness(effectThickness),
73  effect(effect), alignment(alignment), verticalAlignment(verticalAlignment), unlit(unlit), forward(forward), mirror(mirror), fading(fading) {}
74  DrawProps(const QString& str, const glm::vec4& color, const glm::vec2& origin, const glm::vec2& bounds, TextAlignment alignment, bool forward) :
75  str(str), color(color), origin(origin), bounds(bounds), alignment(alignment), forward(forward) {}
76 
77  QString str;
78  glm::vec4 color;
79  glm::vec3 effectColor { glm::vec3(0.0f) };
80  glm::vec2 origin;
81  glm::vec2 bounds;
82  float scale { 1.0f };
83  float effectThickness { 0.0f };
84  TextEffect effect { TextEffect::NO_EFFECT };
85  TextAlignment alignment { TextAlignment::LEFT };
86  TextVerticalAlignment verticalAlignment { TextVerticalAlignment::TOP };
87  bool unlit = true;
88  bool forward;
89  bool mirror = false;
90  bool fading = false;
91  };
92 
93  // Render string to batch
94  void drawString(gpu::Batch& batch, DrawInfo& drawInfo, const DrawProps& props);
95 
96  static Pointer load(const QString& family);
97 
98  bool isLoaded() const { return _loaded; }
99 
100 public slots:
101  void handleFontNetworkReply();
102 
103 private:
104  static Pointer load(const QString& family, QIODevice& fontFile);
105  QStringList tokenizeForWrapping(const QString& str) const;
106  QStringList splitLines(const QString& str) const;
107  float computeTokenWidth(const QString& str) const;
108 
109  const Glyph& getGlyph(const QChar& c) const;
110  void buildVertices(DrawInfo& drawInfo, const QString& str, const glm::vec2& origin, const glm::vec2& bounds, float scale, bool enlargeForShadows,
111  TextAlignment alignment, TextVerticalAlignment verticalAlignment);
112 
113  void setupGPU();
114 
115  // maps characters to cached glyph info
116  // HACK... the operator[] const for QHash returns a
117  // copy of the value, not a const value reference, so
118  // we declare the hash as mutable in order to avoid such
119  // copies
120  mutable QHash<QChar, Glyph> _glyphs;
121  Glyph _tofuGlyph;
122 
123  // Font characteristics
124  QString _family;
125  glm::vec2 _distanceRange { 1.0f };
126  float _fontHeight { 0.0f };
127  float _leading { 0.0f };
128  float _spaceWidth { 0.0f };
129 
130  bool _loaded { false };
131  bool _needsParamsUpdate { false };
132 
133  gpu::TexturePointer _texture;
134  gpu::BufferStreamPointer _stream;
135 
136  static std::map<std::tuple<bool, bool, bool, bool, bool>, gpu::PipelinePointer> _pipelines;
137  static gpu::Stream::FormatPointer _format;
138 };
139 
140 #endif
141