kicad/common/drawtxt.cpp

476 lines
12 KiB
C++
Raw Normal View History

2008-01-19 20:34:10 +00:00
/*************************************************/
/* drawtxt.cpp : Function to draw and plot texts */
/*************************************************/
#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 : */
/****************************************************************************/
2008-01-19 20:34:10 +00:00
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 )
/*****************************************************************************/
2008-01-19 20:34:10 +00:00
/* Draw a graphic text (like module texts)
2008-01-19 20:34:10 +00:00
* 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)
*/
{
2008-01-19 20:34:10 +00:00
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; // Draw coordinate for segments to draw. also used in some other calculation
int cX, cY; // Texte center
int ox, oy; // Draw coordinates for the current char
int coord[100]; // Buffer coordinate used to draw polylines (char shapes)
bool sketch_mode = FALSE;
zoom = panel->GetZoom();
size_h = Size.x;
size_v = Size.y;
if( width < 0 )
{
width = -width;
sketch_mode = TRUE;
}
2008-03-22 05:55:06 +00:00
kk = 0;
2008-01-19 20:34:10 +00:00
ptr = 0; /* ptr = text index */
nbchar = Text.Len();
if( nbchar == 0 )
return;
espacement = (10 * size_h ) / 9; // this is the pitch between chars
2008-03-22 05:55:06 +00:00
ox = cX = Pos.x;
2008-01-19 20:34:10 +00:00
oy = cY = Pos.y;
/* Do not draw the text if out of draw area! */
if( panel )
{
int xm, ym, ll, xc, yc;
int textsize = ABS( espacement );
ll = (textsize * nbchar) / zoom;
2008-03-22 05:55:06 +00:00
2008-01-19 20:34:10 +00:00
xc = GRMapX( cX );
yc = GRMapY( cY );
2008-03-22 05:55:06 +00:00
2008-01-19 20:34:10 +00:00
x0 = panel->m_ClipBox.GetX() - ll;
y0 = panel->m_ClipBox.GetY() - ll;
xm = panel->m_ClipBox.GetRight() + ll;
ym = panel->m_ClipBox.GetBottom() + ll;
2008-03-22 05:55:06 +00:00
2008-01-19 20:34:10 +00:00
if( xc < x0 )
return;
if( yc < y0 )
return;
if( xc > xm )
return;
if( yc > ym )
return;
}
/* Compute the position ux0, uy0 of the first letter , next */
dx = (espacement * nbchar) / 2;
dy = size_v / 2; /* dx, dy = draw offset between first letter and text center */
ux0 = uy0 = 0; /* Decalage du centre du texte / coord de ref */
if( (orient == 0) || (orient == 1800) ) /* Horizontal Text */
{
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 /* Vertical Text */
{
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;
}
}
2008-03-22 05:55:06 +00:00
cX += ux0;
2008-01-19 20:34:10 +00:00
cY += uy0;
2008-03-22 05:55:06 +00:00
2008-01-19 20:34:10 +00:00
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 */
2008-03-22 05:55:06 +00:00
ux0 = cX - dx;
2008-01-19 20:34:10 +00:00
uy0 = cY;
2008-03-22 05:55:06 +00:00
dx += cX;
2008-01-19 20:34:10 +00:00
dy = cY;
2008-03-22 05:55:06 +00:00
2008-01-19 20:34:10 +00:00
RotatePoint( &ux0, &uy0, cX, cY, orient );
RotatePoint( &dx, &dy, cX, cY, orient );
2008-03-22 05:55:06 +00:00
2008-01-19 20:34:10 +00:00
GRLine( &panel->m_ClipBox, DC, ux0, uy0, dx, dy, width, gcolor );
return;
}
#if 0
2008-01-19 20:34:10 +00:00
dx = (espacement * nbchar) / 2;
dy = size_v / 2;/* Decalage du debut du texte / centre */
2008-03-22 05:55:06 +00:00
ux0 = cX - dx;
2008-01-19 20:34:10 +00:00
uy0 = cY;
2008-03-22 05:55:06 +00:00
dx += cX;
2008-01-19 20:34:10 +00:00
dy = cY;
2008-03-22 05:55:06 +00:00
2008-01-19 20:34:10 +00:00
RotatePoint( &ux0, &uy0, cX, cY, orient );
RotatePoint( &dx, &dy, cX, cY, orient );
2008-03-22 05:55:06 +00:00
2008-01-19 20:34:10 +00:00
DC->SetTextForeground( wxColour(
ColorRefs[gcolor].r,
ColorRefs[gcolor].g,
ColorRefs[gcolor].b ) );
2008-03-22 05:55:06 +00:00
2008-01-19 20:34:10 +00:00
DC->DrawRotatedText( Text, GRMapX( ux0 ), GRMapY( uy0 ), (double) orient / 10.0 );
return;
#endif
2008-01-19 20:34:10 +00:00
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':
2008-03-22 05:55:06 +00:00
plume = f_cod;
2008-01-19 20:34:10 +00:00
break;
default:
{
k1 = f_cod; /* trace sur axe V */
k1 = -( (k1 * size_v) / 9 );
2008-03-22 05:55:06 +00:00
2008-01-19 20:34:10 +00:00
ptcar++;
f_cod = *ptcar;
2008-03-22 05:55:06 +00:00
2008-01-19 20:34:10 +00:00
k2 = f_cod; /* trace sur axe H */
k2 = (k2 * size_h) / 9;
dx = k2 + ox; dy = k1 + oy;
2008-03-22 05:55:06 +00:00
2008-01-19 20:34:10 +00:00
RotatePoint( &dx, &dy, cX, cY, orient );
coord[ii++] = dx;
2008-03-22 05:55:06 +00:00
coord[ii++] = dy;
2008-01-19 20:34:10 +00:00
break;
}
}
/* end switch */
}
/* end boucle for = end trace de 1 caractere */
ptr++; ox += espacement;
}
/* end trace du texte */
}
/******************************************************************************************/
2008-01-19 20:34:10 +00:00
void PlotGraphicText( int format_plot, const wxPoint& Pos, int gcolor,
const wxString& Text,
int orient, const wxSize& Size, int h_justify, int v_justify )
/******************************************************************************************/
2008-01-19 20:34:10 +00:00
/*
2008-01-19 20:34:10 +00:00
* id DrawGraphicText, for plot graphic text
*/
{
2008-01-19 20:34:10 +00:00
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;
}
2008-03-22 05:55:06 +00:00
if( gcolor >= 0 && IsPostScript( format_plot ) )
2008-01-19 20:34:10 +00:00
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;
2008-03-22 05:55:06 +00:00
kk = 0;
2008-01-19 20:34:10 +00:00
ptr = 0; /* ptr = text index */
/* calcul de la position du debut des textes: ox et oy */
nbchar = Text.Len();
espacement = (10 * size_h ) / 9;
2008-03-22 05:55:06 +00:00
ox = cX = Pos.x;
2008-01-19 20:34:10 +00:00
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;
}
}
2008-03-22 05:55:06 +00:00
cX += ux0;
2008-01-19 20:34:10 +00:00
cY += uy0; /* cX, cY = coord du centre du texte */
2008-03-22 05:55:06 +00:00
ox = -dx;
2008-01-19 20:34:10 +00:00
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;
2008-03-22 05:55:06 +00:00
2008-01-19 20:34:10 +00:00
k2 = f_cod; /* trace sur axe H */
k2 = (k2 * size_h) / 9;
2008-03-22 05:55:06 +00:00
dx = k2 + ox;
2008-01-19 20:34:10 +00:00
dy = k1 + oy;
2008-03-22 05:55:06 +00:00
2008-01-19 20:34:10 +00:00
RotatePoint( &dx, &dy, orient );
FctPlume( wxPoint( cX + dx, cY + dy ), plume );
2008-03-22 05:55:06 +00:00
x0 = k2;
2008-01-19 20:34:10 +00:00
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' );
}