2 * Copyright (c) 2009 The FreeBSD Foundation
5 * This software was developed by Ed Schouten under sponsorship from the
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/refcount.h>
37 #include <sys/systm.h>
39 #include <dev/vt/vt.h>
41 static MALLOC_DEFINE(M_VTFONT, "vtfont", "vt font");
43 /* Some limits to prevent abnormal fonts from being loaded. */
44 #define VTFONT_MAXMAPPINGS 65536
45 #define VTFONT_MAXGLYPHS 131072
46 #define VTFONT_MAXGLYPHSIZE 2097152
47 #define VTFONT_MAXDIMENSION 128
50 vtfont_bisearch(const struct vt_font_map *map, unsigned int len, uint32_t src)
60 /* Character below minimal entry. */
61 if (src < map[0].vfm_src)
63 /* Optimization: ASCII characters occur very often. */
64 if (src <= map[0].vfm_src + map[0].vfm_len)
65 return (src - map[0].vfm_src + map[0].vfm_dst);
66 /* Character above maximum entry. */
67 if (src > map[max].vfm_src + map[max].vfm_len)
72 mid = (min + max) / 2;
73 if (src < map[mid].vfm_src)
75 else if (src > map[mid].vfm_src + map[mid].vfm_len)
78 return (src - map[mid].vfm_src + map[mid].vfm_dst);
85 vtfont_lookup(const struct vt_font *vf, term_char_t c)
90 unsigned int normal_map;
91 unsigned int bold_map;
94 * No support for printing right hand sides for CJK fullwidth
95 * characters. Simply print a space and assume that the left
96 * hand side describes the entire character.
98 src = TCHAR_CHARACTER(c);
99 if (TCHAR_FORMAT(c) & TF_CJK_RIGHT) {
100 normal_map = VFNT_MAP_NORMAL_RIGHT;
101 bold_map = VFNT_MAP_BOLD_RIGHT;
103 normal_map = VFNT_MAP_NORMAL;
104 bold_map = VFNT_MAP_BOLD;
107 if (TCHAR_FORMAT(c) & TF_BOLD) {
108 dst = vtfont_bisearch(vf->vf_map[bold_map],
109 vf->vf_map_count[bold_map], src);
113 dst = vtfont_bisearch(vf->vf_map[normal_map],
114 vf->vf_map_count[normal_map], src);
117 stride = howmany(vf->vf_width, 8) * vf->vf_height;
118 return (&vf->vf_bytes[dst * stride]);
122 vtfont_ref(struct vt_font *vf)
125 refcount_acquire(&vf->vf_refcount);
130 vtfont_unref(struct vt_font *vf)
134 if (refcount_release(&vf->vf_refcount)) {
135 for (i = 0; i < VFNT_MAPS; i++)
136 free(vf->vf_map[i], M_VTFONT);
137 free(vf->vf_bytes, M_VTFONT);
143 vtfont_validate_map(struct vt_font_map *vfm, unsigned int length,
144 unsigned int glyph_count)
146 unsigned int i, last = 0;
148 for (i = 0; i < length; i++) {
150 if (i > 0 && vfm[i].vfm_src <= last)
153 * Destination extends amount of glyphs.
155 if (vfm[i].vfm_dst >= glyph_count ||
156 vfm[i].vfm_dst + vfm[i].vfm_len >= glyph_count)
158 last = vfm[i].vfm_src + vfm[i].vfm_len;
165 vtfont_load(vfnt_t *f, struct vt_font **ret)
167 size_t glyphsize, mapsize;
172 /* Make sure the dimensions are valid. */
173 if (f->width < 1 || f->height < 1)
175 if (f->width > VTFONT_MAXDIMENSION || f->height > VTFONT_MAXDIMENSION ||
176 f->glyph_count > VTFONT_MAXGLYPHS)
179 /* Not too many mappings. */
180 for (i = 0; i < VFNT_MAPS; i++)
181 if (f->map_count[i] > VTFONT_MAXMAPPINGS)
184 /* Character 0 must always be present. */
185 if (f->glyph_count < 1)
188 glyphsize = howmany(f->width, 8) * f->height * f->glyph_count;
189 if (glyphsize > VTFONT_MAXGLYPHSIZE)
192 /* Allocate new font structure. */
193 vf = malloc(sizeof *vf, M_VTFONT, M_WAITOK | M_ZERO);
194 vf->vf_bytes = malloc(glyphsize, M_VTFONT, M_WAITOK);
195 vf->vf_height = f->height;
196 vf->vf_width = f->width;
199 /* Allocate, copy in, and validate mappings. */
200 for (i = 0; i < VFNT_MAPS; i++) {
201 vf->vf_map_count[i] = f->map_count[i];
202 if (f->map_count[i] == 0)
204 mapsize = f->map_count[i] * sizeof(struct vt_font_map);
205 vf->vf_map[i] = malloc(mapsize, M_VTFONT, M_WAITOK);
206 error = copyin(f->map[i], vf->vf_map[i], mapsize);
209 error = vtfont_validate_map(vf->vf_map[i], vf->vf_map_count[i],
215 /* Copy in glyph data. */
216 error = copyin(f->glyphs, vf->vf_bytes, glyphsize);
224 bad: vtfont_unref(vf);