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
|
use ui;
use core:geometry;
use graphics;
class TextWindow extends Frame {
TextPainter p;
init() {
init("Text", Size(300, 400)) {}
painter = p;
create();
}
Bool onClick(Bool pressed, Point at, MouseButton button) {
if (pressed) {
if (button == MouseButton:right) {
var iter = p.text.text.begin;
p.text.color(iter, iter + 1, blue);
repaint();
} else {
p.setFont(Font("Courier", 20));
}
}
return true;
}
}
class TextPainter extends Painter {
Text text;
Brush color;
Brush hl;
Brush[] bg;
Text[] lines;
Float[] baseline;
Rect[] rects;
init() {
Font f("Arial", 25);
Font f2("Arial", 20);
f2.italic = true;
f2.underline = true;
// f2.weight = 700;
init() {
text = Text("ABÅÄあお☃😀!\n\tà̖\n👩💻\nLast line", f);
color = SolidBrush(black);
hl = SolidBrush(red);
}
{
var iter = text.text.begin();
text.color(iter + 3, iter + 6, green);
text.color(iter + 2, iter + 4, red);
text.underline(iter + 1, iter + 4);
text.underline(iter, iter + 2);
text.italic(iter, iter + 3);
}
bg << SolidBrush(green + white * 0.6);
bg << SolidBrush(red + white * 0.6);
bg << SolidBrush(blue + white * 0.6);
bg << SolidBrush(yellow);
for (l in text.lineInfo) {
lines << Text(l.text, f2);
baseline << l.baseline;
}
updateBoxes();
}
void setFont(Font f) {
text = Text(text.text, f);
updateBoxes();
repaint();
}
void updateBoxes() {
baseline.clear();
for (l in text.lineInfo) {
baseline << l.baseline;
}
var iter = text.text.begin();
var end = text.text.end();
rects.clear();
while (iter != end) {
var next = iter;
next++;
if (iter.v != Char(10)) {
var bounds = text.boundsOf(iter, next);
if (bounds.any)
rects << bounds[0];
else
print("Empty bounds for ${iter.v}");
}
iter = next;
}
}
Bool render(Size size, Graphics g) {
Point offset(8, 8);
Nat colorId = 0;
for (r in rects) {
g.fill(r + offset, bg[colorId]);
if (++colorId == bg.count)
colorId = 0;
}
for (b in baseline) {
g.line(Point(0, offset.y + b), Point(size.w, offset.y + b), hl);
}
g.draw(text, color, offset);
offset.y += text.size.h + 8;
for (l in lines) {
g.draw(l, color, offset);
offset.y += l.size.h;
}
false;
}
}
void text() {
TextWindow window;
window.waitForClose();
}
|