arcengine 读取字体库

转载自:http://blog.csdn.net/xuchenhuics/article/details/17715557



字体文件可以储存文字、图片等符号,通过ArcMap的符号配置工具可以使用字体文件(ttf格式)中包含的图片或符号。在Arc Engine中,也可以使用ICharacterMarkerSymbol接口根据字体文件生成对应的符号,通过ICharacterMarkerSymbol.CharacterIndex属性,设置符号对应的unicode可以生成对应的字体符号。但在实际应用中,用户是不可能知道符号与unicode编码的映射关系。因此,需要将ttf文件中的符号以图形化的方式显示出来,让用户自主选择生成ICharacterMarkerSymbol对象。本文将介绍读取ttf文件中符号及绘制方法,并最终生成CharacterMarkerSymbol


一、字体文件(ttf)的格式


Ttf文件的结构共包括四部分,字体头、位置索引、图元数据和图元指令。在我们上述的讨论中,我们只需要关注字体头部分,就可以满足我们的要求。(详细信息请参考:http://venrar.blog.163.com/blog/static/60699459200881102648855/


通过调用Win32 API 函数:GetFontUnicodeRanges,我们可以获得支持的图元的数量、支持的UNICODE区域的数量以及设备上下文中字体的这些区域的详细信息,上述信息以GLYPHSET结构返回。 GetFontUnicodeRanges函数通常需要调用两 次。第一次调用时得到以NULL指针作为最后一个参数,GDI会返回所需内存的大小。调用者然后分配所需的内存,再次调用以得到真正的数据。下面是C#里调用该函数的方法:
首先定义一个FontRange对象:
  1. public class FontRange
  2. {
  3. public FontRange (ushort first,ushort count )
  4. {
  5. this.first = first;
  6. end = (uint) first + (uint) count;
  7. }
  8. private ushort first;
  9. private uint end;
  10. public ushort Count
  11. {
  12. get {return (ushort) (end - (uint) first);}
  13. }
  14. public uint End
  15. {
  16. get {return end;}
  17. }
  18. public ushort First
  19. {
  20. get {return first;}
  21. }
  22. public ushort Last
  23. {
  24. get {return (ushort) (end - 1UL);}
  25. }
  26. public bool Contains (
  27. ushort codePoint )
  28. {
  29. return (codePoint >= first && (uint) codePoint < end);
  30. }
  31. }
复制代码

然后调用GetFontUnicodeRanges函数返回,下面是获取字体范围的关键方法,完整代码见附件。

  1. public FontGlyphSet (Font font )
  2. {
  3.             IntPtr glyphSetData = IntPtr.Zero;
  4.             IntPtr savedFont = HGDI_ERROR;
  5.             IntPtr hdc = IntPtr.Zero;
  6.             Graphics g = null;
  7.             try 
  8.             {
  9.                 g = Graphics.FromHwnd(IntPtr.Zero);
  10.                 hdc = g.GetHdc();
  11.                 IntPtr hFont = font.ToHfont();
  12.                 
  13.                 savedFont = SelectObject(hdc, hFont);
  14.                 if (savedFont == HGDI_ERROR)
  15.                     throw new Exception(
  16.                         "Unexpected failure of SelectObject.");
  17.                  //第一次调用,返回所需内存
  18.                 size = GetFontUnicodeRanges(hdc, IntPtr.Zero);
  19.                 if (size == 0)
  20.                     throw new Exception(
  21.                         "Unexpected failure of GetFontUnicodeRanges.");
  22.                  //分配内存
  23.                 glyphSetData = Marshal.AllocHGlobal((int) size);
  24.                  //第二次调用,获取范围信息
  25.                 if (GetFontUnicodeRanges(hdc, glyphSetData) == 0)
  26.                     throw new Exception(
  27.                         "Unexpected failure of GetFontUnicodeRanges.");
  28.                  //从内存里提取全局的范围信息
  29.                 int offset = uintSize;
  30.                 flags = (uint) Marshal.ReadInt32(
  31.                     glyphSetData, offset);
  32.                 offset += uintSize;

  33.                 codePointCount = (uint) Marshal.ReadInt32(
  34.                     glyphSetData, offset);
  35.                 offset += uintSize;
  36.                     uint rangeCount = (uint) Marshal.ReadInt32(
  37.                     glyphSetData, offset);
  38.                 offset += uintSize;
  39.                 ranges = new FontRange[rangeCount];
  40.                  //从内存提取范围信息,返回FontRange对象,一个字体文件包括多个Unicode范围
  41.                 for(uint index = 0; index < rangeCount; index++)
  42.                 {
  43.                     ushort first = (ushort) Marshal.ReadInt16(
  44.                         glyphSetData, offset);
  45.                     offset += Marshal.SizeOf(typeof(ushort));
  46.                     ushort count = (ushort) Marshal.ReadInt16(
  47.                         glyphSetData, offset);
  48.                     offset += Marshal.SizeOf(typeof(ushort));
  49.                     ranges[index] = new FontRange(first, count);
  50.                 }
  51.             }       
  52. }
复制代码

二、符号绘制
符号绘制的关键是我们如何根据获得的Unicode值绘制对应的符号。有两种方式可以实现:
1、  将Unicode赋值给ICharacterMarkerSymbol.CharacterIndex ,生成CharacterMarkerSymbol对象,然后通过ISymbol.Draw方法将符号绘制到窗体上;
2、  使用.Net里的Graphics.DrawString()方法将符号绘制到窗体上。
这两种方法效果类似,但第一种方法将创建上百个CharacterMarkerSymbol,而且必须缓存这些对象(因为窗体上的绘制方法会频繁调用,缓存可以提高响应效率)。所以,我选择了第二种方法将符号绘制到窗体上。
为什么使用DrawString()方法可以绘制符号呢?我们都知道,电脑上的文字都有一个编码,我们可以把绘制文字的过程理解为电脑根据这个编码去字体库里找到相应的图片,然后绘制到屏幕上。在.net里,Char类型和int类型是可以转换的。因此,用如下代码就可以依据Unicode值绘制ttf文件里的符号:
  1. Private void DrawFontMarker(Font font, int unicode ,Graphics graph, Rectangle rect,Brush fontBrush)
  2. {
  3. char marker=(char)unicode;
  4. graph.DrawString(marker.ToString(),font,fontBrush,rect);
  5. }
复制代码

实现了单个符号的绘制,对于多个符号,我们只需要依据步骤一里获得Unicode范围,用遍历的方法逐个绘制,即可将所有符号绘制到窗体上,并记录每个符号匹配的Unicode


在绘制所有的符号时,要注意窗体的刷新范围,尽量使用局部重绘,这样可以提高绘制效率,避免窗体闪烁。


三、CharacterMarkerSymbol生成


CharacterMarkerSymbol的生成就比较简单。依据用户的操作,获得字体对象FontUnicode编码,代码如下:


  1. ICharacterMarkerSymbol symbol=new CharacterMarkerSymbolClass();
  2. m_ symbol.Font = OLE.GetIFontDispFromFont(font) as stdole.IFontDisp; 
  3. m_ symbol.CharacterIndex =unicode;
复制代码
下图为基于Engine开发的符号选择器中通过字体文件配置点状符号的效果图。

猜你喜欢

转载自blog.csdn.net/wangtao510/article/details/53373617