This repository has been archived by the owner on Aug 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DocumentRender.cpp
154 lines (115 loc) · 4.16 KB
/
DocumentRender.cpp
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "DocumentRender.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkData.h"
#include "include/core/SkImage.h"
#include "include/core/SkRRect.h"
#include "include/core/SkFontMgr.h"
#include "include/core/SkTextBlob.h"
#include <codecvt>
#include <vector>
#include <locale>
#if _MSC_VER >= 1900
std::u32string
convert_to_utf32(std::string text)
{
std::wstring_convert<std::codecvt_utf8<int32_t>, int32_t> utf32conv;
auto intstr = utf32conv.from_bytes(text);
return std::u32string(intstr.begin(), intstr.end());
}
#else
std::u32string
convert_to_utf32(std::string text)
{
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> utf32conv;
return utf32conv.from_bytes(text);
}
#endif
struct GlyphDesc
{
SkGlyphID id;
SkFont font;
SkScalar width;
SkRect bounds;
};
DocumentRender::DocumentRender(std::unique_ptr<IFontFactory> fontFactory)
: reader_(std::move(fontFactory))
{}
void
DocumentRender::renderNode(SkCanvas& canvas, YGNodeRef node)
{
canvas.save();
reader_.saveDrawParams();
canvas.translate(YGNodeLayoutGetLeft(node), YGNodeLayoutGetTop(node));
SkRect rect = SkRect::MakeXYWH(
0, 0, YGNodeLayoutGetWidth(node), YGNodeLayoutGetHeight(node));
json::Value* jsonNode = static_cast<json::Value*>(YGNodeGetContext(node));
if (jsonNode) {
if (jsonNode->HasMember(NodeAttributes::image)) {
SkPaint paint;
paint.setFilterQuality(kHigh_SkFilterQuality);
paint.setAntiAlias(true);
auto image = reader_.readImage(*jsonNode);
canvas.drawImageRect(image, rect, &paint);
}
SkPaint& paint = reader_.currentDrawParams().paint;
SkFont& font = reader_.currentDrawParams().font;
readStyle(paint, font, *jsonNode, node, canvas, rect);
if (jsonNode->HasMember(NodeAttributes::text)) {
auto text= reader_.readStringText(* jsonNode);
auto textBlob = reader_.readText(font, *jsonNode);
paint.setBlendMode(SkBlendMode::kSrc);
std::u32string utf32 = convert_to_utf32(text);
std::vector<GlyphDesc> glyphs(utf32.size());
sk_sp<SkFontMgr> mgr(SkFontMgr::RefDefault());
for (size_t i = 0; i < glyphs.size(); ++i) {
glyphs[i].id = font.unicharToGlyph(utf32[i]);
glyphs[i].font = font;
if (glyphs[i].id == 0) {
sk_sp<SkTypeface> fallback(mgr->matchFamilyStyleCharacter(
nullptr, font.getTypeface()->fontStyle(), nullptr, 0, utf32[i]));
glyphs[i].font.setTypeface(fallback);
glyphs[i].id = glyphs[i].font.unicharToGlyph(utf32[i]);
}
glyphs[i].font.getWidthsBounds(&glyphs[i].id, 1, &glyphs[i].width, &glyphs[i].bounds, &paint);
}
SkScalar offset = 0;
for (size_t i = 0; i < utf32.size(); ++i) {
canvas.drawSimpleText(&utf32[i],
sizeof(utf32[i]),
SkTextEncoding::kUTF32,
offset - textBlob->bounds().fLeft,
-textBlob->bounds().fTop,
glyphs[i].font,
paint);
offset += glyphs[i].width;
}
/*canvas.drawTextBlob(
textBlob, -textBlob->bounds().fLeft, -textBlob->bounds().fTop, paint);*/
}
}
for (int i = 0; i < YGNodeGetChildCount(node); ++i)
renderNode(canvas, YGNodeGetChild(node, i));
reader_.restoreDrawParams();
canvas.restore();
}
void
DocumentRender::readStyle(SkPaint& paint,
SkFont& font,
rapidjson::Value& jsonNode,
const YGNodeRef& node,
SkCanvas& canvas,
const SkRect& rect)
{
if (!jsonNode.HasMember(NodeAttributes::style))
return;
auto& style = jsonNode[NodeAttributes::style];
reader_.readPaintAttributes(paint, style);
reader_.readFont(font, style);
if (style.HasMember(NodeAttributes::backgroundGradient)) {
SkPaint gradient;
reader_.readGradient(gradient, style,
YGNodeLayoutGetWidth(node),
YGNodeLayoutGetHeight(node));
canvas.drawRect(rect, gradient);
}
}