Example #1
0
	SharedStringList KRubyObject::GetPropertyNames()
	{
		SharedStringList names(new StringList());
		VALUE vars = rb_obj_instance_variables(object);
		for (int i = 0; i < RARRAY_LEN(vars); i++)
		{
			VALUE prop_name = rb_ary_entry(vars, i);
			std::string name(StringValueCStr(prop_name));
			if (name[0] == '@')
				name = name.substr(1);

			names->push_back(new std::string(name));
		}

		VALUE methodsSymbol = rb_intern("methods");
		if (!rb_obj_respond_to(object, methodsSymbol, Qfalse))
			return names;

		VALUE methods = rb_funcall(object, rb_intern("methods"), 0);
		for (int i = 0; i < RARRAY_LEN(methods); i++)
		{
			VALUE rmethodName = rb_ary_entry(methods, i);
			const char *methodName = StringValueCStr(rmethodName);
			names->push_back(new std::string(methodName));
		}

		return names;
	}
Example #2
0
int
ca_has_mask (void *ap)
{
  CArray *ca = (CArray *) ap;

  if ( ca->mask ) {                /* mask array already created */
    return 1;
  }
  else if ( ca_is_value_array(ca) ) {
    return 0;                     /* array itself is returned by CArray#value */
  }
  else if ( ca_is_entity(ca) ) {   /* entity array */
    return ( ca->mask != NULL ) ? 1 : 0;
  }
  else if ( ca_is_caobject(ca) ) { /* CAObject */
    CAObject *co = (CAObject *) ca;
    if ( rb_obj_respond_to(co->self, rb_intern("mask_created?"), Qtrue) ) {
      return RTEST(rb_funcall(co->self, rb_intern("mask_created?"), 0));
    }
    return ( ca->mask != NULL ) ? 1 : 0;
  }
  else {
    CAVirtual *cr = (CAVirtual *) ca; /* virtual array, check parent array */
    if ( ca_has_mask(cr->parent) ) {
      ca_create_mask(ca);
      return 1;
    }
    else {
      return 0;
    }
  }
}
Example #3
0
	static int MethodArity(VALUE method)
	{
		static VALUE aritySymbol = rb_intern("arity");
		if (!rb_obj_respond_to(method, aritySymbol, 0))
			return 0;

		return NUM2INT(rb_apply(method, aritySymbol, rb_ary_new()));
	}
Example #4
0
/*
  call-seq:
    load_image(io, required_components = COMPONENTS_DEFAULT) => [data, width, height, components]
    load_image(io, required_components = COMPONENTS_DEFAULT) { |info| ... } => obj

  Loads an image using stb_image and returns the resulting data and its width,
  height, and the number of components per pixel in the data. The returned data
  is a packed string of unsigned 8-bit integers (8 bits per component). The
  length of the string will always be width * height * components.

  In the second form, the info array is yielded to the block if the image is
  successfully loaded. Otherwise, the method returns nil. This is possibly more
  convenient than doing an <tt>if info ... end</tt> block to check if the
  image was successfully loaded.

  === IO Objects

  IO objects accepted for loading data with must implement at least
  IO#read(length) and either IO#eof or IO#eof?. In addition, they may also
  implement a skip(length) method that skips length bytes and does not return
  any value. If the IO object does not respond to #skip, #read will be called
  instead and its result will be discarded. If you want to avoid unnecessary
  allocations, it may be wise to implement a skip method.

  === Components

  If required_components is provided and not the default value, the image data
  returned will have as many components as are requested. In turn, the number of
  components returned via the array will be the same as required_components.

  Valid options for required_components are:

  [::COMPONENTS_DEFAULT]    The default value, loads as many components as are
                            provided by the image.
  [::COMPONENTS_GREY]       Loads only one component.
  [::COMPONENTS_GREY_ALPHA] Loads two components.
  [::COMPONENTS_RGB]        Loads three components (red, green, and blue).
  [::COMPONENTS_RGB_ALPHA]  Loads four components (red, green, blue, and alpha).

  === Example

    open('image.png') { |io|
      STBI.load_image(io) { |data, width, height, components|
        format = case components
                 when STBI::COMPONENTS_GREY       then Gl::GL_RED
                 when STBI::COMPONENTS_GREY_ALPHA then Gl::GL_RG
                 when STBI_COMPONENTS_RGB         then Gl::RGB
                 when STBI_COMPONENTS_RGB_ALPHA   then Gl::RGBA
                 end

        Gl::glTexImage2D(Gl::GL_TEXTURE_2D, 0, format, width, height, 0,
                         format, Gl::GL_UNSIGNED_BYTE, data)
      }
    }
 */
