Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fabric gcanvas #156

Merged
merged 15 commits into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ before_script:
script:
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then cppcheck --error-exitcode=0 --enable=warning,style,unusedFunction --std=c++11 --verbose --language=c++ core/src; fi
- cd ./core/test/linux && mkdir build && cd build && cmake ../ && make
- cp ../util/font/wqy-microhei.ttc ./ && cp ../util/font/.fontcache ./
- mkdir -p ~/.gAssets/fonts/ && cp ../util/font/wqy-microhei.ttc ~/.gAssets/fonts/wqy-microhei.ttc && cp ../util/font/.fontcache ~/.gAssets/fonts/.fontcache
- "./gcanvasTest"
- cat result.txt | awk '{printf "%-30s| %-18s| %-20s\n",$1,$2,$3}'| column -t
- cd ../../../../ && cd ./node/ && local=true npm install && npm run dev
- cd ../../../../ && npm install cmake-js -g && cd ./node/ && local=true npm install && npm run dev
- case=app.js npm run test


18 changes: 9 additions & 9 deletions core/src/platform/Linux/FontTool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ namespace NSFontTool
source.c_str());
}
ASSERT(face);
// printf("the get face called \n");
// INFO("the get face called \n");
return face;
}
else if (sourceType == TST_NET)
Expand Down Expand Up @@ -283,7 +283,7 @@ namespace NSFontTool

size_t nFaces = 0;
cacheFile.read((char *)(&nFaces), sizeof(size_t));
INFO("Importing %lu typefaces...", nFaces);
// INFO("Importing %lu typefaces...", nFaces);
mFaces.resize(nFaces);
std::string home(getenv("HOME"));
std::string pathPreFix = home + FONT_PATH;
Expand Down Expand Up @@ -311,10 +311,10 @@ namespace NSFontTool
#undef READ_STDSTR

face.source = pathPreFix + face.source;
// printf("the face.source is %s \n",face.source.c_str());
// printf("the face.psName is %s \n",face.psName.c_str());
// printf("the face.familyName is %s \n",face.familyName.c_str());
// printf("the face.styleName is %s \n",face.styleName.c_str());
// INFO("the face.source is %s \n",face.source.c_str());
// INFO("the face.psName is %s \n",face.psName.c_str());
// INFO("the face.familyName is %s \n",face.familyName.c_str());
// INFO("the face.styleName is %s \n",face.styleName.c_str());
// this->dumpFontCache();
}

