FabGL
ESP32 Display Controller and Graphics Library
fabfonts.cpp
1 /*
2  Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) - <http://www.fabgl.com>
3  Copyright (c) 2019-2021 Fabrizio Di Vittorio.
4  All rights reserved.
5 
6 
7 * Please contact fdivitto2013@gmail.com if you need a commercial license.
8 
9 
10 * This library and related software is available under GPL v3.
11 
12  FabGL is free software: you can redistribute it and/or modify
13  it under the terms of the GNU General Public License as published by
14  the Free Software Foundation, either version 3 of the License, or
15  (at your option) any later version.
16 
17  FabGL is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with FabGL. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 
27 
28 #define FABGL_FONT_INCLUDE_DEFINITION
29 
30 #include "fabfonts.h"
31 
32 
33 
34 namespace fabgl {
35 
36 
37 // do not include all fonts in "font" folder to avoid waste of flash
38 // for apps that uses getPresetFontInfo() (used in Terminal class), getPresetFontInfoFromHeight()
39 // and getPresetFixedFont().
40 static const FontInfo * FIXED_WIDTH_EMBEDDED_FONTS[] = {
41  // please, bigger fonts first!
42  &FONT_8x19,
43  &FONT_8x16,
44  &FONT_8x14,
45  &FONT_8x8,
46  &FONT_8x9,
47  &FONT_6x8,
48  &FONT_5x7,
49  &FONT_4x6,
50 };
51 
52 
53 static const FontInfo * VAR_WIDTH_EMBEDDED_FONTS[] = {
54  // please, bigger fonts first!
55  &FONT_std_24,
56  &FONT_std_22,
57  &FONT_std_18,
58  &FONT_std_17,
59  &FONT_std_16,
60  &FONT_std_15,
61  &FONT_std_14,
62  &FONT_std_12,
63 };
64 
65 
66 
67 FontInfo const * getFixedWidthFont(int index)
68 {
69  return FIXED_WIDTH_EMBEDDED_FONTS[index];
70 }
71 
72 
73 int getFixedWidthFontCount()
74 {
75  return sizeof(FIXED_WIDTH_EMBEDDED_FONTS) / sizeof(FontInfo*);
76 }
77 
78 
79 FontInfo const * getVarWidthFont(int index)
80 {
81  return VAR_WIDTH_EMBEDDED_FONTS[index];
82 }
83 
84 
85 int getVarWidthFontCount()
86 {
87  return sizeof(VAR_WIDTH_EMBEDDED_FONTS) / sizeof(FontInfo*);
88 }
89 
90 
91 FontInfo const * getPresetFontInfo(int viewPortWidth, int viewPortHeight, int columns, int rows)
92 {
93  FontInfo const * fontInfo = nullptr;
94  for (int i = 0; i < getFixedWidthFontCount(); ++i) {
95  fontInfo = getFixedWidthFont(i);
96  if (viewPortWidth / fontInfo->width >= columns && viewPortHeight / fontInfo->height >= rows)
97  return fontInfo;
98  }
99  // not found, return the smallest
100  return fontInfo;
101 }
102 
103 
104 FontInfo const * getPresetFontInfoFromHeight(int height, bool fixedWidth)
105 {
106  FontInfo const * fontInfo = nullptr;
107  if (fixedWidth) {
108  for (int i = 0; i < getFixedWidthFontCount(); ++i) {
109  fontInfo = getFixedWidthFont(i);
110  if (height >= fontInfo->height)
111  return fontInfo;
112  }
113  } else {
114  for (int i = 0; i < getVarWidthFontCount(); ++i) {
115  fontInfo = getVarWidthFont(i);
116  if (height >= fontInfo->height)
117  return fontInfo;
118  }
119  }
120  // not found, return the smallest
121  return fontInfo;
122 }
123 
124 
125 FontInfo const * getPresetFixedFont(int width, int height)
126 {
127  FontInfo const * fontInfo = nullptr;
128  for (int i = 0; i < getFixedWidthFontCount(); ++i) {
129  fontInfo = getFixedWidthFont(i);
130  if (height == fontInfo->height && width == fontInfo->width)
131  return fontInfo;
132  }
133  // not found, return the smallest (TODO: find a way to get meaningful result)
134  return fontInfo;
135 }
136 
137 
138 
139 }
Definition: canvas.cpp:36
uint8_t height
uint8_t width