Skip to content

Commit

Permalink
replace some remaining glib calls
Browse files Browse the repository at this point in the history
  • Loading branch information
chearon authored and zbjornson committed Feb 26, 2022
1 parent 6fd0fa5 commit 009d594
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 41 deletions.
10 changes: 5 additions & 5 deletions src/Canvas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -706,9 +706,9 @@ NAN_METHOD(Canvas::StreamJPEGSync) {
char *
str_value(Local<Value> val, const char *fallback, bool can_be_number) {
if (val->IsString() || (can_be_number && val->IsNumber())) {
return g_strdup(*Nan::Utf8String(val));
return strdup(*Nan::Utf8String(val));
} else if (fallback) {
return g_strdup(fallback);
return strdup(fallback);
} else {
return NULL;
}
Expand Down Expand Up @@ -765,9 +765,9 @@ NAN_METHOD(Canvas::RegisterFont) {
Nan::ThrowError(GENERIC_FACE_ERROR);
}

g_free(family);
g_free(weight);
g_free(style);
free(family);
free(weight);
free(style);
}

NAN_METHOD(Canvas::DeregisterAllFonts) {
Expand Down
48 changes: 12 additions & 36 deletions src/register_font.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,35 +94,12 @@ to_utf8(FT_Byte* buf, FT_UInt len, FT_UShort pid, FT_UShort eid) {
* system, fall back to the other
*/

typedef struct _NameDef {
const char *buf;
int rank; // the higher the more desirable
} NameDef;

gint
_name_def_compare(gconstpointer a, gconstpointer b) {
return ((NameDef*)a)->rank > ((NameDef*)b)->rank ? -1 : 1;
}

// Some versions of GTK+ do not have this, particualrly the one we
// currently link to in node-canvas's wiki
void
_free_g_list_item(gpointer data, gpointer user_data) {
NameDef *d = (NameDef *)data;
free((void *)(d->buf));
}

void
_g_list_free_full(GList *list) {
g_list_foreach(list, _free_g_list_item, NULL);
g_list_free(list);
}

char *
get_family_name(FT_Face face) {
FT_SfntName name;
GList *list = NULL;
char *utf8name = NULL;

int best_rank = -1;
char* best_buf = NULL;

for (unsigned i = 0; i < FT_Get_Sfnt_Name_Count(face); ++i) {
FT_Get_Sfnt_Name(face, i, &name);
Expand All @@ -131,20 +108,19 @@ get_family_name(FT_Face face) {
char *buf = to_utf8(name.string, name.string_len, name.platform_id, name.encoding_id);

if (buf) {
NameDef *d = (NameDef*)malloc(sizeof(NameDef));
d->buf = (const char*)buf;
d->rank = GET_NAME_RANK(name);

list = g_list_insert_sorted(list, (gpointer)d, _name_def_compare);
int rank = GET_NAME_RANK(name);
if (rank > best_rank) {
best_rank = rank;
if (best_buf) free(best_buf);
best_buf = buf;
} else {
free(buf);
}
}
}
}

GList *best_def = g_list_first(list);
if (best_def) utf8name = (char*) strdup(((NameDef*)best_def->data)->buf);
if (list) _g_list_free_full(list);

return utf8name;
return best_buf;
}

PangoWeight
Expand Down

0 comments on commit 009d594

Please sign in to comment.