// ************************************************************************************************************
// Author: Marcin Załęczny
// Release Date: 2010-11-08
// Licence: The GNU Lesser General Public License, version 3.0 (LGPLv3)
// ************************************************************************************************************
#include "mzstdxpsCommon.h"
#include <math.h>
#include "mzString.h"
#include "mzFont.h"





VOID WINAPI XpsMeasureString(WCHAR* text, float fontSize, WCHAR* styleSimulation, WCHAR* fontName, float* tw, float* th) {
    float sum_width = 0;
    float cur_height = 0;
    
    INT len = wcslen(text);
    WCHAR ch;
    
    if (th) {
        *th = 0;
    }
    if (tw) {
        *tw = 0;
    }
    
    float factorUnitW = 0.1432;
    float factorUnitH = 0.1328;
    for (INT i = 0; i < len; i++) {
        ch = text[i];
        if (isCharUpper(ch)) {
            if (ch == L'W') {
                sum_width += fontSize - (factorUnitW-0.20)*fontSize;
                cur_height = fontSize - (factorUnitH-0.2)*fontSize;
            } else if (ch == L'P') {
                sum_width += fontSize - (factorUnitW+0.20)*fontSize;
                cur_height = fontSize - (factorUnitH-0.2)*fontSize;
            } else {
                sum_width += fontSize - factorUnitW*fontSize;
                cur_height = fontSize - factorUnitH*fontSize;
            }
        } else if (isCharLower(ch)) {
            sum_width += fontSize - 3*factorUnitW*fontSize;
            cur_height = fontSize - 2*factorUnitH*fontSize;
        } else if (isCharDigit(ch)) {
            sum_width += fontSize - 3*factorUnitW*fontSize;
            cur_height = fontSize - factorUnitH*fontSize;
        } else if (ch == L' ') {
            sum_width += fontSize - 4*factorUnitW*fontSize;
            cur_height = factorUnitH*fontSize;
        } else if (ch == L'.') {
            sum_width += fontSize - 5*factorUnitW*fontSize;
            cur_height = 2*factorUnitH*fontSize;
        } else {
            sum_width += fontSize - 3*factorUnitW*fontSize;
            cur_height = fontSize - factorUnitH*fontSize;
        }
    }
    
    if (th) {
        *th = cur_height;
    }
    if (tw) {
        *tw = sum_width;
    }
}







#ifdef MZSTDXPS_DLL
#ifdef MZSTDXPS_INTERNAL
// Memory management functions we are compiling only if dll library is to be build
VOID* __cdecl operator new(size_t size) {
    HANDLE heap = GetProcessHeap();
    if (!heap) {
        return NULL;
    }
    VOID* pv = HeapAlloc(heap, 0, size);
    if (!pv) {
        return NULL;
    }
    return pv;
}

VOID* __cdecl operator new(size_t size, CHAR ch) {
    HANDLE heap = GetProcessHeap();
    if (!heap) {
        return NULL;
    }
    VOID* pv = HeapAlloc(heap, 0, size);
    if (!pv) {
        return NULL;
    }
    memset(pv, ch, size);
    return pv;
}

VOID* __cdecl operator new[](size_t size) {
    HANDLE heap = GetProcessHeap();
    if (!heap) {
        return NULL;
    }
    VOID* pv = HeapAlloc(heap, 0, size);
    if (!pv) {
        return NULL;
    }
    return pv;
}

VOID  __cdecl operator delete(VOID* p) {
    HANDLE heap = GetProcessHeap();
    if (!heap) return;
    HeapFree(heap, 0, p);
}

VOID  __cdecl operator delete[](VOID* p) {
    HANDLE heap = GetProcessHeap();
    if (!heap) return;
    HeapFree(heap, 0, p);
}
#endif // MZSTDXPS_DLL
#endif // MZSTDXPS_INTERNAL
