main.c revision cba9a24f
1/* $NetBSD: main.c,v 1.2 2011/12/28 16:55:27 macallan Exp $ */ 2 3/* 4 * Copyright (c) 2011 Michael Lorenz 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28#include <stdio.h> 29#include <ctype.h> 30 31#include <ft2build.h> 32#include FT_FREETYPE_H 33 34char cvr[] = " .-+oaOX"; 35FT_Library library; 36FT_Face face; 37int baseline, above = 0, below = 0, advance = 0; 38 39int push_size(int); 40 41int 42push_size(int letter) 43{ 44 int glyph_index, error; 45 int new_above, new_below, new_advance; 46 47 glyph_index = FT_Get_Char_Index(face, letter); 48 printf("idx: %d\n", glyph_index); 49 error = FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT); 50 if (error) { 51 printf("wtf?!\n"); 52 return -1; 53 } 54 FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL); 55 printf("%d x %d\n", face->glyph->bitmap.width, face->glyph->bitmap.rows); 56 printf("offset: %d %d\n", face->glyph->bitmap_left, face->glyph->bitmap_top); 57 new_advance = (int)(face->glyph->advance.x >> 6); 58 printf("advance: %d\n", new_advance); 59 new_above = face->glyph->bitmap_top; 60 new_below = face->glyph->bitmap.rows - face->glyph->bitmap_top; 61 if (new_above > above) above = new_above; 62 if (new_below > below) below = new_below; 63 if (new_advance > advance) advance = new_advance; 64 return 0; 65} 66 67int 68main(int argc, char *argv[]) 69{ 70 int error, glyph_index; 71 int x, y, idx, width_in_bytes, height = 22, cell_height; 72 int width, datalen, didx, i, start, end; 73 FILE *output; 74 uint8_t *fontdata; 75 char fontname[128], filename[128]; 76 77 if (argc != 3) { 78 printf("usage: ttf2wsfont some_font.ttf height\n"); 79 return 0; 80 } 81 82 sscanf(argv[2], "%d", &height); 83 84 error = FT_Init_FreeType( &library ); 85 if (error) { 86 printf("Failed to initialize freefont2\n"); 87 return -1; 88 } 89 error = FT_New_Face(library, argv[1], 0, &face ); 90 if ( error == FT_Err_Unknown_File_Format ) { 91 printf("unsupported font format\n"); 92 return -1; 93 } 94 error = FT_Set_Pixel_Sizes(face, /* handle to face object */ 95 0, /* pixel_width */ 96 height - (height / 10) ); /* pixel_height */ 97 if (error) { 98 printf("couldn't set character cell size\n"); 99 } 100 101 push_size('W'); 102 push_size('g'); 103 push_size(192); 104 printf("above: %d below: %d advance: %d\n", above, below, advance); 105 width = advance; 106 baseline = above; 107 cell_height = above + below; 108 datalen = 256 * width * cell_height; 109 fontdata = malloc(datalen); 110 111 112 for (i = 0; i < 256; i++) { 113 glyph_index = FT_Get_Char_Index(face, i); 114 FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT); 115 FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL); 116 width_in_bytes = face->glyph->bitmap.width; 117 start = baseline - face->glyph->bitmap_top; 118 end = start + face->glyph->bitmap.rows; 119 if (end > cell_height) 120 end = cell_height; 121 idx = 0; 122 if (start < 0) { 123 idx += (0 - start) * width_in_bytes; 124 start = 0; 125 } 126 didx = i * width * cell_height + /* character cell */ 127 start * width + /* pixels above baseline */ 128 face->glyph->bitmap_left; /* pixels left from border */ 129 memset(&fontdata[i * width * cell_height], 0, width * cell_height); 130 for (y = start; y < end; y++) { 131 for (x = 0; x < width_in_bytes; x++) { 132 fontdata[didx + x] = face->glyph->bitmap.buffer[idx + x]; 133 } 134 idx += width_in_bytes; 135 didx += width; 136 } 137 } 138 139 /* now output as a header file */ 140 snprintf(fontname, 128, "%s_%dx%d", face->family_name, width, cell_height); 141 for (i = 0; i < strlen(fontname); i++) { 142 if (isblank(fontname[i])) 143 fontname[i]='_'; 144 } 145 snprintf(filename, 128, "%s.h", fontname); 146 if ((output = fopen(filename, "w")) == NULL) { 147 fprintf(stderr, "Can't open output file %s\n", filename); 148 return -1; 149 } 150 fprintf(output, "static u_char %s_data[];\n", fontname); 151 fprintf(output, "\n"); 152 fprintf(output, "static struct wsdisplay_font %s = {\n", fontname); 153 fprintf(output, "\t\"%s\",\t\t\t/* typeface name */\n", face->family_name); 154 fprintf(output, "\t0,\t\t\t\t/* firstchar */\n"); 155 fprintf(output, "\t255 - 0 + 1,\t\t\t/* numchar */\n"); 156 fprintf(output, "\tWSDISPLAY_FONTENC_ISO,\t\t/* encoding */\n"); 157 fprintf(output, "\t%d,\t\t\t\t/* width */\n", width); 158 fprintf(output, "\t%d,\t\t\t\t/* height */\n", cell_height); 159 fprintf(output, "\t%d,\t\t\t\t/* stride */\n", width); 160 fprintf(output, "\tWSDISPLAY_FONTORDER_L2R,\t/* bit order */\n"); 161 fprintf(output, "\tWSDISPLAY_FONTORDER_L2R,\t/* byte order */\n"); 162 fprintf(output, "\t%s_data\t\t/* data */\n", fontname); 163 fprintf(output, "};\n\n"); 164 fprintf(output, "static u_char %s_data[] = {\n", fontname); 165 for (i = 0; i < 256; i++) { 166 fprintf(output, "\t/* %d */\n", i); 167 idx = i * width * cell_height; 168 for (y = 0; y < cell_height; y++) { 169 for (x = 0; x < width; x++) { 170 fprintf(output, "0x%02x, ",fontdata[idx + x]); 171 } 172 fprintf(output, "/* "); 173 for (x = 0; x < width; x++) { 174 fprintf(output, "%c",cvr[fontdata[idx + x] >> 5]); 175 } 176 fprintf(output, " */\n"); 177 178 idx += width; 179 } 180 } 181 fprintf(output, "};\n"); 182 fclose(output); 183 free(fontdata); 184 FT_Done_Face(face); 185 FT_Done_FreeType(library); 186} 187