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-2011 Jiri "BlueBear" Dluhos *
20 * <jiri.bluebear.dluhos@gmail.com> *
22 * Copyright (C) 2009-2014 Cyril Hrubis <metan@ucw.cz> *
24 *****************************************************************************/
26 #include "gfx/GP_Gfx.h"
27 #include "core/GP_FnPerBpp.h"
28 #include "core/GP_Debug.h"
29 #include "GP_TextMetric.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
,
38 GP_Size height
= GP_TextHeight(style
);
40 switch (align
& 0x0f) {
42 *topleft_x
= x
- width
+ 1;
48 *topleft_x
= x
- width
/2;
51 GP_DEBUG(1, "ALIGN 0x%0x\n", align
);
55 switch (align
& 0x70) {
57 *topleft_y
= y
- height
+ 1;
59 case GP_VALIGN_CENTER
:
60 *topleft_y
= y
- height
/2;
62 case GP_VALIGN_BASELINE
:
63 *topleft_y
= y
- GP_TextAscent(style
) + 1;
69 GP_DEBUG(1, "VALIGN 0x%0x\n", align
);
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
,
81 GP_CHECK_PIXMAP(pixmap
);
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
)
109 size
= vsnprintf(NULL
, 0, fmt
, va
);
111 vsnprintf(buf
, sizeof(buf
), fmt
, 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
, ...)
127 ret
= GP_VPrint(pixmap
, style
, x
, y
, align
,
128 fg_color
, bg_color
, fmt
, va
);
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
));