markus / Markus.Fonts / FontHelper.cs @ 24c5e56c
이력 | 보기 | 이력해설 | 다운로드 (1.79 KB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.IO; |
4 |
using System.Linq; |
5 |
using System.Reflection; |
6 |
using System.Text; |
7 |
using System.Threading.Tasks; |
8 |
|
9 |
namespace Markus.Fonts |
10 |
{ |
11 |
public static class FontHelper |
12 |
{ |
13 |
public static System.Windows.Media.FontFamily GetFontFamily(string name) |
14 |
{ |
15 |
MarkusFont markusFont = Markus.Fonts.FontDictionary.GetMarkusFonts().Find(x => x.IsDefault); |
16 |
|
17 |
var fonts = Markus.Fonts.FontDictionary.GetMarkusFonts().Where(x => x.FamilyNames.Contains(name)); |
18 |
|
19 |
if(fonts.Count() > 0) |
20 |
{ |
21 |
markusFont = fonts.First(); |
22 |
} |
23 |
|
24 |
string fontAssemblyName = Assembly.GetAssembly(typeof(Markus.Fonts.FontHelper)).GetName().Name; |
25 |
|
26 |
var uri = new Uri($"pack://application:,,,/{fontAssemblyName};component/"); |
27 |
|
28 |
return new System.Windows.Media.FontFamily(uri,$"Resources/#{markusFont.FontFamilyName}"); |
29 |
} |
30 |
|
31 |
public static Stream GetFontStream(string fileName) |
32 |
{ |
33 |
Assembly asm = Assembly.GetExecutingAssembly(); |
34 |
|
35 |
return asm.GetManifestResourceStream($"Markus.Fonts.Resources.{fileName}"); |
36 |
} |
37 |
|
38 |
public static string FontName(this System.Windows.Media.FontFamily fontFamily) |
39 |
{ |
40 |
string result = fontFamily.ToString(); |
41 |
|
42 |
try |
43 |
{ |
44 |
var family = fontFamily.ToString(); |
45 |
|
46 |
if (family.IndexOf('#') > 0) |
47 |
{ |
48 |
result = family.Substring(family.IndexOf('#')+ 1); |
49 |
} |
50 |
} |
51 |
catch (Exception ex) |
52 |
{ |
53 |
throw new Exception("FontName Error" + ex.ToString()); |
54 |
} |
55 |
|
56 |
System.Diagnostics.Debug.WriteLine($"Font Name {result}"); |
57 |
|
58 |
return result; |
59 |
} |
60 |
} |
61 |
} |