version 2.8
mf_scaledfont.c
1 /*
2  * This file is subject to the terms of the GFX License. If a copy of
3  * the license was not distributed with this file, you can obtain one at:
4  *
5  * http://ugfx.org/license.html
6  */
7 
8 #include "mf_scaledfont.h"
9 
10 #ifndef MF_NO_COMPILE
11 
12 struct scaled_renderstate
13 {
14  mf_pixel_callback_t orig_callback;
15  void *orig_state;
16  uint8_t x_scale;
17  uint8_t y_scale;
18  int16_t x0;
19  int16_t y0;
20 };
21 
22 static void scaled_pixel_callback(int16_t x, int16_t y, uint8_t count,
23  uint8_t alpha, void *state)
24 {
25  struct scaled_renderstate *rstate = state;
26  uint8_t dy;
27 
28  count *= rstate->x_scale;
29  x = rstate->x0 + x * rstate->x_scale;
30  y = rstate->y0 + y * rstate->y_scale;
31 
32  for (dy = 0; dy < rstate->y_scale; dy++)
33  {
34  rstate->orig_callback(x, y + dy, count, alpha, rstate->orig_state);
35  }
36 }
37 
38 uint8_t mf_scaled_character_width(const struct mf_font_s *font,
39  uint16_t character)
40 {
41  struct mf_scaledfont_s *sfont = (struct mf_scaledfont_s*)font;
42  uint8_t basewidth;
43 
44  basewidth = sfont->basefont->character_width(sfont->basefont, character);
45 
46  return sfont->x_scale * basewidth;
47 }
48 
49 uint8_t mf_scaled_render_character(const struct mf_font_s *font,
50  int16_t x0, int16_t y0,
51  uint16_t character,
52  mf_pixel_callback_t callback,
53  void *state)
54 {
55  struct mf_scaledfont_s *sfont = (struct mf_scaledfont_s*)font;
56  struct scaled_renderstate rstate;
57  uint8_t basewidth;
58 
59  rstate.orig_callback = callback;
60  rstate.orig_state = state;
61  rstate.x_scale = sfont->x_scale;
62  rstate.y_scale = sfont->y_scale;
63  rstate.x0 = x0;
64  rstate.y0 = y0;
65 
66  basewidth = sfont->basefont->render_character(sfont->basefont, 0, 0,
67  character, scaled_pixel_callback, &rstate);
68 
69  return sfont->x_scale * basewidth;
70 }
71 
72 void mf_scale_font(struct mf_scaledfont_s *newfont,
73  const struct mf_font_s *basefont,
74  uint8_t x_scale, uint8_t y_scale)
75 {
76  newfont->font = *basefont;
77  newfont->basefont = basefont;
78 
79  newfont->font.width *= x_scale;
80  newfont->font.height *= y_scale;
81  newfont->font.baseline_x *= x_scale;
82  newfont->font.baseline_y *= y_scale;
83  newfont->font.min_x_advance *= x_scale;
84  newfont->font.max_x_advance *= x_scale;
85  newfont->font.line_height *= y_scale;
86  newfont->font.character_width = &mf_scaled_character_width;
87  newfont->font.render_character = &mf_scaled_render_character;
88 
89  newfont->x_scale = x_scale;
90  newfont->y_scale = y_scale;
91 }
92 
93 #endif //MF_NO_COMPILE