Expand Down Expand Up @@ -571,12 +571,12 @@ namespace NSFontTool
Typeface *selectTypeface(wchar_t charcode, const std::string &key = "")
{
const std::vector<Typeface *> &faces = selectTypefaces(key);
// printf("the key is %s \n", key.c_str());
// INFO("the key is %s \n", key.c_str());
for (auto it = faces.rbegin(); it != faces.rend(); it++)
{
FT_Face face = (*it)->getFace();
std::string str = (*it)->psName;
// printf("the it fontName is %s \n", str.c_str());
// INFO("the it fontName is %s \n", str.c_str());
FT_UInt glyphIndex = FT_Get_Char_Index(face, charcode);
if (glyphIndex != 0)
{
Expand Down Expand Up @@ -682,7 +682,7 @@ namespace NSFontTool
{
ASSERT(mTypefaceProvider);
Typeface *t = nullptr;
printf("loadFaceOfChar caled \n");
// INFO("loadFaceOfChar caled \n");
// find best-matched typeface containing this glyph
t = mTypefaceProvider->selectTypeface(charcode, mFontName);

Expand Down
140 changes: 111 additions & 29 deletions node/binding/Canvas.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* Created by G-Canvas Open Source Team.
* Copyright (c) 2017, Alibaba, Inc. All rights reserved.
*
* This source code is licensed under the Apache Licence 2.0.
* For the full copyright and license information, please view
* the LICENSE file in the root directory of this source tree.
*/
#include "Canvas.h"
#include "TextMetrics.h"
namespace NodeBinding
Expand All @@ -14,8 +22,6 @@ namespace NodeBinding
mHeight = info[1].As<Napi::Number>().Int32Value();
mRenderContext = std::make_shared<GRenderContext>(mWidth, mHeight);
mRenderContext->initRenderEnviroment();


}

int Canvas::getWidth()
Expand Down Expand Up @@ -53,7 +59,8 @@ namespace NodeBinding
InstanceMethod("createJPEG", &Canvas::createJPEG),
InstanceMethod("createPNGStreamSync", &Canvas::createPNGStreamSync),
InstanceMethod("createJPGStreamSync", &Canvas::createJPGStreamSync),
});
InstanceMethod("toBuffer", &Canvas::ToBuffer),
});
constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
return;
Expand Down Expand Up @@ -122,65 +129,140 @@ namespace NodeBinding
Napi::Value Canvas::createJPGStreamSync(const Napi::CallbackInfo &info)
{
NodeBinding::checkArgs(info, 2);
Napi::Function callback = info[0].As<Napi::Function>();
if (this->mRenderContext)
{
this->mRenderContext->makeCurrent();
this->mRenderContext->drawFrame();
}
unsigned char *data = (unsigned char *)malloc(1 * sizeof(unsigned char));
unsigned long size = 0;
int ret = this->mRenderContext->getImagePixelJPG(&data, size);
if (ret == 0 && size > 0)
Napi::Buffer<unsigned char> buffer = this->getJPGBuffer(info, size);
if (size >= 0)
{
Napi::Function callback = info[0].As<Napi::Function>();
//handlescope 表示作用域,一般调用callback函数时使用
Napi::HandleScope scope(info.Env());
Napi::Buffer<unsigned char> buffer = Napi::Buffer<unsigned char>::Copy(info.Env(), data, size);
callback.Call({info.Env().Null(),
buffer,
Napi::Number::New(info.Env(), size)});
}
else
{
Napi::Function callback = info[0].As<Napi::Function>();
Napi::HandleScope scope(info.Env());
callback.Call({Napi::String::New(Env(), "createJPGStreamFail"),
info.Env().Null(),
info.Env().Null()});
}
if (data)
{
delete data;
data = nullptr;
}
return info.Env().Undefined();
}

