2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009 The FreeBSD Foundation
7 * This software was developed by Ed Schouten under sponsorship from the
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/refcount.h>
39 #include <sys/systm.h>
41 #include <dev/vt/vt.h>
43 static MALLOC_DEFINE(M_VTFONT, "vtfont", "vt font");
45 /* Some limits to prevent abnormal fonts from being loaded. */
46 #define VTFONT_MAXMAPPINGS 65536
47 #define VTFONT_MAXGLYPHS 131072
48 #define VTFONT_MAXGLYPHSIZE 2097152
49 #define VTFONT_MAXDIMENSION 128
52 vtfont_bisearch(const vfnt_map_t *map, unsigned int len, uint32_t src)
62 /* Character below minimal entry. */
63 if (src < map[0].vfm_src)
65 /* Optimization: ASCII characters occur very often. */
66 if (src <= map[0].vfm_src + map[0].vfm_len)
67 return (src - map[0].vfm_src + map[0].vfm_dst);
68 /* Character above maximum entry. */
69 if (src > map[max].vfm_src + map[max].vfm_len)
74 mid = (min + max) / 2;
75 if (src < map[mid].vfm_src)
77 else if (src > map[mid].vfm_src + map[mid].vfm_len)
80 return (src - map[mid].vfm_src + map[mid].vfm_dst);
87 vtfont_lookup(const struct vt_font *vf, term_char_t c)
92 unsigned int normal_map;
93 unsigned int bold_map;
95 src = TCHAR_CHARACTER(c);
96 if (TCHAR_FORMAT(c) & TF_CJK_RIGHT) {
97 normal_map = VFNT_MAP_NORMAL_RIGHT;
98 bold_map = VFNT_MAP_BOLD_RIGHT;
100 normal_map = VFNT_MAP_NORMAL;
101 bold_map = VFNT_MAP_BOLD;
104 if (TCHAR_FORMAT(c) & TF_BOLD) {
105 dst = vtfont_bisearch(vf->vf_map[bold_map],
106 vf->vf_map_count[bold_map], src);
110 dst = vtfont_bisearch(vf->vf_map[normal_map],
111 vf->vf_map_count[normal_map], src);
114 stride = howmany(vf->vf_width, 8) * vf->vf_height;
115 return (&vf->vf_bytes[dst * stride]);
119 vtfont_ref(struct vt_font *vf)
122 refcount_acquire(&vf->vf_refcount);
127 vtfont_unref(struct vt_font *vf)
131 if (refcount_release(&vf->vf_refcount)) {
132 for (i = 0; i < VFNT_MAPS; i++)
133 free(vf->vf_map[i], M_VTFONT);
134 free(vf->vf_bytes, M_VTFONT);
140 vtfont_validate_map(vfnt_map_t *vfm, unsigned int length,
141 unsigned int glyph_count)
143 unsigned int i, last = 0;
145 for (i = 0; i < length; i++) {
147 if (i > 0 && vfm[i].vfm_src <= last)
150 * Destination extends amount of glyphs.
152 if (vfm[i].vfm_dst >= glyph_count ||
153 vfm[i].vfm_dst + vfm[i].vfm_len >= glyph_count)
155 last = vfm[i].vfm_src + vfm[i].vfm_len;
162 vtfont_load(vfnt_t *f, struct vt_font **ret)
164 size_t glyphsize, mapsize;
169 /* Make sure the dimensions are valid. */
170 if (f->width < 1 || f->height < 1)
172 if (f->width > VTFONT_MAXDIMENSION || f->height > VTFONT_MAXDIMENSION ||
173 f->glyph_count > VTFONT_MAXGLYPHS)
176 /* Not too many mappings. */
177 for (i = 0; i < VFNT_MAPS; i++)
178 if (f->map_count[i] > VTFONT_MAXMAPPINGS)
181 /* Character 0 must always be present. */
182 if (f->glyph_count < 1)
185 glyphsize = howmany(f->width, 8) * f->height * f->glyph_count;
186 if (glyphsize > VTFONT_MAXGLYPHSIZE)
189 /* Allocate new font structure. */
190 vf = malloc(sizeof *vf, M_VTFONT, M_WAITOK | M_ZERO);
191 vf->vf_bytes = malloc(glyphsize, M_VTFONT, M_WAITOK);
192 vf->vf_height = f->height;
193 vf->vf_width = f->width;
196 /* Allocate, copy in, and validate mappings. */
197 for (i = 0; i < VFNT_MAPS; i++) {
198 vf->vf_map_count[i] = f->map_count[i];
199 if (f->map_count[i] == 0)
201 mapsize = f->map_count[i] * sizeof(vfnt_map_t);
202 vf->vf_map[i] = malloc(mapsize, M_VTFONT, M_WAITOK);
203 error = copyin(f->map[i], vf->vf_map[i], mapsize);
206 error = vtfont_validate_map(vf->vf_map[i], vf->vf_map_count[i],
212 /* Copy in glyph data. */
213 error = copyin(f->glyphs, vf->vf_bytes, glyphsize);
221 bad: vtfont_unref(vf);