/*
 * Copyright (C) Research In Motion Limited 2009. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "config.h"
#include "FontCascade.h"

#include "FloatRect.h"
#include "GraphicsContext.h"
#include "NotImplemented.h"
#include "PainterOpenVG.h"
#include "SurfaceOpenVG.h"
#include "GlyphBuffer.h"
#include "FontPlatformData.h"

#include <openvg.h>

namespace WebCore {

#define SYNTHETIC_OBLIQUE_ANGLE 14

void FontCascade::drawGlyphs(GraphicsContext* context, const Font* fontData, const GlyphBuffer& glyphBuffer,
                      int from, int numGlyphs, const FloatPoint& point) const
{
	if(context->paintingDisabled())
		return;

    PainterOpenVG* painter = context->platformContext()->activePainter();

    if(!painter)
    	return;

    Vector<VGuint, 128> characters(numGlyphs);
    Vector<VGfloat, 128> adjustmentsX(numGlyphs);
    VGfloat* adjustmentsY = 0;
    characters[0] = glyphBuffer.glyphAt(from);
    fontData->platformData().uploadGlyph(characters[0]);
    adjustmentsX[0] = 0.0f;

    painter->save();
    if (fontData->platformData().italic()) {
        painter->translate(point.x(), point.y());
    	painter->shear(-tanf(SYNTHETIC_OBLIQUE_ANGLE * acosf(0.0f) / 90.0f),0.0f);
        painter->translate(-point.x(), -point.y());
    }
    for (int i = 1; i < numGlyphs; ++i) {
        characters[i] = glyphBuffer.glyphAt(from + i);
        fontData->platformData().uploadGlyph(characters[i]);

        // The glyphbuffr advances do not necessarily match the OpenVG
        // escapement values so we account for the difference here.
        adjustmentsX[i-1] = glyphBuffer.advanceAt(from + i) - fontData->platformData().widthForGlyph(characters[i]);

        // The Cairo font renderer currently does not support kerning
        // so to match its behaviour we disable OpenVG kerning.
        //adjustmentsX[i - 1] += fontData->platformData().kerningDistance(characters[i - 1], characters[i]);
    }

    painter->drawText(fontData->platformData().vgFont(), characters.size(), characters.data(),
                      adjustmentsX.data(), adjustmentsY, point);
    if (fontData->platformData().bold()) {
    	float offset = fontData->platformData().syntheticBoldOffset();
    	painter->translate(offset, 0.0f);
        painter->drawText(fontData->platformData().vgFont(), characters.size(), characters.data(),
                          adjustmentsX.data(), adjustmentsY, point);
    }
    painter->restore();
}

}

