markus / Markus.Fonts / FontHelper.cs @ 3dbace4e
이력 | 보기 | 이력해설 | 다운로드 (2.01 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 |
Uri uri = null; |
27 |
|
28 |
if(UriParser.IsKnownScheme("pack")) |
29 |
{ |
30 |
uri = new Uri($"pack://application:,,,/{fontAssemblyName};component/"); |
31 |
} |
32 |
else |
33 |
{ |
34 |
uri = new Uri($"ms-appx:///{fontAssemblyName};component/"); |
35 |
} |
36 |
|
37 |
return new System.Windows.Media.FontFamily(uri,$"Resources/#{markusFont.FontFamilyName}"); |
38 |
} |
39 |
|
40 |
public static Stream GetFontStream(string fileName) |
41 |
{ |
42 |
Assembly asm = Assembly.GetExecutingAssembly(); |
43 |
|
44 |
return asm.GetManifestResourceStream($"Markus.Fonts.Resources.{fileName}"); |
45 |
} |
46 |
|
47 |
public static string FontName(this System.Windows.Media.FontFamily fontFamily) |
48 |
{ |
49 |
string result = fontFamily.ToString(); |
50 |
|
51 |
try |
52 |
{ |
53 |
var family = fontFamily.ToString(); |
54 |
|
55 |
if (family.IndexOf('#') > 0) |
56 |
{ |
57 |
result = family.Substring(family.IndexOf('#')+ 1); |
58 |
} |
59 |
} |
60 |
catch (Exception ex) |
61 |
{ |
62 |
throw new Exception("FontName Error" + ex.ToString()); |
63 |
} |
64 |
|
65 |
//System.Diagnostics.Debug.WriteLine($"Font Name {result}"); |
66 |
|
67 |
return result; |
68 |
} |
69 |
} |
70 |
} |