|
| 1 | +#include "ruby.h" |
| 2 | +#include <fontconfig.h> |
| 3 | +#include <string.h> |
| 4 | +#include <stdio.h> |
| 5 | + |
| 6 | +const char *blacklist_families[] = { |
| 7 | + "micro.pcf", |
| 8 | + "deccurs.pcf", |
| 9 | + "decsess.pcf", |
| 10 | + "cursor.pcf", |
| 11 | + }; |
| 12 | + |
| 13 | +#define NBLACK (sizeof(blacklist_families)/sizeof(blacklist_families[0])) |
| 14 | + |
| 15 | +VALUE FontconfigSetting = Qnil; |
| 16 | + |
| 17 | +void Init_fontconfig_setting(); |
| 18 | +VALUE method_fc_installed_families(VALUE self, VALUE array_elements); |
| 19 | +VALUE method_fc_is_family_installed(VALUE self, VALUE str_family); |
| 20 | + |
| 21 | +void Init_fontconfig_setting() { |
| 22 | + FontconfigSetting = rb_define_module("FontconfigSetting"); |
| 23 | + rb_define_method(FontconfigSetting, "fc_installed_families", |
| 24 | + method_fc_installed_families, 1); |
| 25 | + rb_define_method(FontconfigSetting, "fc_is_family_installed", |
| 26 | + method_fc_is_family_installed, 1); |
| 27 | +} |
| 28 | + |
| 29 | +VALUE method_fc_installed_families(VALUE self, VALUE array_elements) { |
| 30 | + VALUE str_family_list = rb_ary_new(); |
| 31 | + |
| 32 | + FcObjectSet *objectset; |
| 33 | + FcFontSet *fontset; |
| 34 | + FcPattern *empty; |
| 35 | + |
| 36 | + char *str_pattern; |
| 37 | + int i, j; |
| 38 | + VALUE el; |
| 39 | + |
| 40 | + FcInit(); |
| 41 | + objectset = FcObjectSetCreate(); |
| 42 | + while ((el = rb_ary_shift(array_elements)) != Qnil) |
| 43 | + { |
| 44 | + FcObjectSetAdd(objectset, StringValueCStr(el)); |
| 45 | + } |
| 46 | + empty = FcPatternCreate(); |
| 47 | + fontset = FcFontList (NULL, empty, objectset); |
| 48 | + FcObjectSetDestroy (objectset); |
| 49 | + FcPatternDestroy(empty); |
| 50 | + |
| 51 | + for (i = 0; i < fontset->nfont; i++) |
| 52 | + { |
| 53 | + str_pattern = (char *)FcPatternFormat(fontset->fonts[i], (const FcChar8 *)"%{=fclist}"); |
| 54 | + for (j = 0; j < NBLACK; j++) |
| 55 | + if (strcmp(blacklist_families[j], str_pattern) == 0) |
| 56 | + break; |
| 57 | + if (j < NBLACK) |
| 58 | + continue; |
| 59 | + rb_ary_push(str_family_list, rb_str_new2(str_pattern)); |
| 60 | + } |
| 61 | + |
| 62 | + return str_family_list; |
| 63 | +} |
| 64 | + |
| 65 | +VALUE method_fc_is_family_installed(VALUE self, VALUE str_family) { |
| 66 | + FcObjectSet *objectset; |
| 67 | + FcPattern *pattern; |
| 68 | + FcFontSet *fontset; |
| 69 | + |
| 70 | + char *family; |
| 71 | + |
| 72 | + family = StringValueCStr(str_family); |
| 73 | + |
| 74 | + FcInit(); |
| 75 | + objectset = FcObjectSetBuild(FC_FAMILY, NULL); |
| 76 | + pattern = FcNameParse((FcChar8 *)family); |
| 77 | + fontset = FcFontList (NULL, pattern, objectset); |
| 78 | + FcPatternDestroy (pattern); |
| 79 | + FcObjectSetDestroy (objectset); |
| 80 | + |
| 81 | + if (fontset->nfont > 0) |
| 82 | + return Qtrue; |
| 83 | + else |
| 84 | + return Qfalse; |
| 85 | +} |
| 86 | + |
0 commit comments