/*************************************************************************** * fcbanner - banner using fontconfig * (C) Nicolas George - 2004-03-02 ***************************************************************************/ /* * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* About: fcbanner is a variant on the banner program that uses fontconfig and freetype to draw its characters. Thus, it can easily draw using various fonts -- any font you can get in Gnome or Mozilla for example -- and handles non-ASCII characters if they are present in the font. Supported environment: fcbanner runs on Unix-like systems provided that: - they support the fontconfig and freetype libraries, of course; - they support the wide characters API (wchar.h); - the wchar_t coding coincides with Unicode. This includes GNU/Linux, Solaris >= 8 with UTF-8 locales (but NOT on monobyte locales!), FreeBSD >= 5.0, and maybe some other/older systems I could not test. If wchar_t is not Unicode-based, fcbanner will work but will probably give strange results on non-ASCII characters. Build: gcc -Wall -W -std=c99 -D_XOPEN_SOURCE=600 -g -O3 -o fcbanner \ `pkg-config --cflags freetype2 fontconfig` fcbanner.c \ `pkg-config --libs freetype2 fontconfig` Usage: echo text | fcbanner -f font See the Fontconfig User Guide for the generic syntax of the font specification. The supported features for now are font size and font matrix. The encoding of the input text is determined by the LC_CTYPE category of the locale settings (LC_ALL, LC_CTYPE ou LANG environment variable). Examples: echo '無' | recode html..utf8 | \ LC_CTYPE=en_US.UTF-8 fcbanner -f 'AR PL KaitiM Big5:size=64' Changes: 2005-06-15 Support font matrix. Version 1.0 release. */ #include #include #include #include #include #include #include #include #include FT_FREETYPE_H const char *font_name = "sans-serif"; FcConfig *config; FT_Library freetype; FT_Face font; char ascii_art[2 * 2 * 2 * 2 * 2 * 2] = { /* .. #. .# ## .. .. .. .. .. .. .. .. */ ' ', '`', '\'', '"', /* .. #. .# ## #. #. #. #. .. .. .. .. */ ' ', '`', '\'', '^', /* .. #. .# ## .# .# .# .# .. .. .. .. */ ' ', '`', '\'', '^', /* .. #. .# ## ## ## ## ## .. .. .. .. */ '-', '+', '+', '*', /* .. #. .# ## .. .. .. .. #. #. #. #. */ '.', ':', ':', ':', /* .. #. .# ## #. #. #. #. #. #. #. #. */ ',', '|', '/', 'T', /* .. #. .# ## .# .# .# .# #. #. #. #. */ ',', ')', '/', ']', /* .. #. .# ## ## ## ## ## #. #. #. #. */ '+', '}', 'P', 'F', /* .. #. .# ## .. .. .. .. .# .# .# .# */ '.', ':', ':', ':', /* .. #. .# ## #. #. #. #. .# .# .# .# */ ',', '\\', '(', '[', /* .. #. .# ## .# .# .# .# .# .# .# .# */ ',', '\\', '|', 'T', /* .. #. .# ## ## ## ## ## .# .# .# .# */ '+', 'S', '{', 'R', /* .. #. .# ## .. .. .. .. ## ## ## ## */ '_', 'L', 'J', '=', /* .. #. .# ## #. #. #. #. ## ## ## ## */ '_', 'L', '[', '[', /* .. #. .# ## .# .# .# .# ## ## ## ## */ '_', ']', 'J', ']', /* .. #. .# ## ## ## ## ## ## ## ## ## */ '%', 'B', 'B', 'M', }; unsigned char **buffer = NULL; int xmin = 0, xmax = 0, ymin = 0, ymax = 0; int xcur = 0, ycur = 0; int baselineskip; void enlarge_buffer(int xi, int yi, int xa, int ya) { int y; if(xi < xmin || xa > xmax) { if(xi > xmin) xi = xmin; if(xa < xmax) xa = xmax; for(y = ymin; y < ymax; y++) { if((buffer[y] = realloc(buffer[y] + xmin, xa - xi)) == NULL) { perror("malloc"); exit(1); } buffer[y] -= xi; memmove(buffer[y] + xmin, buffer[y] + xi, xmax - xmin); memset(buffer[y] + xi, 0, xmin - xi); memset(buffer[y] + xmax, 0, xa - xmax); } xmin = xi; xmax = xa; } if(yi < ymin || ya > ymax) { if(yi > ymin) yi = ymin; if(ya < ymax) ya = ymax; if((buffer = realloc(buffer + ymin, (ya - yi) * sizeof(unsigned char *))) == NULL) { perror("malloc"); exit(1); } buffer -= yi; memmove(buffer + ymin, buffer + yi, sizeof(unsigned char *) * (ymax - ymin)); for(y = yi; y < ymin; y++) { if((buffer[y] = calloc(xmax - xmin, 1)) == NULL) { perror("malloc"); exit(1); } buffer[y] -= xmin; } for(y = ymax; y < ya; y++) { if((buffer[y] = calloc(xmax - xmin, 1)) == NULL) { perror("malloc"); exit(1); } buffer[y] -= xmin; } ymin = yi; ymax = ya; } } void render_glyph(int glyph) { FT_Error error; int index, x0, y0, w, h, x, y; unsigned char *p, *q; x0 = (xcur + 32) / 64; y0 = (ycur + 32) / 64; index = FT_Get_Char_Index(font, glyph); if((error = FT_Load_Glyph(font, index, FT_LOAD_DEFAULT))) { fprintf(stderr, "Error loading glyph: %d\n", error); exit(1); } if((error = FT_Render_Glyph(font->glyph, FT_RENDER_MODE_NORMAL))) { fprintf(stderr, "Error rendering glyph: %d\n", error); exit(1); } if(font->glyph->bitmap.pixel_mode != FT_PIXEL_MODE_GRAY) { fprintf(stderr, "Unsupported pixel mode: %d\n", font->glyph->bitmap.pixel_mode); exit(1); } x0 += font->glyph->bitmap_left; y0 -= font->glyph->bitmap_top; xcur += font->glyph->advance.x; ycur -= font->glyph->advance.y; w = font->glyph->bitmap.width; h = font->glyph->bitmap.rows; p = font->glyph->bitmap.buffer; enlarge_buffer(x0, y0, x0 + w, y0 + h); for(y = 0; y < h; y++) { q = buffer[y0 + y] + x0; for(x = 0; x < w; x++) { *q = 255 - (255 - p[x]) * (255 - *q) / 255; q++; } p += font->glyph->bitmap.pitch; } } void render_string(const char *string) { int r; size_t l; wchar_t c; mbtowc(NULL, NULL, 0); l = strlen(string); while(l > 0) { r = mbtowc(&c, string, l); if(r > 0) { render_glyph(c); } else { r = 1; } l -= r; string += r; } } void newline(void) { xcur = 0; ycur += baselineskip; } int main(int argc, char **argv) { FT_Error error; int x, y, v, i; FcPattern *pattern, *fpat; FcResult result = FcResultMatch; FcChar8 *filename; int font_index; double font_size; FcMatrix *font_matrix = NULL; int o; while((o = getopt(argc, argv, "hf:")) != -1) { switch(o) { case 'f': font_name = optarg; break; default: fprintf(o == 'h' ? stdout : stderr, "Usage: fcbanner [-f font] [text] ...\n"); exit(o == 'h' ? 0 : 1); } } argc -= optind; argv += optind; if(setlocale(LC_CTYPE, "") == NULL) { perror("setlocale"); exit(1); } if(!FcInit()) { fprintf(stderr, "Error initializing fontconfig.\n"); exit(1); } if((pattern = FcNameParse(font_name)) == NULL) { fprintf(stderr, "Error parsing font name.\n"); exit(1); } if(!FcConfigSubstitute(NULL, pattern, FcMatchPattern)) { fprintf(stderr, "Error substituting configuration.\n"); exit(1); } FcDefaultSubstitute(pattern); if((fpat = FcFontMatch(NULL, pattern, &result)) == NULL || result != FcResultMatch) { fprintf(stderr, "Error matching the font.\n"); exit(1); } if(FcPatternGetString(fpat, FC_FILE, 0, &filename) != FcResultMatch) { fprintf(stderr, "Error finding the font name.\n"); exit(1); } if(FcPatternGetInteger(fpat, FC_INDEX, 0, &font_index) != FcResultMatch) { fprintf(stderr, "Error finding the font index.\n"); exit(1); } if(FcPatternGetDouble(fpat, FC_SIZE, 0, &font_size) != FcResultMatch) { fprintf(stderr, "Error finding the font size.\n"); exit(1); } if(FcPatternGetMatrix(fpat, FC_MATRIX, 0, &font_matrix) != FcResultMatch) { font_matrix = NULL; /* make sure */ } if((error = FT_Init_FreeType(&freetype))) { fprintf(stderr, "Error initializing Freetype2: %d\n", error); exit(1); } if((error = FT_New_Face(freetype, filename, font_index, &font))) { fprintf(stderr, "Error loading the font: %d\n", error); exit(1); } if((error = FT_Set_Char_Size(font, font_size * 64, font_size * 64, 72 * 2, 72 * 3))) { fprintf(stderr, "Error setting pixel size: %d\n", error); exit(1); } if(font_matrix != NULL) { FT_Matrix m; FT_Vector v; m.xx = font_matrix->xx * 65536; m.xy = font_matrix->xy * 65536; m.yx = font_matrix->yx * 65536; m.yy = font_matrix->yy * 65536; v.x = v.y = 0; FT_Set_Transform(font, &m, &v); } baselineskip = (font->ascender - font->descender) * font_size * 64 * 3 / font->units_per_EM; if(argc > 0) { for(i = 0; i < argc; i++) { render_string(argv[i]); newline(); } } else { wint_t c; while((c = fgetwc(stdin)) != WEOF) { if(c == L'\n') newline(); else render_glyph(c); } } enlarge_buffer(xmin, ymin - ((ymax - ymin) % 3 == 1 ? 1 : 0), xmax + ((xmax - xmin) % 2 > 0 ? 1 : 0), ymax + ((ymax - ymin) % 3 > 0 ? 1 : 0)); for(y = ymin; y < ymax; y += 3) { for(x = xmin; x < xmax; x += 2) { v = (buffer[y][x] >= 128 ? 1 : 0) | (buffer[y][x + 1] >= 128 ? 2 : 0) | (buffer[y + 1][x] >= 128 ? 4 : 0) | (buffer[y + 1][x + 1] >= 128 ? 8 : 0) | (buffer[y + 2][x] >= 128 ? 16 : 0) | (buffer[y + 2][x + 1] >= 128 ? 32 : 0); putchar(ascii_art[v]); } putchar('\n'); } return(0); }