#include "kjs/wk_config.h"
#include <font/BTFontData.h>

#include "BCFontPlatformDataPrivateDirectFB.h"
#include "BTLogHelper.h"
#include <graphics/FloatRect.h>
#include <font/Font.h>
#include <font/FontCache.h>
#include <font/FontDescription.h>
#include <font/GlyphBuffer.h>
#ifdef __OWB__
#include "BIInternationalization.h"
#else
#include <unicode/uchar.h>
#include <unicode/unorm.h>
#endif //__OWB__
#include <wtf/MathExtras.h>

#include "BCFontPlatformDataPrivateDirectFB.h"
#include <font/BTFontPlatformData.h>
#include <directfb.h>

namespace BAL {

void BTFontData::platformInit()
{
    DBGML(MODULE_FONTS, LEVEL_INFO, "\nBTFontData::platformInit()\n");
    IDirectFBFont * font = static_cast<BCFontPlatformDataPrivateDirectFB *> (m_font.impl())->getDFBFont ();
    if (font) {
        int spaceWidth;
        DFBRectangle rect;

        font->GetAscender(font, &m_ascent);
        font->GetDescender(font, &m_descent); m_descent *= (-1);
        font->GetHeight(font, &m_lineSpacing);

        font->GetGlyphExtents(font, 'x', &rect, &spaceWidth);
        m_xHeight = (float) rect.h;
        m_spaceWidth = (float) spaceWidth;
        m_lineGap = m_lineSpacing - m_ascent + m_descent;
    }
    else
    {
        DirectFBError ("BTFontData::platformInit: Could not find font.\n", DFB_OK);
    }

    DBGML(MODULE_FONTS, LEVEL_INFO, "\nBTFontData::platformInit:\n");
    DBGML(MODULE_FONTS, LEVEL_INFO, "\n\tm_ascent = %d - m_descent = %d\n", m_ascent, m_descent);
    DBGML(MODULE_FONTS, LEVEL_INFO, "\n\tm_lineSpacing = %d - m_lineGap = %d\n", m_lineSpacing, m_lineGap);
    DBGML(MODULE_FONTS, LEVEL_INFO, "\n\tm_xHeight = %f - m_spaceWidth = %f\n", m_xHeight, m_spaceWidth);
}

void BTFontData::platformDestroy()
{
    DBGML(MODULE_FONTS, LEVEL_INFO, "\nBTFontData::platformDestroy()\n");
    if (m_smallCapsFontData)
        delete m_smallCapsFontData;
    m_smallCapsFontData = NULL;
}

BTFontData* BTFontData::smallCapsFontData(const FontDescription& fontDescription) const
{
    DBGML(MODULE_FONTS, LEVEL_INFO, "\nBTFontData::smallCapsFontData()\n");
    if (!m_smallCapsFontData) {
        FontDescription desc = FontDescription(fontDescription);
        desc.setSpecifiedSize(0.70f*fontDescription.computedSize());
        const FontPlatformData* pdata = new FontPlatformData(desc, desc.family().family());
        m_smallCapsFontData = new FontData(*pdata);
    }
    return m_smallCapsFontData;
}

bool BTFontData::containsCharacters(const UChar* characters, int length) const
{
    DBGML(MODULE_FONTS, LEVEL_INFO, "\nBTFontData::containsCharacters()\n");
    IDirectFBFont * font = static_cast<BCFontPlatformDataPrivateDirectFB *> (m_font.impl())->getDFBFont ();
    DFBTextEncodingID ret_encoding;
    DFBResult err;

    if (!font)
    {
        DirectFBError ("BTFontData::containsCharacters: Could not find font.\n", DFB_OK);
        return false;
    }

    for (int i = 0; i < length; i++) {
        if ((err = font->FindEncoding(font, (char *)(characters[i]), &ret_encoding)) != DFB_OK) {
            return false;
        }
    }
    return true;
}

void BTFontData::determinePitch()
{
    DBGML(MODULE_FONTS, LEVEL_INFO, "\nBTFontData::determinePitch()\n");
    m_treatAsFixedPitch = m_font.isFixedPitch();
}

float BTFontData::platformWidthForGlyph(Glyph glyph) const
{
    DBGML(MODULE_FONTS, LEVEL_INFO, "\nBTFontData::platformWidthForGlyph('%c')\n", glyph);
    IDirectFBFont * font = static_cast<BCFontPlatformDataPrivateDirectFB *> (m_font.impl())->getDFBFont ();
    DFBRectangle rect = { -1, -1, -1, -1 };
    int advance;

    if(! font)
    {
        DirectFBError ("BTFontData::platformWidthForGlyph: Could not find font.\n", DFB_OK);
        return m_spaceWidth;
    }

    if(font->GetGlyphExtents(font, glyph, &rect, &advance) != DFB_OK)
        return m_spaceWidth;

    DBGML(MODULE_FONTS, LEVEL_INFO, "\n\tx=%d, y=%d, w=%d, h=%d, advance=%d\n", rect.x, rect.y, rect.w, rect.h, advance); fflush(stdout);

    if(advance)
        return (float)(advance);

    return m_spaceWidth;
}

}
