--- deliantra/Deliantra-Client/Client.xs 2006/06/18 19:13:19 1.120 +++ deliantra/Deliantra-Client/Client.xs 2006/07/12 17:21:01 1.130 @@ -20,12 +20,12 @@ #include #include +#define PANGO_ENABLE_BACKEND +#define G_DISABLE_CAST_CHECKS + #include #include -#include -#include -#include #ifndef _WIN32 # include @@ -44,6 +44,8 @@ #include "glext.h" +#define OBJ_STR "\xef\xbf\xbc" /* U+FFFC, objetc replacement character */ + #define FOW_DARKNESS 32 #define MAP_EXTEND_X 32 @@ -51,6 +53,12 @@ #define MIN_FONT_HEIGHT 10 +#if 0 +# define PARACHUTE SDL_INIT_NOPARACHUTE +#else +# define PARACHUTE 0 +#endif + static struct { #define GL_FUNC(ptr,name) ptr name; @@ -68,22 +76,45 @@ glBlendFunc (sa, da); } +#include "texcache.c" + +#include "pango-font.c" +#include "pango-fontmap.c" +#include "pango-render.c" + typedef Mix_Chunk *CFClient__MixChunk; typedef Mix_Music *CFClient__MixMusic; typedef PangoFontDescription *CFClient__Font; +static int +shape_attr_p (PangoLayoutRun *run) +{ + GSList *attrs = run->item->analysis.extra_attrs; + + while (attrs) + { + PangoAttribute *attr = attrs->data; + + if (attr->klass->type == PANGO_ATTR_SHAPE) + return 1; + + attrs = attrs->next; + } + + return 0; +} + typedef struct cf_layout { - PangoLayout *pl; // either derived from a cairo or ft2 context - int rgba; // wether we use rgba (cairo) or grayscale (ft2) + PangoLayout *pl; float r, g, b, a; // default color for rgba mode int base_height; CFClient__Font font; } *CFClient__Layout; static CFClient__Font default_font; -static PangoContext *ft2_context, *cairo_context; -static PangoFontMap *ft2_fontmap, *cairo_fontmap; +static PangoContext *opengl_context; +static PangoFontMap *opengl_fontmap; static void substitute_func (FcPattern *pattern, gpointer data) @@ -92,11 +123,7 @@ #ifdef FC_HINT_STYLE FcPatternAddBool (pattern, FC_HINT_STYLE, FC_HINT_FULL); #endif -#ifdef _WIN32 - FcPatternAddBool (pattern, FC_AUTOHINT, 1); -#else FcPatternAddBool (pattern, FC_AUTOHINT, 0); -#endif } static void @@ -438,6 +465,13 @@ newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); } +int +in_destruct () + CODE: + RETVAL = PL_main_cv == Nullcv; + OUTPUT: + RETVAL + NV floor (NV x) NV ceil (NV x) @@ -445,32 +479,14 @@ void pango_init () CODE: - // delayed, so it can pick up new fonts added by AddFontResourceEx { - { - ft2_fontmap = pango_ft2_font_map_new (); - pango_ft2_font_map_set_default_substitute ((PangoFT2FontMap *)ft2_fontmap, substitute_func, 0, 0); - ft2_context = pango_ft2_font_map_create_context ((PangoFT2FontMap *)ft2_fontmap); - } - { - cairo_font_options_t *fopt = cairo_font_options_create (); - cairo_fontmap = pango_cairo_font_map_get_default (); - cairo_context = pango_cairo_font_map_create_context ((PangoCairoFontMap *)cairo_fontmap); -#ifdef _WIN32 - // cairo looks like shit eaten twice on windows - cairo_font_options_set_antialias (fopt, CAIRO_ANTIALIAS_NONE); -#else - cairo_font_options_set_antialias (fopt, CAIRO_ANTIALIAS_GRAY); -#endif - cairo_font_options_set_hint_style (fopt, CAIRO_HINT_STYLE_FULL); - cairo_font_options_set_hint_metrics (fopt, CAIRO_HINT_METRICS_ON); - pango_cairo_context_set_font_options (cairo_context, fopt); - cairo_font_options_destroy (fopt); - } + opengl_fontmap = pango_opengl_font_map_new (); + pango_opengl_font_map_set_default_substitute ((PangoOpenGLFontMap *)opengl_fontmap, substitute_func, 0, 0); + opengl_context = pango_opengl_font_map_create_context ((PangoOpenGLFontMap *)opengl_fontmap); } int -SDL_Init (U32 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO) +SDL_Init (U32 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | PARACHUTE) void SDL_Quit () @@ -617,11 +633,7 @@ void add_font (char *file) CODE: - FcConfigAppFontAddFile (0, (const FcChar8 *)file); /* no idea wether this is required */ -#ifdef _WIN32 - // cairo... sigh... requires win2000 - AddFontResourceEx (file, FR_PRIVATE, 0); -#endif + FcConfigAppFontAddFile (0, (const FcChar8 *)file); void load_image_inline (SV *image_) @@ -673,15 +685,15 @@ assert (surface2->pitch == surface2->w * 4); - EXTEND (SP, 5); + SDL_LockSurface (surface2); + EXTEND (SP, 6); PUSHs (sv_2mortal (newSViv (surface2->w))); PUSHs (sv_2mortal (newSViv (surface2->h))); - SDL_LockSurface (surface2); PUSHs (sv_2mortal (newSVpvn (surface2->pixels, surface2->h * surface2->pitch))); - SDL_UnlockSurface (surface2); PUSHs (sv_2mortal (newSViv (surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA) ? GL_RGBA : GL_RGB))); PUSHs (sv_2mortal (newSViv (GL_RGBA))); PUSHs (sv_2mortal (newSViv (GL_UNSIGNED_BYTE))); + SDL_UnlockSurface (surface2); SDL_FreeSurface (surface); SDL_FreeSurface (surface2); @@ -760,13 +772,17 @@ MODULE = CFClient PACKAGE = CFClient::Layout +void +reset_glyph_cache () + CODE: + tc_clear (); + CFClient::Layout -new (SV *class, int rgba = 0) +new (SV *class) CODE: New (0, RETVAL, 1, struct cf_layout); - RETVAL->pl = pango_layout_new (rgba ? cairo_context : ft2_context); - RETVAL->rgba = rgba; + RETVAL->pl = pango_layout_new (opengl_context); RETVAL->r = 1.; RETVAL->g = 1.; RETVAL->b = 1.; @@ -785,13 +801,6 @@ g_object_unref (self->pl); Safefree (self); -int -is_rgba (CFClient::Layout self) - CODE: - RETVAL = self->rgba; - OUTPUT: - RETVAL - void set_text (CFClient::Layout self, SV *text_) CODE: @@ -812,6 +821,91 @@ pango_layout_set_markup (self->pl, text, textlen); } +void +set_shapes (CFClient::Layout self, ...) + CODE: +{ + PangoAttrList *attrs = 0; + const char *text = pango_layout_get_text (self->pl); + const char *pos = text; + int arg = 4; + + while (arg < items && (pos = strstr (pos, OBJ_STR))) + { + PangoRectangle inkrect, rect; + PangoAttribute *attr; + + int x = SvIV (ST (arg - 3)); + int y = SvIV (ST (arg - 2)); + int w = SvIV (ST (arg - 1)); + int h = SvIV (ST (arg )); + + inkrect.x = 0; + inkrect.y = 0; + inkrect.width = 0; + inkrect.height = 0; + + rect.x = x * PANGO_SCALE; + rect.y = y * PANGO_SCALE; + rect.width = w * PANGO_SCALE; + rect.height = h * PANGO_SCALE; + + if (!attrs) + attrs = pango_layout_get_attributes (self->pl); + + attr = pango_attr_shape_new (&inkrect, &rect); + attr->start_index = pos - text; + attr->end_index = attr->start_index + sizeof (OBJ_STR) - 1; + pango_attr_list_insert (attrs, attr); + + arg += 4; + pos += sizeof (OBJ_STR) - 1; + } + + if (attrs) + pango_layout_set_attributes (self->pl, attrs); +} + +void +get_shapes (CFClient::Layout self) + PPCODE: +{ + PangoLayoutIter *iter = pango_layout_get_iter (self->pl); + + do + { + PangoLayoutRun *run = pango_layout_iter_get_run (iter); + + if (run && shape_attr_p (run)) + { + PangoRectangle extents; + pango_layout_iter_get_run_extents (iter, 0, &extents); + + EXTEND (SP, 2); + PUSHs (sv_2mortal (newSViv (PANGO_PIXELS (extents.x)))); + PUSHs (sv_2mortal (newSViv (PANGO_PIXELS (extents.y)))); + } + } + while (pango_layout_iter_next_run (iter)); + + pango_layout_iter_free (iter); +} + +int +has_wrapped (CFClient::Layout self) + CODE: +{ + int lines = 1; + const char *text = pango_layout_get_text (self->pl); + + while (*text) + lines += *text++ == '\n'; + + RETVAL = lines < pango_layout_get_line_count (self->pl); +} + OUTPUT: + RETVAL + SV * get_text (CFClient::Layout self) CODE: @@ -890,6 +984,18 @@ } int +descent (CFClient::Layout self) + CODE: +{ + PangoRectangle rect; + PangoLayoutLine *line = pango_layout_get_line (self->pl, 0); + pango_layout_line_get_pixel_extents (line, 0, &rect); + RETVAL = PANGO_DESCENT (rect); +} + OUTPUT: + RETVAL + +int xy_to_index (CFClient::Layout self, int x, int y) CODE: { @@ -914,95 +1020,13 @@ } void -render (CFClient::Layout self) +render (CFClient::Layout self, float x, float y) PPCODE: -{ - SV *retval; - int w, h; - - layout_get_pixel_size (self, &w, &h); - - if (self->rgba) - { - cairo_surface_t *surface; - cairo_t *cairo; - - retval = newSV (w * h * 4); - SvPOK_only (retval); - SvCUR_set (retval, w * h * 4); - - memset (SvPVX (retval), 0, w * h * 4); - - surface = cairo_image_surface_create_for_data ( - (void*)SvPVX (retval), CAIRO_FORMAT_ARGB32, w, h, w * 4); - cairo = cairo_create (surface); - cairo_set_source_rgba (cairo, self->r, self->g, self->b, self->a); - - pango_cairo_show_layout (cairo, self->pl); - - cairo_destroy (cairo); - cairo_surface_destroy (surface); - - // what a mess, and its premultiplied, too :( - { - uint32_t *p = (uint32_t *)SvPVX (retval); - uint32_t *e = p + w * h; - - while (p < e) - { - uint32_t rgba = *p; - rgba = (rgba >> 24) | (rgba << 8); -#if 0 -#ifdef _WIN32 - {//D - uint8_t r = rgba >> 24; - uint8_t g = rgba >> 16; - uint8_t b = rgba >> 8; - uint8_t a = rgba >> 0; - - rgba = (rgba & 0xffffff00) | a; - } -#endif -#endif - rgba = SDL_SwapBE32 (rgba); - *p++ = rgba; - } - } - - EXTEND (SP, 5); - PUSHs (sv_2mortal (newSViv (w))); - PUSHs (sv_2mortal (newSViv (h))); - PUSHs (sv_2mortal (retval)); - PUSHs (sv_2mortal (newSViv (GL_RGBA))); - PUSHs (sv_2mortal (newSViv (GL_RGBA))); - } - else - { - FT_Bitmap bitmap; - - retval = newSV (w * h); - SvPOK_only (retval); - SvCUR_set (retval, w * h); - - bitmap.rows = h; - bitmap.width = w; - bitmap.pitch = w; - bitmap.buffer = (unsigned char*)SvPVX (retval); - bitmap.num_grays = 256; - bitmap.pixel_mode = FT_PIXEL_MODE_GRAY; - - memset (bitmap.buffer, 0, w * h); - - pango_ft2_render_layout (&bitmap, self->pl, 0 * PANGO_SCALE, 0 * PANGO_SCALE); - - EXTEND (SP, 5); - PUSHs (sv_2mortal (newSViv (w))); - PUSHs (sv_2mortal (newSViv (h))); - PUSHs (sv_2mortal (retval)); - PUSHs (sv_2mortal (newSViv (GL_ALPHA))); - PUSHs (sv_2mortal (newSViv (GL_ALPHA))); - } -} + pango_opengl_render_layout_subpixel ( + self->pl, + x * PANGO_SCALE, y * PANGO_SCALE, + self->r, self->g, self->b, self->a + ); MODULE = CFClient PACKAGE = CFClient::Texture @@ -1414,26 +1438,26 @@ CODE: { static float color[16][3] = { - { 0.00, 0.00, 0.00 }, - { 1.00, 1.00, 1.00 }, - { 0.00, 0.00, 0.55 }, - { 1.00, 0.00, 0.00 }, - - { 1.00, 0.54, 0.00 }, - { 0.11, 0.56, 1.00 }, - { 0.93, 0.46, 0.00 }, - { 0.18, 0.54, 0.34 }, - - { 0.56, 0.73, 0.56 }, - { 0.80, 0.80, 0.80 }, - { 0.55, 0.41, 0.13 }, - { 0.99, 0.77, 0.26 }, - - { 0.74, 0.65, 0.41 }, - - { 0.00, 1.00, 1.00 }, - { 1.00, 0.00, 1.00 }, - { 1.00, 1.00, 0.00 }, + { 0.00F, 0.00F, 0.00F }, + { 1.00F, 1.00F, 1.00F }, + { 0.00F, 0.00F, 0.55F }, + { 1.00F, 0.00F, 0.00F }, + + { 1.00F, 0.54F, 0.00F }, + { 0.11F, 0.56F, 1.00F }, + { 0.93F, 0.46F, 0.00F }, + { 0.18F, 0.54F, 0.34F }, + + { 0.56F, 0.73F, 0.56F }, + { 0.80F, 0.80F, 0.80F }, + { 0.55F, 0.41F, 0.13F }, + { 0.99F, 0.77F, 0.26F }, + + { 0.74F, 0.65F, 0.41F }, + + { 0.00F, 1.00F, 1.00F }, + { 1.00F, 0.00F, 1.00F }, + { 1.00F, 1.00F, 0.00F }, }; int x, y; @@ -1892,10 +1916,7 @@ b *= a; } // microsoft visual "c" rounds instead of truncating... - glColor4ub (MIN ((int)(r * 256.f), 255), - MIN ((int)(g * 256.f), 255), - MIN ((int)(b * 256.f), 255), - MIN ((int)(a * 256.f), 255)); + glColor4f (r, g, b, a); void glInterleavedArrays (int format, int stride, char *data)