Browse Source

* Add Cucul::Font

tags/v0.99.beta14
Pascal Terjan pterjan 17 years ago
parent
commit
f5b3266b6d
9 changed files with 181 additions and 7 deletions
  1. +3
    -1
      ruby/Makefile.am
  2. +6
    -0
      ruby/common.h
  3. +30
    -3
      ruby/cucul-canvas.c
  4. +3
    -0
      ruby/cucul-canvas.h
  5. +99
    -0
      ruby/cucul-font.c
  6. +9
    -0
      ruby/cucul-font.h
  7. +3
    -3
      ruby/cucul.c
  8. +6
    -0
      ruby/t/tc_canvas.rb
  9. +22
    -0
      ruby/t/tc_font.rb

+ 3
- 1
ruby/Makefile.am View File

@@ -7,11 +7,13 @@ TESTS = test
endif

cucul_la_CPPFLAGS = -I$(top_srcdir)/cucul -I$(RUBY_ARCHDIR)
cucul_la_SOURCES = cucul.c cucul-canvas.c
cucul_la_SOURCES = cucul.c cucul-canvas.c cucul-font.c
cucul_la_LDFLAGS = -module -avoid-version -shared -L$(RUBY_LIBDIR) -l$(RUBY_SO_NAME)
cucul_la_LIBADD = ../cucul/libcucul.la

EXTRA_DIST = cucul-canvas.h \
cucul-canvas.h \
common.h \
test.rb \
t/tc_frame.rb \
README


+ 6
- 0
ruby/common.h View File

@@ -0,0 +1,6 @@
#ifndef __COMMON_H__
#define __COMMON_H__

#define _SELF (DATA_PTR(self))

#endif

+ 30
- 3
ruby/cucul-canvas.c View File

@@ -12,9 +12,10 @@
#include <ruby.h>
#include <cucul.h>
#include <errno.h>
#include "cucul-canvas.h"
#include "cucul-font.h"
#include "common.h"

#define _SELF (DATA_PTR(self))
VALUE cCanvas;

