File: ttf2font.c

package info (click to toggle)
tuxpuck 0.8.2-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 880 kB
  • ctags: 460
  • sloc: ansic: 3,319; makefile: 108
file content (77 lines) | stat: -rw-r--r-- 2,008 bytes parent folder | download | duplicates (5)
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
/* ttf2font.c - Copyright (C) 2001-2002 Jacob Kroon, see COPYING for details */

#include <stdlib.h>
#include <stdio.h>
#include <ft2build.h>
#include FT_FREETYPE_H

/* defines */
#define START_CHAR	' '
#define END_CHAR	'z'

void errorc(char *msg)
{
  fprintf(stderr, "%s\n", msg);
  exit(1);
}

void errorcc(char *msg1, char *msg2)
{
  fprintf(stderr, "%s%s\n", msg1, msg2);
  exit(1);
}

void write_glyph(FILE * file, FT_GlyphSlot glyph)
{
  short int x, y;
  short int xsize, ysize;
  short int xpos, ypos, advance;

  FT_Bitmap bitmap = glyph->bitmap;
  xsize = bitmap.width;
  ysize = bitmap.rows;
  xpos = glyph->bitmap_left;
  ypos = glyph->bitmap_top;
  advance = glyph->advance.x >> 6;
  fwrite(&xsize, sizeof(xsize), 1, file);
  fwrite(&ysize, sizeof(ysize), 1, file);
  fwrite(&xpos, sizeof(xpos), 1, file);
  fwrite(&ypos, sizeof(ypos), 1, file);
  fwrite(&advance, sizeof(advance), 1, file);
  for (y = 0; y < bitmap.rows; y++)
    for (x = 0; x < bitmap.width; x++)
      fwrite(&bitmap.buffer[y * bitmap.pitch + x], 1, 1, file);
}

int main(int argc, char **argv)
{
  FT_Library ftl;
  FT_Face face;
  FT_UInt xsize = 0;
  FT_UInt ysize = 0;
  unsigned char ch;
  FILE *out = NULL;

  if (argc != 5)
    errorc("Usage: ttf2font <in> <out> <x-size> <y_size>");
  if (sscanf(argv[3], "%d", &xsize) != 1
      || sscanf(argv[4], "%d", &ysize) != 1)
    errorc("Wrong format of font size");
  if (FT_Init_FreeType(&ftl))
    errorc("Could't initialize FreeType2 library");
  if (FT_New_Face(ftl, argv[1], 0, &face))
    errorcc("Couldn't open font ", argv[1]);
  if (FT_Set_Charmap(face, face->charmaps[0]))
    errorc("Error setting first charmap");
  if (FT_Set_Pixel_Sizes(face, xsize, ysize))
    errorc("Error setting font size");
  out = fopen(argv[2], "wb");
  for (ch = START_CHAR; ch <= END_CHAR; ch++) {
    if (FT_Load_Char(face, ch, FT_LOAD_RENDER))
      errorc("Error rendering glyph");
    fwrite(&ch, 1, 1, out);
    write_glyph(out, face->glyph);
  }
  fclose(out);
  return 0;
}