Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Font utilities

  • Loading branch information...
commit 4fc64bbe9b0007f83a4c272bc6faf41dfbeacb0c 1 parent 56fa082
Sébastien Bourdeauducq authored June 23, 2011
10  ttf2tff/Makefile
... ...
@@ -0,0 +1,10 @@
  1
+
  2
+FT_CFLAGS = -I/usr/include/freetype2
  3
+
  4
+all: tffview ttf2tff
  5
+
  6
+tffview: tffview.c
  7
+	gcc -lSDL $^ -o $@
  8
+
  9
+ttf2tff: ttf2tff.c
  10
+	gcc $(FT_CFLAGS) -lfreetype $^ -o $@
140  ttf2tff/tffview.c
... ...
@@ -0,0 +1,140 @@
  1
+/*
  2
+ * \brief   Viewer for tff (trivial font format) font files
  3
+ * \date    2005-11-07
  4
+ * \author  Norman Feske
  5
+ */
  6
+
  7
+#include <SDL/SDL.h>
  8
+#include <stdlib.h>
  9
+#include <fcntl.h>
  10
+#include <unistd.h>
  11
+#include <sys/stat.h>
  12
+
  13
+typedef unsigned char  u8;
  14
+typedef unsigned short u16;
  15
+typedef unsigned long  u32;
  16
+
  17
+
  18
+/**
  19
+ * Multiply 16bit color with alpha value
  20
+ */
  21
+static inline u16 blend(u16 color, int alpha)
  22
+{
  23
+	return ((((alpha >> 3) * (color & 0xf81f)) >> 5) & 0xf81f)
  24
+	      | (((alpha * (color & 0x07e0)) >> 8) & 0x7e0);
  25
+}
  26
+
  27
+
  28
+/**
  29
+ * Find highest value in array
  30
+ */
  31
+static int highest(u32 *buf, int num)
  32
+{
  33
+	int i, max = 0;
  34
+	for (i = 0; i < num; i++)
  35
+		if (buf[i] > max) max = buf[i];
  36
+	return max;
  37
+}
  38
+
  39
+
  40
+/**
  41
+ * Draw single character
  42
+ */
  43
+static void draw_char(u8 *src, int src_w, int w, int h, u16 *dst, int dst_w)
  44
+{
  45
+	int x, y;
  46
+	for (y = 0; y < h; y++)
  47
+		for (x = 0; x < w; x++)
  48
+			dst[y*dst_w + x] = blend(0xffff, src[y*src_w + x]);
  49
+}
  50
+
  51
+
  52
+/**
  53
+ * Draw a tinted box
  54
+ */
  55
+static void tint(u16 *dst, int dst_w, int w, int h, u16 color)
  56
+{
  57
+	int x, y;
  58
+	for (y = 0; y < h; y++)
  59
+		for (x = 0; x < w; x++)
  60
+			dst[y*dst_w + x] |= color;
  61
+}
  62
+
  63
+
  64
+/**
  65
+ * Main program
  66
+ */
  67
+int main(int argc, char **argv)
  68
