Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Strings.cpp « Display « DevCore « STM32F415APP - github.com/nickshl/DevBoy.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e60086780d3828d3515efe8cfa60ba27a4bb461e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//******************************************************************************
//  @file String.cpp
//  @author Nicolai Shlapunov
//
//  @details DevCore: String Visual Object Class, implementation
//
//  @copyright Copyright (c) 2016, Devtronic & Nicolai Shlapunov
//             All rights reserved.
//
//  @section SUPPORT
//
//   Devtronic invests time and resources providing this open source code,
//   please support Devtronic and open-source hardware/software by
//   donations and/or purchasing products from Devtronic.
//
//******************************************************************************

// *****************************************************************************
// ***   Includes   ************************************************************
// *****************************************************************************
#include "Strings.h"
#include "Fonts.h"

#include <cstring> // for strlen()

// *****************************************************************************
// *****************************************************************************
// ***   Strings   *************************************************************
// *****************************************************************************
// *****************************************************************************
const String::FontProfile String::fonts[FONTS_MAX] =
//  W   H BPP  Pointer to data
{ { 4,  6,  6, (const uint8_t*)font4x6},
  { 6,  8,  8, (const uint8_t*)font6x8},
  { 8,  8,  8, (const uint8_t*)font8x8},
  { 8, 12, 12, (const uint8_t*)font8x12},
  {12, 16, 32, (const uint8_t*)font12x16} };

// *****************************************************************************
// ***   Constructor   *********************************************************
// *****************************************************************************
String::String(const char* str, int32_t x, int32_t y, uint32_t tc, FontType ft)
{
  SetParams(str, x, y, tc, ft);
}

// *****************************************************************************
// ***   Constructor   *********************************************************
// *****************************************************************************
String::String(const char* str, int32_t x, int32_t y, uint32_t tc, uint32_t bgc, FontType ft)
{
  SetParams(str, x, y, tc, bgc, ft);
}

// *****************************************************************************
// ***   SetParams   ***********************************************************
// *****************************************************************************
void String::SetParams(const char* str, int32_t x, int32_t y, uint32_t tc, FontType ft)
{
  string = (const uint8_t*)str;
  x_start = x;
  y_start = y;
  txt_color = tc;
  bg_color = 0;
  font_type = ft;
  transpatent_bg = true;
  width = fonts[ft].w * strlen(str);
  height = fonts[ft].h;
  x_end = x + width - 1;
  y_end = y + height - 1;
  rotation = 0;
}

// *****************************************************************************
// ***   SetParams   ***********************************************************
// *****************************************************************************
void String::SetParams(const char* str, int32_t x, int32_t y, uint32_t tc, uint32_t bgc, FontType ft)
{
  string = (const uint8_t*)str;
  x_start = x;
  y_start = y;
  txt_color = tc;
  bg_color = bgc;
  font_type = ft;
  transpatent_bg = false;
  width = fonts[ft].w * strlen(str);
  height = fonts[ft].h;
  x_end = x + width - 1;
  y_end = y + height - 1;
  rotation = 0;
}

// *****************************************************************************
// ***   SetColor   ************************************************************
// *****************************************************************************
void String::SetColor(uint32_t tc, uint32_t bgc, bool is_trnsp)
{
  txt_color = tc;
  bg_color = bgc;
  transpatent_bg = is_trnsp;
}

// *****************************************************************************
// ***   SetString   ***********************************************************
// *****************************************************************************
void String::SetString(const char* str)
{
  // Lock object for changes
  LockVisObject();
  //Set new pointer to string
  string = (const uint8_t*)str;
  width = fonts[font_type].w * strlen(str);
  x_end = x_start + width - 1;
  // Unlock object after changes
  UnlockVisObject();
}

// *****************************************************************************
// ***   Put line in buffer   **************************************************
// *****************************************************************************
void String::DrawInBufW(uint16_t* buf, int32_t n, int32_t line, int32_t start_x)
{
  // Draw only if needed
  if((line >= y_start) && (line <= y_end) && (string != nullptr))
  {
    // Current symbol X position
    int32_t x = x_start;
    // Number of bytes need skipped for draw line
    uint32_t skip_bytes = (line - y_start) * fonts[font_type].bytes_per_char / fonts[font_type].h;
    // Pointer to string. Will increment for get characters.
    const uint8_t* str = string;

    // While we have symbols
    while(*str != '\0')
    {
      uint32_t b = 0;
      uint32_t w = 0;
      // Get all symbol line
      for(uint32_t i = 0; i < fonts[font_type].bytes_per_char; i++)
      {
        b |= fonts[font_type].font_data[((uint32_t)(*str)) * fonts[font_type].bytes_per_char + skip_bytes + i] << (i*8);
      }
      // Output symbol line
      while(w < fonts[font_type].w)
      {
        // Put color in buffer only if visible
        if((x >= start_x) && (x < start_x+n))
        {
          if((b&1) == 1)
          {
            buf[x] = txt_color;
          }
          else if(transpatent_bg == false)
          {
            buf[x] = bg_color;
          }
          else
          {
            // Empty statement
          }
        }
        b >>= 1;
        w++;
        x++;
      }
      str++;
    }
  }
}

// *****************************************************************************
// ***   Put line in buffer   **************************************************
// *****************************************************************************
void String::DrawInBufH(uint16_t* buf, int32_t n, int32_t row, int32_t start_y)
{
  // Draw only if needed
  if((row >= x_start) && (row <= x_end) && (string != nullptr))
  {
    // Find line in symbol
    int16_t start = y_start - start_y;
    // Find line in symbol
    int16_t line = (row - x_start);
    
    if(line >= 0)
    {
      // Get symbol
      uint8_t c = string[line / fonts[font_type].w];
      // Find line in symbol
      line %= fonts[font_type].w;
      // Index to symbol in data array
      uint16_t s_idx = c * fonts[font_type].bytes_per_char;
      // Index to symbol in data array
      uint16_t bytes_per_line = fonts[font_type].bytes_per_char / fonts[font_type].h;
      // Get symbols lines
      for(int32_t i = 0; i < fonts[font_type].h; i++)
      {
        uint32_t b = *(uint32_t *)(&fonts[font_type].font_data[s_idx + i*bytes_per_line]);
        if(b & (1U<<line))
        {
          if((start+i > 0) && (start+i < n))
          {
            buf[start+i] = txt_color;
          }
        }
      }
    }
  }
}

// *****************************************************************************
// ***   GetFontW   ************************************************************
// *****************************************************************************
uint32_t String::GetFontW(FontType ft)
{
  // Zero my default
  uint32_t font_w = 0U;
  // If provided valid font number
  if(ft < FONTS_MAX)
  {
    // Get font width
    font_w = fonts[ft].w;
  }
  // Return result
  return font_w;
};

// *****************************************************************************
// ***   GetFontH   ************************************************************
// *****************************************************************************
uint32_t String::GetFontH(FontType ft)
{
  // Zero my default
  uint32_t font_h = 0U;
  // If provided valid font number
  if(ft < FONTS_MAX)
  {
    // Get font height
    font_h = fonts[ft].h;
  }
  // Return result
  return font_h;
};