etrump_adaptation.c 60.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 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 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962
/*****************************************************************************
 *
 * Filename:
 * ---------
 *  etrump_adaption.c
 *
 * Project:
 * -------- 
 *  MAUI
 *
 * Description:
 * ------------
 *  adaption layer for etrump engine
 *
 * Author:
 * -------
 *  
 *
 *============================================================================
 *             HISTORY
 * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *------------------------------------------------------------------------------
 * removed!
 *
 * removed!
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 ****************************************************************************/

#if defined(FONT_ENGINE_ETRUMP)

/*****************************************************************************
 * Include
 *****************************************************************************/

#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_CACHE_H
//#include FT_INTERNAL_OBJECTS_H // need detail structure for hacking the memory allocate
#include FT_BITMAP_H

#include "mmi_include.h"
#include "font_engine_interface.h"
#include "gd_include.h"
#include "setjmp.h"
#ifdef WIN32
#include "kal_adm.h"
#endif
#include "gdi_image_alpha_bmp.h"

    #include "MMIDataType.h"
    #include "gdi_datatype.h"
    #include "kal_general_types.h"
    #include "kal_public_defs.h"
    #include "kal_public_api.h"
    #include "DebugInitDef_Int.h"
    #include "string.h"
    #include "stdio.h"
    #include "wchar.h"
    #include "gdi_include.h"
    #include "MMI_fw_trc.h"
    #include "kal_trace.h"
    #include "mmi_fw_trc.h"
    #include "gui_data_types.h"

#include "gdi_font.h"

#include "vector_font_resource_def.h"

#include "fs_gprot.h"

#include "et_common/et_common.h"
#include "textlayout/textlayout.h"

#include "CustFontDataHW.h"

#ifdef __MMI_FONT_THIRD_ROM_SUPPORT__
#include "third_rom.h"
#include "ResThirdROMUtil.h"
#endif

#if defined(__MMI_FE_VECTOR_FONT_ON_FILE_SYSTEM__)
    #define POOL_SIZE 512*1024
#elif defined(__MMI_VECTOR_FONT_MEMORY_CARD_SUPPORT__) || defined(__MMI_FONT_THIRD_ROM_SUPPORT__) 
    #define POOL_SIZE 340*1024
#else
    #define POOL_SIZE (32 + POP_FONT_FILES*8 + 8)*1024
#endif
static unsigned char mem_pool[POOL_SIZE];


#if MMI_FE_EXT_RESOURCE_NUM > 0
fs_file_tbl_struct gFS_FileTable_VFont[MMI_FE_EXT_RESOURCE_NUM];
#endif

/*****************************************************************************
 * Define
 *****************************************************************************/
#define K(X)    ((X)*1024)
#define FREETYPE_FILE_NAME_LENGTH 64
#define TEST_FORMAT_CHAR(ch)  ((ch) >= 0x200C && (ch) <= 0x200F)
#define  FREETYPE_TEXT_LENGTH FE_MAX_CLUSTER_LEN*2


#if defined(__MMI_LANG_THAI__) \
 || defined(__MMI_INDIC_ALG__) \
 || defined(__MMI_LANG_MYANMAR__) \
 || defined(__MMI_LANG_LAO__)
#define OPENTYPE_SUPPORT
#endif
/*****************************************************************************
 * Typedef
 *****************************************************************************/
typedef struct
{
    U16 res_id;
    S32 face_index;
    S32 cmap_index;
    U32 scale_ratio;
    FT_Face face;
    FT_StreamRec stream;
#ifdef OPENTYPE_SUPPORT
    FT_Layout layout;
#endif //OPENTYPE_SUPPORT
    U8 font_name[FREETYPE_FILE_NAME_LENGTH];
} etrump_font_info, *etrump_font_info_p;

#if defined(__MMI_VECTOR_FONT_MEMORY_CARD_SUPPORT__) || defined(__MMI_FE_VECTOR_FONT_ON_FILE_SYSTEM__) || defined(__MMI_FONT_THIRD_ROM_SUPPORT__) 
#define FONT_SIZE_MIN 9
#define FONT_SIZE_MAX 64

typedef struct
{
    S32 ascent;
    S32 descent;
} etrump_char_info;

static etrump_char_info et_char_info[15][FONT_SIZE_MAX-FONT_SIZE_MIN+1];

#define UNICODE_REGION_MAX  8
#define UNICODE_FONT_NUM    16

typedef struct
{
    U32  region_count;
    U16  start[UNICODE_REGION_MAX];
    U16  end[UNICODE_REGION_MAX];
    U16  resid;
} font_unicode_region;

static font_unicode_region unicode_region[UNICODE_FONT_NUM];
#endif
/*****************************************************************************
 * External functions
 *****************************************************************************/
extern U8 *GetFontResource(U16 ResId, S32 *size);
extern void gdi_draw_font_by_abm(
                S32 x, 
                S32 y, 
                gdi_color color, 
                U8 *font_data, 
                S32 char_width, 
                S32 char_height);
extern void gdi_font_prepare_4bits_font_palette(void *header,int header_size, gdi_color color);
extern unsigned long mmi_fe_font_file_reader(void* handle, unsigned long offset, unsigned char* buffer, unsigned long count);
extern void mmi_fe_font_file_closer(void* handle);
#if defined(__MMI_FE_VECTOR_FONT_ON_FILE_SYSTEM__) || defined(__MMI_VECTOR_FONT_MEMORY_CARD_SUPPORT__)
extern void* mmi_fe_get_font_stream_handle(U16 resid);
#endif
#ifdef __MMI_FONT_THIRD_ROM_SUPPORT__
extern U8 *mmi_fe_get_font_resource(U16 ResId, mmi_fe_font_resource_type_enum *type, S32 *offset, S32 *size);
#else
extern U8 *mmi_fe_get_font_resource(U16 ResId, mmi_fe_font_resource_type_enum *type, S32 *size);
#endif
extern S32 mmi_fe_get_system_font_number(void);

/*****************************************************************************
 * Global variables
 *****************************************************************************/

/* error handling */
//static MMI_BOOL render_flag = MMI_FALSE;

/* font attribute */
static U32 etrump_font_attr;
static gdi_color etrump_text_color;
static gdi_color etrump_border_color;
/* font size */
static int etrump_size;
static U32 etrump_scale_ratio;

/* engine global context */
static FT_Library etrump_library; 
static FT_Face etrump_face; 
static etrump_font_info etrump_font_list[FE_MAX_FONT_FILE_NUMBER*FE_MAX_FONT_FAMILY_NUMBER];

/* header for amb */
static U32 etrump_4bits_text_color_header[((6+16)*2+16)/4];
static U32 etrump_4bits_border_color_header[((6+16)*2+16)/4];

static U8 etrump_bits_revise_table[256];

static MMI_BOOL is_color_init = MMI_FALSE;
static MMI_BOOL is_border_color_init = MMI_FALSE;

typedef struct
{
    U16 normal_data_size;
    U16 border_data_size;
    U8  data[1];
} etrump_render_data_struct;


#define MAX_BITMAP_SIZE (128*128/2)
static U32 etrump_render_data_pool[((sizeof(etrump_render_data_struct)+MAX_BITMAP_SIZE* 2)+3)/4];

