Skip to content

Commit

Permalink
switch freetype engine to be factory of wrapped objects, ref #17
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemorris committed Mar 17, 2014
1 parent 7c4ab88 commit 3188f3e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 39 deletions.
33 changes: 4 additions & 29 deletions src/freetype_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,36 +64,11 @@ FreetypeEngine::~FreetypeEngine() {
}

v8::Handle<v8::Value> FreetypeEngine::New(const v8::Arguments& args) {
if (!args.IsConstructCall()) {
return ThrowException(v8::Exception::TypeError(v8::String::New("Constructor must be called with new keyword")));
}
if (args.Length() != 1) {
return ThrowException(v8::Exception::TypeError(v8::String::New("Constructor must be called with new keyword")));
}

FT_Face ft_face = nullptr;
if (args[0]->IsExternal()) {
ft_face = (FT_Face)v8::External::Cast(*args[0])->Value();
} else {
v8::String::Utf8Value font_name(args[0]->ToString());
/*
PangoFontDescription *desc = pango_font_description_from_string(*font_name);
pango_font = pango_font_map_load_font(pango_fontmap(), pango_context(), desc);
*/
}
}

if (ft_face) {
FT_Font* font = FT_Font::New(ft_face);
// v8::Local<v8::Object> font = v8::Object::New();
font->Wrap(args.This());
args.This()->Set(v8::String::NewSymbol("family"), v8::String::New(ft_face->family_name), v8::ReadOnly);
args.This()->Set(v8::String::NewSymbol("style"), v8::String::New(ft_face->style_name), v8::ReadOnly);
args.This()->Set(v8::String::NewSymbol("length"), v8::Number::New(ft_face->num_glyphs), v8::ReadOnly);

return args.This();
} else {
return ThrowException(v8::Exception::Error(v8::String::New("No matching font found")));
}
v8::Handle<v8::Value> FreetypeEngine::CreateFont(const v8::Arguments& args) {
v8::HandleScope scope;
return scope.Close(FT_Font::NewInstance(args));
}

v8::Handle<v8::Value> FreetypeEngine::New(std::string const& family_name) {
Expand Down
1 change: 1 addition & 0 deletions src/freetype_engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class FreetypeEngine : public node::ObjectWrap {
~FreetypeEngine();

static v8::Handle<v8::Value> New(const v8::Arguments& args);
static v8::Handle<v8::Value> CreateFont(const v8::Arguments& args);
static v8::Handle<v8::Value> GetGlyph(uint32_t, const v8::AccessorInfo& info);
static v8::Handle<v8::Value> Metrics(v8::Local<v8::String> property, const v8::AccessorInfo &info);

Expand Down
57 changes: 51 additions & 6 deletions src/freetype_font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@

v8::Persistent<v8::Function> FT_Font::constructor;

FT_Font::FT_Font(FT_Face face) : node::ObjectWrap() {
FT_Font::FT_Font(FT_Face face) {
}

FT_Font::~FT_Font() {
FT_Done_Face(ft_face);
}

void FT_Font::Init() {
// Prepare constructor template
v8::Local<v8::FunctionTemplate> tpl = v8::FunctionTemplate::New(New);
v8::Local<v8::String> name = v8::String::NewSymbol("FT_Font");
Local<FunctionTemplate> tpl = FunctionTemplate::New(New);
tpl->SetClassName(String::NewSymbol("FT_Font"));
tpl->InstanceTemplate()->SetInternalFieldCount(1);

// Prototype
tpl->PrototypeTemplate()->Set(String::NewSymbol("HasInstance"),
FunctionTemplate::New(HasInstance)->GetFunction());
constructor = v8::Persistent<v8::Function>::New(tpl->GetFunction());

constructor->SetClassName(v8::String::NewSymbol("FT_Font"));
constructor->InstanceTemplate()->SetInternalFieldCount(1);
}

v8::Handle<v8::Value> FT_Font::New(FT_Face ft_face) {
Expand All @@ -29,7 +32,49 @@ v8::Handle<v8::Value> FT_Font::New(FT_Face ft_face) {
return scope.Close(object);
}

bool FreetypeEngine::HasInstance(v8::Handle<v8::Value> val) {
v8::Handle<v8::Value> FT_Font::New(const v8::Arguments& args) {
if (!args.IsConstructCall()) {
return ThrowException(v8::Exception::TypeError(v8::String::New("Constructor must be called with new keyword")));
}
if (args.Length() != 1) {
return ThrowException(v8::Exception::TypeError(v8::String::New("Constructor must be called with new keyword")));
}

FT_Face ft_face = nullptr;
if (args[0]->IsExternal()) {
ft_face = (FT_Face)v8::External::Cast(*args[0])->Value();
} else {
v8::String::Utf8Value font_name(args[0]->ToString());
/*
PangoFontDescription *desc = pango_font_description_from_string(*font_name);
pango_font = pango_font_map_load_font(pango_fontmap(), pango_context(), desc);
*/
}

if (ft_face) {
FT_Font* font = new FT_Font(ft_face);
font->Wrap(args.This());
args.This()->Set(v8::String::NewSymbol("family"), v8::String::New(ft_face->family_name), v8::ReadOnly);
args.This()->Set(v8::String::NewSymbol("style"), v8::String::New(ft_face->style_name), v8::ReadOnly);
args.This()->Set(v8::String::NewSymbol("length"), v8::Number::New(ft_face->num_glyphs), v8::ReadOnly);

return args.This();
} else {
return ThrowException(v8::Exception::Error(v8::String::New("No matching font found")));
}
}

v8::Handle<v8::Value> FT_Font::NewInstance(const v8::Arguments& args) {
v8::HandleScope scope;

const unsigned argc = 1;
v8::Handle<v8::Value> argv[argc] = { args[0] };
v8::Local<v8::Object> instance = constructor->NewInstance(argc, argv);

return scope.Close(instance);
}

bool FT_Font::HasInstance(v8::Handle<v8::Value> val) {
if (!val->IsObject()) return false;
return constructor->HasInstance(val->ToObject());
}
11 changes: 7 additions & 4 deletions src/freetype_font.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ extern "C"

class FT_Font : public node::ObjectWrap {
public:
static v8::Persistent<v8::FunctionTemplate> constructor;
static void Init();
static v8::Handle<v8::Value> NewInstance(const v8::Arguments& args);
static bool HasInstance(v8::Handle<v8::Value> val);

static FT_Font* New(FT_Face ft_face);
protected:
FT_Font(FT_Face ft_face);
private:
explicit FT_Font(FT_Face ft_face);
~FT_Font();

static v8::Handle<v8::Value> New(const v8::Arguments& args);
static v8::Persistent<v8::FunctionTemplate> constructor;
};

0 comments on commit 3188f3e

Please sign in to comment.