static VALUE sr_load_image(int argc, VALUE *argv, VALUE sr_self)
{
  VALUE sr_callbacks;
  VALUE sr_req_comp;
  VALUE sr_image_data = Qnil;
  int x = 0;
  int y = 0;
  int components[2] = {
    STBI_default,
    0
  };

  rb_scan_args(argc, argv, "11", &sr_callbacks, &sr_req_comp);

  if (NIL_P(sr_callbacks)) {
    rb_raise(rb_eArgError, "IO object cannot be nil");
    return Qnil;
  } if (RTEST(sr_req_comp)) {
    components[0] = FIX2INT(sr_req_comp);
  }

  if (!rb_obj_respond_to(sr_callbacks, s_read_funcid, 0) ||
      !(rb_obj_respond_to(sr_callbacks, s_eof_funcid, 0) ||
        rb_obj_respond_to(sr_callbacks, s_eof_qm_funcid, 0))) {
    rb_raise(rb_eTypeError, "IO object does not respond to either read or eof/eof?");
  }

  stbi_uc *data = stbi_load_from_callbacks(&s_st_callbacks, &sr_callbacks,
    &x, &y, &components[1], components[0]);

  if (data) {
    const long length = x * y * components[!components[0]];
    sr_image_data = rb_ary_new3(4,
      rb_external_str_new((const char *)data, length),
      INT2FIX(x), INT2FIX(y),
      INT2FIX(components[!components[0]]));
    stbi_image_free(data);
  }

  if (!NIL_P(sr_image_data) && rb_block_given_p()) {
    return rb_yield_splat(sr_image_data);
  } else {
    return sr_image_data;
  }
}
Example #5
0
	SharedValue KRubyObject::Get(const char *name)
	{
		std::string iv_name = std::string("@") + name;
		ID iv_ID = rb_intern(iv_name.c_str());
		ID get_ID = rb_intern(name);
		ID mm_ID = rb_intern("method_missing");

		VALUE ruby_value = Qnil;
		if (rb_obj_respond_to(object, get_ID, Qtrue) == Qtrue)
		{
			ruby_value = rb_funcall(object, rb_intern("method"), 1, ID2SYM(get_ID));
		}
		else if (rb_ivar_defined(object, iv_ID))
		{
			ruby_value = rb_ivar_get(object, iv_ID);
		}
		else if (rb_obj_respond_to(object, mm_ID, Qtrue) == Qtrue)
		{
			// If this object has a method_missing, call that and return the result,
			int error;
			VALUE rargs = rb_ary_new();
			rb_ary_push(rargs, object);
			rb_ary_push(rargs, ID2SYM(get_ID));
			ruby_value = rb_protect(kobj_do_method_missing_call, rargs, &error);

			// protect against NoMethodErrors which we don't want to propogate
			// back through Kroll, but other exceptions should be thrown.
			VALUE exception = rb_gv_get("$!");
			if (rb_obj_is_kind_of(exception, rb_eNoMethodError) == Qtrue
				|| rb_obj_is_kind_of(exception,rb_eNameError) == Qtrue)
			{
				return Value::Undefined;
			}
			else
			{
				SharedValue exceptionValue = RubyUtils::ToKrollValue(exception);
				ValueException e = ValueException(exceptionValue);
				throw e;
			}
		}

		return RubyUtils::ToKrollValue(ruby_value);
	}
