vg_lite_text.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /****************************************************************************
  2. *
  3. * The MIT License (MIT)
  4. *
  5. * Copyright 2020 NXP
  6. * All Rights Reserved.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining
  9. * a copy of this software and associated documentation files (the
  10. * 'Software'), to deal in the Software without restriction, including
  11. * without limitation the rights to use, copy, modify, merge, publish,
  12. * distribute, sub license, and/or sell copies of the Software, and to
  13. * permit persons to whom the Software is furnished to do so, subject
  14. * to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice (including the
  17. * next paragraph) shall be included in all copies or substantial
  18. * portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  21. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  23. * IN NO EVENT SHALL VIVANTE AND/OR ITS SUPPLIERS BE LIABLE FOR ANY
  24. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  25. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  26. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. *
  28. *****************************************************************************/
  29. /** Include Files */
  30. #include "vg_lite.h"
  31. #include "vg_lite_text.h"
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <mcufont.h>
  35. #include "vft_draw.h"
  36. /** Macros */
  37. /** Data structures */
  38. typedef struct {
  39. /* Font related parameters */
  40. vg_lite_font_attributes_t *attributes;
  41. vg_lite_buffer_t buffer;
  42. uint16_t width;
  43. uint16_t height;
  44. uint16_t y;
  45. const struct mf_font_s *rcd_font;
  46. } text_context_t;
  47. /** Internal or external API prototypes */
  48. struct mf_font_s *_vg_lite_get_raster_font(vg_lite_font_t font_idx);
  49. int vg_lite_is_font_valid(vg_lite_font_t font);
  50. /** Globals */
  51. vg_lite_font_attributes_t g_font_attribs;
  52. vg_lite_font_t g_last_font = VG_LITE_INVALID_FONT;
  53. int g_last_font_attrib_idx;
  54. /** Externs if any */
  55. /* Capture unique values of alpha */
  56. static unsigned int g_index_table[257];
  57. /* text_color is in ARGB8888 format */
  58. int init_256pallet_color_table(unsigned int bg_color, unsigned int fg_color)
  59. {
  60. int i;
  61. g_index_table[0] = 0; /* Background color */
  62. int fg_r = ((fg_color>>0)&0xff);
  63. int fg_g = ((fg_color>>8)&0xff);
  64. int fg_b = ((fg_color>>16)&0xff);
  65. int bg_r = ((bg_color>>0)&0xff);
  66. int bg_g = ((bg_color>>8)&0xff);
  67. int bg_b = ((bg_color>>16)&0xff);
  68. for (i=1; i<256; i++) {
  69. int r, g, b;
  70. int a = 0xff;
  71. register int mult, mult2;
  72. mult = ((i*1024)/256);
  73. mult2 = (((256-i)*1024)/256);
  74. /* Blend with white bg */
  75. r = ( ((bg_r * mult2)>>10) +((fg_r * mult)>>10) );
  76. g = ( ((bg_g * mult2)>>10) +((fg_g * mult)>>10) );
  77. b = ( ((bg_b * mult2)>>10) +((fg_b * mult)>>10) );
  78. g_index_table[i] = ((a) + (r<<8) + (g<<16)+(b<<24));
  79. }
  80. return 0;
  81. }
  82. /* Callback to write to a memory buffer. */
  83. static void pixel_callback(int16_t x, int16_t y, uint8_t count, uint8_t alpha,
  84. void *state)
  85. {
  86. text_context_t *s = (text_context_t*)state;
  87. uint32_t pos;
  88. uint32_t value;
  89. uint32_t *raw_pixel_buffer = (uint32_t *)s->buffer.memory;
  90. int stride = s->buffer.stride;
  91. if (y < 0 || y >= s->height) return;
  92. if (x < 0 || x + count >= s->width) return;
  93. while (count--)
  94. {
  95. pos = (uint32_t)stride * y + x;
  96. value = g_index_table[alpha];
  97. raw_pixel_buffer[pos] = value;
  98. x++;
  99. }
  100. }
  101. /* Callback to render characters. */
  102. static uint8_t character_callback(int16_t x, int16_t y, mf_char character,
  103. void *state)
  104. {
  105. text_context_t *s = (text_context_t*)state;
  106. return mf_render_character(s->rcd_font, x, y, character, pixel_callback, state);
  107. }
  108. /* Callback to render lines. */
  109. static bool line_callback(const char *line, uint16_t count, void *state)
  110. {
  111. text_context_t *s = (text_context_t*)state;
  112. if (s->attributes->justify)
  113. {
  114. mf_render_justified(s->rcd_font, s->attributes->anchor, s->y,
  115. s->width - s->attributes->margin * 2,
  116. line, count, character_callback, state);
  117. }
  118. else
  119. {
  120. mf_render_aligned(s->rcd_font, s->attributes->anchor, s->y,
  121. (enum mf_align_t)s->attributes->alignment, line, count,
  122. character_callback, state);
  123. }
  124. s->y += s->rcd_font->line_height;
  125. return true;
  126. }
  127. /* Callback to just count the lines.
  128. * Used to decide the image height */
  129. bool count_lines(const char *line, uint16_t count, void *state)
  130. {
  131. int *linecount = (int*)state;
  132. (*linecount)++;
  133. return true;
  134. }
  135. vg_lite_error_t alloc_font_buffer(vg_lite_buffer_t *buffer, int width , int height)
  136. {
  137. vg_lite_error_t error;
  138. /* Align width to 16 pixels, heigh to 16 pixels */
  139. width = ((width+15)&(~15));
  140. height = ((height+15)&(~15));
  141. /* Allocate memory from VGLITE space */
  142. buffer->width = width;
  143. buffer->height = height;
  144. buffer->format = VG_LITE_ARGB8888;
  145. buffer->stride = 0;
  146. error = vg_lite_allocate(buffer);
  147. buffer->stride = width;
  148. buffer->tiled = VG_LITE_LINEAR;
  149. return error;
  150. }
  151. static vg_lite_error_t free_font_buffer(vg_lite_buffer_t *buffer)
  152. {
  153. vg_lite_error_t error;
  154. error = vg_lite_free(buffer);
  155. return error;
  156. }
  157. void matrix_multiply(vg_lite_matrix_t * matrix, vg_lite_matrix_t * mult)
  158. {
  159. vg_lite_matrix_t temp;
  160. int row, column;
  161. /* Process all rows. */
  162. for (row = 0; row < 3; row++) {
  163. /* Process all columns. */
  164. for (column = 0; column < 3; column++) {
  165. /* Compute matrix entry. */
  166. temp.m[row][column] = (matrix->m[row][0] * mult->m[0][column])
  167. + (matrix->m[row][1] * mult->m[1][column])
  168. + (matrix->m[row][2] * mult->m[2][column]);
  169. }
  170. }
  171. /* Copy temporary matrix into result. */
  172. memcpy(matrix, &temp, sizeof(temp));
  173. }
  174. vg_lite_error_t vg_lite_draw_text(vg_lite_buffer_t *target,
  175. char *text,
  176. vg_lite_font_t font,
  177. int x,
  178. int y,
  179. vg_lite_matrix_t *matrix,
  180. vg_lite_blend_t blend,
  181. vg_lite_font_attributes_t *attributes)
  182. {
  183. vg_lite_error_t error;
  184. int height;
  185. text_context_t ctx_text;
  186. vg_lite_matrix_t m_text;
  187. int text_img_size = 0;
  188. font_face_desc_t* font_face = NULL;
  189. int text_width_in_pixels = 0;
  190. int tmpX;
  191. memset(&ctx_text, 0, sizeof(ctx_text));
  192. ctx_text.attributes = attributes;
  193. if ( vg_lite_is_font_valid(font) != 0 ) {
  194. return VG_LITE_INVALID_ARGUMENT;
  195. }
  196. error = vg_lite_load_font_data(font,
  197. attributes->font_height);
  198. if ( error != 0 ) {
  199. return VG_LITE_INVALID_ARGUMENT;
  200. }
  201. if(attributes->tspan_has_dx_dy != 0)
  202. {
  203. if ( x < 0 )
  204. x = attributes->last_x + attributes->last_dx;
  205. if ( y < 0 )
  206. y = attributes->last_y;
  207. }
  208. // Dynamic decision
  209. if ( attributes->is_vector_font == 0 ) {
  210. init_256pallet_color_table(attributes->bg_color, attributes->text_color);
  211. /* Application specifies actual font by reading proper rcd file */
  212. ctx_text.rcd_font = _vg_lite_get_raster_font(font);
  213. /* Count number of lines to decide font buffer size. */
  214. height = 0;
  215. mf_text_draw_area(ctx_text.rcd_font, attributes->width - 2 * attributes->margin,
  216. text, &height, &text_width_in_pixels);
  217. height *= attributes->font_height;
  218. height += 4;
  219. if( attributes->tspan_has_dx_dy == 0) {
  220. y -= attributes->font_height;
  221. }
  222. tmpX = x;
  223. if(attributes->alignment == eTextAlignCenter) {
  224. tmpX -= text_width_in_pixels/2;
  225. } else if(attributes->alignment == eTextAlignRight) {
  226. tmpX -= text_width_in_pixels;
  227. }
  228. /* Manually calculate X-offset for text-alignemnt; mcufont just
  229. * doesn't render half part(left-part) for center alignment and
  230. * full string skipped for right-alignment, So again set it to
  231. * left-alignment for font-attrib, as offsets are already
  232. * calculated and alignment handled in other way */
  233. attributes->alignment = 0;
  234. /* Allocate and initialize vg_lite_buffer that can hold font text
  235. * Note: ctx_text.width and ctx_text.height get used by internal
  236. * state of MF rendering engine. Based on amount of text to render
  237. * This buffer gets allocated, and buffer width gets aligned to
  238. * 16 pixel boundary
  239. */
  240. ctx_text.width = attributes->width;
  241. /* Memory optimization */
  242. ctx_text.width = text_width_in_pixels;
  243. /* Align width to 16 pixel boundary */
  244. if (ctx_text.width & 15) {
  245. ctx_text.width += 15;
  246. ctx_text.width &= (~15);
  247. }
  248. ctx_text.height = height;
  249. error = alloc_font_buffer(&ctx_text.buffer, ctx_text.width, ctx_text.height);
  250. if ( error != VG_LITE_SUCCESS) {
  251. printf("WARNING: alloc_font_buffer failed(%d).\r\n",error);
  252. }
  253. ctx_text.y = 2;
  254. /* Initialize vg_lite buffer with transperant color */
  255. /* Due to alignment requirement of vg_lite, font buffer can be larger */
  256. if ( ctx_text.buffer.format == VG_LITE_ARGB8888 )
  257. text_img_size = ctx_text.buffer.width * ctx_text.buffer.height * 4;
  258. else
  259. text_img_size = ctx_text.buffer.width * ctx_text.buffer.height;
  260. memset(ctx_text.buffer.memory, 0,
  261. text_img_size);
  262. /* Render font text into vg_lite_buffer */
  263. mf_wordwrap(ctx_text.rcd_font, attributes->width - 2 * attributes->margin,
  264. text, line_callback, &ctx_text);
  265. /* Draw font bitmap on render target */
  266. vg_lite_identity(&m_text);
  267. matrix_multiply(&m_text, matrix);
  268. vg_lite_translate(x, y, &m_text);
  269. vg_lite_scale(1.0, 1.0, &m_text);
  270. if ( ctx_text.buffer.format == VG_LITE_ARGB8888 )
  271. ctx_text.buffer.stride = ctx_text.width*4;
  272. error = vg_lite_blit(target, &ctx_text.buffer, &m_text, blend,
  273. 0, VG_LITE_FILTER_POINT);
  274. if ( error != VG_LITE_SUCCESS) {
  275. printf("WARNING: vg_lite_blit failed(%d).\r\n",error);
  276. }
  277. error = vg_lite_finish();
  278. if ( error != VG_LITE_SUCCESS) {
  279. printf("WARNING: vg_lite_finish failed(%d).\r\n",error);
  280. }
  281. error = free_font_buffer(&ctx_text.buffer);
  282. if ( error != VG_LITE_SUCCESS) {
  283. printf("WARNING: vg_lite_finish failed(%d).\r\n",error);
  284. }
  285. attributes->last_dx = text_width_in_pixels;
  286. } else {
  287. error = (vg_lite_error_t)vg_lite_vtf_draw_text(target,
  288. x, y,
  289. blend,
  290. font,
  291. matrix,
  292. attributes,
  293. text);
  294. /* Note: vg_lite_vtf_draw_text updates attributes->last_dx internally
  295. This assignment is just to keep code similar to rcd code */
  296. attributes->last_dx = attributes->last_dx;
  297. error = vg_lite_finish();
  298. if ( error != VG_LITE_SUCCESS) {
  299. printf("WARNING: vg_lite_finish failed(%d).\r\n",error);
  300. }
  301. vft_unload(font_face);
  302. }
  303. attributes->last_x = x;
  304. attributes->last_y = y;
  305. return error;
  306. }