1 /*
2 * font.h
3 * DIN Is Noise is copyright (c) 2006-2021 Jagannathan Sampath
4 * DIN Is Noise is released under GNU Public License 2.0
5 * For more information, please visit https://dinisnoise.org/
6 */
7 
8 #ifndef _FONT
9 #define _FONT
10 
11 #include <string>
12 #include <fstream>
13 #include <map>
14 
15 #include "dingl.h"
16 #include "glyph.h"
17 
18 #define MAKETYPE(N, T, U, V) \
19 struct N { \
20   T U;\
21   T V;\
22   N () { U = 0; V = 0;}\
23 };
24 
25 MAKETYPE (charwidtht, int, max, avg)
26 MAKETYPE (charheightt, int, max, avg)
27 MAKETYPE (cellsizet, int, x, y)
28 MAKETYPE (spacingt, int, ch, word)
29 
30 struct font {
31 
32   std::string fname;
33   std::string name;
34 
35   int nchars;
36 
37   charwidtht charwidth;
38   charheightt charheight;
39   cellsizet cellsize;
40   spacingt spacing;
41 
42   int lift;
43   int headroom; // space above char
44 
45   std::map <char, glyph> characters; // vector desc of chars
46   std::map < char, std::map<char, int> > kern; // char-char kerning
47 
48   int mod;
49 
50   font (const std::string& fn);
51   ~font ();
52   void load (const std::string& fn);
53   void load (std::ifstream& file);
54   void save ();
filenamefont55   const std::string& filename () const { return fname;}
56 
57   int char_width (char c);
58   int char_height (char c);
59   void calc_line_height ();
60 
61   void draw_char (char c, int x, int y, int z = 0);
62 
63   const std::map<char, glyph>& get_chars ();
64   void set_chars (const std::map<char, glyph>& chars);
65 
66 #ifdef __SVG_OUT__
67   std::ofstream svg;
68   void write_char (char c, int x, int y, int z = 0);
69 #endif
70 
71 #ifdef __PLOTTER_OUT__
72 	std::ofstream hpgl;
73 	void plot_char (char c, int x, int y, int z = 0);
74 #endif
75 
76 };
77 
78 extern font fnt;
79 extern int line_height;
80 
81 inline void draw_char (char c, int x, int y, int z = 0) {
82   fnt.draw_char (c, x, y, z);
83 }
84 
85 int draw_string (const std::string& s, int x, int y, int z = 0);
86 
87 #ifdef __SVG_OUT__
88 	int write_string (const std::string& s, int x, int y, int z = 0);
89 #endif
90 
91 #ifdef __PLOTTER_OUT__
92 	int plot_string (const std::string& s, int x, int y, int z = 0);
93 #endif
94 
95 int get_char_width (const std::string& s);
96 int get_char_height (const std::string& s);
97 
98 #endif
99