<?php

/*******************************************************************************

 WINBINDER - A native Windows binding for PHP

 Copyright  2004-2005 Hypervisual - see LICENSE.TXT for details
 Author: Rubem Pechansky (http://www.hypervisual.com/winbinder/contact.php)

 Code sample: TrueType data reader application

*******************************************************************************/

//------------------------------------------------------------ SYSTEM PARAMETERS

define("PATH_SCRIPT",   dirname(__FILE__) . "/");
define("PATH_DATA",     PATH_SCRIPT);
define("PATH_INC",      PATH_SCRIPT "../include/");
define("PATH_RES",      PATH_SCRIPT "../resources/");

//----------------------------------------------------------------- DEPENDENCIES

include PATH_INC "winbinder.php";

//-------------------------------------------------------------------- CONSTANTS

define("APPNAME",           "TrueType Data Reader");
define("VERSION",           "0.17");

// Identifiers for main window

define("ID_ABOUT",      1001);
define("ID_CLEAR",      1002);
define("ID_OPEN",       1003);
define("ID_SELECT",     1004);
define("ID_WEBSITE",    1006);
define("ID_VENDORID",   1051);
define("ID_FONTNAME",   1052);
define("ID_FAMILY",     1053);
define("ID_SUBFAMILY",  1054);
define("ID_PSNAME",     1055);
define("ID_UNIQUEID",   1056);
define("ID_COPYRIGHT",  1057);
define("ID_TRADEMARK",  1058);
define("ID_VERSION",    1059);
define("ID_WEIGHT",     1060);

// Control identifiers for Select Font dialog

define("ID_SYSFONT",    2001);

//-------------------------------------------------------------- EXECUTABLE CODE

create_main_window();
wb_main_loop();

//-------------------------------------------------------- MAIN WINDOW FUNCTIONS

/* Create main window */

function create_main_window()
{
    global 
$mainwin$toolbar$mainmenu;

    
// Create main window (invisible) and controls

    
$mainwin wb_create_window(NULLAppWindow"TTF Data"WBC_CENTERWBC_CENTER475370WBC_INVISIBLE);

    
// Insert controls

    
wb_create_control($mainwinFrame"", -129524300);

    
wb_create_control($mainwinFrame"Font data"104244914400);
    
wb_create_control($mainwinLabel"Full font name:"136694210WBC_RIGHT);
    
wb_create_control($mainwinEditBox""1196332521ID_FONTNAMEWBC_READONLY);
    
wb_create_control($mainwinLabel"Family / weight:"139594160WBC_RIGHT);
    
wb_create_control($mainwinEditBox""1199216021ID_FAMILYWBC_READONLY);
    
wb_create_control($mainwinEditBox""286926521ID_SUBFAMILYWBC_READONLY);
    
wb_create_control($mainwinEditBox""356928821ID_WEIGHTWBC_READONLY);
    
wb_create_control($mainwinLabel"PostScript name:"1712591210WBC_RIGHT);
    
wb_create_control($mainwinEditBox""11912332521ID_PSNAMEWBC_READONLY);
    
wb_create_control($mainwinLabel"Unique font ID:"1715491210WBC_RIGHT);
    
wb_create_control($mainwinEditBox""11915232521ID_UNIQUEIDWBC_READONLY);

    
wb_create_control($mainwinFrame"Identifiers and legal information"101954491200WBC_ENABLED);
    
wb_create_control($mainwinLabel"Vendor ID:"1622294190WBC_RIGHT);
    
wb_create_control($mainwinEditBox""11921910621ID_VENDORIDWBC_READONLY);
    
wb_create_control($mainwinLabel"Version:"23522267190WBC_RIGHT);
    
wb_create_control($mainwinEditBox""30521913921ID_VERSIONWBC_READONLY);
    
wb_create_control($mainwinLabel"Copyright:"1625192160WBC_RIGHT);
    
wb_create_control($mainwinEditBox""11925032521ID_COPYRIGHTWBC_READONLY);
    
wb_create_control($mainwinLabel"Trademark:"1628192160WBC_RIGHT);
    
wb_create_control($mainwinEditBox""11927932521ID_TRADEMARKWBC_READONLY);

    
// Create main menu

    
$mainmenu wb_create_control($mainwinMenu, array(
    
"&Font",
        array(
ID_OPEN,      "&Open Font File...\tCtrl+O"""PATH_RES "menu_open.bmp""Ctrl+O"),
        array(
ID_SELECT,    "Select Installed &Font...\tCtrl+F""""""Ctrl+F"),
        
null,
        array(
ID_CLEAR,     "&Clear Data\tCtrl+Del""""""Ctrl+Del"),
        
null,
        array(
IDCLOSE,      "E&xit\tAlt+F4"""PATH_RES "menu_exit.bmp"),

    
"&Help",
        array(
ID_WEBSITE,   "&Web site"),
        
null,
        array(
ID_ABOUT,     "&About..."""PATH_RES "menu_help.bmp"),

    ), 
$mainwin);

    
// Create toolbar

    
$toolbar wb_create_control($mainwinToolBar, array(
        
null,
        array(
ID_OPEN,      NULL"Open Font File (Ctrl+O)",        1),
        array(
ID_SELECT,    NULL"Select Installed Font (Ctrl+F)"5),
        array(
ID_CLEAR,     NULL"Clear Data",                     12),
        
null,
        array(
ID_WEBSITE,   NULL"Go to the WinBinder web site",   11),
    ), 
00161500PATH_RES "toolbar.bmp");

    
clear_screen($mainwin);

    
// Show window

    
wb_set_image($mainwinPATH_RES "ttf.ico");
    
wb_set_handler($mainwin"process_main");
    
wb_set_text($mainwinAPPNAME);
    
wb_set_visible($mainwintrue);
}

