[cairo-commit] rcairo/src cairo.def, 1.15, 1.16 rb_cairo.c, 1.19, 1.20 rb_cairo.h, 1.20, 1.21 rb_cairo_glyph.c, 1.3, 1.4 rb_cairo_private.c, 1.6, 1.7 rb_cairo_private.h, 1.9, 1.10 rb_cairo_scaled_font.c, 1.5, 1.6 rb_cairo_text_cluster.c, NONE, 1.1

Kouhei Sutou commit at pdx.freedesktop.org
Sat Aug 16 01:16:42 PDT 2008


Committed by: kou

Update of /cvs/cairo/rcairo/src
In directory kemper:/tmp/cvs-serv8545/src

Modified Files:
	cairo.def rb_cairo.c rb_cairo.h rb_cairo_glyph.c 
	rb_cairo_private.c rb_cairo_private.h rb_cairo_scaled_font.c 
Added Files:
	rb_cairo_text_cluster.c 
Log Message:
        * test/test_text_cluster.rb: add tests for Cairo::TextCluster.

        * test/test_scaled_font.rb (ScaledFontTest#test_text_to_glyphs):
        add a test for Cairo::ScaledFont#text_to_glyphs.

        * src/rb_cairo.h, src/rb_cairo.c, src/cairo.def,
        src/rb_cairo_text_cluster.c: add Cairo::TextCluster.

        * src/rb_cairo_scaled_font.c: support
        Cairo::ScaledFont#text_to_glyphs.

        * src/rb_cairo_private.[ch] (rb_cairo__glyphs_to_ruby_object),
        (rb_cairo__text_clusters_to_ruby_object): add.


Index: cairo.def
===================================================================
RCS file: /cvs/cairo/rcairo/src/cairo.def,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -d -r1.15 -r1.16
--- cairo.def	14 Aug 2008 08:11:10 -0000	1.15
+++ cairo.def	16 Aug 2008 08:16:37 -0000	1.16
@@ -23,6 +23,7 @@
     rb_cCairo_ScaledFont      DATA
     rb_cCairo_TextExtents     DATA
     rb_cCairo_Glyph           DATA
+    rb_cCairo_TextCluster     DATA
     rb_cCairo_Surface         DATA
     rb_cCairo_ImageSurface    DATA
     rb_cCairo_PDFSurface      DATA
@@ -76,6 +77,8 @@
     rb_cairo_text_extents_to_ruby_object
     rb_cairo_glyph_from_ruby_object
     rb_cairo_glyph_to_ruby_object
+    rb_cairo_text_cluster_from_ruby_object
+    rb_cairo_text_cluster_to_ruby_object
     rb_cairo_surface_from_ruby_object
     rb_cairo_surface_to_ruby_object
     rb_cairo_surface_to_ruby_object_with_destroy

Index: rb_cairo.c
===================================================================
RCS file: /cvs/cairo/rcairo/src/rb_cairo.c,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -d -r1.19 -r1.20
--- rb_cairo.c	13 Aug 2008 08:27:44 -0000	1.19
+++ rb_cairo.c	16 Aug 2008 08:16:39 -0000	1.20
@@ -100,4 +100,5 @@
   Init_cairo_text_extents ();
   Init_cairo_pattern ();
   Init_cairo_glyph ();
+  Init_cairo_text_cluster ();
 }

Index: rb_cairo.h
===================================================================
RCS file: /cvs/cairo/rcairo/src/rb_cairo.h,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -d -r1.20 -r1.21
--- rb_cairo.h	14 Aug 2008 08:11:11 -0000	1.20
+++ rb_cairo.h	16 Aug 2008 08:16:39 -0000	1.21
@@ -160,6 +160,9 @@
 #define RVAL2CRGLYPH(obj)       (rb_cairo_glyph_from_ruby_object(obj))
 #define CRGLYPH2RVAL(glyph)     (rb_cairo_glyph_to_ruby_object(glyph))
 
