aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/evas/src/lib/engines/common_16/evas_soft16_font.c
blob: fbad4d261c8a371fdb22c2d16991eb1039461c90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include "evas_common_soft16.h"

EFL_ALWAYS_INLINE void
_glyph_pt_mask_solid_solid(DATA16 *dst,
			   const DATA16 rgb565,
			   const DATA32 rgb565_unpack,
			   const DATA8 *mask)
{
   DATA8 alpha = *mask >> 3;

   if (alpha == 31) *dst = rgb565;
   else if (alpha > 0)
     {
	DATA32 d;

	d = RGB_565_UNPACK(*dst);
	d = RGB_565_UNPACKED_BLEND_UNMUL(rgb565_unpack, d, alpha);
	*dst = RGB_565_PACK(d);
     }
}

static void
_glyph_scanline_mask_solid_solid(DATA16 *dst,
				 int size,
				 const DATA16 rgb565,
				 const DATA32 rgb565_unpack,
				 const DATA8 *mask)
{
   DATA16 *start, *end;

   start = dst;
   pld(start, 0);
   pld(mask, 0);
   end = start + (size & ~3);

   while (start < end)
     {
	pld(start, 16);
	pld(mask, 4);
	UNROLL4({
	   _glyph_pt_mask_solid_solid(start, rgb565, rgb565_unpack, mask);
	   start++;
	   mask++;
	});
     }

   end = start + (size & 3);
   for (; start < end; start++, mask++)
      _glyph_pt_mask_solid_solid(start, rgb565, rgb565_unpack, mask);
}

EFL_ALWAYS_INLINE void
_glyph_pt_mask_transp_solid(DATA16 *dst,
			    DATA32 rgb565_unpack,
			    DATA8 alpha,
			    const DATA8 *mask)
{
   DATA32 a, b;
   int rel_alpha;

   rel_alpha = *mask >> 3;
   alpha = (alpha * rel_alpha) >> 5;
   if (alpha == 0)
     return;

   alpha++;

   a = ((rgb565_unpack * rel_alpha) >> 5) & RGB_565_UNPACKED_MASK;
   b = RGB_565_UNPACK(*dst);
   b = RGB_565_UNPACKED_BLEND(a, b, alpha);
   *dst = RGB_565_PACK(b);
}

static void
_glyph_scanline_mask_transp_solid(DATA16 *dst,
				  int size,
				  const DATA32 rgb565_unpack,
				  const DATA8 rel_alpha,
				  const DATA8 *mask)
{
   DATA16 *start, *end;

   start = dst;
   pld(start, 0);
   pld(mask, 0);
   end = start + (size & ~3);

   while (start < end)
     {
	pld(start, 16);
	pld(mask, 4);
	UNROLL4({
	   _glyph_pt_mask_transp_solid(start, rgb565_unpack, rel_alpha, mask);
	   start++;
	   mask++;
	});
     }

   end = start + (size & 3);
   for (; start < end; start++, mask++)
      _glyph_pt_mask_transp_solid(start, rgb565_unpack, rel_alpha, mask);
}

static inline void
_calc_ext(const Soft16_Image *dst, const RGBA_Draw_Context *dc,
	  Eina_Rectangle *ext)
{
   EINA_RECTANGLE_SET(ext, 0, 0, dst->cache_entry.w, dst->cache_entry.h);

   if (dc->clip.use)
     {
	int v;

	EINA_RECTANGLE_SET(ext, dc->clip.x, dc->clip.y, dc->clip.w, dc->clip.h);
	if (ext->x < 0)
	  {
	     ext->w += ext->x;
	     ext->x = 0;
	  }
	if (ext->y < 0)
	  {
	     ext->h += ext->y;
	     ext->y = 0;
	  }

	v = dst->cache_entry.w - ext->x;
	if (ext->w > v) ext->w = v;

	v = dst->cache_entry.h - ext->y;
	if (ext->h > v) ext->h = v;
     }
}

static inline void
_glyph_scanline(Soft16_Image *dst, const DATA8 *p_mask,
		const Eina_Rectangle ext, int dx, int dy, int max_x, int max_y,
		int w, DATA8 alpha, const DATA16 rgb565,
		const DATA32 rgb565_unpack)
{
   int size, in_x, in_w;
   DATA16 *p_pixels;

   if ((dx >= max_x) || (dy < ext.y) || (dy >= max_y)) return;

   in_x = 0;
   in_w = 0;

   if (dx + w > max_x) in_w += (dx + w) - max_x;

   if (dx < ext.x)
     {
	in_w += ext.x - dx;
	in_x = ext.x - dx;
	dx = ext.x;
     }

   size = w - in_w;
   p_pixels = dst->pixels + (dy * dst->stride) + dx;
   p_mask += in_x;

   if (size > 1)
     {
	if (alpha == 31)
	   _glyph_scanline_mask_solid_solid
	       (p_pixels, size, rgb565, rgb565_unpack, p_mask);
	else if (alpha != 0)
	   _glyph_scanline_mask_transp_solid
	       (p_pixels, size, rgb565_unpack, alpha, p_mask);
     }
   else if (size == 1)
     {
	if (alpha == 31)
	   _glyph_pt_mask_solid_solid(p_pixels, rgb565, rgb565_unpack, p_mask);
	else if (alpha != 0)
	   _glyph_pt_mask_transp_solid(p_pixels, rgb565_unpack, alpha, p_mask);
     }
}