Napi::Value Canvas::createPNGStreamSync(const Napi::CallbackInfo &info)
{
NodeBinding::checkArgs(info, 2);
Napi::Function callback = info[0].As<Napi::Function>();
if (this->mRenderContext)
{
this->mRenderContext->makeCurrent();
this->mRenderContext->drawFrame();
}
std::vector<unsigned char> in;
int ret = this->mRenderContext->getImagePixelPNG(in);
if (ret == 0)
unsigned long size = 0;
Napi::Buffer<unsigned char> buffer = this->getPNGBuffer(info, size);
if (size >= 0)
{
Napi::Function callback = info[0].As<Napi::Function>();
//handlescope 表示作用域,一般调用callback函数时使用
Napi::HandleScope scope(info.Env());
Napi::Buffer<unsigned char> buffer = Napi::Buffer<unsigned char>::Copy(info.Env(), &in[0], in.size());
callback.Call({info.Env().Null(),
buffer,
Napi::Number::New(info.Env(), in.size())});
Napi::Number::New(info.Env(), size)});
}
else
{
Napi::Function callback = info[0].As<Napi::Function>();
Napi::HandleScope scope(info.Env());
callback.Call({Napi::String::New(Env(), "createPNGStreamFail"),
info.Env().Null(),
info.Env().Null()});
}
return info.Env().Undefined();
}
Napi::Buffer<unsigned char> Canvas::getPNGBuffer(const Napi::CallbackInfo &info, unsigned long &size)
{
if (this->mRenderContext)
{
this->mRenderContext->makeCurrent();
this->mRenderContext->drawFrame();
}
std::vector<unsigned char> in;
int ret = this->mRenderContext->getImagePixelPNG(in);
if (ret == 0)
{
size = in.size();
return Napi::Buffer<unsigned char>::Copy(info.Env(), &in[0], in.size());
}
else
{
return Napi::Buffer<unsigned char>::New(info.Env(), nullptr, 0);
}
}
Napi::Buffer<unsigned char> Canvas::getJPGBuffer(const Napi::CallbackInfo &info, unsigned long &size)
{
if (this->mRenderContext)
{
this->mRenderContext->makeCurrent();
this->mRenderContext->drawFrame();
}
unsigned char *data = nullptr;
int ret = this->mRenderContext->getImagePixelJPG(&data, size);
if (ret == 0)
{
return Napi::Buffer<unsigned char>::Copy(info.Env(), data, size);
}
else
{
size = -1;
return Napi::Buffer<unsigned char>::New(info.Env(), nullptr, 0);
}
}
Napi::Buffer<unsigned char> Canvas::getRawDataBuffer(const Napi::CallbackInfo &info, unsigned long &size)
{
unsigned char *data = new unsigned char[4 * mWidth * mHeight];
int ret = this->mRenderContext->readPixelAndSampleFromCurrentCtx(data);
if (ret == 0)
{
return Napi::Buffer<unsigned char>::Copy(info.Env(), data, 4 * mWidth * mHeight);
}
else
{
size = -1;
return Napi::Buffer<unsigned char>::Copy(info.Env(), nullptr, 0);
}
}
Napi::Value Canvas::ToBuffer(const Napi::CallbackInfo &info)
{
unsigned long size = 0;
//默认输出png 编码
if (info.Length() == 0)
{
return this->getPNGBuffer(info, size);
}
else
{
Napi::Buffer<unsigned char> ret;
if (info.Length() == 1)
{
std::string mimeType = info[0].As<Napi::String>().Utf8Value();
if (mimeType == "image/png")
{
ret = this->getPNGBuffer(info, size);
}
else if (mimeType == "image/jpeg")
{
ret = this->getJPGBuffer(info, size);
}
else if (mimeType == "raw")
{
ret = this->getRawDataBuffer(info, size);
}
}
if (size < 0)
{
return info.Env().Null();
}
else
{
return ret;
}
}
}
Canvas::~Canvas()
{
Expand Down
66 changes: 39 additions & 27 deletions node/binding/Canvas.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* Created by G-Canvas Open Source Team.
* Copyright (c) 2017, Alibaba, Inc. All rights reserved.
*
* This source code is licensed under the Apache Licence 2.0.
* For the full copyright and license information, please view
* the LICENSE file in the root directory of this source tree.
*/
#ifndef CANVAS_H
#define CANVAS_H
#include "GRenderContext.h"
Expand All @@ -9,34 +17,38 @@
#include <memory>
namespace NodeBinding
{
class Canvas : public Napi::ObjectWrap<Canvas>
{
public:
static void Init(Napi::Env env,Napi::Object exports);
Canvas(const Napi::CallbackInfo &info);
virtual ~Canvas();
static Napi::Object NewInstance(Napi::Env env, Napi::Value arg, Napi::Value arg2);
Napi::ObjectReference mRef;
int getWidth();
int getHeight();
std::shared_ptr<GRenderContext> mRenderContext;

private:
static Napi::FunctionReference constructor;
Napi::Value getWidth(const Napi::CallbackInfo &info);
Napi::Value getHeight(const Napi::CallbackInfo &info);
Napi::Value getContext(const Napi::CallbackInfo &info);
Napi::Value createPNGStreamSync(const Napi::CallbackInfo &info);
Napi::Value createJPGStreamSync(const Napi::CallbackInfo &info);
Napi::ObjectReference context2dRef;
class Canvas : public Napi::ObjectWrap<Canvas>
{
public:
static void Init(Napi::Env env, Napi::Object exports);
Canvas(const Napi::CallbackInfo &info);
virtual ~Canvas();
static Napi::Object NewInstance(Napi::Env env, Napi::Value arg, Napi::Value arg2);
Napi::ObjectReference mRef;
int getWidth();
int getHeight();
std::shared_ptr<GRenderContext> mRenderContext;

private:
static Napi::FunctionReference constructor;
Napi::Value getWidth(const Napi::CallbackInfo &info);
Napi::Value getHeight(const Napi::CallbackInfo &info);
Napi::Value getContext(const Napi::CallbackInfo &info);
Napi::Value createPNGStreamSync(const Napi::CallbackInfo &info);
Napi::Value createJPGStreamSync(const Napi::CallbackInfo &info);
Napi::Value ToBuffer(const Napi::CallbackInfo &info);
Napi::Buffer<unsigned char> getPNGBuffer(const Napi::CallbackInfo &info, unsigned long &size);
Napi::Buffer<unsigned char> getJPGBuffer(const Napi::CallbackInfo &info, unsigned long &size);
Napi::Buffer<unsigned char> getRawDataBuffer(const Napi::CallbackInfo &info, unsigned long &size);
Napi::ObjectReference context2dRef;

void setWidth(const Napi::CallbackInfo &info, const Napi::Value &value);
void setHeight(const Napi::CallbackInfo &info, const Napi::Value &value);
void createPNG(const Napi::CallbackInfo &info);
void createJPEG(const Napi::CallbackInfo &info);
void setWidth(const Napi::CallbackInfo &info, const Napi::Value &value);
void setHeight(const Napi::CallbackInfo &info, const Napi::Value &value);
void createPNG(const Napi::CallbackInfo &info);
void createJPEG(const Napi::CallbackInfo &info);

int mWidth = 0;
int mHeight = 0;
};
int mWidth = 0;
int mHeight = 0;
};
} // namespace NodeBinding
#endif
8 changes: 8 additions & 0 deletions node/binding/CanvasGradient.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* Created by G-Canvas Open Source Team.
* Copyright (c) 2017, Alibaba, Inc. All rights reserved.
*
* This source code is licensed under the Apache Licence 2.0.
* For the full copyright and license information, please view
* the LICENSE file in the root directory of this source tree.
*/
#include "CanvasGradient.h"
#include "CanvasRenderingContext2D.h"
#include "NodeBindingUtil.h"
Expand Down
8 changes: 8 additions & 0 deletions node/binding/CanvasGradient.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* Created by G-Canvas Open Source Team.
* Copyright (c) 2017, Alibaba, Inc. All rights reserved.
*
* This source code is licensed under the Apache Licence 2.0.
* For the full copyright and license information, please view
* the LICENSE file in the root directory of this source tree.
*/
#ifndef GRADIENT_H
#define GRADIENT_H
#include <napi.h>
Expand Down
8 changes: 8 additions & 0 deletions node/binding/CanvasPattern.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* Created by G-Canvas Open Source Team.
* Copyright (c) 2017, Alibaba, Inc. All rights reserved.
*
* This source code is licensed under the Apache Licence 2.0.
* For the full copyright and license information, please view
* the LICENSE file in the root directory of this source tree.
*/
#include "CanvasPattern.h"
#include "NodeBindingUtil.h"
namespace NodeBinding
Expand Down
8 changes: 8 additions & 0 deletions node/binding/CanvasPattern.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* Created by G-Canvas Open Source Team.
* Copyright (c) 2017, Alibaba, Inc. All rights reserved.
*
* This source code is licensed under the Apache Licence 2.0.
* For the full copyright and license information, please view
* the LICENSE file in the root directory of this source tree.
*/
#ifndef PATTERN_H
#define PATTERN_H
#include <napi.h>
Expand Down
Loading