Skip to content

Commit f76f0c0

Browse files
committed
add match preview tab
1 parent 906d886 commit f76f0c0

File tree

6 files changed

+413
-41
lines changed

6 files changed

+413
-41
lines changed

Rakefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ end
88

99
task :compile do
1010
olddir = Dir.pwd
11-
["ft2_rendering", "fontconfig_setting"].each do |ext|
11+
["ft2_rendering", "fontconfig_setting", "font_specimen"].each do |ext|
1212
Dir.chdir("src/ext/#{ext}")
1313
ruby 'extconf.rb'
1414
sh 'make'
@@ -20,7 +20,7 @@ namespace :test do
2020
task :prepare => :compile do
2121
# let yast know where ft2_rendering extension is
2222
mkdir_p "src/lib/yast"
23-
["ft2_rendering", "fontconfig_setting"].each do |ext|
23+
["ft2_rendering", "fontconfig_setting", "font_specimen"].each do |ext|
2424
ln_sf("../../ext/#{ext}/#{ext}.so", "src/lib/yast/#{ext}.so")
2525
end
2626
end

src/ext/font_specimen/extconf.rb

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require 'mkmf'
2+
3+
$LDFLAGS << ' -lfont-specimen -lfontconfig -lfreetype -lpng16 -lharfbuzz'
4+
5+
extension_name = 'font_specimen'
6+
7+
dir_config(extension_name)
8+
create_makefile(extension_name)
9+

src/ext/font_specimen/font-specimen.c

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <ruby.h>
2+
#include <ruby/io.h>
3+
#include <font-specimen.h>
4+
5+
#define MAX_SCRIPTS 20
6+
7+
VALUE FontSpecimen = Qnil;
8+
9+
void Init_font_specimen();
10+
VALUE method_font_scripts(VALUE self, VALUE str_pattern);
11+
VALUE method_specimen_write(VALUE self,
12+
VALUE str_pattern,
13+
VALUE str_script,
14+
VALUE int_png_fd,
15+
VALUE int_width,
16+
VALUE int_height);
17+
18+
void Init_font_specimen() {
19+
FontSpecimen = rb_define_module("FontSpecimen");
20+
rb_define_method(FontSpecimen, "font_scripts",
21+
method_font_scripts, 1);
22+
rb_define_method(FontSpecimen, "specimen_write",
23+
method_specimen_write, 5);
24+
}
25+
26+
VALUE method_font_scripts(VALUE self, VALUE str_pattern) {
27+
VALUE res_hash = rb_hash_new();
28+
29+
char *pattern;
30+
const char *scripts[MAX_SCRIPTS];
31+
double coverages[MAX_SCRIPTS];
32+
char str_coverage[6];
33+
34+
int s, nscripts;
35+
36+
pattern = StringValueCStr(str_pattern);
37+
38+
if ((nscripts = specimen_font_scripts(pattern, SCRIPT_SORT_PERCENT,
39+
scripts, coverages, MAX_SCRIPTS)) <= 0)
40+
return res_hash;
41+
42+
for (s = 0; s < nscripts; s++)
43+
{
44+
snprintf(str_coverage, 6, "%.1f", coverages[s]);
45+
rb_hash_aset(res_hash,
46+
rb_str_new2(scripts[s]), rb_str_new2(str_coverage));
47+
}
48+
49+
return res_hash;
50+
}
51+
52+
VALUE method_specimen_write(VALUE self,
53+
VALUE str_pattern,
54+
VALUE str_script,
55+
VALUE png_file,
56+
VALUE int_width,
57+
VALUE int_height)
58+
{
59+
char *pattern = StringValueCStr(str_pattern);
60+
char *script = StringValueCStr(str_script);
61+
rb_io_t *fptr = RFILE(png_file)->fptr;
62+
int png_fd = fptr->fd;
63+
int width = NUM2INT(int_width);
64+
int height = NUM2INT(int_height);
65+
66+
FILE *png = NULL;
67+
68+
png = rb_fdopen(png_fd, "w");
69+
if (!png)
70+
return Qfalse;
71+
72+
if (specimen_write(SPECIMEN_COMPACT, pattern, script, png, width, height))
73+
return Qfalse;
74+
75+
fflush(png);
76+
77+
return Qtrue;
78+
}
79+