+#define RVAL2CRTEXTCLUSTER(obj)     (rb_cairo_text_cluster_from_ruby_object(obj))
+#define CRTEXTCLUSTER2RVAL(cluster) (rb_cairo_text_cluster_to_ruby_object(cluster))
+
 #define RVAL2CRSURFACE(obj)     (rb_cairo_surface_from_ruby_object(obj))
 #define CRSURFACE2RVAL(surface) (rb_cairo_surface_to_ruby_object(surface))
 #define CRSURFACE2RVAL_WITH_DESTROY(surface) \
@@ -195,6 +198,9 @@
 cairo_glyph_t        *rb_cairo_glyph_from_ruby_object        (VALUE obj);
 VALUE                 rb_cairo_glyph_to_ruby_object          (cairo_glyph_t *glyph);
 
+cairo_text_cluster_t *rb_cairo_text_cluster_from_ruby_object (VALUE obj);
+VALUE                 rb_cairo_text_cluster_to_ruby_object   (cairo_text_cluster_t *cluster);
+
 cairo_surface_t      *rb_cairo_surface_from_ruby_object      (VALUE obj);
 VALUE                 rb_cairo_surface_to_ruby_object        (cairo_surface_t *surface);
 VALUE                 rb_cairo_surface_to_ruby_object_with_destroy

Index: rb_cairo_glyph.c
===================================================================
RCS file: /cvs/cairo/rcairo/src/rb_cairo_glyph.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- rb_cairo_glyph.c	3 May 2007 02:47:39 -0000	1.3
+++ rb_cairo_glyph.c	16 Aug 2008 08:16:39 -0000	1.4
@@ -37,7 +37,7 @@
 {
   if (ptr)
     {
-      free (ptr);
+      xfree (ptr);
     }
 }
 
@@ -46,7 +46,9 @@
 {
   if (glyph)
     {
-      cairo_glyph_t *new_glyph = ALLOC (cairo_glyph_t);
+      cairo_glyph_t *new_glyph;
+
+      new_glyph = ALLOC (cairo_glyph_t);
       *new_glyph = *glyph;
       return Data_Wrap_Struct (rb_cCairo_Glyph, NULL, cr_glyph_free, new_glyph);
     }

Index: rb_cairo_private.c
===================================================================
RCS file: /cvs/cairo/rcairo/src/rb_cairo_private.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- rb_cairo_private.c	14 Aug 2008 08:11:13 -0000	1.6
+++ rb_cairo_private.c	16 Aug 2008 08:16:39 -0000	1.7
@@ -129,6 +129,37 @@
   return RSTRING_PTR (inspected);
 }
 
+VALUE
+rb_cairo__glyphs_to_ruby_object (cairo_glyph_t *glyphs, int n_glyphs)
+{
+  int i;
+  VALUE rb_glyphs;
+
+  rb_glyphs = rb_ary_new2 (n_glyphs);
+  for (i = 0; i < n_glyphs; i++)
+    {
+      RARRAY_PTR (rb_glyphs)[i] = CRGLYPH2RVAL (glyphs + i);
+    }
+
+  return rb_glyphs;
+}
+
+VALUE
+rb_cairo__text_clusters_to_ruby_object (cairo_text_cluster_t *clusters,
+                                        int n_clusters)
+{
+  int i;
+  VALUE rb_clusters;
+
+  rb_clusters = rb_ary_new2 (n_clusters);
+  for (i = 0; i < n_clusters; i++)
+    {
+      RARRAY_PTR (rb_clusters)[i] = CRTEXTCLUSTER2RVAL (clusters + i);
+    }
+
+  return rb_clusters;
+}
+
 void
 Init_cairo_private (void)
 {

Index: rb_cairo_private.h
===================================================================
RCS file: /cvs/cairo/rcairo/src/rb_cairo_private.h,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- rb_cairo_private.h	14 Aug 2008 08:11:14 -0000	1.9
+++ rb_cairo_private.h	16 Aug 2008 08:16:39 -0000	1.10
@@ -57,6 +57,7 @@
 extern void Init_cairo_text_extents (void);
 extern void Init_cairo_pattern (void);
 extern void Init_cairo_glyph (void);
+extern void Init_cairo_text_cluster (void);
 
 #define RB_CAIRO__GLYPHS_TO_ARRAY(rb_array, glyphs, length)     \
 do                                                              \
@@ -92,4 +93,8 @@
 
 const char *rb_cairo__inspect (VALUE object);
 
+VALUE rb_cairo__glyphs_to_ruby_object (cairo_glyph_t *glyphs, int n_glyphs);
+VALUE rb_cairo__text_clusters_to_ruby_object (cairo_text_cluster_t *clusters,
+                                              int n_clusters);
+
 #endif

Index: rb_cairo_scaled_font.c
===================================================================
RCS file: /cvs/cairo/rcairo/src/rb_cairo_scaled_font.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- rb_cairo_scaled_font.c	14 Aug 2008 12:37:51 -0000	1.5
+++ rb_cairo_scaled_font.c	16 Aug 2008 08:16:39 -0000	1.6
@@ -114,6 +114,42 @@
   return CRTEXTEXTENTS2RVAL (&extents);
 }
 