/* font family */
U16 etrump_font_res_list[FE_MAX_FONT_FILE_NUMBER*FE_MAX_FONT_FAMILY_NUMBER];
U8  etrump_font_number = 0;
U8  etrump_curr_font = 0;
U8  hp_customer_font = 0;  // highest priority customer font

static U8 sys_font_on_memory_card  = 0; 
/*****************************************************************************
 * Local functions
 *****************************************************************************/
static MMI_BOOL etrump_init(U16* font_list, U8 font_number);
static void etrump_reset_face_glyphs();
static void etrump_set_font_attr(U32 attr);
static void etrump_set_font_size(U32 size, U32 attr);
static void etrump_set_font_color(gdi_color foreground_rgb,gdi_color border_rgb);
#if defined(__MMI_VECTOR_FONT_KERNING__)
static MMI_BOOL etrump_get_font_data(
        U8 is_need_glyph_data,
        U16 ucs2, 
        U8** glyph_data,
        U32 *glyph_size,
        U32 *glyph_attr, 
        U16 *glyph_width,
        U16 *glyph_height,
        S32 *bearing_x,
        S32 *bearing_y, 
        S32 *advance_x,
        S32 *advance_y, 
        S32 *lsb_delta,
        S32 *rsb_delta,
        MMI_BOOL* valid);
#else
static MMI_BOOL etrump_get_font_data(
        U8 is_need_glyph_data,
        U16 ucs2, 
        U8** glyph_data,
        U32 *glyph_size,
        U32 *glyph_attr, 
        U16 *glyph_width,
        U16 *glyph_height,
        S32 *bearing_x,
        S32 *bearing_y, 
        S32 *advance_x,
        S32 *advance_y, 
        MMI_BOOL* valid);
#endif /* __MMI_VECTOR_FONT_KERNING__ */ 
static void etrump_show_font_data(
        U16 ucs2, 
        S32 x,
        S32 y,
        U8* glyph_ptr,
        U32 glyph_size,
        U32 glyph_attr,
        U16 glyph_width,
        U16 glyph_height,
        U32 bordered);
#if defined(__MMI_VECTOR_FONT_MEMORY_CARD_SUPPORT__) || defined(__MMI_FE_VECTOR_FONT_ON_FILE_SYSTEM__) || defined(__MMI_FONT_THIRD_ROM_SUPPORT__) 
static MMI_BOOL etrump_get_font_info(U16 resid, U32 font_size,S32 *height,S32 *ascent, S32 *descent, MMI_BOOL with_attr);
#else
static MMI_BOOL etrump_get_font_info(U32 font_size,S32 *height,S32 *ascent, S32 *descent, MMI_BOOL with_attr);
#endif
static void etrump_set_antialias(MMI_BOOL value);
static MMI_BOOL etrump_set_font(U16 fontres);
//static void etrump_restart(void);
#if defined(__MMI_VECTOR_FONT_KERNING__)
static MMI_BOOL etrump_get_font_kerning (U16 left_ucs2, U16 right_ucs2, signed long *x, signed long *y);
static MMI_BOOL etrump_has_kerning (void);
#endif



FILE *etrump_dummy_file_open(const char*filename, const char *mode) 
{
    MMI_ASSERT(0);
    return NULL;
}
int etrump_dummy_file_close(FILE *a) 
{
    MMI_ASSERT(0);
    return 0;
}
int etrump_dummy_file_seek(FILE *stream,long int offset,int origin) 
{
    MMI_ASSERT(0);
    return 0;
}
int etrump_dummy_file_tell(FILE *stream) 
{
    MMI_ASSERT(0);
    return 0;
}
int etrump_dummy_file_read(void *ptr,size_t size,size_t count,FILE *stream)
{
    MMI_ASSERT(0);
    return 0;
}
int etrump_dummy_file_error(FILE *stream)
{
    MMI_ASSERT(0);
    return 0;
}


static MMI_BOOL is_system_font_resid(U16 resid)
{
#ifndef __MMI_FONT_THIRD_ROM_SUPPORT__
    if (resid >= IMG_GLOBAL_FONT_1 && resid <= IMG_GLOBAL_FONT_MAX)
        return MMI_TRUE;
    else
#endif
        return MMI_FALSE;
}

#if defined(__MMI_VECTOR_FONT_MEMORY_CARD_SUPPORT__) || defined(__MMI_FE_VECTOR_FONT_ON_FILE_SYSTEM__) || defined(__MMI_FONT_THIRD_ROM_SUPPORT__) 
static void etrump_set_font_info(FT_Face face, U16 resid)
{
    S32 i;
    FT_Size size;
    U16 tmp_resid = resid;

    if (resid > IMG_GLOBAL_FONT_MAX)
        tmp_resid = resid - IMG_GLOBAL_FONT_MAX + POP_FONT_FILES;
    else if (resid >= IMG_GLOBAL_FONT_1)
        tmp_resid = resid - IMG_GLOBAL_FONT_1;
    else
        tmp_resid = resid + POP_FONT_FILES + sys_font_on_memory_card;

    for (i = 0; i <= FONT_SIZE_MAX-FONT_SIZE_MIN; i++)
    {
        U32 font_size = FONT_SIZE_MIN+i;

        if (etrump_scale_ratio)
            font_size = font_size * etrump_scale_ratio / 64;

        FT_Set_Pixel_Sizes(face, font_size, 0);

        size = face->size;
        et_char_info[tmp_resid][i].ascent = (size->metrics.ascender)>>6;
        et_char_info[tmp_resid][i].descent = (-(size->metrics.descender)) >>6;
    }
}

static MMI_BOOL etrump_get_unicode_region(FT_Face face, U16 resid, U8 index)
{
    S32 error = 0;

    if (!face || !face->stream)
        return MMI_FALSE;

    error = FT_Face_Get_Unicode_Region(face, (FT_UniRegion *)&unicode_region[index]);
    if (error)
    {
        return MMI_FALSE;
    }
    else
    {
        unicode_region[index].resid = resid;
        return MMI_TRUE;
    }
}
#endif

/*****************************************************************************
 * FUNCTION
 *  etrump_set_font_attr
 * DESCRIPTION
 *  To set font attribute
 * PARAMETERS
 *  U32   [IN]  the desired attirbute
 * RETURNS
 *  void
 *****************************************************************************/
static void etrump_set_font_attr(U32 attr)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

    etrump_font_attr = attr;
}


/*****************************************************************************
 * FUNCTION
 *  etrump_set_antialias
 * DESCRIPTION
 *  To turn on or turn off antialias
 * PARAMETERS
 *  MMI_BOOL   [IN]  the desired value
 * RETURNS
 *  void
 *****************************************************************************/
static void etrump_set_antialias(MMI_BOOL value)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

    if (value)
        etrump_font_attr |= FE_FONT_ATTR_ANTIALIAS;
    if (!value)
        etrump_font_attr &= (~FE_FONT_ATTR_ANTIALIAS);
    
}


