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 <gpu/Batch.h>
19 #include <gpu/Pipeline.h>
20 
21 class Font : public QObject {
22  Q_OBJECT
23 
24 public:
25  using Pointer = std::shared_ptr<Font>;
26 
27  Font();
28 
29  void read(QIODevice& path);
30 
31  struct DrawParams {
32  vec4 color { 0 };
33 
34  vec3 effectColor { 0 };
35  float effectThickness { 0 };
36 
37  int effect { 0 };
38 
39 #if defined(__clang__)
40  __attribute__((unused))
41 #endif
42  vec3 _spare;
43  };
44 
45  struct DrawInfo {
46  gpu::BufferPointer verticesBuffer { nullptr };
47  gpu::BufferPointer indicesBuffer { nullptr };
48  gpu::BufferPointer paramsBuffer { nullptr };
49  uint32_t indexCount;
50 
51  QString string;
52  glm::vec2 origin;
53  glm::vec2 bounds;
54  DrawParams params;
55  };
56 
57  glm::vec2 computeExtent(const QString& str) const;
58  float getFontSize() const { return _fontSize; }
59 
60  // Render string to batch
61  void drawString(gpu::Batch& batch, DrawInfo& drawInfo, const QString& str, const glm::vec4& color,
62  const glm::vec3& effectColor, float effectThickness, TextEffect effect, TextAlignment alignment,
63  const glm::vec2& origin, const glm::vec2& bound, float scale, bool unlit, bool forward);
64 
65  static Pointer load(const QString& family);
66 
67  bool isLoaded() const { return _loaded; }
68  void setLoaded(bool loaded) { _loaded = loaded; }
69 
70 public slots:
71  void handleFontNetworkReply();
72 
73 private:
74  static Pointer load(QIODevice& fontFile);
75  QStringList tokenizeForWrapping(const QString& str) const;
76  QStringList splitLines(const QString& str) const;
77  glm::vec2 computeTokenExtent(const QString& str) const;
78 
79  const Glyph& getGlyph(const QChar& c) const;
80  void buildVertices(DrawInfo& drawInfo, const QString& str, const glm::vec2& origin, const glm::vec2& bounds, float scale, bool enlargeForShadows,
81  TextAlignment alignment);
82 
83  void setupGPU();
84 
85  // maps characters to cached glyph info
86  // HACK... the operator[] const for QHash returns a
87  // copy of the value, not a const value reference, so
88  // we declare the hash as mutable in order to avoid such
89  // copies
90  mutable QHash<QChar, Glyph> _glyphs;
91 
92  // Font characteristics
93  QString _family;
94  float _fontSize { 0.0f };
95  float _leading { 0.0f };
96  float _ascent { 0.0f };
97  float _descent { 0.0f };
98  float _spaceWidth { 0.0f };
99 
100  float _scale { 0.0f };
101  TextAlignment _alignment;
102 
103  bool _loaded { true };
104 
105  gpu::TexturePointer _texture;
106  gpu::BufferStreamPointer _stream;
107 
108  static std::map<std::tuple<bool, bool, bool>, gpu::PipelinePointer> _pipelines;
109  static gpu::Stream::FormatPointer _format;
110 };
111 
112 #endif
113