drawtxt.cpp 9.38 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
	/************************************************/
	/* drawtxt.cpp : trace des textes de composants */
	/************************************************/

#include "fctsys.h"
#include "gr_basic.h"

#include "common.h"
#include "plot_common.h"

#include "trigo.h"
#include "macros.h"

#ifndef DEFAULT_SIZE_TEXT
#define DEFAULT_SIZE_TEXT 50
#endif

#define EDA_DRAWBASE
#include "grfonte.h"

/* fonctions locales : */


/****************************************************************************/
void DrawGraphicText(WinEDA_DrawPanel * panel, wxDC * DC,
			const wxPoint & Pos, int gcolor, const wxString & Text,
			int orient, const wxSize & Size, int h_justify, int v_justify, int width)
/*****************************************************************************/
/* Draw a graphic text (like module texts)
	Text = text to draw
	Pos = text position (according to h_justify, v_justify)
	Size = text size (size.x or size.y can be < 0 for mirrored texts) 
	orient = angle in 0.1 degree
	mode_color = GR_OR, GR_XOR..
	h_justify = horizontal justification (Left, center, right)
	v_justify = vertical justification (bottom, center, top)
	width = line width (pen width) (default = 0)
		if width < 0 : draw segments in sketch mode, width = abs(width)
*/
{
int ii, kk,nbchar, AsciiCode, endcar;
int k1 , k2, x0, y0;
int zoom;
int size_h , size_v , espacement ;
SH_CODE f_cod , plume = 'U';
const SH_CODE * ptcar;
int ptr;
int ux0, uy0, dx, dy;	// Coord de trace des segments de texte & variables de calcul */
int cX, cY;				// Centre du texte
int ox, oy;				// coord de trace du caractere courant
int coord[100];			// liste des coord des segments a tracer
bool sketch_mode = FALSE;
	
	zoom = panel->GetZoom();

	size_h = Size.x;
	size_v = Size.y;

	if ( width < 0 )
	{
		width = - width;
		sketch_mode = TRUE;
	}
	kk = 0 ; ptr = 0;	/* ptr = text index */

	/* calcul de la position du debut des textes: ox et oy */
	nbchar = Text.Len();
	if ( nbchar == 0 ) return;

	espacement = (10 * size_h ) / 9;
	ox = cX = Pos.x; oy = cY = Pos.y;

	/* Si le texte est totalement hors fenetre d'affichage, termin! */
	if ( panel )
	{
		int xm, ym, ll, xc, yc;
		int textsize = ABS(espacement);
		ll = (textsize * nbchar) / zoom;
		xc = GRMapX(cX);
		yc = GRMapY(cY);
		x0 = panel->m_ClipBox.GetX() - ll;
		y0 = panel->m_ClipBox.GetY() -ll ;
		xm = panel->m_ClipBox.GetRight() + ll;
		ym = panel->m_ClipBox.GetBottom() + ll;
		if ( xc < x0 ) return;
		if ( yc < y0 ) return;
		if ( xc > xm ) return;
		if ( yc > ym ) return;
	}


	/* Calcul du cadrage du texte */
	dx = (espacement * nbchar) / 2;
	dy = size_v / 2;	/* Decalage du debut du texte / centre */

	ux0 = uy0 = 0;  /* Decalage du centre du texte / coord de ref */

	if( (orient == 0) || (orient == 1800) ) /* Texte Horizontal */
		{
		switch(h_justify)
			{
			case GR_TEXT_HJUSTIFY_CENTER:
				break;

			case GR_TEXT_HJUSTIFY_RIGHT:
				ux0 = - dx;
				break;

			case GR_TEXT_HJUSTIFY_LEFT:
				ux0 = dx;
				break;
			}

		switch(v_justify)
			{
			case GR_TEXT_VJUSTIFY_CENTER:
				break;

			case GR_TEXT_VJUSTIFY_TOP:
				uy0 = dy;
				break;

			case GR_TEXT_VJUSTIFY_BOTTOM:
				uy0 = -dy;
				break;
			}
		}

	else	/* Texte Vertical */
		{
		switch(h_justify)
			{
			case GR_TEXT_HJUSTIFY_CENTER:
				break;

			case GR_TEXT_HJUSTIFY_RIGHT:
				ux0 = - dy;
				break;

			case GR_TEXT_HJUSTIFY_LEFT:
				ux0 = dy;
				break;
			}

		switch(v_justify)
			{
			case GR_TEXT_VJUSTIFY_CENTER:
				break;

			case GR_TEXT_VJUSTIFY_TOP:
				uy0 = dx;
				break;

			case GR_TEXT_VJUSTIFY_BOTTOM:
				uy0 = -dx;
				break;
			}
		}
	cX += ux0; cY += uy0;
	ox = cX - dx; ; oy = cY + dy;

	if( (Size.x/zoom) == 0 ) return;

	if( ABS((Size.x/zoom)) < 3) /* chars trop petits pour etre dessines */
	{	/* le texte est symbolise par une barre */
		dx = (espacement * nbchar) / 2;
		dy = size_v / 2;	 /* Decalage du debut du texte / centre */

		ux0 = cX - dx; uy0 = cY;
		dx += cX; dy = cY;
		RotatePoint(&ux0, &uy0, cX, cY, orient);
		RotatePoint(&dx, &dy, cX, cY, orient);
		GRLine(&panel->m_ClipBox, DC, ux0, uy0, dx, dy, width, gcolor);

		return;
	}

#if 0
dx = (espacement * nbchar) / 2;
dy = size_v / 2;	 /* Decalage du debut du texte / centre */
ux0 = cX - dx; uy0 = cY;
dx += cX; dy = cY;
RotatePoint(&ux0, &uy0, cX, cY, orient);
RotatePoint(&dx, &dy, cX, cY, orient);
DC->SetTextForeground( wxColour(
						ColorRefs[gcolor].r,
						ColorRefs[gcolor].g,
						ColorRefs[gcolor].b) );
DC->DrawRotatedText(Text, GRMapX(ux0), GRMapY(uy0), (double) orient / 10.0);
return;
#endif

	while(kk++ < nbchar)
		{
		x0 = 0 ; y0 = 0 ;
		AsciiCode = Text.GetChar(ptr) & 255;
		ptcar = graphic_fonte_shape[AsciiCode] ; /* ptcar pointe la description
						du caractere a dessiner */

		for(ii = 0, endcar = FALSE; ! endcar; ptcar++)
			{
			 f_cod = *ptcar ; /* get code n de la forme selectionnee */
			 switch(f_cod)
				 {
				 case 'X' :
					endcar = TRUE;/* fin du caractere */
					break;

				 case 'U' :
						if(ii && (plume == 'D' ) )
							{
							if ( width <= 1 )
								GRPoly(&panel->m_ClipBox, DC, ii /2, coord, 0, 0,
									gcolor, gcolor);
							else if ( sketch_mode )
							{
								int ik, * coordptr;
								coordptr = coord;
								for ( ik = 0; ik < (ii-2); ik += 2,  coordptr+= 2)
									GRCSegm(&panel->m_ClipBox, DC, *coordptr, *(coordptr+1),
											*(coordptr+2), *(coordptr+3), width, gcolor) ;
							}
								
							else
								GRPoly(&panel->m_ClipBox, DC, ii /2, coord, 0,
									width, gcolor, gcolor);
							}
						plume = f_cod; ii = 0;
						break;

				 case 'D' : plume = f_cod ; break ;

				 default  :
					{
					k1 = f_cod; /* trace sur axe V */
					k1 = - ((k1 * size_v) / 9) ;
					ptcar++ ;
					f_cod = *ptcar  ;
					k2 = f_cod; /* trace sur axe H */
					k2 = (k2 * size_h) / 9 ;
					dx = k2 + ox; dy = k1 + oy;
					RotatePoint(&dx, &dy, cX, cY, orient);
					coord[ii] = dx;  ii++; coord[ii] = dy; ii++;
					break ;
					}
				 } /* end switch */
			} /* end boucle for = end trace de 1 caractere */

		ptr++; ox += espacement;
		} /* end trace du texte */

}


