1 /*****************************************************************************
2 * This file is part of gfxprim library. *
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. *
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. *
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 *
19 * Copyright (C) 2009-2010 Jiri "BlueBear" Dluhos *
20 * <jiri.bluebear.dluhos@gmail.com> *
22 * Copyright (C) 2009-2010 Cyril Hrubis <metan@ucw.cz> *
24 *****************************************************************************/
26 #include "algo/Text.algo.h"
27 #include "gfx/GP_Gfx.h"
28 #include "core/GP_FnPerBpp.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
);
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) {
62 topleft_x
= x
- width
+ 1;
68 topleft_x
= x
- width
/2;
73 switch (align
& 0xf0) {
75 topleft_y
= y
- height
+ 1;
77 case GP_VALIGN_CENTER
:
78 topleft_y
= y
- height
/2;
80 case GP_VALIGN_BASELINE
:
81 topleft_y
= y
- height
+ style
->font
->baseline
;
90 GP_FN_PER_BPP(GP_Text
, context
->bpp
, context
,
91 style
, topleft_x
, topleft_y
, str
, pixel
);
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
);
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) {
114 topleft_x
= x
- width
;
119 case GP_ALIGN_CENTER
:
120 topleft_x
= x
- width
/2;
125 switch (align
& 0xf0) {
126 case GP_VALIGN_ABOVE
:
127 topleft_y
= y
- height
;
129 case GP_VALIGN_CENTER
:
130 topleft_y
= y
- height
/2;
132 case GP_VALIGN_BASELINE
:
133 topleft_y
= y
- height
+ style
->font
->baseline
;
135 case GP_VALIGN_BELOW
:
142 GP_TText_internal(context
, style
, topleft_x
, topleft_y
, str
, pixel
);
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
);
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
,
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
);
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
,