-
Notifications
You must be signed in to change notification settings - Fork 5
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
add match preview tab 2 #9
Changes from 13 commits
f76f0c0
9fceabd
563f551
a84ef0a
cac6609
ee8a47e
8d96002
fb52efc
dcdb079
a59b787
a4ce5d8
5b84e3a
a321b2b
1d940bd
e310d4f
9af31c1
189c315
dc4e8d1
044c75d
4793848
b428409
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
require 'mkmf' | ||
|
||
$LDFLAGS << ' -lfont-specimen -lfontconfig -lfreetype -lpng16 -lharfbuzz' | ||
|
||
extension_name = 'font_specimen' | ||
|
||
dir_config(extension_name) | ||
create_makefile(extension_name) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#include <ruby.h> | ||
#include <ruby/io.h> | ||
#include <font-specimen.h> | ||
|
||
#define MAX_SCRIPTS 20 | ||
|
||
VALUE FontSpecimen = Qnil; | ||
|
||
void Init_font_specimen(); | ||
VALUE method_font_scripts(VALUE self, VALUE str_pattern); | ||
VALUE method_specimen_write(VALUE self, | ||
VALUE str_pattern, | ||
VALUE str_script, | ||
VALUE int_png_fd, | ||
VALUE int_width, | ||
VALUE int_height); | ||
|
||
void Init_font_specimen() { | ||
FontSpecimen = rb_define_module("FontSpecimen"); | ||
rb_define_method(FontSpecimen, "font_scripts", | ||
method_font_scripts, 1); | ||
rb_define_method(FontSpecimen, "specimen_write", | ||
method_specimen_write, 5); | ||
} | ||
|
||
VALUE method_font_scripts(VALUE self, VALUE str_pattern) { | ||
VALUE res_hash = rb_hash_new(); | ||
|
||
char *pattern; | ||
const char *scripts[MAX_SCRIPTS]; | ||
double coverages[MAX_SCRIPTS]; | ||
char str_coverage[6]; | ||
|
||
int s, nscripts; | ||
|
||
pattern = StringValueCStr(str_pattern); | ||
|
||
if ((nscripts = specimen_font_scripts(pattern, SCRIPT_SORT_PERCENT, | ||
scripts, coverages, MAX_SCRIPTS)) <= 0) | ||
return res_hash; | ||
|
||
for (s = 0; s < nscripts; s++) | ||
{ | ||
snprintf(str_coverage, 6, "%.1f", coverages[s]); | ||
rb_hash_aset(res_hash, | ||
rb_str_new2(scripts[s]), rb_str_new2(str_coverage)); | ||
} | ||
|
||
return res_hash; | ||
} | ||
|
||
VALUE method_specimen_write(VALUE self, | ||
VALUE str_pattern, | ||
VALUE str_script, | ||
VALUE png_file, | ||
VALUE int_width, | ||
VALUE int_height) | ||
{ | ||
char *pattern = StringValueCStr(str_pattern); | ||
char *script = StringValueCStr(str_script); | ||
rb_io_t *fptr = RFILE(png_file)->fptr; | ||
int png_fd = fptr->fd; | ||
int width = NUM2INT(int_width); | ||
int height = NUM2INT(int_height); | ||
|
||
FILE *png = NULL; | ||
|
||
png = rb_fdopen(png_fd, "w"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. again premature initialization is not needed. |
||
if (!png) | ||
return Qfalse; | ||
|
||
if (specimen_write(SPECIMEN_COMPACT, pattern, script, png, width, height)) | ||
return Qfalse; | ||
|
||
fflush(png); | ||
|
||
return Qtrue; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,13 +17,16 @@ VALUE FontconfigSetting = Qnil; | |
void Init_fontconfig_setting(); | ||
VALUE method_fc_installed_families(VALUE self, VALUE array_elements); | ||
VALUE method_fc_is_family_installed(VALUE self, VALUE str_family); | ||
VALUE method_fc_match_family(VALUE self, VALUE str_family); | ||
|
||
void Init_fontconfig_setting() { | ||
FontconfigSetting = rb_define_module("FontconfigSetting"); | ||
rb_define_method(FontconfigSetting, "installed_families", | ||
method_fc_installed_families, 1); | ||
rb_define_method(FontconfigSetting, "family_installed?", | ||
method_fc_is_family_installed, 1); | ||
rb_define_method(FontconfigSetting, "match_family", | ||
method_fc_match_family, 1); | ||
} | ||
|
||
VALUE method_fc_installed_families(VALUE self, VALUE array_elements) { | ||
|
@@ -62,25 +65,84 @@ VALUE method_fc_installed_families(VALUE self, VALUE array_elements) { | |
return str_family_list; | ||
} | ||
|
||
VALUE method_fc_is_family_installed(VALUE self, VALUE str_family) { | ||
FcPattern *find(char *str_pattern) | ||
{ | ||
FcObjectSet *objectset; | ||
FcPattern *pattern; | ||
FcPattern *pattern, *result; | ||
FcFontSet *fontset; | ||
|
||
char *family; | ||
|
||
family = StringValueCStr(str_family); | ||
|
||
FcInit(); | ||
objectset = FcObjectSetBuild(FC_FAMILY, NULL); | ||
pattern = FcNameParse((FcChar8 *)family); | ||
pattern = FcNameParse((FcChar8 *)str_pattern); | ||
fontset = FcFontList (NULL, pattern, objectset); | ||
FcPatternDestroy (pattern); | ||
FcObjectSetDestroy (objectset); | ||
|
||
if (fontset->nfont > 0) | ||
return Qtrue; | ||
result = FcPatternDuplicate(fontset->fonts[0]); | ||
else | ||
return Qfalse; | ||
result = NULL; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it can be written like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. my version is more readable because of second argument of your ternary operator |
||
|
||
FcFontSetDestroy(fontset); | ||
return result; | ||
} | ||
|
||
FcPattern *match(char *str_pattern) | ||
{ | ||
FcPattern *pattern, *font; | ||
FcResult r; | ||
|
||
FcInit(); | ||
pattern = FcNameParse((FcChar8 *)str_pattern); | ||
FcConfigSubstitute(NULL, pattern, FcMatchPattern); | ||
FcDefaultSubstitute(pattern); | ||
font = FcFontMatch(0, pattern, &r); | ||
FcPatternDestroy (pattern); | ||
|
||
if (r == FcResultMatch) | ||
return font; | ||
else | ||
return NULL; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thats ugly |
||
} | ||
|
||
VALUE method_fc_is_family_installed(VALUE self, VALUE str_family) | ||
{ | ||
FcPattern *font; | ||
char *family; | ||
VALUE res; | ||
|
||
family = StringValueCStr(str_family); | ||
font = find(family); | ||
|
||
if (font) | ||
res = Qtrue; | ||
else | ||
res = Qfalse; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That heavily depends on taste, but ok, it is more compact. |
||
|
||
FcPatternDestroy(font); | ||
return res; | ||
} | ||
|
||
VALUE method_fc_match_family(VALUE self, VALUE str_family) | ||
{ | ||
FcPattern *font; | ||
char *family; | ||
VALUE res; | ||
|
||
family = StringValueCStr(str_family); | ||
font = match(family); | ||
|
||
if (font) | ||
{ | ||
FcPatternGetString(font, FC_FAMILY, 0, (FcChar8**)&family); | ||
res = rb_str_new2(family); | ||
} | ||
else | ||
{ | ||
res = Qnil; | ||
} | ||
|
||
FcPatternDestroy(font); | ||
return res; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not simple
char *pattern = StringValueCStr(str_pattern);
premature initialization is not needed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean char * one. For res_hash it make sense to init it to empty.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok.