Mir

Merge lp:~vanvugt/mir/typo-implement-utf8 into lp:mir

Proposed by Daniel van Vugt
Status: Work in progress
Proposed branch: lp:~vanvugt/mir/typo-implement-utf8
Merge into: lp:mir
Diff against target: 75 lines (+43/-5)
2 files modified
playground/demo-shell/typo/typo_renderer.cpp (+42/-5)
playground/demo-shell/typo/typo_renderer.h (+1/-0)
To merge this branch: bzr merge lp:~vanvugt/mir/typo-implement-utf8
Reviewer Review Type Date Requested Status
Mir development team Pending
Review via email: mp+309918@code.launchpad.net

Commit message

Implement typo::Renderer::unicode_from_utf8

Description of the change

To post a comment you must log in.

Unmerged revisions

3800. By Daniel van Vugt

Implement typo::Renderer::unicode_from_utf8

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'playground/demo-shell/typo/typo_renderer.cpp'
2--- playground/demo-shell/typo/typo_renderer.cpp 2016-10-11 12:51:40 +0000
3+++ playground/demo-shell/typo/typo_renderer.cpp 2016-11-03 07:51:47 +0000
4@@ -18,6 +18,7 @@
5
6 #include "typo_renderer.h"
7 #include <cstring>
8+#include <cstdint>
9
10 using namespace mir::typo;
11
12@@ -49,11 +50,47 @@
13 {
14 }
15
16+const unsigned long Renderer::invalid_unicode = 0x80000000UL;
17+
18+namespace {
19+int utf8_char_len(uint8_t const* c)
20+{
21+ return (c[0] < 0x80) ? 1 :
22+ ((c[0] & 0xE0) == 0xC0) ? 2 :
23+ ((c[0] & 0xF0) == 0xE0) ? 3 :
24+ ((c[0] & 0xF8) == 0xF0) ? 4 : 0;
25+}
26+}
27+
28 unsigned long Renderer::unicode_from_utf8(char const** utf8)
29 {
30- int char_len = 1; // TODO: Add support for non-ASCII UTF-8
31- unsigned long unicode = **utf8;
32- if (unicode)
33- *utf8 += char_len;
34- return unicode;
35+ uint8_t b0 = **(const uint8_t**)utf8;
36+ int len = utf8_char_len(&b0);
37+ unsigned long u;
38+ if (len == 1)
39+ {
40+ u = b0;
41+ }
42+ else if (len > 1)
43+ {
44+ u = b0 & (0x7F >> len);
45+ for (int i = 1; i < len; i++)
46+ {
47+ uint8_t bi = (*(const uint8_t**)utf8)[i];
48+ if ((bi & 0xC0) != 0x80)
49+ {
50+ len = i + 1;
51+ u = invalid_unicode;
52+ break;
53+ }
54+ u = (u << 6) | ((unsigned long)bi & 0x3F);
55+ }
56+ }
57+ else
58+ {
59+ u = invalid_unicode;
60+ len = 1;
61+ }
62+ *utf8 += len;
63+ return u;
64 }
65
66=== modified file 'playground/demo-shell/typo/typo_renderer.h'
67--- playground/demo-shell/typo/typo_renderer.h 2016-10-11 12:51:40 +0000
68+++ playground/demo-shell/typo/typo_renderer.h 2016-11-03 07:51:47 +0000
69@@ -53,6 +53,7 @@
70 virtual void render(char const* str, Image& img) = 0;
71
72 protected:
73+ static const unsigned long invalid_unicode;
74 static unsigned long unicode_from_utf8(char const** utf8);
75 };
76

Subscribers

People subscribed via source and target branches