aboutsummaryrefslogtreecommitdiff
path: root/textures/base/pack/down.png
blob: 7d13857ca62ea7b5cd102577cc962c1cc96a7c93 (plain)
ofshex dumpascii
0000 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 01 00 00 00 00 80 08 06 00 00 00 e4 b5 b7 .PNG........IHDR................
0020 0a 00 00 00 06 62 4b 47 44 00 00 00 00 00 00 f9 43 bb 7f 00 00 00 09 70 48 59 73 00 00 0b 13 00 .....bKGD.......C......pHYs.....
0040 00 0b 13 01 00 9a 9c 18 00 00 00 07 74 49 4d 45 07 de 03 16 09 1d 1d 45 89 9e 26 00 00 00 19 74 ............tIME.......E..&....t
0060 45 58 74 43 6f 6d 6d 65 6e 74 00 43 72 65 61 74 65 64 20 77 69 74 68 20 47 49 4d 50 57 81 0e 17 EXtComment.Created.with.GIMPW...
0080 00 00 06 e5 49 44 41 54 78 da ed dd 3b 8c 15 55 1c c7 f1 ef 1a 0a 5b 2b c5 47 22 12 40 70 91 05 ....IDATx...;..U......[+.G".@p..
00a0 83 f2 88 af 10 15 89 68 22 4a 8c 89 6b 61 74 51 63 30 b1 a1 93 52 ec 20 22 4a 61 01 26 c6 60 b0 .......h"J..katQc0...R.."Ja.&.`.
00c0 59 8d 0f 0a 10 05 83 ca 3b 44 11 dc 28 2f 05 a4 10 31 80 81 b5 f8 cf 95 5b ac b8 b0 7b ee 3d 33 Y.......;D..(/...1......[...{.=3
00e0 f3 fd 74 26 02 f7 ce 9c ff 6f fe e7 dc 99 33 1d fd fd fd 48 aa a7 2b 3c 04 92 01 20 c9 00 90 64 ..t&.....o....3....H..+<.......d
0100 00 48 32 00 24 19 00 92 0c 00 49 06 80 24 03 40 92 01 20 c9 00 90 64 00 48 32 00 24 19 00 92 0c .H2.$.....I..$.@......d.H2.$....
0120 00 49 b9 1a d1 a2 7f c7 4d 07 a4 4b d7 61 07 20 c9 00 90 64 00 48 32 00 24 19 00 92 0c 00 49 06 .I......M..K.a.....d.H2.$.....I.
0140 80 24 03 40 92 01 20 c9 00 90 64 00 48 1a c0 88 4c 3f 57 87 a7 46 15 94 dd 33 31 76 00 92 53 00 .$.@......d.H...L?W..F...31v..S.
0160 49 06 80 24 03 40 92 01 20 c9 00 90 64 00 48 32 00 24 19 00 92 0c 00 49 06 80 24 03 40 92 01 20 I..$.@......d.H2.$.....I..$.@...
0180 c9 00 90 64 00 48 32 00 24 19 00 92 0c 80 ff e5 db 84 55 35 59 8e e9 11 25 38 60 6e 0f 26 0b bf ...d.H2.$.........U5Y...%8`n.&..
01a0 86 01 60 10 c8 c2 37 00 0c 02 59 f8 06 80 41 20 0b df 00 30 08 64 e1 0f 9f 2b 3c 01 52 7d c7 5e ..`...7...Y...A....0.d...+<.R}.^
01c0 ab 02 e0 55 e0 74 e2 13 61 10 a8 95 85 9f 72 bc fd 05 2c 6e c5 17 e9 e8 ef 6f 59 dd 8c 02 96 03 ...U.t..a.....r...,n.....oY.....
01e0 0f a6 fe 4e 8e 4f 95 f8 aa ff 01 b0 10 38 5c b5 00 68 78 08 58 01 5c 6f 10 c8 c2 ff d7 01 e0 05 ...N.O.......8\..hx.X.\o........
0200 a0 b7 ea 6b 00 bd 40 27 b0 14 38 ef b4 40 35 6f f7 cf 15 b5 d0 d9 ea e2 6f 57 07 d0 6c 06 f0 76 ...k..@'..8..@5o........oW..l..v
0220 f1 e5 ed 06 54 b7 ab fe 77 c0 73 c0 d6 76 7d c1 76 ff 0a b0 09 98 52 cc 79 4e d9 0d a8 26 57 fd ....T...w.s..v}.v.....R.yN...&W.
0240 3f 8b 31 3f ad 9d c5 9f 43 07 d0 ec 26 e0 4d e0 01 bb 01 55 f8 aa ff 29 f0 3c d0 97 c3 97 cd e9 ?.1?....C...&.M....U...).<......
0260 3e 80 9f 80 d9 c0 7c e0 a8 dd 80 2a 76 d5 3f 5a 8c ed d9 b9 14 7f 6e 01 d0 b0 06 18 07 ac 4c 7c >.....|....*v.?Z......n.......L|
0280 42 0c 01 b5 62 2c 9c 27 16 f9 c6 15 63 3b 2b 39 4d 01 06 72 27 f0 16 30 c1 69 81 4a 78 11 d8 03 B...b,.'....c;+9M..r'..0.i.Jx...
02a0 2c 00 36 e6 7a 00 72 bf 15 78 23 d0 05 2c 02 ce d8 0d a8 24 e7 fc 4c 31 66 bb 72 2e fe 32 74 00 ,.6.z.r..x#..,.....$..L1f.r..2t.
02c0 cd 6e 25 7e 32 9c 66 37 a0 8c c3 7e 0b f1 d3 de 8e 32 1c 8c 32 3d 0c b4 93 b8 6f a0 1b 38 9e 78 .n%~2.f7...~.....2..2=....o..8.x
02e0 80 d8 11 54 b3 f0 53 9e d7 e3 c5 d8 9c 56 96 e2 2f 5b 00 34 4e e2 6a 5c 24 54 5e e7 72 15 71 33 ...T..S......V../[.4N.j\$T^.r.q3
0300 db ea b2 8d 9b 32 4d 01 06 72 37 b1 48 78 b3 d3 02 b5 a1 f0 f7 02 3d c0 fa b2 1e a0 b2 ef 07 b0 .....2M..r7.Hx........=.........
0320 01 98 4c 3c 3a e9 22 a1 5a 75 ce ce 16 63 ae ab cc c5 5f 85 0e a0 d9 d8 a2 1b b8 d7 6e c0 c2 4f ..L<:.".Zu...c...._.........n..O
0340 e8 eb e2 aa bf b3 0a 07 ab 4a 2f 06 d9 0b cc 22 16 62 7e b7 1b b0 f8 87 d9 1f 45 e1 cf ac 4a f1 .........J/....".b~.......E...J.
0360 57 ad 03 68 76 03 f0 06 f0 b0 dd 80 85 3f 0c 3e 21 9e d5 ef ab da 81 ab ea ab c1 0e 00 8f 00 f3 W..hv........?.>!...............
0380 80 43 76 03 16 ff 65 fa 0d 78 92 d8 c5 aa af 8a 07 af ea ef 06 5c 0b 8c 21 16 6c ce 26 1c 80 06 .Cv...e..x...........\..!.l.&...
03a0 41 7b 0a 3f d5 71 3f 07 2c 01 46 03 ef 55 f9 20 56 75 0a 30 90 56 dc 49 e8 94 a0 fc 57 fd 6f 89 A{.?.q?.,.F..U..Vu.0.V.I....W.o.
03c0 3b f9 b6 d5 e1 40 d6 e9 ed c0 3b 89 05 9c 1e 62 41 c7 6e c0 ab 7e b3 c6 26 1d d3 eb 52 fc 75 eb ;....@....;....bA.n..~..&...R.u.
03e0 00 9a 8d 04 5e 23 7e 31 48 7a 7c ad d9 ec af f8 50 e1 45 3e 3b 80 81 1d 01 9e 06 9e 00 7e 2d f1 ....^#~1Hz|.....P.E>;........~-.
0400 c0 b5 f8 87 e6 04 f0 0c 30 a7 8e c5 5f e7 00 68 78 9f 58 e8 59 42 2c fc 38 2d a8 47 bb df d8 a4 ........0..._..hx.X.YB,.8-.G....
0420 63 34 f0 4e 9d cf 4f 5d a7 00 03 e9 22 16 09 6f 77 4a 50 e9 ab fe 1e 62 1d e8 4b 0f b3 1d 40 b3 c4.N..O]...."..owJP....b..K...@.
0440 ed c4 02 50 0f 70 d2 6e a0 72 57 fd d3 5c d8 a4 c3 e2 b7 03 b8 a8 6b 81 65 c4 8d 44 76 03 e5 bf ...P.p.n.rW..\........k.e..Dv...
0460 ea af 23 76 e2 dd e7 61 b6 03 18 8c c3 c0 63 c0 5c e2 ae 42 bb 81 72 5e f5 1b 9b 74 dc 6f f1 1b ..#v...a......c.\..B..r^...t.o..
0480 00 97 a3 17 98 48 2c 18 a5 5c 24 f4 aa 3f fc 7f ef 4a 62 e3 98 d5 18 b4 4e 01 86 c1 94 62 50 dd .....H,..\$..?...Jb.....N....bP.
04a0 e6 b4 20 eb d0 fb 81 58 c7 d9 e0 90 b5 03 18 4e 5b 89 db 88 17 12 77 8d 39 2d c8 eb fb 35 36 e9 .......X.......N[.....w.9-...56.
04c0 98 6c f1 db 01 a4 36 8a 78 8d d9 6c bb 81 2c ae fa 9b 8b ab fe 2e 87 a6 1d 40 2b f4 11 8f 88 ce .l....6.x..l..,..........@+.....
04e0 25 dd e3 c6 55 e9 06 52 7e 8f e6 4d 3a 2c 7e 03 a0 e5 7a 89 dd 60 97 12 77 97 95 ed ca 59 e6 ab %...U..R~..M:,~...z..`..w....Y..
0500 fe 2a 62 33 d8 95 b8 c8 e7 14 20 03 33 89 3b 09 6f 71 5a 90 b4 20 7f 01 5e 2c c2 57 76 00 d9 f8 .*b3........3.;.oqZ.....^,.Wv...
0520 8a 58 80 5a 44 dc 75 56 d7 6e 20 f5 26 1d e3 2d 7e 3b 80 dc 8d 06 56 00 f7 d5 a8 1b 48 39 90 be .X.ZD.uV.n..&..-~;....V.....H9..
0540 21 36 e9 d8 ee d0 b2 03 28 83 fd c4 dd 67 f3 81 63 35 e8 06 52 6f d2 31 c3 e2 b7 03 28 ab ab 88 !6......(....g..c5..Ro.1....(...
0560 cd 47 9e 4d 78 d5 ee a8 58 e1 03 7c 08 bc 44 da 4d 5d 0d 00 03 a0 65 ee 22 5e 5c 32 be 22 21 90 .G.Mx...X..|..D.M]....e."^\2."!.
0580 6a e0 1c 24 1e dc 71 9e ef 14 a0 52 be 00 26 11 8b 84 67 12 15 64 7f 8b 0a 3f c5 bf d3 d8 a4 63 j..$..q....R..&...g..d...?.....c
05a0 a2 c5 6f 07 50 75 63 88 45 c2 59 29 cf 6d 89 da fd 7d c5 55 7f 9d 43 c3 0e a0 0e 7e 24 7e 21 e8 ..o.Puc.E.Y).m...}.U..C....~$~!.
05c0 26 1e 59 cd bd 45 4f bd 49 c7 04 8b df 0e a0 ae ae 06 5e 07 9e 22 bf 45 c2 94 83 e3 f3 e2 aa bf &.Y..EO.I.........^..".E........
05e0 df 21 60 00 08 ee 21 16 09 c7 65 12 02 a9 06 c6 31 e2 4e be 35 9e 72 a7 00 ba 60 3d 30 95 74 9b .!`...!...e.....1.N.5.r...`=0.t.
0600 8f 0c 76 f1 2e d5 22 5f f3 26 1d 16 bf 1d 80 2e 62 22 f1 5c c1 f4 16 77 03 a9 06 c3 f7 c0 02 7c ..v..."_.&......b".\...w.......|
0620 4e df 0e 40 83 b2 8b 78 b8 a8 9b 78 79 45 ea 6e 20 d5 55 df 4d 3a ec 00 34 44 d7 10 0f c1 74 97 N..@...x...xyE.n..U.M:..4D....t.
0640 ec 73 6f 26 ee df df ed 29 34 00 34 74 73 80 e5 c0 8d 99 7f ce 13 c0 cb c0 bb f8 9c be 53 00 0d .so&....)4.4ts...............S..
0660 9b 8f 89 bd 06 16 03 7f 67 fa 19 57 15 9f d1 9d 78 ed 00 94 d0 24 62 91 f0 8e 4c 3e cf cf c4 4f ........g..W....x....$b...L>...O
0680 7b 1f 79 6a ec 00 94 de 0e e2 11 d9 1e 62 5f bc 76 69 6c d2 31 c1 e2 b7 03 50 7b 8c 24 ee 1d 78 {.yj.........b_.vil.1....P{.$..x
06a0 bc c5 ff ee 96 22 80 7c 4e df 0e 40 6d 74 84 d8 78 64 2e b1 5f 5e 6a 27 8b c2 9f 6e f1 1b 00 ca .....".|N..@mt..xd.._^j'...n....
06c0 47 2f b1 d7 c0 12 d2 bd c6 6c 2d 17 76 e2 3d ef 21 77 0a a0 3c 4d 26 16 09 a7 0e d3 df e7 26 1d G/.......l-.v.=.!w..<M&.......&.
06e0 76 00 2a 91 6d c4 22 e1 50 5f 63 d6 d8 a4 a3 d3 e2 b7 03 50 39 5d 07 2c 03 1e bd c4 3f b7 bb 98 v.*.m.".P_c........P9].,....?...
0700 eb 6f f2 10 da 01 a8 bc 0e 01 f3 88 45 c2 83 83 f8 ff 4f 15 9d c3 14 8b df 00 50 75 34 bf c6 ec .o..........E.....O.......Pu4...
0720 bf 16 09 3f 23 6e 32 5a 46 be 77 1b ca 29 80 86 68 06 b1 48 d8 59 fc b7 9b 74 18 00 aa 99 2b 89 ...?#n2ZF.w..)..h..H.Y...t....+.
0740 fd f8 c6 02 af 10 f7 13 c8 00 90 e4 1a 80 24 03 40 92 01 20 c9 00 90 64 00 48 32 00 24 19 00 92 ..............$.@......d.H2.$...
0760 0c 00 49 06 80 a4 d2 f8 07 9f 78 df 9d d0 76 a6 e2 00 00 00 00 49 45 4e 44 ae 42 60 82 ..I.......x...v......IEND.B`.
394'>394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
/*
   CGUITTFont FreeType class for Irrlicht
   Copyright (c) 2009-2010 John Norman
   Copyright (c) 2016 Nathanaël Courant

   This software is provided 'as-is', without any express or implied
   warranty. In no event will the authors be held liable for any
   damages arising from the use of this software.

   Permission is granted to anyone to use this software for any
   purpose, including commercial applications, and to alter it and
   redistribute it freely, subject to the following restrictions:

   1. The origin of this software must not be misrepresented; you
      must not claim that you wrote the original software. If you use
      this software in a product, an acknowledgment in the product
      documentation would be appreciated but is not required.

   2. Altered source versions must be plainly marked as such, and
      must not be misrepresented as being the original software.

   3. This notice may not be removed or altered from any source
      distribution.

   The original version of this class can be located at:
   http://irrlicht.suckerfreegames.com/

   John Norman
   john@suckerfreegames.com
*/

#include <irrlicht.h>
#include <iostream>
#include "CGUITTFont.h"

namespace irr
{
namespace gui
{

// Manages the FT_Face cache.
struct SGUITTFace : public virtual irr::IReferenceCounted
{
	SGUITTFace() : face_buffer(0), face_buffer_size(0)
	{
		memset((void*)&face, 0, sizeof(FT_Face));
	}

	~SGUITTFace()
	{
		FT_Done_Face(face);
		delete[] face_buffer;
	}

	FT_Face face;
	FT_Byte* face_buffer;
	FT_Long face_buffer_size;
};

// Static variables.
FT_Library CGUITTFont::c_library;
core::map<io::path, SGUITTFace*> CGUITTFont::c_faces;
bool CGUITTFont::c_libraryLoaded = false;
scene::IMesh* CGUITTFont::shared_plane_ptr_ = 0;
scene::SMesh CGUITTFont::shared_plane_;

//

/** Checks that no dimension of the FT_BitMap object is negative.  If either is
 * negative, abort execution.
 */
inline void checkFontBitmapSize(const FT_Bitmap &bits)
{
	if ((s32)bits.rows < 0 || (s32)bits.width < 0) {
		std::cout << "Insane font glyph size. File: "
		          << __FILE__ << " Line " << __LINE__
		          << std::endl;
		abort();
	}
}

video::IImage* SGUITTGlyph::createGlyphImage(const FT_Bitmap& bits, video::IVideoDriver* driver) const
{
	// Make sure our casts to s32 in the loops below will not cause problems
	checkFontBitmapSize(bits);

	// Determine what our texture size should be.
	// Add 1 because textures are inclusive-exclusive.
	core::dimension2du d(bits.width + 1, bits.rows + 1);
	core::dimension2du texture_size;
	//core::dimension2du texture_size(bits.width + 1, bits.rows + 1);

	// Create and load our image now.
	video::IImage* image = 0;
	switch (bits.pixel_mode)
	{
		case FT_PIXEL_MODE_MONO:
		{
			// Create a blank image and fill it with transparent pixels.
			texture_size = d.getOptimalSize(true, true);
			image = driver->createImage(video::ECF_A1R5G5B5, texture_size);
			image->fill(video::SColor(0, 255, 255, 255));

			// Load the monochrome data in.
			const u32 image_pitch = image->getPitch() / sizeof(u16);
			u16* image_data = (u16*)image->lock();
			u8* glyph_data = bits.buffer;

			for (s32 y = 0; y < (s32)bits.rows; ++y)
			{
				u16* row = image_data;
				for (s32 x = 0; x < (s32)bits.width; ++x)
				{
					// Monochrome bitmaps store 8 pixels per byte.  The left-most pixel is the bit 0x80.
					// So, we go through the data each bit at a time.
					if ((glyph_data[y * bits.pitch + (x / 8)] & (0x80 >> (x % 8))) != 0)
						*row = 0xFFFF;
					++row;
				}
				image_data += image_pitch;
			}
			image->unlock();
			break;
		}

		case FT_PIXEL_MODE_GRAY:
		{
			// Create our blank image.
			texture_size = d.getOptimalSize(!driver->queryFeature(video::EVDF_TEXTURE_NPOT), !driver->queryFeature(video::EVDF_TEXTURE_NSQUARE), true, 0);
			image = driver->createImage(video::ECF_A8R8G8B8, texture_size);
			image->fill(video::SColor(0, 255, 255, 255));

			// Load the grayscale data in.
			const float gray_count = static_cast<float>(bits.num_grays);
			const u32 image_pitch = image->getPitch() / sizeof(u32);
			u32* image_data = (u32*)image->lock();
			u8* glyph_data = bits.buffer;
			for (s32 y = 0; y < (s32)bits.rows; ++y)
			{
				u8* row = glyph_data;
				for (s32 x = 0; x < (s32)bits.width; ++x)
				{
					image_data[y * image_pitch + x] |= static_cast<u32>(255.0f * (static_cast<float>(*row++) / gray_count)) << 24;
					//data[y * image_pitch + x] |= ((u32)(*bitsdata++) << 24);
				}
				glyph_data += bits.pitch;
			}
			image->unlock();
			break;
		}
		default:
			// TODO: error message?
			return 0;
	}
	return image;
}

void SGUITTGlyph::preload(u32 char_index, FT_Face face, video::IVideoDriver* driver, u32 font_size, const FT_Int32 loadFlags)
{
	if (isLoaded) return;

	// Set the size of the glyph.
	FT_Set_Pixel_Sizes(face, 0, font_size);

	// Attempt to load the glyph.
	if (FT_Load_Glyph(face, char_index, loadFlags) != FT_Err_Ok)
		// TODO: error message?
		return;

	FT_GlyphSlot glyph = face->glyph;
	FT_Bitmap bits = glyph->bitmap;

	// Setup the glyph information here:
	advance = glyph->advance;
	offset = core::vector2di(glyph->bitmap_left, glyph->bitmap_top);

	// Try to get the last page with available slots.
	CGUITTGlyphPage* page = parent->getLastGlyphPage();

	// If we need to make a new page, do that now.
	if (!page)
	{
		page = parent->createGlyphPage(bits.pixel_mode);
		if (!page)
			// TODO: add error message?
			return;
	}

	glyph_page = parent->getLastGlyphPageIndex();
	u32 texture_side_length = page->texture->getOriginalSize().Width;
	core::vector2di page_position(
		(page->used_slots % (texture_side_length / font_size)) * font_size,
		(page->used_slots / (texture_side_length / font_size)) * font_size
		);
	source_rect.UpperLeftCorner = page_position;
	source_rect.LowerRightCorner = core::vector2di(page_position.X + bits.width, page_position.Y + bits.rows);

	page->dirty = true;
	++page->used_slots;
	--page->available_slots;

	// We grab the glyph bitmap here so the data won't be removed when the next glyph is loaded.
	surface = createGlyphImage(bits, driver);

	// Set our glyph as loaded.
	isLoaded = true;
}

void SGUITTGlyph::unload()
{
	if (surface)
	{
		surface->drop();
		surface = 0;
	}
	isLoaded = false;
}

//////////////////////

CGUITTFont* CGUITTFont::createTTFont(IGUIEnvironment *env, const io::path& filename, const u32 size, const bool antialias, const bool transparency, const u32 shadow, const u32 shadow_alpha)
{
	if (!c_libraryLoaded)
	{
		if (FT_Init_FreeType(&c_library))
			return 0;
		c_libraryLoaded = true;
	}

	CGUITTFont* font = new CGUITTFont(env);
	bool ret = font->load(filename, size, antialias, transparency);
	if (!ret)
	{
		font->drop();
		return 0;
	}

	font->shadow_offset = shadow;
	font->shadow_alpha = shadow_alpha;

	return font;
}

CGUITTFont* CGUITTFont::createTTFont(IrrlichtDevice *device, const io::path& filename, const u32 size, const bool antialias, const bool transparency)
{
	if (!c_libraryLoaded)
	{
		if (FT_Init_FreeType(&c_library))
			return 0;
		c_libraryLoaded = true;
	}

	CGUITTFont* font = new CGUITTFont(device->getGUIEnvironment());
	font->Device = device;
	bool ret = font->load(filename, size, antialias, transparency);
	if (!ret)
	{
		font->drop();
		return 0;
	}

	return font;
}

CGUITTFont* CGUITTFont::create(IGUIEnvironment *env, const io::path& filename, const u32 size, const bool antialias, const bool transparency)
{
	return CGUITTFont::createTTFont(env, filename, size, antialias, transparency);
}

CGUITTFont* CGUITTFont::create(IrrlichtDevice *device, const io::path& filename, const u32 size, const bool antialias, const bool transparency)
{
	return CGUITTFont::createTTFont(device, filename, size, antialias, transparency);
}

//////////////////////

//! Constructor.
CGUITTFont::CGUITTFont(IGUIEnvironment *env)
: use_monochrome(false), use_transparency(true), use_hinting(true), use_auto_hinting(true),
batch_load_size(1), Device(0), Environment(env), Driver(0), GlobalKerningWidth(0), GlobalKerningHeight(0)
{
	#ifdef _DEBUG
	setDebugName("CGUITTFont");
	#endif

	if (Environment)
	{
		// don't grab environment, to avoid circular references
		Driver = Environment->getVideoDriver();
	}

	if (Driver)
		Driver->grab();

	setInvisibleCharacters(L" ");

	// Glyphs aren't reference counted, so don't try to delete them when we free the array.
	Glyphs.set_free_when_destroyed(false);
}

bool CGUITTFont::load(const io::path& filename, const u32 size, const bool antialias, const bool transparency)
{
	// Some sanity checks.
	if (Environment == 0 || Driver == 0) return false;
	if (size == 0) return false;
	if (filename.size() == 0) return false;

	io::IFileSystem* filesystem = Environment->getFileSystem();
	irr::ILogger* logger = (Device != 0 ? Device->getLogger() : 0);
	this->size = size;
	this->filename = filename;

	// Update the font loading flags when the font is first loaded.
	this->use_monochrome = !antialias;
	this->use_transparency = transparency;
	update_load_flags();

	// Log.
	if (logger)
		logger->log(L"CGUITTFont", core::stringw(core::stringw(L"Creating new font: ") + core::ustring(filename).toWCHAR_s() + L" " + core::stringc(size) + L"pt " + (antialias ? L"+antialias " : L"-antialias ") + (transparency ? L"+transparency" : L"-transparency")).c_str(), irr::ELL_INFORMATION);

	// Grab the face.
	SGUITTFace* face = 0;
	core::map<io::path, SGUITTFace*>::Node* node = c_faces.find(filename);
	if (node == 0)
	{
		face = new SGUITTFace();
		c_faces.set(filename, face);

		if (filesystem)
		{
			// Read in the file data.
			io::IReadFile* file = filesystem->createAndOpenFile(filename);
			if (file == 0)
			{
				if (logger) logger->log(L"CGUITTFont", L"Failed to open the file.", irr::ELL_INFORMATION);

				c_faces.remove(filename);
				delete face;
				face = 0;
				return false;
			}
			face->face_buffer = new FT_Byte[file->getSize()];
			file->read(face->face_buffer, file->getSize());
			face->face_buffer_size = file->getSize();
			file->drop();

			// Create the face.
			if (FT_New_Memory_Face(c_library, face->face_buffer, face->face_buffer_size, 0, &face->face))
			{
				if (logger) logger->log(L"CGUITTFont", L"FT_New_Memory_Face failed.", irr::ELL_INFORMATION);

				c_faces.remove(filename);
				delete face;
				face = 0;
				return false;
			}
		}
		else
		{
			core::ustring converter(filename);
			if (FT_New_Face(c_library, reinterpret_cast<const char*>(converter.toUTF8_s().c_str()), 0, &face->face))
			{
				if (logger) logger->log(L"CGUITTFont", L"FT_New_Face failed.", irr::ELL_INFORMATION);

				c_faces.remove(filename);
				delete face;
				face = 0;
				return false;
			}
		}
	}
	else
	{
		// Using another instance of this face.
		face = node->getValue();
		face->grab();
	}

	// Store our face.
	tt_face = face->face;

	// Store font metrics.
	FT_Set_Pixel_Sizes(tt_face, size, 0);
	font_metrics = tt_face->size->metrics;

	// Allocate our glyphs.
	Glyphs.clear();
	Glyphs.reallocate(tt_face->num_glyphs);
	Glyphs.set_used(tt_face->num_glyphs);
	for (FT_Long i = 0; i < tt_face->num_glyphs; ++i)
	{
		Glyphs[i].isLoaded = false;
		Glyphs[i].glyph_page = 0;
		Glyphs[i].source_rect = core::recti();
		Glyphs[i].offset = core::vector2di();
		Glyphs[i].advance = FT_Vector();
		Glyphs[i].surface = 0;
		Glyphs[i].parent = this;
	}

	// Cache the first 127 ascii characters.
	u32 old_size = batch_load_size;
	batch_load_size = 127;
	getGlyphIndexByChar((uchar32_t)0);
	batch_load_size = old_size;

	return true;
}

CGUITTFont::~CGUITTFont()
{
	// Delete the glyphs and glyph pages.
	reset_images();
	CGUITTAssistDelete::Delete(Glyphs);
	//Glyphs.clear();

	// We aren't using this face anymore.
	core::map<io::path, SGUITTFace*>::Node* n = c_faces.find(filename);
	if (n)
	{
		SGUITTFace* f = n->getValue();

		// Drop our face.  If this was the last face, the destructor will clean up.
		if (f->drop())
			c_faces.remove(filename);

		// If there are no more faces referenced by FreeType, clean up.
		if (c_faces.size() == 0)
		{
			FT_Done_FreeType(c_library);
			c_libraryLoaded = false;
		}
	}

	// Drop our driver now.
	if (Driver)
		Driver->drop();
}

void CGUITTFont::reset_images()
{
	// Delete the glyphs.
	for (u32 i = 0; i != Glyphs.size(); ++i)
		Glyphs[i].unload();

	// Unload the glyph pages from video memory.
	for (u32 i = 0; i != Glyph_Pages.size(); ++i)
		delete Glyph_Pages[i];
	Glyph_Pages.clear();

	// Always update the internal FreeType loading flags after resetting.
	update_load_flags();
}

void CGUITTFont::update_glyph_pages() const
{
	for (u32 i = 0; i != Glyph_Pages.size(); ++i)
	{
		if (Glyph_Pages[i]->dirty)
			Glyph_Pages[i]->updateTexture();
	}
}

CGUITTGlyphPage* CGUITTFont::getLastGlyphPage() const
{
	CGUITTGlyphPage* page = 0;
	if (Glyph_Pages.empty())
		return 0;
	else
	{
		page = Glyph_Pages[getLastGlyphPageIndex()];
		if (page->available_slots == 0)
			page = 0;
	}
	return page;
}

CGUITTGlyphPage* CGUITTFont::createGlyphPage(const u8& pixel_mode)
{
	CGUITTGlyphPage* page = 0;
	
	// Name of our page.
	io::path name("TTFontGlyphPage_");
	name += tt_face->family_name;
	name += ".";
	name += tt_face->style_name;
	name += ".";
	name += size;
	name += "_";
	name += Glyph_Pages.size(); // The newly created page will be at the end of the collection.

	// Create the new page.
	page = new CGUITTGlyphPage(Driver, name);

	// Determine our maximum texture size.
	// If we keep getting 0, set it to 1024x1024, as that number is pretty safe.
	core::dimension2du max_texture_size = max_page_texture_size;
	if (max_texture_size.Width == 0 || max_texture_size.Height == 0)
		max_texture_size = Driver->getMaxTextureSize();
	if (max_texture_size.Width == 0 || max_texture_size.Height == 0)
		max_texture_size = core::dimension2du(1024, 1024);

	// We want to try to put at least 144 glyphs on a single texture.
	core::dimension2du page_texture_size;
	if (size <= 21) page_texture_size = core::dimension2du(256, 256);
	else if (size <= 42) page_texture_size = core::dimension2du(512, 512);
	else if (size <= 84) page_texture_size = core::dimension2du(1024, 1024);
	else if (size <= 168) page_texture_size = core::dimension2du(2048, 2048);
	else page_texture_size = core::dimension2du(4096, 4096);

	if (page_texture_size.Width > max_texture_size.Width || page_texture_size.Height > max_texture_size.Height)
		page_texture_size = max_texture_size;

	if (!page->createPageTexture(pixel_mode, page_texture_size)) {
		// TODO: add error message?
		delete page;
		return 0;
	}

	if (page)
	{
		// Determine the number of glyph slots on the page and add it to the list of pages.
		page->available_slots = (page_texture_size.Width / size) * (page_texture_size.Height / size);
		Glyph_Pages.push_back(page);
	}
	return page;
}

void CGUITTFont::setTransparency(const bool flag)
{
	use_transparency = flag;
	reset_images();
}

void CGUITTFont::setMonochrome(const bool flag)
{
	use_monochrome = flag;
	reset_images();
}

void CGUITTFont::setFontHinting(const bool enable, const bool enable_auto_hinting)
{
	use_hinting = enable;
	use_auto_hinting = enable_auto_hinting;
	reset_images();
}

void CGUITTFont::draw(const core::stringw& text, const core::rect<s32>& position, video::SColor color, bool hcenter, bool vcenter, const core::rect<s32>* clip)
{
	draw(EnrichedString(std::wstring(text.c_str()), color), position, color, hcenter, vcenter, clip);
}

void CGUITTFont::draw(const EnrichedString &text, const core::rect<s32>& position, video::SColor color, bool hcenter, bool vcenter, const core::rect<s32>* clip)
{
	std::vector<video::SColor> colors = text.getColors();

	if (!Driver)
		return;

	// Clear the glyph pages of their render information.
	for (u32 i = 0; i < Glyph_Pages.size(); ++i)
	{
		Glyph_Pages[i]->render_positions.clear();
		Glyph_Pages[i]->render_source_rects.clear();
	}

	// Set up some variables.
	core::dimension2d<s32> textDimension;
	core::position2d<s32> offset = position.UpperLeftCorner;