… | |
… | |
18 | #include <SDL_endian.h> |
18 | #include <SDL_endian.h> |
19 | #include <SDL_image.h> |
19 | #include <SDL_image.h> |
20 | #include <SDL_mixer.h> |
20 | #include <SDL_mixer.h> |
21 | #include <SDL_opengl.h> |
21 | #include <SDL_opengl.h> |
22 | |
22 | |
|
|
23 | #define PANGO_ENABLE_BACKEND |
|
|
24 | #define G_DISABLE_CAST_CHECKS |
|
|
25 | |
23 | #include <glib/gmacros.h> |
26 | #include <glib/gmacros.h> |
24 | |
27 | |
25 | #include <pango/pango.h> |
28 | #include <pango/pango.h> |
26 | #include <pango/pangofc-fontmap.h> |
|
|
27 | #include <pango/pangoft2.h> |
|
|
28 | #include <pango/pangocairo.h> |
|
|
29 | |
29 | |
30 | #ifndef _WIN32 |
30 | #ifndef _WIN32 |
31 | # include <sys/types.h> |
31 | # include <sys/types.h> |
32 | # include <sys/socket.h> |
32 | # include <sys/socket.h> |
33 | # include <netinet/in.h> |
33 | # include <netinet/in.h> |
… | |
… | |
51 | #define MAP_EXTEND_X 32 |
51 | #define MAP_EXTEND_X 32 |
52 | #define MAP_EXTEND_Y 512 |
52 | #define MAP_EXTEND_Y 512 |
53 | |
53 | |
54 | #define MIN_FONT_HEIGHT 10 |
54 | #define MIN_FONT_HEIGHT 10 |
55 | |
55 | |
|
|
56 | #if 1 |
|
|
57 | # define PARACHUTE SDL_INIT_NOPARACHUTE |
|
|
58 | #else |
|
|
59 | # define PARACHUTE 0 |
|
|
60 | #endif |
|
|
61 | |
56 | static struct |
62 | static struct |
57 | { |
63 | { |
58 | #define GL_FUNC(ptr,name) ptr name; |
64 | #define GL_FUNC(ptr,name) ptr name; |
59 | #include "glfunc.h" |
65 | #include "glfunc.h" |
60 | #undef GL_FUNC |
66 | #undef GL_FUNC |
… | |
… | |
68 | gl.BlendFuncSeparateEXT (sa, da, saa, daa); |
74 | gl.BlendFuncSeparateEXT (sa, da, saa, daa); |
69 | else |
75 | else |
70 | glBlendFunc (sa, da); |
76 | glBlendFunc (sa, da); |
71 | } |
77 | } |
72 | |
78 | |
|
|
79 | #include "texcache.c" |
|
|
80 | |
|
|
81 | #include "pango-font.c" |
|
|
82 | #include "pango-fontmap.c" |
|
|
83 | #include "pango-render.c" |
|
|
84 | |
73 | typedef Mix_Chunk *CFClient__MixChunk; |
85 | typedef Mix_Chunk *CFClient__MixChunk; |
74 | typedef Mix_Music *CFClient__MixMusic; |
86 | typedef Mix_Music *CFClient__MixMusic; |
75 | |
87 | |
76 | typedef PangoFontDescription *CFClient__Font; |
88 | typedef PangoFontDescription *CFClient__Font; |
77 | |
89 | |
… | |
… | |
92 | |
104 | |
93 | return 0; |
105 | return 0; |
94 | } |
106 | } |
95 | |
107 | |
96 | typedef struct cf_layout { |
108 | typedef struct cf_layout { |
97 | PangoLayout *pl; // either derived from a cairo or ft2 context |
109 | PangoLayout *pl; |
98 | int rgba; // wether we use rgba (cairo) or grayscale (ft2) |
|
|
99 | float r, g, b, a; // default color for rgba mode |
110 | float r, g, b, a; // default color for rgba mode |
100 | int base_height; |
111 | int base_height; |
101 | CFClient__Font font; |
112 | CFClient__Font font; |
102 | } *CFClient__Layout; |
113 | } *CFClient__Layout; |
103 | |
114 | |
104 | static CFClient__Font default_font; |
115 | static CFClient__Font default_font; |
105 | static PangoContext *ft2_context, *cairo_context; |
116 | static PangoContext *opengl_context; |
106 | static PangoFontMap *ft2_fontmap, *cairo_fontmap; |
117 | static PangoFontMap *opengl_fontmap; |
107 | |
118 | |
108 | static void |
119 | static void |
109 | substitute_func (FcPattern *pattern, gpointer data) |
120 | substitute_func (FcPattern *pattern, gpointer data) |
110 | { |
121 | { |
111 | FcPatternAddBool (pattern, FC_HINTING, 1); |
122 | FcPatternAddBool (pattern, FC_HINTING, 1); |
… | |
… | |
463 | NV ceil (NV x) |
474 | NV ceil (NV x) |
464 | |
475 | |
465 | void |
476 | void |
466 | pango_init () |
477 | pango_init () |
467 | CODE: |
478 | CODE: |
468 | // delayed, so it can pick up new fonts added by AddFontResourceEx |
|
|
469 | { |
479 | { |
470 | { |
|
|
471 | ft2_fontmap = pango_ft2_font_map_new (); |
480 | opengl_fontmap = pango_opengl_font_map_new (); |
472 | pango_ft2_font_map_set_default_substitute ((PangoFT2FontMap *)ft2_fontmap, substitute_func, 0, 0); |
481 | pango_opengl_font_map_set_default_substitute ((PangoOpenGLFontMap *)opengl_fontmap, substitute_func, 0, 0); |
473 | ft2_context = pango_ft2_font_map_create_context ((PangoFT2FontMap *)ft2_fontmap); |
482 | opengl_context = pango_opengl_font_map_create_context ((PangoOpenGLFontMap *)opengl_fontmap); |
474 | } |
|
|
475 | { |
|
|
476 | cairo_font_options_t *fopt = cairo_font_options_create (); |
|
|
477 | cairo_fontmap = pango_cairo_font_map_get_default (); |
|
|
478 | cairo_context = pango_cairo_font_map_create_context ((PangoCairoFontMap *)cairo_fontmap); |
|
|
479 | #ifdef _WIN32 |
|
|
480 | // cairo looks like shit eaten twice on windows |
|
|
481 | cairo_font_options_set_antialias (fopt, CAIRO_ANTIALIAS_NONE); |
|
|
482 | #else |
|
|
483 | cairo_font_options_set_antialias (fopt, CAIRO_ANTIALIAS_GRAY); |
|
|
484 | #endif |
|
|
485 | cairo_font_options_set_hint_style (fopt, CAIRO_HINT_STYLE_FULL); |
|
|
486 | cairo_font_options_set_hint_metrics (fopt, CAIRO_HINT_METRICS_ON); |
|
|
487 | pango_cairo_context_set_font_options (cairo_context, fopt); |
|
|
488 | cairo_font_options_destroy (fopt); |
|
|
489 | } |
|
|
490 | } |
483 | } |
491 | |
484 | |
492 | int |
485 | int |
493 | SDL_Init (U32 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO) |
486 | SDL_Init (U32 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | PARACHUTE) |
494 | |
487 | |
495 | void |
488 | void |
496 | SDL_Quit () |
489 | SDL_Quit () |
497 | |
490 | |
498 | void |
491 | void |
… | |
… | |
636 | |
629 | |
637 | void |
630 | void |
638 | add_font (char *file) |
631 | add_font (char *file) |
639 | CODE: |
632 | CODE: |
640 | FcConfigAppFontAddFile (0, (const FcChar8 *)file); /* no idea wether this is required */ |
633 | FcConfigAppFontAddFile (0, (const FcChar8 *)file); /* no idea wether this is required */ |
641 | #ifdef _WIN32 |
|
|
642 | // cairo... sigh... requires win2000 |
|
|
643 | AddFontResourceEx (file, FR_PRIVATE, 0); |
|
|
644 | #endif |
|
|
645 | |
634 | |
646 | void |
635 | void |
647 | load_image_inline (SV *image_) |
636 | load_image_inline (SV *image_) |
648 | ALIAS: |
637 | ALIAS: |
649 | load_image_file = 1 |
638 | load_image_file = 1 |
… | |
… | |
778 | CODE: |
767 | CODE: |
779 | default_font = self; |
768 | default_font = self; |
780 | |
769 | |
781 | MODULE = CFClient PACKAGE = CFClient::Layout |
770 | MODULE = CFClient PACKAGE = CFClient::Layout |
782 | |
771 | |
|
|
772 | void |
|
|
773 | clear_font_cache () |
|
|
774 | CODE: |
|
|
775 | pango_fc_font_map_cache_clear ((PangoFcFontMap *)opengl_fontmap); |
|
|
776 | tc_clear (); |
|
|
777 | |
783 | CFClient::Layout |
778 | CFClient::Layout |
784 | new (SV *class, int rgba = 0) |
779 | new (SV *class, int type = 0) |
785 | CODE: |
780 | CODE: |
786 | New (0, RETVAL, 1, struct cf_layout); |
781 | New (0, RETVAL, 1, struct cf_layout); |
787 | |
782 | |
788 | RETVAL->pl = pango_layout_new (rgba ? cairo_context : ft2_context); |
783 | RETVAL->pl = pango_layout_new (opengl_context); |
789 | RETVAL->rgba = rgba; |
|
|
790 | RETVAL->r = 1.; |
784 | RETVAL->r = 1.; |
791 | RETVAL->g = 1.; |
785 | RETVAL->g = 1.; |
792 | RETVAL->b = 1.; |
786 | RETVAL->b = 1.; |
793 | RETVAL->a = 1.; |
787 | RETVAL->a = 1.; |
794 | RETVAL->base_height = MIN_FONT_HEIGHT; |
788 | RETVAL->base_height = MIN_FONT_HEIGHT; |
… | |
… | |
803 | DESTROY (CFClient::Layout self) |
797 | DESTROY (CFClient::Layout self) |
804 | CODE: |
798 | CODE: |
805 | g_object_unref (self->pl); |
799 | g_object_unref (self->pl); |
806 | Safefree (self); |
800 | Safefree (self); |
807 | |
801 | |
808 | int |
|
|
809 | is_rgba (CFClient::Layout self) |
|
|
810 | CODE: |
|
|
811 | RETVAL = self->rgba; |
|
|
812 | OUTPUT: |
|
|
813 | RETVAL |
|
|
814 | |
|
|
815 | void |
802 | void |
816 | set_text (CFClient::Layout self, SV *text_) |
803 | set_text (CFClient::Layout self, SV *text_) |
817 | CODE: |
804 | CODE: |
818 | { |
805 | { |
819 | STRLEN textlen; |
806 | STRLEN textlen; |
… | |
… | |
837 | CODE: |
824 | CODE: |
838 | { |
825 | { |
839 | PangoAttrList *attrs = 0; |
826 | PangoAttrList *attrs = 0; |
840 | const char *text = pango_layout_get_text (self->pl); |
827 | const char *text = pango_layout_get_text (self->pl); |
841 | const char *pos = text; |
828 | const char *pos = text; |
842 | int arg = 2; |
829 | int arg = 4; |
843 | |
830 | |
844 | while (arg < items && (pos = strstr (pos, OBJ_STR))) |
831 | while (arg < items && (pos = strstr (pos, OBJ_STR))) |
845 | { |
832 | { |
846 | PangoRectangle rect; |
833 | PangoRectangle inkrect, rect; |
847 | PangoAttribute *attr; |
834 | PangoAttribute *attr; |
848 | |
835 | |
|
|
836 | int x = SvIV (ST (arg - 3)); |
|
|
837 | int y = SvIV (ST (arg - 2)); |
849 | int w = SvIV (ST (arg - 1)); |
838 | int w = SvIV (ST (arg - 1)); |
850 | int h = SvIV (ST (arg)); |
839 | int h = SvIV (ST (arg )); |
851 | |
840 | |
852 | arg += 2; |
|
|
853 | |
|
|
854 | rect.x = 0; |
841 | inkrect.x = 0; |
|
|
842 | inkrect.y = 0; |
|
|
843 | inkrect.width = 0; |
|
|
844 | inkrect.height = 0; |
|
|
845 | |
|
|
846 | rect.x = x * PANGO_SCALE; |
855 | rect.y = -h * PANGO_SCALE; |
847 | rect.y = y * PANGO_SCALE; |
856 | rect.width = w * PANGO_SCALE; |
848 | rect.width = w * PANGO_SCALE; |
857 | rect.height = h * PANGO_SCALE; |
849 | rect.height = h * PANGO_SCALE; |
858 | |
850 | |
859 | if (!attrs) |
851 | if (!attrs) |
860 | attrs = pango_layout_get_attributes (self->pl); |
852 | attrs = pango_layout_get_attributes (self->pl); |
861 | |
853 | |
862 | attr = pango_attr_shape_new (&rect, &rect); |
854 | attr = pango_attr_shape_new (&inkrect, &rect); |
863 | attr->start_index = pos - text; |
855 | attr->start_index = pos - text; |
864 | attr->end_index = attr->start_index + sizeof (OBJ_STR) - 1; |
856 | attr->end_index = attr->start_index + sizeof (OBJ_STR) - 1; |
865 | pango_attr_list_insert (attrs, attr); |
857 | pango_attr_list_insert (attrs, attr); |
866 | |
858 | |
|
|
859 | arg += 4; |
867 | pos += sizeof (OBJ_STR) - 1; |
860 | pos += sizeof (OBJ_STR) - 1; |
868 | } |
861 | } |
869 | |
862 | |
870 | if (attrs) |
863 | if (attrs) |
871 | pango_layout_set_attributes (self->pl, attrs); |
864 | pango_layout_set_attributes (self->pl, attrs); |
… | |
… | |
986 | PUSHs (sv_2mortal (newSViv (w))); |
979 | PUSHs (sv_2mortal (newSViv (w))); |
987 | PUSHs (sv_2mortal (newSViv (h))); |
980 | PUSHs (sv_2mortal (newSViv (h))); |
988 | } |
981 | } |
989 | |
982 | |
990 | int |
983 | int |
|
|
984 | descent (CFClient::Layout self) |
|
|
985 | CODE: |
|
|
986 | { |
|
|
987 | PangoRectangle rect; |
|
|
988 | PangoLayoutLine *line = pango_layout_get_line (self->pl, 0); |
|
|
989 | pango_layout_line_get_pixel_extents (line, 0, &rect); |
|
|
990 | RETVAL = PANGO_DESCENT (rect); |
|
|
991 | } |
|
|
992 | OUTPUT: |
|
|
993 | RETVAL |
|
|
994 | |
|
|
995 | int |
991 | xy_to_index (CFClient::Layout self, int x, int y) |
996 | xy_to_index (CFClient::Layout self, int x, int y) |
992 | CODE: |
997 | CODE: |
993 | { |
998 | { |
994 | int index, trailing; |
999 | int index, trailing; |
995 | pango_layout_xy_to_index (self->pl, x * PANGO_SCALE, y * PANGO_SCALE, &index, &trailing); |
1000 | pango_layout_xy_to_index (self->pl, x * PANGO_SCALE, y * PANGO_SCALE, &index, &trailing); |
… | |
… | |
1010 | PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE))); |
1015 | PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE))); |
1011 | PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE))); |
1016 | PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE))); |
1012 | } |
1017 | } |
1013 | |
1018 | |
1014 | void |
1019 | void |
1015 | render (CFClient::Layout self) |
1020 | render (CFClient::Layout self, float x, float y) |
1016 | PPCODE: |
1021 | PPCODE: |
1017 | { |
1022 | pango_opengl_render_layout_subpixel ( |
1018 | SV *retval; |
1023 | self->pl, |
1019 | int w, h; |
1024 | x * PANGO_SCALE, y * PANGO_SCALE, |
1020 | |
1025 | self->r, self->g, self->b, self->a |
1021 | layout_get_pixel_size (self, &w, &h); |
|
|
1022 | |
|
|
1023 | if (self->rgba) |
|
|
1024 | { |
1026 | ); |
1025 | cairo_surface_t *surface; |
|
|
1026 | cairo_t *cairo; |
|
|
1027 | |
|
|
1028 | retval = newSV (w * h * 4); |
|
|
1029 | SvPOK_only (retval); |
|
|
1030 | SvCUR_set (retval, w * h * 4); |
|
|
1031 | |
|
|
1032 | memset (SvPVX (retval), 0, w * h * 4); |
|
|
1033 | |
|
|
1034 | surface = cairo_image_surface_create_for_data ( |
|
|
1035 | (void*)SvPVX (retval), CAIRO_FORMAT_ARGB32, w, h, w * 4); |
|
|
1036 | cairo = cairo_create (surface); |
|
|
1037 | cairo_set_source_rgba (cairo, self->r, self->g, self->b, self->a); |
|
|
1038 | |
|
|
1039 | pango_cairo_show_layout (cairo, self->pl); |
|
|
1040 | |
|
|
1041 | cairo_destroy (cairo); |
|
|
1042 | cairo_surface_destroy (surface); |
|
|
1043 | |
|
|
1044 | // what a mess, and its premultiplied, too :( |
|
|
1045 | { |
|
|
1046 | uint32_t *p = (uint32_t *)SvPVX (retval); |
|
|
1047 | uint32_t *e = p + w * h; |
|
|
1048 | |
|
|
1049 | while (p < e) |
|
|
1050 | { |
|
|
1051 | uint32_t rgba = *p; |
|
|
1052 | rgba = (rgba >> 24) | (rgba << 8); |
|
|
1053 | #if 0 |
|
|
1054 | #ifdef _WIN32 |
|
|
1055 | {//D |
|
|
1056 | uint8_t r = rgba >> 24; |
|
|
1057 | uint8_t g = rgba >> 16; |
|
|
1058 | uint8_t b = rgba >> 8; |
|
|
1059 | uint8_t a = rgba >> 0; |
|
|
1060 | |
|
|
1061 | rgba = (rgba & 0xffffff00) | a; |
|
|
1062 | } |
|
|
1063 | #endif |
|
|
1064 | #endif |
|
|
1065 | rgba = SDL_SwapBE32 (rgba); |
|
|
1066 | *p++ = rgba; |
|
|
1067 | } |
|
|
1068 | } |
|
|
1069 | |
|
|
1070 | EXTEND (SP, 5); |
|
|
1071 | PUSHs (sv_2mortal (newSViv (w))); |
|
|
1072 | PUSHs (sv_2mortal (newSViv (h))); |
|
|
1073 | PUSHs (sv_2mortal (retval)); |
|
|
1074 | PUSHs (sv_2mortal (newSViv (GL_RGBA))); |
|
|
1075 | PUSHs (sv_2mortal (newSViv (GL_RGBA))); |
|
|
1076 | } |
|
|
1077 | else |
|
|
1078 | { |
|
|
1079 | FT_Bitmap bitmap; |
|
|
1080 | |
|
|
1081 | retval = newSV (w * h); |
|
|
1082 | SvPOK_only (retval); |
|
|
1083 | SvCUR_set (retval, w * h); |
|
|
1084 | |
|
|
1085 | bitmap.rows = h; |
|
|
1086 | bitmap.width = w; |
|
|
1087 | bitmap.pitch = w; |
|
|
1088 | bitmap.buffer = (unsigned char*)SvPVX (retval); |
|
|
1089 | bitmap.num_grays = 256; |
|
|
1090 | bitmap.pixel_mode = FT_PIXEL_MODE_GRAY; |
|
|
1091 | |
|
|
1092 | memset (bitmap.buffer, 0, w * h); |
|
|
1093 | |
|
|
1094 | pango_ft2_render_layout (&bitmap, self->pl, 0 * PANGO_SCALE, 0 * PANGO_SCALE); |
|
|
1095 | |
|
|
1096 | EXTEND (SP, 5); |
|
|
1097 | PUSHs (sv_2mortal (newSViv (w))); |
|
|
1098 | PUSHs (sv_2mortal (newSViv (h))); |
|
|
1099 | PUSHs (sv_2mortal (retval)); |
|
|
1100 | PUSHs (sv_2mortal (newSViv (GL_ALPHA))); |
|
|
1101 | PUSHs (sv_2mortal (newSViv (GL_ALPHA))); |
|
|
1102 | } |
|
|
1103 | } |
|
|
1104 | |
1027 | |
1105 | MODULE = CFClient PACKAGE = CFClient::Texture |
1028 | MODULE = CFClient PACKAGE = CFClient::Texture |
1106 | |
1029 | |
1107 | void |
1030 | void |
1108 | pad2pot (SV *data_, SV *w_, SV *h_) |
1031 | pad2pot (SV *data_, SV *w_, SV *h_) |
… | |
… | |
1510 | void |
1433 | void |
1511 | draw_magicmap (CFClient::Map self, int dx, int dy, int w, int h, unsigned char *data) |
1434 | draw_magicmap (CFClient::Map self, int dx, int dy, int w, int h, unsigned char *data) |
1512 | CODE: |
1435 | CODE: |
1513 | { |
1436 | { |
1514 | static float color[16][3] = { |
1437 | static float color[16][3] = { |
1515 | { 0.00, 0.00, 0.00 }, |
1438 | { 0.00F, 0.00F, 0.00F }, |
1516 | { 1.00, 1.00, 1.00 }, |
1439 | { 1.00F, 1.00F, 1.00F }, |
1517 | { 0.00, 0.00, 0.55 }, |
1440 | { 0.00F, 0.00F, 0.55F }, |
1518 | { 1.00, 0.00, 0.00 }, |
1441 | { 1.00F, 0.00F, 0.00F }, |
1519 | |
1442 | |
1520 | { 1.00, 0.54, 0.00 }, |
1443 | { 1.00F, 0.54F, 0.00F }, |
1521 | { 0.11, 0.56, 1.00 }, |
1444 | { 0.11F, 0.56F, 1.00F }, |
1522 | { 0.93, 0.46, 0.00 }, |
1445 | { 0.93F, 0.46F, 0.00F }, |
1523 | { 0.18, 0.54, 0.34 }, |
1446 | { 0.18F, 0.54F, 0.34F }, |
1524 | |
1447 | |
1525 | { 0.56, 0.73, 0.56 }, |
1448 | { 0.56F, 0.73F, 0.56F }, |
1526 | { 0.80, 0.80, 0.80 }, |
1449 | { 0.80F, 0.80F, 0.80F }, |
1527 | { 0.55, 0.41, 0.13 }, |
1450 | { 0.55F, 0.41F, 0.13F }, |
1528 | { 0.99, 0.77, 0.26 }, |
1451 | { 0.99F, 0.77F, 0.26F }, |
1529 | |
1452 | |
1530 | { 0.74, 0.65, 0.41 }, |
1453 | { 0.74F, 0.65F, 0.41F }, |
1531 | |
1454 | |
1532 | { 0.00, 1.00, 1.00 }, |
1455 | { 0.00F, 1.00F, 1.00F }, |
1533 | { 1.00, 0.00, 1.00 }, |
1456 | { 1.00F, 0.00F, 1.00F }, |
1534 | { 1.00, 1.00, 0.00 }, |
1457 | { 1.00F, 1.00F, 0.00F }, |
1535 | }; |
1458 | }; |
1536 | |
1459 | |
1537 | int x, y; |
1460 | int x, y; |
1538 | |
1461 | |
1539 | glEnable (GL_TEXTURE_2D); |
1462 | glEnable (GL_TEXTURE_2D); |