#define simple_func(x) \
static VALUE x (VALUE self) \
@@ -201,10 +202,12 @@ static VALUE blit(int argc, VALUE* argv, VALUE self) {

Check_Type(x, T_FIXNUM);
Check_Type(y, T_FIXNUM);
//FIXME rather check that class is cCanvas
Check_Type(src, TYPE(self));
Data_Get_Struct(src, cucul_canvas_t, csrc);
if(!NIL_P(mask))
{
//FIXME rather check that class is cCanvas
Check_Type(mask, TYPE(self));
Data_Get_Struct(mask, cucul_canvas_t, cmask);
}
@@ -484,6 +487,29 @@ static VALUE free_frame(VALUE self, VALUE id)

/****/

static VALUE render_canvas(VALUE self, VALUE font, VALUE width, VALUE height, VALUE pitch)
{
void *buf;
cucul_font_t *f;
VALUE b;

//FIXME rather check that class is cFont
Check_Type(font, TYPE(self));

buf = malloc(width*height*4);
if(buf == NULL)
{
rb_raise(rb_eNoMemError, "Out of memory");
}

f = DATA_PTR(font);
cucul_render_canvas(_SELF, f, buf, NUM2UINT(width), NUM2UINT(height), NUM2UINT(pitch));

b = rb_str_new(buf, width*height*4);
free(buf);
return b;
}

static VALUE import_memory(VALUE self, VALUE data, VALUE format)
{
long int bytes;
@@ -565,7 +591,7 @@ static VALUE import_list(void)

void Init_cucul_canvas(VALUE mCucul)
{
VALUE cCanvas = rb_define_class_under(mCucul, "Canvas", rb_cObject);
cCanvas = rb_define_class_under(mCucul, "Canvas", rb_cObject);
rb_define_alloc_func(cCanvas, canvas_alloc);

rb_define_method(cCanvas, "initialize", canvas_initialize, 2);
@@ -634,6 +660,7 @@ void Init_cucul_canvas(VALUE mCucul)
rb_define_method(cCanvas, "create_frame", create_frame, 1);
rb_define_method(cCanvas, "free_frame", free_frame, 1);

rb_define_method(cCanvas, "render", render_canvas, 4);
rb_define_method(cCanvas, "import_memory", import_memory, 2);
rb_define_method(cCanvas, "import_file", import_file, 2);
rb_define_method(cCanvas, "export_memory", export_memory, 1);


+ 3
- 0
ruby/cucul-canvas.h View File

@@ -1,6 +1,9 @@
#ifndef __CUCUL_CANVAS_H__
#define __CUCUL_CANVAS_H__

#include <ruby.h>

extern VALUE cCanvas;
extern void Init_cucul_canvas(VALUE);

#endif

+ 99
- 0
ruby/cucul-font.c View File

@@ -0,0 +1,99 @@
/*
* libcucul Ruby bindings
* Copyright (c) 2007 Pascal Terjan <pterjan@linuxfr.org>
*
* This library is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*/

#include <ruby.h>
#include <cucul.h>
#include <errno.h>
#include "common.h"

VALUE cFont;

void font_free(void *font)
{
cucul_free_font((cucul_font_t *)font);
}

static VALUE font_alloc(VALUE klass)
{
VALUE obj;
obj = Data_Wrap_Struct(klass, 0, font_free, NULL);
return obj;
}

static VALUE font_initialize(VALUE self, VALUE name)
{
cucul_font_t *font;

font = cucul_load_font(StringValuePtr(name), 0);
if(font == NULL)
{
rb_raise(rb_eRuntimeError, strerror(errno));
}
_SELF = font;
return self;
}

static VALUE font_list(void)
{
VALUE ary;
char const* const* list;
list = cucul_get_font_list();
ary = rb_ary_new();
while (*list != NULL)
{
rb_ary_push(ary, rb_str_new2(*list));
list++;
}

return ary;
}

static VALUE get_font_width(VALUE self)
{
return UINT2NUM(cucul_get_font_width(_SELF));
}

static VALUE get_font_height(VALUE self)
{
return UINT2NUM(cucul_get_font_height(_SELF));
}

static VALUE get_font_blocks(VALUE self)
{
VALUE ary;
unsigned long int const *list;
list = cucul_get_font_blocks(_SELF);
ary = rb_ary_new();
while (*list != 0L)
{
rb_ary_push(ary, ULONG2NUM(*list));
list++;
}

return ary;
}

void Init_cucul_font(VALUE mCucul)
{
cFont = rb_define_class_under(mCucul, "Font", rb_cObject);
rb_define_alloc_func(cFont, font_alloc);

rb_define_method(cFont, "initialize", font_initialize, 1);
rb_define_method(cFont, "width", get_font_width, 0);
rb_define_method(cFont, "height", get_font_height, 0);
rb_define_method(cFont, "blocks", get_font_blocks, 0);
rb_define_singleton_method(cFont, "list", font_list, 0);
}


+ 9
- 0
ruby/cucul-font.h View File

@@ -0,0 +1,9 @@
#ifndef __CUCUL_FONT_H__
#define __CUCUL_FONT_H__

#include <ruby.h>

extern VALUE cFont;
extern void Init_cucul_font(VALUE);

#endif

+ 3
- 3
ruby/cucul.c View File

@@ -13,12 +13,11 @@
#include <cucul.h>

#include "cucul-canvas.h"

static VALUE mCucul;
#include "cucul-font.h"

void Init_cucul()
{
mCucul = rb_define_module("Cucul");
VALUE mCucul = rb_define_module("Cucul");

rb_define_const(mCucul, "BLACK", INT2FIX(CUCUL_BLACK));
rb_define_const(mCucul, "BLUE", INT2FIX(CUCUL_BLUE));
@@ -45,4 +44,5 @@ void Init_cucul()
rb_define_const(mCucul, "BLINK", INT2FIX(CUCUL_BLINK));

Init_cucul_canvas(mCucul);
Init_cucul_font(mCucul);
}

+ 6
- 0
ruby/t/tc_canvas.rb View File

@@ -44,4 +44,10 @@ class TC_Canvas < Test::Unit::TestCase
@c.put_char(1, 1, 42)
assert_equal(42, @c.get_char(1,1))
end
def test_render
c = Cucul::Canvas.new(4,4)
c.put_str(0,0,"plop")
f = Cucul::Font.new(Cucul::Font.list[0])
assert_not_nil(c.render(f, c.width*f.width, c.height*f.height, c.width*f.width*4))
end
end

+ 22
- 0
ruby/t/tc_font.rb View File

@@ -0,0 +1,22 @@
require 'test/unit'
require 'cucul'

class TC_Canvas < Test::Unit::TestCase
def test_list
assert_not_nil(Cucul::Font.list)
end
def test_load
Cucul::Font.list.each{|f|
font = Cucul::Font.new(f)
assert_not_nil(font)
assert_not_nil(font.width)
assert_not_nil(font.height)
assert_not_nil(font.blocks)
}
end
def test_fail_load
assert_raise(RuntimeError) {
Cucul::Font.new("This font should not exist")
}
end
end

Loading…
Cancel
Save