/// <summary> /// 填充所有字符轮廓 /// </summary> /// <param name="x">起始X</param> /// <param name="y">Y值</param> /// <param name="spacing">字符间隔</param> private void FillWordOutlines(int x, int y, int spacing, Font font) { string text = tbWords.Text; if (!string.IsNullOrEmpty(text)) { foreach (char t in text) { TreeNode wordNode = new TreeNode(t.ToString()); tvList.Nodes.Add(wordNode); //获取字符编码 uint ch = GetGB2312Coding(t); //获取轮廓数据 DOutline outline = WordGraph.GetOutline(ch, font); //构建轮廓实例 WordOutlineDrawing word = BuildWordOutline(outline, new PointF(x, y), wordNode); wordOutlines.Add(word); //下个字的起始位置=当前起始位置+宽度+间隔 x += (int)outline.Width + spacing; } } }
/// <summary> /// 通过轮廓数据构建字体轮廓 /// </summary> /// <param name="outline">轮廓数据</param> /// <param name="offset">偏移量</param> /// <param name="wordNode"></param> /// <returns></returns> private WordOutlineDrawing BuildWordOutline(DOutline outline, PointF offset, TreeNode wordNode) { //获取轮廓大小 SizeF size = new SizeF(outline.Width, outline.Height); WordOutlineDrawing word = new WordOutlineDrawing(size); //--------------------- wordNode.Tag = word; //--------------------- int index = 0; //遍历填充轮廓数据 foreach (DPolygon p in outline.Polygons) { //新增多边形实例 PolygonDrawing polygon = new PolygonDrawing(); //--------------------- TreeNode polygonNode = new TreeNode("多边形" + (++index)); polygonNode.Tag = polygon; wordNode.Nodes.Add(polygonNode); //--------------------- //起始点 PointF start = new PointF(offset.X + ConvertUtil.FixedToFloat(p.Start.x), offset.Y - ConvertUtil.FixedToFloat(p.Start.y)); PointF point = start; foreach (DLine l in p.Lines) { LineDrawing line = null; //如果类型为1则为折线,为2则为曲线 if (l.Type == 1) { line = new PolylineDrawing(); } else { line = new CurvelineDrawing(); } //加入起始点 line.Points.Add(point); //--------------------- StringBuilder builder = new StringBuilder(l.Type == 1 ? "折" : "曲"); builder.AppendFormat(" ({0},{1}) ", point.X, point.Y); //--------------------- foreach (POINTFX fx in l.Points) { point = new PointF(offset.X + ConvertUtil.FixedToFloat(fx.x), offset.Y - ConvertUtil.FixedToFloat(fx.y)); Console.WriteLine("{0}.{1}, {2}.{3}({4},{5})", fx.x.value, fx.x.fract, fx.y.value, fx.y.fract, ConvertUtil.FixedToFloat(fx.x), ConvertUtil.FixedToFloat(fx.y)); line.Points.Add(point); builder.AppendFormat("({0},{1}) ", point.X, point.Y); } polygon.Lines.Add(line); //--------------------- TreeNode lineNode = new TreeNode(builder.ToString()); lineNode.Tag = line; polygonNode.Nodes.Add(lineNode); //--------------------- } if (point != start) { //增加结束到开始的闭合线段 LineDrawing endLine = new BeelineDrawing(); endLine.Points.Add(point); endLine.Points.Add(start); polygon.Lines.Add(endLine); //--------------------- TreeNode endNode = new TreeNode(string.Format("直 ({0},{1}) ({0},{1}) ", point.X, point.Y, start.X, start.Y)); endNode.Tag = endLine; polygonNode.Nodes.Add(endNode); //--------------------- } //加入到字符轮廓的多边形列表中 word.Polygons.Add(polygon); } return(word); }