/*****************************************************************************
 * FUNCTION
 *  etrump_set_font_size
 * DESCRIPTION
 *  To set font size & attr
 * PARAMETERS
 *  U32   [IN]  the desired size
 *  U32   [IN]  the desired attribute
 * RETURNS
 *  void
 *****************************************************************************/
static void etrump_set_font_size(U32 size, U32 attr)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (etrump_size != size) 
    {
        etrump_size = size;

        if (etrump_scale_ratio)
            size = size * etrump_scale_ratio / 64;

        FT_Set_Pixel_Sizes(etrump_face, size, 0);
    }
    etrump_font_attr = attr;
}


/*****************************************************************************
 * FUNCTION
 *  etrump_set_font_color
 * DESCRIPTION
 *  To set font color
 * PARAMETERS
 *  gdi_color   [IN]  the font color
 *  gdi_color   [IN]  the background color 
 * RETURNS
 *  void
 *****************************************************************************/
static void etrump_set_font_color(gdi_color foreground_rgb,gdi_color border_rgb)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if(etrump_text_color != foreground_rgb || !is_color_init)
    {
        gdi_font_prepare_4bits_font_palette(etrump_4bits_text_color_header,
                                            sizeof(etrump_4bits_text_color_header),
                                            etrump_text_color = foreground_rgb);
        is_color_init = MMI_TRUE;
    }
    if(etrump_border_color != border_rgb || !is_border_color_init)
    {
        gdi_font_prepare_4bits_font_palette(etrump_4bits_border_color_header,
                                            sizeof(etrump_4bits_border_color_header),
                                            etrump_border_color = border_rgb);
        is_border_color_init = MMI_TRUE;
    }    
}


/*****************************************************************************
 * FUNCTION
 *  etrump_get_font_info
 * DESCRIPTION
 *  To get font information regardless of the exact ucs2 character
 * PARAMETERS
 *  
 * RETURNS
 *  void
 *****************************************************************************/
#if defined(__MMI_VECTOR_FONT_MEMORY_CARD_SUPPORT__) || defined(__MMI_FE_VECTOR_FONT_ON_FILE_SYSTEM__) || defined(__MMI_FONT_THIRD_ROM_SUPPORT__) 
static MMI_BOOL etrump_get_font_info(U16 resid, U32 font_size,S32 *height,S32 *ascent, S32 *descent, MMI_BOOL with_attr)
{
    U16 tmp_resid = resid;

    if (tmp_resid == 0xff)
        tmp_resid = etrump_curr_font;
    else if (tmp_resid >= IMG_GLOBAL_FONT_MAX)
        tmp_resid = tmp_resid - IMG_GLOBAL_FONT_MAX + POP_FONT_FILES;
    else if (resid >= IMG_GLOBAL_FONT_1)
        tmp_resid = resid - IMG_GLOBAL_FONT_1;
    else
        tmp_resid = resid + POP_FONT_FILES + sys_font_on_memory_card;

    if (font_size < FONT_SIZE_MIN)
        font_size = FONT_SIZE_MIN;
    else if (font_size > FONT_SIZE_MAX)
        font_size = FONT_SIZE_MAX;

    *ascent = et_char_info[tmp_resid][font_size - FONT_SIZE_MIN].ascent;
    *descent = et_char_info[tmp_resid][font_size - FONT_SIZE_MIN].descent;

    if (with_attr) 
    {
        if (etrump_font_attr & FE_FONT_ATTR_BORDER)
        {
            *ascent  += 1;
            *descent += 1;
        }
    }

    *height = *ascent + *descent;

    return MMI_TRUE;
}
#else
static MMI_BOOL etrump_get_font_info(U32 font_size,S32 *height,S32 *ascent, S32 *descent, MMI_BOOL with_attr)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    FT_Size size;
    U32 save_size = etrump_size;
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    
    if (save_size != font_size)
        etrump_set_font_size(font_size, etrump_font_attr);
    
    size = etrump_face->size;
    *ascent = (size->metrics.ascender)>>6;
    *descent = (-(size->metrics.descender)) >>6;

    if (with_attr) 
    {
        if (etrump_font_attr & FE_FONT_ATTR_BORDER)
        {
            *ascent  += 1;
            *descent += 1;
        }
    }

    *height = *ascent + *descent;
    
    if(save_size != font_size)
        etrump_set_font_size(save_size, etrump_font_attr);

    return MMI_TRUE;
}
#endif

/*****************************************************************************
 * FUNCTION
 *  etrump_data_convert
 * DESCRIPTION
 * PARAMETERS
 * RETURNS
 *****************************************************************************/
S32 etrump_data_convert(U8* dest, U8* src, S32 pitch, S32 width, S32 height, S32 size, MMI_BOOL antialias)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    S32 glyph_size;
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (antialias)
    {
        int i;
        for(i = 0; i<size;i+=2)
        {
            *dest++ = (src[1]&0xf0) | (src[0] >> 4);
            src+=2;
        }
        glyph_size = (size +1)>>1;
    }
    else
    {
        int i,j;
        int skip_padding = ((pitch<<3) - width)>>3;
        int shift = 0;
        int base_shift = (8-width) & 0x7;

        glyph_size = (width*height+7)>>3;

        memset(dest,0,glyph_size);
        for(i=0;i<height;i++)
        {
            for(j=0;j<width;j+=8)
            {
                U32 s = (U32)(*src++);
                if(s)
                {
                    if(shift)
                    {
                        s <<= shift;
                        *dest++ |= etrump_bits_revise_table[s>>8];
                        *dest   |= etrump_bits_revise_table[s&0xff];
                    }
                    else
                        *dest++ |= etrump_bits_revise_table[s];
                }
                else
                    dest++;
            }
            if(base_shift && !shift) dest--;
            if((shift+=base_shift) >8) dest--;
            shift&=0x7;
            src+=skip_padding;
        }
    }
    return glyph_size;
}


/*****************************************************************************
 * FUNCTION
 *  etrump_map_char
 * DESCRIPTION
 * PARAMETERS
 * RETURNS
 *****************************************************************************/
#if defined(__MMI_VECTOR_FONT_KERNING__)
U16 etrump_map_char(U16 ucs2)
#else
static U16 etrump_map_char(U16 ucs2)
#endif /* __MMI_VECTOR_FONT_KERNING__ */
{
    U16 glyph_index;

    glyph_index = FT_Get_Char_Index(etrump_face, ucs2);
    return glyph_index;
}


/*****************************************************************************
 * FUNCTION
 *  etrump_get_font_data
 * DESCRIPTION
 * PARAMETERS
 * RETURNS
 *****************************************************************************/
#if defined(__MMI_VECTOR_FONT_KERNING__)
static MMI_BOOL etrump_get_font_data(
        U8 is_need_glyph_data,
        U16 ucs2, 
        U8** glyph_data,
        U32 *glyph_size,
        U32 *glyph_attr, 
        U16 *glyph_width,
        U16 *glyph_height,
        S32 *bearing_x,
        S32 *bearing_y, 
        S32 *advance_x,
        S32 *advance_y, 
        S32 *lsb_delta,
        S32 *rsb_delta,
        MMI_BOOL* valid)