+{
  69
+	SDL_Surface *screen;
  70
+	SDL_Event    event;
  71
+	u8  *tff;
  72
+	u16 *scr_adr;
  73
+	int  x, y;
  74
+	u32  otab[256], wtab[256];
  75
+	u8  *img;
  76
+	u32  cw, w, h;
  77
+
  78
+	if (argc < 2) {
  79
+		printf("\nusage:\n\n  tffview <fontfile>\n\n");
  80
+		return 0;
  81
+	}
  82
+
  83
+	int fh = open(argv[1], O_RDONLY);
  84
+	if (fh < 0) {
  85
+		printf("Error: Could not open file %d for reading.\n", (int)argv[1]);
  86
+		return -1;
  87
+	}
  88
+
  89
+	int cnt = 0;
  90
+	cnt += read(fh, otab, sizeof(otab));
  91
+	cnt += read(fh, wtab, sizeof(wtab));
  92
+	cnt += read(fh, &w,   sizeof(w));
  93
+	cnt += read(fh, &h,   sizeof(h));
  94
+
  95
+	if (cnt != 1024*2 + 8) {
  96
+		printf("Error: Could not read %d bytes from font file\n", 1024*2 + 8);
  97
+		return -1;
  98
+	}
  99
+
  100
+	cw = highest(wtab, 256);
  101
+
  102
+	printf("Size of font image is %dx%d, character cell is %dx%d.\n",
  103
+	       (int)w, (int)h, (int)cw, (int)h);
  104
+
  105
+	if (!(img = malloc(w*h))) {
  106
+		printf("Error: Could not allocate %d bytes for font image\n", (int)(w*h));
  107
+		return -1;
  108
+	}
  109
+
  110
+	if (read(fh, img, w*h) != w*h) {
  111
+		printf("Error: Could not read font image from font file\n");
  112
+		return -1;
  113
+	}
  114
+
  115
+	if (SDL_Init(SDL_INIT_VIDEO) < 0) return 0;
  116
+
  117
+	screen = SDL_SetVideoMode(cw*16, h*16, 16, SDL_SWSURFACE);
  118
+	scr_adr = (u16 *)(screen->pixels);
  119
+
  120
+	for (y = 0; y < 16; y++)
  121
+		for (x = 0; x < 16; x++) {
  122
+			int n = y*16 + x;
  123
+			u16 *d = scr_adr + x*cw + y*16*cw*h;
  124
+			draw_char(img + otab[n], w, wtab[n], h, d, cw*16);
  125
+			tint(d, cw*16, cw, h, (x + y) & 1 ? 0 : 0x3800);
  126
+		}
  127
+
  128
+	SDL_UpdateRect(screen, 0, 0, 16*cw, 16*h);
  129
+
  130
+	while (1) {
  131
+		SDL_WaitEvent(&event);
  132
+		switch (event.type) {
  133
+			case SDL_KEYDOWN:
  134
+			case SDL_QUIT:
  135
+				return 0;
  136
+		}
  137
+	}
  138
+
  139
+	return 0;
  140
+}
161  ttf2tff/ttf2tff.c
... ...
@@ -0,0 +1,161 @@
  1
+/*
  2
+ * \brief   Convert truetype fonts to trivial font format
  3
+ * \date    2005-11-07
  4
+ * \author  Norman Feske
  5
+ */
  6
+
  7
+#include <stdio.h>
  8
+#include <stdlib.h>
  9
+#include <fcntl.h>
  10
+#include <unistd.h>
  11
+#include <sys/stat.h>
  12
+#include <ft2build.h>
  13
+#include FT_FREETYPE_H
  14
+
  15
+typedef unsigned char  u8;
  16
+typedef unsigned short u16;
  17
+typedef unsigned long  u32;
  18
+
  19
+
  20
+void draw_bitmap(u8 *dst, int dst_w, int w, int h, u8 *src, int src_w) {
  21
+	int x, y;
  22
+	for (y = 0; y < h; y++) {
  23
+		for (x = 0; x < w; x++)
  24
+			dst[x] = src[x];
  25
+		src += src_w;
  26
+		dst += dst_w;
  27
+	}
  28
+}
  29
+
  30
+
  31
+static int max(int a, int b) { return a > b ? a : b; }
  32
+
  33
+
  34
+/**
  35
+ * Main program
  36
+ */
  37
+int main(int argc, char **argv)
  38
