Rename GP_Context -> GP_Pixmap
[gfxprim.git] / libs / text / GP_Text.c
blobd3764d0f975e7dae783844620b5ca4ec0a367298
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-2011 Jiri "BlueBear" Dluhos *
20 * <jiri.bluebear.dluhos@gmail.com> *
21 * *
22 * Copyright (C) 2009-2014 Cyril Hrubis <metan@ucw.cz> *
23 * *
24 *****************************************************************************/
26 #include "gfx/GP_Gfx.h"
27 #include "core/GP_FnPerBpp.h"
28 #include "core/GP_Debug.h"
29 #include "GP_TextMetric.h"
30 #include "GP_Text.h"
32 GP_TextStyle GP_DefaultStyle = GP_DEFAULT_TEXT_STYLE;
34 static int do_align(GP_Coord *topleft_x, GP_Coord *topleft_y, int align,
35 GP_Coord x, GP_Coord y, const GP_TextStyle *style,
36 GP_Size width)
38 GP_Size height = GP_TextHeight(style);
40 switch (align & 0x0f) {
41 case GP_ALIGN_LEFT:
42 *topleft_x = x - width + 1;
43 break;
44 case GP_ALIGN_RIGHT:
45 *topleft_x = x;
46 break;
47 case GP_ALIGN_CENTER:
48 *topleft_x = x - width/2;
49 break;
50 default:
51 GP_DEBUG(1, "ALIGN 0x%0x\n", align);
52 return 1;
55 switch (align & 0x70) {
56 case GP_VALIGN_ABOVE:
57 *topleft_y = y - height + 1;
58 break;
59 case GP_VALIGN_CENTER:
60 *topleft_y = y - height/2;
61 break;
62 case GP_VALIGN_BASELINE:
63 *topleft_y = y - GP_TextAscent(style) + 1;
64 break;
65 case GP_VALIGN_BELOW:
66 *topleft_y = y;
67 break;
68 default:
69 GP_DEBUG(1, "VALIGN 0x%0x\n", align);
70 return 1;
73 return 0;
76 void GP_Text(GP_Pixmap *pixmap, const GP_TextStyle *style,
77 GP_Coord x, GP_Coord y, int align,
78 GP_Pixel fg_color, GP_Pixel bg_color,
79 const char *str)
81 GP_CHECK_PIXMAP(pixmap);
83 if (str == NULL)
84 return;
86 if (style == NULL)
87 style = &GP_DefaultStyle;
89 GP_Coord topleft_x, topleft_y;
91 GP_Size w = GP_TextWidth(style, str);
93 GP_ASSERT(do_align(&topleft_x, &topleft_y, align, x, y, style, w) == 0,
94 "Invalid aligment flags");
96 GP_Text_Raw(pixmap, style, topleft_x, topleft_y,
97 align & GP_TEXT_NOBG, fg_color, bg_color, str);
100 GP_Size GP_VPrint(GP_Pixmap *pixmap, const GP_TextStyle *style,
101 GP_Coord x, GP_Coord y, int align,
102 GP_Pixel fg_color, GP_Pixel bg_color,
103 const char *fmt, va_list va)
105 va_list vac;
106 int size;
108 va_copy(vac, va);
109 size = vsnprintf(NULL, 0, fmt, va);
110 char buf[size+1];
111 vsnprintf(buf, sizeof(buf), fmt, vac);
112 va_end(vac);
114 GP_Text(pixmap, style, x, y, align, fg_color, bg_color, buf);
116 return GP_TextWidth(style, buf);
119 GP_Size GP_Print(GP_Pixmap *pixmap, const GP_TextStyle *style,
120 GP_Coord x, GP_Coord y, int align,
121 GP_Pixel fg_color, GP_Pixel bg_color, const char *fmt, ...)
123 va_list va;
124 GP_Size ret;
126 va_start(va, fmt);
127 ret = GP_VPrint(pixmap, style, x, y, align,
128 fg_color, bg_color, fmt, va);
129 va_end(va);
131 return ret;
134 void GP_TextClear(GP_Pixmap *pixmap, const GP_TextStyle *style,
135 GP_Coord x, GP_Coord y, int align,
136 GP_Pixel bg_color, GP_Size size)
138 GP_Coord topleft_x, topleft_y;
140 GP_ASSERT(do_align(&topleft_x, &topleft_y, align, x, y, style, size) == 0,
141 "Invalid aligment flags");
143 GP_FillRectXYWH(pixmap, topleft_x, topleft_y,
144 size, GP_TextHeight(style), bg_color);
147 void GP_TextClearStr(GP_Pixmap *pixmap, const GP_TextStyle *style,
148 GP_Coord x, GP_Coord y, int align,
149 GP_Pixel bg_color, const char *str)
151 GP_TextClear(pixmap, style, x, y, align,
152 bg_color, GP_TextWidth(style, str));