您现在的位置:网站首页 > 经验分享 > 鞋业CAD软件柔性内核技术:源码[8-2
设计师介绍:

昵称:烦夫子
类别:界面/平面设计师
年龄:38
现所在地:北京

查看该设计师的主页>>

关注好友

统计中心

主页浏览总数:24259
总积分:89
文章数:88
作品数:70

鞋业CAD软件柔性内核技术:源码[8-2

作者:烦夫子  更新时间: 2007-11-19   浏览人数:18199  评论:0  
分享到:
柔性图形内核之八
                         Text
 
//应用于CAD/CAM图形类程序设计
//我将逐步开放数以十万记的自主版权软件源码,谢谢大家的支持!
//以后将开源的项目: CAD、工控、机械设计图、电子布线图等
 

// Text.cpp: implementation of the Text class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "SoftDraw.h"
#include "Text.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

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

Text::Text (const Object *p) : Object (p)
{
 name = TText;
 num = 0;
 fdata = new FontData[MAX_CHAR];
    TextColor = RGB(0,0,0);     //文字颜色
 color = TextColor;
 //0:忽略, 1:慢笔, 2:慢刀, 3:快笔, 4:快刀, 5:半刀
 plottype = 1; //1:Pen 2:HalfCut 4:Cutter

    appPath = " ";

}

Text::~Text ()
{
 Flush ();
 delete fdata;
}

void Text::Flush ()
{
 for (int i = 0; i < num; i++) {
  for (int j = 0; j < fdata[i].num; j++)
   delete fdata[i].s[j].v;
  delete fdata[i].s;
  }
 num = 0;
}

void Text::GetStringStroke ()
{
 int  k, p;
 Vector org;
 CString engfile, chnfile;

 switch (style.engfont) {
  case 0: engfile = "osmeng.shx"; break;
  default: return;
  }

 switch (style.chnfont) {
  case 0: chnfile = "osmchn.shx";  break;
  case 1: chnfile = "osms128.shx"; break;
  case 2: chnfile = "osmf128.shx"; break;
  case 3: chnfile = "osmh128.shx"; break;
  case 4: chnfile = "osmk128.shx"; break;
  default: return;
  }

 FontFile feng, fchn;
 if (! feng.Open (appPath + engfile, CFile::modeRead) ||
  ! fchn.Open (appPath + chnfile, CFile::modeRead)) {
  AfxMessageBox ("Not Find Files!");
  return;
  }

 Flush ();
 CalParam ();
 k = p = 0;
 while (p < string.GetLength ()) {
  if (string[p] & 0x80) {  // Chinese Char
   fchn.GetCharStroke (string.Mid (p, 2), org, fdata[k++]);
   p += 2;
   }
  else {     // English char
   switch (string[p]) {
    case ' ':
     break;
    case '\n':
     if (style.vertical) {
      org.x -= 1.0;
      org.y = 0;
      }
     else {
      org.x = 0;
      org.y += 1.0;
      }
    case '\r':
     p++;
     continue;
    default:
     feng.GetCharStroke (string.Mid (p, 1), org, fdata[k++]);
     break;
    }
   p++;
   }

  if (style.vertical) org.y += 1.0;
  else org.x += 1.0;
  }
 num = k;
}

void Text::SetString (const char *p)
{
 ASSERT (strlen (p) <= MAX_CHAR);

 string = p;
 if (strlen (p) > MAX_CHAR)
  string.Delete (MAX_CHAR, strlen(p) - MAX_CHAR);

 GetStringStroke ();
}

void Text::GetStyle (float& h, float& w, float& o, BOOL& v, int& ef, int& cf) const
{
 h  = style.height;
 w  = style.width;
 o  = style.obliquing;
 v  = style.vertical;
 ef = style.engfont;
 cf = style.chnfont;
}

void Text::SetString (const CString& p)
{
 ASSERT (p.GetLength () <= MAX_CHAR);

 string = p;
 if (p.GetLength () > MAX_CHAR)
  string.Delete (MAX_CHAR, p.GetLength () - MAX_CHAR);

 GetStringStroke ();
}

void Text::SetStyle (float h, float w, float o, BOOL v, int ef, int cf)
{
 style.height    = h;// note word height
 style.width     = w;
 style.obliquing = o;
 style.vertical  = v;
 style.engfont   = ef;
 style.chnfont   = cf;
 CalParam ();
}

void Text::SetTextHight(float h)
{
    style.height = h;
 CalParam ();
}

void Text::SetPoint (Vector o, Vector t)
{
 org = o;
 tag = t;
 if (org == tag)
  tag = org + Vector (1, 0);

 CalParam ();
 GetMinMax ();
}

void Text::CalParam ()
{
    VectorRun dp;
 
 clip = tan (style.obliquing * PI / 180);
 angle = tag - org;
 float l = dp.Length (angle);
 if (l < MINI) angle = Vector (1, 0);
 else angle /= l;
}

Vector Text::FCtoDC (const Vector& pt, const Vector& porg) const
{
 Vector v;
    VectorRun dp;

 v.x = pt.x * style.width;
 v.y = -pt.y * style.height;
 v.x += v.y * clip;
 v = dp.Rotate (porg * style.width + v, angle) + org;

 return v;
}

void Text::SetPropText(int ty, int wd, COLORREF c, int grade, float l, float w, int el)
{
 linetype = ty;
 linewidth = wd;
 color = c;

 if (el >= 0 && el <= 5)
  plottype = el;
 else
  plottype =0;
}

void Text::GetMinMax ()
{
 VectorRun dp;
 MinP = Vector (1000, 1000);
 MaxP = Vector (0, 0);
 for (int i = 0; i < num; i++)
  for (int j = 0; j < fdata[i].num; j++)
   for (int k = 0; k < fdata[i].s[j].num; k++) {
    Vector v = FCtoDC (fdata[i].s[j].v[k], fdata[i].org);
    MinP = dp.VMin (MinP, v);
    MaxP = dp.VMax (MaxP, v);
    }
}

long Text::GetSize () const
{
 long s = 0;
 for (int i = 0; i < num; i++) {
  for (int j = 0; j < fdata[i].num; j++)
   s += fdata[i].s[j].num * sizeof (Vector);
  s += fdata[i].num * sizeof (StrokeData);
  }

 return sizeof (Text) + s;
}

void Text::FastDraw (CDC* dc, float scale, DrawMode mode, TransformFunc f) const
{
    COLORREF cc = RGB(0,0,0);

 CPen pen(linetype,linewidth,(mode==DNormal)? color : cc),*oldp;
 oldp = dc->SelectObject( &pen);
 
 CPoint *p = new CPoint[400];
 for (int i = 0; i < num; i++)
  for (int j = 0; j < fdata[i].num; j++)
  {
   for (int k = 0; k < fdata[i].s[j].num && k<400; k++)
    p[k] = f (FCtoDC (fdata[i].s[j].v[k], fdata[i].org)) * scale;
   dc->Polyline (p, k);
  }
 dc->SelectObject(oldp);

 delete [] p;
}

void Text::GetString (CString& p) const
{
 p = string;
}

Object& Text::operator = (const Object& aObject)
{
 ASSERT (name == aObject.name);

 Text *p = &((Text&) aObject);

 string = p->string;
 style = p->style;
 org = p->org;
 tag = p->tag;

 GetStringStroke ();

 linetype = aObject.linetype;
 color = aObject.color;
 linewidth = aObject.linewidth;
 plottype = aObject.plottype;
 MinP = aObject.MinP;
 MaxP = aObject.MaxP;

 return *this;
}

BOOL Text::operator ^ (const Object& aObject)
{
 return FALSE;
}

BOOL Text::operator == (const Object& aObject)
{
 ASSERT (aObject.name == TText);

 Text *p = &((Text&) aObject);

 return (string          == p->string          &&
   style.height    == p->style.height    &&
   style.width     == p->style.height    &&
   style.obliquing == p->style.obliquing &&
   style.vertical  == p->style.vertical  &&
   style.engfont   == p->style.engfont   &&
   style.chnfont   == p->style.chnfont);
}

(目前有0人发表看法,  我要发表评论
我要评论:
  只有登录后才能评论!
评论者: 匿名游客    (立即登录 或 注册)