µGFX  2.9
version 2.9
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.io/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  gU8 x_scale;
17  gU8 y_scale;
18  gI16 x0;
19  gI16 y0;
20 };
21 
22 static void scaled_pixel_callback(gI16 x, gI16 y, gU8 count,
23  gU8 alpha, void *state)
24 {
25  struct scaled_renderstate *rstate = state;
26  gU8 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 gU8 mf_scaled_character_width(const struct mf_font_s *font,
39  gU16 character)
40 {
41  struct mf_scaledfont_s *sfont = (struct mf_scaledfont_s*)font;
42  gU8 basewidth;
43 
44  basewidth = sfont->basefont->character_width(sfont->basefont, character);
45 
46  return sfont->x_scale * basewidth;
47 }
48 
49 gU8 mf_scaled_render_character(const struct mf_font_s *font,
50  gI16 x0, gI16 y0,
51  gU16 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  gU8 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  gU8 x_scale, gU8 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