#else
static MMI_BOOL etrump_get_font_data(
        U8 is_need_glyph_data,
        U16 ucs2, 
        U8** glyph_data,
        U32 *glyph_size,
        U32 *glyph_attr, 
        U16 *glyph_width,
        U16 *glyph_height,
        S32 *bearing_x,
        S32 *bearing_y, 
        S32 *advance_x,
        S32 *advance_y, 
        MMI_BOOL* valid)
#endif /* __MMI_VECTOR_FONT_KERNING__ */
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    FT_Error    error = 0;
    U8*         bitmap_buffer = NULL;
    //S32 loop = 0;
    S32 pitch = 0;
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    etrump_reset_face_glyphs();

    //render_flag = MMI_TRUE;
    *valid = MMI_TRUE;

    if ((is_need_glyph_data != 2) && (etrump_font_attr & (FE_FONT_ATTR_ITALIC|FE_FONT_ATTR_OBLIQUE))) 
    {
        FT_Matrix matrix;
        matrix.xx = (FT_Fixed)(   1 * 0x10000L);
        matrix.xy = (FT_Fixed)(0.25 * 0x10000L);
        matrix.yx = (FT_Fixed)(   0 * 0x10000L);
        matrix.yy = (FT_Fixed)(   1 * 0x10000L);

        FT_Set_Transform(etrump_face, &matrix, NULL);
    }
    
    {
        switch (is_need_glyph_data)
        {
            case 0:
                error = FT_Load_Char(etrump_face, ucs2, FT_LOAD_DEFAULT | ((etrump_font_attr & FE_FONT_ATTR_ANTIALIAS)? 0: FT_LOAD_TARGET_MONO));
                break;
            case 1:
                error = FT_Load_Char(etrump_face, ucs2, FT_LOAD_RENDER | ((etrump_font_attr & FE_FONT_ATTR_ANTIALIAS)? 0: FT_LOAD_TARGET_MONO));
                break;
            case 2:
                if (etrump_map_char(ucs2))
                {
                    *valid = MMI_TRUE;
                }
                else
                {
                    *valid = MMI_FALSE;
                }
                //render_flag = MMI_FALSE;
                return MMI_TRUE;
                        //break;
            case 3:
                error = FT_Load_Glyph(etrump_face, ucs2, FT_LOAD_RENDER | ((etrump_font_attr & FE_FONT_ATTR_ANTIALIAS)? 0: FT_LOAD_TARGET_MONO));
                break;
        }

        if (error)
        {
            *glyph_width  = 0;
            *glyph_height = 0;
            *bearing_x = 0;
            *bearing_y = 0;
            *advance_x = 0;
            *advance_y = 0;
#if defined(__MMI_VECTOR_FONT_KERNING__)
            *lsb_delta = 0;
            *rsb_delta = 0;
#endif /* __MMI_VECTOR_FONT_KERNING__ */ 
            *glyph_size = 0;
            //render_flag = MMI_FALSE;
            if (glyph_attr) 
                *glyph_attr = FE_GLYPH_ATTR_USING_FONT_ENGINE;
            return MMI_TRUE;
        }
                
        if (etrump_font_attr & (FE_FONT_ATTR_BOLD))
        {
            error = FT_Bitmap_Embolden(etrump_library, &(etrump_face->glyph->bitmap), (FT_Pos)0x40, 0);
        }

        if(is_need_glyph_data)
        {
            *glyph_width  = (U16)etrump_face->glyph->bitmap.width;
            *glyph_height = (U16)etrump_face->glyph->bitmap.rows;
            bitmap_buffer = etrump_face->glyph->bitmap.buffer;
            pitch = etrump_face->glyph->bitmap.pitch;
        }
        else
        {
            *glyph_width  = (U16)etrump_face->glyph->metrics.width >>6;
            *glyph_height = (U16)etrump_face->glyph->metrics.height>>6;
            bitmap_buffer = NULL;
        }
        *bearing_x = etrump_face->glyph->metrics.horiBearingX>>6;
        *bearing_y = etrump_face->glyph->metrics.horiBearingY>>6;
        *advance_x = etrump_face->glyph->advance.x >> 6;
        *advance_y = etrump_face->glyph->advance.y >> 6;
#if defined(__MMI_VECTOR_FONT_KERNING__)
        *lsb_delta = etrump_face->glyph->lsb_delta;
        *rsb_delta = etrump_face->glyph->rsb_delta;
#endif /* __MMI_VECTOR_FONT_KERNING__ */ 
    }

    if (is_need_glyph_data)
    {
        int size;
        etrump_render_data_struct *D = (etrump_render_data_struct*) etrump_render_data_pool;

        size = (*glyph_width) * (*glyph_height); 

        // convert 8 bits into 4 bits
        D->normal_data_size = etrump_data_convert(&(D->data[0]), bitmap_buffer, pitch,(*glyph_width), (*glyph_height), size, (MMI_BOOL) (etrump_font_attr & FE_FONT_ATTR_ANTIALIAS));
        D->normal_data_size = (D->normal_data_size+3) & (~3);
                
        *glyph_data = (U8*)D;
        (*glyph_size) = (U32)&(D->data[D->normal_data_size]) - (U32)D;    
    }

    if (etrump_font_attr & (FE_FONT_ATTR_ITALIC|FE_FONT_ATTR_OBLIQUE))
        FT_Set_Transform(etrump_face, NULL, NULL);

    if (glyph_attr) 
        *glyph_attr = FE_GLYPH_ATTR_USING_FONT_ENGINE;
    
    //render_flag = MMI_FALSE;
    return MMI_TRUE;
}


/*****************************************************************************
 * FUNCTION
 *  etrump_show_font_data
 * DESCRIPTION
 * PARAMETERS
 * RETURNS
 *****************************************************************************/
static void etrump_show_font_data(
                            U16 ucs2, 
                            S32 x,
                            S32 y,
                            U8* glyph_ptr,
                            U32 glyph_size,
                            U32 glyph_attr,
                            U16 glyph_width,
                            U16 glyph_height,
                            U32 bordered)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    MMI_BOOL is_gray_bitmap;

    etrump_render_data_struct *D = (etrump_render_data_struct*)glyph_ptr;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

    if (glyph_ptr == NULL || glyph_size == 0 || glyph_width ==0 || glyph_height == 0) return;

    if (etrump_font_attr & FE_FONT_ATTR_ANTIALIAS)
        is_gray_bitmap = MMI_TRUE;
    else
        is_gray_bitmap = MMI_FALSE;
    
    if (etrump_font_attr & FE_FONT_ATTR_BORDER)
    {    
        {
            gdi_color temp_text_color = 0, temp_border_color = 0;

            if (bordered == 1 || bordered == 3)
            {
                temp_text_color = /*0xFF000000 | */etrump_text_color;
            }
            if (bordered == 1 || bordered == 2)
            {
                temp_border_color = /*0xFF000000 | */etrump_border_color;
            }

            gdi_show_char_bordered(
                x,
                y,
                temp_text_color,
                temp_border_color,
                D->data,
                glyph_size,
                glyph_width,
                glyph_height,
                etrump_font_attr,
                is_gray_bitmap);
        }
    }
    else if (etrump_font_attr & FE_FONT_ATTR_ENGRAVE)
    {
        gdi_show_char(
            x,
            y,
            /*0xFF000000 | */etrump_border_color,
            D->data,
            glyph_size,
            glyph_width,
            glyph_height,
            etrump_font_attr,
            is_gray_bitmap);
        gdi_show_char(
            x,
            y + 1,
            /*0xFF000000 | */etrump_text_color,
            D->data,
            glyph_size,
            glyph_width,
            glyph_height,
            etrump_font_attr,
            is_gray_bitmap);
    }
    else if (glyph_size)
    {
        gdi_show_char(
            x,
            y,
            /*0xFF000000 | */etrump_text_color,
            D->data,
            glyph_size,
            glyph_width,
            glyph_height,
            etrump_font_attr,
            is_gray_bitmap);
    }
}


