ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/Client.xs
(Generate patch)

Comparing deliantra/Deliantra-Client/Client.xs (file contents):
Revision 1.60 by root, Sun Apr 23 04:47:02 2006 UTC vs.
Revision 1.61 by root, Sun Apr 23 21:47:30 2006 UTC

40#define FOW_DARKNESS 32 40#define FOW_DARKNESS 32
41 41
42#define MAP_EXTEND_X 32 42#define MAP_EXTEND_X 32
43#define MAP_EXTEND_Y 512 43#define MAP_EXTEND_Y 512
44 44
45#define MIN_FONT_HEIGHT 8 * PANGO_SCALE 45#define MIN_FONT_HEIGHT 8
46 46
47typedef Mix_Chunk *CFClient__MixChunk; 47typedef Mix_Chunk *CFClient__MixChunk;
48typedef Mix_Music *CFClient__MixMusic; 48typedef Mix_Music *CFClient__MixMusic;
49 49
50static PangoContext *context; 50typedef PangoFontDescription *CFClient__Font;
51static PangoFontMap *fontmap;
52 51
53typedef struct cf_layout { 52typedef struct cf_layout {
54 PangoLayout *pl; 53 PangoLayout *pl;
55 int base_height; 54 int base_height;
55 CFClient__Font font;
56} *CFClient__Layout; 56} *CFClient__Layout;
57
58static CFClient__Font default_font;
59static PangoContext *context;
60static PangoFontMap *fontmap;
57 61
58static void 62static void
59substitute_func (FcPattern *pattern, gpointer data) 63substitute_func (FcPattern *pattern, gpointer data)
60{ 64{
61 FcPatternAddBool (pattern, FC_HINTING , 1); 65 FcPatternAddBool (pattern, FC_HINTING , 1);
62 FcPatternAddBool (pattern, FC_AUTOHINT, 0); 66 FcPatternAddBool (pattern, FC_AUTOHINT, 0);
63} 67}
64 68
65static void 69static void
66layout_update (CFClient__Layout self) 70layout_update_font (CFClient__Layout self)
67{ 71{
68 /* use a random scale factor to account for unknown descenders, 0.8 works 72 /* use a random scale factor to account for unknown descenders, 0.8 works
69 * reasonably well with bitstream vera 73 * reasonably well with bitstream vera
70 */ 74 */
71 PangoFontDescription *font = (PangoFontDescription *) 75 PangoFontDescription *font = self->font ? self->font : default_font;
72 pango_layout_get_font_description (self->pl);
73 76
74 int height = self->base_height * (PANGO_SCALE * 8 / 10);
75
76 if (height < MIN_FONT_HEIGHT)
77 height = MIN_FONT_HEIGHT;
78
79 if (pango_font_description_get_size (font) != height)
80 {
81 font = pango_font_description_copy (font);
82 pango_font_description_set_absolute_size (font, height); 77 pango_font_description_set_absolute_size (font,
78 MAX (MIN_FONT_HEIGHT, self->base_height) * (PANGO_SCALE * 8 / 10));
79
83 pango_layout_set_font_description (self->pl, font); 80 pango_layout_set_font_description (self->pl, font);
84 }
85} 81}
86 82
87static void 83static void
88layout_get_pixel_size (CFClient__Layout self, int *w, int *h) 84layout_get_pixel_size (CFClient__Layout self, int *w, int *h)
89{ 85{
90 layout_update (self);
91
92 pango_layout_get_pixel_size (self->pl, w, h); 86 pango_layout_get_pixel_size (self->pl, w, h);
93 87
94 *w = (*w + 3) & ~3; 88 *w = (*w + 3) & ~3;
95 if (!*w) *w = 1; 89 if (!*w) *w = 1;
96 if (!*h) *h = 1; 90 if (!*h) *h = 1;
521add_font (char *file) 515add_font (char *file)
522 CODE: 516 CODE:
523 FcConfigAppFontAddFile (0, (const FcChar8 *)file); /* no idea wether this is required */ 517 FcConfigAppFontAddFile (0, (const FcChar8 *)file); /* no idea wether this is required */
524 518
525void 519void
526set_font (char *file)
527 CODE:
528{
529 int count;
530 FcPattern *pattern = FcFreeTypeQuery ((const FcChar8 *)file, 0, 0, &count);
531 PangoFontDescription *font = pango_fc_font_description_from_pattern (pattern, 0);
532 FcPatternDestroy (pattern);
533 pango_font_description_set_absolute_size (font, MIN_FONT_HEIGHT);
534 pango_context_set_font_description (context, font);
535}
536
537void
538load_image_inline (SV *image_) 520load_image_inline (SV *image_)
539 ALIAS: 521 ALIAS:
540 load_image_file = 1 522 load_image_file = 1
541 PPCODE: 523 PPCODE:
542{ 524{
631#else 613#else
632 fprintf (stderr, "FATAL: %s\n", message); 614 fprintf (stderr, "FATAL: %s\n", message);
633#endif 615#endif
634 exit (1); 616 exit (1);
635 617
618MODULE = CFClient PACKAGE = CFClient::Font
619
620CFClient::Font
621new_from_file (SV *class, char *path)
622 CODE:
623{
624 int count;
625 FcPattern *pattern = FcFreeTypeQuery ((const FcChar8 *)path, 0, 0, &count);
626 RETVAL = pango_fc_font_description_from_pattern (pattern, 0);
627 FcPatternDestroy (pattern);
628}
629 OUTPUT:
630 RETVAL
631
632void
633DESTROY (CFClient::Font self)
634 CODE:
635 pango_font_description_free (self);
636
637void
638make_default (CFClient::Font self)
639 CODE:
640 default_font = self;
641
636MODULE = CFClient PACKAGE = CFClient::Layout 642MODULE = CFClient PACKAGE = CFClient::Layout
637 643
638CFClient::Layout 644CFClient::Layout
639new (SV *class, int base_height = 10) 645new (SV *class, int base_height = MIN_FONT_HEIGHT)
640 CODE: 646 CODE:
641 New (0, RETVAL, 1, struct cf_layout); 647 New (0, RETVAL, 1, struct cf_layout);
648 RETVAL->pl = pango_layout_new (context);
642 RETVAL->base_height = base_height; 649 RETVAL->base_height = base_height;
643 RETVAL->pl = pango_layout_new (context); 650 RETVAL->font = 0;
644 pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR); 651 pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR);
645 pango_layout_set_font_description (RETVAL->pl, 652 pango_layout_set_font_description (RETVAL->pl, default_font);
646 pango_font_description_copy (
647 pango_context_get_font_description (context)));
648 OUTPUT: 653 OUTPUT:
649 RETVAL 654 RETVAL
650 655
651void 656void
652DESTROY (CFClient::Layout self) 657DESTROY (CFClient::Layout self)
681 SvUTF8_on (RETVAL); 686 SvUTF8_on (RETVAL);
682 OUTPUT: 687 OUTPUT:
683 RETVAL 688 RETVAL
684 689
685void 690void
691set_font (CFClient::Layout self, CFClient::Font font = 0)
692 CODE:
693 if (self->font != font)
694 {
695 self->font = font;
696 layout_update_font (self);
697 }
698
699void
686set_height (CFClient::Layout self, int base_height) 700set_height (CFClient::Layout self, int base_height)
687 CODE: 701 CODE:
702 if (self->base_height != base_height)
703 {
688 self->base_height = base_height; 704 self->base_height = base_height;
705 layout_update_font (self);
706 }
689 707
690void 708void
691set_width (CFClient::Layout self, int max_width = -1) 709set_width (CFClient::Layout self, int max_width = -1)
692 CODE: 710 CODE:
693 pango_layout_set_width (self->pl, max_width < 0 ? max_width : max_width * PANGO_SCALE); 711 pango_layout_set_width (self->pl, max_width < 0 ? max_width : max_width * PANGO_SCALE);
696size (CFClient::Layout self) 714size (CFClient::Layout self)
697 PPCODE: 715 PPCODE:
698{ 716{
699 int w, h; 717 int w, h;
700 718
701 layout_update (self);
702 layout_get_pixel_size (self, &w, &h); 719 layout_get_pixel_size (self, &w, &h);
703 720
704 EXTEND (SP, 2); 721 EXTEND (SP, 2);
705 PUSHs (sv_2mortal (newSViv (w))); 722 PUSHs (sv_2mortal (newSViv (w)));
706 PUSHs (sv_2mortal (newSViv (h))); 723 PUSHs (sv_2mortal (newSViv (h)));
709int 726int
710xy_to_index (CFClient::Layout self, int x, int y) 727xy_to_index (CFClient::Layout self, int x, int y)
711 CODE: 728 CODE:
712{ 729{
713 int index, trailing; 730 int index, trailing;
714
715 layout_update (self);
716 pango_layout_xy_to_index (self->pl, x * PANGO_SCALE, y * PANGO_SCALE, &index, &trailing); 731 pango_layout_xy_to_index (self->pl, x * PANGO_SCALE, y * PANGO_SCALE, &index, &trailing);
717
718 RETVAL = index; 732 RETVAL = index;
719} 733}
720 OUTPUT: 734 OUTPUT:
721 RETVAL 735 RETVAL
722 736
723void 737void
724cursor_pos (CFClient::Layout self, int index) 738cursor_pos (CFClient::Layout self, int index)
725 PPCODE: 739 PPCODE:
726{ 740{
727 PangoRectangle strong_pos; 741 PangoRectangle strong_pos;
728 layout_update (self);
729 pango_layout_get_cursor_pos (self->pl, index, &strong_pos, 0); 742 pango_layout_get_cursor_pos (self->pl, index, &strong_pos, 0);
730 743
731 EXTEND (SP, 3); 744 EXTEND (SP, 3);
732 PUSHs (sv_2mortal (newSViv (strong_pos.x / PANGO_SCALE))); 745 PUSHs (sv_2mortal (newSViv (strong_pos.x / PANGO_SCALE)));
733 PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE))); 746 PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE)));
740{ 753{
741 SV *retval; 754 SV *retval;
742 int w, h; 755 int w, h;
743 FT_Bitmap bitmap; 756 FT_Bitmap bitmap;
744 757
745 layout_update (self);
746 layout_get_pixel_size (self, &w, &h); 758 layout_get_pixel_size (self, &w, &h);
747 759
748 retval = newSV (w * h); 760 retval = newSV (w * h);
749 SvPOK_only (retval); 761 SvPOK_only (retval);
750 SvCUR_set (retval, w * h); 762 SvCUR_set (retval, w * h);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines