1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** GUIX Component                                                        */
17 /**                                                                       */
18 /**   Widget Management (Widget)                                          */
19 /**                                                                       */
20 /**************************************************************************/
21 
22 #define GX_SOURCE_CODE
23 
24 
25 /* Include necessary system files.  */
26 
27 #include "gx_api.h"
28 #include "gx_system.h"
29 #include "gx_canvas.h"
30 #include "gx_context.h"
31 #include "gx_widget.h"
32 #include "gx_utility.h"
33 
34 /**************************************************************************/
35 /*                                                                        */
36 /*  FUNCTION                                               RELEASE        */
37 /*                                                                        */
38 /*    _gx_widget_text_blend                               PORTABLE C      */
39 /*                                                           6.1          */
40 /*  AUTHOR                                                                */
41 /*                                                                        */
42 /*    Kenneth Maxwell, Microsoft Corporation                              */
43 /*                                                                        */
44 /*  DESCRIPTION (deprecated)                                              */
45 /*                                                                        */
46 /*    This function blends the specified text using current brush and     */
47 /*    text alignment.                                                     */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    widget                                Widget control block          */
52 /*    tColor                                Text Color                    */
53 /*    font_id                               Font Id                       */
54 /*    string                                Drawing string                */
55 /*    x_offset                              Drawing position adjustment   */
56 /*    y_offset                              Drawing position adjustment   */
57 /*    alpha                                 Blending value 0-255          */
58 /*                                                                        */
59 /*  OUTPUT                                                                */
60 /*                                                                        */
61 /*    status                                Completion status             */
62 /*                                                                        */
63 /*  CALLS                                                                 */
64 /*                                                                        */
65 /*    _gx_utility_string_length_check       Validate string length        */
66 /*    _gx_widget_text_blend_ext             New version of this function  */
67 /*                                                                        */
68 /*  CALLED BY                                                             */
69 /*                                                                        */
70 /*    GUIX Internal Code                                                  */
71 /*                                                                        */
72 /*  RELEASE HISTORY                                                       */
73 /*                                                                        */
74 /*    DATE              NAME                      DESCRIPTION             */
75 /*                                                                        */
76 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
77 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
78 /*                                            resulting in version 6.1    */
79 /*                                                                        */
80 /**************************************************************************/
81 #if defined(GX_ENABLE_DEPRECATED_STRING_API)
_gx_widget_text_blend(GX_WIDGET * widget,UINT tColor,UINT font_id,GX_CONST GX_CHAR * string,INT x_offset,INT y_offset,UCHAR alpha)82 UINT  _gx_widget_text_blend(GX_WIDGET *widget,
83                             UINT tColor, UINT font_id,
84                             GX_CONST GX_CHAR *string, INT x_offset, INT y_offset, UCHAR alpha)
85 {
86 UINT      status;
87 GX_STRING new_string;
88 
89     new_string.gx_string_ptr = string;
90 
91     /* Calcualte text length. */
92     status = _gx_utility_string_length_check(string, &new_string.gx_string_length, GX_MAX_STRING_LENGTH);
93     if (status != GX_SUCCESS)
94     {
95         return status;
96     }
97 
98     status = _gx_widget_text_blend_ext(widget, tColor, font_id, &new_string, x_offset, y_offset, alpha);
99 
100     return status;
101 }
102 #endif
103 
104 
105 /**************************************************************************/
106 /*                                                                        */
107 /*  FUNCTION                                               RELEASE        */
108 /*                                                                        */
109 /*    _gx_widget_text_blend_ext                           PORTABLE C      */
110 /*                                                           6.1          */
111 /*  AUTHOR                                                                */
112 /*                                                                        */
113 /*    Kenneth Maxwell, Microsoft Corporation                              */
114 /*                                                                        */
115 /*  DESCRIPTION                                                           */
116 /*                                                                        */
117 /*    This function blends the specified text using current brush and     */
118 /*    text alignment.                                                     */
119 /*                                                                        */
120 /*  INPUT                                                                 */
121 /*                                                                        */
122 /*    widget                                Widget control block          */
123 /*    tColor                                Text Color                    */
124 /*    font_id                               Font Id                       */
125 /*    string                                Drawing string                */
126 /*    x_offset                              Drawing position adjustment   */
127 /*    y_offset                              Drawing position adjustment   */
128 /*    alpha                                 Blending value 0-255          */
129 /*                                                                        */
130 /*  OUTPUT                                                                */
131 /*                                                                        */
132 /*    status                                Completion status             */
133 /*                                                                        */
134 /*  CALLS                                                                 */
135 /*                                                                        */
136 /*    _gx_context_line_color_set            Set the line color            */
137 /*    _gx_context_font_set                  Set the font in the context   */
138 /*    _gx_context_brush_get                 Get the context brush         */
139 /*    _gx_widget_height_get                 Get widget height             */
140 /*    _gx_widget_width_get                  Get widget width              */
141 /*    _gx_widget_border_width_get           Get widget border width       */
142 /*    _gx_system_string_width_get           Get string width              */
143 /*    _gx_canvas_text_draw                  Draw text on the canvas       */
144 /*                                                                        */
145 /*  CALLED BY                                                             */
146 /*                                                                        */
147 /*    GUIX Internal Code                                                  */
148 /*                                                                        */
149 /*  RELEASE HISTORY                                                       */
150 /*                                                                        */
151 /*    DATE              NAME                      DESCRIPTION             */
152 /*                                                                        */
153 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
154 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
155 /*                                            resulting in version 6.1    */
156 /*                                                                        */
157 /**************************************************************************/
_gx_widget_text_blend_ext(GX_WIDGET * widget,UINT tColor,UINT font_id,GX_CONST GX_STRING * string,INT x_offset,INT y_offset,UCHAR alpha)158 UINT  _gx_widget_text_blend_ext(GX_WIDGET *widget,
159                                 UINT tColor, UINT font_id,
160                                 GX_CONST GX_STRING *string, INT x_offset, INT y_offset, UCHAR alpha)
161 {
162 GX_VALUE  text_width;
163 GX_VALUE  text_height;
164 GX_VALUE  widget_width;
165 GX_VALUE  widget_height;
166 GX_VALUE  x_pos;
167 GX_VALUE  y_pos;
168 GX_VALUE  border_width;
169 
170 GX_BRUSH *brush;
171 
172     /* Is there a string?  */
173     if (string && string -> gx_string_ptr)
174     {
175         _gx_context_line_color_set(tColor);
176         _gx_context_font_set(font_id);
177         _gx_context_brush_get(&brush);
178 
179         if (!brush -> gx_brush_font)
180         {
181             return(GX_SUCCESS);
182         }
183         brush -> gx_brush_alpha = alpha;
184 
185         text_height = brush -> gx_brush_font -> gx_font_line_height;
186         _gx_widget_height_get(widget, &widget_height);
187 
188         _gx_widget_border_width_get(widget, &border_width);
189 
190         x_pos = widget -> gx_widget_size.gx_rectangle_left;
191         y_pos = widget -> gx_widget_size.gx_rectangle_top;
192         y_pos = (GX_VALUE)(y_pos + (widget_height - text_height) / 2);
193 
194         switch (widget -> gx_widget_style & GX_STYLE_TEXT_ALIGNMENT_MASK)
195         {
196         case GX_STYLE_TEXT_RIGHT:
197             _gx_system_string_width_get_ext(brush -> gx_brush_font, string, &text_width);
198             _gx_widget_width_get(widget, &widget_width);
199             x_pos = (GX_VALUE)(x_pos + widget_width - 1);
200             x_pos = (GX_VALUE)(x_pos - text_width - border_width);
201             break;
202 
203         case GX_STYLE_TEXT_LEFT:
204             x_pos = (GX_VALUE)(x_pos + border_width);
205             break;
206 
207         case GX_STYLE_TEXT_CENTER:
208         default:
209             _gx_system_string_width_get_ext(brush -> gx_brush_font, string, &text_width);
210             _gx_widget_width_get(widget, &widget_width);
211             x_pos = (GX_VALUE)(x_pos + ((widget_width - text_width) / 2));
212             break;
213         }
214 
215         /* Draw the text.  */
216         _gx_canvas_text_draw_ext((GX_VALUE)(x_pos + x_offset), (GX_VALUE)(y_pos + y_offset), string);
217     }
218 
219     return(GX_SUCCESS);
220 }
221 
222