/*
Copyright (c) 1993-2008, Cognitive Technologies
All rights reserved.

пЮГПЕЬЮЕРЯЪ ОНБРНПМНЕ ПЮЯОПНЯРПЮМЕМХЕ Х ХЯОНКЭГНБЮМХЕ ЙЮЙ Б БХДЕ ХЯУНДМНЦН ЙНДЮ,
РЮЙ Х Б ДБНХВМНИ ТНПЛЕ, Я ХГЛЕМЕМХЪЛХ ХКХ АЕГ, ОПХ ЯНАКЧДЕМХХ ЯКЕДСЧЫХУ СЯКНБХИ:

      * оПХ ОНБРНПМНЛ ПЮЯОПНЯРПЮМЕМХХ ХЯУНДМНЦН ЙНДЮ ДНКФМШ НЯРЮБЮРЭЯЪ СЙЮГЮММНЕ
        БШЬЕ СБЕДНЛКЕМХЕ НА ЮБРНПЯЙНЛ ОПЮБЕ, ЩРНР ЯОХЯНЙ СЯКНБХИ Х ОНЯКЕДСЧЫХИ
        НРЙЮГ НР ЦЮПЮМРХИ.
      * оПХ ОНБРНПМНЛ ПЮЯОПНЯРПЮМЕМХХ ДБНХВМНЦН ЙНДЮ Б ДНЙСЛЕМРЮЖХХ Х/ХКХ Б
        ДПСЦХУ ЛЮРЕПХЮКЮУ, ОНЯРЮБКЪЕЛШУ ОПХ ПЮЯОПНЯРПЮМЕМХХ, ДНКФМШ ЯНУПЮМЪРЭЯЪ
        СЙЮГЮММЮЪ БШЬЕ ХМТНПЛЮЖХЪ НА ЮБРНПЯЙНЛ ОПЮБЕ, ЩРНР ЯОХЯНЙ СЯКНБХИ Х
        ОНЯКЕДСЧЫХИ НРЙЮГ НР ЦЮПЮМРХИ.
      * мХ МЮГБЮМХЕ Cognitive Technologies, МХ ХЛЕМЮ ЕЕ ЯНРПСДМХЙНБ МЕ ЛНЦСР
        АШРЭ ХЯОНКЭГНБЮМШ Б ЙЮВЕЯРБЕ ЯПЕДЯРБЮ ОНДДЕПФЙХ Х/ХКХ ОПНДБХФЕМХЪ
        ОПНДСЙРНБ, НЯМНБЮММШУ МЮ ЩРНЛ он, АЕГ ОПЕДБЮПХРЕКЭМНЦН ОХЯЭЛЕММНЦН
        ПЮГПЕЬЕМХЪ.

щрю опнцпюллю опеднярюбкемю бкюдекэжюлх юбрнпяйху опюб х/хкх дпсцхлх кхжюлх "йюй
нмю еярэ" аег йюйнцн-кхан бхдю цюпюмрхи, бшпюфеммшу ъбмн хкх ондпюгслебюелшу,
бйкчвюъ цюпюмрхх йнллепвеяйни жеммнярх х опхцндмнярх дкъ йнмйпермни жекх, мн ме
нцпюмхвхбюъяэ хлх. мх бкюдекеж юбрнпяйху опюб х мх ндмн дпсцне кхжн, йнрнпне
лнфер хглемърэ х/хкх онбрнпмн пюяопнярпюмърэ опнцпюллс, мх б йнел яксвюе ме
мея╗р нрберярбеммнярх, бйкчвюъ кчаше наыхе, яксвюимше, яоежхюкэмше хкх
онякеднбюбьхе сашрйх, ябъгюммше я хяонкэгнбюмхел хкх онмеяеммше бякедярбхе
мебнглнфмнярх хяонкэгнбюмхъ опнцпюллш (бйкчвюъ онрепх дюммшу, хкх дюммше,
ярюбьхе мецндмшлх, хкх сашрйх х/хкх онрепх днунднб, онмеяеммше хг-гю деиярбхи
рперэху кхж х/хкх нрйюгю опнцпюллш пюанрюрэ янблеярмн я дпсцхлх опнцпюллюлх,
мн ме нцпюмхвхбюъяэ щрхлх яксвюълх), мн ме нцпюмхвхбюъяэ хлх, дюфе еякх рюйни
бкюдекеж хкх дпсцне кхжн ашкх хгбеыемш н бнглнфмнярх рюйху сашрйнб х онрепэ.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * 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.
    * Neither the name of the Cognitive Technologies nor the names of its
      contributors may be used to endorse or promote products derived from this
      software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT OWNER OR CONTRIBUTORS 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.
*/

