MPLABĀ® Harmony Graphics Suite
legato_font.h
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2020 Microchip Technology Inc. and its subsidiaries.
3 *
4 * Subject to your compliance with these terms, you may use Microchip software
5 * and any derivatives exclusively with Microchip products. It is your
6 * responsibility to comply with third party license terms applicable to your
7 * use of third party software (including open source software) that may
8 * accompany Microchip software.
9 *
10 * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
11 * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
12 * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
13 * PARTICULAR PURPOSE.
14 *
15 * IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
16 * INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
17 * WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
18 * BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
19 * FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN
20 * ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
21 * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
22 *******************************************************************************/
23 
24 /*******************************************************************************
25  Module for Microchip Graphics Library - Legato User Interface Library
26 
27  Company:
28  Microchip Technology Inc.
29 
30  File Name:
31  legato_font.h
32 
33  Summary:
34  Describes font assets
35 
36  Description:
37  Type definitions.
38 *******************************************************************************/
39 
46 #ifndef LE_FONT_H
47 #define LE_FONT_H
48 
51 
52 // *****************************************************************************
57 typedef enum leFontBPP
58 {
62 
63 // *****************************************************************************
64 /* Structure:
65  struct leFontGlyph
66 
67  Summary:
68  Describes a font glyph. A glyph is an individual character of a font.
69  Each glyph contains kerning data and options unique to itself. This
70  data is used for proper glyph positioning when rendering text.
71 
72  leChar codePoint - the code point of the glyph
73  int16_t width - the glyph's overall width
74  int16_t height - the glyph's overall height
75  int16_t advance - the amount the cursor should advance when preparing to
76  render this glyph
77  int16_t bearingX - the X offset from the advance
78  int16_t bearingY - the Y offset from the string baseline
79  uint16_t flags - general purpose flags
80  uint16_t dataRowWidth - the width of one row of glyph data in bytes
81  uint32_t dataOffset - the offset of this glyph in the font glyph table
82 */
89 typedef struct leFontGlyph
90 {
91  leChar codePoint;
92  int16_t width;
93  int16_t height;
94  int16_t advance;
95  int16_t bearingX;
96  int16_t bearingY;
97  uint16_t flags;
98  uint16_t dataRowWidth;
99  uint32_t dataOffset;
101 
102 // *****************************************************************************
103 /* Enumeration:
104  enum leFontType
105 
106  Summary:
107  Defines font types. Currently only raster fonts are supported but this
108  differentiation will allow for future expansion into vector font support.
109 */
115 typedef enum leFontType
116 {
117  LE_RASTER_FONT,
118  LE_VECTOR_FONT
120 
121 // *****************************************************************************
122 /* Structure:
123  struct leFont
124 
125  Summary:
126  The base definition of a font object.
127 
128  leStreamDescriptor header - describes where the font data is located
129  leFontType type - indicates the type of font
130 */
135 typedef struct leFont
136 {
137  leStreamDescriptor header;
138  leFontType type;
140 
141 // *****************************************************************************
142 /* Structure:
143  leFontAsset
144 
145  Summary:
146  Describes a rasterized font object. A raster font asset is a series of
147  raster images that represent linguistic characters. These characters are
148  referenced by an index called a 'code point'. This code point is 1-2 bytes
149  in length. Code points may be encoded to save space. Fonts also contain
150  general kerning data that describes character positioning data.
151 
152  struct leFont base - base font data
153  uint16_t height - font height in pixels
154  uint16_t baseline - the general font baseline in pixels;
155  leFontBPP bpp - the bits per pixel value of this font.
156  const uint8_t* glyphTable - pointer to the font's glyph data table
157 */
166 typedef struct leRasterFont
167 {
168  struct leFont base;
169  uint16_t height;
170  uint16_t baseline;
171  leFontBPP bpp;
172  const uint8_t* glyphTable;
174 
175 #if LE_STREAMING_ENABLED == 1
176 // *****************************************************************************
181 typedef void (*leFontStream_DrawCompleteFn)(uint32_t codepoint);
182 
183 // *****************************************************************************
184 /* Structure:
185  struct leFontStream
186 
187  Summary:
188  A specialized stream object that is capable of streaming and rendering
189  font glyph data
190 
191  leStream stream - base stream data
192  leResult open - function that opens this stream
193  leResult drawGlyph - function that streams and renders font glyph data
194 
195  Arguments:
196  const leFontGlyph* glyph - the glyph to render
197  int32_t x - screen X location to render the glyph
198  int32_t y - screen Y location to render the glyph
199  leColor clr - the glyph render color
200  uint32_t alpha - a global alpha value to apply
201  leFontStream_DrawCompleteFn cb - callback to be called when the glyph
202  finished drawing
203 
204  leBool isDone - function that queries if the stream is finished
205  void close - function that closes the stream
206 
207  leFontStream_DrawCompleteFn cb - callback that indicates when the stream has finished a particular code point
208 */
213 typedef struct leFontStream
214 {
215  leStream stream;
216 
217  leResult (*open)();
218  leResult (*drawGlyph)(const leFontGlyph* glyph, int32_t x, int32_t y, leColor clr, uint32_t alpha, leFontStream_DrawCompleteFn cb);
219  leBool (*isDone)();
220  void (*close)();
221 
222  leFontStream_DrawCompleteFn cb;
223 } leFontStream;
224 #endif
225 
226 #if LE_INCLUDE_DEFAULT_1BPP_FONT == 1
227 // *****************************************************************************
228 /* Type:
229  leRasterFont LiberationMono1
230 
231  Summary:
232  A pre-defined 12 point monospaced 1bpp font that includes the standard
233  ASCII range of characters.
234 */
235 LIB_EXPORT extern leRasterFont LiberationMono1;
236 #endif
237 
238 #if LE_INCLUDE_DEFAULT_8BPP_FONT == 1
239 // *****************************************************************************
240 /* Type:
241  lleRasterFont LiberationMono8
242 
243  Summary:
244  A pre-defined 12 point monospaced 8bpp font that includes the standard
245  ASCII range of characters.
246 */
247 LIB_EXPORT extern leRasterFont LiberationMono8;
248 #endif
249 
250 // *****************************************************************************
251 /* Function:
252  leResult leFont_GetGlyphInfo(const leFont* fnt,
253  uint32_t codepoint,
254  leFontGlyph* glyph)
255 
256  Summary:
257  Given a font asset and a codepoint, retrieves the glyph kerning data from
258  the font glyph data table.
259 
260  Parameters:
261  const leFont* fnt - the font to query
262  uint32_t codepoint - the codepoint to lookup
263  leFontGlyph* glyph - the glyph data that was retrieved
264 
265  Returns:
266 
267  Remarks:
268 */
283  uint32_t codepoint,
284  leFontGlyph* glyph);
285 
286 // *****************************************************************************
287 /* Function:
288  leResult leFont_GetGlyphRect(const leFontGlyph* glyph,
289  leRect* rect)
290 
291  Summary:
292  Given a font asset and a codepoint, retrieves the glyph rectangle in pixels.
293 
294  Parameters:
295  const leFontGlyph* glyph - the glyph kerning information
296  leRect* rect - the glyph rectangle in pixels
297 
298  Returns:
299 
300  Remarks:
301 */
316  leRect* rect);
317 
318 // *****************************************************************************
319 /* Function:
320  void leFont_DrawUnknownGlyph(int32_t x,
321  int32_t y,
322  const leFontGlyph* glyph,
323  leColor clr,
324  uint32_t a)
325 
326  Summary:
327  Draws the 'unknown glyph' symbol
328 
329  Parameters:
330  int32_t x - the screen x location to draw
331  int32_t y - the screen y location to draw
332  const leFontGlyph* glyph - the glyph kerning information
333  leColor clr - the glyph render color
334  uint32_t alpha - a global alpha value to apply
335 
336  Returns:
337 
338  Remarks:
339 */
355 void leFont_DrawUnknownGlyph(int32_t x,
356  int32_t y,
357  const leFontGlyph* glyph,
358  leColor clr,
359  uint32_t a);
360 
361 // *****************************************************************************
362 /* Function:
363  leResult leFont_DrawGlyph(const leFont* fnt,
364  const leFontGlyph* glyph,
365  int32_t x,
366  int32_t y,
367  leColor clr,
368  uint32_t a)
369 
370  Summary:
371  Draws a glyph
372 
373  Parameters:
374  const leFont* fnt - the font object to draw
375  const leFontGlyph* glyph - the glyph kerning information
376  int32_t x - the screen x location to draw
377  int32_t y - the screen y location to draw
378  leColor clr - the glyph render color
379  uint32_t alpha - a global alpha value to apply
380 
381  Returns:
382 
383  Remarks:
384 */
400 leResult leFont_DrawGlyph(const leFont* fnt,
401  const leFontGlyph* glyph,
402  int32_t x,
403  int32_t y,
404  leColor clr,
405  uint32_t a);
406 
407 // *****************************************************************************
408 /* Function:
409  leResult leFont_DrawGlyphData(const leFont* fnt,
410  const leFontGlyph* glyph,
411  const uint8_t* data,
412  int32_t x,
413  int32_t y,
414  leColor clr,
415  uint32_t a)
416 
417  Summary:
418  Draws a glyph from a raw data buffer
419 
420  Parameters:
421  const leFont* fnt - the font object to draw
422  const leFontGlyph* glyph - the glyph kerning information
423  const uint8_t* data - the data buffer to reference
424  int32_t x - the screen x location to draw
425  int32_t y - the screen y location to draw
426  leColor clr - the glyph render color
427  uint32_t alpha - a global alpha value to apply
428 
429  Returns:
430 
431  Remarks:
432 */
449  const leFontGlyph* glyph,
450  const uint8_t* data,
451  int32_t x,
452  int32_t y,
453  leColor clr,
454  uint32_t a);
455 
456 // *****************************************************************************
457 /* Function:
458  void leFont_DrawGlyphRow(leFontBPP bpp,
459  const uint8_t* data,
460  int32_t x,
461  int32_t y,
462  int32_t colStart,
463  int32_t colEnd,
464  leColor clr,
465  uint32_t a)
466 
467  Summary:
468  Draws a glyph from a raw data buffer
469 
470  Parameters:
471  leFontBPP bpp - the data format of the row data
472  const uint8_t* data - the data to reference
473  int32_t x - the screen x location to draw
474  int32_t y - the screen y location to draw
475  int32_t colStart - index of the start of the data
476  int32_t colEnd - index of the end of the data
477  leColor clr - the glyph render color
478  uint32_t alpha - a global alpha value to apply
479 
480  Returns:
481 
482  Remarks:
483 */
498  const uint8_t* data,
499  int32_t x,
500  int32_t y,
501  int32_t colStart,
502  int32_t colEnd,
503  leColor clr,
504  uint32_t a);
505 
506 #if LE_STREAMING_ENABLED == 1
507 // *****************************************************************************
508 /* Function:
509  leFontStream* leFont_GetStream(const leFont* fnt)
510 
511  Summary:
512  Creates a font stream for a given font
513 
514  Parameters:
515  const leFont* fnt - the font to stream
516 
517  Returns:
518 
519  Remarks:
520 */
531 leFontStream* leFont_GetStream(const leFont* fnt);
532 #endif
533 
534 #endif /* LE_FONT_H */
legato_math.h
Defines common math functions for general use.
legato_error.h
Error functions, macros and definitions.
leResult
leResult
This enum represents function call results.
Definition: legato_common.h:134
lePercentWholeRounded
LIB_EXPORT uint32_t lePercentWholeRounded(uint32_t l, uint32_t r)
Calculate percent whole rounded.
Definition: legato_math.c:204
legato_color.h
Color definitions and functions.
leRect
This struct represents a rectangle.
Definition: legato_common.h:405
leFont_DrawGlyphData
leResult leFont_DrawGlyphData(const leFont *fnt, const leFontGlyph *glyph, const uint8_t *data, int32_t x, int32_t y, leColor clr, uint32_t a)
Draws a glyph.
Definition: legato_font.c:290
LE_FONT_BPP_8
@ LE_FONT_BPP_8
Definition: legato_font.h:60
leFontBPP
leFontBPP
This enum represents a font.
Definition: legato_font.h:58
LE_FONT_BPP_1
@ LE_FONT_BPP_1
Definition: legato_font.h:59
lePercentOf
LIB_EXPORT uint32_t lePercentOf(uint32_t num, uint32_t percent)
Calculate percent of a number.
Definition: legato_math.c:218
legato_renderer.h
leFont_GetGlyphRect
leResult leFont_GetGlyphRect(const leFontGlyph *glyph, leRect *rect)
Get glyph rectangle.
Definition: legato_font.c:227
leStreamDescriptor::location
uint32_t location
Definition: legato_stream.h:58
leFont
struct leFont leFont
This struct represents a font object.
leFont
This struct represents a font object.
Definition: legato_font.h:136
leRasterFont
This struct represents a rasterized font object.
Definition: legato_font.h:167
leFont_DrawGlyph
leResult leFont_DrawGlyph(const leFont *fnt, const leFontGlyph *glyph, int32_t x, int32_t y, leColor clr, uint32_t a)
Draws a glyph.
Definition: legato_font.c:349
LE_STREAM_LOCATION_ID_INTERNAL
#define LE_STREAM_LOCATION_ID_INTERNAL
leEventResult
Definition: legato_stream.h:67
leBool
leBool
This enum represents booleans.
Definition: legato_common.h:157
legato_stream.h
Defines a common header for all stream operations.
leFontGlyph
This struct represents a font glyph.
Definition: legato_font.h:90
leFontGlyph
struct leFontGlyph leFontGlyph
This struct represents a font glyph.
leFont_GetGlyphInfo
leResult leFont_GetGlyphInfo(const leFont *fnt, uint32_t codepoint, leFontGlyph *glyph)
Get glyph info.
Definition: legato_font.c:171
leFontType
leFontType
This enum represents a font type.
Definition: legato_font.h:116
leClampi
LIB_EXPORT int32_t leClampi(int32_t min, int32_t max, int32_t i)
Calculates clamp of an integer.
Definition: legato_math.c:171
legato_rect.h
Rectangle functions and definitions.
leFont_DrawGlyphRow
void leFont_DrawGlyphRow(leFontBPP bpp, const uint8_t *data, int32_t x, int32_t y, int32_t colStart, int32_t colEnd, leColor clr, uint32_t a)
Draws a glyph from a raw data buffer.
Definition: legato_font.c:159
leChar
uint16_t leChar
This typedef represents Legato character.
Definition: legato_common.h:424
leStreamDescriptor
This struct represents a stream descriptor.
Definition: legato_stream.h:57
LE_TRUE
@ LE_TRUE
Definition: legato_common.h:159
legato_font.h
Font functions and definitions.
leStreamDescriptor::address
void * address
Definition: legato_stream.h:59
leFont_DrawUnknownGlyph
void leFont_DrawUnknownGlyph(int32_t x, int32_t y, const leFontGlyph *glyph, leColor clr, uint32_t a)
Drawn unknown glyph.
Definition: legato_font.c:241
leRasterFont
struct leRasterFont leRasterFont
This struct represents a rasterized font object.