1 /*
2   fonts.h
3 
4   Copyright (c) 2009-2017
5   http://www.tuxpaint.org/
6 
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 2 of the License, or
10   (at your option) any later version.
11 
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16 
17   You should have received a copy of the GNU General Public License
18   along with this program; if not, write to the Free Software
19   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   (See COPYING.txt)
21 
22   $Id$
23 */
24 
25 #ifndef FONTS_H
26 #define FONTS_H
27 
28 // plan to rip this out as soon as it is considered stable
29 //#define THREADED_FONTS
30 #define FORKED_FONTS
31 #if defined(WIN32) || defined(__BEOS__)
32 #undef FORKED_FONTS
33 #endif
34 #ifdef __APPLE__
35 #undef FORKED_FONTS
36 #endif
37 
38 
39 #include "SDL.h"
40 #include "SDL_ttf.h"
41 
42 #ifndef NO_SDLPANGO
43 #include "SDL_Pango.h"
44 #endif
45 
46 #define PANGO_DEFAULT_FONT "BitStream Vera"
47 
48 #include "compiler.h"
49 
50 /* Disable threaded font loading on Windows */
51 #if !defined(FORKED_FONTS) && !defined(WIN32)
52 #include "SDL_thread.h"
53 #include "SDL_mutex.h"
54 #else
55 /* This shouldn't really be here :-)
56  * Move into 'fonts.c' and the code in 'tuxpaint.c'
57  * that uses this lot should be put into 'fonts.c' as well.
58  */
59 
60 #ifdef NO_SDLPANGO
61 /* Only kill SDL_thread stuff when we're not using Pango, because we need it to let fontconfig make its cache (takes a long time the first time) -bjk 2010.04.27 */
62 #define SDL_CreateThread(fn,vp) (void*)(long)(fn(vp))
63 #define SDL_WaitThread(tid,rcp) do{(void)tid;(void)rcp;}while(0)
64 #define SDL_Thread int
65 #define SDL_mutex int
66 #define SDL_CreateMutex() 0     // creates in released state
67 #define SDL_DestroyMutex(lock)
68 #define SDL_mutexP(lock)        // take lock
69 #define SDL_mutexV(lock)        // release lock
70 #endif
71 
72 #endif
73 
74 extern SDL_Thread *font_thread;
75 
76 extern volatile long font_thread_done;
77 extern volatile long font_thread_aborted;
78 extern volatile long waiting_for_fonts;
79 extern int font_socket_fd;
80 
81 extern int no_system_fonts;
82 extern int all_locale_fonts;
83 
84 /* FIXME: SDL_ttf is up to 2.0.8, so we can probably fully remove this;
85    -bjk 2007.06.05 */
86 /*
87 TTF_Font *BUGFIX_TTF_OpenFont206(const char *const file, int ptsize);
88 #define TTF_OpenFont    BUGFIX_TTF_OpenFont206
89 */
90 
91 
92 /* Stuff that wraps either SDL_Pango or SDL_TTF for font rendering: */
93 
94 enum
95 {
96 #ifndef NO_SDLPANGO
97   FONT_TYPE_PANGO,
98 #endif
99   FONT_TYPE_TTF
100 };
101 
102 typedef struct TuxPaint_Font_s
103 {
104 #ifndef NO_SDLPANGO
105   SDLPango_Context *pango_context;
106 #endif
107   int typ;
108   TTF_Font *ttf_font;
109   int height;
110   char *desc;
111 } TuxPaint_Font;
112 
113 int TuxPaint_Font_FontHeight(TuxPaint_Font * tpf);
114 
115 
116 #ifdef FORKED_FONTS
117 void reliable_write(int fd, const void *buf, size_t count);
118 void run_font_scanner(SDL_Surface * screen, const char *restrict const locale);
119 void receive_some_font_info(SDL_Surface * screen);
120 #endif
121 
122 //////////////////////////////////////////////////////////////////////
123 // font stuff
124 
125 // example from a Debian box with MS fonts:
126 // start with 232 files
127 // remove "Cursor", "Webdings", "Dingbats", "Standard Symbols L"
128 // split "Condensed" faces out into own family
129 // group by family
130 // end up with 34 user choices
131 
132 extern int text_state;
133 extern unsigned text_size;
134 
135 // nice progression (alternating 33% and 25%) 9 12 18 24 36 48 72 96 144 192
136 // commonly hinted sizes seem to be: 9, 10, 12, 14, 18, 20 (less so), 24
137 // reasonable: 9,12,18... and 10,14,18...
138 static int text_sizes[] = {
139 #ifndef OLPC_XO
140   9,
141 #endif
142   12, 18, 24, 36, 48,
143   56, 64, 96, 112, 128, 160
144 };                              // point sizes
145 
146 #define MIN_TEXT_SIZE 0u
147 #define MAX_TEXT_SIZE (sizeof text_sizes / sizeof text_sizes[0] - 1)
148 
149 // for sorting through the font files at startup
150 typedef struct style_info
151 {
152   char *filename;
153   char *directory;
154   char *family;                 // name like "FooCorp Thunderstruck"
155   char *style;                  // junk like "Oblique Demi-Bold"
156   int italic;
157   int boldness;
158   int score;
159   int truetype;                 // Is it? (TrueType gets priority)
160 } style_info;
161 
162 // user's notion of a font
163 typedef struct family_info
164 {
165   char *directory;
166   char *family;
167   char *filename[4];
168   TuxPaint_Font *handle;
169   int score;
170 } family_info;
171 
172 extern TuxPaint_Font *medium_font, *small_font, *large_font, *locale_font;
173 
174 extern family_info **user_font_families;
175 extern int num_font_families;
176 
177 extern style_info **user_font_styles;
178 extern int num_font_styles;
179 extern int num_font_styles_max;
180 
181 extern int button_label_y_nudge;
182 
183 TuxPaint_Font *getfonthandle(int desire);
184 
185 int charset_works(TuxPaint_Font * font, const char *s);
186 
187 TuxPaint_Font *TuxPaint_Font_OpenFont(const char *pangodesc, const char *ttffilename, int size);
188 void TuxPaint_Font_CloseFont(TuxPaint_Font * tpf);
189 const char *TuxPaint_Font_FontFaceFamilyName(TuxPaint_Font * tpf);
190 const char *TuxPaint_Font_FontFaceStyleName(TuxPaint_Font * tpf);
191 
192 #ifdef NO_SDLPANGO
193 TuxPaint_Font *load_locale_font(TuxPaint_Font * fallback, int size);
194 #else
195 void sdl_color_to_pango_color(SDL_Color sdl_color, SDLPango_Matrix * pango_color);
196 #endif
197 
198 int load_user_fonts(SDL_Surface * screen, void *vp, const char *restrict const locale);
199 
200 #endif
201