/*----------------------------------------------------------------------*/
/*	STIC-MAC.H  19.10.1993  MACROS (#include to STIC_DIS, STIC_BIG)	*/
/*----------------------------------------------------------------------*/
// 	begin it  24.03.1993	(from STIC-TAB.H):	MACROS ONLY
/*----------------------------------------------------------------------*/
/*----------------------------------------------------------------------*/
/* ╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟ */
/* ╟╟╟								╟╟╟ */
/* ╟╟╟	values for tables in STIC-TD.H, STIC-TB.H		╟╟╟ */
/* ╟╟╟								╟╟╟ */
/* ╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟ */

#define LF 60	/* long central flag	*/
#define NF 12	/* small central flag	*/
#define MD  4	/* min dis value	*/
#define SI 20	/* similar  ╨		*/
#define BP 80	/* bad proportions	*/
#define ST 80	/* similar  't'		*/
#define BR 40	/* similar  braces	*/
//////#define BR_i 20	/* similar  braces for case letter 'i' with point {Ь} */
#define BR_i 0	/* similar  braces for case letter 'i' with point {Ь} */
#define DB 70	/* auxiliary beam	*/
/*----------------------------------------------------------------------*/
/* ╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟ */
/* ╟╟╟                                                          ╟╟╟ */
/* ╟╟╟ defines for some discrimations operations                ╟╟╟ */
/* ╟╟╟                                                          ╟╟╟ */
/* ╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟╟ */

/* N.B. argument t is result of calculations           */
#define CALC_LENGTH_BEAM_FLAGS(l,r,width,signums,t)     \
	{                                               \
	if ( l->c_meandr && r->c_meandr )	t = 2;	\
	else	{					\
		if ( width>3 )		t=3;		\
		else if ( width==1 )	t=1;		\
		else			t=2;		\
		}                                       \
	if ( t==2 && signums->height<16 )	t=1;	\
	}

#define DIS_BEAM(L,R,i,dist,tab)                \
	{                                       \
	if( L->mount[i]+R->mount[i] > (dist) )  \
		dis += tab;                     \
	}

#define DIS_BEAM_MK(L,R,i,dist,tab)			\
	{						\
	if ( L->mount[i] && R->mount[i] && !inc &&	\
	     ((L->mount[i] + R->mount[i])==(dist)) )	\
		dis += tab;				\
	}

#define DIS_BRACES(L,R,tab)         \
	{                           \
	DIS_BRACE(L,R,tab)          \
	DIS_BRACE(R,L,tab)          \
	}

#define DIS_BRACE(L,R,tab)					\
	{							\
	if( L->mount[0]==0 && L->mount[4]==0 &&			\
	    L->num_long_flags==0 &&				\
	    R->mount[0]>1  && R->mount[4]>1  &&			\
	    R->m_meandr==0  &&					\
	    (L->conc[0]<3 || L->ce_pos[0]<4) )			\
		dis += (tab)*(R->mount[0] + R->mount[4] - 2);	\
	}

#define DIS_BRACES_CIRCLE(L,R,wid,tab)  \
	{                               \
	DIS_BRACE_CIRCLE(L,R,wid,tab)   \
	DIS_BRACE_CIRCLE(R,L,wid,tab)   \
	}

			// 10.06.1993: CENSURE max 4*tab:
#define DIS_BRACE_CIRCLE(L,R,wid,tab)					\
	{								\
	if( L->mount[0]==0 && L->mount[4]==0 &&				\
	    R->mount[0]*3>=wid && R->mount[4]*3>=wid    &&		\
	    L->num_concs<3                              &&		\
	    (L->mount[2]>0||L->conc[0]>0&&L->conc[4]>0) &&		\
	    R->mount[0]>0  && R->mount[4]>0  && R->m_meandr<4 )		\
		dis += (tab) * min ((R->mount[0] + R->mount[4]), 4);	\
	}


#define DIS_CENTER_CONCS(L,R,i,inc,t1,t2)      \
		{                              \
		DIS_CENTER_CONC(L,i,inc,t1,t2) \
		DIS_CENTER_CONC(R,i,inc,t1,t2) \
		}

#define DIS_CENTER_CONC(U,i,inc,t1,t2)              \
	{ int16_t t,m;                                  \
	m = ((U->up_serif==0)&&(U->down_serif==0)); \
	t = U->conc[i];                             \
	if( t>1 )                                   \
		dis += (t1)*(t-1);                  \
	else if( t>0 && m &&!inc && U->m_meandr>0 ) \
		dis += (t2);}

#define DIS_CENTER_FLAGS(L,R,i,width,inc,t1,t2)		\
		{					\
		DIS_CENTER_FLAG(L,i,width,inc,t1,t2)	\
		DIS_CENTER_FLAG(R,i,width,inc,t1,t2)	\
		}

