FreeRDP
rdtk_text_field.c
1 
19 #include <winpr/assert.h>
20 #include <winpr/cast.h>
21 
22 #include <rdtk/config.h>
23 
24 #include "rdtk_font.h"
25 
26 #include "rdtk_text_field.h"
27 
28 int rdtk_text_field_draw(rdtkSurface* surface, uint16_t nXDst, uint16_t nYDst, uint16_t nWidth,
29  uint16_t nHeight, rdtkTextField* textField, const char* text)
30 {
31  uint16_t textWidth = 0;
32  uint16_t textHeight = 0;
33 
34  WINPR_ASSERT(surface);
35  WINPR_ASSERT(textField);
36  WINPR_ASSERT(text);
37 
38  rdtkEngine* engine = surface->engine;
39  rdtkFont* font = engine->font;
40  textField = surface->engine->textField;
41  rdtkNinePatch* ninePatch = textField->ninePatch;
42 
43  rdtk_font_text_draw_size(font, &textWidth, &textHeight, text);
44 
45  rdtk_nine_patch_draw(surface, nXDst, nYDst, nWidth, nHeight, ninePatch);
46 
47  if ((textWidth > 0) && (textHeight > 0))
48  {
49  const int fwd = (ninePatch->width - ninePatch->fillWidth);
50  const int fhd = (ninePatch->height - ninePatch->fillHeight);
51 
52  uint16_t fillWidth = nWidth - WINPR_ASSERTING_INT_CAST(uint16_t, fwd);
53  uint16_t fillHeight = nHeight - WINPR_ASSERTING_INT_CAST(uint16_t, fhd);
54  uint16_t offsetX = WINPR_ASSERTING_INT_CAST(uint16_t, ninePatch->fillLeft);
55  uint16_t offsetY = WINPR_ASSERTING_INT_CAST(uint16_t, ninePatch->fillTop);
56 
57  if (textWidth < fillWidth)
58  {
59  const int wd = ((fillWidth - textWidth) / 2) + ninePatch->fillLeft;
60  offsetX = WINPR_ASSERTING_INT_CAST(uint16_t, wd);
61  }
62  else if (textWidth < ninePatch->width)
63  {
64  const int wd = ((ninePatch->width - textWidth) / 2);
65  offsetX = WINPR_ASSERTING_INT_CAST(uint16_t, wd);
66  }
67 
68  if (textHeight < fillHeight)
69  {
70  const int wd = ((fillHeight - textHeight) / 2) + ninePatch->fillTop;
71  offsetY = WINPR_ASSERTING_INT_CAST(uint16_t, wd);
72  }
73  else if (textHeight < ninePatch->height)
74  {
75  const int wd = ((ninePatch->height - textHeight) / 2);
76  offsetY = WINPR_ASSERTING_INT_CAST(uint16_t, wd);
77  }
78 
79  rdtk_font_draw_text(surface, nXDst + offsetX, nYDst + offsetY, font, text);
80  }
81 
82  return 1;
83 }
84 
85 rdtkTextField* rdtk_text_field_new(rdtkEngine* engine, rdtkNinePatch* ninePatch)
86 {
87  WINPR_ASSERT(engine);
88  WINPR_ASSERT(ninePatch);
89 
90  rdtkTextField* textField = (rdtkTextField*)calloc(1, sizeof(rdtkTextField));
91 
92  if (!textField)
93  return NULL;
94 
95  textField->engine = engine;
96  textField->ninePatch = ninePatch;
97 
98  return textField;
99 }
100 
101 void rdtk_text_field_free(rdtkTextField* textField)
102 {
103  free(textField);
104 }
105 
106 int rdtk_text_field_engine_init(rdtkEngine* engine)
107 {
108  WINPR_ASSERT(engine);
109 
110  if (!engine->textField)
111  {
112  engine->textField = rdtk_text_field_new(engine, engine->textField9patch);
113  if (!engine->textField)
114  return -1;
115  }
116 
117  return 1;
118 }
119 
120 int rdtk_text_field_engine_uninit(rdtkEngine* engine)
121 {
122  WINPR_ASSERT(engine);
123  if (engine->textField)
124  {
125  rdtk_text_field_free(engine->textField);
126  engine->textField = NULL;
127  }
128 
129  return 1;
130 }