/******************************************************************************************/
void PlotGraphicText(int format_plot, const wxPoint & Pos, int gcolor,
				 const wxString & Text,
				int orient, const wxSize & Size, int h_justify, int v_justify)
/******************************************************************************************/
/*
 id DrawGraphicText, for plot graphic text
*/
{
int kk, nbchar, end, AsciiCode;
int k1 , k2 , x0 , y0, ox, oy ;
int size_h , size_v , espacement ;
SH_CODE f_cod , plume = 'U';
const SH_CODE * ptcar;
int ptr;
int ux0, uy0, dx, dy;	// Coord de trace des segments de texte & variables de calcul */
int cX, cY;				// Centre du texte
void (*FctPlume)(wxPoint pos, int state);
	
	switch ( format_plot)
	{
		case PLOT_FORMAT_POST:
			FctPlume = LineTo_PS;
			break;

		case PLOT_FORMAT_HPGL:
			FctPlume = Move_Plume_HPGL;
			break;

		case PLOT_FORMAT_GERBER:
		default:
			return;
	}

	if ( (gcolor >= 0) && (format_plot == PLOT_FORMAT_POST) )
		SetColorMapPS ( gcolor );

	size_h = Size.x;
	size_v = Size.y;
	if(size_h == 0) size_h = DEFAULT_SIZE_TEXT;
	if(size_v == 0) size_v = DEFAULT_SIZE_TEXT;

	kk = 0 ; ptr = 0; /* ptr = text index */

	/* calcul de la position du debut des textes: ox et oy */
	nbchar = Text.Len();

	espacement = (10 * size_h ) / 9;
	ox = cX = Pos.x; oy = cY = Pos.y;

	/* Calcul du cadrage du texte */
	dx = (espacement * nbchar) / 2;
	dy = size_v / 2;	/* Decalage du debut du texte / centre */

	ux0 = uy0 = 0;  /* Decalage du centre du texte / ccord de ref */

	if( (orient == 0) || (orient == 1800) ) /* Texte Horizontal */
	{
		switch(h_justify)
		{
			case GR_TEXT_HJUSTIFY_CENTER:
				break;

			case GR_TEXT_HJUSTIFY_RIGHT:
				ux0 = - dx;
				break;

			case GR_TEXT_HJUSTIFY_LEFT:
				ux0 = dx;
				break;
		}

		switch(v_justify)
		{
			case GR_TEXT_VJUSTIFY_CENTER:
				break;

			case GR_TEXT_VJUSTIFY_TOP:
				uy0 = dy;
				break;

			case GR_TEXT_VJUSTIFY_BOTTOM:
				uy0 = -dy;
				break;
		}
	}

	else	/* Texte Vertical */
	{
		switch(h_justify)
		{
			case GR_TEXT_HJUSTIFY_CENTER:
				break;

			case GR_TEXT_HJUSTIFY_RIGHT:
				ux0 = -dy;
				break;

			case GR_TEXT_HJUSTIFY_LEFT:
				ux0 = dy;
				break;
		}

		switch(v_justify)
		{
			case GR_TEXT_VJUSTIFY_CENTER:
				break;

			case GR_TEXT_VJUSTIFY_TOP:
				uy0 = dx;
				break;

			case GR_TEXT_VJUSTIFY_BOTTOM:
				uy0 = -dx;
				break;
		}
	}
	cX += ux0; cY += uy0;	/* cX, cY = coord du centre du texte */
	ox = -dx; oy = +dy; /* ox, oy = coord debut texte, relativement au centre */

	FctPlume(wxPoint(0,0), 'Z');

	while(kk++ < nbchar)
		{
		AsciiCode = Text.GetChar(ptr) & 255;
		ptcar = graphic_fonte_shape[AsciiCode] ; /* ptcar pointe la description
						du caractere a dessiner */

		for(end = 0 ; end == 0; ptcar++)
			{
			 f_cod = *ptcar ; /* get code n de la forme selectionnee */
			 switch(f_cod)
				{
				case 'X' : end = 1 ;/* fin du caractere */

				case 'U' :
				case 'D' : plume = f_cod ; break ;

				default  :
					k1 = f_cod ; /* trace sur axe V */
					k1 = -(k1 * size_v) / 9 ;
					ptcar++ ;
					f_cod = *ptcar;
					k2 = f_cod; /* trace sur axe H */
					k2 = (k2 * size_h) / 9 ;
					dx = k2 + ox; dy = k1 + oy;
					RotatePoint(&dx, &dy, orient);
					FctPlume(wxPoint(cX + dx, cY + dy), plume);
					x0 = k2 ; y0 = k1 ;
					break ;
				} /* end switch */
			} /* end boucle for = end trace de 1 caractere */

		FctPlume(wxPoint(0,0), 'Z');
		ptr++; ox += espacement;
		} /* end trace du texte */
	FctPlume(wxPoint(0,0), 'Z');
}