SysFontWin32.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #if ((UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN) || (UNITY_STANDALONE && (!UNITY_IOS) && (!UNITY_ANDROID)))
  2. using System;
  3. using CommonUI.Gemo;
  4. using UnityEngine;
  5. using System.Drawing.Text;
  6. using CommonLang;
  7. namespace CommonUI_Win32
  8. {
  9. public class SysFontWin32
  10. {
  11. private static SysFontWin32 _instance;
  12. public static SysFontWin32 Instance
  13. {
  14. get
  15. {
  16. if (_instance == null)
  17. {
  18. _instance = new SysFontWin32();
  19. }
  20. return _instance;
  21. }
  22. }
  23. public static System.Drawing.Graphics TestGFX { get { return Instance.testGFX; } }
  24. private System.Drawing.Bitmap testBuffer;
  25. private System.Drawing.Graphics testGFX ;
  26. private System.Drawing.FontFamily testFontFamily;
  27. private PrivateFontCollection loadFonts;
  28. private SysFontWin32()
  29. {
  30. this.testBuffer = new System.Drawing.Bitmap(100, 100);
  31. this.testGFX = System.Drawing.Graphics.FromImage(testBuffer);
  32. this.testFontFamily = new System.Drawing.FontFamily("微软雅黑");
  33. this.loadFonts = new PrivateFontCollection();
  34. }
  35. ~SysFontWin32()
  36. {
  37. loadFonts.Dispose();
  38. }
  39. public static void LoadFont(string filepath)
  40. {
  41. try
  42. {
  43. Instance.loadFonts.AddFontFile(filepath);
  44. Instance.testFontFamily = Instance.loadFonts.Families[0];
  45. }
  46. catch (Exception err) { Debug.LogException(err); }
  47. }
  48. public static void LoadFontBinary(byte[] data)
  49. {
  50. try
  51. {
  52. Instance.loadFonts.AddMemoryFont(CUtils.ToIntPtr(data), data.Length);
  53. Instance.testFontFamily = Instance.loadFonts.Families[0];
  54. }
  55. catch (Exception err) { Debug.LogException(err); }
  56. }
  57. public static System.Drawing.Font CreateFont(float size, CommonUI.Display.FontStyle style)
  58. {
  59. System.Drawing.FontStyle fs = System.Drawing.FontStyle.Regular;
  60. switch (style)
  61. {
  62. case CommonUI.Display.FontStyle.STYLE_BOLD:
  63. fs = System.Drawing.FontStyle.Bold;
  64. break;
  65. case CommonUI.Display.FontStyle.STYLE_ITALIC:
  66. fs = System.Drawing.FontStyle.Italic;
  67. break;
  68. case CommonUI.Display.FontStyle.STYLE_PLAIN:
  69. fs = System.Drawing.FontStyle.Regular;
  70. break;
  71. case CommonUI.Display.FontStyle.STYLE_UNDERLINED:
  72. fs = System.Drawing.FontStyle.Underline;
  73. break;
  74. }
  75. return new System.Drawing.Font(Instance.testFontFamily, size, fs, System.Drawing.GraphicsUnit.Pixel, 137);
  76. }
  77. static public System.Drawing.SizeF GetTextBounds(
  78. string text,
  79. System.Drawing.Font font,
  80. int borderTime,
  81. float expectWidth = 0)
  82. {
  83. System.Drawing.SizeF size;
  84. if (expectWidth > 0)
  85. {
  86. size = Instance.testGFX.MeasureString(text, font, (int)(expectWidth), System.Drawing.StringFormat.GenericTypographic);
  87. }
  88. else
  89. {
  90. size = Instance.testGFX.MeasureString(text, font, int.MaxValue, System.Drawing.StringFormat.GenericTypographic);
  91. }
  92. size.Width = Mathf.CeilToInt(size.Width + 3f);
  93. size.Height = Mathf.CeilToInt(size.Height + 3f);
  94. return size;
  95. }
  96. public static bool TestTextLineBreak(string text, float size, CommonUI.Display.FontStyle style,
  97. int borderTime,
  98. float testWidth,
  99. out float realWidth,
  100. out float realHeight)
  101. {
  102. realWidth = 0;
  103. realHeight = 0;
  104. testWidth = Mathf.CeilToInt(testWidth);
  105. System.Drawing.Font cur_font = CreateFont(size, style);
  106. System.Drawing.SizeF max = GetTextBounds(text, cur_font, borderTime, 0);
  107. realWidth = max.Width;
  108. realHeight = max.Height;
  109. if (testWidth > 0 && max.Width > testWidth)
  110. {
  111. System.Drawing.SizeF min = GetTextBounds(text, cur_font, borderTime, testWidth);
  112. realWidth = min.Width;
  113. realHeight = min.Height;
  114. return true;
  115. }
  116. return false;
  117. }
  118. public static Size2D SysFontTexture(
  119. string text,
  120. CommonUI.Display.FontStyle style,
  121. int fontSize,
  122. uint fontColor,
  123. int borderTime,
  124. uint borderColor,
  125. Size2D expectSize,
  126. out byte[] _pixelData,
  127. out int _pixelW,
  128. out int _pixelH)
  129. {
  130. System.Drawing.Font font = SysFontWin32.CreateFont(fontSize, style);
  131. System.Drawing.SizeF bounds = SysFontWin32.GetTextBounds(text, font, borderTime, expectSize != null ? expectSize.width : 0);
  132. System.Drawing.Bitmap src = SysFontWin32.GenStringBuffer(
  133. Mathf.CeilToInt(bounds.Width),
  134. Mathf.CeilToInt(bounds.Height),
  135. text, font, fontColor, borderTime, borderColor);
  136. try
  137. {
  138. bounds.Width = src.Width;
  139. bounds.Height = src.Height;
  140. _pixelW = src.Width;
  141. _pixelH = src.Height;
  142. _pixelData = new byte[_pixelW * _pixelH * 4];
  143. System.Drawing.Color pixel;
  144. int pos;
  145. for (int y = 0; y < _pixelH; y++)
  146. {
  147. for (int x = 0; x < _pixelW; x++)
  148. {
  149. pixel = src.GetPixel(x, _pixelH - y - 1);
  150. pos = (x + y * _pixelW) * 4;
  151. _pixelData[pos + 0] = (pixel.R);
  152. _pixelData[pos + 1] = (pixel.G);
  153. _pixelData[pos + 2] = (pixel.B);
  154. _pixelData[pos + 3] = (pixel.A);
  155. }
  156. }
  157. }
  158. finally
  159. {
  160. src.Dispose();
  161. }
  162. return new Size2D(bounds.Width, bounds.Height);
  163. }
  164. static public System.Drawing.Bitmap GenStringBuffer(
  165. int w, int h, string text,
  166. System.Drawing.Font font,
  167. uint fontColor,
  168. int borderTime,
  169. uint borderColor)
  170. {
  171. System.Drawing.Bitmap src = new System.Drawing.Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  172. using (System.Drawing.Graphics gfx = System.Drawing.Graphics.FromImage(src))
  173. {
  174. gfx.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
  175. gfx.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
  176. gfx.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  177. gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
  178. gfx.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
  179. System.Drawing.SolidBrush bbrush = new System.Drawing.SolidBrush(
  180. System.Drawing.Color.FromArgb((int)CommonUI.Display.Color.toARGB(borderColor)));
  181. System.Drawing.SolidBrush fbrush = new System.Drawing.SolidBrush(
  182. System.Drawing.Color.FromArgb((int)CommonUI.Display.Color.toARGB(fontColor)));
  183. //test board
  184. //gfx.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.Red), 0, 0, w - 1, h - 1);
  185. float[,] offset_8 =
  186. {
  187. { 0, 0},{ 1, 0},{ 2, 0},
  188. { 0, 1},/*1, 1*/{ 2, 1},
  189. { 0, 2},{ 1, 2},{ 2, 2}
  190. };
  191. float[,] offset_4 =
  192. {
  193. /*0, 0*/{ 1, 0},/*2, 0*/
  194. { 0, 1},/*1, 1*/{ 2, 1},
  195. /*0, 2*/{ 1, 2},/*2, 2*/
  196. };
  197. System.Drawing.RectangleF expectRect = new System.Drawing.RectangleF(1f, 1f, w - 1f, h - 1f);
  198. CommonUI.Data.TextBorderCount bt = (CommonUI.Data.TextBorderCount)borderTime;
  199. switch (bt)
  200. {
  201. case CommonUI.Data.TextBorderCount.Border_4:
  202. for (int i = 0; i < 4; i++)
  203. {
  204. SysFontWin32.DrawString(text, gfx, font, bbrush, expectRect, offset_4[i, 0], offset_4[i, 1]);
  205. }
  206. break;
  207. case CommonUI.Data.TextBorderCount.Border:
  208. for (int i = 0; i < 8; i++)
  209. {
  210. SysFontWin32.DrawString(text, gfx, font, bbrush, expectRect, offset_8[i, 0], offset_8[i, 1]);
  211. }
  212. break;
  213. case CommonUI.Data.TextBorderCount.Shadow:
  214. SysFontWin32.DrawString(text, gfx, font, bbrush, expectRect, 1, 2);
  215. break;
  216. case CommonUI.Data.TextBorderCount.Shadow_L_T:
  217. SysFontWin32.DrawString(text, gfx, font, bbrush, expectRect, 0, 0);
  218. break;
  219. case CommonUI.Data.TextBorderCount.Shadow_C_T:
  220. SysFontWin32.DrawString(text, gfx, font, bbrush, expectRect, 1, 0);
  221. break;
  222. case CommonUI.Data.TextBorderCount.Shadow_R_T:
  223. SysFontWin32.DrawString(text, gfx, font, bbrush, expectRect, 2, 0);
  224. break;
  225. case CommonUI.Data.TextBorderCount.Shadow_L_C:
  226. SysFontWin32.DrawString(text, gfx, font, bbrush, expectRect, 0, 1);
  227. break;
  228. case CommonUI.Data.TextBorderCount.Shadow_C_C:
  229. break;
  230. case CommonUI.Data.TextBorderCount.Shadow_R_C:
  231. SysFontWin32.DrawString(text, gfx, font, bbrush, expectRect, 2, 1);
  232. break;
  233. case CommonUI.Data.TextBorderCount.Shadow_L_B:
  234. SysFontWin32.DrawString(text, gfx, font, bbrush, expectRect, 0, 2);
  235. break;
  236. case CommonUI.Data.TextBorderCount.Shadow_C_B:
  237. SysFontWin32.DrawString(text, gfx, font, bbrush, expectRect, 1, 2);
  238. break;
  239. case CommonUI.Data.TextBorderCount.Shadow_R_B:
  240. SysFontWin32.DrawString(text, gfx, font, bbrush, expectRect, 2, 2);
  241. break;
  242. }
  243. SysFontWin32.DrawString(text, gfx, font, fbrush, expectRect, 1, 1);
  244. }
  245. return src;
  246. }
  247. //-------------------------------------------------------------------------------------------------------------------
  248. static private void DrawString(
  249. string text,
  250. System.Drawing.Graphics gfx,
  251. System.Drawing.Font font,
  252. System.Drawing.SolidBrush brush,
  253. System.Drawing.RectangleF expectRect,
  254. float x, float y)
  255. {
  256. gfx.TranslateTransform(x, y);
  257. gfx.DrawString(text, font, brush, expectRect, System.Drawing.StringFormat.GenericTypographic);
  258. gfx.TranslateTransform(-x, -y);
  259. }
  260. }
  261. }
  262. #endif