Class: Fontconfig::Pattern

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/fontconfig.rb,
ext/fontconfig/fc_pattern.c

Defined Under Namespace

Classes: Proxy

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(*args) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'ext/fontconfig/fc_pattern.c', line 35

static VALUE rb_pattern_new(int argc, VALUE *argv, VALUE klass){
  VALUE optional = Qnil;
  rb_scan_args(argc, argv, "01", &optional);
  if(optional != Qnil){
    if(TYPE(optional) == T_STRING)
      return rb_pattern_parse(klass, optional);

    rb_raise(rb_eArgError, "parameter must be a string");
  } else {
    return pattern_wrap(FcPatternCreate());
  }
}

.parse(string) ⇒ Object



28
29
30
31
32
33
# File 'ext/fontconfig/fc_pattern.c', line 28

static VALUE rb_pattern_parse(VALUE klass, VALUE string){
  if(TYPE(string) != T_STRING)
    string = rb_any_to_s(string);
  FcPattern* p = FcNameParse(RSTRING_PTR(string));
  return pattern_wrap(p);
}

Instance Method Details

#==(other) ⇒ Object



74
75
76
77
78
79
80
# File 'ext/fontconfig/fc_pattern.c', line 74

static VALUE rb_pattern_equal(VALUE self, VALUE other){
  if(!is_fc_pattern(other)){
    //TODO: cast?
    return Qfalse;
  }
  return FcPatternEqual(PATTERN_UNWRAP(self), PATTERN_UNWRAP(other)) ? Qtrue : Qfalse;
}

#[](key) ⇒ Object



34
35
36
37
38
# File 'lib/fontconfig.rb', line 34

def [](key)
  key = key.to_s
  return nil unless has_key?(key)
  return Proxy.new(self, key)
end

#add(*args) ⇒ Object

filter - need set



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'ext/fontconfig/fc_pattern.c', line 87

static VALUE rb_pattern_add(int argc, VALUE *argv, VALUE self){
  VALUE object, value, append;
  rb_scan_args(argc, argv, "21", &object, &value, &append);
  FcValue val;
  switch(TYPE(value)){
    case T_NONE: case T_NIL: val.type = FcTypeVoid; break;
    case T_FIXNUM: val.type = FcTypeInteger; val.u.i = NUM2INT(value); break;
    case T_FLOAT: val.type = FcTypeDouble; val.u.d = NUM2DBL(value); break;
    case T_STRING: val.type = FcTypeString; val.u.s = RSTRING_PTR(value); break;
    case T_TRUE: val.type = FcTypeBool; val.u.b = 1; break;
    case T_FALSE: val.type = FcTypeBool; val.u.b = 0; break;
    case T_OBJECT:
    case T_DATA:
      
      //TODO: increment object references?
      // ...
      //  FcTypeMatrix: //    m               FcMatrix *
      //  FcTypeCharSet: //   c               FcCharSet *
      //  FcTypeFTFace: //  f   void * (FT_Face)
      //  FcTypeLangSet
    default:
      rb_raise(rb_eArgError, "unsupported type for value");
      break;
  }
  if(TYPE(object)!=T_STRING)
    object = rb_any_to_s(object);
  int res = FcPatternAdd(PATTERN_UNWRAP(self), RSTRING_PTR(object), val, RTEST(append));
  //TODO: raise on errors?
  return res? Qtrue : Qfalse;
}

#config_substitute!(config = Fontconfig.current_config, kind = :font) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/fontconfig.rb', line 47

def config_substitute! config=Fontconfig.current_config, kind=:font
  #kind - :pattern, :font, :scan
  unless config.substitute(self)
    raise "cannot perform substitutions on pattern"
  end
  self
end

#debug_printObject



69
70
71
72
# File 'ext/fontconfig/fc_pattern.c', line 69

static VALUE rb_pattern_debugprint(VALUE self){
  FcPatternPrint(PATTERN_UNWRAP(self));
  return Qnil;
}

#default_substitute!Object



177
178
179
180
# File 'ext/fontconfig/fc_pattern.c', line 177

static VALUE rb_pattern_default_substitute(VALUE self){
  FcDefaultSubstitute(PATTERN_UNWRAP(self));
  return self;
}

#delete(object) ⇒ Object



162
163
164
165
166
167
# File 'ext/fontconfig/fc_pattern.c', line 162

static VALUE rb_pattern_delete(VALUE self, VALUE object){
  if(TYPE(object)!=T_STRING)
    object = rb_any_to_s(object);
  int res = FcPatternDel(PATTERN_UNWRAP(self), RSTRING_PTR(object));
  return res? Qtrue : Qfalse;
}

#dupObject



64
65
66
67
# File 'ext/fontconfig/fc_pattern.c', line 64

static VALUE rb_pattern_duplicate(VALUE self){
  FcPattern* p = FcPatternDuplicate(PATTERN_UNWRAP(self));
  return pattern_wrap(p);
}

#each_keyObject Also known as: each



185
186
187
188
189
190
191
192
193
194
195
# File 'ext/fontconfig/fc_pattern.c', line 185

static VALUE rb_pattern_each_key(VALUE self){
  FcPattern* p = PATTERN_UNWRAP(self);
  int i;
  FcPatternElt* e;
  for (i = 0; i < p->num; i++){
    e = &FcPatternElts(p)[i];
    //FIXME: return as symbols?
    rb_yield(rb_str_new2(FcObjectName(e->object)));
  }
  return self;
}

