Fix the directory layout and build system.
[gfxprim.git] / libs / text / GP_Text.c
blobaf11a48a6f6202434d2f96555f12ee1ddae0f0b9
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 "algo/Text.algo.h"
27 #include "gfx/GP_Gfx.h"
28 #include "core/GP_FnPerBpp.h"
29 #include "GP_Text.h"
31 static GP_TextStyle DefaultStyle = GP_DEFAULT_TEXT_STYLE;
33 /* Generate drawing functions for various bit depths. */
34 DEF_TEXT_FN(GP_Text1bpp, GP_Context *, GP_Pixel, GP_HLine1bpp)
35 DEF_TEXT_FN(GP_Text2bpp, GP_Context *, GP_Pixel, GP_HLine2bpp)
36 DEF_TEXT_FN(GP_Text4bpp, GP_Context *, GP_Pixel, GP_HLine4bpp)
37 DEF_TEXT_FN(GP_Text8bpp, GP_Context *, GP_Pixel, GP_HLine8bpp)
38 DEF_TEXT_FN(GP_Text16bpp, GP_Context *, GP_Pixel, GP_HLine16bpp)
39 DEF_TEXT_FN(GP_Text24bpp, GP_Context *, GP_Pixel, GP_HLine24bpp)
40 DEF_TEXT_FN(GP_Text32bpp, GP_Context *, GP_Pixel, GP_HLine32bpp)
41 DEF_TEXT_FN(GP_TText_internal, GP_Context *, GP_Pixel, GP_THLine)
43 GP_RetCode GP_Text(GP_Context *context, const GP_TextStyle *style,
44 int x, int y, int align, const char *str, GP_Pixel pixel)
46 GP_CHECK_CONTEXT(context);
47 GP_CHECK_TEXT_STYLE(style);
49 if (str == NULL)
50 return GP_ENULLPTR;
53 if (style == NULL)
54 style = &DefaultStyle;
56 int width = GP_TextWidth(style, str);
57 int height = GP_TextHeight(style);
59 int topleft_x, topleft_y;
60 switch (align & 0x0f) {
61 case GP_ALIGN_LEFT:
62 topleft_x = x - width + 1;
63 break;
64 case GP_ALIGN_RIGHT:
65 topleft_x = x;
66 break;
67 case GP_ALIGN_CENTER:
68 topleft_x = x - width/2;
69 break;
70 default:
71 return GP_EINVAL;
73 switch (align & 0xf0) {
74 case GP_VALIGN_ABOVE:
75 topleft_y = y - height + 1;
76 break;
77 case GP_VALIGN_CENTER:
78 topleft_y = y - height/2;
79 break;
80 case GP_VALIGN_BASELINE:
81 topleft_y = y - height + style->font->baseline;
82 break;
83 case GP_VALIGN_BELOW:
84 topleft_y = y;
85 break;
86 default:
87 return GP_EINVAL;
90 GP_FN_PER_BPP(GP_Text, context->bpp, context,
91 style, topleft_x, topleft_y, str, pixel);
93 return GP_ESUCCESS;
96 GP_RetCode GP_TText(GP_Context *context, const GP_TextStyle *style,
97 int x, int y, int align, const char *str, GP_Pixel pixel)
99 GP_CHECK_CONTEXT(context);
100 GP_CHECK_TEXT_STYLE(style);
102 if (str == NULL)
103 return GP_ENULLPTR;
105 if (style == NULL)
106 style = &DefaultStyle;
108 int width = GP_TextWidth(style, str);
109 int height = GP_TextHeight(style);
111 int topleft_x, topleft_y;
112 switch (align & 0x0f) {
113 case GP_ALIGN_LEFT:
114 topleft_x = x - width;
115 break;
116 case GP_ALIGN_RIGHT:
117 topleft_x = x;
118 break;
119 case GP_ALIGN_CENTER:
120 topleft_x = x - width/2;
121 break;
122 default:
123 return GP_EINVAL;
125 switch (align & 0xf0) {
126 case GP_VALIGN_ABOVE:
127 topleft_y = y - height;
128 break;
129 case GP_VALIGN_CENTER:
130 topleft_y = y - height/2;
131 break;
132 case GP_VALIGN_BASELINE:
133 topleft_y = y - height + style->font->baseline;
134 break;
135 case GP_VALIGN_BELOW:
136 topleft_y = y;
137 break;
138 default:
139 return GP_EINVAL;
142 GP_TText_internal(context, style, topleft_x, topleft_y, str, pixel);
143 return GP_ESUCCESS;
146 GP_RetCode GP_BoxCenteredText(GP_Context *context, const GP_TextStyle *style,
147 int x, int y, int w, int h, const char *str, GP_Pixel pixel)
149 GP_CHECK_CONTEXT(context);
150 GP_CHECK_TEXT_STYLE(style);
152 if (str == NULL)
153 return GP_ENULLPTR;
155 if (style == NULL)
156 style = &DefaultStyle;
158 const int mid_x = x + w/2;
159 const int mid_y = y + h/2;
160 const int font_ascent = GP_TextAscent(style);
162 return GP_Text(context, style, mid_x,
163 mid_y + font_ascent/2,
164 GP_ALIGN_CENTER | GP_VALIGN_BASELINE,
165 str, pixel);
168 GP_RetCode GP_TBoxCenteredText(GP_Context *context, const GP_TextStyle *style,
169 int x, int y, int w, int h, const char *str, GP_Pixel pixel)
171 GP_CHECK_CONTEXT(context);
172 GP_CHECK_TEXT_STYLE(style);
174 if (str == NULL)
175 return GP_ENULLPTR;
177 if (style == NULL)
178 style = &DefaultStyle;
180 const int mid_x = x + w/2;
181 const int mid_y = y + h/2;
182 const int font_ascent = GP_TextAscent(style);
184 return GP_TText(context, style, mid_x,
185 mid_y + font_ascent/2,
186 GP_ALIGN_CENTER | GP_VALIGN_BASELINE,
187 str, pixel);