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 *****************************************************************************/
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
);
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) {
61 topleft_x
= x
- width
+ 1;
67 topleft_x
= x
- width
/2;
72 switch (align
& 0xf0) {
74 topleft_y
= y
- height
+ 1;
76 case GP_VALIGN_CENTER
:
77 topleft_y
= y
- height
/2;
79 case GP_VALIGN_BASELINE
:
80 topleft_y
= y
- height
+ style
->font
->baseline
;
89 GP_FN_PER_BPP(GP_Text
, context
->bpp
, context
,
90 style
, topleft_x
, topleft_y
, str
, pixel
);
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
);
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) {
113 topleft_x
= x
- width
;
118 case GP_ALIGN_CENTER
:
119 topleft_x
= x
- width
/2;
124 switch (align
& 0xf0) {
125 case GP_VALIGN_ABOVE
:
126 topleft_y
= y
- height
;
128 case GP_VALIGN_CENTER
:
129 topleft_y
= y
- height
/2;
131 case GP_VALIGN_BASELINE
:
132 topleft_y
= y
- height
+ style
->font
->baseline
;
134 case GP_VALIGN_BELOW
:
141 GP_TText_internal(context
, style
, topleft_x
, topleft_y
, str
, pixel
);
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
);
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
,
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
);
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
,