Skip to content

Commit

Permalink
First iteration that makes font load up
Browse files Browse the repository at this point in the history
  • Loading branch information
jviereck committed Jun 24, 2012
1 parent 3ca90a7 commit 03ec023
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 1 deletion.
82 changes: 81 additions & 1 deletion src/CanvasRenderingContext2d.cc
Expand Up @@ -16,6 +16,10 @@
#include "CanvasGradient.h" #include "CanvasGradient.h"
#include "CanvasPattern.h" #include "CanvasPattern.h"


#include <ft2build.h>
#include <cairo-ft.h>
#include FT_FREETYPE_H

Persistent<FunctionTemplate> Context2d::constructor; Persistent<FunctionTemplate> Context2d::constructor;


/* /*
Expand Down Expand Up @@ -45,6 +49,7 @@ enum {
, TEXT_BASELINE_HANGING , TEXT_BASELINE_HANGING
}; };



/* /*
* Initialize Context2d. * Initialize Context2d.
*/ */
Expand Down Expand Up @@ -90,6 +95,7 @@ Context2d::Initialize(Handle<Object> target) {
NODE_SET_PROTOTYPE_METHOD(constructor, "arc", Arc); NODE_SET_PROTOTYPE_METHOD(constructor, "arc", Arc);
NODE_SET_PROTOTYPE_METHOD(constructor, "arcTo", ArcTo); NODE_SET_PROTOTYPE_METHOD(constructor, "arcTo", ArcTo);
NODE_SET_PROTOTYPE_METHOD(constructor, "_setFont", SetFont); NODE_SET_PROTOTYPE_METHOD(constructor, "_setFont", SetFont);
NODE_SET_PROTOTYPE_METHOD(constructor, "loadFontFace", LoadFontFace);
NODE_SET_PROTOTYPE_METHOD(constructor, "_setFillColor", SetFillColor); NODE_SET_PROTOTYPE_METHOD(constructor, "_setFillColor", SetFillColor);
NODE_SET_PROTOTYPE_METHOD(constructor, "_setStrokeColor", SetStrokeColor); NODE_SET_PROTOTYPE_METHOD(constructor, "_setStrokeColor", SetStrokeColor);
NODE_SET_PROTOTYPE_METHOD(constructor, "_setFillPattern", SetFillPattern); NODE_SET_PROTOTYPE_METHOD(constructor, "_setFillPattern", SetFillPattern);
Expand Down Expand Up @@ -1611,6 +1617,73 @@ Context2d::MoveTo(const Arguments &args) {
return Undefined(); return Undefined();
} }


/*
* Load font:
* - filepath
*/

Handle<Value>
Context2d::loadFontFace(const char *name, const char *path)
{
FT_Library library; /* handle to library */
FT_Face ft_face; /* handle to face object */
FT_Error ft_error;

ft_error = FT_Init_FreeType( &library );
if (ft_error) {
return ThrowException(Exception::Error(String::New("Could not load library")));
}

ft_error = FT_New_Face( library, path, 0, &ft_face );
if (ft_error == FT_Err_Unknown_File_Format) {
return ThrowException(Exception::Error(String::New("Could not load font file")));
}

cairo_font_face_t *cr_face;
cr_face = cairo_ft_font_face_create_for_ft_face(ft_face, 0);

_loaded_fonts.insert(
pair<string, font_data*>(
std::string(name), new font_data(ft_face, cr_face)
)
);

printf("=== Context2d::loadFont - done\n");

return Undefined();
}

Handle<Value>
Context2d::LoadFontFace(const Arguments &args) {
HandleScope scope;

// Ignore invalid args
if (!args[0]->IsString()
|| !args[1]->IsString()) return Undefined();

printf("=== Context2d::LoadFontFace\n");

String::AsciiValue fontName(args[0]);
String::AsciiValue filePath(args[1]);

Context2d *context = ObjectWrap::Unwrap<Context2d>(args.This());
context->loadFontFace(*fontName, *filePath);

return Undefined();
}

cairo_font_face_t*
Context2d::lookupLoadedFontFace(const char *name)
{
map_font_entry::iterator iter;
iter = _loaded_fonts.find(string(name));
if (iter != _loaded_fonts.end()) {
return iter->second->cr_face;
} else {
return NULL;
}
}

/* /*
* Set font: * Set font:
* - weight * - weight
Expand Down Expand Up @@ -1657,7 +1730,14 @@ Context2d::SetFont(const Arguments &args) {
w = CAIRO_FONT_WEIGHT_BOLD; w = CAIRO_FONT_WEIGHT_BOLD;
} }


cairo_select_font_face(ctx, *family, s, w); cairo_font_face_t *cr_face = context->lookupLoadedFontFace(*family);
if (cr_face) {
printf("=== Context2d::SetFont: Pickup font\n");
cairo_set_font_face(ctx, cr_face);
} else {
printf("=== Context2d::SetFont: Pickup font NOPE\n");
cairo_select_font_face(ctx, *family, s, w);
}


return Undefined(); return Undefined();
} }
Expand Down
29 changes: 29 additions & 0 deletions src/CanvasRenderingContext2d.h
Expand Up @@ -12,11 +12,31 @@
#include "Canvas.h" #include "Canvas.h"
#include "CanvasGradient.h" #include "CanvasGradient.h"


#include <ft2build.h>
#include <cairo-ft.h>
#include FT_FREETYPE_H

#include <string>
#include <map>
using namespace std;

typedef enum { typedef enum {
TEXT_DRAW_PATHS, TEXT_DRAW_PATHS,
TEXT_DRAW_GLYPHS TEXT_DRAW_GLYPHS
} canvas_draw_mode_t; } canvas_draw_mode_t;


typedef struct font_data {
FT_Face ft_face;
cairo_font_face_t *cr_face;

font_data(FT_Face ft_face, cairo_font_face_t *cr_face):
ft_face(ft_face), cr_face(cr_face)
{
}
};

typedef map<string, font_data*> map_font_entry;

/* /*
* State struct. * State struct.
* *
Expand Down Expand Up @@ -70,6 +90,7 @@ class Context2d: public node::ObjectWrap {
static Handle<Value> FillText(const Arguments &args); static Handle<Value> FillText(const Arguments &args);
static Handle<Value> StrokeText(const Arguments &args); static Handle<Value> StrokeText(const Arguments &args);
static Handle<Value> SetFont(const Arguments &args); static Handle<Value> SetFont(const Arguments &args);
static Handle<Value> LoadFontFace(const Arguments &args);
static Handle<Value> SetFillColor(const Arguments &args); static Handle<Value> SetFillColor(const Arguments &args);
static Handle<Value> SetStrokeColor(const Arguments &args); static Handle<Value> SetStrokeColor(const Arguments &args);
static Handle<Value> SetFillPattern(const Arguments &args); static Handle<Value> SetFillPattern(const Arguments &args);
Expand Down Expand Up @@ -134,11 +155,19 @@ class Context2d: public node::ObjectWrap {
void save(); void save();
void restore(); void restore();


Handle<Value> loadFontFace(const char *name, const char *path);
cairo_font_face_t* lookupLoadedFontFace(const char *name);

private: private:
~Context2d(); ~Context2d();
Canvas *_canvas; Canvas *_canvas;
cairo_t *_context; cairo_t *_context;
cairo_path_t *_path; cairo_path_t *_path;

map_font_entry _loaded_fonts;
// FT_Library ft_library; /* handle to library */
// FT_Face ft_face; /* handle to face object */
// FT_Error ft_error;
}; };


#endif #endif

0 comments on commit 03ec023

Please sign in to comment.