/// <summary> /// �ӻ���������������䵽��������(һ���������������������Σ�ÿ�����������ɸ�ֱ���������) /// </summary> /// <param name="outline">����</param> /// <param name="buffer">������ָ��</param> /// <param name="bufferSize">��������С</param> /// <returns></returns> private static void FillPolygons(DOutline outline, IntPtr buffer, int bufferSize) { //�����ͷ��С int polygonHeaderSize = Marshal.SizeOf(typeof(TTPOLYGONHEADER)); //�ߴ�С int lineSize = Marshal.SizeOf(typeof(TTPOLYCURVEHEAD)); //���С int pointFxSize = Marshal.SizeOf(typeof(POINTFX)); //��������ֵַ int ptr = buffer.ToInt32(); //ƫ���� int offset = 0; //�����Ķ�����б� IList<DPolygon> polygons = outline.Polygons; while (offset < bufferSize) { //�����ͷ��Ϣ TTPOLYGONHEADER header = (TTPOLYGONHEADER)Marshal.PtrToStructure(new IntPtr(ptr + offset), typeof(TTPOLYGONHEADER)); //��������� DPolygon polygon = new DPolygon(header.dwType); //��ʼ�� polygon.Start = header.pfxStart; //��ȡβ���� int endCurvesIndex = offset + header.cb; //���ƫ��һ���� offset += polygonHeaderSize; while (offset < endCurvesIndex) { //�߶���Ϣ TTPOLYCURVEHEAD lineHeader = (TTPOLYCURVEHEAD)Marshal.PtrToStructure(new IntPtr(ptr + offset), typeof(TTPOLYCURVEHEAD)); //ƫ�Ƶ���������ַ offset += lineSize; //�����߶� DLine line = new DLine(lineHeader.wType); //��ȡ�����У������߶��� for (int i = 0; i < lineHeader.cpfx; i++) { POINTFX point = (POINTFX)Marshal.PtrToStructure(new IntPtr(ptr + offset), typeof(POINTFX)); //��������߶� line.Points.Add(point); //ƫ�� offset += pointFxSize; } //���������� polygon.Lines.Add(line); } //������μ������� polygons.Add(polygon); } }
/// <summary> /// ��ȡ����������б� /// </summary> /// <param name="hdc">����</param> /// <param name="uChar">�ַ�</param> /// <returns></returns> private static DOutline GetOutline(IntPtr hdc, uint uChar) { // ת�þ��� MAT2 mat2 = new MAT2(); mat2.eM11.value = 1; mat2.eM22.value = 1; GLYPHMETRICS lpGlyph = new GLYPHMETRICS(); //��ȡ��������С int bufferSize = GdiNativeMethods.GetGlyphOutline(hdc, uChar, GGO_NATIVE, out lpGlyph, 0, IntPtr.Zero, ref mat2); if (bufferSize > 0) { //��ȡ�ɹ������й��ڴ� IntPtr buffer = Marshal.AllocHGlobal(bufferSize); try { int ret = GdiNativeMethods.GetGlyphOutline(hdc, uChar, GGO_NATIVE, out lpGlyph, (uint)bufferSize, buffer, ref mat2); if (ret > 0) { //�������� DOutline outline = new DOutline(lpGlyph.gmBlackBoxX, lpGlyph.gmBlackBoxY); //�ӻ����������������� FillPolygons(outline, buffer, bufferSize); return outline; } else { throw new Exception("��ȡ��������ʧ�ܣ�"); } } finally { //�ͷ��й��ڴ� Marshal.FreeHGlobal(buffer); } } else { throw new Exception("δ�ܻ�ȡ��������"); } }
/// <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; }