1
+
2
+ #include < Arduino.h>
3
+ #include < unity.h>
4
+ #include < deque>
5
+ #include < Fonts/OpenSansCyrillicLatin18.h>
6
+ #include < Fonts/RobotoMedium24.h>
7
+ #include < tcUnicodeHelper.h>
8
+ #include < IoLogging.h>
9
+
10
+ class UnitTestPlotter : public TextPlotPipeline {
11
+ private:
12
+ std::deque<Coord> pixelsDrawn;
13
+ static const size_t max_size = 32 ;
14
+ uint32_t col = 0 ;
15
+ Coord where = {0 ,0 };
16
+ public:
17
+ UnitTestPlotter () = default ;
18
+ ~UnitTestPlotter () = default ;
19
+
20
+ void drawPixel (uint16_t x, uint16_t y, uint32_t color) override {
21
+ if (pixelsDrawn.size () == max_size) {
22
+ pixelsDrawn.pop_front ();
23
+ }
24
+ pixelsDrawn.push_back (Coord (x, y));
25
+ }
26
+
27
+ void setCursor (const Coord &p) override {
28
+ where = p;
29
+ }
30
+
31
+ Coord getCursor () override {
32
+ return where;
33
+ }
34
+
35
+ Coord getDimensions () override {
36
+ return Coord (320 , 200 );
37
+ }
38
+
39
+ void init () {
40
+ pixelsDrawn.clear ();
41
+ where = {0 ,0 };
42
+ }
43
+ } unitTestPlotter;
44
+
45
+ UnicodeFontHandler* handler = nullptr ;
46
+
47
+ void setUp () {
48
+ unitTestPlotter.init ();
49
+ handler = new UnicodeFontHandler (&unitTestPlotter, ENCMODE_UTF8);
50
+ handler->setFont (OpenSansCyrillicLatin18);
51
+ handler->setDrawColor (20 );
52
+ }
53
+
54
+ void tearDown () {
55
+ delete handler;
56
+ }
57
+
58
+ bool checkGlyph (uint32_t code, uint32_t bmpOffset, int width, int height, int xAdvance, int xOffset, int yOffset) {
59
+ GlyphWithBitmap glyphWithBitmap;
60
+ if (handler->findCharInFont (code, glyphWithBitmap)) {
61
+ const UnicodeFontGlyph *glyph = glyphWithBitmap.getGlyph ();
62
+ bool success = true ;
63
+ if (bmpOffset != glyph->relativeBmpOffset ) {
64
+ serlogF4 (SER_DEBUG, " Bmp offset out " , code, bmpOffset, glyph->relativeBmpOffset );
65
+ success = false ;
66
+ }
67
+ if (width != glyph->width || height != glyph->height ) {
68
+ serlogF4 (SER_DEBUG, " Bmp width out " , code, width, glyph->width );
69
+ serlogF3 (SER_DEBUG, " Bmp height " , height, glyph->height );
70
+ success = false ;
71
+ }
72
+ if (xOffset != glyph->xOffset || yOffset != glyph->yOffset ) {
73
+ serlogF4 (SER_DEBUG, " Bmp xoffs out " , code, xOffset, glyph->xOffset );
74
+ serlogF3 (SER_DEBUG, " Bmp yoffs " , yOffset, glyph->yOffset );
75
+ success = false ;
76
+ }
77
+ if (xAdvance != glyph->xAdvance ) {
78
+ serlogF4 (SER_DEBUG, " Bmp xadv out " , code, xAdvance, glyph->xAdvance );
79
+ success = false ;
80
+ }
81
+
82
+ return success;
83
+ } else {
84
+ serlogF2 (SER_DEBUG, " Glyph not found " , code);
85
+ return false ;
86
+ }
87
+ }
88
+
89
+ void test_TextExtents () {
90
+ int bl;
91
+ handler->setFont (OpenSansCyrillicLatin18);
92
+ Coord coord = handler->textExtents (" Abc" , &bl, false );
93
+ TEST_ASSERT_EQUAL_INT16 (31 , coord.x );
94
+ TEST_ASSERT_EQUAL_INT16 (24 , coord.y );
95
+
96
+ handler->setFont (RobotoMedium24pt);
97
+ coord = handler->textExtents (" Abc" , &bl, false );
98
+ TEST_ASSERT_EQUAL_INT16 (43 , coord.x );
99
+ TEST_ASSERT_EQUAL_INT16 (28 , coord.y );
100
+ }
101
+
102
+ void test_GetGlyphOnEachRange () {
103
+ TEST_ASSERT_TRUE (checkGlyph (65 , 320 , 11 , 13 , 11 , 0 , -18 ));
104
+ TEST_ASSERT_TRUE (checkGlyph (55 + 128 , 198 , 2 , 2 , 5 , 1 , -12 ));
105
+ TEST_ASSERT_TRUE (checkGlyph (17 + 1024 , 285 , 8 , 13 , 11 , 2 , -18 ));
106
+ }
107
+
108
+ void test_ReadingEveryGlyphInRange () {
109
+ GlyphWithBitmap glyphWithBitmap;
110
+
111
+ // test all known characters work
112
+ for (int i = 32 ; i < 127 ; i++) {
113
+ serlogF2 (SER_DEBUG, " Test character = " , i);
114
+ TEST_ASSERT_TRUE (handler->findCharInFont (i, glyphWithBitmap));
115
+ TEST_ASSERT_NOT_NULL (glyphWithBitmap.getGlyph ());
116
+ TEST_ASSERT_NOT_NULL (glyphWithBitmap.getBitmapData ());
117
+ }
118
+
119
+ // test a few that should fail
120
+ TEST_ASSERT_FALSE (handler->findCharInFont (0 , glyphWithBitmap));
121
+ TEST_ASSERT_FALSE (handler->findCharInFont (5 , glyphWithBitmap));
122
+ TEST_ASSERT_FALSE (handler->findCharInFont (10 , glyphWithBitmap));
123
+ TEST_ASSERT_FALSE (handler->findCharInFont (0xFFFF , glyphWithBitmap));
124
+ }
125
+
126
+ void test_AdafruitFont () {
127
+ GlyphWithBitmap glyphWithBitmap;
128
+ handler->setFont (RobotoMedium24pt);
129
+
130
+ // test a few that should fail
131
+ TEST_ASSERT_TRUE (checkGlyph (32 , 0 , 0 , 0 , 6 , 0 , -28 ));
132
+ TEST_ASSERT_TRUE (checkGlyph (48 , 243 , 12 , 17 , 14 , 1 , -23 ));
133
+ TEST_ASSERT_TRUE (checkGlyph (65 , 611 , 16 , 17 , 16 , 0 , -23 ));
134
+ TEST_ASSERT_TRUE (checkGlyph (126 , 1990 , 14 , 5 , 16 , 1 , -16 ));
135
+ TEST_ASSERT_FALSE (handler->findCharInFont (5 , glyphWithBitmap));
136
+ TEST_ASSERT_FALSE (handler->findCharInFont (127 , glyphWithBitmap));
137
+ TEST_ASSERT_FALSE (handler->findCharInFont (31 , glyphWithBitmap));
138
+ }
139
+
140
+ void setup () {
141
+ UNITY_BEGIN ();
142
+ RUN_TEST (test_TextExtents);
143
+ RUN_TEST (test_GetGlyphOnEachRange);
144
+ RUN_TEST (test_ReadingEveryGlyphInRange);
145
+ RUN_TEST (test_AdafruitFont);
146
+ UNITY_END ();
147
+ }
148
+
149
+ void loop () {}
0 commit comments