/*<<<***************************************************************************
The MIT License (MIT)

Copyright(C) 2013 - 2015 Marc Mirti Mancinelli, Christophe Lardau and Philippe Pamart (xdcube.org)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
////////////////////////////////////////////////////////////////////////////////
#Title: gdiBase
#Project: xdcGdi
////////////////////////////////////////////////////////////////////////////////
#Author: Marc MIRTI MANCINELLI
#History:
<11/09/2013: creation.
12/02/2014: this notice inserted.
***************************************************************************>>>*/
#include "stdafx.h"
#include "gdiBase.h"
#include "gdiFont.h"

xbEimpl(gdiEsys,xbEadv);

bool gdiRectLow::PtInRect(const POINT &pt) const
{
	if (pt.x < left || pt.x >= right)
		return false;
	if (pt.y < top || pt.y >= bottom)
		return false;
	return true;
}

void gdiRectLow::Normalize()
{
	if (left > right)
	{
		long tmp = right;
		right = left;
		left = tmp;
	}
	if (top > bottom)
	{
		long tmp = bottom;
		bottom = top;
		top = tmp;
	}
}

bool gdiRectLow::Intersect(const RECT &rect1, const RECT &rect2)
{
	return ::IntersectRect(this,&rect1,&rect2)?true:false;
}

bool gdiRectF::PtInRect(const gdiPointF &pt) const
{
	if (pt.x < left || pt.x >= right)
		return false;
	if (pt.y < top || pt.y >= bottom)
		return false;
	return true;
}

void gdiRectF::Normalize()
{
	if (left > right)
	{
		gdinum tmp = right;
		right = left;
		left = tmp;
	}
	if (top > bottom)
	{
		gdinum tmp = bottom;
		bottom = top;
		top = tmp;
	}
}

bool gdiRectF::Intersect(const gdiRectF &rect1, const gdiRectF &rect2)
{
	left = max(rect1.left,rect2.left);
	right = min(rect1.right,rect2.right);
	top = max(rect1.top,rect2.top);
	bottom = min(rect1.bottom,rect2.bottom);
	return IsValid();
}

/*
gmffBase::gmffBase(const gmString &id)
:m_id(id)
{
	m_bBigEndian = false;
	m_bFFidentified = false;
	m_bFFended = false;
	m_bFFok = true;
}

int gmffBase::AddFFdata(const BYTE *ptr,int lg)
{
	int cByteRd = 0;
	while(lg > 0)
	{
		if (m_dta.IsEmpty())
		{
			bool bBufferize = false;
			int ret = onAddFFdata(ptr,lg,&bBufferize);
			if (ret < 0)
			{
				if (ret == gmffAddRetBadFF)
					return gmffAddRetBadFF;
				xbThrow gmllExcept("gmffBase::AddFFdata() : internal error, onAddFFdata bad return");
			}
			if (ret > 0)
			{
				ptr += ret;
				cByteRd += ret;
				lg -= ret;
				if (bBufferize && lg <= 0) 
					bBufferize = false;
			}
			if (bBufferize)
			{
				m_dta.Add(ptr,lg);
				return cByteRd + lg;
			}
			if (m_bFFended) 
				return cByteRd;
			continue;
		}

		bool bBufferize = false;
		m_dta.Add(ptr,lg);
		int ret = onAddFFdata(m_dta,m_dta.Length,&bBufferize);
		if (ret < 0)
		{
			if (ret == gmffAddRetBadFF)
				return gmffAddRetBadFF;
			xbThrow gmllExcept("gmffBase::AddFFdata() : internal error, onAddFFdata bad return (2)");
		}
		if (m_bFFended) 
		{
			gmAssert(!bBufferize);
			if (ret < m_dta.Length - lg)
				xbThrow gmllExcept("gmffBase::AddFFdata() : internal error, unexpected end of format");
			int rd = cByteRd+ret - m_dta.Length + lg;
			m_dta.Clear();
			return rd;
		}
		if (ret > 0)
		{
			lg -= ret;
			cByteRd += ret;
			if (m_dta.Length <= ret)
			{
				gmAssert(m_dta.Length == ret);
				m_dta.Clear();
				return cByteRd;
			}
			m_dta.Delete(1,ret);
		}
		if (bBufferize)
			return cByteRd + lg;

		gmSTdata<BYTE> dta;
		dta.Attach(m_dta);
		
		ret = AddFFdata(dta,dta.Length);
		if (ret < 0)
		{
			if (ret == gmffAddRetBadFF)
				return gmffAddRetBadFF;
			xbThrow gmllExcept("gmffBase::AddFFdata() : internal error, AddFFdata bad return (3)");
		}
		if (ret < dta.Length - lg)
			xbThrow gmllExcept("gmffBase::AddFFdata() : internal error, AddFFdata bad return (4)");
		return cByteRd + ret - dta.Length + lg;
	}
	return cByteRd;
}

bool gmffBase::IsFFidentified() const
{
	return m_bFFidentified;
}

void gmffBase::ResetFF()
{
	m_bFFidentified = false;
	m_bBigEndian = false;
	m_dta.Empty();
	m_bFFended = false;
	m_bFFok = true;
}

bool gmffBase::EndFF()
{
	if (!m_bFFidentified)
		return false;
	if (m_bFFended) 
		return m_bFFok;

	m_bFFended = true;
	onEndFF();
	return m_bFFok;
}
*/

