Added .gitignore
[gfxprim.git] / core / GP_Text.c
blob4e331b03d34e6d34c77c9d376af918ea61d00869
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-2010 Jiri "BlueBear" Dluhos *
20 * <jiri.bluebear.dluhos@gmail.com> *
21 * *
22 * Copyright (C) 2009-2010 Cyril Hrubis <metan@ucw.cz> *
23 * *
24 *****************************************************************************/
26 #include "GP.h"
27 #include "algo/Text.algo.h"
28 #include "GP_FnPerBpp.h"
30 static GP_TextStyle DefaultStyle = GP_DEFAULT_TEXT_STYLE;
32 /* Generate drawing functions for various bit depths. */
33 DEF_TEXT_FN(GP_Text1bpp, GP_Context *, GP_Pixel, GP_HLine1bpp)
34 DEF_TEXT_FN(GP_Text2bpp, GP_Context *, GP_Pixel, GP_HLine2bpp)
35 DEF_TEXT_FN(GP_Text4bpp, GP_Context *, GP_Pixel, GP_HLine4bpp)
36 DEF_TEXT_FN(GP_Text8bpp, GP_Context *, GP_Pixel, GP_HLine8bpp)
37 DEF_TEXT_FN(GP_Text16bpp, GP_Context *, GP_Pixel, GP_HLine16bpp)
38 DEF_TEXT_FN(GP_Text24bpp, GP_Context *, GP_Pixel, GP_HLine24bpp)
39 DEF_TEXT_FN(GP_Text32bpp, GP_Context *, GP_Pixel, GP_HLine32bpp)
40 DEF_TEXT_FN(GP_TText_internal, GP_Context *, GP_Pixel, GP_THLine)
42 GP_RetCode GP_Text(GP_Context *context, const GP_TextStyle *style,
43 int x, int y, int align, const char *str, GP_Pixel pixel)
45 GP_CHECK_CONTEXT(context);
46 GP_CHECK_TEXT_STYLE(style);
48 if (str == NULL)
49 return GP_ENULLPTR;
52 if (style == NULL)
53 style = &DefaultStyle;
55 int width = GP_TextWidth(style, str);
56 int height = GP_TextHeight(style);
58 int topleft_x, topleft_y;
59 switch (align & 0x0f) {
60 case GP_ALIGN_LEFT:
61 topleft_x = x - width + 1;
62 break;
63 case GP_ALIGN_RIGHT:
64 topleft_x = x;
65 break;
66 case GP_ALIGN_CENTER:
67 topleft_x = x - width/2;
68 break;
69 default:
70 return GP_EINVAL;
72 switch (align & 0xf0) {
73 case GP_VALIGN_ABOVE:
74 topleft_y = y - height + 1;
75 break;
76 case GP_VALIGN_CENTER:
77 topleft_y = y - height/2;
78 break;
79 case GP_VALIGN_BASELINE:
80 topleft_y = y - height + style->font->baseline;
81 break;
82 case GP_VALIGN_BELOW:
83 topleft_y = y;
84 break;
85 default:
86 return GP_EINVAL;
89 GP_FN_PER_BPP(GP_Text, context->bpp, context,
90 style, topleft_x, topleft_y, str, pixel);
92 return GP_ESUCCESS;
95 GP_RetCode GP_TText(GP_Context *context, const GP_TextStyle *style,
96 int x, int y, int align, const char *str, GP_Pixel pixel)
98 GP_CHECK_CONTEXT(context);
99 GP_CHECK_TEXT_STYLE(style);
101 if (str == NULL)
102 return GP_ENULLPTR;
104 if (style == NULL)
105 style = &DefaultStyle;
107 int width = GP_TextWidth(style, str);
108 int height = GP_TextHeight(style);
110 int topleft_x, topleft_y;
111 switch (align & 0x0f) {
112 case GP_ALIGN_LEFT:
113 topleft_x = x - width;
114 break;
115 case GP_ALIGN_RIGHT:
116 topleft_x = x;
117 break;
118 case GP_ALIGN_CENTER:
119 topleft_x = x - width/2;
120 break;
121 default:
122 return GP_EINVAL;
124 switch (align & 0xf0) {
125 case GP_VALIGN_ABOVE:
126 topleft_y = y - height;
127 break;
128 case GP_VALIGN_CENTER:
129 topleft_y = y - height/2;
130 break;
131 case GP_VALIGN_BASELINE:
132 topleft_y = y - height + style->font->baseline;
133 break;
134 case GP_VALIGN_BELOW:
135 topleft_y = y;
136 break;
137 default:
138 return GP_EINVAL;
141 GP_TText_internal(context, style, topleft_x, topleft_y, str, pixel);
142 return GP_ESUCCESS;
145 GP_RetCode GP_BoxCenteredText(GP_Context *context, const GP_TextStyle *style,
146 int x, int y, int w, int h, const char *str, GP_Pixel pixel)
148 GP_CHECK_CONTEXT(context);
149 GP_CHECK_TEXT_STYLE(style);
151 if (str == NULL)
152 return GP_ENULLPTR;
154 if (style == NULL)
155 style = &DefaultStyle;
157 const int mid_x = x + w/2;
158 const int mid_y = y + h/2;
159 const int font_ascent = GP_TextAscent(style);
161 return GP_Text(context, style, mid_x,
162 mid_y + font_ascent/2,
163 GP_ALIGN_CENTER | GP_VALIGN_BASELINE,
164 str, pixel);
167 GP_RetCode GP_TBoxCenteredText(GP_Context *context, const GP_TextStyle *style,
168 int x, int y, int w, int h, const char *str, GP_Pixel pixel)
170 GP_CHECK_CONTEXT(context);
171 GP_CHECK_TEXT_STYLE(style);
173 if (str == NULL)
174 return GP_ENULLPTR;
176 if (style == NULL)
177 style = &DefaultStyle;
179 const int mid_x = x + w/2;
180 const int mid_y = y + h/2;
181 const int font_ascent = GP_TextAscent(style);
183 return GP_TText(context, style, mid_x,
184 mid_y + font_ascent/2,
185 GP_ALIGN_CENTER | GP_VALIGN_BASELINE,
186 str, pixel);