#define DIS_FLAGS(i,t1,t2)				\
		{					\
		DIS_CENTER_FLAG(l,i,wid,inc,t1,t2)	\
		DIS_CENTER_FLAG(r,i,wid,inc,t1,t2)	\
		}

#define DIS_FLAGS_L_R(i,t1)				\
		{					\
		DIS_CENTER_FLAG(l,i,wid,inc,t1,d_L)	\
		DIS_CENTER_FLAG(r,i,wid,inc,t1,d_R)	\
		}

#define DIS_CENTER_FLAG(U,i,width,inc,t1,t2)             \
	{ int16_t t,dist=1;                                  \
	t = U->mount[i];                                 \
	if( U->mount[0]>width || U->mount[4]>width )     \
		dist++;                                  \
	if( t>dist )                                     \
		dis += (t1)*(t-1);                       \
	else if( t>((dist)-1) && !inc && U->c_meandr>0 ) \
		dis += (t2);}

#define DIS_CENTER_FLAGS_1(L,R,i,width,inc,t1,t2)	\
		{					\
		DIS_CENTER_FLAG_1(L,i,width,inc,t1,t2)	\
		DIS_CENTER_FLAG_1(R,i,width,inc,t1,t2)	\
		}

#define DIS_CENTER_FLAG_1(U,i,width,inc,t1,t2)			\
	{ int16_t t,dist=1;						\
	t = U->mount[i];					\
	if( U->mount[0]>width )  dist++;			\
	if( t>dist )						\
		dis += (t1)*(t-1);				\
	else if( t>((dist)-1) && !inc && U->c_meandr>0 )	\
		dis += (t2);}

#define DIS_CENTER_FLAGS_2(L,R,i,width,inc,t1,t2)	\
		{					\
		DIS_CENTER_FLAG_2(L,i,width,inc,t1,t2)	\
		DIS_CENTER_FLAG_2(R,i,width,inc,t1,t2)	\
		}

#define DIS_CENTER_FLAG_2(U,i,width,inc,t1,t2)			\
	{ int16_t t,dist=1;						\
	t = U->mount[i];					\
	if( width>=6 )  dist++;					\
	if( t>dist )						\
		dis += (t1)*(t-1);				\
	else if( t>((dist)-1) && !inc && U->c_meandr>0 )	\
		dis += (t2);}

#define DIS_CURVE(L,R,i,lev,tab)         	   \
	{                                          \
	int16_t ll,rr;                                 \
	ll=L->mount[i]; rr=R->conc[i];             \
	if( ll>lev && rr>lev && (ll>0||rr>0) )     \
		dis += (ll+rr)*(tab);              \
	ll=L->conc[i]; rr=R->mount[i];             \
	if( ll>lev && rr>lev && (ll>0||rr>0))      \
		dis += (ll+rr)*(tab);              \
	}

					// 27.07.1993:	for lev==1 only;
#define DIS_CURVE_MK(L,R,i,lev,tab)		\
	{					\
int16_t	lm=L->mount[i],	rm=R->mount[i];		\
int16_t	lc=L->conc[i],	rc=R->conc[i];		\
	if (lc==0 && lm>lev && rc>lev)		\
		dis += (lm+rc)*(tab);		\
	if (rc==0 && rm>lev && lc>lev)		\
		dis += (rm+lc)*(tab);		\
	}

#define DIS_DIFFERENT_SERIFS(L,R,i,dist,wid,tab)      \
	{                                             \
	int16_t t,m;                                      \
	t =  abs(L->mount[i]-R->mount[i]) ;           \
	m =  min(L->mount[i],R->mount[i]) ;           \
	if( t>max(((wid)>>1),dist) || m==0 )          \
		dis += (tab)*t;                       \
	}

#define DIS_HALF_SERIF(L,R,i,d,tab)                            	     \
	{if( L->mount[i]==0 && R->mount[i]>d &&                      \
	    (L->conc[i]>0||R->mount[i]>d+1) )                        \
		dis += tab*(R->mount[i]+L->conc[i]);                 \
	else if( L->mount[i]>d && R->mount[i]==0  &&                 \
		 (R->conc[i]>0||L->mount[i]>d+1) )                   \
		dis += tab*(L->mount[i]+R->conc[i]);}

#define FOUR_CONC(l,r,tab)                                      \
	{                                                       \
	if( FOUR_CONC_COND(l,r) || FOUR_CONC_COND(r,l) )        \
		dis += (tab);                                   \
	}

#define FOUR_CONC_COND(l,r)                             \
	(l->conc[0] && l->conc[4] && l->c_meandr<3  &&  \
	 l->ce_pos[0]>0 &&  r->m_meandr)                \


/*----------------------------------------------------------------------*/
