1 /*
2 * Copyright 2020 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include <emscripten.h>
9 #include <emscripten/bind.h>
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkSurface.h"
12 #include "tools/skui/InputState.h"
13 #include "tools/skui/ModifierKey.h"
14 #include "tools/viewer/SKPSlide.h"
15 #include "tools/viewer/SampleSlide.h"
16 #include "tools/viewer/SvgSlide.h"
17 #include <GLES3/gl3.h>
18 #include <string>
19
20 using namespace emscripten;
21
MakeSlide(std::string name)22 static sk_sp<Slide> MakeSlide(std::string name) {
23 if (name == "PathText") {
24 extern Sample* MakePathTextSample();
25 return sk_make_sp<SampleSlide>(MakePathTextSample);
26 }
27 if (name == "TessellatedWedge") {
28 extern Sample* MakeTessellatedWedgeSample();
29 return sk_make_sp<SampleSlide>(MakeTessellatedWedgeSample);
30 }
31 return nullptr;
32 }
33
MakeSkpSlide(std::string name,std::string skpData)34 static sk_sp<Slide> MakeSkpSlide(std::string name, std::string skpData) {
35 auto stream = std::make_unique<SkMemoryStream>(skpData.data(), skpData.size(),
36 /*copyData=*/true);
37 return sk_make_sp<SKPSlide>(SkString(name.c_str()), std::move(stream));
38 }
39
MakeSvgSlide(std::string name,std::string svgText)40 static sk_sp<Slide> MakeSvgSlide(std::string name, std::string svgText) {
41 auto stream = std::make_unique<SkMemoryStream>(svgText.data(), svgText.size(),
42 /*copyData=*/true);
43 return sk_make_sp<SvgSlide>(SkString(name.c_str()), std::move(stream));
44 }
45
EMSCRIPTEN_BINDINGS(Viewer)46 EMSCRIPTEN_BINDINGS(Viewer) {
47 function("MakeSlide", &MakeSlide);
48 function("MakeSkpSlide", &MakeSkpSlide);
49 function("MakeSvgSlide", &MakeSvgSlide);
50 class_<Slide>("Slide")
51 .smart_ptr<sk_sp<Slide>>("sk_sp<Slide>")
52 .function("load", &Slide::load)
53 .function("animate", &Slide::animate)
54 .function("draw", optional_override([](Slide& self, SkCanvas& canvas) {
55 self.draw(&canvas);
56 }))
57 .function("onChar", &Slide::onChar)
58 .function("onMouse", &Slide::onMouse);
59 enum_<skui::InputState>("InputState")
60 .value("Down", skui::InputState::kDown)
61 .value("Up", skui::InputState::kUp)
62 .value("Move", skui::InputState::kMove)
63 .value("Right", skui::InputState::kRight)
64 .value("Left", skui::InputState::kLeft);
65 enum_<skui::ModifierKey>("ModifierKey")
66 .value("None", skui::ModifierKey::kNone)
67 .value("Shift", skui::ModifierKey::kShift)
68 .value("Control", skui::ModifierKey::kControl)
69 .value("Option", skui::ModifierKey::kOption)
70 .value("Command", skui::ModifierKey::kCommand)
71 .value("FirstPress", skui::ModifierKey::kFirstPress);
72 }
73