--- deliantra/Deliantra-Client/Client.xs 2006/04/11 12:27:51 1.13 +++ deliantra/Deliantra-Client/Client.xs 2006/04/11 13:05:11 1.14 @@ -19,6 +19,21 @@ static PangoContext *context; static PangoFontMap *fontmap; +typedef struct cf_layout { + PangoLayout *pl; + int base_height; +} *Crossfire__Client__Layout; + +static void +layout_get_pixel_size (Crossfire__Client__Layout self, int *w, int *h) +{ + pango_layout_get_pixel_size (self->pl, w, h); + + *w = (*w + 3) & ~3; + if (!*w) *w = 1; + if (!*h) *h = 1; +} + MODULE = Crossfire::Client PACKAGE = Crossfire::Client PROTOTYPES: ENABLE @@ -64,28 +79,64 @@ pango_context_set_font_description (context, font); } -MODULE = Crossfire::Client PACKAGE = Crossfire::Client +MODULE = Crossfire::Client PACKAGE = Crossfire::Client::Layout + +Crossfire::Client::Layout +new (SV *class, int base_height = 10) + CODE: + New (0, RETVAL, 1, struct cf_layout); + RETVAL->base_height = base_height; + RETVAL->pl = pango_layout_new (context); + pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR); + OUTPUT: + RETVAL void -font_render (SV *text_, int height = 10) - PPCODE: +DESTROY (Crossfire::Client::Layout self) + CODE: + g_object_unref (self->pl); + Safefree (self); + +void +set_markup (Crossfire::Client::Layout self, SV *text_) + CODE: { STRLEN textlen; char *text = SvPVutf8 (text_, textlen); + + pango_layout_set_markup (self->pl, text, textlen); +} + +void +set_width (Crossfire::Client::Layout self, int max_width = -1) + CODE: + pango_layout_set_width (self->pl, max_width < 0 ? max_width : max_width * PANGO_SCALE); + +void +size (Crossfire::Client::Layout self) + PPCODE: +{ + int w, h; + + PangoFontDescription *font = pango_context_get_font_description (context); + pango_font_description_set_absolute_size (font, self->base_height * PANGO_SCALE); + + layout_get_pixel_size (self, &w, &h); + + EXTEND (SP, 2); + PUSHs (sv_2mortal (newSViv (w))); + PUSHs (sv_2mortal (newSViv (h))); +} + +void +render (Crossfire::Client::Layout self) + PPCODE: +{ SV *retval; int w, h; FT_Bitmap bitmap; - PangoLayout *layout; - PangoFontDescription *font = pango_context_get_font_description (context); - pango_font_description_set_absolute_size (font, height * PANGO_SCALE); - layout = pango_layout_new (context); - pango_layout_set_markup (layout, text, textlen); - pango_layout_get_pixel_size (layout, &w, &h); - - w = (w + 3) & ~3; - if (!w) w = 1; - if (!h) h = 1; + layout_get_pixel_size (self, &w, &h); retval = newSV (w * h); SvPOK_only (retval); @@ -100,8 +151,7 @@ memset (bitmap.buffer, 0, w * h); - pango_ft2_render_layout (&bitmap, layout, 0 * PANGO_SCALE, 0 * PANGO_SCALE); - g_object_unref (layout); + pango_ft2_render_layout (&bitmap, self->pl, 0 * PANGO_SCALE, 0 * PANGO_SCALE); EXTEND (SP, 3); PUSHs (sv_2mortal (newSViv (w)));