// Copyright (c) 2017-2019 TMS Software. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef flxdraw_DEFINED
#define flxdraw_DEFINED
#include "include/core/SkData.h"
#include "include/core/SkStream.h"
#include "include/core/SkImage.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkFont.h"
#include "include/core/SkSurface.h"
#include "include/core/SkTypeface.h"
#include "include/core/SkPathEffect.h"
#include "include/core/SkColorFilter.h"
#include "include/effects/SkColorMatrix.h"
#include "include/effects/SkDashPathEffect.h"
#include "include/effects/SkGradientShader.h"
//#include "harfbuzz/hb.h"
#pragma pack(8)
#define EXPORT int32_t
#define START_EXPORT
#define END_EXPORT return 0;
#define SK_CANVAS ((SkCanvas*) aCanvas)
#define CHECK_SK_CANVAS if (!aCanvas) return -1;
#define SK_IMAGE ((SkImage*) aImage)
#define CHECK_SK_IMAGE if (!aImage) return -1;
#define SK_SURFACE ((SkSurface*) aSurface)
#define CHECK_SK_SURFACE if (!aSurface) return -1;
#define SK_PAINT ((SkPaint*) aPaint)
#define CHECK_SK_PAINT if (!aPaint) return -1;
#define SK_FONT ((SkFont*) aFont)
#define CHECK_SK_FONT if (!aFont) return -1;
#define SK_DATA ((SkData*) aData)
#define CHECK_SK_DATA if (!aData) return -1;
#define SK_STREAM ((SkStream*) aStream)
#define CHECK_SK_STREAM if (!aStream) return -1;
#define SK_STREAMASSET ((SkStreamAsset*) aStream)
#define CHECK_SK_STREAMASSET if (!aStream) return -1;
#define SK_STRING ((SkString*) aString)
#define CHECK_SK_STRING if (!aString) return -1;
#define SK_PATH ((SkPath*) aPath)
#define CHECK_SK_PATH if (!aPath) return -1;
#define SK_TYPEFACE ((SkTypeface*) aTypeface)
#define CHECK_SK_TYPEFACE if (!aTypeface) return -1;
#define SK_SHADER ((SkShader*) aShader)
#define SK_COLOR_FILTER ((SkColorFilter*)aFilter)
#define SK_PATH_EFFECT ((SkPathEffect*)aPathEffect)
typedef void *PSkCanvas;
typedef void *PSkImage;
typedef void *PSkSurface;
typedef void *PSkPaint;
typedef void *PSkFont;
typedef void *PSkPath;
typedef void *PSkTypeface;
typedef void *PSkString;
typedef void *PSkData;
typedef void *PSkStreamAsset;
typedef void *PSkShader;
typedef void *PSkColorFilter;
typedef void *PSkPathEffect;
typedef SkScalar *PSkScalar;
typedef SkColor *PSkColor;
struct CSkMatrix {
SkScalar ScaleX;
SkScalar SkewX;
SkScalar TransX;
SkScalar SkewY;
SkScalar ScaleY;
SkScalar TransY;
SkScalar Persp0;
SkScalar Persp1;
SkScalar Persp2;
static CSkMatrix FromSkMatrix(SkMatrix m) {
CSkMatrix Result{};
Result.ScaleX = m.getScaleX();
Result.SkewX = m.getSkewX();
Result.TransX = m.getTranslateX();
Result.SkewY = m.getSkewY();
Result.ScaleY = m.getScaleY();
Result.TransY = m.getTranslateX();
Result.Persp0 = m.getPerspX();
Result.Persp1 = m.getPerspY();
Result.Persp2 = m.getPerspX();
Result.Persp2 = m[8];
return Result;
}
SkMatrix GetSkMatrix() {
SkMatrix m;
m.setAll(ScaleX, SkewX, TransX, SkewY, ScaleY, TransY, Persp0, Persp1, Persp2);
return m;
}
};
typedef CSkMatrix *PCSkMatrix;
struct CSkPoint {
SkScalar fX, fY;
SkPoint ToSkPoint() {
return SkPoint::Make(fX, fY);
}
};
typedef SkPoint *PSkPoint;
struct CSkRect {
SkScalar fLeft, fTop, fRight, fBottom;
SkRect ToSkRect() {
return SkRect::MakeLTRB(fLeft, fTop, fRight, fBottom);
}
};
extern "C"
{
EXPORT SkiaReleaseSkData(void *aData) {
SkSafeUnref(SK_DATA);
return 0;
}
EXPORT SkiaReleaseSkStream(void *aStream) {
if (aStream) delete SK_STREAM;
return 0;
}
EXPORT SkiaReleaseSkImage(void *aImage) {
SkSafeUnref(SK_IMAGE);
return 0;
}
EXPORT SkiaReleaseSkSurface(void *aSurface) {
SkSafeUnref(SK_SURFACE);
return 0;
}
EXPORT SkiaReleaseSkTypeface(void *aTypeface) {
SkSafeUnref(SK_TYPEFACE);
return 0;
}
EXPORT SkiaReleaseSkShader(void *aShader) {
SkSafeUnref(SK_SHADER);
return 0;
}
EXPORT SkiaReleaseSkColorFilter(void *aFilter) {
SkSafeUnref(SK_COLOR_FILTER);
return 0;
}
EXPORT SkiaReleaseSkPathEffect(void *aPathEffect) {
SkSafeUnref(SK_PATH_EFFECT);
return 0;
}
EXPORT SkiaDeleteSkString(PSkString aString) {
if (aString) delete SK_STRING;
return 0;
}
EXPORT SkiaDeleteSkPaint(PSkPaint aPaint) {
if (aPaint) delete SK_PAINT;
return 0;
}
EXPORT SkiaDeleteSkFont(PSkFont aFont) {
if (aFont) delete SK_FONT;
return 0;
}
EXPORT SkiaDeleteSkPath(PSkPath aPath) {
if (aPath) delete SK_PATH;
return 0;
}
EXPORT SkCanvasSave(PSkCanvas aCanvas) {
START_EXPORT
CHECK_SK_CANVAS
SK_CANVAS->save();
END_EXPORT
}
EXPORT SkCanvasRestore(PSkCanvas aCanvas) {
START_EXPORT
CHECK_SK_CANVAS
SK_CANVAS->restore();
END_EXPORT
}
EXPORT SkCanvasTranslate(PSkCanvas aCanvas, SkScalar dx, SkScalar dy) {
START_EXPORT
CHECK_SK_CANVAS
SK_CANVAS->translate(dx, dy);
END_EXPORT
}
EXPORT SkCanvasScale(PSkCanvas aCanvas, SkScalar sx, SkScalar sy) {
START_EXPORT
CHECK_SK_CANVAS
SK_CANVAS->scale(sx, sy);
END_EXPORT
}
EXPORT SkCanvasRotate(PSkCanvas aCanvas, SkScalar degrees) {
START_EXPORT
CHECK_SK_CANVAS
SK_CANVAS->rotate(degrees);
END_EXPORT
}
EXPORT SkCanvasGetTotalMatrix(PSkCanvas aCanvas, CSkMatrix &matrix) {
START_EXPORT
CHECK_SK_CANVAS
SkMatrix m = SK_CANVAS->getTotalMatrix();
matrix = CSkMatrix::FromSkMatrix(m);
END_EXPORT
}
EXPORT SkCanvasDrawImageRect(PSkCanvas aCanvas, PSkImage aImage, CSkRect *src, CSkRect *dst, PSkPaint aPaint) {
START_EXPORT
CHECK_SK_CANVAS
CHECK_SK_IMAGE
if (dst == nullptr)
{
if (src == nullptr) return -2;
dst = src;
}
if (src == nullptr)
{
SK_CANVAS->drawImageRect(SK_IMAGE, dst->ToSkRect(), SK_PAINT);
} else {
SK_CANVAS->drawImageRect(SK_IMAGE, src->ToSkRect(), dst->ToSkRect(), SK_PAINT);
}
END_EXPORT
}
EXPORT SkCanvasDrawImage(PSkCanvas aCanvas, PSkImage aImage, SkScalar left, SkScalar top, PSkPaint aPaint) {
START_EXPORT
CHECK_SK_CANVAS
CHECK_SK_IMAGE
SK_CANVAS->drawImage(SK_IMAGE, left, top, SK_PAINT);
END_EXPORT
}
EXPORT SkCanvasDrawColor(PSkCanvas aCanvas, SkColor color) {
START_EXPORT
CHECK_SK_CANVAS
SK_CANVAS->drawColor(color);
END_EXPORT
}
EXPORT SkCanvasDrawPath(PSkCanvas aCanvas, PSkPath aPath, PSkPaint aPaint) {
START_EXPORT
CHECK_SK_CANVAS
CHECK_SK_PATH
CHECK_SK_PAINT
SK_CANVAS->draw