Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | |
Kevin Lubick | 6b921b7 | 2019-09-18 16:18:17 -0400 | [diff] [blame] | 8 | #include "include/android/SkAnimatedImage.h" |
| 9 | #include "include/codec/SkAndroidCodec.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkBlendMode.h" |
| 11 | #include "include/core/SkBlurTypes.h" |
| 12 | #include "include/core/SkCanvas.h" |
| 13 | #include "include/core/SkColor.h" |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 14 | #include "include/core/SkColorFilter.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "include/core/SkData.h" |
Kevin Lubick | 6b921b7 | 2019-09-18 16:18:17 -0400 | [diff] [blame] | 16 | #include "include/core/SkDrawable.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "include/core/SkEncodedImageFormat.h" |
| 18 | #include "include/core/SkFilterQuality.h" |
| 19 | #include "include/core/SkFont.h" |
| 20 | #include "include/core/SkFontMgr.h" |
| 21 | #include "include/core/SkFontTypes.h" |
| 22 | #include "include/core/SkImage.h" |
Kevin Lubick | 15b4023 | 2019-10-29 09:55:39 -0400 | [diff] [blame] | 23 | #include "include/core/SkImageFilter.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "include/core/SkImageInfo.h" |
| 25 | #include "include/core/SkMaskFilter.h" |
| 26 | #include "include/core/SkPaint.h" |
| 27 | #include "include/core/SkPath.h" |
| 28 | #include "include/core/SkPathEffect.h" |
| 29 | #include "include/core/SkPathMeasure.h" |
| 30 | #include "include/core/SkPicture.h" |
| 31 | #include "include/core/SkPictureRecorder.h" |
Kevin Lubick | 2e5fe35 | 2019-09-03 12:59:06 -0400 | [diff] [blame] | 32 | #include "include/core/SkRRect.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 33 | #include "include/core/SkScalar.h" |
| 34 | #include "include/core/SkShader.h" |
| 35 | #include "include/core/SkString.h" |
| 36 | #include "include/core/SkStrokeRec.h" |
| 37 | #include "include/core/SkSurface.h" |
| 38 | #include "include/core/SkSurfaceProps.h" |
| 39 | #include "include/core/SkTextBlob.h" |
| 40 | #include "include/core/SkTypeface.h" |
| 41 | #include "include/core/SkTypes.h" |
| 42 | #include "include/core/SkVertices.h" |
| 43 | #include "include/effects/SkCornerPathEffect.h" |
| 44 | #include "include/effects/SkDashPathEffect.h" |
| 45 | #include "include/effects/SkDiscretePathEffect.h" |
| 46 | #include "include/effects/SkGradientShader.h" |
Kevin Lubick | 15b4023 | 2019-10-29 09:55:39 -0400 | [diff] [blame] | 47 | #include "include/effects/SkImageFilters.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 48 | #include "include/effects/SkTrimPathEffect.h" |
| 49 | #include "include/pathops/SkPathOps.h" |
| 50 | #include "include/utils/SkParsePath.h" |
| 51 | #include "include/utils/SkShadowUtils.h" |
| 52 | #include "modules/skshaper/include/SkShaper.h" |
| 53 | #include "src/core/SkFontMgrPriv.h" |
| 54 | #include "src/core/SkMakeUnique.h" |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 55 | |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 56 | #include <iostream> |
| 57 | #include <string> |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 58 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 59 | #include "modules/canvaskit/WasmAliases.h" |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 60 | #include <emscripten.h> |
| 61 | #include <emscripten/bind.h> |
Kevin Lubick | 4683942 | 2019-01-03 14:27:27 -0500 | [diff] [blame] | 62 | |
Kevin Lubick | 53965c9 | 2018-10-11 08:51:55 -0400 | [diff] [blame] | 63 | #if SK_SUPPORT_GPU |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 64 | #include "include/gpu/GrBackendSurface.h" |
| 65 | #include "include/gpu/GrContext.h" |
| 66 | #include "include/gpu/gl/GrGLInterface.h" |
| 67 | #include "include/gpu/gl/GrGLTypes.h" |
Kevin Lubick | 3f67f41 | 2019-03-11 16:11:58 -0400 | [diff] [blame] | 68 | |
Kevin Lubick | 53965c9 | 2018-10-11 08:51:55 -0400 | [diff] [blame] | 69 | #include <GL/gl.h> |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 70 | #include <emscripten/html5.h> |
Kevin Lubick | 53965c9 | 2018-10-11 08:51:55 -0400 | [diff] [blame] | 71 | #endif |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 72 | |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 73 | #ifdef SK_INCLUDE_PARAGRAPH |
| 74 | #include "modules/skparagraph/include/Paragraph.h" |
| 75 | #endif |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 76 | // Aliases for less typing |
| 77 | using BoneIndices = SkVertices::BoneIndices; |
| 78 | using BoneWeights = SkVertices::BoneWeights; |
| 79 | using Bone = SkVertices::Bone; |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 80 | |
Kevin Lubick | 61887c7 | 2019-09-26 13:20:50 -0400 | [diff] [blame] | 81 | #ifndef SK_NO_FONTS |
| 82 | sk_sp<SkFontMgr> SkFontMgr_New_Custom_Data(const uint8_t** datas, const size_t* sizes, int n); |
| 83 | #endif |
| 84 | |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 85 | struct SimpleMatrix { |
| 86 | SkScalar scaleX, skewX, transX; |
| 87 | SkScalar skewY, scaleY, transY; |
| 88 | SkScalar pers0, pers1, pers2; |
| 89 | }; |
| 90 | |
| 91 | SkMatrix toSkMatrix(const SimpleMatrix& sm) { |
| 92 | return SkMatrix::MakeAll(sm.scaleX, sm.skewX , sm.transX, |
| 93 | sm.skewY , sm.scaleY, sm.transY, |
| 94 | sm.pers0 , sm.pers1 , sm.pers2); |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 95 | } |
| 96 | |
Kevin Lubick | 5d5723c | 2018-12-07 10:09:11 -0500 | [diff] [blame] | 97 | SimpleMatrix toSimpleSkMatrix(const SkMatrix& sm) { |
| 98 | SimpleMatrix m {sm[0], sm[1], sm[2], |
| 99 | sm[3], sm[4], sm[5], |
| 100 | sm[6], sm[7], sm[8]}; |
| 101 | return m; |
| 102 | } |
| 103 | |
Kevin Lubick | ea905ec | 2018-11-30 14:05:58 -0500 | [diff] [blame] | 104 | struct SimpleImageInfo { |
| 105 | int width; |
| 106 | int height; |
| 107 | SkColorType colorType; |
| 108 | SkAlphaType alphaType; |
| 109 | // TODO color spaces? |
| 110 | }; |
| 111 | |
| 112 | SkImageInfo toSkImageInfo(const SimpleImageInfo& sii) { |
| 113 | return SkImageInfo::Make(sii.width, sii.height, sii.colorType, sii.alphaType); |
| 114 | } |
| 115 | |
Kevin Lubick | 543f352 | 2019-03-08 10:04:28 -0500 | [diff] [blame] | 116 | #if SK_SUPPORT_GPU |
| 117 | sk_sp<GrContext> MakeGrContext(EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context) |
| 118 | { |
| 119 | EMSCRIPTEN_RESULT r = emscripten_webgl_make_context_current(context); |
| 120 | if (r < 0) { |
| 121 | printf("failed to make webgl context current %d\n", r); |
| 122 | return nullptr; |
| 123 | } |
| 124 | // setup GrContext |
| 125 | auto interface = GrGLMakeNativeInterface(); |
| 126 | // setup contexts |
| 127 | sk_sp<GrContext> grContext(GrContext::MakeGL(interface)); |
| 128 | return grContext; |
| 129 | } |
| 130 | |
| 131 | sk_sp<SkSurface> MakeOnScreenGLSurface(sk_sp<GrContext> grContext, int width, int height) { |
| 132 | glClearColor(0, 0, 0, 0); |
| 133 | glClearStencil(0); |
| 134 | glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
| 135 | |
| 136 | |
| 137 | // Wrap the frame buffer object attached to the screen in a Skia render |
| 138 | // target so Skia can render to it |
| 139 | GrGLint buffer; |
| 140 | glGetIntegerv(GL_FRAMEBUFFER_BINDING, &buffer); |
| 141 | GrGLFramebufferInfo info; |
| 142 | info.fFBOID = (GrGLuint) buffer; |
| 143 | SkColorType colorType; |
| 144 | |
| 145 | info.fFormat = GL_RGBA8; |
| 146 | colorType = kRGBA_8888_SkColorType; |
| 147 | |
| 148 | GrBackendRenderTarget target(width, height, 0, 8, info); |
| 149 | |
| 150 | sk_sp<SkSurface> surface(SkSurface::MakeFromBackendRenderTarget(grContext.get(), target, |
| 151 | kBottomLeft_GrSurfaceOrigin, |
| 152 | colorType, nullptr, nullptr)); |
| 153 | return surface; |
| 154 | } |
| 155 | |
| 156 | sk_sp<SkSurface> MakeRenderTarget(sk_sp<GrContext> grContext, int width, int height) { |
| 157 | SkImageInfo info = SkImageInfo::MakeN32(width, height, SkAlphaType::kPremul_SkAlphaType); |
| 158 | |
| 159 | sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(grContext.get(), |
| 160 | SkBudgeted::kYes, |
| 161 | info, 0, |
| 162 | kBottomLeft_GrSurfaceOrigin, |
| 163 | nullptr, true)); |
| 164 | return surface; |
| 165 | } |
| 166 | |
| 167 | sk_sp<SkSurface> MakeRenderTarget(sk_sp<GrContext> grContext, SimpleImageInfo sii) { |
| 168 | sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(grContext.get(), |
| 169 | SkBudgeted::kYes, |
| 170 | toSkImageInfo(sii), 0, |
| 171 | kBottomLeft_GrSurfaceOrigin, |
| 172 | nullptr, true)); |
| 173 | return surface; |
| 174 | } |
| 175 | #endif |
| 176 | |
| 177 | |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 178 | //======================================================================================== |
| 179 | // Path things |
| 180 | //======================================================================================== |
| 181 | |
| 182 | // All these Apply* methods are simple wrappers to avoid returning an object. |
| 183 | // The default WASM bindings produce code that will leak if a return value |
| 184 | // isn't assigned to a JS variable and has delete() called on it. |
| 185 | // These Apply methods, combined with the smarter binding code allow for chainable |
| 186 | // commands that don't leak if the return value is ignored (i.e. when used intuitively). |
| 187 | |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 188 | void ApplyAddArc(SkPath& orig, const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle) { |
| 189 | orig.addArc(oval, startAngle, sweepAngle); |
| 190 | } |
| 191 | |
Kevin Lubick | e384df4 | 2019-08-26 15:48:09 -0400 | [diff] [blame] | 192 | void ApplyAddOval(SkPath& orig, const SkRect& oval, bool ccw, unsigned start) { |
| 193 | orig.addOval(oval, ccw ? SkPath::Direction::kCCW_Direction : |
| 194 | SkPath::Direction::kCW_Direction, start); |
| 195 | } |
| 196 | |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 197 | void ApplyAddPath(SkPath& orig, const SkPath& newPath, |
| 198 | SkScalar scaleX, SkScalar skewX, SkScalar transX, |
| 199 | SkScalar skewY, SkScalar scaleY, SkScalar transY, |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 200 | SkScalar pers0, SkScalar pers1, SkScalar pers2, |
| 201 | bool extendPath) { |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 202 | SkMatrix m = SkMatrix::MakeAll(scaleX, skewX , transX, |
| 203 | skewY , scaleY, transY, |
| 204 | pers0 , pers1 , pers2); |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 205 | orig.addPath(newPath, m, extendPath ? SkPath::kExtend_AddPathMode : |
| 206 | SkPath::kAppend_AddPathMode); |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 207 | } |
| 208 | |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 209 | void ApplyAddRect(SkPath& path, SkScalar left, SkScalar top, |
| 210 | SkScalar right, SkScalar bottom, bool ccw) { |
| 211 | path.addRect(left, top, right, bottom, |
Kevin Lubick | 1646e7d | 2018-12-07 13:03:08 -0500 | [diff] [blame] | 212 | ccw ? SkPath::Direction::kCCW_Direction : |
| 213 | SkPath::Direction::kCW_Direction); |
Alexander Khovansky | 3e11933 | 2018-11-15 02:01:19 +0300 | [diff] [blame] | 214 | } |
| 215 | |
Kevin Lubick | da3d8ac | 2019-01-07 11:08:55 -0500 | [diff] [blame] | 216 | void ApplyAddRoundRect(SkPath& path, SkScalar left, SkScalar top, |
| 217 | SkScalar right, SkScalar bottom, uintptr_t /* SkScalar* */ rPtr, |
| 218 | bool ccw) { |
| 219 | // See comment below for uintptr_t explanation |
| 220 | const SkScalar* radii = reinterpret_cast<const SkScalar*>(rPtr); |
| 221 | path.addRoundRect(SkRect::MakeLTRB(left, top, right, bottom), radii, |
| 222 | ccw ? SkPath::Direction::kCCW_Direction : SkPath::Direction::kCW_Direction); |
| 223 | } |
| 224 | |
| 225 | |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 226 | void ApplyArcTo(SkPath& p, SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, |
| 227 | SkScalar radius) { |
| 228 | p.arcTo(x1, y1, x2, y2, radius); |
| 229 | } |
| 230 | |
Kevin Lubick | 1646e7d | 2018-12-07 13:03:08 -0500 | [diff] [blame] | 231 | void ApplyArcToAngle(SkPath& p, SkRect& oval, SkScalar startAngle, SkScalar sweepAngle, bool forceMoveTo) { |
| 232 | p.arcTo(oval, startAngle, sweepAngle, forceMoveTo); |
| 233 | } |
| 234 | |
Kevin Lubick | 79b7134 | 2019-11-01 14:36:52 -0400 | [diff] [blame] | 235 | void ApplyArcToArcSize(SkPath& orig, SkScalar rx, SkScalar ry, SkScalar xAxisRotate, |
| 236 | bool useSmallArc, bool ccw, SkScalar x, SkScalar y) { |
Kevin Lubick | e384df4 | 2019-08-26 15:48:09 -0400 | [diff] [blame] | 237 | auto arcSize = useSmallArc ? SkPath::ArcSize::kSmall_ArcSize : SkPath::ArcSize::kLarge_ArcSize; |
| 238 | auto sweep = ccw ? SkPath::Direction::kCCW_Direction : SkPath::Direction::kCW_Direction; |
| 239 | orig.arcTo(rx, ry, xAxisRotate, arcSize, sweep, x, y); |
| 240 | } |
| 241 | |
Kevin Lubick | 79b7134 | 2019-11-01 14:36:52 -0400 | [diff] [blame] | 242 | void ApplyRArcToArcSize(SkPath& orig, SkScalar rx, SkScalar ry, SkScalar xAxisRotate, |
| 243 | bool useSmallArc, bool ccw, SkScalar dx, SkScalar dy) { |
| 244 | auto arcSize = useSmallArc ? SkPath::ArcSize::kSmall_ArcSize : SkPath::ArcSize::kLarge_ArcSize; |
| 245 | auto sweep = ccw ? SkPath::Direction::kCCW_Direction : SkPath::Direction::kCW_Direction; |
| 246 | orig.rArcTo(rx, ry, xAxisRotate, arcSize, sweep, dx, dy); |
| 247 | } |
| 248 | |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 249 | void ApplyClose(SkPath& p) { |
| 250 | p.close(); |
| 251 | } |
| 252 | |
| 253 | void ApplyConicTo(SkPath& p, SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, |
| 254 | SkScalar w) { |
| 255 | p.conicTo(x1, y1, x2, y2, w); |
| 256 | } |
| 257 | |
Kevin Lubick | 79b7134 | 2019-11-01 14:36:52 -0400 | [diff] [blame] | 258 | void ApplyRConicTo(SkPath& p, SkScalar dx1, SkScalar dy1, SkScalar dx2, SkScalar dy2, |
| 259 | SkScalar w) { |
| 260 | p.rConicTo(dx1, dy1, dx2, dy2, w); |
| 261 | } |
| 262 | |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 263 | void ApplyCubicTo(SkPath& p, SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, |
| 264 | SkScalar x3, SkScalar y3) { |
| 265 | p.cubicTo(x1, y1, x2, y2, x3, y3); |
| 266 | } |
| 267 | |
Kevin Lubick | 79b7134 | 2019-11-01 14:36:52 -0400 | [diff] [blame] | 268 | void ApplyRCubicTo(SkPath& p, SkScalar dx1, SkScalar dy1, SkScalar dx2, SkScalar dy2, |
| 269 | SkScalar dx3, SkScalar dy3) { |
| 270 | p.rCubicTo(dx1, dy1, dx2, dy2, dx3, dy3); |
| 271 | } |
| 272 | |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 273 | void ApplyLineTo(SkPath& p, SkScalar x, SkScalar y) { |
| 274 | p.lineTo(x, y); |
| 275 | } |
| 276 | |
Kevin Lubick | 79b7134 | 2019-11-01 14:36:52 -0400 | [diff] [blame] | 277 | void ApplyRLineTo(SkPath& p, SkScalar dx, SkScalar dy) { |
| 278 | p.rLineTo(dx, dy); |
| 279 | } |
| 280 | |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 281 | void ApplyMoveTo(SkPath& p, SkScalar x, SkScalar y) { |
| 282 | p.moveTo(x, y); |
| 283 | } |
| 284 | |
Kevin Lubick | 79b7134 | 2019-11-01 14:36:52 -0400 | [diff] [blame] | 285 | void ApplyRMoveTo(SkPath& p, SkScalar dx, SkScalar dy) { |
| 286 | p.rMoveTo(dx, dy); |
| 287 | } |
| 288 | |
Kevin Lubick | da3d8ac | 2019-01-07 11:08:55 -0500 | [diff] [blame] | 289 | void ApplyReset(SkPath& p) { |
| 290 | p.reset(); |
| 291 | } |
| 292 | |
| 293 | void ApplyRewind(SkPath& p) { |
| 294 | p.rewind(); |
| 295 | } |
| 296 | |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 297 | void ApplyQuadTo(SkPath& p, SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) { |
| 298 | p.quadTo(x1, y1, x2, y2); |
| 299 | } |
| 300 | |
Kevin Lubick | 79b7134 | 2019-11-01 14:36:52 -0400 | [diff] [blame] | 301 | void ApplyRQuadTo(SkPath& p, SkScalar dx1, SkScalar dy1, SkScalar dx2, SkScalar dy2) { |
| 302 | p.rQuadTo(dx1, dy1, dx2, dy2); |
| 303 | } |
| 304 | |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 305 | void ApplyTransform(SkPath& orig, |
| 306 | SkScalar scaleX, SkScalar skewX, SkScalar transX, |
| 307 | SkScalar skewY, SkScalar scaleY, SkScalar transY, |
| 308 | SkScalar pers0, SkScalar pers1, SkScalar pers2) { |
| 309 | SkMatrix m = SkMatrix::MakeAll(scaleX, skewX , transX, |
| 310 | skewY , scaleY, transY, |
| 311 | pers0 , pers1 , pers2); |
| 312 | orig.transform(m); |
| 313 | } |
| 314 | |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 315 | bool EMSCRIPTEN_KEEPALIVE ApplySimplify(SkPath& path) { |
| 316 | return Simplify(path, &path); |
| 317 | } |
| 318 | |
| 319 | bool EMSCRIPTEN_KEEPALIVE ApplyPathOp(SkPath& pathOne, const SkPath& pathTwo, SkPathOp op) { |
| 320 | return Op(pathOne, pathTwo, op, &pathOne); |
| 321 | } |
| 322 | |
| 323 | JSString EMSCRIPTEN_KEEPALIVE ToSVGString(const SkPath& path) { |
| 324 | SkString s; |
| 325 | SkParsePath::ToSVGString(path, &s); |
| 326 | return emscripten::val(s.c_str()); |
| 327 | } |
| 328 | |
Kevin Lubick | a40f832 | 2018-12-17 16:01:36 -0500 | [diff] [blame] | 329 | SkPathOrNull EMSCRIPTEN_KEEPALIVE MakePathFromSVGString(std::string str) { |
| 330 | SkPath path; |
| 331 | if (SkParsePath::FromSVGString(str.c_str(), &path)) { |
| 332 | return emscripten::val(path); |
| 333 | } |
| 334 | return emscripten::val::null(); |
| 335 | } |
| 336 | |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 337 | SkPathOrNull EMSCRIPTEN_KEEPALIVE MakePathFromOp(const SkPath& pathOne, const SkPath& pathTwo, SkPathOp op) { |
| 338 | SkPath out; |
| 339 | if (Op(pathOne, pathTwo, op, &out)) { |
| 340 | return emscripten::val(out); |
| 341 | } |
| 342 | return emscripten::val::null(); |
| 343 | } |
| 344 | |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 345 | SkPath EMSCRIPTEN_KEEPALIVE CopyPath(const SkPath& a) { |
| 346 | SkPath copy(a); |
| 347 | return copy; |
| 348 | } |
| 349 | |
| 350 | bool EMSCRIPTEN_KEEPALIVE Equals(const SkPath& a, const SkPath& b) { |
| 351 | return a == b; |
| 352 | } |
| 353 | |
Kevin Lubick | da3d8ac | 2019-01-07 11:08:55 -0500 | [diff] [blame] | 354 | // ================================================================================= |
| 355 | // Creating/Exporting Paths with cmd arrays |
| 356 | // ================================================================================= |
| 357 | |
| 358 | static const int MOVE = 0; |
| 359 | static const int LINE = 1; |
| 360 | static const int QUAD = 2; |
| 361 | static const int CONIC = 3; |
| 362 | static const int CUBIC = 4; |
| 363 | static const int CLOSE = 5; |
| 364 | |
| 365 | template <typename VisitFunc> |
| 366 | void VisitPath(const SkPath& p, VisitFunc&& f) { |
| 367 | SkPath::RawIter iter(p); |
| 368 | SkPoint pts[4]; |
| 369 | SkPath::Verb verb; |
| 370 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 371 | f(verb, pts, iter); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | JSArray EMSCRIPTEN_KEEPALIVE ToCmds(const SkPath& path) { |
| 376 | JSArray cmds = emscripten::val::array(); |
| 377 | |
| 378 | VisitPath(path, [&cmds](SkPath::Verb verb, const SkPoint pts[4], SkPath::RawIter iter) { |
| 379 | JSArray cmd = emscripten::val::array(); |
| 380 | switch (verb) { |
| 381 | case SkPath::kMove_Verb: |
| 382 | cmd.call<void>("push", MOVE, pts[0].x(), pts[0].y()); |
| 383 | break; |
| 384 | case SkPath::kLine_Verb: |
| 385 | cmd.call<void>("push", LINE, pts[1].x(), pts[1].y()); |
| 386 | break; |
| 387 | case SkPath::kQuad_Verb: |
| 388 | cmd.call<void>("push", QUAD, pts[1].x(), pts[1].y(), pts[2].x(), pts[2].y()); |
| 389 | break; |
| 390 | case SkPath::kConic_Verb: |
| 391 | cmd.call<void>("push", CONIC, |
| 392 | pts[1].x(), pts[1].y(), |
| 393 | pts[2].x(), pts[2].y(), iter.conicWeight()); |
| 394 | break; |
| 395 | case SkPath::kCubic_Verb: |
| 396 | cmd.call<void>("push", CUBIC, |
| 397 | pts[1].x(), pts[1].y(), |
| 398 | pts[2].x(), pts[2].y(), |
| 399 | pts[3].x(), pts[3].y()); |
| 400 | break; |
| 401 | case SkPath::kClose_Verb: |
| 402 | cmd.call<void>("push", CLOSE); |
| 403 | break; |
| 404 | case SkPath::kDone_Verb: |
| 405 | SkASSERT(false); |
| 406 | break; |
| 407 | } |
| 408 | cmds.call<void>("push", cmd); |
| 409 | }); |
| 410 | return cmds; |
| 411 | } |
| 412 | |
| 413 | // This type signature is a mess, but it's necessary. See, we can't use "bind" (EMSCRIPTEN_BINDINGS) |
| 414 | // and pointers to primitive types (Only bound types like SkPoint). We could if we used |
| 415 | // cwrap (see https://becominghuman.ai/passing-and-returning-webassembly-array-parameters-a0f572c65d97) |
| 416 | // but that requires us to stick to C code and, AFAIK, doesn't allow us to return nice things like |
| 417 | // SkPath or SkOpBuilder. |
| 418 | // |
Kevin Lubick | 61887c7 | 2019-09-26 13:20:50 -0400 | [diff] [blame] | 419 | // So, basically, if we are using C++ and EMSCRIPTEN_BINDINGS, we can't have primitive pointers |
Kevin Lubick | da3d8ac | 2019-01-07 11:08:55 -0500 | [diff] [blame] | 420 | // in our function type signatures. (this gives an error message like "Cannot call foo due to unbound |
| 421 | // types Pi, Pf"). But, we can just pretend they are numbers and cast them to be pointers and |
| 422 | // the compiler is happy. |
| 423 | SkPathOrNull EMSCRIPTEN_KEEPALIVE MakePathFromCmds(uintptr_t /* float* */ cptr, int numCmds) { |
| 424 | const auto* cmds = reinterpret_cast<const float*>(cptr); |
| 425 | SkPath path; |
| 426 | float x1, y1, x2, y2, x3, y3; |
| 427 | |
| 428 | // if there are not enough arguments, bail with the path we've constructed so far. |
| 429 | #define CHECK_NUM_ARGS(n) \ |
| 430 | if ((i + n) > numCmds) { \ |
| 431 | SkDebugf("Not enough args to match the verbs. Saw %d commands\n", numCmds); \ |
| 432 | return emscripten::val::null(); \ |
| 433 | } |
| 434 | |
| 435 | for(int i = 0; i < numCmds;){ |
| 436 | switch (sk_float_floor2int(cmds[i++])) { |
| 437 | case MOVE: |
| 438 | CHECK_NUM_ARGS(2); |
| 439 | x1 = cmds[i++], y1 = cmds[i++]; |
| 440 | path.moveTo(x1, y1); |
| 441 | break; |
| 442 | case LINE: |
| 443 | CHECK_NUM_ARGS(2); |
| 444 | x1 = cmds[i++], y1 = cmds[i++]; |
| 445 | path.lineTo(x1, y1); |
| 446 | break; |
| 447 | case QUAD: |
| 448 | CHECK_NUM_ARGS(4); |
| 449 | x1 = cmds[i++], y1 = cmds[i++]; |
| 450 | x2 = cmds[i++], y2 = cmds[i++]; |
| 451 | path.quadTo(x1, y1, x2, y2); |
| 452 | break; |
| 453 | case CONIC: |
| 454 | CHECK_NUM_ARGS(5); |
| 455 | x1 = cmds[i++], y1 = cmds[i++]; |
| 456 | x2 = cmds[i++], y2 = cmds[i++]; |
| 457 | x3 = cmds[i++]; // weight |
| 458 | path.conicTo(x1, y1, x2, y2, x3); |
| 459 | break; |
| 460 | case CUBIC: |
| 461 | CHECK_NUM_ARGS(6); |
| 462 | x1 = cmds[i++], y1 = cmds[i++]; |
| 463 | x2 = cmds[i++], y2 = cmds[i++]; |
| 464 | x3 = cmds[i++], y3 = cmds[i++]; |
| 465 | path.cubicTo(x1, y1, x2, y2, x3, y3); |
| 466 | break; |
| 467 | case CLOSE: |
| 468 | path.close(); |
| 469 | break; |
| 470 | default: |
| 471 | SkDebugf(" path: UNKNOWN command %f, aborting dump...\n", cmds[i-1]); |
| 472 | return emscripten::val::null(); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | #undef CHECK_NUM_ARGS |
| 477 | |
| 478 | return emscripten::val(path); |
| 479 | } |
| 480 | |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 481 | //======================================================================================== |
| 482 | // Path Effects |
| 483 | //======================================================================================== |
| 484 | |
| 485 | bool ApplyDash(SkPath& path, SkScalar on, SkScalar off, SkScalar phase) { |
| 486 | SkScalar intervals[] = { on, off }; |
| 487 | auto pe = SkDashPathEffect::Make(intervals, 2, phase); |
| 488 | if (!pe) { |
| 489 | SkDebugf("Invalid args to dash()\n"); |
| 490 | return false; |
| 491 | } |
| 492 | SkStrokeRec rec(SkStrokeRec::InitStyle::kHairline_InitStyle); |
| 493 | if (pe->filterPath(&path, path, &rec, nullptr)) { |
| 494 | return true; |
| 495 | } |
| 496 | SkDebugf("Could not make dashed path\n"); |
| 497 | return false; |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 498 | } |
| 499 | |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 500 | bool ApplyTrim(SkPath& path, SkScalar startT, SkScalar stopT, bool isComplement) { |
| 501 | auto mode = isComplement ? SkTrimPathEffect::Mode::kInverted : SkTrimPathEffect::Mode::kNormal; |
| 502 | auto pe = SkTrimPathEffect::Make(startT, stopT, mode); |
| 503 | if (!pe) { |
| 504 | SkDebugf("Invalid args to trim(): startT and stopT must be in [0,1]\n"); |
| 505 | return false; |
| 506 | } |
| 507 | SkStrokeRec rec(SkStrokeRec::InitStyle::kHairline_InitStyle); |
| 508 | if (pe->filterPath(&path, path, &rec, nullptr)) { |
| 509 | return true; |
| 510 | } |
| 511 | SkDebugf("Could not trim path\n"); |
| 512 | return false; |
| 513 | } |
| 514 | |
| 515 | struct StrokeOpts { |
Kevin Lubick | b9db390 | 2018-11-26 11:47:54 -0500 | [diff] [blame] | 516 | // Default values are set in interface.js which allows clients |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 517 | // to set any number of them. Otherwise, the binding code complains if |
| 518 | // any are omitted. |
| 519 | SkScalar width; |
| 520 | SkScalar miter_limit; |
| 521 | SkPaint::Join join; |
| 522 | SkPaint::Cap cap; |
Kevin Lubick | 1646e7d | 2018-12-07 13:03:08 -0500 | [diff] [blame] | 523 | float precision; |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 524 | }; |
| 525 | |
| 526 | bool ApplyStroke(SkPath& path, StrokeOpts opts) { |
| 527 | SkPaint p; |
| 528 | p.setStyle(SkPaint::kStroke_Style); |
| 529 | p.setStrokeCap(opts.cap); |
| 530 | p.setStrokeJoin(opts.join); |
| 531 | p.setStrokeWidth(opts.width); |
| 532 | p.setStrokeMiter(opts.miter_limit); |
| 533 | |
Kevin Lubick | 1646e7d | 2018-12-07 13:03:08 -0500 | [diff] [blame] | 534 | return p.getFillPath(path, &path, nullptr, opts.precision); |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | // to map from raw memory to a uint8array |
| 538 | Uint8Array getSkDataBytes(const SkData *data) { |
| 539 | return Uint8Array(typed_memory_view(data->size(), data->bytes())); |
| 540 | } |
| 541 | |
Kevin Lubick | 1ba9c4d | 2019-02-22 10:04:06 -0500 | [diff] [blame] | 542 | // Text Shaping abstraction |
| 543 | |
| 544 | struct ShapedTextOpts { |
| 545 | SkFont font; |
| 546 | bool leftToRight; |
| 547 | std::string text; |
| 548 | SkScalar width; |
| 549 | }; |
| 550 | |
| 551 | std::unique_ptr<SkShaper> shaper; |
| 552 | |
| 553 | static sk_sp<SkTextBlob> do_shaping(const ShapedTextOpts& opts, SkPoint* pt) { |
Ben Wagner | 3bdb69c | 2019-04-01 19:01:09 -0400 | [diff] [blame] | 554 | SkTextBlobBuilderRunHandler builder(opts.text.c_str(), {0, 0}); |
Kevin Lubick | 1ba9c4d | 2019-02-22 10:04:06 -0500 | [diff] [blame] | 555 | if (!shaper) { |
| 556 | shaper = SkShaper::Make(); |
| 557 | } |
Ben Wagner | 3bdb69c | 2019-04-01 19:01:09 -0400 | [diff] [blame] | 558 | shaper->shape(opts.text.c_str(), opts.text.length(), |
| 559 | opts.font, opts.leftToRight, |
| 560 | opts.width, &builder); |
| 561 | *pt = builder.endPoint(); |
Kevin Lubick | 1ba9c4d | 2019-02-22 10:04:06 -0500 | [diff] [blame] | 562 | return builder.makeBlob(); |
| 563 | } |
| 564 | |
| 565 | class ShapedText { |
| 566 | public: |
| 567 | ShapedText(ShapedTextOpts opts) : fOpts(opts) {} |
| 568 | |
| 569 | SkRect getBounds() { |
| 570 | this->init(); |
| 571 | return SkRect::MakeLTRB(0, 0, fOpts.width, fPoint.y()); |
| 572 | } |
| 573 | |
| 574 | SkTextBlob* blob() { |
| 575 | this->init(); |
| 576 | return fBlob.get(); |
| 577 | } |
| 578 | private: |
| 579 | const ShapedTextOpts fOpts; |
| 580 | SkPoint fPoint; |
| 581 | sk_sp<SkTextBlob> fBlob; |
| 582 | |
| 583 | void init() { |
| 584 | if (!fBlob) { |
| 585 | fBlob = do_shaping(fOpts, &fPoint); |
| 586 | } |
| 587 | } |
| 588 | }; |
| 589 | |
| 590 | void drawShapedText(SkCanvas& canvas, ShapedText st, SkScalar x, |
Kevin Lubick | 77d9b5c | 2019-10-29 10:48:26 -0400 | [diff] [blame] | 591 | SkScalar y, SkPaint paint) { |
Kevin Lubick | 1ba9c4d | 2019-02-22 10:04:06 -0500 | [diff] [blame] | 592 | canvas.drawTextBlob(st.blob(), x, y, paint); |
| 593 | } |
| 594 | |
Kevin Lubick | 77d9b5c | 2019-10-29 10:48:26 -0400 | [diff] [blame] | 595 | int saveLayerRec(SkCanvas& canvas, const SkPaint* paint, |
| 596 | const SkImageFilter* backdrop, SkCanvas::SaveLayerFlags flags) { |
| 597 | return canvas.saveLayer(SkCanvas::SaveLayerRec(nullptr, paint, backdrop, flags)); |
| 598 | } |
| 599 | |
| 600 | int saveLayerRecBounds(SkCanvas& canvas, const SkPaint* paint, const SkImageFilter* backdrop, |
| 601 | SkCanvas::SaveLayerFlags flags, const SkRect& bounds) { |
| 602 | return canvas.saveLayer(SkCanvas::SaveLayerRec(&bounds, paint, backdrop, flags)); |
| 603 | } |
| 604 | |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 605 | // This is simpler than dealing with an SkPoint and SkVector |
| 606 | struct PosTan { |
| 607 | SkScalar px, py, tx, ty; |
| 608 | }; |
| 609 | |
Kevin Lubick | 2e5fe35 | 2019-09-03 12:59:06 -0400 | [diff] [blame] | 610 | // SimpleRRect is simpler than passing a (complex) SkRRect over the wire to JS. |
| 611 | struct SimpleRRect { |
| 612 | SkRect rect; |
Kevin Lubick | 7d644e1 | 2019-09-11 14:22:22 -0400 | [diff] [blame] | 613 | |
| 614 | SkScalar rx1; |
| 615 | SkScalar ry1; |
| 616 | SkScalar rx2; |
| 617 | SkScalar ry2; |
| 618 | SkScalar rx3; |
| 619 | SkScalar ry3; |
| 620 | SkScalar rx4; |
| 621 | SkScalar ry4; |
Kevin Lubick | 2e5fe35 | 2019-09-03 12:59:06 -0400 | [diff] [blame] | 622 | }; |
| 623 | |
| 624 | SkRRect toRRect(const SimpleRRect& r) { |
Kevin Lubick | 7d644e1 | 2019-09-11 14:22:22 -0400 | [diff] [blame] | 625 | SkVector fRadii[4] = {{r.rx1, r.ry1}, {r.rx2, r.ry2}, |
| 626 | {r.rx3, r.ry3}, {r.rx4, r.ry4}}; |
| 627 | SkRRect rr; |
| 628 | rr.setRectRadii(r.rect, fRadii); |
| 629 | return rr; |
Kevin Lubick | 2e5fe35 | 2019-09-03 12:59:06 -0400 | [diff] [blame] | 630 | } |
| 631 | |
Kevin Lubick | 50f377e | 2019-09-11 15:23:00 -0400 | [diff] [blame] | 632 | struct TonalColors { |
| 633 | SkColor ambientColor; |
| 634 | SkColor spotColor; |
| 635 | }; |
| 636 | |
| 637 | TonalColors computeTonalColors(const TonalColors& in) { |
| 638 | TonalColors out; |
| 639 | SkShadowUtils::ComputeTonalColors(in.ambientColor, in.spotColor, |
| 640 | &out.ambientColor, &out.spotColor); |
| 641 | return out; |
| 642 | } |
| 643 | |
Kevin Lubick | 2e5fe35 | 2019-09-03 12:59:06 -0400 | [diff] [blame] | 644 | // These objects have private destructors / delete methods - I don't think |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 645 | // we need to do anything other than tell emscripten to do nothing. |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 646 | namespace emscripten { |
| 647 | namespace internal { |
| 648 | template<typename ClassType> |
| 649 | void raw_destructor(ClassType *); |
| 650 | |
| 651 | template<> |
| 652 | void raw_destructor<SkData>(SkData *ptr) { |
| 653 | } |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 654 | |
| 655 | template<> |
| 656 | void raw_destructor<SkVertices>(SkVertices *ptr) { |
| 657 | } |
Kevin Lubick | ec4903d | 2019-01-14 08:36:08 -0500 | [diff] [blame] | 658 | |
Kevin Lubick | 61887c7 | 2019-09-26 13:20:50 -0400 | [diff] [blame] | 659 | #ifndef SK_NO_FONTS |
Kevin Lubick | ec4903d | 2019-01-14 08:36:08 -0500 | [diff] [blame] | 660 | template<> |
| 661 | void raw_destructor<SkTextBlob>(SkTextBlob *ptr) { |
| 662 | } |
Kevin Lubick | 61887c7 | 2019-09-26 13:20:50 -0400 | [diff] [blame] | 663 | |
| 664 | template<> |
| 665 | void raw_destructor<SkTypeface>(SkTypeface *ptr) { |
| 666 | } |
| 667 | #endif |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 668 | } |
| 669 | } |
| 670 | |
Kevin Lubick | 61887c7 | 2019-09-26 13:20:50 -0400 | [diff] [blame] | 671 | // Some signatures below have uintptr_t instead of a pointer to a primitive |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 672 | // type (e.g. SkScalar). This is necessary because we can't use "bind" (EMSCRIPTEN_BINDINGS) |
| 673 | // and pointers to primitive types (Only bound types like SkPoint). We could if we used |
| 674 | // cwrap (see https://becominghuman.ai/passing-and-returning-webassembly-array-parameters-a0f572c65d97) |
| 675 | // but that requires us to stick to C code and, AFAIK, doesn't allow us to return nice things like |
| 676 | // SkPath or SkCanvas. |
| 677 | // |
Kevin Lubick | 61887c7 | 2019-09-26 13:20:50 -0400 | [diff] [blame] | 678 | // So, basically, if we are using C++ and EMSCRIPTEN_BINDINGS, we can't have primitive pointers |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 679 | // in our function type signatures. (this gives an error message like "Cannot call foo due to unbound |
| 680 | // types Pi, Pf"). But, we can just pretend they are numbers and cast them to be pointers and |
| 681 | // the compiler is happy. |
| 682 | EMSCRIPTEN_BINDINGS(Skia) { |
Kevin Lubick | 53965c9 | 2018-10-11 08:51:55 -0400 | [diff] [blame] | 683 | #if SK_SUPPORT_GPU |
Kevin Lubick | 53965c9 | 2018-10-11 08:51:55 -0400 | [diff] [blame] | 684 | function("currentContext", &emscripten_webgl_get_current_context); |
| 685 | function("setCurrentContext", &emscripten_webgl_make_context_current); |
Kevin Lubick | 543f352 | 2019-03-08 10:04:28 -0500 | [diff] [blame] | 686 | function("MakeGrContext", &MakeGrContext); |
| 687 | function("MakeOnScreenGLSurface", &MakeOnScreenGLSurface); |
| 688 | function("MakeRenderTarget", select_overload<sk_sp<SkSurface>(sk_sp<GrContext>, int, int)>(&MakeRenderTarget)); |
| 689 | function("MakeRenderTarget", select_overload<sk_sp<SkSurface>(sk_sp<GrContext>, SimpleImageInfo)>(&MakeRenderTarget)); |
| 690 | |
Kevin Lubick | 3d99b1e | 2018-10-16 10:15:01 -0400 | [diff] [blame] | 691 | constant("gpu", true); |
Kevin Lubick | b07204a | 2018-11-20 14:07:42 -0500 | [diff] [blame] | 692 | #endif |
Kevin Lubick | 50f377e | 2019-09-11 15:23:00 -0400 | [diff] [blame] | 693 | function("computeTonalColors", &computeTonalColors); |
Kevin Lubick | 6b921b7 | 2019-09-18 16:18:17 -0400 | [diff] [blame] | 694 | function("_decodeAnimatedImage", optional_override([](uintptr_t /* uint8_t* */ iptr, |
| 695 | size_t length)->sk_sp<SkAnimatedImage> { |
| 696 | uint8_t* imgData = reinterpret_cast<uint8_t*>(iptr); |
| 697 | sk_sp<SkData> bytes = SkData::MakeFromMalloc(imgData, length); |
| 698 | auto codec = SkAndroidCodec::MakeFromData(bytes); |
| 699 | if (nullptr == codec) { |
| 700 | return nullptr; |
| 701 | } |
| 702 | return SkAnimatedImage::Make(std::move(codec)); |
| 703 | }), allow_raw_pointers()); |
Kevin Lubick | 0a1293c | 2018-12-03 12:31:04 -0500 | [diff] [blame] | 704 | function("_decodeImage", optional_override([](uintptr_t /* uint8_t* */ iptr, |
| 705 | size_t length)->sk_sp<SkImage> { |
| 706 | uint8_t* imgData = reinterpret_cast<uint8_t*>(iptr); |
Kevin Lubick | 88aff5f | 2019-02-28 16:05:09 -0500 | [diff] [blame] | 707 | sk_sp<SkData> bytes = SkData::MakeFromMalloc(imgData, length); |
| 708 | return SkImage::MakeFromEncoded(std::move(bytes)); |
Kevin Lubick | 0a1293c | 2018-12-03 12:31:04 -0500 | [diff] [blame] | 709 | }), allow_raw_pointers()); |
Kevin Lubick | ea905ec | 2018-11-30 14:05:58 -0500 | [diff] [blame] | 710 | function("_getRasterDirectSurface", optional_override([](const SimpleImageInfo ii, |
Kevin Lubick | 52b9f37 | 2018-12-04 13:57:36 -0500 | [diff] [blame] | 711 | uintptr_t /* uint8_t* */ pPtr, |
Kevin Lubick | ea905ec | 2018-11-30 14:05:58 -0500 | [diff] [blame] | 712 | size_t rowBytes)->sk_sp<SkSurface> { |
Kevin Lubick | 52b9f37 | 2018-12-04 13:57:36 -0500 | [diff] [blame] | 713 | uint8_t* pixels = reinterpret_cast<uint8_t*>(pPtr); |
Kevin Lubick | ea905ec | 2018-11-30 14:05:58 -0500 | [diff] [blame] | 714 | SkImageInfo imageInfo = toSkImageInfo(ii); |
| 715 | return SkSurface::MakeRasterDirect(imageInfo, pixels, rowBytes, nullptr); |
| 716 | }), allow_raw_pointers()); |
Kevin Lubick | 53965c9 | 2018-10-11 08:51:55 -0400 | [diff] [blame] | 717 | function("_getRasterN32PremulSurface", optional_override([](int width, int height)->sk_sp<SkSurface> { |
| 718 | return SkSurface::MakeRasterN32Premul(width, height, nullptr); |
| 719 | }), allow_raw_pointers()); |
Kevin Lubick | b07204a | 2018-11-20 14:07:42 -0500 | [diff] [blame] | 720 | |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 721 | function("getSkDataBytes", &getSkDataBytes, allow_raw_pointers()); |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 722 | function("MakeSkCornerPathEffect", &SkCornerPathEffect::Make, allow_raw_pointers()); |
| 723 | function("MakeSkDiscretePathEffect", &SkDiscretePathEffect::Make, allow_raw_pointers()); |
Kevin Lubick | 15b4023 | 2019-10-29 09:55:39 -0400 | [diff] [blame] | 724 | // Deprecated: use Canvaskit.SkMaskFilter.MakeBlur |
Kevin Lubick | 61ef7b2 | 2018-11-27 13:26:59 -0500 | [diff] [blame] | 725 | function("MakeBlurMaskFilter", optional_override([](SkBlurStyle style, SkScalar sigma, bool respectCTM)->sk_sp<SkMaskFilter> { |
| 726 | // Adds a little helper because emscripten doesn't expose default params. |
| 727 | return SkMaskFilter::MakeBlur(style, sigma, respectCTM); |
| 728 | }), allow_raw_pointers()); |
Kevin Lubick | da3d8ac | 2019-01-07 11:08:55 -0500 | [diff] [blame] | 729 | function("_MakePathFromCmds", &MakePathFromCmds); |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 730 | function("MakePathFromOp", &MakePathFromOp); |
Kevin Lubick | a40f832 | 2018-12-17 16:01:36 -0500 | [diff] [blame] | 731 | function("MakePathFromSVGString", &MakePathFromSVGString); |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 732 | |
| 733 | // These won't be called directly, there's a JS helper to deal with typed arrays. |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 734 | function("_MakeSkDashPathEffect", optional_override([](uintptr_t /* float* */ cptr, int count, SkScalar phase)->sk_sp<SkPathEffect> { |
| 735 | // See comment above for uintptr_t explanation |
| 736 | const float* intervals = reinterpret_cast<const float*>(cptr); |
| 737 | return SkDashPathEffect::Make(intervals, count, phase); |
| 738 | }), allow_raw_pointers()); |
Kevin Lubick | 52b9f37 | 2018-12-04 13:57:36 -0500 | [diff] [blame] | 739 | function("_MakeImage", optional_override([](SimpleImageInfo ii, |
| 740 | uintptr_t /* uint8_t* */ pPtr, int plen, |
| 741 | size_t rowBytes)->sk_sp<SkImage> { |
| 742 | // See comment above for uintptr_t explanation |
| 743 | uint8_t* pixels = reinterpret_cast<uint8_t*>(pPtr); |
| 744 | SkImageInfo info = toSkImageInfo(ii); |
| 745 | sk_sp<SkData> pixelData = SkData::MakeFromMalloc(pixels, plen); |
| 746 | |
| 747 | return SkImage::MakeRasterData(info, pixelData, rowBytes); |
| 748 | }), allow_raw_pointers()); |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 749 | function("_MakeLinearGradientShader", optional_override([](SkPoint start, SkPoint end, |
| 750 | uintptr_t /* SkColor* */ cPtr, uintptr_t /* SkScalar* */ pPtr, |
Mike Reed | 5c5de21 | 2019-04-03 16:51:47 -0400 | [diff] [blame] | 751 | int count, SkTileMode mode, uint32_t flags)->sk_sp<SkShader> { |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 752 | SkPoint points[] = { start, end }; |
| 753 | // See comment above for uintptr_t explanation |
| 754 | const SkColor* colors = reinterpret_cast<const SkColor*> (cPtr); |
| 755 | const SkScalar* positions = reinterpret_cast<const SkScalar*>(pPtr); |
| 756 | |
| 757 | return SkGradientShader::MakeLinear(points, colors, positions, count, |
| 758 | mode, flags, nullptr); |
| 759 | }), allow_raw_pointers()); |
| 760 | function("_MakeLinearGradientShader", optional_override([](SkPoint start, SkPoint end, |
| 761 | uintptr_t /* SkColor* */ cPtr, uintptr_t /* SkScalar* */ pPtr, |
Mike Reed | 5c5de21 | 2019-04-03 16:51:47 -0400 | [diff] [blame] | 762 | int count, SkTileMode mode, uint32_t flags, |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 763 | const SimpleMatrix& lm)->sk_sp<SkShader> { |
| 764 | SkPoint points[] = { start, end }; |
| 765 | // See comment above for uintptr_t explanation |
| 766 | const SkColor* colors = reinterpret_cast<const SkColor*> (cPtr); |
| 767 | const SkScalar* positions = reinterpret_cast<const SkScalar*>(pPtr); |
| 768 | |
| 769 | SkMatrix localMatrix = toSkMatrix(lm); |
| 770 | |
| 771 | return SkGradientShader::MakeLinear(points, colors, positions, count, |
| 772 | mode, flags, &localMatrix); |
| 773 | }), allow_raw_pointers()); |
| 774 | function("_MakeRadialGradientShader", optional_override([](SkPoint center, SkScalar radius, |
| 775 | uintptr_t /* SkColor* */ cPtr, uintptr_t /* SkScalar* */ pPtr, |
Mike Reed | 5c5de21 | 2019-04-03 16:51:47 -0400 | [diff] [blame] | 776 | int count, SkTileMode mode, uint32_t flags)->sk_sp<SkShader> { |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 777 | // See comment above for uintptr_t explanation |
| 778 | const SkColor* colors = reinterpret_cast<const SkColor*> (cPtr); |
| 779 | const SkScalar* positions = reinterpret_cast<const SkScalar*>(pPtr); |
| 780 | |
| 781 | return SkGradientShader::MakeRadial(center, radius, colors, positions, count, |
| 782 | mode, flags, nullptr); |
| 783 | }), allow_raw_pointers()); |
| 784 | function("_MakeRadialGradientShader", optional_override([](SkPoint center, SkScalar radius, |
| 785 | uintptr_t /* SkColor* */ cPtr, uintptr_t /* SkScalar* */ pPtr, |
Mike Reed | 5c5de21 | 2019-04-03 16:51:47 -0400 | [diff] [blame] | 786 | int count, SkTileMode mode, uint32_t flags, |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 787 | const SimpleMatrix& lm)->sk_sp<SkShader> { |
| 788 | // See comment above for uintptr_t explanation |
| 789 | const SkColor* colors = reinterpret_cast<const SkColor*> (cPtr); |
| 790 | const SkScalar* positions = reinterpret_cast<const SkScalar*>(pPtr); |
| 791 | |
| 792 | SkMatrix localMatrix = toSkMatrix(lm); |
| 793 | return SkGradientShader::MakeRadial(center, radius, colors, positions, count, |
| 794 | mode, flags, &localMatrix); |
| 795 | }), allow_raw_pointers()); |
Kevin Lubick | eb2f6b0 | 2018-11-29 15:07:02 -0500 | [diff] [blame] | 796 | function("_MakeTwoPointConicalGradientShader", optional_override([]( |
| 797 | SkPoint start, SkScalar startRadius, |
| 798 | SkPoint end, SkScalar endRadius, |
| 799 | uintptr_t /* SkColor* */ cPtr, uintptr_t /* SkScalar* */ pPtr, |
Mike Reed | 5c5de21 | 2019-04-03 16:51:47 -0400 | [diff] [blame] | 800 | int count, SkTileMode mode, uint32_t flags)->sk_sp<SkShader> { |
Kevin Lubick | eb2f6b0 | 2018-11-29 15:07:02 -0500 | [diff] [blame] | 801 | // See comment above for uintptr_t explanation |
| 802 | const SkColor* colors = reinterpret_cast<const SkColor*> (cPtr); |
| 803 | const SkScalar* positions = reinterpret_cast<const SkScalar*>(pPtr); |
| 804 | |
| 805 | return SkGradientShader::MakeTwoPointConical(start, startRadius, end, endRadius, |
| 806 | colors, positions, count, mode, |
| 807 | flags, nullptr); |
| 808 | }), allow_raw_pointers()); |
| 809 | function("_MakeTwoPointConicalGradientShader", optional_override([]( |
| 810 | SkPoint start, SkScalar startRadius, |
| 811 | SkPoint end, SkScalar endRadius, |
| 812 | uintptr_t /* SkColor* */ cPtr, uintptr_t /* SkScalar* */ pPtr, |
Mike Reed | 5c5de21 | 2019-04-03 16:51:47 -0400 | [diff] [blame] | 813 | int count, SkTileMode mode, uint32_t flags, |
Kevin Lubick | eb2f6b0 | 2018-11-29 15:07:02 -0500 | [diff] [blame] | 814 | const SimpleMatrix& lm)->sk_sp<SkShader> { |
| 815 | // See comment above for uintptr_t explanation |
| 816 | const SkColor* colors = reinterpret_cast<const SkColor*> (cPtr); |
| 817 | const SkScalar* positions = reinterpret_cast<const SkScalar*>(pPtr); |
| 818 | |
| 819 | SkMatrix localMatrix = toSkMatrix(lm); |
| 820 | return SkGradientShader::MakeTwoPointConical(start, startRadius, end, endRadius, |
| 821 | colors, positions, count, mode, |
| 822 | flags, &localMatrix); |
| 823 | }), allow_raw_pointers()); |
| 824 | |
Kevin Lubick | 543f352 | 2019-03-08 10:04:28 -0500 | [diff] [blame] | 825 | #if SK_SUPPORT_GPU |
| 826 | class_<GrContext>("GrContext") |
Kevin Lubick | cd54466 | 2019-03-22 15:41:36 -0400 | [diff] [blame] | 827 | .smart_ptr<sk_sp<GrContext>>("sk_sp<GrContext>") |
| 828 | .function("getResourceCacheLimitBytes", optional_override([](GrContext& self)->size_t { |
| 829 | int maxResources = 0;// ignored |
| 830 | size_t currMax = 0; |
| 831 | self.getResourceCacheLimits(&maxResources, &currMax); |
| 832 | return currMax; |
| 833 | })) |
| 834 | .function("getResourceCacheUsageBytes", optional_override([](GrContext& self)->size_t { |
| 835 | int usedResources = 0;// ignored |
| 836 | size_t currUsage = 0; |
| 837 | self.getResourceCacheUsage(&usedResources, &currUsage); |
| 838 | return currUsage; |
| 839 | })) |
| 840 | .function("setResourceCacheLimitBytes", optional_override([](GrContext& self, size_t maxResourceBytes)->void { |
| 841 | int maxResources = 0; |
| 842 | size_t currMax = 0; // ignored |
| 843 | self.getResourceCacheLimits(&maxResources, &currMax); |
| 844 | self.setResourceCacheLimits(maxResources, maxResourceBytes); |
| 845 | })); |
Kevin Lubick | 543f352 | 2019-03-08 10:04:28 -0500 | [diff] [blame] | 846 | #endif |
| 847 | |
Kevin Lubick | 6b921b7 | 2019-09-18 16:18:17 -0400 | [diff] [blame] | 848 | class_<SkAnimatedImage>("SkAnimatedImage") |
| 849 | .smart_ptr<sk_sp<SkAnimatedImage>>("sk_sp<SkAnimatedImage>") |
Kevin Lubick | 47bd9f1 | 2019-11-08 06:55:15 -0800 | [diff] [blame] | 850 | .function("decodeNextFrame", &SkAnimatedImage::decodeNextFrame) |
| 851 | .function("getFrameCount", &SkAnimatedImage::getFrameCount) |
Kevin Lubick | 6b921b7 | 2019-09-18 16:18:17 -0400 | [diff] [blame] | 852 | .function("getRepetitionCount", &SkAnimatedImage::getRepetitionCount) |
Kevin Lubick | 47bd9f1 | 2019-11-08 06:55:15 -0800 | [diff] [blame] | 853 | .function("height", optional_override([](SkAnimatedImage& self)->int32_t { |
| 854 | return self.dimensions().height(); |
| 855 | })) |
| 856 | .function("reset", &SkAnimatedImage::reset) |
| 857 | .function("width", optional_override([](SkAnimatedImage& self)->int32_t { |
| 858 | return self.dimensions().width(); |
| 859 | })); |
Kevin Lubick | 6b921b7 | 2019-09-18 16:18:17 -0400 | [diff] [blame] | 860 | |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 861 | class_<SkCanvas>("SkCanvas") |
| 862 | .constructor<>() |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 863 | .function("clear", &SkCanvas::clear) |
Kevin Lubick | eb2f6b0 | 2018-11-29 15:07:02 -0500 | [diff] [blame] | 864 | .function("clipPath", select_overload<void (const SkPath&, SkClipOp, bool)>(&SkCanvas::clipPath)) |
Kevin Lubick | 47bd9f1 | 2019-11-08 06:55:15 -0800 | [diff] [blame] | 865 | .function("clipRRect", optional_override([](SkCanvas& self, const SimpleRRect& r, SkClipOp op, bool doAntiAlias) { |
Kevin Lubick | 6dbc4ed | 2019-10-22 09:43:34 -0400 | [diff] [blame] | 866 | self.clipRRect(toRRect(r), op, doAntiAlias); |
| 867 | })) |
Kevin Lubick | 52b9f37 | 2018-12-04 13:57:36 -0500 | [diff] [blame] | 868 | .function("clipRect", select_overload<void (const SkRect&, SkClipOp, bool)>(&SkCanvas::clipRect)) |
| 869 | .function("concat", optional_override([](SkCanvas& self, const SimpleMatrix& m) { |
| 870 | self.concat(toSkMatrix(m)); |
| 871 | })) |
Kevin Lubick | da3d8ac | 2019-01-07 11:08:55 -0500 | [diff] [blame] | 872 | .function("drawArc", &SkCanvas::drawArc) |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 873 | .function("_drawAtlas", optional_override([](SkCanvas& self, |
| 874 | const sk_sp<SkImage>& atlas, uintptr_t /* SkRSXform* */ xptr, |
| 875 | uintptr_t /* SkRect* */ rptr, uintptr_t /* SkColor* */ cptr, int count, |
| 876 | SkBlendMode mode, const SkPaint* paint)->void { |
| 877 | // See comment above for uintptr_t explanation |
| 878 | const SkRSXform* dstXforms = reinterpret_cast<const SkRSXform*>(xptr); |
| 879 | const SkRect* srcRects = reinterpret_cast<const SkRect*>(rptr); |
| 880 | const SkColor* colors = nullptr; |
| 881 | if (cptr) { |
| 882 | colors = reinterpret_cast<const SkColor*>(cptr); |
| 883 | } |
| 884 | self.drawAtlas(atlas, dstXforms, srcRects, colors, count, mode, nullptr, paint); |
| 885 | }), allow_raw_pointers()) |
Kevin Lubick | e384df4 | 2019-08-26 15:48:09 -0400 | [diff] [blame] | 886 | .function("drawCircle", select_overload<void (SkScalar, SkScalar, SkScalar, const SkPaint& paint)>(&SkCanvas::drawCircle)) |
Kevin Lubick | 6dbc4ed | 2019-10-22 09:43:34 -0400 | [diff] [blame] | 887 | .function("drawColor", &SkCanvas::drawColor) |
Kevin Lubick | 2e5fe35 | 2019-09-03 12:59:06 -0400 | [diff] [blame] | 888 | .function("drawDRRect",optional_override([](SkCanvas& self, const SimpleRRect& o, const SimpleRRect& i, const SkPaint& paint) { |
| 889 | self.drawDRRect(toRRect(o), toRRect(i), paint); |
| 890 | })) |
Kevin Lubick | 6b921b7 | 2019-09-18 16:18:17 -0400 | [diff] [blame] | 891 | .function("drawAnimatedImage", optional_override([](SkCanvas& self, sk_sp<SkAnimatedImage>& aImg, |
| 892 | SkScalar x, SkScalar y)->void { |
| 893 | self.drawDrawable(aImg.get(), x, y); |
| 894 | }), allow_raw_pointers()) |
Kevin Lubick | 0a1293c | 2018-12-03 12:31:04 -0500 | [diff] [blame] | 895 | .function("drawImage", select_overload<void (const sk_sp<SkImage>&, SkScalar, SkScalar, const SkPaint*)>(&SkCanvas::drawImage), allow_raw_pointers()) |
| 896 | .function("drawImageRect", optional_override([](SkCanvas& self, const sk_sp<SkImage>& image, |
| 897 | SkRect src, SkRect dst, |
| 898 | const SkPaint* paint, bool fastSample)->void { |
| 899 | self.drawImageRect(image, src, dst, paint, |
| 900 | fastSample ? SkCanvas::kFast_SrcRectConstraint : |
| 901 | SkCanvas::kStrict_SrcRectConstraint); |
| 902 | }), allow_raw_pointers()) |
Kevin Lubick | da3d8ac | 2019-01-07 11:08:55 -0500 | [diff] [blame] | 903 | .function("drawLine", select_overload<void (SkScalar, SkScalar, SkScalar, SkScalar, const SkPaint&)>(&SkCanvas::drawLine)) |
| 904 | .function("drawOval", &SkCanvas::drawOval) |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 905 | .function("drawPaint", &SkCanvas::drawPaint) |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 906 | #ifdef SK_INCLUDE_PARAGRAPH |
| 907 | .function("drawParagraph", optional_override([](SkCanvas& self, skia::textlayout::Paragraph* p, |
| 908 | SkScalar x, SkScalar y) { |
| 909 | p->paint(&self, x, y); |
| 910 | }), allow_raw_pointers()) |
| 911 | #endif |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 912 | .function("drawPath", &SkCanvas::drawPath) |
Kevin Lubick | cc13fd3 | 2019-04-05 13:00:01 -0400 | [diff] [blame] | 913 | // Of note, picture is *not* what is colloquially thought of as a "picture", what we call |
| 914 | // a bitmap. An SkPicture is a series of draw commands. |
| 915 | .function("drawPicture", select_overload<void (const sk_sp<SkPicture>&)>(&SkCanvas::drawPicture)) |
Kevin Lubick | 2e5fe35 | 2019-09-03 12:59:06 -0400 | [diff] [blame] | 916 | .function("drawRRect",optional_override([](SkCanvas& self, const SimpleRRect& r, const SkPaint& paint) { |
| 917 | self.drawRRect(toRRect(r), paint); |
| 918 | })) |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 919 | .function("drawRect", &SkCanvas::drawRect) |
Kevin Lubick | da3d8ac | 2019-01-07 11:08:55 -0500 | [diff] [blame] | 920 | .function("drawRoundRect", &SkCanvas::drawRoundRect) |
Kevin Lubick | 61ef7b2 | 2018-11-27 13:26:59 -0500 | [diff] [blame] | 921 | .function("drawShadow", optional_override([](SkCanvas& self, const SkPath& path, |
| 922 | const SkPoint3& zPlaneParams, |
| 923 | const SkPoint3& lightPos, SkScalar lightRadius, |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 924 | SkColor ambientColor, SkColor spotColor, |
Kevin Lubick | 61ef7b2 | 2018-11-27 13:26:59 -0500 | [diff] [blame] | 925 | uint32_t flags) { |
| 926 | SkShadowUtils::DrawShadow(&self, path, zPlaneParams, lightPos, lightRadius, |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 927 | ambientColor, spotColor, flags); |
Kevin Lubick | 61ef7b2 | 2018-11-27 13:26:59 -0500 | [diff] [blame] | 928 | })) |
Kevin Lubick | 61887c7 | 2019-09-26 13:20:50 -0400 | [diff] [blame] | 929 | #ifndef SK_NO_FONTS |
Kevin Lubick | 1ba9c4d | 2019-02-22 10:04:06 -0500 | [diff] [blame] | 930 | .function("_drawShapedText", &drawShapedText) |
Kevin Lubick | ec4903d | 2019-01-14 08:36:08 -0500 | [diff] [blame] | 931 | .function("_drawSimpleText", optional_override([](SkCanvas& self, uintptr_t /* char* */ sptr, |
| 932 | size_t len, SkScalar x, SkScalar y, const SkFont& font, |
| 933 | const SkPaint& paint) { |
| 934 | // See comment above for uintptr_t explanation |
| 935 | const char* str = reinterpret_cast<const char*>(sptr); |
| 936 | |
| 937 | self.drawSimpleText(str, len, SkTextEncoding::kUTF8, x, y, font, paint); |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 938 | })) |
Kevin Lubick | ec4903d | 2019-01-14 08:36:08 -0500 | [diff] [blame] | 939 | .function("drawTextBlob", select_overload<void (const sk_sp<SkTextBlob>&, SkScalar, SkScalar, const SkPaint&)>(&SkCanvas::drawTextBlob)) |
Kevin Lubick | 61887c7 | 2019-09-26 13:20:50 -0400 | [diff] [blame] | 940 | #endif |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 941 | .function("drawVertices", select_overload<void (const sk_sp<SkVertices>&, SkBlendMode, const SkPaint&)>(&SkCanvas::drawVertices)) |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 942 | .function("flush", &SkCanvas::flush) |
Kevin Lubick | e384df4 | 2019-08-26 15:48:09 -0400 | [diff] [blame] | 943 | .function("getSaveCount", &SkCanvas::getSaveCount) |
Kevin Lubick | 5d5723c | 2018-12-07 10:09:11 -0500 | [diff] [blame] | 944 | .function("getTotalMatrix", optional_override([](const SkCanvas& self)->SimpleMatrix { |
| 945 | SkMatrix m = self.getTotalMatrix(); |
| 946 | return toSimpleSkMatrix(m); |
| 947 | })) |
Kevin Lubick | 543f352 | 2019-03-08 10:04:28 -0500 | [diff] [blame] | 948 | .function("makeSurface", optional_override([](SkCanvas& self, SimpleImageInfo sii)->sk_sp<SkSurface> { |
| 949 | return self.makeSurface(toSkImageInfo(sii), nullptr); |
| 950 | }), allow_raw_pointers()) |
Kevin Lubick | 52b9f37 | 2018-12-04 13:57:36 -0500 | [diff] [blame] | 951 | .function("_readPixels", optional_override([](SkCanvas& self, SimpleImageInfo di, |
| 952 | uintptr_t /* uint8_t* */ pPtr, |
| 953 | size_t dstRowBytes, int srcX, int srcY) { |
| 954 | uint8_t* pixels = reinterpret_cast<uint8_t*>(pPtr); |
| 955 | SkImageInfo dstInfo = toSkImageInfo(di); |
| 956 | |
| 957 | return self.readPixels(dstInfo, pixels, dstRowBytes, srcX, srcY); |
| 958 | })) |
Kevin Lubick | eb2f6b0 | 2018-11-29 15:07:02 -0500 | [diff] [blame] | 959 | .function("restore", &SkCanvas::restore) |
Kevin Lubick | b3574c9 | 2019-03-06 08:25:36 -0500 | [diff] [blame] | 960 | .function("restoreToCount", &SkCanvas::restoreToCount) |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 961 | .function("rotate", select_overload<void (SkScalar, SkScalar, SkScalar)>(&SkCanvas::rotate)) |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 962 | .function("save", &SkCanvas::save) |
Kevin Lubick | 77d9b5c | 2019-10-29 10:48:26 -0400 | [diff] [blame] | 963 | // 2 params |
Kevin Lubick | b3574c9 | 2019-03-06 08:25:36 -0500 | [diff] [blame] | 964 | .function("saveLayer", select_overload<int (const SkRect&, const SkPaint*)>(&SkCanvas::saveLayer), |
| 965 | allow_raw_pointers()) |
Kevin Lubick | 77d9b5c | 2019-10-29 10:48:26 -0400 | [diff] [blame] | 966 | // 3 params (effectively with SaveLayerRec, but no bounds) |
| 967 | .function("saveLayer", saveLayerRec, allow_raw_pointers()) |
| 968 | // 4 params (effectively with SaveLayerRec) |
| 969 | .function("saveLayer", saveLayerRecBounds, allow_raw_pointers()) |
| 970 | |
Kevin Lubick | 006a6f3 | 2018-10-19 14:34:34 -0400 | [diff] [blame] | 971 | .function("scale", &SkCanvas::scale) |
Kevin Lubick | 006a6f3 | 2018-10-19 14:34:34 -0400 | [diff] [blame] | 972 | .function("skew", &SkCanvas::skew) |
Kevin Lubick | 52b9f37 | 2018-12-04 13:57:36 -0500 | [diff] [blame] | 973 | .function("translate", &SkCanvas::translate) |
| 974 | .function("_writePixels", optional_override([](SkCanvas& self, SimpleImageInfo di, |
| 975 | uintptr_t /* uint8_t* */ pPtr, |
| 976 | size_t srcRowBytes, int dstX, int dstY) { |
| 977 | uint8_t* pixels = reinterpret_cast<uint8_t*>(pPtr); |
| 978 | SkImageInfo dstInfo = toSkImageInfo(di); |
| 979 | |
| 980 | return self.writePixels(dstInfo, pixels, srcRowBytes, dstX, dstY); |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 981 | })); |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 982 | |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 983 | class_<SkColorFilter>("SkColorFilter") |
| 984 | .smart_ptr<sk_sp<SkColorFilter>>("sk_sp<SkColorFilter>>") |
| 985 | .class_function("MakeBlend", &SkColorFilters::Blend) |
| 986 | .class_function("MakeCompose", &SkColorFilters::Compose) |
| 987 | .class_function("MakeLerp", &SkColorFilters::Lerp) |
| 988 | .class_function("MakeLinearToSRGBGamma", &SkColorFilters::LinearToSRGBGamma) |
| 989 | .class_function("_makeMatrix", optional_override([](uintptr_t /* float* */ fPtr) { |
| 990 | float* twentyFloats = reinterpret_cast<float*>(fPtr); |
| 991 | return SkColorFilters::Matrix(twentyFloats); |
| 992 | })) |
| 993 | .class_function("MakeSRGBToLinearGamma", &SkColorFilters::SRGBToLinearGamma); |
| 994 | |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 995 | class_<SkData>("SkData") |
| 996 | .smart_ptr<sk_sp<SkData>>("sk_sp<SkData>>") |
| 997 | .function("size", &SkData::size); |
| 998 | |
Kevin Lubick | 6b921b7 | 2019-09-18 16:18:17 -0400 | [diff] [blame] | 999 | class_<SkDrawable>("SkDrawable") |
| 1000 | .smart_ptr<sk_sp<SkDrawable>>("sk_sp<SkDrawable>>"); |
| 1001 | |
Kevin Lubick | 61887c7 | 2019-09-26 13:20:50 -0400 | [diff] [blame] | 1002 | #ifndef SK_NO_FONTS |
Kevin Lubick | 35ac038 | 2019-01-02 15:13:57 -0500 | [diff] [blame] | 1003 | class_<SkFont>("SkFont") |
| 1004 | .constructor<>() |
| 1005 | .constructor<sk_sp<SkTypeface>>() |
| 1006 | .constructor<sk_sp<SkTypeface>, SkScalar>() |
| 1007 | .constructor<sk_sp<SkTypeface>, SkScalar, SkScalar, SkScalar>() |
| 1008 | .function("getScaleX", &SkFont::getScaleX) |
| 1009 | .function("getSize", &SkFont::getSize) |
| 1010 | .function("getSkewX", &SkFont::getSkewX) |
| 1011 | .function("getTypeface", &SkFont::getTypeface, allow_raw_pointers()) |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 1012 | .function("_getWidths", optional_override([](SkFont& self, uintptr_t /* char* */ sptr, |
| 1013 | size_t strLen, size_t expectedCodePoints, |
| 1014 | uintptr_t /* SkScalar* */ wptr) -> bool { |
| 1015 | char* str = reinterpret_cast<char*>(sptr); |
| 1016 | SkScalar* widths = reinterpret_cast<SkScalar*>(wptr); |
| 1017 | |
| 1018 | SkGlyphID* glyphStorage = new SkGlyphID[expectedCodePoints]; |
| 1019 | int actualCodePoints = self.textToGlyphs(str, strLen, SkTextEncoding::kUTF8, |
| 1020 | glyphStorage, expectedCodePoints); |
| 1021 | if (actualCodePoints != expectedCodePoints) { |
| 1022 | SkDebugf("Actually %d glyphs, expected only %d\n", |
| 1023 | actualCodePoints, expectedCodePoints); |
| 1024 | return false; |
| 1025 | } |
| 1026 | |
| 1027 | self.getWidths(glyphStorage, actualCodePoints, widths); |
| 1028 | delete[] glyphStorage; |
| 1029 | return true; |
| 1030 | })) |
Kevin Lubick | 35ac038 | 2019-01-02 15:13:57 -0500 | [diff] [blame] | 1031 | .function("measureText", optional_override([](SkFont& self, std::string text) { |
| 1032 | // TODO(kjlubick): This does not work well for non-ascii |
| 1033 | // Need to maybe add a helper in interface.js that supports UTF-8 |
| 1034 | // Otherwise, go with std::wstring and set UTF-32 encoding. |
| 1035 | return self.measureText(text.c_str(), text.length(), SkTextEncoding::kUTF8); |
| 1036 | })) |
| 1037 | .function("setScaleX", &SkFont::setScaleX) |
| 1038 | .function("setSize", &SkFont::setSize) |
| 1039 | .function("setSkewX", &SkFont::setSkewX) |
| 1040 | .function("setTypeface", &SkFont::setTypeface, allow_raw_pointers()); |
| 1041 | |
Kevin Lubick | 1ba9c4d | 2019-02-22 10:04:06 -0500 | [diff] [blame] | 1042 | class_<ShapedText>("ShapedText") |
| 1043 | .constructor<ShapedTextOpts>() |
| 1044 | .function("getBounds", &ShapedText::getBounds); |
| 1045 | |
Kevin Lubick | ddd0a33 | 2018-12-12 10:35:13 -0500 | [diff] [blame] | 1046 | class_<SkFontMgr>("SkFontMgr") |
| 1047 | .smart_ptr<sk_sp<SkFontMgr>>("sk_sp<SkFontMgr>") |
Kevin Lubick | 61887c7 | 2019-09-26 13:20:50 -0400 | [diff] [blame] | 1048 | .class_function("_fromData", optional_override([](uintptr_t /* uint8_t** */ dPtr, |
| 1049 | uintptr_t /* size_t* */ sPtr, |
| 1050 | int numFonts)->sk_sp<SkFontMgr> { |
| 1051 | // See comment above for uintptr_t explanation |
| 1052 | auto datas = reinterpret_cast<const uint8_t**>(dPtr); |
| 1053 | auto sizes = reinterpret_cast<const size_t*>(sPtr); |
| 1054 | |
| 1055 | return SkFontMgr_New_Custom_Data(datas, sizes, numFonts); |
| 1056 | }), allow_raw_pointers()) |
Kevin Lubick | ddd0a33 | 2018-12-12 10:35:13 -0500 | [diff] [blame] | 1057 | .class_function("RefDefault", &SkFontMgr::RefDefault) |
| 1058 | #ifdef SK_DEBUG |
| 1059 | .function("dumpFamilies", optional_override([](SkFontMgr& self) { |
| 1060 | int numFam = self.countFamilies(); |
Kevin Lubick | 61887c7 | 2019-09-26 13:20:50 -0400 | [diff] [blame] | 1061 | SkDebugf("There are %d font families\n", numFam); |
Kevin Lubick | ddd0a33 | 2018-12-12 10:35:13 -0500 | [diff] [blame] | 1062 | for (int i = 0 ; i< numFam; i++) { |
| 1063 | SkString s; |
| 1064 | self.getFamilyName(i, &s); |
Kevin Lubick | 61887c7 | 2019-09-26 13:20:50 -0400 | [diff] [blame] | 1065 | SkDebugf("\t%s\n", s.c_str()); |
Kevin Lubick | ddd0a33 | 2018-12-12 10:35:13 -0500 | [diff] [blame] | 1066 | } |
| 1067 | })) |
| 1068 | #endif |
| 1069 | .function("countFamilies", &SkFontMgr::countFamilies) |
| 1070 | .function("_makeTypefaceFromData", optional_override([](SkFontMgr& self, |
| 1071 | uintptr_t /* uint8_t* */ fPtr, |
| 1072 | int flen)->sk_sp<SkTypeface> { |
| 1073 | // See comment above for uintptr_t explanation |
| 1074 | uint8_t* font = reinterpret_cast<uint8_t*>(fPtr); |
| 1075 | sk_sp<SkData> fontData = SkData::MakeFromMalloc(font, flen); |
| 1076 | |
| 1077 | return self.makeFromData(fontData); |
| 1078 | }), allow_raw_pointers()); |
Kevin Lubick | 61887c7 | 2019-09-26 13:20:50 -0400 | [diff] [blame] | 1079 | #endif |
Kevin Lubick | ddd0a33 | 2018-12-12 10:35:13 -0500 | [diff] [blame] | 1080 | |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 1081 | class_<SkImage>("SkImage") |
| 1082 | .smart_ptr<sk_sp<SkImage>>("sk_sp<SkImage>") |
Kevin Lubick | 0a1293c | 2018-12-03 12:31:04 -0500 | [diff] [blame] | 1083 | .function("height", &SkImage::height) |
| 1084 | .function("width", &SkImage::width) |
Alexander Khovansky | 3e11933 | 2018-11-15 02:01:19 +0300 | [diff] [blame] | 1085 | .function("_encodeToData", select_overload<sk_sp<SkData>()const>(&SkImage::encodeToData)) |
Kevin Lubick | a064c28 | 2019-04-04 09:28:53 -0400 | [diff] [blame] | 1086 | .function("_encodeToDataWithFormat", select_overload<sk_sp<SkData>(SkEncodedImageFormat encodedImageFormat, int quality)const>(&SkImage::encodeToData)) |
| 1087 | // Allow localMatrix to be optional, so we have 2 declarations of these shaders |
| 1088 | .function("_makeShader", optional_override([](sk_sp<SkImage> self, |
| 1089 | SkTileMode tx, SkTileMode ty)->sk_sp<SkShader> { |
| 1090 | return self->makeShader(tx, ty, nullptr); |
| 1091 | }), allow_raw_pointers()) |
| 1092 | .function("_makeShader", optional_override([](sk_sp<SkImage> self, |
| 1093 | SkTileMode tx, SkTileMode ty, |
| 1094 | const SimpleMatrix& lm)->sk_sp<SkShader> { |
| 1095 | SkMatrix localMatrix = toSkMatrix(lm); |
| 1096 | |
| 1097 | return self->makeShader(tx, ty, &localMatrix); |
Kevin Lubick | d6b32ed | 2019-05-06 13:04:03 -0400 | [diff] [blame] | 1098 | }), allow_raw_pointers()) |
| 1099 | .function("_readPixels", optional_override([](sk_sp<SkImage> self, |
| 1100 | SimpleImageInfo sii, uintptr_t /* uint8_t* */ pPtr, |
| 1101 | size_t dstRowBytes, int srcX, int srcY)->bool { |
| 1102 | // See comment above for uintptr_t explanation |
| 1103 | uint8_t* pixels = reinterpret_cast<uint8_t*>(pPtr); |
| 1104 | SkImageInfo ii = toSkImageInfo(sii); |
| 1105 | |
| 1106 | return self->readPixels(ii, pixels, dstRowBytes, srcX, srcY); |
Kevin Lubick | a064c28 | 2019-04-04 09:28:53 -0400 | [diff] [blame] | 1107 | }), allow_raw_pointers()); |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 1108 | |
Kevin Lubick | 15b4023 | 2019-10-29 09:55:39 -0400 | [diff] [blame] | 1109 | class_<SkImageFilter>("SkImageFilter") |
| 1110 | .smart_ptr<sk_sp<SkImageFilter>>("sk_sp<SkImageFilter>") |
| 1111 | .class_function("MakeBlur", optional_override([](SkScalar sigmaX, SkScalar sigmaY, |
| 1112 | SkTileMode tileMode, sk_sp<SkImageFilter> input)->sk_sp<SkImageFilter> { |
| 1113 | // Emscripten does not like default args nor SkIRect* much |
| 1114 | return SkImageFilters::Blur(sigmaX, sigmaY, tileMode, input); |
| 1115 | })) |
| 1116 | .class_function("MakeColorFilter", optional_override([](sk_sp<SkColorFilter> cf, |
| 1117 | sk_sp<SkImageFilter> input)->sk_sp<SkImageFilter> { |
| 1118 | // Emscripten does not like default args nor SkIRect* much |
| 1119 | return SkImageFilters::ColorFilter(cf, input); |
| 1120 | })) |
| 1121 | .class_function("MakeCompose", &SkImageFilters::Compose); |
| 1122 | |
Kevin Lubick | 61ef7b2 | 2018-11-27 13:26:59 -0500 | [diff] [blame] | 1123 | class_<SkMaskFilter>("SkMaskFilter") |
Kevin Lubick | 15b4023 | 2019-10-29 09:55:39 -0400 | [diff] [blame] | 1124 | .smart_ptr<sk_sp<SkMaskFilter>>("sk_sp<SkMaskFilter>") |
| 1125 | .class_function("MakeBlur", optional_override([](SkBlurStyle style, SkScalar sigma, bool respectCTM)->sk_sp<SkMaskFilter> { |
| 1126 | // Adds a little helper because emscripten doesn't expose default params. |
| 1127 | return SkMaskFilter::MakeBlur(style, sigma, respectCTM); |
| 1128 | }), allow_raw_pointers()); |
Kevin Lubick | 61ef7b2 | 2018-11-27 13:26:59 -0500 | [diff] [blame] | 1129 | |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 1130 | class_<SkPaint>("SkPaint") |
| 1131 | .constructor<>() |
| 1132 | .function("copy", optional_override([](const SkPaint& self)->SkPaint { |
| 1133 | SkPaint p(self); |
| 1134 | return p; |
| 1135 | })) |
Kevin Lubick | 12c0e50 | 2018-11-28 12:51:56 -0500 | [diff] [blame] | 1136 | .function("getBlendMode", &SkPaint::getBlendMode) |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 1137 | .function("getColor", &SkPaint::getColor) |
Kevin Lubick | 0a1293c | 2018-12-03 12:31:04 -0500 | [diff] [blame] | 1138 | .function("getFilterQuality", &SkPaint::getFilterQuality) |
Kevin Lubick | b9db390 | 2018-11-26 11:47:54 -0500 | [diff] [blame] | 1139 | .function("getStrokeCap", &SkPaint::getStrokeCap) |
| 1140 | .function("getStrokeJoin", &SkPaint::getStrokeJoin) |
Kevin Lubick | 0a1293c | 2018-12-03 12:31:04 -0500 | [diff] [blame] | 1141 | .function("getStrokeMiter", &SkPaint::getStrokeMiter) |
| 1142 | .function("getStrokeWidth", &SkPaint::getStrokeWidth) |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 1143 | .function("setAntiAlias", &SkPaint::setAntiAlias) |
Kevin Lubick | 12c0e50 | 2018-11-28 12:51:56 -0500 | [diff] [blame] | 1144 | .function("setBlendMode", &SkPaint::setBlendMode) |
Mike Reed | dc2b98f | 2019-05-20 11:47:49 -0400 | [diff] [blame] | 1145 | .function("setColor", optional_override([](SkPaint& self, SkColor c) { |
| 1146 | self.setColor(c); |
| 1147 | })) |
| 1148 | .function("setColorf", optional_override([](SkPaint& self, |
| 1149 | float r, float g, float b, float a) { |
| 1150 | self.setColor({r, g, b, a}); |
| 1151 | })) |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 1152 | .function("setColorFilter", &SkPaint::setColorFilter) |
Kevin Lubick | 0a1293c | 2018-12-03 12:31:04 -0500 | [diff] [blame] | 1153 | .function("setFilterQuality", &SkPaint::setFilterQuality) |
Kevin Lubick | 15b4023 | 2019-10-29 09:55:39 -0400 | [diff] [blame] | 1154 | .function("setImageFilter", &SkPaint::setImageFilter) |
Kevin Lubick | 61ef7b2 | 2018-11-27 13:26:59 -0500 | [diff] [blame] | 1155 | .function("setMaskFilter", &SkPaint::setMaskFilter) |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 1156 | .function("setPathEffect", &SkPaint::setPathEffect) |
| 1157 | .function("setShader", &SkPaint::setShader) |
Kevin Lubick | b9db390 | 2018-11-26 11:47:54 -0500 | [diff] [blame] | 1158 | .function("setStrokeCap", &SkPaint::setStrokeCap) |
| 1159 | .function("setStrokeJoin", &SkPaint::setStrokeJoin) |
Kevin Lubick | 0a1293c | 2018-12-03 12:31:04 -0500 | [diff] [blame] | 1160 | .function("setStrokeMiter", &SkPaint::setStrokeMiter) |
| 1161 | .function("setStrokeWidth", &SkPaint::setStrokeWidth) |
Kevin Lubick | ec4903d | 2019-01-14 08:36:08 -0500 | [diff] [blame] | 1162 | .function("setStyle", &SkPaint::setStyle); |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 1163 | |
| 1164 | class_<SkPathEffect>("SkPathEffect") |
| 1165 | .smart_ptr<sk_sp<SkPathEffect>>("sk_sp<SkPathEffect>"); |
| 1166 | |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 1167 | class_<SkPath>("SkPath") |
| 1168 | .constructor<>() |
| 1169 | .constructor<const SkPath&>() |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 1170 | .function("_addArc", &ApplyAddArc) |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 1171 | // interface.js has 3 overloads of addPath |
Kevin Lubick | e384df4 | 2019-08-26 15:48:09 -0400 | [diff] [blame] | 1172 | .function("_addOval", &ApplyAddOval) |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 1173 | .function("_addPath", &ApplyAddPath) |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 1174 | // interface.js has 4 overloads of addRect |
| 1175 | .function("_addRect", &ApplyAddRect) |
Kevin Lubick | da3d8ac | 2019-01-07 11:08:55 -0500 | [diff] [blame] | 1176 | // interface.js has 4 overloads of addRoundRect |
| 1177 | .function("_addRoundRect", &ApplyAddRoundRect) |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 1178 | .function("_arcTo", &ApplyArcTo) |
Kevin Lubick | 1646e7d | 2018-12-07 13:03:08 -0500 | [diff] [blame] | 1179 | .function("_arcTo", &ApplyArcToAngle) |
Kevin Lubick | 79b7134 | 2019-11-01 14:36:52 -0400 | [diff] [blame] | 1180 | .function("_arcTo", &ApplyArcToArcSize) |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 1181 | .function("_close", &ApplyClose) |
| 1182 | .function("_conicTo", &ApplyConicTo) |
Kevin Lubick | b9db390 | 2018-11-26 11:47:54 -0500 | [diff] [blame] | 1183 | .function("countPoints", &SkPath::countPoints) |
Kevin Lubick | 1646e7d | 2018-12-07 13:03:08 -0500 | [diff] [blame] | 1184 | .function("contains", &SkPath::contains) |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 1185 | .function("_cubicTo", &ApplyCubicTo) |
Kevin Lubick | b9db390 | 2018-11-26 11:47:54 -0500 | [diff] [blame] | 1186 | .function("getPoint", &SkPath::getPoint) |
Kevin Lubick | 1646e7d | 2018-12-07 13:03:08 -0500 | [diff] [blame] | 1187 | .function("isEmpty", &SkPath::isEmpty) |
Kevin Lubick | 2b79d1c | 2018-12-14 16:10:38 -0500 | [diff] [blame] | 1188 | .function("isVolatile", &SkPath::isVolatile) |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 1189 | .function("_lineTo", &ApplyLineTo) |
| 1190 | .function("_moveTo", &ApplyMoveTo) |
Kevin Lubick | 79b7134 | 2019-11-01 14:36:52 -0400 | [diff] [blame] | 1191 | .function("_quadTo", &ApplyQuadTo) |
| 1192 | .function("_rArcTo", &ApplyRArcToArcSize) |
| 1193 | .function("_rConicTo", &ApplyRConicTo) |
| 1194 | .function("_rCubicTo", &ApplyRCubicTo) |
| 1195 | .function("_rLineTo", &ApplyRLineTo) |
| 1196 | .function("_rMoveTo", &ApplyRMoveTo) |
| 1197 | .function("_rQuadTo", &ApplyRQuadTo) |
Kevin Lubick | da3d8ac | 2019-01-07 11:08:55 -0500 | [diff] [blame] | 1198 | .function("reset", &ApplyReset) |
| 1199 | .function("rewind", &ApplyRewind) |
Kevin Lubick | 2b79d1c | 2018-12-14 16:10:38 -0500 | [diff] [blame] | 1200 | .function("setIsVolatile", &SkPath::setIsVolatile) |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 1201 | .function("_transform", select_overload<void(SkPath&, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar)>(&ApplyTransform)) |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 1202 | |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 1203 | // PathEffects |
| 1204 | .function("_dash", &ApplyDash) |
| 1205 | .function("_trim", &ApplyTrim) |
| 1206 | .function("_stroke", &ApplyStroke) |
| 1207 | |
| 1208 | // PathOps |
| 1209 | .function("_simplify", &ApplySimplify) |
| 1210 | .function("_op", &ApplyPathOp) |
| 1211 | |
| 1212 | // Exporting |
| 1213 | .function("toSVGString", &ToSVGString) |
Kevin Lubick | da3d8ac | 2019-01-07 11:08:55 -0500 | [diff] [blame] | 1214 | .function("toCmds", &ToCmds) |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 1215 | |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 1216 | .function("setFillType", &SkPath::setFillType) |
| 1217 | .function("getFillType", &SkPath::getFillType) |
| 1218 | .function("getBounds", &SkPath::getBounds) |
| 1219 | .function("computeTightBounds", &SkPath::computeTightBounds) |
| 1220 | .function("equals", &Equals) |
Kevin Lubick | b9db390 | 2018-11-26 11:47:54 -0500 | [diff] [blame] | 1221 | .function("copy", &CopyPath) |
| 1222 | #ifdef SK_DEBUG |
| 1223 | .function("dump", select_overload<void() const>(&SkPath::dump)) |
| 1224 | .function("dumpHex", select_overload<void() const>(&SkPath::dumpHex)) |
| 1225 | #endif |
| 1226 | ; |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 1227 | |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 1228 | class_<SkPathMeasure>("SkPathMeasure") |
| 1229 | .constructor<const SkPath&, bool, SkScalar>() |
| 1230 | .function("getLength", &SkPathMeasure::getLength) |
| 1231 | .function("getPosTan", optional_override([](SkPathMeasure& self, |
| 1232 | SkScalar distance) -> PosTan { |
| 1233 | SkPoint p{0, 0}; |
| 1234 | SkVector v{0, 0}; |
| 1235 | if (!self.getPosTan(distance, &p, &v)) { |
| 1236 | SkDebugf("zero-length path in getPosTan\n"); |
| 1237 | } |
| 1238 | return PosTan{p.x(), p.y(), v.x(), v.y()}; |
| 1239 | })) |
| 1240 | .function("isClosed", &SkPathMeasure::isClosed) |
| 1241 | .function("nextContour", &SkPathMeasure::nextContour); |
| 1242 | |
Kevin Lubick | cc13fd3 | 2019-04-05 13:00:01 -0400 | [diff] [blame] | 1243 | class_<SkPictureRecorder>("SkPictureRecorder") |
| 1244 | .constructor<>() |
| 1245 | .function("beginRecording", optional_override([](SkPictureRecorder& self, |
| 1246 | const SkRect& bounds) -> SkCanvas* { |
| 1247 | return self.beginRecording(bounds, nullptr, 0); |
| 1248 | }), allow_raw_pointers()) |
| 1249 | .function("finishRecordingAsPicture", optional_override([](SkPictureRecorder& self) |
| 1250 | -> sk_sp<SkPicture> { |
| 1251 | return self.finishRecordingAsPicture(0); |
| 1252 | }), allow_raw_pointers()); |
| 1253 | |
| 1254 | class_<SkPicture>("SkPicture") |
| 1255 | .smart_ptr<sk_sp<SkPicture>>("sk_sp<SkPicture>") |
| 1256 | // The serialized format of an SkPicture (informally called an "skp"), is not something |
| 1257 | // that clients should ever rely on. It is useful when filing bug reports, but that's |
| 1258 | // about it. The format may change at anytime and no promises are made for backwards |
| 1259 | // or forward compatibility. |
| 1260 | .function("DEBUGONLY_serialize", optional_override([](SkPicture& self) -> sk_sp<SkData> { |
| 1261 | // Emscripten doesn't play well with optional arguments, which we don't |
| 1262 | // want to expose anyway. |
| 1263 | return self.serialize(); |
| 1264 | }), allow_raw_pointers()); |
| 1265 | |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 1266 | class_<SkShader>("SkShader") |
| 1267 | .smart_ptr<sk_sp<SkShader>>("sk_sp<SkShader>"); |
| 1268 | |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 1269 | class_<SkSurface>("SkSurface") |
| 1270 | .smart_ptr<sk_sp<SkSurface>>("sk_sp<SkSurface>") |
Greg Daniel | bae7121 | 2019-03-01 15:24:35 -0500 | [diff] [blame] | 1271 | .function("_flush", select_overload<void()>(&SkSurface::flush)) |
Kevin Lubick | 543f352 | 2019-03-08 10:04:28 -0500 | [diff] [blame] | 1272 | .function("getCanvas", &SkSurface::getCanvas, allow_raw_pointers()) |
| 1273 | .function("height", &SkSurface::height) |
Mike Reed | 114bde8 | 2018-11-21 09:12:09 -0500 | [diff] [blame] | 1274 | .function("makeImageSnapshot", select_overload<sk_sp<SkImage>()>(&SkSurface::makeImageSnapshot)) |
| 1275 | .function("makeImageSnapshot", select_overload<sk_sp<SkImage>(const SkIRect& bounds)>(&SkSurface::makeImageSnapshot)) |
Kevin Lubick | 543f352 | 2019-03-08 10:04:28 -0500 | [diff] [blame] | 1276 | .function("makeSurface", optional_override([](SkSurface& self, SimpleImageInfo sii)->sk_sp<SkSurface> { |
| 1277 | return self.makeSurface(toSkImageInfo(sii)); |
| 1278 | }), allow_raw_pointers()) |
| 1279 | .function("width", &SkSurface::width); |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 1280 | |
Kevin Lubick | 61887c7 | 2019-09-26 13:20:50 -0400 | [diff] [blame] | 1281 | #ifndef SK_NO_FONTS |
Kevin Lubick | ec4903d | 2019-01-14 08:36:08 -0500 | [diff] [blame] | 1282 | class_<SkTextBlob>("SkTextBlob") |
| 1283 | .smart_ptr<sk_sp<SkTextBlob>>("sk_sp<SkTextBlob>>") |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 1284 | .class_function("_MakeFromRSXform", optional_override([](uintptr_t /* char* */ sptr, |
| 1285 | size_t strBtyes, |
| 1286 | uintptr_t /* SkRSXform* */ xptr, |
| 1287 | const SkFont& font, |
| 1288 | SkTextEncoding encoding)->sk_sp<SkTextBlob> { |
| 1289 | // See comment above for uintptr_t explanation |
| 1290 | const char* str = reinterpret_cast<const char*>(sptr); |
| 1291 | const SkRSXform* xforms = reinterpret_cast<const SkRSXform*>(xptr); |
| 1292 | |
| 1293 | return SkTextBlob::MakeFromRSXform(str, strBtyes, xforms, font, encoding); |
| 1294 | }), allow_raw_pointers()) |
Kevin Lubick | 543f352 | 2019-03-08 10:04:28 -0500 | [diff] [blame] | 1295 | .class_function("_MakeFromText", optional_override([](uintptr_t /* char* */ sptr, |
| 1296 | size_t len, const SkFont& font, |
| 1297 | SkTextEncoding encoding)->sk_sp<SkTextBlob> { |
Kevin Lubick | ec4903d | 2019-01-14 08:36:08 -0500 | [diff] [blame] | 1298 | // See comment above for uintptr_t explanation |
| 1299 | const char* str = reinterpret_cast<const char*>(sptr); |
| 1300 | return SkTextBlob::MakeFromText(str, len, font, encoding); |
| 1301 | }), allow_raw_pointers()); |
| 1302 | |
Kevin Lubick | ddd0a33 | 2018-12-12 10:35:13 -0500 | [diff] [blame] | 1303 | class_<SkTypeface>("SkTypeface") |
| 1304 | .smart_ptr<sk_sp<SkTypeface>>("sk_sp<SkTypeface>"); |
Kevin Lubick | 61887c7 | 2019-09-26 13:20:50 -0400 | [diff] [blame] | 1305 | #endif |
Kevin Lubick | ddd0a33 | 2018-12-12 10:35:13 -0500 | [diff] [blame] | 1306 | |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 1307 | class_<SkVertices>("SkVertices") |
| 1308 | .smart_ptr<sk_sp<SkVertices>>("sk_sp<SkVertices>") |
| 1309 | .function("_applyBones", optional_override([](SkVertices& self, uintptr_t /* Bone* */ bptr, int boneCount)->sk_sp<SkVertices> { |
| 1310 | // See comment above for uintptr_t explanation |
| 1311 | const Bone* bones = reinterpret_cast<const Bone*>(bptr); |
| 1312 | return self.applyBones(bones, boneCount); |
| 1313 | })) |
| 1314 | .function("bounds", &SkVertices::bounds) |
| 1315 | .function("mode", &SkVertices::mode) |
| 1316 | .function("uniqueID", &SkVertices::uniqueID) |
Kevin Lubick | 12c0e50 | 2018-11-28 12:51:56 -0500 | [diff] [blame] | 1317 | #ifdef SK_DEBUG |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 1318 | .function("dumpPositions", optional_override([](SkVertices& self)->void { |
| 1319 | auto pos = self.positions(); |
| 1320 | for(int i = 0; i< self.vertexCount(); i++) { |
| 1321 | SkDebugf("position[%d] = (%f, %f)\n", i, pos[i].x(), pos[i].y()); |
| 1322 | } |
| 1323 | })) |
Kevin Lubick | 12c0e50 | 2018-11-28 12:51:56 -0500 | [diff] [blame] | 1324 | #endif |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 1325 | .function("vertexCount", &SkVertices::vertexCount); |
| 1326 | |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 1327 | // Not intended to be called directly by clients |
| 1328 | class_<SkVertices::Builder>("_SkVerticesBuilder") |
| 1329 | .constructor<SkVertices::VertexMode, int, int, uint32_t>() |
| 1330 | .function("boneIndices", optional_override([](SkVertices::Builder& self)->uintptr_t /* BoneIndices* */{ |
| 1331 | // Emscripten won't let us return bare pointers, but we can return ints just fine. |
| 1332 | return reinterpret_cast<uintptr_t>(self.boneIndices()); |
| 1333 | })) |
| 1334 | .function("boneWeights", optional_override([](SkVertices::Builder& self)->uintptr_t /* BoneWeights* */{ |
| 1335 | // Emscripten won't let us return bare pointers, but we can return ints just fine. |
| 1336 | return reinterpret_cast<uintptr_t>(self.boneWeights()); |
| 1337 | })) |
| 1338 | .function("colors", optional_override([](SkVertices::Builder& self)->uintptr_t /* SkColor* */{ |
| 1339 | // Emscripten won't let us return bare pointers, but we can return ints just fine. |
| 1340 | return reinterpret_cast<uintptr_t>(self.colors()); |
| 1341 | })) |
| 1342 | .function("detach", &SkVertices::Builder::detach) |
| 1343 | .function("indices", optional_override([](SkVertices::Builder& self)->uintptr_t /* uint16_t* */{ |
| 1344 | // Emscripten won't let us return bare pointers, but we can return ints just fine. |
| 1345 | return reinterpret_cast<uintptr_t>(self.indices()); |
| 1346 | })) |
| 1347 | .function("positions", optional_override([](SkVertices::Builder& self)->uintptr_t /* SkPoint* */{ |
| 1348 | // Emscripten won't let us return bare pointers, but we can return ints just fine. |
| 1349 | return reinterpret_cast<uintptr_t>(self.positions()); |
| 1350 | })) |
| 1351 | .function("texCoords", optional_override([](SkVertices::Builder& self)->uintptr_t /* SkPoint* */{ |
| 1352 | // Emscripten won't let us return bare pointers, but we can return ints just fine. |
| 1353 | return reinterpret_cast<uintptr_t>(self.texCoords()); |
| 1354 | })); |
| 1355 | |
Kevin Lubick | ea905ec | 2018-11-30 14:05:58 -0500 | [diff] [blame] | 1356 | enum_<SkAlphaType>("AlphaType") |
| 1357 | .value("Opaque", SkAlphaType::kOpaque_SkAlphaType) |
| 1358 | .value("Premul", SkAlphaType::kPremul_SkAlphaType) |
| 1359 | .value("Unpremul", SkAlphaType::kUnpremul_SkAlphaType); |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 1360 | |
| 1361 | enum_<SkBlendMode>("BlendMode") |
| 1362 | .value("Clear", SkBlendMode::kClear) |
| 1363 | .value("Src", SkBlendMode::kSrc) |
| 1364 | .value("Dst", SkBlendMode::kDst) |
| 1365 | .value("SrcOver", SkBlendMode::kSrcOver) |
| 1366 | .value("DstOver", SkBlendMode::kDstOver) |
| 1367 | .value("SrcIn", SkBlendMode::kSrcIn) |
| 1368 | .value("DstIn", SkBlendMode::kDstIn) |
| 1369 | .value("SrcOut", SkBlendMode::kSrcOut) |
| 1370 | .value("DstOut", SkBlendMode::kDstOut) |
| 1371 | .value("SrcATop", SkBlendMode::kSrcATop) |
| 1372 | .value("DstATop", SkBlendMode::kDstATop) |
| 1373 | .value("Xor", SkBlendMode::kXor) |
| 1374 | .value("Plus", SkBlendMode::kPlus) |
| 1375 | .value("Modulate", SkBlendMode::kModulate) |
| 1376 | .value("Screen", SkBlendMode::kScreen) |
| 1377 | .value("Overlay", SkBlendMode::kOverlay) |
| 1378 | .value("Darken", SkBlendMode::kDarken) |
| 1379 | .value("Lighten", SkBlendMode::kLighten) |
| 1380 | .value("ColorDodge", SkBlendMode::kColorDodge) |
| 1381 | .value("ColorBurn", SkBlendMode::kColorBurn) |
| 1382 | .value("HardLight", SkBlendMode::kHardLight) |
| 1383 | .value("SoftLight", SkBlendMode::kSoftLight) |
| 1384 | .value("Difference", SkBlendMode::kDifference) |
| 1385 | .value("Exclusion", SkBlendMode::kExclusion) |
| 1386 | .value("Multiply", SkBlendMode::kMultiply) |
| 1387 | .value("Hue", SkBlendMode::kHue) |
| 1388 | .value("Saturation", SkBlendMode::kSaturation) |
| 1389 | .value("Color", SkBlendMode::kColor) |
| 1390 | .value("Luminosity", SkBlendMode::kLuminosity); |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 1391 | |
Kevin Lubick | 61ef7b2 | 2018-11-27 13:26:59 -0500 | [diff] [blame] | 1392 | enum_<SkBlurStyle>("BlurStyle") |
| 1393 | .value("Normal", SkBlurStyle::kNormal_SkBlurStyle) |
| 1394 | .value("Solid", SkBlurStyle::kSolid_SkBlurStyle) |
| 1395 | .value("Outer", SkBlurStyle::kOuter_SkBlurStyle) |
| 1396 | .value("Inner", SkBlurStyle::kInner_SkBlurStyle); |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 1397 | |
Kevin Lubick | eb2f6b0 | 2018-11-29 15:07:02 -0500 | [diff] [blame] | 1398 | enum_<SkClipOp>("ClipOp") |
| 1399 | .value("Difference", SkClipOp::kDifference) |
| 1400 | .value("Intersect", SkClipOp::kIntersect); |
| 1401 | |
Kevin Lubick | ea905ec | 2018-11-30 14:05:58 -0500 | [diff] [blame] | 1402 | enum_<SkColorType>("ColorType") |
| 1403 | .value("Alpha_8", SkColorType::kAlpha_8_SkColorType) |
| 1404 | .value("RGB_565", SkColorType::kRGB_565_SkColorType) |
| 1405 | .value("ARGB_4444", SkColorType::kARGB_4444_SkColorType) |
| 1406 | .value("RGBA_8888", SkColorType::kRGBA_8888_SkColorType) |
| 1407 | .value("RGB_888x", SkColorType::kRGB_888x_SkColorType) |
| 1408 | .value("BGRA_8888", SkColorType::kBGRA_8888_SkColorType) |
| 1409 | .value("RGBA_1010102", SkColorType::kRGBA_1010102_SkColorType) |
| 1410 | .value("RGB_101010x", SkColorType::kRGB_101010x_SkColorType) |
| 1411 | .value("Gray_8", SkColorType::kGray_8_SkColorType) |
| 1412 | .value("RGBA_F16", SkColorType::kRGBA_F16_SkColorType) |
Robert Phillips | d470e1b | 2019-09-04 15:05:35 -0400 | [diff] [blame] | 1413 | .value("RGBA_F32", SkColorType::kRGBA_F32_SkColorType) |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 1414 | .value("R8G8_unorm", SkColorType::kR8G8_unorm_SkColorType) |
| 1415 | .value("A16_unorm", SkColorType::kA16_unorm_SkColorType) |
| 1416 | .value("R16G16_unorm", SkColorType::kR16G16_unorm_SkColorType) |
| 1417 | .value("A16_float", SkColorType::kA16_float_SkColorType) |
| 1418 | .value("R16G16_float", SkColorType::kR16G16_float_SkColorType) |
| 1419 | .value("R16G16B16A16_unorm", SkColorType::kR16G16B16A16_unorm_SkColorType); |
Kevin Lubick | ea905ec | 2018-11-30 14:05:58 -0500 | [diff] [blame] | 1420 | |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 1421 | enum_<SkPath::FillType>("FillType") |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 1422 | .value("Winding", SkPath::FillType::kWinding_FillType) |
| 1423 | .value("EvenOdd", SkPath::FillType::kEvenOdd_FillType) |
| 1424 | .value("InverseWinding", SkPath::FillType::kInverseWinding_FillType) |
| 1425 | .value("InverseEvenOdd", SkPath::FillType::kInverseEvenOdd_FillType); |
| 1426 | |
Kevin Lubick | 0a1293c | 2018-12-03 12:31:04 -0500 | [diff] [blame] | 1427 | enum_<SkFilterQuality>("FilterQuality") |
| 1428 | .value("None", SkFilterQuality::kNone_SkFilterQuality) |
| 1429 | .value("Low", SkFilterQuality::kLow_SkFilterQuality) |
| 1430 | .value("Medium", SkFilterQuality::kMedium_SkFilterQuality) |
| 1431 | .value("High", SkFilterQuality::kHigh_SkFilterQuality); |
| 1432 | |
Kevin Lubick | 61ef7b2 | 2018-11-27 13:26:59 -0500 | [diff] [blame] | 1433 | enum_<SkEncodedImageFormat>("ImageFormat") |
| 1434 | .value("PNG", SkEncodedImageFormat::kPNG) |
| 1435 | .value("JPEG", SkEncodedImageFormat::kJPEG); |
| 1436 | |
| 1437 | enum_<SkPaint::Style>("PaintStyle") |
| 1438 | .value("Fill", SkPaint::Style::kFill_Style) |
| 1439 | .value("Stroke", SkPaint::Style::kStroke_Style) |
| 1440 | .value("StrokeAndFill", SkPaint::Style::kStrokeAndFill_Style); |
| 1441 | |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 1442 | enum_<SkPathOp>("PathOp") |
| 1443 | .value("Difference", SkPathOp::kDifference_SkPathOp) |
| 1444 | .value("Intersect", SkPathOp::kIntersect_SkPathOp) |
| 1445 | .value("Union", SkPathOp::kUnion_SkPathOp) |
| 1446 | .value("XOR", SkPathOp::kXOR_SkPathOp) |
| 1447 | .value("ReverseDifference", SkPathOp::kReverseDifference_SkPathOp); |
| 1448 | |
Kevin Lubick | b9db390 | 2018-11-26 11:47:54 -0500 | [diff] [blame] | 1449 | enum_<SkPaint::Cap>("StrokeCap") |
| 1450 | .value("Butt", SkPaint::Cap::kButt_Cap) |
| 1451 | .value("Round", SkPaint::Cap::kRound_Cap) |
| 1452 | .value("Square", SkPaint::Cap::kSquare_Cap); |
| 1453 | |
| 1454 | enum_<SkPaint::Join>("StrokeJoin") |
| 1455 | .value("Miter", SkPaint::Join::kMiter_Join) |
| 1456 | .value("Round", SkPaint::Join::kRound_Join) |
| 1457 | .value("Bevel", SkPaint::Join::kBevel_Join); |
| 1458 | |
Kevin Lubick | ec4903d | 2019-01-14 08:36:08 -0500 | [diff] [blame] | 1459 | enum_<SkTextEncoding>("TextEncoding") |
| 1460 | .value("UTF8", SkTextEncoding::kUTF8) |
| 1461 | .value("UTF16", SkTextEncoding::kUTF16) |
| 1462 | .value("UTF32", SkTextEncoding::kUTF32) |
| 1463 | .value("GlyphID", SkTextEncoding::kGlyphID); |
Kevin Lubick | b9db390 | 2018-11-26 11:47:54 -0500 | [diff] [blame] | 1464 | |
Mike Reed | 5c5de21 | 2019-04-03 16:51:47 -0400 | [diff] [blame] | 1465 | enum_<SkTileMode>("TileMode") |
| 1466 | .value("Clamp", SkTileMode::kClamp) |
| 1467 | .value("Repeat", SkTileMode::kRepeat) |
| 1468 | .value("Mirror", SkTileMode::kMirror) |
Mike Reed | 5c5de21 | 2019-04-03 16:51:47 -0400 | [diff] [blame] | 1469 | .value("Decal", SkTileMode::kDecal); |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 1470 | |
| 1471 | enum_<SkVertices::VertexMode>("VertexMode") |
| 1472 | .value("Triangles", SkVertices::VertexMode::kTriangles_VertexMode) |
| 1473 | .value("TrianglesStrip", SkVertices::VertexMode::kTriangleStrip_VertexMode) |
Kevin Lubick | b9db390 | 2018-11-26 11:47:54 -0500 | [diff] [blame] | 1474 | .value("TriangleFan", SkVertices::VertexMode::kTriangleFan_VertexMode); |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 1475 | |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 1476 | |
| 1477 | // A value object is much simpler than a class - it is returned as a JS |
| 1478 | // object and does not require delete(). |
| 1479 | // https://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/embind.html#value-types |
Kevin Lubick | 1ba9c4d | 2019-02-22 10:04:06 -0500 | [diff] [blame] | 1480 | value_object<ShapedTextOpts>("ShapedTextOpts") |
| 1481 | .field("font", &ShapedTextOpts::font) |
| 1482 | .field("leftToRight", &ShapedTextOpts::leftToRight) |
| 1483 | .field("text", &ShapedTextOpts::text) |
| 1484 | .field("width", &ShapedTextOpts::width); |
| 1485 | |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 1486 | value_object<SkRect>("SkRect") |
| 1487 | .field("fLeft", &SkRect::fLeft) |
| 1488 | .field("fTop", &SkRect::fTop) |
| 1489 | .field("fRight", &SkRect::fRight) |
| 1490 | .field("fBottom", &SkRect::fBottom); |
| 1491 | |
Kevin Lubick | 2e5fe35 | 2019-09-03 12:59:06 -0400 | [diff] [blame] | 1492 | value_object<SimpleRRect>("SkRRect") |
| 1493 | .field("rect", &SimpleRRect::rect) |
Kevin Lubick | 7d644e1 | 2019-09-11 14:22:22 -0400 | [diff] [blame] | 1494 | .field("rx1", &SimpleRRect::rx1) |
| 1495 | .field("ry1", &SimpleRRect::ry1) |
| 1496 | .field("rx2", &SimpleRRect::rx2) |
| 1497 | .field("ry2", &SimpleRRect::ry2) |
| 1498 | .field("rx3", &SimpleRRect::rx3) |
| 1499 | .field("ry3", &SimpleRRect::ry3) |
| 1500 | .field("rx4", &SimpleRRect::rx4) |
| 1501 | .field("ry4", &SimpleRRect::ry4); |
Kevin Lubick | 2e5fe35 | 2019-09-03 12:59:06 -0400 | [diff] [blame] | 1502 | |
Mike Reed | 114bde8 | 2018-11-21 09:12:09 -0500 | [diff] [blame] | 1503 | value_object<SkIRect>("SkIRect") |
| 1504 | .field("fLeft", &SkIRect::fLeft) |
| 1505 | .field("fTop", &SkIRect::fTop) |
| 1506 | .field("fRight", &SkIRect::fRight) |
| 1507 | .field("fBottom", &SkIRect::fBottom); |
| 1508 | |
Kevin Lubick | 50f377e | 2019-09-11 15:23:00 -0400 | [diff] [blame] | 1509 | value_object<TonalColors>("TonalColors") |
| 1510 | .field("ambient", &TonalColors::ambientColor) |
| 1511 | .field("spot", &TonalColors::spotColor); |
| 1512 | |
Kevin Lubick | ea905ec | 2018-11-30 14:05:58 -0500 | [diff] [blame] | 1513 | value_object<SimpleImageInfo>("SkImageInfo") |
| 1514 | .field("width", &SimpleImageInfo::width) |
| 1515 | .field("height", &SimpleImageInfo::height) |
| 1516 | .field("colorType", &SimpleImageInfo::colorType) |
| 1517 | .field("alphaType", &SimpleImageInfo::alphaType); |
| 1518 | |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 1519 | // SkPoints can be represented by [x, y] |
| 1520 | value_array<SkPoint>("SkPoint") |
| 1521 | .element(&SkPoint::fX) |
| 1522 | .element(&SkPoint::fY); |
| 1523 | |
Kevin Lubick | 61ef7b2 | 2018-11-27 13:26:59 -0500 | [diff] [blame] | 1524 | // SkPoint3s can be represented by [x, y, z] |
| 1525 | value_array<SkPoint3>("SkPoint3") |
| 1526 | .element(&SkPoint3::fX) |
| 1527 | .element(&SkPoint3::fY) |
| 1528 | .element(&SkPoint3::fZ); |
| 1529 | |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 1530 | // PosTan can be represented by [px, py, tx, ty] |
| 1531 | value_array<PosTan>("PosTan") |
| 1532 | .element(&PosTan::px) |
| 1533 | .element(&PosTan::py) |
| 1534 | .element(&PosTan::tx) |
| 1535 | .element(&PosTan::ty); |
| 1536 | |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 1537 | // {"w": Number, "h", Number} |
| 1538 | value_object<SkSize>("SkSize") |
| 1539 | .field("w", &SkSize::fWidth) |
| 1540 | .field("h", &SkSize::fHeight); |
| 1541 | |
| 1542 | value_object<SkISize>("SkISize") |
| 1543 | .field("w", &SkISize::fWidth) |
| 1544 | .field("h", &SkISize::fHeight); |
| 1545 | |
Kevin Lubick | ec4903d | 2019-01-14 08:36:08 -0500 | [diff] [blame] | 1546 | value_object<StrokeOpts>("StrokeOpts") |
| 1547 | .field("width", &StrokeOpts::width) |
| 1548 | .field("miter_limit", &StrokeOpts::miter_limit) |
| 1549 | .field("join", &StrokeOpts::join) |
| 1550 | .field("cap", &StrokeOpts::cap) |
| 1551 | .field("precision", &StrokeOpts::precision); |
| 1552 | |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 1553 | // Allows clients to supply a 1D array of 9 elements and the bindings |
| 1554 | // will automatically turn it into a 3x3 2D matrix. |
| 1555 | // e.g. path.transform([0,1,2,3,4,5,6,7,8]) |
| 1556 | // This is likely simpler for the client than exposing SkMatrix |
| 1557 | // directly and requiring them to do a lot of .delete(). |
| 1558 | value_array<SimpleMatrix>("SkMatrix") |
| 1559 | .element(&SimpleMatrix::scaleX) |
| 1560 | .element(&SimpleMatrix::skewX) |
| 1561 | .element(&SimpleMatrix::transX) |
| 1562 | |
| 1563 | .element(&SimpleMatrix::skewY) |
| 1564 | .element(&SimpleMatrix::scaleY) |
| 1565 | .element(&SimpleMatrix::transY) |
| 1566 | |
| 1567 | .element(&SimpleMatrix::pers0) |
| 1568 | .element(&SimpleMatrix::pers1) |
| 1569 | .element(&SimpleMatrix::pers2); |
| 1570 | |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 1571 | constant("TRANSPARENT", SK_ColorTRANSPARENT); |
| 1572 | constant("RED", SK_ColorRED); |
| 1573 | constant("BLUE", SK_ColorBLUE); |
| 1574 | constant("YELLOW", SK_ColorYELLOW); |
| 1575 | constant("CYAN", SK_ColorCYAN); |
| 1576 | constant("BLACK", SK_ColorBLACK); |
| 1577 | constant("WHITE", SK_ColorWHITE); |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 1578 | // TODO(?) |
| 1579 | |
Kevin Lubick | da3d8ac | 2019-01-07 11:08:55 -0500 | [diff] [blame] | 1580 | constant("MOVE_VERB", MOVE); |
| 1581 | constant("LINE_VERB", LINE); |
| 1582 | constant("QUAD_VERB", QUAD); |
| 1583 | constant("CONIC_VERB", CONIC); |
| 1584 | constant("CUBIC_VERB", CUBIC); |
| 1585 | constant("CLOSE_VERB", CLOSE); |
Kevin Lubick | 77d9b5c | 2019-10-29 10:48:26 -0400 | [diff] [blame] | 1586 | |
| 1587 | constant("SaveLayerInitWithPrevious", SkCanvas::SaveLayerFlagsSet::kInitWithPrevious_SaveLayerFlag); |
| 1588 | constant("SaveLayerF16ColorType", SkCanvas::SaveLayerFlagsSet::kF16ColorType); |
| 1589 | |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 1590 | } |