/*****************************************************************************
 * FUNCTION
 *  etrump_font_stream_reader
 * DESCRIPTION
 * PARAMETERS
 * RETURNS
 *****************************************************************************/
unsigned long etrump_font_stream_reader(FT_Stream stream,unsigned long offset,unsigned char* buffer,unsigned long count)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifndef __MMI_FONT_THIRD_ROM_SUPPORT__
    MMI_ASSERT(0); // This function only support in ENFB
    return count;
#else
    if(count > 0)
    {
        if(stream->size < offset + count )
            count = stream->size - offset;
        
        offset += stream->descriptor.value;
        MMI_RESOURCE_NFB_RES_Load(ENFB_CONTENT_FONT, buffer, offset, count);
    }
    return count;
#endif
}


void etrump_font_stream_close(FT_Stream stream)
{
    // Resource didn't need to release any resource
}


/*****************************************************************************
 * FUNCTION
 *  etrump_font_file_reader
 * DESCRIPTION
 * PARAMETERS
 * RETURNS
 *****************************************************************************/
unsigned long etrump_font_file_reader(FT_Stream stream,unsigned long offset, unsigned char* buffer,unsigned long count)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    
    return mmi_fe_font_file_reader((void*)stream->descriptor.value, offset, buffer, count);
}


/*****************************************************************************
 * FUNCTION
 *  etrump_font_file_close
 * DESCRIPTION
 * PARAMETERS
 * RETURNS
 *****************************************************************************/
void etrump_font_file_close(FT_Stream stream)
{
    mmi_fe_font_file_closer((void*)stream->descriptor.value);
    stream->descriptor.value = 0;
}


  
//static FT_StreamRec g_stream[FE_MAX_FONT_FILE_NUMBER];

/*****************************************************************************
* FUNCTION
*  etrump_face_requester
* DESCRIPTION
* PARAMETERS
* RETURNS
*****************************************************************************/
FT_Error etrump_face_requester(FTC_FaceID face_id,FT_Library library,FT_Pointer request_data,FT_Face *aface) 
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    FT_Error    error = 0;
    U8*         font_data;
    S32         font_size;
    //BOOL        is_enfb;
    U16 font_res;
    S32 face_index = 0;
    mmi_fe_font_resource_type_enum type; 
    #ifdef __MMI_FONT_THIRD_ROM_SUPPORT__
    U32 offset;
    #endif

    //U32 res_id = 0;
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    //res_id = etrump_font_list[etrump_curr_font].res_id;
    font_res = etrump_font_list[etrump_curr_font].res_id;
    face_index = etrump_font_list[etrump_curr_font].face_index;

    {
        FT_Open_Args args;
        #ifdef __MMI_FONT_THIRD_ROM_SUPPORT__
        font_data = mmi_fe_get_font_resource(font_res, &type, &offset, &font_size);
        #else
        font_data = mmi_fe_get_font_resource(font_res, &type, &font_size);
        #endif
        
        switch (type)
        {
            case MMI_FE_FONT_OPEN_FILE_IVALID:
                error = FT_Err_Cannot_Open_Resource;
                return error;
                //break;
            case MMI_FE_FONT_OPEN_MEMORY:
                args.flags = FT_OPEN_MEMORY;
                args.memory_base = font_data;
                args.memory_size = font_size;
                break;
            case MMI_FE_FONT_OPEN_FILE_STREAM:
                {
                    FT_StreamRec* stream = &etrump_font_list[etrump_curr_font].stream;
                    memset(stream,0,sizeof(FT_StreamRec));
                    args.flags = FT_OPEN_STREAM;
                    args.stream = stream;

#if defined(__MMI_FE_VECTOR_FONT_ON_FILE_SYSTEM__) || defined(__MMI_VECTOR_FONT_MEMORY_CARD_SUPPORT__)
                stream->descriptor.value = (FS_HANDLE)mmi_fe_get_font_stream_handle(font_res);
#else
                    stream->descriptor.value = 0;
#endif
                    stream->size             = font_size;
                    stream->read             = etrump_font_file_reader;
                    stream->close            = etrump_font_file_close;
                }
                break;
            #ifdef __MMI_FONT_THIRD_ROM_SUPPORT__
            case MMI_FE_FONT_OPEN_ENFB_RESOURCE:
                {
                    FT_StreamRec* stream = &etrump_font_list[etrump_curr_font].stream;
                    memset(stream,0,sizeof(FT_StreamRec));
                    args.flags = FT_OPEN_STREAM;
                    args.stream = stream;

                    stream->descriptor.value = (long)offset;
                    stream->size             = font_size;
                    stream->read             = etrump_font_stream_reader;
                    stream->close            = etrump_font_stream_close;
                }
                break;
            #endif
        }

        error = FT_Open_Face(etrump_library,&args,face_index,aface);
        if (error)
            return error;

        {
            etrump_font_list[etrump_curr_font].face_index = (*aface)->face_index;
            etrump_font_list[etrump_curr_font].cmap_index = (*aface)->charmap ? FT_Get_Charmap_Index((*aface)->charmap):0;
            memcpy(etrump_font_list[etrump_curr_font].font_name, (*aface)->family_name, FREETYPE_FILE_NAME_LENGTH);
        }

        if ((*aface)->charmaps)
            (*aface)->charmap = (*aface)->charmaps[etrump_font_list[etrump_curr_font].cmap_index];
            
    }
    return error;
}


/*****************************************************************************
 * FUNCTION
 *  etrump_install_font_int
 * DESCRIPTION
 *  add font resource
 * PARAMETERS
 * RETURNS
 *****************************************************************************/