/* Main window handler */

function process_main($window$id)
{
    static 
$font_mask = array(
        array(
"TrueType Fonts",     "*.ttf"),
        array(
"All files",          "*.*")
    );

    switch(
$id) {

        case 
ID_CLEAR:
            
clear_screen($window);
            break;

        case 
ID_OPEN:
            
$file wb_sys_dlg_open($window"Open Font File"$font_mask);
            if(
$file)
                
get_ttf_values($file);
            break;

        case 
ID_SELECT:
            
create_select_dlg($window);
            break;

        case 
ID_WEBSITE:
            if(!
wb_exec("http://hypervisual.com/winbinder/"))
                
wb_message_box($window"Problem opening web site.");
            break;

        case 
ID_ABOUT:
            
wb_message_box($window,
                
"WinBinder version " WBC_VERSION "\nPHP version " PHP_VERSION .  "\n" wb_get_system_info("osversion"),
                
"About " APPNAME " v. " VERSION);
            break;

        case 
IDCLOSE:
            
wb_destroy_window($window);
            break;
    }
}

/* Clear data fields */

function clear_screen($window)
{
    global 
$toolbar$mainmenu;

    
$controls = array(ID_WEIGHTID_VENDORIDID_FONTNAMEID_FAMILYID_SUBFAMILY,
        
ID_UNIQUEIDID_PSNAMEID_COPYRIGHTID_VERSIONID_TRADEMARK);

    foreach(
$controls as $id)
        
wb_set_text(wb_get_control($window$id), "");

    
// Enable controls

    
wb_set_enabled(wb_get_control($toolbarID_CLEAR), false);
    
wb_set_enabled(wb_get_control($mainmenuID_CLEAR), false);

    
// Reset title bar string

    
wb_set_text($windowAPPNAME);
}

//------------------------------------------------- FONT SELECT DIALOG FUNCTIONS

// Create the Select Font dialog

function create_select_dlg($parent)
{
    global 
$filedir;

    
// Create window

    
$seldlg wb_create_window($parentModalDialog"Select a font file"WBC_CENTERWBC_CENTER211232WBC_INVISIBLE WBC_NOTIFYWBC_DBLCLICK);

    
// Insert controls

    
wb_create_control($seldlgListBox,    "",         4,  19200160ID_SYSFONT);
    
wb_create_control($seldlgPushButton"OK",      22175,  70,  26IDOK);
    
wb_create_control($seldlgPushButton"Cancel"109175,  70,  26IDCANCEL);

    
// Fill up listbox with the list of installed font files

    
$listctrl wb_get_control($seldlgID_SYSFONT);
    
$filedir wb_get_system_info("fontpath");
    
$files get_folder_files($filedirfalsefalse"/.*\.ttf/i");
    
sort($files);
    
wb_set_text($listctrl$files);

    
// Show dialog

    
wb_set_handler($seldlg"process_select_dlg");
    
wb_set_focus($listctrl);
    
wb_set_enabled(wb_get_control($seldlgIDOK), false);
    
wb_set_visible($seldlgtrue);
}

// Window handler

function process_select_dlg($window$id$ctrl=0$lparam=0)
{
    global 
$filedir;

    switch(
$id) {

        case 
ID_SYSFONT:
            if(
$lparam != WBC_DBLCLICK) {
                
wb_set_enabled(wb_get_control($windowIDOK), true);
                return 
true;
            }
            
// Fall-through

        
case IDOK:
            
$font wb_get_text(wb_get_control($windowID_SYSFONT));
            if(
$font)
                
get_ttf_values($filedir $font);

            
// Fall-through

        
case IDCANCEL:
            
wb_destroy_window($window);
            return 
true;

    }
    return 
false;
}

//------------------------------------------------------ TRUETYPE DATA FUNCTIONS

/* Read TTF values from file $fontfile and fill out data fields */

