WHY_CPP  0.1
text.cpp
1 #include <whycpp/drawing.h>
2 #include <whycpp/text.h>
3 #include <whycpp/types.h>
4 #include "../context.h"
5 #include "../holders/fonts_holder.h"
6 
7 void Print(Context &ctx, char ch, i32 x, i32 y, const RGBA &color, i32 size) {
8  auto font = ctx.Get<FontsHolder>()->GetFont();
9  for (uint8_t row = 0; row < Glyph::SIZE; row++) {
10  for (uint8_t col = 0; col < 8; col++) {
11  if (((font->At(ch, row) >> col) & 0x1) == 1) {
12  for (i32 i = 0; i < size; i++) {
13  for (i32 j = 0; j < size; j++) {
14  SetPixel(ctx, x + col * size + i, y + row * size + j, color);
15  }
16  }
17  }
18  }
19  }
20 }
21 
22 void Print(Context &ctx, const std::string &str, i32 x, i32 y, const RGBA &color, i32 size, i32 spacing) {
23  auto font = ctx.Get<FontsHolder>()->GetFont();
24  // TODO: add \n, \t, \r and other escape symbols support
25  i32 x_ = x;
26  i32 y_ = y;
27 
28  for (char ch : str) {
29  // TODO: it should be state machine, but .....
30  if (ch == '\n') {
31  y_ += (font->GetHeight() + font->GetSpacing() + spacing) * size;
32  x_ = x;
33  continue;
34  } else if (ch == '\r') {
35  x_ = x;
36  continue;
37  }
38 
39  Print(ctx, ch, x_, y_, color, size);
40  x_ += (font->GetWidth() + font->GetSpacing()) * size;
41  }
42 }
Definition: color.h:11
void SetPixel(Context &ctx, i32 x, i32 y, const RGBA &color)
Definition: drawing.cpp:16
void Print(Context &ctx, char ch, i32 x, i32 y, const RGBA &color, i32 size)
Definition: text.cpp:7