core: Get rid of GP_Color.
[gfxprim.git] / demos / ttf2img / ttf2img.c
blobdeaa37a48fab98f6513b694d664448bc96735c60
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
18 * *
19 * Copyright (C) 2009-2012 Cyril Hrubis <metan@ucw.cz> *
20 * *
21 *****************************************************************************/
25 Uses gfxprim to render small image with defined string.
29 #include <GP.h>
31 static const char help[] = {
32 "usage: ttf2img -f font.ttf -i file.png -s string [-d debug_level]\n",
35 static void print_help(int i)
37 fputs(help, stderr);
38 exit(i);
41 int main(int argc, char *argv[])
43 const char *font_path = NULL;
44 const char *img_path = NULL;
45 const char *string = "Foo Bar!";
46 int opt, debug_level = 0;
47 int img_w = 400, img_h = 100;
49 while ((opt = getopt(argc, argv, "d:f:i:s:")) != -1) {
50 switch (opt) {
51 case 'f':
52 font_path = optarg;
53 break;
54 case 'i':
55 img_path = optarg;
56 break;
57 case 'd':
58 debug_level = atoi(optarg);
59 break;
60 case 'h':
61 print_help(0);
62 break;
63 case 's':
64 string = optarg;
65 break;
66 default:
67 fprintf(stderr, "Invalid paramter '%c'\n", opt);
68 print_help(1);
72 if (font_path == NULL || img_path == NULL)
73 print_help(1);
75 GP_SetDebugLevel(debug_level);
77 GP_Context *context = GP_ContextAlloc(img_w, img_h, GP_PIXEL_RGB888);
79 GP_Pixel black_pixel = GP_RGBToContextPixel(0x00, 0x00, 0x00, context);
80 GP_Pixel white_pixel = GP_RGBToContextPixel(0xff, 0xff, 0xff, context);
82 GP_Fill(context, white_pixel);
84 GP_TextStyle style = GP_DEFAULT_TEXT_STYLE;
86 style.font = GP_FontFaceLoad(font_path, 27, 0);
88 GP_Text(context, &style, img_w/2, img_h/2, GP_ALIGN_CENTER|GP_VALIGN_CENTER,
89 black_pixel, white_pixel, string);
91 GP_SavePNG(context, img_path, NULL);
93 GP_ContextFree(context);
95 return 0;