function get_ttf_values($fontfile)
{
    global 
$mainwin$toolbar$mainmenu;

    static 
$weights = array(
        
"Don't care" => 0,
        
"Thin"=>        100,
        
"Extra Light"=> 200,
        
"Light"=>       300,
        
"Normal"=>      400,
        
"Medium"=>      500,
        
"Demi Bold"=>   600,
        
"Bold"=>        700,
        
"Extra Bold"=>  800,
        
"Black"=>       900,
    );

    static 
$stringnames = array(
        
"COPYRIGHT",
        
"FACENAME",
        
"SUBFAMILY",
        
"UNIQUEFONTID",
        
"FAMILY",
        
"VERSION",
        
"PSNAME",
        
"TRADEMARK",
        
"UC_COPYRIGHT",
        
"UC_FAMILY",
        
"UC_SUBFAMILY",
        
"UC_UNIQUEFONTID",
        
"UC_FACENAME",
        
"UC_VERSION",
        
"UC_PSNAME",
        
"UC_TRADEMARK"
    
);

    
//---------------------------------------- Open TTF file

    
$fh fopen($fontfile"rb");
    if(!
$fh) {
        
wb_message_box($mainwin"Cannot open file $fontfile.");
        return;
    }

    
//---------------------------------------- Read file and header data

    
fseek($fh0);
    
$data fread($fh12);
    
$ttf_head unpack("nsfntVersion" $data);
    if(
$ttf_head["sfntVersion"] != 1) {
        
wb_message_box($mainwin"The file $fontfile does not seem to be a TrueType font.");
        return;
    }

    
//---------------------------------------- Read the offsets

    
fseek($fh0);
    
$data fread($fh1024);
    
$os2pos strpos($data"OS/2");
    
$namepos strpos($data"name");
    
$cmappos strpos($data"cmap");

    
//---------------------------------------- Read "name" data

    
fseek($fh$namepos);
    
$data fread($fh16);
    
$ttf_name unpack("a4/N/Noffset"$data);
    
fseek($fh$ttf_name["offset"]);
    
$data fread($fh6);
    
$ttf_name unpack("n/ncount/nstringoffset"$data);
    
$strstart ftell($fh) - $ttf_name["stringoffset"];

    
//---------------------------------------- Names loop

    
$names min($ttf_name["count"], 16);
    for(
$i 0$i $names$i++) {
        
$data fread($fh12);
        
$fpos ftell($fh);
        
$ttf_name unpack("n4/nLengthID/nOffsetID"$data);
        
$len $ttf_name["LengthID"];
        if(
$len) {
            
fseek($fh$strstart $ttf_name["OffsetID"]);
            
$data fread($fh$len);
            
fseek($fh$fpos);
            
$name unpack("a" $len "Data"$data);
            
$ttf_strings[$stringnames[$i]] = unicode2ansi($name["Data"]);
        } else {
            
$ttf_strings[$stringnames[$i]] = "";
        }
    }

    
//---------------------------------------- Read "os2" data

    
fseek($fh$os2pos);
    
$data fread($fh16);
    
$ttf_os2 unpack("a4id/N/Noffset/Nlength"$data);
    
fseek($fh$ttf_os2["offset"]);
    
$data fread($fh$ttf_os2["length"]);
    
$ttf_os2 unpack("n2/nusWeightClass/n26/a4achVendID"$data);

    
//---------------------------------------- End data read

    
fclose($fh);

    
//---------------------------------------- Fiil out controls with values

    // OS/2 data

    
wb_set_text(wb_get_control($mainwinID_WEIGHT),    array_search($ttf_os2["usWeightClass"], $weights));
    
wb_set_text(wb_get_control($mainwinID_VENDORID),  $ttf_os2["achVendID"]);

    
// Strings

    
wb_set_text(wb_get_control($mainwinID_FONTNAME),  $ttf_strings["FACENAME"]);
    
wb_set_text(wb_get_control($mainwinID_FAMILY),    @$ttf_strings["FAMILY"]);
    
wb_set_text(wb_get_control($mainwinID_SUBFAMILY), @$ttf_strings["SUBFAMILY"]);
    
wb_set_text(wb_get_control($mainwinID_UNIQUEID),  @$ttf_strings["UNIQUEFONTID"]);
    
wb_set_text(wb_get_control($mainwinID_PSNAME),    @$ttf_strings["PSNAME"]);
    
wb_set_text(wb_get_control($mainwinID_COPYRIGHT), @$ttf_strings["COPYRIGHT"]);
    
wb_set_text(wb_get_control($mainwinID_VERSION),   @$ttf_strings["VERSION"]);
    
wb_set_text(wb_get_control($mainwinID_TRADEMARK), @$ttf_strings["TRADEMARK"]);

    
// Enable controls

    
wb_set_enabled(wb_get_control($toolbarID_CLEAR), true);
    
wb_set_enabled(wb_get_control($mainmenuID_CLEAR), true);

    
// Set title bar with font name

    
wb_set_text($mainwinbasename($fontfile) . " - " APPNAME);
}

//---------------------------------------------------- GENERAL-PURPOSE FUNCTIONS

// utf8_decode dos not work for TrueType fonts

function unicode2ansi($string)
{
    
$string = (string)$string;

    if(!
is_string($string))
        return 
$string;

    
// Is it an empty string?

    
if(strlen($string) == 0)
        return 
$string;

    
// Is it Unicode?

    
if(ord($string[0]) != 0)
        return 
$string;

    
// Do the conversion

    
$len strlen($string);
    
$out "";

    for(
$i 0$i $len$i += 2) {
        if(
ord($string[$i]) == && ord($string[$i 1]) != 0)
            
$out .= $string[$i 1];
    }
    return 
$out;
}

//-------------------------------------------------------------------------- END

?>