Browse Source

* Support fullwidth fonts.

* Add Katakanas and Hiraganas to the default fonts.
tags/v0.99.beta14
Sam Hocevar sam 18 years ago
parent
commit
eb017043ab
5 changed files with 3815 additions and 2992 deletions
  1. +2
    -2
      cucul/font.c
  2. +1743
    -1350
      cucul/font_mono9.h
  3. +2017
    -1617
      cucul/font_monobold12.h
  4. +15
    -5
      test/font2tga.c
  5. +38
    -18
      tools/makefont.c

+ 2
- 2
cucul/font.c View File

@@ -395,7 +395,7 @@ int cucul_render_canvas(cucul_canvas_t *cv, cucul_font_t *f,
unsigned int x, y, xmax, ymax;

if(f->header.bpp != 8)
glyph = malloc(f->header.width * f->header.height);
glyph = malloc(f->header.width * 2 * f->header.height);

if(width < cv->width * f->header.width)
xmax = width / f->header.width;
@@ -476,7 +476,7 @@ int cucul_render_canvas(cucul_canvas_t *cv, cucul_font_t *f,
q = 0xff - p;

for(t = 0; t < 4; t++)
pixel[t] = (((q * argb[t]) + (p * argb[4 + t])) / 0xf);
pixel[t] = (((q * argb[t]) + (p * argb[4 + t])) / 0xf);
}
}
}


+ 1743
- 1350
cucul/font_mono9.h
File diff suppressed because it is too large
View File


+ 2017
- 1617
cucul/font_monobold12.h
File diff suppressed because it is too large
View File


+ 15
- 5
test/font2tga.c View File

@@ -34,17 +34,24 @@ int main(int argc, char *argv[])
cucul_canvas_t *cv;
void *buffer;
unsigned long int len;
unsigned int i, j, x, y, glyphs;
unsigned int i, j, x, y, cells;

fonts = cucul_get_font_list();
f = cucul_load_font(fonts[0], 0);
blocks = cucul_get_font_blocks(f);

for(i = 0, glyphs = 0; blocks[i + 1]; i += 2)
glyphs += blocks[i + 1] - blocks[i];
for(i = 0, cells = 0; blocks[i + 1]; i += 2)
{
cells += blocks[i + 1] - blocks[i];
for(j = blocks[i]; j < blocks[i + 1]; j++)
if(cucul_utf32_is_fullwidth(j))
cells++;
}

/* Create a canvas */
cv = cucul_create_canvas(WIDTH, (glyphs + WIDTH - 1) / WIDTH);
cv = cucul_create_canvas(WIDTH, (cells + WIDTH - 1) / (WIDTH - 1));
cucul_set_color_ansi(cv, CUCUL_RED, CUCUL_RED);
cucul_clear_canvas(cv);
cucul_set_color_ansi(cv, CUCUL_BLACK, CUCUL_WHITE);

/* Put all glyphs on the canvas */
@@ -56,7 +63,10 @@ int main(int argc, char *argv[])
{
cucul_put_char(cv, x, y, j);

if(++x == WIDTH)
if(cucul_utf32_is_fullwidth(j))
++x;

if(++x >= WIDTH - 1)
{
x = 0;
y++;


+ 38
- 18
tools/makefont.c View File

@@ -56,6 +56,8 @@ static unsigned int const blocklist[] =
0x2300, 0x2400, /* Miscellaneous Technical: ⌐ ⌂ ⌠ ⌡ */
0x2500, 0x2580, /* Box Drawing: ═ ║ ╗ ╔ ╩ */
0x2580, 0x25a0, /* Block Elements: ▛ ▞ ░ ▒ ▓ */
0x3040, 0x30a0, /* Hiragana: で す */
0x30a0, 0x3100, /* Katakana: ロ ル */
0, 0
};

@@ -64,7 +66,9 @@ struct glyph
uint32_t unicode;
char buf[10];
unsigned int same_as;
unsigned int data_index;
unsigned int data_offset;
unsigned int data_width;
unsigned int data_size;
};

static void fix_glyph(FT_Bitmap *, uint32_t, unsigned int, unsigned int);
@@ -85,8 +89,9 @@ int main(int argc, char *argv[])
PangoRectangle r;

