forked from apache/nuttx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnxterm_font.c
391 lines (300 loc) · 10.8 KB
/
nxterm_font.c
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/****************************************************************************
* graphics/nxterm/nxterm_font.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/kmalloc.h>
#include "nxterm.h"
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: nxterm_fontsize
****************************************************************************/
static int nxterm_fontsize(FAR struct nxterm_state_s *priv, uint8_t ch,
FAR struct nxgl_size_s *size)
{
FAR const struct nx_fontbitmap_s *fbm;
NXHANDLE hfont;
/* Get the handle of the font managed by the font cache */
hfont = nxf_cache_getfonthandle(priv->fcache);
DEBUGASSERT(hfont != NULL);
/* Does the character code map to a font? */
fbm = nxf_getbitmap(hfont, ch);
if (fbm)
{
/* Yes.. return the font size */
size->w = fbm->metric.width + fbm->metric.xoffset;
size->h = fbm->metric.height + fbm->metric.yoffset;
return OK;
}
return -ENOENT;
}
/****************************************************************************
* Name: nxterm_fillspace
****************************************************************************/
static void nxterm_fillspace(FAR struct nxterm_state_s *priv,
FAR const struct nxgl_rect_s *rect,
FAR const struct nxterm_bitmap_s *bm)
{
#if 0 /* Not necessary now, but perhaps in the future with VT100 support. */
struct nxgl_rect_s bounds;
struct nxgl_rect_s intersection;
int ret;
/* Construct a bounding box for the glyph */
bounds.pt1.x = bm->pos.x;
bounds.pt1.y = bm->pos.y;
bounds.pt2.x = bm->pos.x + priv->spwidth - 1;
bounds.pt2.y = bm->pos.y + priv->fheight - 1;
# /* Should this also be clipped to a region in the window? */
if (rect != NULL)
{
/* Get the intersection of the redraw region and the character bitmap */
nxgl_rectintersect(&intersection, rect, &bounds);
}
else
{
/* The intersection is the whole glyph */
nxgl_rectcopy(&intersection, &bounds);
}
/* Check for empty intersections */
if (!nxgl_nullrect(&intersection))
{
/* Fill the bitmap region with the background color, erasing the
* character from the display. NOTE: This region might actually
* be obscured... NX will handle that case.
*/
ret = priv->ops->fill(priv, &intersection, priv->wndo.wcolor);
if (ret < 0)
{
gerr("ERROR: fill() method failed: %d\n", ret);
}
}
#endif
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: nxterm_addchar
*
* Description:
* This is part of the nxterm_putc logic. It creates and positions a
* the character and renders (or re-uses) a glyph for font.
*
****************************************************************************/
FAR const struct nxterm_bitmap_s *
nxterm_addchar(FAR struct nxterm_state_s *priv, uint8_t ch)
{
FAR struct nxterm_bitmap_s *bm = NULL;
FAR const struct nxfonts_glyph_s *glyph;
/* Is there space for another character on the display? */
if (priv->nchars < priv->maxchars)
{
/* Yes, setup the bitmap information */
bm = &priv->bm[priv->nchars];
bm->code = ch;
bm->flags = 0;
bm->pos.x = priv->fpos.x;
bm->pos.y = priv->fpos.y;
/* Find (or create) the matching glyph */
glyph = nxf_cache_getglyph(priv->fcache, ch);
if (!glyph)
{
/* No, there is no font for this code.
* Just mark this as a space.
*/
bm->flags |= BMFLAGS_NOGLYPH;
/* Set up the next character position */
priv->fpos.x += priv->spwidth;
}
else
{
/* Set up the next character position */
priv->fpos.x += glyph->width;
}
/* Success.. increment nchars to retain this character */
priv->nchars++;
}
return bm;
}
/****************************************************************************
* Name: nxterm_hidechar
*
* Description:
* Erase a character from the window.
*
****************************************************************************/
int nxterm_hidechar(FAR struct nxterm_state_s *priv,
FAR const struct nxterm_bitmap_s *bm)
{
struct nxgl_rect_s bounds;
struct nxgl_size_s fsize;
int ret;
/* Get the size of the font glyph. If nxterm_fontsize, then the
* character will have been rendered as a space, and no display
* modification is required (not an error).
*/
ret = nxterm_fontsize(priv, bm->code, &fsize);
if (ret < 0)
{
/* It was rendered as a space. */
return OK;
}
/* Construct a bounding box for the glyph */
bounds.pt1.x = bm->pos.x;
bounds.pt1.y = bm->pos.y;
bounds.pt2.x = bm->pos.x + fsize.w - 1;
bounds.pt2.y = bm->pos.y + fsize.h - 1;
/* Fill the bitmap region with the background color, erasing the
* character from the display. NOTE: This region might actually
* be obscured... NX will handle that case.
*/
return priv->ops->fill(priv, &bounds, priv->wndo.wcolor);
}
/****************************************************************************
* Name: nxterm_backspace
*
* Description:
* Remove the last character from the window.
*
****************************************************************************/
int nxterm_backspace(FAR struct nxterm_state_s *priv)
{
FAR struct nxterm_bitmap_s *bm;
int ndx;
int ret = -ENOENT;
/* Is there a character on the display? */
if (priv->nchars > 0)
{
/* Yes.. Get the index to the last bitmap on the display */
ndx = priv->nchars - 1;
bm = &priv->bm[ndx];
/* Erase the character from the display */
ret = nxterm_hidechar(priv, bm);
/* The current position to the location where the last character was */
priv->fpos.x = bm->pos.x;
priv->fpos.y = bm->pos.y;
/* Decrement nchars to discard this character */
priv->nchars = ndx;
}
return ret;
}
/****************************************************************************
* Name: nxterm_home
*
* Description:
* Set the next character position to the top-left corner of the display.
*
****************************************************************************/
void nxterm_home(FAR struct nxterm_state_s *priv)
{
/* The first character is one space from the left */
priv->fpos.x = priv->spwidth;
/* And CONFIG_NXTERM_LINESEPARATION lines from the top */
priv->fpos.y = CONFIG_NXTERM_LINESEPARATION;
}
/****************************************************************************
* Name: nxterm_newline
*
* Description:
* Set the next character position to the beginning of the next line.
*
****************************************************************************/
void nxterm_newline(FAR struct nxterm_state_s *priv)
{
/* Carriage return: The first character is one space from the left */
priv->fpos.x = priv->spwidth;
/* Linefeed: Down the max font height + CONFIG_NXTERM_LINESEPARATION */
priv->fpos.y += (priv->fheight + CONFIG_NXTERM_LINESEPARATION);
}
/****************************************************************************
* Name: nxterm_fillchar
*
* Description:
* This implements the character display. It is part of the nxterm_putc
* operation but may also be used when redrawing an existing display.
*
****************************************************************************/
void nxterm_fillchar(FAR struct nxterm_state_s *priv,
FAR const struct nxgl_rect_s *rect,
FAR const struct nxterm_bitmap_s *bm)
{
FAR const struct nxfonts_glyph_s *glyph;
struct nxgl_rect_s bounds;
struct nxgl_rect_s intersection;
struct nxgl_size_s fsize;
int ret;
/* Handle the special case of spaces which have no glyph bitmap */
if (BM_ISSPACE(bm))
{
nxterm_fillspace(priv, rect, bm);
return;
}
/* Get the size of the font glyph (which may not have been created yet) */
ret = nxterm_fontsize(priv, bm->code, &fsize);
if (ret < 0)
{
/* This would mean that there is no bitmap for the character code and
* that the font would be rendered as a space. But this case should
* never happen here because the BM_ISSPACE() should have already
* found all such cases.
*/
return;
}
/* Construct a bounding box for the glyph */
bounds.pt1.x = bm->pos.x;
bounds.pt1.y = bm->pos.y;
bounds.pt2.x = bm->pos.x + fsize.w - 1;
bounds.pt2.y = bm->pos.y + fsize.h - 1;
/* Should this also be clipped to a region in the window? */
if (rect != NULL)
{
/* Get the intersection of the redraw region and the character bitmap */
nxgl_rectintersect(&intersection, rect, &bounds);
}
else
{
/* The intersection is the whole glyph */
nxgl_rectcopy(&intersection, &bounds);
}
/* Check for empty intersections */
if (!nxgl_nullrect(&intersection))
{
FAR const void *src;
/* Find (or create) the glyph that goes with this font */
glyph = nxf_cache_getglyph(priv->fcache, bm->code);
if (!glyph)
{
/* Shouldn't happen */
return;
}
/* Blit the font bitmap into the window */
src = (FAR const void *)glyph->bitmap;
ret = priv->ops->bitmap(priv, &intersection, &src,
&bm->pos, (unsigned int)glyph->stride);
DEBUGASSERT(ret >= 0);
}
}