Example #6
0
static VALUE
obj_respond_to(VALUE obj, SEL sel, int argc, VALUE *argv)
{
    VALUE mid, priv;
    ID id;

    rb_scan_args(argc, argv, "11", &mid, &priv);
    id = rb_to_id(mid);
    return rb_obj_respond_to(obj, id, RTEST(priv)) ? Qtrue : Qfalse;
    return Qfalse;
}
Example #7
0
static void 
rb_w_event_data(struct w_hook_desc *w,uint8_t direction, char **data, size_t *len)
{
   VALUE hooks;
   VALUE hk;
   int count;
   VALUE ret;

   if((!data) || (!*data) || (!len)) {
      w_log_printf("RubyHook: Invalid parameters to data event");
      return;
   }

   hk = rb_const_get(rb_mWireplay, rb_intern("Hooks"));
   hooks = rb_funcall(hk, rb_intern("hooks"), 0);

   count = RARRAY(hooks)->len;

   while(count > 0) {
      VALUE obj = rb_ary_entry(hooks, count - 1);
      
      if(rb_obj_respond_to(obj, rb_intern("on_data"), 0)) {
         ret = rb_funcall(obj, rb_intern("on_data"), 3,
                  w_hook_desc_to_rb_struct(w),
                  INT2FIX(direction),
                  rb_str_new(*data, *len));
         /*
          * If ret is a string, then the plugin has modified the data part
          * we must free/alloc/copy data again and change len accordingly
          */
         if(TYPE(ret) == T_STRING) {
            /* TODO: update data and len */
            long rlen = RSTRING(ret)->len;
            
            free(*data);
            *data = malloc(rlen + 1);
            assert(*data != NULL);

            memcpy(*data, StringValuePtr(ret), rlen);
            *len = rlen;
         }
      }
      
      count--;
   }

   return;
}
Example #8
0
static cairo_status_t
cr_user_font_face_unicode_to_glyph_func (cairo_scaled_font_t *scaled_font,
                                         unsigned long unicode,
                                         unsigned long *glyph_index)
{
  cairo_status_t status = CAIRO_STATUS_SUCCESS;
  cairo_font_face_t *face;
  VALUE self;
  VALUE receiver = Qnil;
  ID id_method_name = cr_id_call;

  face = cairo_scaled_font_get_font_face (scaled_font);
  self = (VALUE)cairo_font_face_get_user_data (face, &ruby_object_key);
  receiver = rb_ivar_get (self, cr_id_unicode_to_glyph);
  if (NIL_P (receiver) &&
      rb_obj_respond_to (self, cr_id_unicode_to_glyph, Qtrue))
    {
      receiver = self;
      id_method_name = cr_id_unicode_to_glyph;
    }

  if (NIL_P (receiver))
    {
      *glyph_index = unicode;
    }
  else
    {
      cr_user_font_face_invoke_data_t data;
      VALUE argv[2];

      argv[0] = CRSCALEDFONT2RVAL (scaled_font);
      argv[1] = ULONG2NUM (unicode);

      data.receiver = receiver;
      data.method = id_method_name;
      data.argc = 2;
      data.argv = argv;
      data.status = &status;
      data.after_hook = cr_user_font_face_unicode_to_glyph_func_after;
      data.after_hook_data = glyph_index;

      rb_cairo__invoke_callback (cr_user_font_face_invoke_func, (VALUE)&data);
    }

  return status;
}
Example #9
0
static cairo_status_t
cr_user_font_face_render_glyph_func (cairo_scaled_font_t *scaled_font,
                                     unsigned long glyph,
                                     cairo_t *cr,
                                     cairo_text_extents_t *extents)
{
  cairo_status_t status = CAIRO_STATUS_SUCCESS;
  cairo_font_face_t *face;
  VALUE self;
  VALUE receiver = Qnil;
  ID id_method_name = cr_id_call;

  face = cairo_scaled_font_get_font_face (scaled_font);
  self = (VALUE)cairo_font_face_get_user_data (face, &ruby_object_key);
  receiver = rb_ivar_get (self, cr_id_render_glyph);
  if (NIL_P (receiver) && rb_obj_respond_to (self, cr_id_render_glyph, Qtrue))
    {
      receiver = self;
      id_method_name = cr_id_render_glyph;
    }

  if (!NIL_P (receiver))
    {
      cr_user_font_face_invoke_data_t data;
      VALUE argv[4];

      argv[0] = CRSCALEDFONT2RVAL (scaled_font);
      argv[1] = ULONG2NUM (glyph);
      argv[2] = CRCONTEXT2RVAL (cr);
      argv[3] = CRTEXTEXTENTS2RVAL (extents);

      data.receiver = receiver;
      data.method = id_method_name;
      data.argc = 4;
      data.argv = argv;
      data.status = &status;
      data.after_hook = cr_user_font_face_render_glyph_func_after;
      data.after_hook_data = extents;

      rb_cairo__invoke_callback (cr_user_font_face_invoke_func, (VALUE)&data);
    }

  return status;
}
Example #10
0
/*
 * Here begins the Ruby hooker
 */