src/ext/fontconfig_setting/fontconfig-setting.c

+71-9
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@ VALUE FontconfigSetting = Qnil;
1717
void Init_fontconfig_setting();
1818
VALUE method_fc_installed_families(VALUE self, VALUE array_elements);
1919
VALUE method_fc_is_family_installed(VALUE self, VALUE str_family);
20+
VALUE method_fc_match_family(VALUE self, VALUE str_family);
2021

2122
void Init_fontconfig_setting() {
2223
FontconfigSetting = rb_define_module("FontconfigSetting");
2324
rb_define_method(FontconfigSetting, "installed_families",
2425
method_fc_installed_families, 1);
2526
rb_define_method(FontconfigSetting, "family_installed?",
2627
method_fc_is_family_installed, 1);
28+
rb_define_method(FontconfigSetting, "match_family",
29+
method_fc_match_family, 1);
2730
}
2831

2932
VALUE method_fc_installed_families(VALUE self, VALUE array_elements) {
@@ -62,25 +65,84 @@ VALUE method_fc_installed_families(VALUE self, VALUE array_elements) {
6265
return str_family_list;
6366
}
6467

65-
VALUE method_fc_is_family_installed(VALUE self, VALUE str_family) {
68+
FcPattern *find(char *str_pattern)
69+
{
6670
FcObjectSet *objectset;
67-
FcPattern *pattern;
71+
FcPattern *pattern, *result;
6872
FcFontSet *fontset;
6973

70-
char *family;
71-
72-
family = StringValueCStr(str_family);
73-
7474
FcInit();
7575
objectset = FcObjectSetBuild(FC_FAMILY, NULL);
76-
pattern = FcNameParse((FcChar8 *)family);
76+
pattern = FcNameParse((FcChar8 *)str_pattern);
7777
fontset = FcFontList (NULL, pattern, objectset);
7878
FcPatternDestroy (pattern);
7979
FcObjectSetDestroy (objectset);
8080

8181
if (fontset->nfont > 0)
82-
return Qtrue;
82+
result = FcPatternDuplicate(fontset->fonts[0]);
8383
else
84-
return Qfalse;
84+
result = NULL;
85+
86+
FcFontSetDestroy(fontset);
87+
return result;
88+
}
89+
90+
FcPattern *match(char *str_pattern)
91+
{
92+
FcPattern *pattern, *font;
93+
FcResult r;
94+
95+
FcInit();
96+
pattern = FcNameParse((FcChar8 *)str_pattern);
97+
FcConfigSubstitute(NULL, pattern, FcMatchPattern);
98+
FcDefaultSubstitute(pattern);
99+
font = FcFontMatch(0, pattern, &r);
100+
FcPatternDestroy (pattern);
101+
102+
if (r == FcResultMatch)
103+
return font;
104+
else
105+
return NULL;
106+
}
107+
108+
VALUE method_fc_is_family_installed(VALUE self, VALUE str_family)
109+
{
110+
FcPattern *font;
111+
char *family;
112+
VALUE res;
113+
114+
family = StringValueCStr(str_family);
115+
font = find(family);
116+
117+
if (font)
118+
res = Qtrue;
119+
else
120+
res = Qfalse;
121+
122+
FcPatternDestroy(font);
123+
return res;
124+
}
125+
126+
VALUE method_fc_match_family(VALUE self, VALUE str_family)
127+
{
128+
FcPattern *font;
129+
char *family;
130+
VALUE res;
131+
132+
family = StringValueCStr(str_family);
133+
font = match(family);
134+
135+
if (font)
136+
{
137+
FcPatternGetString(font, FC_FAMILY, 0, (FcChar8**)&family);
138+
res = rb_str_new2(family);
139+
}
140+
else
141+
{
142+
res = Qnil;
143+
}
144+
145+
FcPatternDestroy(font);
146+
return res;
85147
}
86148

0 commit comments

Comments
 (0)