+#if CAIRO_CHECK_VERSION(1, 7, 2)
+static VALUE
+cr_scaled_font_text_to_glyphs (VALUE self, VALUE rb_x, VALUE rb_y, VALUE rb_utf8)
+{
+  double x, y;
+  const char *utf8;
+  int utf8_len;
+  cairo_glyph_t *glyphs = NULL;
+  int n_glyphs;
+  cairo_text_cluster_t *clusters = NULL;
+  int n_clusters;
+  cairo_bool_t backward;
+  cairo_status_t status;
+  VALUE rb_glyphs, rb_clusters;
+
+  x = NUM2DBL (rb_x);
+  y = NUM2DBL (rb_y);
+  utf8 = RSTRING_PTR (rb_utf8);
+  utf8_len = RSTRING_LEN (rb_utf8);
+
+  status = cairo_scaled_font_text_to_glyphs (_SELF (self),
+                                             x, y, utf8, utf8_len,
+                                             &glyphs, &n_glyphs,
+                                             &clusters, &n_clusters,
+                                             &backward);
+  rb_cairo_check_status (status);
+
+  rb_glyphs = rb_cairo__glyphs_to_ruby_object (glyphs, n_glyphs);
+  cairo_glyph_free (glyphs);
+  rb_clusters = rb_cairo__text_clusters_to_ruby_object (clusters, n_clusters);
+  cairo_text_cluster_free (clusters);
+
+  return rb_ary_new3 (3, rb_glyphs, rb_clusters, CBOOL2RVAL (backward));
+}
+#endif
+
 static VALUE
 cr_scaled_font_get_font_face (VALUE self)
 {
@@ -181,6 +217,10 @@
                     cr_scaled_font_text_extents, 1);
   rb_define_method (rb_cCairo_ScaledFont, "glyph_extents",
                     cr_scaled_font_glyph_extents, 1);
+#if CAIRO_CHECK_VERSION(1, 7, 2)
+  rb_define_method (rb_cCairo_ScaledFont, "text_to_glyphs",
+                    cr_scaled_font_text_to_glyphs, 3);
+#endif
   rb_define_method (rb_cCairo_ScaledFont, "font_face",
                     cr_scaled_font_get_font_face, 0);
   rb_define_method (rb_cCairo_ScaledFont, "font_matrix",

--- NEW FILE: rb_cairo_text_cluster.c ---
/* -*- c-file-style: "gnu"; indent-tabs-mode: nil -*- */
/*
 * Ruby Cairo Binding
 *
 * $Author: kou $
 * $Date: 2008-08-16 08:16:40 $
 *
 * Copyright 2008 Kouhei Sutou <kou at 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))

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));
    }
  Data_Get_Struct (obj, cairo_text_cluster_t, cluster);
  return cluster;
}

static void
cr_text_cluster_free (void *ptr)
{
  if (ptr)
    {
      xfree (ptr);
    }
}

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 Data_Wrap_Struct (rb_cCairo_TextCluster, NULL,
                               cr_text_cluster_free, new_cluster);
    }
  else
    {
      return Qnil;
    }
}

static VALUE
cr_text_cluster_allocate (VALUE klass)
{
  return Data_Wrap_Struct (klass, NULL, cr_text_cluster_free, 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);

  DATA_PTR (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
}



More information about the cairo-commit mailing list