static void rb_w_event_start(struct w_hook_desc *w)
{
   VALUE hooks;
   VALUE hk;
   int count;

   hk = rb_const_get(rb_mWireplay, rb_intern("Hooks"));
   hooks = rb_funcall(hk, rb_intern("hooks"), 0);

   count = RARRAY(hooks)->len;

   while(count > 0) {
      VALUE obj = rb_ary_entry(hooks, count - 1);
      
      if(rb_obj_respond_to(obj, rb_intern("on_start"), 0))
         rb_funcall(obj, rb_intern("on_start"), 1, w_hook_desc_to_rb_struct(w));
      
      count--;
   }
}
Example #11
0
	void KRubyObject::Set(const char *name, SharedValue value)
	{
		VALUE ruby_value = RubyUtils::ToRubyValue(value);
		std::string setter_name = std::string(name) + "=";
		ID set_ID = rb_intern(setter_name.c_str());

		int error = 0;
		if (rb_obj_respond_to(object, set_ID, Qtrue) == Qtrue)
		{
			rb_funcall(object, set_ID, 1, ruby_value);
		}
		else
		{
			// First try calling method missing
			VALUE rargs = rb_ary_new();
			rb_ary_push(rargs, object);
			rb_ary_push(rargs, rb_str_new2(name));
			rb_ary_push(rargs, ruby_value);
			rb_protect(kobj_do_method_missing_call, rargs, &error);

			// If the exception wasn't a normal NoMethod exception actually
			// throw an exception here, because something went wrong.
			VALUE exception = rb_gv_get("$!");
			if (rb_obj_is_kind_of(exception, rb_eNoMethodError) != Qtrue
				&& rb_obj_is_kind_of(exception,rb_eNameError) == Qtrue)
			{
				SharedValue exceptionValue = RubyUtils::ToKrollValue(exception);
				ValueException e = ValueException(exceptionValue);
				throw e;
			}
		}

		// Last resort: set an instance variable
		if (error != 0)
		{
			std::string iv_name = std::string("@") + name;
			rb_iv_set(object, iv_name.c_str(), ruby_value);
		}
	}
Example #12
0
int
rb_respond_to(VALUE obj, ID id)
{
    return rb_obj_respond_to(obj, id, Qfalse);
}
Example #13
0
int
rb_respond_to(VALUE obj, ID id)
{
    return rb_obj_respond_to(obj, id, FALSE);
}
Example #14
0
static cairo_status_t
cr_user_font_face_text_to_glyphs_func (cairo_scaled_font_t *scaled_font,
                                       const char *utf8, int utf8_len,
                                       cairo_glyph_t **glyphs, int *num_glyphs,
                                       cairo_text_cluster_t **clusters,
                                       int *num_clusters,
                                       cairo_text_cluster_flags_t *cluster_flags)
{
  cairo_status_t status = CAIRO_INT_STATUS_UNSUPPORTED;
  cairo_font_face_t *face;
  VALUE self;
  VALUE receiver = Qnil;
  ID id_method_name = cr_id_call;

  face = cairo_scaled_font_get_font_face (scaled_font);
  self = (VALUE)cairo_font_face_get_user_data (face, &ruby_object_key);
  receiver = rb_ivar_get (self, cr_id_text_to_glyphs);
  if (NIL_P (receiver) && rb_obj_respond_to (self, cr_id_text_to_glyphs, Qtrue))
    {
      receiver = self;
      id_method_name = cr_id_text_to_glyphs;
    }

  if (NIL_P (receiver))
    {
      if (num_glyphs)
        *num_glyphs = -1;
    }
  else
    {
      cr_user_font_face_invoke_data_t data;
      cr_text_to_glyphs_after_hook_data_t after_hook_data;
      VALUE text_to_glyphs_data;
      VALUE argv[3];

      argv[0] = CRSCALEDFONT2RVAL (scaled_font);
      argv[1] = rb_str_new (utf8, utf8_len);
      text_to_glyphs_data = rb_funcall (rb_cCairo_UserFontFace_TextToGlyphsData,
                                        cr_id_new,
                                        3,
                                        CBOOL2RVAL (glyphs != NULL),
                                        CBOOL2RVAL (clusters != NULL),
                                        CBOOL2RVAL (cluster_flags != NULL));
      argv[2] = text_to_glyphs_data;

      data.receiver = receiver;
      data.method = id_method_name;
      data.argc = 3;
      data.argv = argv;
      data.status = &status;
      data.after_hook = cr_user_font_face_text_to_glyphs_func_after;
      data.after_hook_data = &after_hook_data;

      after_hook_data.text_to_glyphs_data = text_to_glyphs_data;
      after_hook_data.glyphs = glyphs;
      after_hook_data.num_glyphs = num_glyphs;
      after_hook_data.clusters = clusters;
      after_hook_data.num_clusters = num_clusters;
      after_hook_data.cluster_flags = cluster_flags;

      rb_cairo__invoke_callback (cr_user_font_face_invoke_func, (VALUE)&data);
    }

  return status;
}