File: rb_cairo_text_cluster.c

package info (click to toggle)
ruby-cairo 1.17.13-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,532 kB
  • sloc: ruby: 11,997; ansic: 10,183; sh: 48; makefile: 4
file content (157 lines) | stat: -rw-r--r-- 3,808 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* -*- c-file-style: "gnu"; indent-tabs-mode: nil -*- */
/*
 * Ruby Cairo Binding
 *
 * $Author: kou $
 * $Date: 2008-08-16 08:16:40 $
 *
 * Copyright 2008-2022 Sutou Kouhei <kou@cozmixng.org>
 *
 * This file is made available under the same terms as Ruby
 *
 */


#include "rb_cairo.h"
#include "rb_cairo_private.h"

VALUE rb_cCairo_TextCluster = Qnil;

#if CAIRO_CHECK_VERSION(1, 7, 2)
#define _SELF(self)  (RVAL2CRTEXTCLUSTER(self))

static const rb_data_type_t cr_text_cluster_type = {
  "Cairo::TextCluster",
  {
    NULL,
    ruby_xfree,
  },
  NULL,
  NULL,
  RUBY_TYPED_FREE_IMMEDIATELY,
};

cairo_text_cluster_t *
rb_cairo_text_cluster_from_ruby_object (VALUE obj)
{
  cairo_text_cluster_t *cluster;
  if (!rb_cairo__is_kind_of (obj, rb_cCairo_TextCluster))
    {
      rb_raise (rb_eTypeError,
                "not a cairo cluster: %s", rb_cairo__inspect (obj));
    }
  TypedData_Get_Struct (obj,
                        cairo_text_cluster_t,
                        &cr_text_cluster_type,
                        cluster);
  return cluster;
}

VALUE
rb_cairo_text_cluster_to_ruby_object (cairo_text_cluster_t *cluster)
{
  if (cluster)
    {
      cairo_text_cluster_t *new_cluster;

      new_cluster = ALLOC (cairo_text_cluster_t);
      *new_cluster = *cluster;
      return TypedData_Wrap_Struct (rb_cCairo_TextCluster,
                                    &cr_text_cluster_type,
                                    new_cluster);
    }
  else
    {
      return Qnil;
    }
}

static VALUE
cr_text_cluster_allocate (VALUE klass)
{
  return TypedData_Wrap_Struct (klass, &cr_text_cluster_type, NULL);
}

static VALUE
cr_text_cluster_initialize (VALUE self, VALUE num_bytes, VALUE num_glyphs)
{
  cairo_text_cluster_t *cluster;

  cluster = ALLOC (cairo_text_cluster_t);
  cluster->num_bytes = NUM2INT (num_bytes);
  cluster->num_glyphs = NUM2INT (num_glyphs);

  RTYPEDDATA_DATA (self) = cluster;
  return Qnil;
}

static VALUE
cr_text_cluster_num_bytes (VALUE self)
{
  return INT2NUM (_SELF(self)->num_bytes);
}

static VALUE
cr_text_cluster_num_glyphs (VALUE self)
{
  return INT2NUM (_SELF(self)->num_glyphs);
}

static VALUE
cr_text_cluster_set_num_bytes (VALUE self, VALUE num_bytes)
{
  _SELF(self)->num_bytes = NUM2INT (num_bytes);
  return self;
}

static VALUE
cr_text_cluster_set_num_glyphs (VALUE self, VALUE num_glyphs)
{
  _SELF(self)->num_glyphs = NUM2INT (num_glyphs);
  return self;
}

static VALUE
cr_text_cluster_to_s (VALUE self)
{
  VALUE ret;

  ret = rb_str_new2 ("#<");
  rb_str_cat2 (ret, rb_class2name (CLASS_OF (self)));
  rb_str_cat2 (ret, ": ");
  rb_str_cat2 (ret, "num_bytes=");
  rb_str_concat (ret, rb_inspect (cr_text_cluster_num_bytes (self)));
  rb_str_cat2 (ret, ", ");
  rb_str_cat2 (ret, "num_glyphs=");
  rb_str_concat (ret, rb_inspect (cr_text_cluster_num_glyphs (self)));
  rb_str_cat2 (ret, ">");

  return ret;
}
#endif

void
Init_cairo_text_cluster (void)
{
#if CAIRO_CHECK_VERSION(1, 7, 2)
  rb_cCairo_TextCluster = rb_define_class_under (rb_mCairo, "TextCluster", rb_cObject);

  rb_define_alloc_func (rb_cCairo_TextCluster, cr_text_cluster_allocate);

  rb_define_method (rb_cCairo_TextCluster, "initialize",
                    cr_text_cluster_initialize, 2);

  rb_define_method (rb_cCairo_TextCluster, "num_bytes",
                    cr_text_cluster_num_bytes, 0);
  rb_define_method (rb_cCairo_TextCluster, "num_glyphs",
                    cr_text_cluster_num_glyphs, 0);
  rb_define_method (rb_cCairo_TextCluster, "set_num_bytes",
                    cr_text_cluster_set_num_bytes, 1);
  rb_define_method (rb_cCairo_TextCluster, "set_num_glyphs",
                    cr_text_cluster_set_num_glyphs, 1);

  rb_define_method (rb_cCairo_TextCluster, "to_s", cr_text_cluster_to_s, 0);

  RB_CAIRO_DEF_SETTERS (rb_cCairo_TextCluster);
#endif
}