#include "FontGlyph.h"

void GlyphRunOutlines::addSegment(const GlyphRunSegment& segment)
{
    mFeatures[mFeatures.size() - 1].mSegments.push_back(segment);
}

void GlyphRunOutlines::startFeature(const Point& point, bool isFilled)
{
    GlyphRunFeature feature;
    feature.mFilled = isFilled;

    GlyphRunSegment segment;
    segment.mType = GlyphRunSegment::Type::POINT;
    segment.mPoints.push_back(point);

    feature.mSegments.push_back(segment);

    mFeatures.push_back(feature);
}

std::string GlyphRunOutlines::toPostScriptPath()
{
    std::string path;
    for (const auto& feature : mFeatures)
    {
        if (feature.mSegments.empty())
        {
            continue;
        }

        auto start_point = feature.mSegments[0].mPoints[0];
        path += "M" + std::to_string(start_point.getX()) + " " + std::to_string(start_point.getY()) + " ";

        for (std::size_t idx = 1; idx < feature.mSegments.size(); idx++)
        {
            if (feature.mSegments[idx].mType == GlyphRunSegment::Type::LINE)
            {
                for (const auto& point : feature.mSegments[idx].mPoints)
                {
                    path += "L" + std::to_string(point.getX()) + " " + std::to_string(point.getY()) + " ";
                }
            }
            else if (feature.mSegments[idx].mType == GlyphRunSegment::Type::BEZIER)
            {
                path += "C";
                for (const auto& point : feature.mSegments[idx].mPoints)
                {
                    path += std::to_string(point.getX()) + " " + std::to_string(point.getY()) + " ";
                }
            }     
        }
        path += "z ";
    }
    return path;
}

FontGlyph::FontGlyph(unsigned width, unsigned height, int bearingX, int bearingY, int advanceX, std::unique_ptr<Image> image)
    :
      mWidth(width),
      mHeight(height),
      mBearingX(bearingX),
      mBearingY(bearingY),
      mAdvanceX(advanceX),
      mImage(std::move(image))
{

}

Image* FontGlyph::getImage() const
{
    return mImage.get();
}

unsigned FontGlyph::getWidth() const
{
    return mWidth;
}

unsigned FontGlyph::getHeight() const
{
    return mHeight;
}

int FontGlyph::getBearingX() const
{
    return mBearingX;
}

int FontGlyph::getBearingY() const
{
    return mBearingY;
}

int FontGlyph::getAdvanceX() const
{
    return mAdvanceX;
}