#each_value(key) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
# File 'ext/fontconfig/fc_pattern.c', line 219

static VALUE rb_pattern_each_key_value(VALUE self, VALUE key){
  FcPattern* p = PATTERN_UNWRAP(self);
  FcPatternElt* e = FcPatternObjectFindElt(p, FcObjectFromName (StringValuePtr(key)));
  FcValueListPtr l;
  if(e){
    for (l = FcPatternEltValues(e); l; l = FcValueListNext(l)){
      // FcValueCanonicalize(&l->value); - re allocates value pointers, do not need this
      rb_yield(fc_value_to_value(&l->value));
    }
  }
  return self;
}

#filenameObject



67
68
69
# File 'lib/fontconfig.rb', line 67

def filename
  self[:filename].first
end

#format(format) ⇒ Object



48
49
50
51
52
53
54
55
# File 'ext/fontconfig/fc_pattern.c', line 48

static VALUE rb_pattern_format(VALUE self, VALUE format){
  if(TYPE(format) != T_STRING)
    format = rb_any_to_s(format);
  char* res = FcPatternFormat(PATTERN_UNWRAP(self), RSTRING_PTR(format));
  VALUE str = rb_str_new2((char*)res);
  FcStrFree(res);
  return str;
}

#get(object, id) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'ext/fontconfig/fc_pattern.c', line 140

static VALUE rb_pattern_get(VALUE self, VALUE object, VALUE id){
  Check_Type(id, T_FIXNUM);
  if(TYPE(object)!=T_STRING)
    object = rb_any_to_s(object);
  FcValue val;
  FcResult res = FcPatternGet(PATTERN_UNWRAP(self), RSTRING_PTR(object), FIX2INT(id), &val);
  if(res == FcResultMatch){
    VALUE r_res = fc_value_to_value(&val);
    // no need to free, ptr to internal mem
    return r_res;
  }
  if(res == FcResultNoMatch || res == FcResultNoId){
    //raise?
    return Qnil;
  }
  if(res == FcResultOutOfMemory){
    rb_raise(rb_eRuntimeError, "FcResultOutOfMemory");
  }
  //FcResultTypeMismatch cannot be here
  return Qnil;
}

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


197
198
199
200
201
202
203
204
# File 'ext/fontconfig/fc_pattern.c', line 197

static VALUE rb_pattern_has_key_p(VALUE self, VALUE key){
  FcPattern* p = PATTERN_UNWRAP(self);
  FcPatternElt* e = FcPatternObjectFindElt(p, FcObjectFromName (StringValuePtr(key)));
  if(e){
    return Qtrue;
  }
  return Qfalse;
}

#hashObject



82
83
84
85
# File 'ext/fontconfig/fc_pattern.c', line 82

static VALUE rb_pattern_hash(VALUE self){
  int res = FcPatternHash(PATTERN_UNWRAP(self));
  return INT2NUM(res);
}

#inspectObject



40
41
42
# File 'lib/fontconfig.rb', line 40

def inspect
  "#<#{self.class.name} #{self.format('%{=fcmatch}')}>"
end

#keysObject



206
207
208
209
210
211
212
213
214
215
216
217
# File 'ext/fontconfig/fc_pattern.c', line 206

static VALUE rb_pattern_get_keys(VALUE self){
  FcPattern* p = PATTERN_UNWRAP(self);
  VALUE res = rb_ary_new();
  int i;
  FcPatternElt* e;
  for (i = 0; i < p->num; i++){
    e = &FcPatternElts(p)[i];
    rb_ary_push(res, rb_str_new2(FcObjectName(e->object)));
    // FcValueListPrint (FcPatternEltValues(e));
  }
  return res;
}

#match(config = Fontconfig.current_config) ⇒ Object



62
63
64
65
# File 'lib/fontconfig.rb', line 62

def match config=Fontconfig.current_config
  raise "unprepared pattern match" unless @prepared
  config.font_match self
end

#prepare!(config = Fontconfig.current_config, kind = :font) ⇒ Object



55
56
57
58
59
60
# File 'lib/fontconfig.rb', line 55

def prepare! config=Fontconfig.current_config, kind=:font
  @prepared = true
  config_substitute! config, kind
  default_substitute!
  self
end

#remove(object, id) ⇒ Object



169
170
171
172
173
174
175
# File 'ext/fontconfig/fc_pattern.c', line 169

static VALUE rb_pattern_remove(VALUE self, VALUE object, VALUE id){
  Check_Type(id, T_FIXNUM);
  if(TYPE(object)!=T_STRING)
    object = rb_any_to_s(object);
  int res = FcPatternRemove(PATTERN_UNWRAP(self), RSTRING_PTR(object), FIX2INT(id));
  return res? Qtrue : Qfalse;
}

#to_sObject



57
58
59
60
61
62
# File 'ext/fontconfig/fc_pattern.c', line 57

static VALUE rb_pattern_to_s(VALUE self){
  char* res = FcNameUnparse(PATTERN_UNWRAP(self));
  VALUE str = rb_str_new2((char*)res);
  FcStrFree(res);
  return str;
}

#unparseObject



57
58
59
60
61
62
# File 'ext/fontconfig/fc_pattern.c', line 57

static VALUE rb_pattern_to_s(VALUE self){
  char* res = FcNameUnparse(PATTERN_UNWRAP(self));
  VALUE str = rb_str_new2((char*)res);
  FcStrFree(res);
  return str;
}