static void
_soft16_font_glyph_draw_grayscale(Soft16_Image *dst,
				  RGBA_Draw_Context *dc __UNUSED__, RGBA_Font_Glyph *fg __UNUSED__,
				  int x, int y, DATA8 alpha, DATA16 rgb565,
				  const Eina_Rectangle ext, int bw, int bh,
				  int bpitch, const DATA8 *bitmap)
{
   const DATA32 rgb565_unpack = RGB_565_UNPACK(rgb565);
   int i, max_x, max_y;

   max_x = ext.x + ext.w;
   max_y = ext.y + ext.h;

   for (i = 0; i < bh; i++, bitmap += bpitch)
      _glyph_scanline(dst, bitmap, ext, x, y + i, max_x, max_y, bw,
		      alpha, rgb565, rgb565_unpack);
}

static inline void
_glyph_create_mask_line(DATA8 *mask, const DATA8 *bitmap, int w)
{
   const DATA8 bitrepl[2] = {0x0, 0xff};
   int i;

   for (i = 0; i < w; i += 8, bitmap++)
     {
	int j, size;
	DATA32 bits;

	if (i + 8 < w) size = 8;
	else           size = w - i;

	bits = *bitmap;

	for (j = size - 1; j >= 0; j--, mask++)
	  *mask = bitrepl[(bits >> j) & 0x1];
     }
}

static void
_soft16_font_glyph_draw_mono(Soft16_Image *dst,
			     RGBA_Draw_Context *dc __UNUSED__, RGBA_Font_Glyph *fg __UNUSED__,
			     int x, int y, DATA8 alpha, DATA16 rgb565,
			     const Eina_Rectangle ext, int bw, int bh,
			     int bpitch, const DATA8 *bitmap)
{
   const DATA32 rgb565_unpack = RGB_565_UNPACK(rgb565);
   DATA8 *mask;
   int i, max_x, max_y;

   max_x = ext.x + ext.w;
   max_y = ext.y + ext.h;

   mask = alloca(bpitch);
   for (i = 0; i < bh; i++, bitmap += bpitch)
     {
	_glyph_create_mask_line(mask, bitmap, bw);
	_glyph_scanline(dst, mask, ext, x, y + i, max_x, max_y, bw,
			alpha, rgb565, rgb565_unpack);
     }
}

void
evas_common_soft16_font_glyph_draw(void *data, void *dest __UNUSED__, void *context,
		       RGBA_Font_Glyph *fg, int x, int y)
{
   Soft16_Image *dst;
   RGBA_Draw_Context *dc;
   const DATA8 *bitmap;
   DATA8 alpha, r, g, b;
   DATA16 rgb565;
   Eina_Rectangle ext;
   int bpitch, bw, bh;

   dst = data;
   dc = context;

   alpha = A_VAL(&dc->col.col) >> 3;
   if (alpha == 0) return; /* precision is 5 bits, 3 bits lost */

   r = R_VAL(&dc->col.col) >> 3;
   g = G_VAL(&dc->col.col) >> 2;
   b = B_VAL(&dc->col.col) >> 3;

   if (r > alpha) r = alpha;
   if (g > (alpha << 1)) g = (alpha << 1);
   if (b > alpha) b = alpha;

   rgb565 = (r << 11) | (g << 5) | b;

   bitmap = fg->glyph_out->bitmap.buffer;
   bh = fg->glyph_out->bitmap.rows;
   bw = fg->glyph_out->bitmap.width;
   bpitch = fg->glyph_out->bitmap.pitch;
   if (bpitch < bw) bpitch = bw;

   _calc_ext(dst, dc, &ext);

   if ((fg->glyph_out->bitmap.num_grays == 256) &&
       (fg->glyph_out->bitmap.pixel_mode == ft_pixel_mode_grays))
      _soft16_font_glyph_draw_grayscale(dst, dc, fg, x, y, alpha, rgb565,
					ext, bw, bh, bpitch, bitmap);
   else
      _soft16_font_glyph_draw_mono(dst, dc, fg, x, y, alpha, rgb565,
				   ext, bw, bh, bpitch, bitmap);
}

void *
evas_common_soft16_font_glyph_new(void *data __UNUSED__, RGBA_Font_Glyph *fg __UNUSED__)
{
   return (void *)1; /* core requires != NULL to work */
}

void
evas_common_soft16_font_glyph_free(void *ext_dat __UNUSED__)
{
}