static mmi_fe_error_code etrump_install_font_int(U16 resid, etrump_font_info_p info_ptr, PU16 res_ptr, U8 region_index)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U8*         font_data;
    S32         font_size;
    mmi_fe_font_resource_type_enum type; 
    FT_Face aface = NULL;
    FT_Open_Args args;
    //S32 num_faces;
    FT_Error error;
    #ifdef __MMI_FONT_THIRD_ROM_SUPPORT__
    U32 offset;
    #endif
    /*----------------------------------------------------------------*/
    /* Code Bady                                                */
    /*----------------------------------------------------------------*/
    #ifdef __MMI_FONT_THIRD_ROM_SUPPORT__
    font_data = mmi_fe_get_font_resource(resid, &type, &offset, &font_size);
    #else
    font_data = mmi_fe_get_font_resource(resid, &type, &font_size);
    #endif
    switch (type)
    {
        case MMI_FE_FONT_OPEN_FILE_IVALID:
            return MMI_FE_FONT_ERR_INVALID_FONT;
            //break;
        case MMI_FE_FONT_OPEN_MEMORY:
            args.flags = FT_OPEN_MEMORY;
            args.memory_base = font_data;
            args.memory_size = font_size;
            break;
        case MMI_FE_FONT_OPEN_FILE_STREAM:
            {
                static FT_StreamRec stream;
                memset(&stream,0,sizeof(stream));
                args.flags = FT_OPEN_STREAM;
                args.stream = &stream;
#if defined(__MMI_FE_VECTOR_FONT_ON_FILE_SYSTEM__) || defined(__MMI_VECTOR_FONT_MEMORY_CARD_SUPPORT__)
                stream.descriptor.value = (FS_HANDLE)mmi_fe_get_font_stream_handle(resid);
#else
                stream.descriptor.value = 0;
#endif
                stream.size             = font_size;
                stream.read             = etrump_font_file_reader;
                stream.close            = etrump_font_file_close;
            }
            break;
        #ifdef __MMI_FONT_THIRD_ROM_SUPPORT__
        case MMI_FE_FONT_OPEN_ENFB_RESOURCE:
            {
                static FT_StreamRec stream;
                memset(&stream,0,sizeof(FT_StreamRec));
                args.flags = FT_OPEN_STREAM;
                args.stream = &stream;

                stream.descriptor.value = (long)offset;
                stream.size             = font_size;
                stream.read             = etrump_font_stream_reader;
                stream.close            = etrump_font_stream_close;
            }
            break;
        #endif

    }
    error = FT_Open_Face(etrump_library,&args,0,&aface);
    if (!error)
    {
        //num_faces = aface->num_faces;
        if (info_ptr)
        {
            info_ptr->res_id = resid;
            info_ptr->face_index = aface->face_index;
            info_ptr->cmap_index = aface->charmap ? FT_Get_Charmap_Index(aface->charmap):0;
            memcpy(info_ptr->font_name, aface->family_name, FREETYPE_FILE_NAME_LENGTH);
        }
        if (res_ptr)
        {
            *res_ptr = resid;
        }
#if defined(__MMI_VECTOR_FONT_MEMORY_CARD_SUPPORT__) || defined(__MMI_FE_VECTOR_FONT_ON_FILE_SYSTEM__) || defined(__MMI_FONT_THIRD_ROM_SUPPORT__) 
        etrump_set_font_info(aface, resid);
        etrump_get_unicode_region(aface, resid, region_index);
#endif
    }
    FT_Done_Face(aface);
    aface = NULL;

    if (!error)
    {
        return MMI_FE_FONT_ERR_NONE;
    }
    else
    {
        switch (error)
        {
            case FT_Err_Unknown_File_Format:
            case FT_Err_Invalid_File_Format:    
                return MMI_FE_FONT_ERR_INVALID_FONT;
                //break;
            default:
                return MMI_FE_FONT_ERR_OUT_OF_MEMORY;
        }
    }
}

/*****************************************************************************
 * FUNCTION
 *  etrump_install_font
 * DESCRIPTION
 *  install font resource
 * PARAMETERS
 * RETURNS
 *****************************************************************************/
S32 etrump_install_font(U16* font_list, U8 font_number)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_fe_error_code error;
    S32 i = 0;
    S32 count = 0;
    /*----------------------------------------------------------------*/
    /* Code Bady                                                */
    /*----------------------------------------------------------------*/
#if defined(__MMI_VECTOR_FONT_MEMORY_CARD_SUPPORT__) || defined(__MMI_FE_VECTOR_FONT_ON_FILE_SYSTEM__) || defined(__MMI_FONT_THIRD_ROM_SUPPORT__) 
    memset(unicode_region, 0, UNICODE_FONT_NUM * sizeof(font_unicode_region));
#endif

    for (i = 0; i < font_number; i++)
    {
        error = etrump_install_font_int(font_list[i], &etrump_font_list[count], &etrump_font_res_list[count], count);
        if (error == MMI_FE_FONT_ERR_NONE)
        {
            count ++;
        }
    }
    return count;
}


/*****************************************************************************
 * FUNCTION
 *  etrump_check_font_from_file
 * DESCRIPTION
 * PARAMETERS
 * RETURNS
 *****************************************************************************/
static mmi_fe_error_code etrump_check_font_from_file(U16 resid)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_fe_error_code err;
    U8 i = 0;
    /*----------------------------------------------------------------*/
    /* Code Bady                                                */
    /*----------------------------------------------------------------*/
#if defined(__MMI_VECTOR_FONT_MEMORY_CARD_SUPPORT__) || defined(__MMI_FE_VECTOR_FONT_ON_FILE_SYSTEM__)
    for (i = 0; i < UNICODE_FONT_NUM; i++)
    {
        if(!unicode_region[i].region_count)
            break;
    }
#endif
    err = etrump_install_font_int(resid, &etrump_font_list[etrump_font_number], &etrump_font_res_list[etrump_font_number], i);

    return err;
}

/*****************************************************************************
 * FUNCTION
 *  etrump_set_font
 * DESCRIPTION
 *  set the active font resource
 * PARAMETERS
 * RETURNS
 *****************************************************************************/
static MMI_BOOL etrump_set_font(U16 fontres)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    FT_Error error = 0;
    S32 i = 0;
    FT_Face aface = NULL;
    int temp_size = etrump_size;

    /*----------------------------------------------------------------*/
    /* Code Bady                                                */
    /*----------------------------------------------------------------*/
    for (i = 0; i < etrump_font_number; i ++)
    {
        if (etrump_font_list[i].res_id == fontres)
            break;
    }

    if (etrump_curr_font == i)
        return MMI_TRUE;
    
    etrump_curr_font = i;

    // if the highest priority customer font has been changed, 
    // delete it and create the new highest priority font.
    if (! is_system_font_resid(fontres))
    {
        if (hp_customer_font != fontres)
        {
            int j;

            hp_customer_font = fontres;

            for ( j = 0; j < etrump_font_number; j++)
            {
                if (etrump_font_list[j].res_id != hp_customer_font && !is_system_font_resid(etrump_font_list[j].res_id))
                {
                    U16 temp_resid = etrump_font_list[j].res_id;
#ifdef OPENTYPE_SUPPORT
                    if (etrump_font_list[j].layout)
                    {
                        FT_Done_Layout_Table(etrump_font_list[j].layout);
                    }
#endif
                    if (etrump_font_list[j].face)
                    {
                        FT_Done_Face(etrump_font_list[j].face);
                    }

                    memset(&etrump_font_list[j], 0, sizeof(etrump_font_info));

                    etrump_font_list[j].res_id = temp_resid;

                }
            }

        }
    }

    if (etrump_font_list[i].res_id == fontres)
    {
        if (etrump_font_list[i].face)
        {
            etrump_size = 0;
            etrump_face = etrump_font_list[i].face;
            etrump_scale_ratio = etrump_font_list[i].scale_ratio;
            etrump_set_font_size(temp_size, etrump_font_attr);
            return MMI_TRUE;
        }
    }

    if(etrump_font_list[i].face == NULL)
    {
        error = etrump_face_requester((FTC_FaceID)(etrump_font_list[i].res_id),etrump_library,NULL,&aface);
        if ( error )
        {
            /* can't access the font file. do not render anything */
            return MMI_FALSE;
        }

        etrump_font_list[i].face = aface;
        etrump_font_list[i].scale_ratio = (aface->units_per_EM << 6) / (aface->ascender - aface->descender);
        etrump_font_list[i].scale_ratio = etrump_font_list[i].scale_ratio * 8 / 7;

#ifdef OPENTYPE_SUPPORT
        FT_Load_Layout_Table( aface, &etrump_font_list[i].layout );
#endif //OPENTYPE_SUPPORT
    }
    
    etrump_face = etrump_font_list[i].face;
    etrump_scale_ratio = etrump_font_list[i].scale_ratio;

    etrump_size = 0;
    etrump_set_font_size(temp_size, etrump_font_attr);
    
    return MMI_TRUE;
}


