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