Numworks Epsilon  1.4.1
Graphing Calculator Operating System
context_text.cpp
Go to the documentation of this file.
1 #include <kandinsky/context.h>
2 #include <kandinsky/text.h>
3 #include "small_font.h"
4 #include "large_font.h"
5 
6 KDColor smallCharacterBuffer[BITMAP_SmallFont_CHARACTER_WIDTH*BITMAP_SmallFont_CHARACTER_HEIGHT];
7 KDColor largeCharacterBuffer[BITMAP_LargeFont_CHARACTER_WIDTH*BITMAP_LargeFont_CHARACTER_HEIGHT];
8 
9 KDPoint KDContext::drawString(const char * text, KDPoint p, KDText::FontSize size, KDColor textColor, KDColor backgroundColor, int maxLength) {
10  return writeString(text, p, size, textColor, backgroundColor, maxLength, false);
11 }
12 
13 KDPoint KDContext::blendString(const char * text, KDPoint p, KDText::FontSize size, KDColor textColor) {
14  return writeString(text, p, size, textColor, KDColorWhite, -1, true);
15 }
16 
17 KDPoint KDContext::writeString(const char * text, KDPoint p, KDText::FontSize size, KDColor textColor, KDColor backgroundColor, int maxLength, bool transparentBackground) {
18  KDPoint position = p;
19  int characterWidth = size == KDText::FontSize::Large ? BITMAP_LargeFont_CHARACTER_WIDTH : BITMAP_SmallFont_CHARACTER_WIDTH;
20  int characterHeight = size == KDText::FontSize::Large ? BITMAP_LargeFont_CHARACTER_HEIGHT: BITMAP_SmallFont_CHARACTER_HEIGHT;
21  KDPoint characterSize(characterWidth, 0);
22 
23  const char * end = text+maxLength;
24  while(*text != 0 && text != end) {
25  writeChar(*text, position, size, textColor, backgroundColor, transparentBackground);
26  if (*text == '\n') {
27  position = KDPoint(0, position.y()+characterHeight);
28  } else if (*text == '\t') {
29  position = position.translatedBy(KDPoint(KDText::k_tabCharacterWidth*characterWidth, 0));
30  } else {
31  position = position.translatedBy(characterSize);
32  }
33  text++;
34  }
35  return position;
36 }
37 
38 void KDContext::writeChar(char character, KDPoint p, KDText::FontSize size, KDColor textColor, KDColor backgroundColor, bool transparentBackground) {
39  if (character == '\n' || character == '\t') {
40  return;
41  }
42  char firstCharacter = size == KDText::FontSize::Large ? BITMAP_LargeFont_FIRST_CHARACTER : BITMAP_SmallFont_FIRST_CHARACTER;
43  int characterHeight = size == KDText::FontSize::Large ? BITMAP_LargeFont_CHARACTER_HEIGHT : BITMAP_SmallFont_CHARACTER_HEIGHT;
44  int characterWidth = size == KDText::FontSize::Large ? BITMAP_LargeFont_CHARACTER_WIDTH : BITMAP_SmallFont_CHARACTER_WIDTH;
45 
47 
48  KDRect absoluteRect = absoluteFillRect(KDRect(p, characterWidth, characterHeight));
49  if (transparentBackground) {
50  pullRect(absoluteRect, characterBuffer);
51  }
52  KDCoordinate startingI = m_clippingRect.x() - p.translatedBy(m_origin).x();
53  KDCoordinate startingJ = m_clippingRect.y() - p.translatedBy(m_origin).y();
54  startingI = startingI < 0 ? 0 : startingI;
55  startingJ = startingJ < 0 ? 0 : startingJ;
56 
57  for (KDCoordinate j=0; j<absoluteRect.height(); j++) {
58  for (KDCoordinate i=0; i<absoluteRect.width(); i++) {
59  KDColor * currentPixelAdress = characterBuffer + i + absoluteRect.width()*j; uint8_t intensity = 0;
60  if (size == KDText::FontSize::Large) {
61  intensity = bitmapLargeFont[(uint8_t)character-(uint8_t)firstCharacter][j + startingJ][i +startingI];
62  } else {
63  intensity = bitmapSmallFont[(uint8_t)character-(uint8_t)firstCharacter][j + startingJ][i +startingI];
64  }
65  KDColor backColor = transparentBackground ? *currentPixelAdress : backgroundColor;
66  *currentPixelAdress = KDColor::blend(textColor, backColor, intensity);
67  }
68  }
69  pushRect(absoluteRect, characterBuffer);
70 }
KDCoordinate y() const
Definition: point.h:11
KDCoordinate x() const
Definition: rect.h:36
KDPoint drawString(const char *text, KDPoint p, KDText::FontSize size=KDText::FontSize::Large, KDColor textColor=KDColorBlack, KDColor backgroundColor=KDColorWhite, int maxLength=-1)
Definition: context_text.cpp:9
int16_t KDCoordinate
Definition: coordinate.h:6
unsigned char uint8_t
Definition: stdint.h:4
KDColor largeCharacterBuffer[BITMAP_LargeFont_CHARACTER_WIDTH *BITMAP_LargeFont_CHARACTER_HEIGHT]
Definition: context_text.cpp:7
KDPoint blendString(const char *text, KDPoint p, KDText::FontSize size, KDColor textColor=KDColorBlack)
Definition: point.h:6
KDColor smallCharacterBuffer[BITMAP_SmallFont_CHARACTER_WIDTH *BITMAP_SmallFont_CHARACTER_HEIGHT]
Definition: context_text.cpp:6
virtual void pullRect(KDRect rect, KDColor *pixels)=0
constexpr KDColor KDColorWhite
Definition: color.h:42
virtual void pushRect(KDRect, const KDColor *pixels)=0
KDCoordinate y() const
Definition: rect.h:37
Definition: rect.h:26
Definition: color.h:6
KDCoordinate width() const
Definition: rect.h:39
KDCoordinate x() const
Definition: point.h:10
KDPoint translatedBy(KDPoint other) const
Definition: point.cpp:3
FontSize
Definition: text.h:10
KDCoordinate height() const
Definition: rect.h:40
static KDColor blend(KDColor first, KDColor second, uint8_t alpha)
Definition: color.cpp:3
static constexpr int k_tabCharacterWidth
Definition: text.h:14