/*****************************************************************************
 * FUNCTION
 *  etrump_change_family
 * DESCRIPTION
 *  to change font family
 * PARAMETERS
 * RETURNS
 *****************************************************************************/
BOOL etrump_change_family(U16 family_index)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    
    /*----------------------------------------------------------------*/
    /* Code Bady                                                */
    /*----------------------------------------------------------------*/
    return TRUE;
}


/*****************************************************************************
 * FUNCTION
 *  etrump_restart
 * DESCRIPTION
 *  to restart font engine
 * PARAMETERS
 * RETURNS
 *****************************************************************************/
#if 0
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#if defined(OPENTYPE_SUPPORT)
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif //OPENTYPE_SUPPORT
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif

/*****************************************************************************
 * FUNCTION
 *  etrump_get_font_name
 * DESCRIPTION
 * PARAMETERS
 * RETURNS
 *****************************************************************************/
mmi_fe_error_code etrump_get_font_name(U16 resid, U8* buffer, S32 buf_size)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    S32 i;
    /*----------------------------------------------------------------*/
    /* Code Bady                                                */
    /*----------------------------------------------------------------*/
    for (i = 0; i < etrump_font_number; i ++)
    {
        if (etrump_font_list[i].res_id == resid)
            break;
    }

    memcpy(buffer, etrump_font_list[i].font_name, buf_size);
    return MMI_FE_FONT_ERR_NONE;
}


/*****************************************************************************
 * FUNCTION
 *  etrump_init
 * DESCRIPTION
 * PARAMETERS
 * RETURNS
 *****************************************************************************/
static MMI_BOOL etrump_init(U16* font_list, U8 font_number)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    S32 i;
    FT_Error error = 0;
    ET_Platform platform;
    U8 sys_font_count = 0;

    /*----------------------------------------------------------------*/
    /* Code Bady                                                */
    /*----------------------------------------------------------------*/
#if MMI_FE_EXT_RESOURCE_NUM > 0
    FS_SetFileTable(gFS_FileTable_VFont);
#endif    

    //render_flag = MMI_TRUE;
    etrump_face = NULL;

    if (etrump_library == NULL) {
        memset(&platform, 0, sizeof(ET_Platform));
        ET_Mempool_Initialize(mem_pool, POOL_SIZE, &platform.alloc, &platform.free);
        ET_Platform_Initialize(&platform);
        error = FT_Init_FreeType(&etrump_library);
        MMI_ASSERT(!error);
    }

    /* destroy all faces before installing */
    for (i = 0; i < (FE_MAX_FONT_FILE_NUMBER*FE_MAX_FONT_FAMILY_NUMBER); i++ )
    {
#ifdef OPENTYPE_SUPPORT
        if (etrump_font_list[i].layout)
        {
            FT_Done_Layout_Table(etrump_font_list[i].layout);
        }
#endif
        if (etrump_font_list[i].face)
        {
            FT_Done_Face(etrump_font_list[i].face);
        }
    }
    memset(etrump_font_list, 0, sizeof(etrump_font_info)*(FE_MAX_FONT_FILE_NUMBER*FE_MAX_FONT_FAMILY_NUMBER));

    /* initialize font list */
    sys_font_count = mmi_fe_get_system_font_number();
    if (sys_font_count < font_number)
    {
        if (is_system_font_resid(font_list[0]))
            hp_customer_font = font_list[sys_font_count];
        else
            hp_customer_font = font_list[0];
    }
    else
        hp_customer_font = 0xff;

    memcpy(etrump_font_res_list, font_list, sizeof(U16)*font_number);
    etrump_font_number = font_number;    
    etrump_curr_font = 0xff; /* initialize value */

    etrump_install_font(etrump_font_res_list, etrump_font_number);
    for (i = 0; i < etrump_font_number; i++)
    {
        //etrump_font_list[i].face = NULL;
        if(is_system_font_resid(etrump_font_res_list[i]))
            etrump_set_font(etrump_font_res_list[i]);
        else if(etrump_font_res_list[i] > IMG_GLOBAL_FONT_MAX)
            sys_font_on_memory_card++;
    }
    //render_flag = MMI_FALSE;

    /* prepare bit revise table */
    {   
        S32 i;
        for (i=0;i<256;i++)
        {
            S32 j = i;
            S32 k = 0;
            k |= j&1; k<<=1;j>>=1;
            k |= j&1; k<<=1;j>>=1;
            k |= j&1; k<<=1;j>>=1;
            k |= j&1; k<<=1;j>>=1;
            k |= j&1; k<<=1;j>>=1;
            k |= j&1; k<<=1;j>>=1;
            k |= j&1; k<<=1;j>>=1;
            k |= j&1;
            
            etrump_bits_revise_table[i] = k;

        }
    }

    etrump_text_color = 0;
    etrump_border_color = 0;
    etrump_curr_font = 0xff; /* initialize value */
    return MMI_TRUE;
}	

/*****************************************************************************
 * FUNCTION
 *  etrump_reset_face_glyphs
 * DESCRIPTION
 * PARAMETERS
 * RETURNS
 *****************************************************************************/
static void etrump_reset_face_glyphs()
{
    S32 i;
    //FT_Face face;
    for (i = 0; i < etrump_font_number; i++)
    {
        // recycle memory that was used by load(render) char(glph)
        // including glyph bitmap, outline and glyph loader
        FT_Reset_Glyph(etrump_font_list[i].face);
    }

}

/*****************************************************************************
 * FUNCTION
 *  etrump_shaper
 * DESCRIPTION
 * PARAMETERS
 * RETURNS
 *****************************************************************************/
void etrump_shaper(U32 lang, U32 script, UI_string_type text, S32 text_len, S32 start_index, S32 run_len, mmi_fe_cluster_info_p cluster)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    S32 i = 0;

    /*----------------------------------------------------------------*/
    /* Code Bady                                                */
    /*----------------------------------------------------------------*/

