/// Copyright (c) 2009 Ruben Penalva. All rights reserved.
/// Redistribution and use in source and binary forms, with or without modification, 
/// are permitted provided that the following conditions are met:
///    1. Redistributions of source code must retain the above copyright notice, this 
///    list of conditions and the following disclaimer.
///    2. Redistributions in binary form must reproduce the above copyright notice,
///    this list of conditions and the following disclaimer in the documentation and/or
///    other materials provided with the distribution.
///    3. The name of the author may not be used to endorse or promote products derived
///    from this software without specific prior written permission.
///
/// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
/// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
/// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 
/// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
/// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
/// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
/// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 
/// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

/// @file GlyphOutline.h
/// This source file is part of LetterCDT test.
/// The file holds the GlyphOutline class interface.
/// @author Ruben Penalva (ruben.penalva@rpenalva.com)
#ifndef _GLYPH_OUTLINE_H_
#define _GLYPH_OUTLINE_H_

#include <ft2build.h>  
#include FT_FREETYPE_H 

#include <vector>

#include "Point.h"
#include "Line.h"

namespace LetterCDT
{

enum ContourType
{
	Inner,
	Outer
};

/// @brief Loads a glyph outline from a freetype face
class GlyphOutline
{
public:
	/// Constructor
	/// @param charCode the character code
	/// @param face freetype face from where it has to
	/// loaded the character outline
	GlyphOutline(wchar_t charCode, FT_Face face);
	
	/// Destructor
	~GlyphOutline(void);

	/// @brief Get the points conforming the character
	/// @return vector of points
	const std::vector<Point>& GetPoints(void) const;

	/// @brief Get the segments conforming the character
	/// @return vector of lines
	const std::vector<Line>& GetSegments(void) const;

	/// @brief Get the contours conforming the character.
	/// @note vector layout: 
	/// [S0, E0, S1, E1, ..., Sn, En] being 
	/// Si start segment index, Ei end segment index
	/// @return vector with indices of contours
	const std::vector<int>& GetContours(void) const;

	/// @brief Get the type of contour: Inner or Outer.
	/// For example, 'a' has two contours the exterior
	/// of the letter (Outer) and the hole (Inner)
	/// @return vector with contours types
	const std::vector<ContourType>& GetContoursType(void) const;

	/// @brief Get the width metric
	/// @return width of the glyph
	int GetWidth(void) const;

	/// @brief Get the advance metric
	/// @return advance of the glyph
	int GetAdvance(void) const;

private:
	/// Points
	std::vector<Point> m_points;
	
	/// Segments
	std::vector<Line> m_segments;

	/// Contours
	std::vector<int> m_contours;

	/// Contours types, aligned with contours
	/// vector
	std::vector<ContourType> m_contoursTypes;

	/// Width 
	int m_width;

	/// Advance
	int m_advance;

	/// @note hide copy constructor from interface user
	GlyphOutline(const GlyphOutline&);

	/// @note hide assignment operator from interface user
	GlyphOutline& operator=(const GlyphOutline&);

	/// @brief It loads the glyph from freetype face
	/// @param charCode character code
	/// @param face freetype face from which is going
	/// to be loaded the glyph
	void LoadGlyph(wchar_t charCode, FT_Face face);

	/// @brief It gets the orientation of the contour
	/// @param segmentStartIndex start index of the segment in m_segments
	/// @param segmentEndIndex end index of the segment in m_segments
	/// @note http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/
	/// @note http://local.wasp.uwa.edu.au/~pbourke/geometry/clockwise/index.html
	/// @return true if counter clockwise, false otherwise
	bool ContourCounterClockWise(int segmentStartIndex, int segmentEndIndex);
};

}

#endif