Skip to content
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

Merged
merged 21 commits into from
Nov 26, 2014
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ end

task :compile do
olddir = Dir.pwd
["ft2_rendering", "fontconfig_setting"].each do |ext|
["ft2_rendering", "fontconfig_setting", "font_specimen"].each do |ext|
Dir.chdir("src/ext/#{ext}")
ruby 'extconf.rb'
sh 'make'
Expand All @@ -20,7 +20,7 @@ namespace :test do
task :prepare => :compile do
# let yast know where ft2_rendering extension is
mkdir_p "src/lib/yast"
["ft2_rendering", "fontconfig_setting"].each do |ext|
["ft2_rendering", "fontconfig_setting", "font_specimen"].each do |ext|
ln_sf("../../ext/#{ext}/#{ext}.so", "src/lib/yast/#{ext}.so")
end
end
Expand Down
6 changes: 6 additions & 0 deletions package/yast2-fonts.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
-------------------------------------------------------------------
Thu Nov 13 11:11:22 UTC 2014 - pgajdos@suse.com

- add match preview tab
- 3.1.9

-------------------------------------------------------------------
Fri Oct 24 16:41:08 UTC 2014 - pgajdos@suse.com

Expand Down
3 changes: 2 additions & 1 deletion package/yast2-fonts.spec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


Name: yast2-fonts
Version: 3.1.8
Version: 3.1.9
Release: 0

BuildRoot: %{_tmppath}/%{name}-%{version}-build
Expand All @@ -36,6 +36,7 @@ BuildRequires: rubygem-yast-rake
# extensions
BuildRequires: freetype2-devel
BuildRequires: fontconfig-devel
BuildRequires: font-specimen-devel
BuildRequires: ruby-devel
# for testing
BuildRequires: rubygem-rspec
Expand Down
9 changes: 9 additions & 0 deletions src/ext/font_specimen/extconf.rb
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)

79 changes: 79 additions & 0 deletions src/ext/font_specimen/font-specimen.c
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);
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. char *pattern = StringValueCStr(str_pattern); ok
  2. You mean premature initialization of res_hash? I wanted to return hash (empty or not) in any case.

Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.


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");
Copy link
Member

Choose a reason for hiding this comment

The 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;
}

80 changes: 71 additions & 9 deletions src/ext/fontconfig_setting/fontconfig-setting.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it can be written like result = fontset->nfont > 0 ? FcPatternDuplicate(fontset->fonts[0]) : NULL

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return r == FcResultMatch ? font : NULL;

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

res = font ? Qtrue : Qfalse

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
}

6 changes: 3 additions & 3 deletions src/lib/fonts/fonts-config-state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ class FontsConfigState
"bw_fonts" => {
"name" => N_("Black and White Rendering"),
"fpl" => {
"sans-serif" => [],
"serif" => [],
"monospace" => [],
"sans-serif" => [ "Liberation Sans" ],
"serif" => [ "Liberation Serif" ],
"monospace" => [ "Liberation Mono" ],
},
"search_metric_compatible" => true,
"really_force_fpl" => false,
Expand Down
Loading