#if defined(__MMI_INDIC_ALG__)
    mmi_fe_cluster_info pre_cluster;
    mmi_fe_cluster_info all_cluster;
    mmi_fe_cluster_info_init(&pre_cluster);
    mmi_fe_cluster_info_init(&all_cluster);
#endif

    etrump_reset_face_glyphs();

    //render_flag = MMI_TRUE;
    cluster->glyph_len = 0;

    switch( script ) {
#if defined(__MMI_LANG_THAI__)
        case MMI_FE_OT_SCRIPT_THAI:
            break;
#endif // __MMI_LANG_THAI__

#if defined(__MMI_INDIC_ALG__)
        case MMI_FE_OT_SCRIPT_DEVANAGARI:
        case MMI_FE_OT_SCRIPT_BENGALI:
        case MMI_FE_OT_SCRIPT_GUJARATI:
        case MMI_FE_OT_SCRIPT_GURMUKHI:
        case MMI_FE_OT_SCRIPT_KANNADA:
        case MMI_FE_OT_SCRIPT_MALAYALAM:
        case MMI_FE_OT_SCRIPT_ORIYA:
        case MMI_FE_OT_SCRIPT_TAMIL:
        case MMI_FE_OT_SCRIPT_TELUEU:
            break;
#endif // __MMI_INDIC_ALG__

#if defined(__MMI_LANG_LAO__)
        case MMI_FE_OT_SCRIPT_LAO:
            break;
#endif // __MMI_LANG_LAO__

#if defined(__MMI_LANG_MYANMAR__)
        case MMI_FE_OT_SCRIPT_MYANMAR:
            break;
#endif // __MMI_LANG_MYANMAR__


        default:
            for (i = 0; i < run_len; i ++)
            {
                if (!TEST_FORMAT_CHAR(text[start_index + i]))
                {
                    cluster->glyph_buf[cluster->glyph_len].glyph_index = etrump_map_char(text[start_index + i]);
                    cluster->glyph_len++;
                }
            }                 

            //render_flag = MMI_FALSE;
            return;
    }
#if defined(OPENTYPE_SUPPORT)
#if defined(__MMI_INDIC_ALG__)
    FT_Shape_OpenType_Info(etrump_font_list[etrump_curr_font].layout, text, text_len - run_len, 0, (MT_Cluster_Info)&pre_cluster);
    FT_Shape_OpenType_Info(etrump_font_list[etrump_curr_font].layout, text, text_len, 0, (MT_Cluster_Info)&all_cluster);
			   
    for (i = 0; i < all_cluster.glyph_len; i++)
    {
        memcpy(&cluster->glyph_buf[i], &all_cluster.glyph_buf[pre_cluster.glyph_len + i], sizeof(mmi_fe_glyph_ot_info));
    }
    cluster->glyph_len = all_cluster.glyph_len - pre_cluster.glyph_len;
#else
    FT_Shape_OpenType_Info(etrump_font_list[etrump_curr_font].layout, &text[start_index], run_len, 0, (MT_Cluster_Info)cluster );
#endif
#endif
	  //render_flag = MMI_FALSE;
}


/*****************************************************************************
 * FUNCTION
 *  etrump_check_script
 * DESCRIPTION
 *  to check the script and lang support
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
MMI_BOOL etrump_check_script(U32 lang, U32 script, U32 flags)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return MMI_FALSE;
}

#if defined(__MMI_VECTOR_FONT_MEMORY_CARD_SUPPORT__) || defined(__MMI_FE_VECTOR_FONT_ON_FILE_SYSTEM__) || defined(__MMI_FONT_THIRD_ROM_SUPPORT__) 
MMI_BOOL etrump_get_font_resid(U16 ucs2, U16* res_id, U8* num)
{
    int i, j;
    int idx = 0;

    for (i = 0; i < UNICODE_FONT_NUM; i++)
    {
        int r_count = unicode_region[i].region_count;

        for (j = 0; j < r_count; j++)
        {
            if (ucs2 >= unicode_region[i].start[j] && ucs2 <= unicode_region[i].end[j])
            {
                res_id[idx] = unicode_region[i].resid;
                idx++;
                break;
            }
        }

        if (idx >= *num)
            break;
    }

    *num = idx;
    if(!idx)
        return MMI_FALSE;
    else
        return MMI_TRUE;
}
#endif

#if defined(__MMI_VECTOR_FONT_KERNING__)
#define FTAutoKern_AdjustF(prev, next)   (((next) - (prev) + 32) >> 6 << 16)

/*****************************************************************************
 * FUNCTION
 *  etrump_get_font_kerning
 * DESCRIPTION
 *  
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
static MMI_BOOL etrump_get_font_kerning (U16 left_ucs2, U16 right_ucs2, signed long *x, signed long *y)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    signed long use_kerning;
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    //left_ucs2 = 0x0056;
    //right_ucs2 = 0x0041;
    *x = 0;
    *y = 0;
    use_kerning = FT_HAS_KERNING (etrump_face);

    if ( use_kerning && left_ucs2 && right_ucs2 ) 
    { 
        FT_Vector delta; 
        FT_UInt glyph_index_prev, glyph_index_curr;
        glyph_index_prev = left_ucs2;//FT_Get_Char_Index( etrump_face, left_ucs2);
        glyph_index_curr = right_ucs2;//FT_Get_Char_Index( etrump_face, right_ucs2 );

        FT_Get_Kerning( etrump_face, glyph_index_prev, glyph_index_curr, FT_KERNING_DEFAULT, &delta ); 
        *x = delta.x;
        *y = delta.y;
        return MMI_TRUE; 
    }
    return MMI_FALSE; 
}

/*****************************************************************************
 * FUNCTION
 *  etrump_has_kerning
 * DESCRIPTION
 *  
 * PARAMETERS
 *  void
 * RETURNS
 *  MMI_BOOL
 *****************************************************************************/
static MMI_BOOL etrump_has_kerning (void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    signed long use_kerning;
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    use_kerning = FT_HAS_KERNING (etrump_face);
    if (use_kerning) 
    { 
        return MMI_TRUE; 
    }
    return MMI_FALSE; 
}

#endif /* __MMI_VECTOR_FONT_KERNING__ */

const fe_table_struct font_engine_table=
{
    MMI_FE_ENGINE_VERSION_09B_FEATURE_SET,
    etrump_init,
    etrump_set_font_attr,
    etrump_set_font_size,
    etrump_set_font_color,
    etrump_get_font_data,
    etrump_show_font_data,
    etrump_get_font_info,
    etrump_set_antialias,
    etrump_set_font,
    etrump_shaper,
    etrump_check_font_from_file,
    etrump_get_font_name
#if defined(__MMI_VECTOR_FONT_MEMORY_CARD_SUPPORT__) || defined(__MMI_FE_VECTOR_FONT_ON_FILE_SYSTEM__) || defined(__MMI_FONT_THIRD_ROM_SUPPORT__) 
    ,etrump_get_font_resid
#endif
#if defined(__MMI_VECTOR_FONT_KERNING__)
    ,etrump_get_font_kerning,
    etrump_map_char,
    etrump_has_kerning
#endif /* __MMI_VECTOR_FONT_KERNING__ */ 
};


#endif