--- deliantra/Deliantra-Client/Client.xs 2006/04/23 04:47:02 1.60 +++ deliantra/Deliantra-Client/Client.xs 2006/04/23 21:47:30 1.61 @@ -42,19 +42,23 @@ #define MAP_EXTEND_X 32 #define MAP_EXTEND_Y 512 -#define MIN_FONT_HEIGHT 8 * PANGO_SCALE +#define MIN_FONT_HEIGHT 8 typedef Mix_Chunk *CFClient__MixChunk; typedef Mix_Music *CFClient__MixMusic; -static PangoContext *context; -static PangoFontMap *fontmap; +typedef PangoFontDescription *CFClient__Font; typedef struct cf_layout { PangoLayout *pl; int base_height; + CFClient__Font font; } *CFClient__Layout; +static CFClient__Font default_font; +static PangoContext *context; +static PangoFontMap *fontmap; + static void substitute_func (FcPattern *pattern, gpointer data) { @@ -63,32 +67,22 @@ } static void -layout_update (CFClient__Layout self) +layout_update_font (CFClient__Layout self) { /* use a random scale factor to account for unknown descenders, 0.8 works * reasonably well with bitstream vera */ - PangoFontDescription *font = (PangoFontDescription *) - pango_layout_get_font_description (self->pl); + PangoFontDescription *font = self->font ? self->font : default_font; - int height = self->base_height * (PANGO_SCALE * 8 / 10); + pango_font_description_set_absolute_size (font, + MAX (MIN_FONT_HEIGHT, self->base_height) * (PANGO_SCALE * 8 / 10)); - if (height < MIN_FONT_HEIGHT) - height = MIN_FONT_HEIGHT; - - if (pango_font_description_get_size (font) != height) - { - font = pango_font_description_copy (font); - pango_font_description_set_absolute_size (font, height); - pango_layout_set_font_description (self->pl, font); - } + pango_layout_set_font_description (self->pl, font); } static void layout_get_pixel_size (CFClient__Layout self, int *w, int *h) { - layout_update (self); - pango_layout_get_pixel_size (self->pl, w, h); *w = (*w + 3) & ~3; @@ -523,18 +517,6 @@ FcConfigAppFontAddFile (0, (const FcChar8 *)file); /* no idea wether this is required */ void -set_font (char *file) - CODE: -{ - int count; - FcPattern *pattern = FcFreeTypeQuery ((const FcChar8 *)file, 0, 0, &count); - PangoFontDescription *font = pango_fc_font_description_from_pattern (pattern, 0); - FcPatternDestroy (pattern); - pango_font_description_set_absolute_size (font, MIN_FONT_HEIGHT); - pango_context_set_font_description (context, font); -} - -void load_image_inline (SV *image_) ALIAS: load_image_file = 1 @@ -633,18 +615,41 @@ #endif exit (1); +MODULE = CFClient PACKAGE = CFClient::Font + +CFClient::Font +new_from_file (SV *class, char *path) + CODE: +{ + int count; + FcPattern *pattern = FcFreeTypeQuery ((const FcChar8 *)path, 0, 0, &count); + RETVAL = pango_fc_font_description_from_pattern (pattern, 0); + FcPatternDestroy (pattern); +} + OUTPUT: + RETVAL + +void +DESTROY (CFClient::Font self) + CODE: + pango_font_description_free (self); + +void +make_default (CFClient::Font self) + CODE: + default_font = self; + MODULE = CFClient PACKAGE = CFClient::Layout CFClient::Layout -new (SV *class, int base_height = 10) +new (SV *class, int base_height = MIN_FONT_HEIGHT) CODE: New (0, RETVAL, 1, struct cf_layout); - RETVAL->base_height = base_height; RETVAL->pl = pango_layout_new (context); + RETVAL->base_height = base_height; + RETVAL->font = 0; pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR); - pango_layout_set_font_description (RETVAL->pl, - pango_font_description_copy ( - pango_context_get_font_description (context))); + pango_layout_set_font_description (RETVAL->pl, default_font); OUTPUT: RETVAL @@ -683,9 +688,22 @@ RETVAL void +set_font (CFClient::Layout self, CFClient::Font font = 0) + CODE: + if (self->font != font) + { + self->font = font; + layout_update_font (self); + } + +void set_height (CFClient::Layout self, int base_height) CODE: - self->base_height = base_height; + if (self->base_height != base_height) + { + self->base_height = base_height; + layout_update_font (self); + } void set_width (CFClient::Layout self, int max_width = -1) @@ -698,7 +716,6 @@ { int w, h; - layout_update (self); layout_get_pixel_size (self, &w, &h); EXTEND (SP, 2); @@ -711,10 +728,7 @@ CODE: { int index, trailing; - - layout_update (self); pango_layout_xy_to_index (self->pl, x * PANGO_SCALE, y * PANGO_SCALE, &index, &trailing); - RETVAL = index; } OUTPUT: @@ -725,7 +739,6 @@ PPCODE: { PangoRectangle strong_pos; - layout_update (self); pango_layout_get_cursor_pos (self->pl, index, &strong_pos, 0); EXTEND (SP, 3); @@ -742,7 +755,6 @@ int w, h; FT_Bitmap bitmap; - layout_update (self); layout_get_pixel_size (self, &w, &h); retval = newSV (w * h);