/*
 * This file is part of libtqtvd_astronav
 *
 * Copyright (C) 2008-2017 TQTVD Software
 * Copyright (C) 2017 Mirakulo Software (mirakulo@mirakulo.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

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

#include "BCFontPlatformDataPrivateGraphicManager.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 "BCFontPlatformDataPrivateGraphicManager.h"
#include <font/BTFontPlatformData.h>
#include "IGraphicManagerAdapter.h"

namespace BAL {

void BTFontData::platformInit()
{
    DBGML(MODULE_FONTS, LEVEL_INFO, "\nBTFontData::platformInit()\n");
    IGraphicManagerAdapterFont * font = static_cast<BCFontPlatformDataPrivateGraphicManager *> (m_font.impl())->getFont ();
    if (font) {
        int spaceWidth;
        ARectangle rect;
 
        m_ascent = font->GetAscender();
        m_descent = font->GetDescender() * (-1);
        m_lineSpacing = font->GetHeight();

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

    DBGML(MODULE_FONTS, LEVEL_INFO, "BTFontData::platformInit:\n");
    DBGML(MODULE_FONTS, LEVEL_INFO, "\tm_ascent = %d - m_descent = %d\n", m_ascent, m_descent);
    DBGML(MODULE_FONTS, LEVEL_INFO, "\tm_lineSpacing = %d - m_lineGap = %d\n", m_lineSpacing, m_lineGap);
    DBGML(MODULE_FONTS, LEVEL_INFO, "\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");
    IGraphicManagerAdapterFont * font = static_cast<BCFontPlatformDataPrivateGraphicManager *> (m_font.impl())->getFont ();

    if (! font)
    {
        printf ("BTFontData::containsCharacters: Could not find font.\n");
        return false;
    }
    else
    {
        return font->ContainsCharacters((char *)(characters), length);
    }
}

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);
    IGraphicManagerAdapterFont * font = static_cast<BCFontPlatformDataPrivateGraphicManager *> (m_font.impl())->getFont ();
    ARectangle rect ( -1, -1, -1, -1 );
    int advance;

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

    if(font->GetGlyphExtents(glyph, &rect, &advance) != 0)
        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;
}

}