FT_Bitmap img;
int width, height, blocks, glyphs;
unsigned int n, b, i, index, glyph_size, control_size, data_size;
int width, height, blocks, glyphs, fullwidth;
unsigned int n, b, i, index;
unsigned int glyph_size, control_size, data_size, current_offset;
uint8_t *glyph_data;
struct glyph *gtab;

@@ -149,14 +154,18 @@ int main(int argc, char *argv[])
/* Compute blocks and glyphs count */
blocks = 0;
glyphs = 0;
fullwidth = 0;
for(b = 0; blocklist[b + 1]; b += 2)
{
blocks++;
glyphs += blocklist[b + 1] - blocklist[b];
for(i = blocklist[b]; i < blocklist[b + 1]; i++)
if(cucul_utf32_is_fullwidth(i))
fullwidth++;
}

control_size = 24 + 12 * blocks + 8 * glyphs;
data_size = glyph_size * glyphs;
data_size = glyph_size * (glyphs + fullwidth);

gtab = malloc(glyphs * sizeof(struct glyph));
glyph_data = malloc(data_size);
@@ -211,14 +220,19 @@ int main(int argc, char *argv[])
printf("\n");

/* Render all glyphs, so that we can know their offset */
n = index = 0;
current_offset = n = index = 0;
for(b = 0; blocklist[b + 1]; b += 2)
{
for(i = blocklist[b]; i < blocklist[b + 1]; i++)
{
int x, y, bytes;
unsigned int k;
int x, y, bytes, current_width = width;
unsigned int k, current_size = glyph_size;

if(cucul_utf32_is_fullwidth(i))
{
current_width *= 2;
current_size *= 2;
}
gtab[n].unicode = i;
bytes = cucul_utf32_to_utf8(gtab[n].buf, gtab[n].unicode);
gtab[n].buf[bytes] = '\0';
@@ -229,19 +243,19 @@ int main(int argc, char *argv[])
pango_ft2_render_layout(&img, l, 0, 0);

/* Fix glyphs that we know how to handle better */
fix_glyph(&img, gtab[n].unicode, width, height);
fix_glyph(&img, gtab[n].unicode, current_width, height);

/* Write bitmap as an escaped C string */
memset(glyph_data + n * glyph_size, 0, glyph_size);
memset(glyph_data + current_offset, 0, current_size);
k = 0;
for(y = 0; y < height; y++)
{
for(x = 0; x < width; x++)
for(x = 0; x < current_width; x++)
{
uint8_t pixel = img.buffer[y * img.pitch + x];

pixel >>= (8 - bpp);
glyph_data[n * glyph_size + k / 8]
glyph_data[current_offset + k / 8]
|= pixel << (8 - bpp - (k % 8));
k += bpp;
}
@@ -252,16 +266,22 @@ int main(int argc, char *argv[])
* our data is small enough for this to work. */
for(k = 0; k < n; k++)
{
if(!memcmp(glyph_data + k * glyph_size,
glyph_data + n * glyph_size, glyph_size))
if(gtab[k].data_size != current_size)
continue;
#if 0
if(!memcmp(glyph_data + gtab[k].data_offset,
glyph_data + current_offset, current_size))
break;
#endif
}

gtab[n].data_index = index;
gtab[n].data_offset = current_offset;
gtab[n].data_width = current_width;
gtab[n].data_size = current_size;
gtab[n].same_as = k;

if(k == n)
index++;
current_offset += current_size;

n++;
}
@@ -273,9 +293,9 @@ int main(int argc, char *argv[])
{
for(i = blocklist[b]; i < blocklist[b + 1]; i++)
{
printf_u16("\"%s", width);
printf_u16("\"%s", gtab[n].data_width);
printf_u16("%s", height);
printf_u32("%s\"\n", gtab[gtab[n].same_as].data_index * glyph_size);
printf_u32("%s\"\n", gtab[gtab[n].same_as].data_offset);
n++;
}
}
@@ -293,7 +313,7 @@ int main(int argc, char *argv[])

if(gtab[n].same_as == n)
printf_hex(" */ \"%s\"\n",
glyph_data + n * glyph_size, glyph_size);
glyph_data + gtab[n].data_offset, gtab[n].data_size);
else
{
printf(" is ");


Loading…
Cancel
Save