/*
 * 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 "BALConfiguration.h"
#include "BCGlyphBufferGraphicManager.h"

#include "BTLogHelper.h"

IMPLEMENT_CREATE_DELETE(BIGlyphBuffer, BCGlyphBufferGraphicManager);

namespace BAL {

bool BCGlyphBufferGraphicManager::isEmpty() const
{
    DBGML(MODULE_FONTS, LEVEL_INFO, "\nBCGlyphBufferGraphicManager::isEmpty()\n");
    return m_fontData.isEmpty();
}

int BCGlyphBufferGraphicManager::size() const
{
    DBGML(MODULE_FONTS, LEVEL_INFO, "\nBCGlyphBufferGraphicManager::size()\n");
    return m_fontData.size();
}

void BCGlyphBufferGraphicManager::clear()
{
    DBGML(MODULE_FONTS, LEVEL_INFO, "\nBCGlyphBufferGraphicManager::clear()\n"); 
    m_fontData.clear();
    m_glyphs.clear();
    m_advances.clear();
}

GlyphBufferGlyph* BCGlyphBufferGraphicManager::glyphs(int from)
{
    DBGML(MODULE_FONTS, LEVEL_INFO, "\nBCGlyphBufferGraphicManager::glyphs(%d)\n", from);
    return m_glyphs.data() + from;
}

const GlyphBufferGlyph* BCGlyphBufferGraphicManager::glyphs(int from) const
{
    DBGML(MODULE_FONTS, LEVEL_INFO, "\nBCGlyphBufferGraphicManager::glyphs(%d) const\n", from);
    return m_glyphs.data() + from;
}

GlyphBufferAdvance* BCGlyphBufferGraphicManager::advances(int from)
{
    DBGML(MODULE_FONTS, LEVEL_INFO, "\nBCGlyphBufferGraphicManager::advances(%d)\n", from);
    return m_advances.data() + from;
}

const GlyphBufferAdvance* BCGlyphBufferGraphicManager::advances(int from) const
{
    DBGML(MODULE_FONTS, LEVEL_INFO, "\nBCGlyphBufferGraphicManager::advances(%d) const\n", from);
    return m_advances.data() + from;
}

const BTFontData* BCGlyphBufferGraphicManager::fontDataAt(int index) const
{
    DBGML(MODULE_FONTS, LEVEL_INFO, "\nBCGlyphBufferGraphicManager::fontDataAt(%d) const\n", index);
    return m_fontData[index];
}

void BCGlyphBufferGraphicManager::swap(int index1, int index2)
{
    DBGML(MODULE_FONTS, LEVEL_INFO, "\nBCGlyphBufferGraphicManager::swap()\n");
    const BTFontData* f = m_fontData[index1];
    m_fontData[index1] = m_fontData[index2];
    m_fontData[index2] = f;

    GlyphBufferGlyph g = m_glyphs[index1];
    m_glyphs[index1] = m_glyphs[index2];
    m_glyphs[index2] = g;

    GlyphBufferAdvance s = m_advances[index1];
    m_advances[index1] = m_advances[index2];
    m_advances[index2] = s;
}

Glyph BCGlyphBufferGraphicManager::glyphAt(int index) const
{
    DBGML(MODULE_FONTS, LEVEL_INFO, "\nBCGlyphBufferGraphicManager::glyphAt(%d) const\n", index);
    return m_glyphs[index];
}

float BCGlyphBufferGraphicManager::advanceAt(int index) const
{
    DBGML(MODULE_FONTS, LEVEL_INFO, "\nBCGlyphBufferGraphicManager::advanceAt(%d) const = %f\n", index, m_advances[index].width());
    return m_advances[index].width();
}

FloatSize BCGlyphBufferGraphicManager::offsetAt(int index) const
{
    DBGML(MODULE_FONTS, LEVEL_INFO, "\nBCGlyphBufferGraphicManager::offsetAt(%d) const\n", index);
    return FloatSize();
}

void BCGlyphBufferGraphicManager::add(Glyph glyph, const BTFontData* font, float width, const FloatSize* offset)
{
    DBGML(MODULE_FONTS, LEVEL_INFO, "\nBCGlyphBufferGraphicManager::add('%c')\n", glyph);
    m_fontData.append(font);
    m_glyphs.append(glyph);
    m_advances.append(FloatSize(width, 0));
}

} //namespace BAL