+{
  39
+	static FT_Library   library;
  40
+	static FT_Face      face;
  41
+	static FT_GlyphSlot slot;
  42
+	static u32 otab[256], wtab[256];
  43
+	u32    img_w, img_h;
  44
+	int    img_l;
  45
+	int    res, i;
  46
+	int    size = 16*64;
  47
+	u8    *img;
  48
+
  49
+	if (argc != 4) {
  50
+		printf("\nConvert truetype fonts to trivial font format\n");
  51
+		printf("\nusage:\n\n  ttf2tff <source-ttf> <points> <destination-tff>\n\n");
  52
+		return 0;
  53
+	}
  54
+
  55
+	if ((res = FT_Init_FreeType(&library))) {
  56
+		printf("Error: FT_Init_FreeType returned %d\n", res);
  57
+		return -1;
  58
+	}
  59
+
  60
+	if ((res = FT_New_Face(library, argv[1], 0, &face))) {
  61
+		printf("Error: FT_New_Face returned %d - invalid font file?\n", res);
  62
+		return -1;
  63
+	}
  64
+
  65
+	size = atof(argv[2])*64;
  66
+
  67
+	printf("size       = %d\n",     size);
  68
+	printf("num_faces  = %d\n",     (int)face->num_faces);
  69
+	printf("num_glyphs = %d\n",     (int)face->num_glyphs);
  70
+	printf("face_flags = 0x08%x\n", (int)face->face_flags);
  71
+
  72
+	slot = face->glyph;
  73
+
  74
+	if ((res = FT_Set_Char_Size(face, 0, size, 0, 0))) {
  75
+		printf("Error: FT_Set_Char_Size returned %d\n", res);
  76
+		return;
  77
+	}
  78
+
  79
+	/* calculate dimensions of font image */
  80
+	img_w = 0;
  81
+	img_h = 0;
  82
+	img_l = 0;
  83
+	for (i = 0; i < 256; i++) {
  84
+		int w, h, l;
  85
+
  86
+		if (FT_Load_Char(face, i, FT_LOAD_RENDER | FT_LOAD_TARGET_LIGHT))
  87
+			continue;
  88
+
  89
+		w = max(slot->bitmap.width, slot->advance.x >> 6);
  90
+		h = slot->bitmap.rows;
  91
+		l = slot->bitmap.rows - slot->bitmap_top;
  92
+
  93
+		wtab[i] = w;
  94
+		otab[i] = img_w;
  95
+
  96
+		img_w += w;
  97
+		img_h  = h > img_h ? h : img_h;
  98
+		img_l  = l > img_l ? l : img_l;
  99
+	}
  100
+	printf("font image is %dx%d (lowline at -%d) pixels\n",
  101
+	       (int)img_w, (int)img_h, (int)img_l);
  102
+
  103
+	/* allocate font image */
  104
+	if (!(img = malloc(img_w*(img_h + 50)))) {
  105
+		printf("Error: Could not allocate %d bytes for font image\n", (int)(img_w*img_h));
  106
+		return -1;
  107
+	}
  108
+	bzero(img, img_w*img_h);
  109
+
  110
+	/* draw glyphs into font image */
  111
+	int pen_x = 0;
  112
+	for (i = 0; i < 256; i++) {
  113
+		int x, y, w, h;
  114
+		u8 *s;
  115
+
  116
+		if (FT_Load_Char(face, i, FT_LOAD_RENDER | FT_LOAD_TARGET_LIGHT))
  117
+			continue;
  118
+
  119
+		s = slot->bitmap.buffer;
  120
+		x = otab[i];
  121
+		w = slot->bitmap.width;
  122
+
  123
+		if (w < wtab[i]) {
  124
+			x += (wtab[i] - w) >> 1;
  125
+		}
  126
+		if (slot->bitmap_left < 0) {
  127
+			s -= slot->bitmap_left;
  128
+			w += slot->bitmap_left;
  129
+		}
  130
+		y = img_h - slot->bitmap_top;
  131
+		h = slot->bitmap.rows;
  132
+
  133
+		draw_bitmap(img + x + y*img_w, img_w, w, h, s, slot->bitmap.pitch);
  134
+
  135
+		pen_x += wtab[i];
  136
+	}
  137
+
  138
+	img_h += img_l;
  139
+
  140
+	/* write tff file */
  141
+	int fh = creat(argv[3], S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
  142
+	if (fh < 0) {
  143
+		printf("Error: Could not open file %s for writing\n", argv[3]);
  144
+		return -1;
  145
+	}
  146
+
  147
+	int cnt = 0;
  148
+	cnt += write(fh,  otab,  sizeof(otab));
  149
+	cnt += write(fh,  wtab,  sizeof(wtab));
  150
+	cnt += write(fh, &img_w, sizeof(img_w));
  151
+	cnt += write(fh, &img_h, sizeof(img_h));
  152
+	cnt += write(fh,  img,   img_w*img_h);
  153
+
  154
+	if (cnt != sizeof(otab) + sizeof(wtab) + sizeof(img_w) + sizeof(img_h) + img_w*img_h) {
  155
+		printf("Error: Write error\n");
  156
+		return -1;
  157
+	}
  158
+	close(fh);
  159
+
  160
+	return 0;
  161
+}

0 notes on commit 4fc64bb

Please sign in to comment.
Something went wrong with that request. Please try again.