GLVis  v4.2
Accurate and flexible finite element visualization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
font.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2022, Lawrence Livermore National Security, LLC. Produced
2 // at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3 // LICENSE and NOTICE for details. LLNL-CODE-443271.
4 //
5 // This file is part of the GLVis visualization tool and library. For more
6 // information and source code availability see https://glvis.org.
7 //
8 // GLVis is free software; you can redistribute it and/or modify it under the
9 // terms of the BSD-3 license. We welcome feedback and contributions, see file
10 // CONTRIBUTING.md for details.
11 
12 #ifndef GLVIS_FONT_HPP
13 #define GLVIS_FONT_HPP
14 
15 #include <ft2build.h>
16 #include FT_FREETYPE_H
17 #include FT_GLYPH_H
18 
19 #include <string>
20 #include <iostream>
21 
22 #include "gl/platform_gl.hpp"
23 
24 using namespace std;
25 
26 class GlVisFont
27 {
28 public:
29  struct glyph
30  {
31  uint32_t w, h;
32  int32_t bear_x, bear_y;
33  float adv_x, adv_y;
34  float tex_x;
35  };
36 private:
37  bool init;
38  bool font_init;
39 
40  GLenum alpha_channel;
41 
42  glyph font_chars[256];
43  float tex_w;
44  float tex_h;
45  uint32_t font_tex;
46 
47  FT_Library library;
48  FT_Face face;
49  bool face_has_kerning;
50 public:
51 
52  bool LoadFont(const std::string& path, int font_index, int font_size);
53 
54  const glyph &GetTexChar(char c) const
55  {
56  return font_chars[(uint8_t) c];
57  }
58 
60  void getObjectSize(const std::string& text, int& w, int& h);
61 
63  : init(false),
64  font_init(false),
65  face_has_kerning(false)
66  {
67  if (FT_Init_FreeType(&library))
68  {
69  cout << "GLVis: Can not initialize FreeType library!" << endl;
70  }
71  init = true;
72  }
73 
75  {
76  if (init)
77  {
78  FT_Done_FreeType(library);
79  }
80  }
81 
82  float GetKerning(char cprev, char c)
83  {
84  if (!face_has_kerning || cprev == '\0')
85  {
86  return 0;
87  }
88  FT_UInt glyph_prev = FT_Get_Char_Index(face, cprev);
89  FT_UInt glyph_curr = FT_Get_Char_Index(face, c);
90 
91  FT_Vector delta;
92  FT_Get_Kerning(face, glyph_prev, glyph_curr,
93  FT_KERNING_DEFAULT, &delta);
94  return delta.x / 64.f;
95  }
96 
97  bool isFontLoaded() { return font_init; }
98 
99  float getFontLineSpacing() const { return face->size->metrics.height/64.f; }
100 
101  float getFontDescender() const { return face->size->metrics.descender/64.f; }
102 
103  float getAtlasWidth() { return tex_w; }
104 
105  float getAtlasHeight() { return tex_h; }
106 
107  unsigned getFontTex() { return font_tex; }
108 
109  void setAlphaChannel(GLenum alpha) { alpha_channel = alpha; }
110 };
111 
112 #endif /* GLVIS_FONT_HPP */
int32_t bear_y
Definition: font.hpp:32
bool isFontLoaded()
Definition: font.hpp:97
void setAlphaChannel(GLenum alpha)
Definition: font.hpp:109
float getFontLineSpacing() const
Definition: font.hpp:99
GlVisFont()
Definition: font.hpp:62
float getFontDescender() const
Definition: font.hpp:101
float GetKerning(char cprev, char c)
Definition: font.hpp:82
~GlVisFont()
Definition: font.hpp:74
unsigned getFontTex()
Definition: font.hpp:107
float getAtlasHeight()
Definition: font.hpp:105
const glyph & GetTexChar(char c) const
Definition: font.hpp:54
float tex_x
Definition: font.hpp:34
float adv_y
Definition: font.hpp:33
uint32_t w
Definition: font.hpp:31
float getAtlasWidth()
Definition: font.hpp:103
int font_size
Definition: aux_vis.cpp:1641