1 /*  RetroArch - A frontend for libretro.
2  *  Copyright (C) 2010-2014 - Hans-Kristian Arntzen
3  *  Copyright (C) 2011-2017 - Daniel De Matteis
4  *
5  *  RetroArch is free software: you can redistribute it and/or modify it under the terms
6  *  of the GNU General Public License as published by the Free Software Found-
7  *  ation, either version 3 of the License, or (at your option) any later version.
8  *
9  *  RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10  *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11  *  PURPOSE.  See the GNU General Public License for more details.
12  *
13  *  You should have received a copy of the GNU General Public License along with RetroArch.
14  *  If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #include <vita2d.h>
18 
19 #include <encodings/utf.h>
20 
21 #include "../common/vita2d_common.h"
22 
23 #include "../font_driver.h"
24 
25 #include "../../configuration.h"
26 #include "../../verbosity.h"
27 
28 typedef struct
29 {
30    vita_video_t *vita;
31    vita2d_texture *texture;
32    const font_renderer_driver_t *font_driver;
33    void *font_data;
34    struct font_atlas *atlas;
35 } vita_font_t;
36 
vita2d_font_init_font(void * data,const char * font_path,float font_size,bool is_threaded)37 static void *vita2d_font_init_font(void *data,
38       const char *font_path, float font_size,
39       bool is_threaded)
40 {
41    unsigned int stride, pitch, j, k;
42    const uint8_t         *frame32 = NULL;
43    uint8_t                 *tex32 = NULL;
44    const struct font_atlas *atlas = NULL;
45    vita_font_t              *font = (vita_font_t*)calloc(1, sizeof(*font));
46 
47    if (!font)
48       return NULL;
49 
50    font->vita                     = (vita_video_t*)data;
51 
52    if (!font_renderer_create_default(
53             &font->font_driver,
54             &font->font_data, font_path, font_size))
55       goto error;
56 
57    font->atlas   = font->font_driver->get_atlas(font->font_data);
58    atlas         = font->atlas;
59 
60    if (!atlas)
61       goto error;
62 
63    font->texture = vita2d_create_empty_texture_format(
64          atlas->width,
65          atlas->height,
66          SCE_GXM_TEXTURE_FORMAT_U8_R111);
67 
68    if (!font->texture)
69       goto error;
70 
71    vita2d_texture_set_filters(font->texture,
72          SCE_GXM_TEXTURE_FILTER_POINT,
73          SCE_GXM_TEXTURE_FILTER_LINEAR);
74 
75    stride  = vita2d_texture_get_stride(font->texture);
76    tex32   = vita2d_texture_get_datap(font->texture);
77    frame32 = atlas->buffer;
78    pitch   = atlas->width;
79 
80    for (j = 0; j < atlas->height; j++)
81       for (k = 0; k < atlas->width; k++)
82          tex32[k + j*stride] = frame32[k + j*pitch];
83 
84    font->atlas->dirty = false;
85 
86    return font;
87 
88 error:
89    RARCH_WARN("Couldn't initialize font renderer.\n");
90    free(font);
91    return NULL;
92 }
93 
vita2d_font_free_font(void * data,bool is_threaded)94 static void vita2d_font_free_font(void *data, bool is_threaded)
95 {
96    vita_font_t *font = (vita_font_t*)data;
97    if (!font)
98       return;
99 
100    if (font->font_driver && font->font_data)
101       font->font_driver->free(font->font_data);
102 
103    vita2d_wait_rendering_done();
104    vita2d_free_texture(font->texture);
105 
106    free(font);
107 }
108 
vita2d_font_get_message_width(void * data,const char * msg,unsigned msg_len,float scale)109 static int vita2d_font_get_message_width(void *data, const char *msg,
110       unsigned msg_len, float scale)
111 {
112    unsigned i;
113    int delta_x       = 0;
114    vita_font_t *font = (vita_font_t*)data;
115 
116    if (!font)
117       return 0;
118 
119    for (i = 0; i < msg_len; i++)
120    {
121       const struct font_glyph *glyph = NULL;
122       const char *msg_tmp            = &msg[i];
123       unsigned code                  = utf8_walk(&msg_tmp);
124       unsigned skip                  = msg_tmp - &msg[i];
125 
126       if (skip > 1)
127          i += skip - 1;
128 
129       glyph = font->font_driver->get_glyph(font->font_data, code);
130 
131       if (!glyph) /* Do something smarter here ... */
132          glyph = font->font_driver->get_glyph(font->font_data, '?');
133 
134       if (!glyph)
135          continue;
136 
137       delta_x += glyph->advance_x;
138    }
139 
140    return delta_x * scale;
141 }
142 
vita2d_font_render_line(vita_font_t * font,const char * msg,unsigned msg_len,float scale,const unsigned int color,float pos_x,float pos_y,unsigned width,unsigned height,unsigned text_align)143 static void vita2d_font_render_line(
144       vita_font_t *font, const char *msg, unsigned msg_len,
145       float scale, const unsigned int color, float pos_x,
146       float pos_y,
147       unsigned width, unsigned height, unsigned text_align)
148 {
149    unsigned i;
150    int x           = roundf(pos_x * width);
151    int y           = roundf((1.0f - pos_y) * height);
152    int delta_x     = 0;
153    int delta_y     = 0;
154 
155    switch (text_align)
156    {
157       case TEXT_ALIGN_RIGHT:
158          x -= vita2d_font_get_message_width(font, msg, msg_len, scale);
159          break;
160       case TEXT_ALIGN_CENTER:
161          x -= vita2d_font_get_message_width(font, msg, msg_len, scale) / 2;
162          break;
163    }
164 
165    for (i = 0; i < msg_len; i++)
166    {
167       int off_x, off_y, tex_x, tex_y, width, height;
168       unsigned int stride, pitch, j, k;
169       const struct font_glyph *glyph = NULL;
170       const uint8_t         *frame32 = NULL;
171       uint8_t                 *tex32 = NULL;
172       const char *msg_tmp            = &msg[i];
173       unsigned code                  = utf8_walk(&msg_tmp);
174       unsigned skip                  = msg_tmp - &msg[i];
175 
176       if (skip > 1)
177          i += skip - 1;
178 
179       glyph = font->font_driver->get_glyph(font->font_data, code);
180 
181       if (!glyph) /* Do something smarter here ... */
182          glyph = font->font_driver->get_glyph(font->font_data, '?');
183 
184       if (!glyph)
185          continue;
186 
187       off_x  = glyph->draw_offset_x;
188       off_y  = glyph->draw_offset_y;
189       tex_x  = glyph->atlas_offset_x;
190       tex_y  = glyph->atlas_offset_y;
191       width  = glyph->width;
192       height = glyph->height;
193 
194       if (font->atlas->dirty)
195       {
196         stride  = vita2d_texture_get_stride(font->texture);
197         tex32   = vita2d_texture_get_datap(font->texture);
198         frame32 = font->atlas->buffer;
199         pitch   = font->atlas->width;
200 
201         for (j = 0; j < font->atlas->height; j++)
202            for (k = 0; k < font->atlas->width; k++)
203               tex32[k + j*stride] = frame32[k + j*pitch];
204 
205          font->atlas->dirty = false;
206       }
207 
208       vita2d_draw_texture_tint_part_scale(font->texture,
209             x + (off_x + delta_x) * scale,
210             y + (off_y + delta_y) * scale,
211             tex_x, tex_y, width, height,
212             scale,
213             scale,
214             color);
215 
216       delta_x += glyph->advance_x;
217       delta_y += glyph->advance_y;
218    }
219 }
220 
vita2d_font_render_message(vita_font_t * font,const char * msg,float scale,const unsigned int color,float pos_x,float pos_y,unsigned width,unsigned height,unsigned text_align)221 static void vita2d_font_render_message(
222       vita_font_t *font, const char *msg, float scale,
223       const unsigned int color, float pos_x, float pos_y,
224       unsigned width, unsigned height, unsigned text_align)
225 {
226    struct font_line_metrics *line_metrics = NULL;
227    int lines                              = 0;
228    float line_height;
229 
230    if (!msg || !*msg)
231       return;
232 
233    /* If font line metrics are not supported just draw as usual */
234    if (!font->font_driver->get_line_metrics ||
235        !font->font_driver->get_line_metrics(font->font_data, &line_metrics))
236    {
237       vita2d_font_render_line(font, msg, strlen(msg),
238             scale, color, pos_x, pos_y, width, height, text_align);
239       return;
240    }
241 
242    line_height = line_metrics->height * scale / font->vita->vp.height;
243 
244    for (;;)
245    {
246       const char *delim = strchr(msg, '\n');
247       unsigned msg_len  = (delim) ?
248          (unsigned)(delim - msg) : strlen(msg);
249 
250       /* Draw the line */
251       vita2d_font_render_line(font, msg, msg_len,
252             scale, color, pos_x, pos_y - (float)lines * line_height,
253             width, height, text_align);
254 
255       if (!delim)
256          break;
257 
258       msg += msg_len + 1;
259       lines++;
260    }
261 }
262 
vita2d_font_render_msg(void * userdata,void * data,const char * msg,const struct font_params * params)263 static void vita2d_font_render_msg(
264       void *userdata,
265       void *data,
266       const char *msg,
267       const struct font_params *params)
268 {
269    float x, y, scale, drop_mod, drop_alpha;
270    int drop_x, drop_y;
271    unsigned max_glyphs;
272    enum text_alignment text_align;
273    bool full_screen                 = false ;
274    unsigned color, color_dark, r, g, b,
275             alpha, r_dark, g_dark, b_dark, alpha_dark;
276    vita_video_t             *vita   = (vita_video_t *)userdata;
277    vita_font_t                *font = (vita_font_t *)data;
278    unsigned width                   = vita->video_width;
279    unsigned height                  = vita->video_height;
280    settings_t *settings             = config_get_ptr();
281    float video_msg_pos_x            = settings->floats.video_msg_pos_x;
282    float video_msg_pos_y            = settings->floats.video_msg_pos_y;
283    float video_msg_color_r          = settings->floats.video_msg_color_r;
284    float video_msg_color_g          = settings->floats.video_msg_color_g;
285    float video_msg_color_b          = settings->floats.video_msg_color_b;
286 
287    if (!font || !msg || !*msg)
288       return;
289 
290    if (params)
291    {
292       x              = params->x;
293       y              = params->y;
294       scale          = params->scale;
295       full_screen    = params->full_screen;
296       text_align     = params->text_align;
297       drop_x         = params->drop_x;
298       drop_y         = params->drop_y;
299       drop_mod       = params->drop_mod;
300       drop_alpha     = params->drop_alpha;
301       r    				= FONT_COLOR_GET_RED(params->color);
302       g    				= FONT_COLOR_GET_GREEN(params->color);
303       b    				= FONT_COLOR_GET_BLUE(params->color);
304       alpha    		= FONT_COLOR_GET_ALPHA(params->color);
305       color    		= RGBA8(r,g,b,alpha);
306    }
307    else
308    {
309       x              = video_msg_pos_x;
310       y              = video_msg_pos_y;
311       scale          = 1.0f;
312       full_screen    = true;
313       text_align     = TEXT_ALIGN_LEFT;
314 
315       r              = (video_msg_color_r * 255);
316       g              = (video_msg_color_g * 255);
317       b              = (video_msg_color_b * 255);
318       alpha			   = 255;
319       color 		   = RGBA8(r,g,b,alpha);
320 
321       drop_x         = -2;
322       drop_y         = -2;
323       drop_mod       = 0.3f;
324       drop_alpha     = 1.0f;
325    }
326 
327    video_driver_set_viewport(width, height, full_screen, false);
328 
329    max_glyphs        = strlen(msg);
330 
331    if (drop_x || drop_y)
332       max_glyphs    *= 2;
333 
334    if (drop_x || drop_y)
335    {
336       r_dark         = r * drop_mod;
337       g_dark         = g * drop_mod;
338       b_dark         = b * drop_mod;
339       alpha_dark		= alpha * drop_alpha;
340       color_dark     = RGBA8(r_dark,g_dark,b_dark,alpha_dark);
341 
342       vita2d_font_render_message(font, msg, scale, color_dark,
343             x + scale * drop_x / width, y +
344             scale * drop_y / height, width, height, text_align);
345    }
346 
347    vita2d_font_render_message(font, msg, scale,
348          color, x, y, width, height, text_align);
349 }
350 
vita2d_font_get_glyph(void * data,uint32_t code)351 static const struct font_glyph *vita2d_font_get_glyph(
352       void *data, uint32_t code)
353 {
354    vita_font_t *font = (vita_font_t*)data;
355 
356    if (!font || !font->font_driver || !font->font_driver->ident)
357        return NULL;
358    return font->font_driver->get_glyph((void*)font->font_driver, code);
359 }
360 
vita2d_font_get_line_metrics(void * data,struct font_line_metrics ** metrics)361 static bool vita2d_font_get_line_metrics(void* data, struct font_line_metrics **metrics)
362 {
363    vita_font_t *font = (vita_font_t*)data;
364 
365    if (!font || !font->font_driver || !font->font_data)
366       return -1;
367 
368    return font->font_driver->get_line_metrics(font->font_data, metrics);
369 }
370 
371 font_renderer_t vita2d_vita_font = {
372    vita2d_font_init_font,
373    vita2d_font_free_font,
374    vita2d_font_render_msg,
375    "vita2dfont",
376    vita2d_font_get_glyph,
377    NULL,                      /* bind_block */
378    NULL,                      /* flush */
379    vita2d_font_get_message_width,
380    vita2d_font_get_line_metrics
381 };
382