markus / FinalService / FinalServiceBase / MarkupToPDF / Common / CommonFont.cs @ 42d49521
이력 | 보기 | 이력해설 | 다운로드 (4.1 KB)
1 |
using iTextSharp.text.pdf; |
---|---|
2 |
using MarkusFont = Markus.Fonts; |
3 |
using System; |
4 |
using System.Collections.Generic; |
5 |
using System.IO; |
6 |
using System.Linq; |
7 |
using System.Text; |
8 |
using System.Windows; |
9 |
using System.Windows.Media; |
10 |
|
11 |
namespace MarkupToPDF.Common |
12 |
{ |
13 |
public static class CommonFont |
14 |
{ |
15 |
public static BaseFont CreateBaseFont(string name, string encoding, bool embedded, bool cached,Stream fontStream, byte[] pfb) |
16 |
{ |
17 |
var fontBytes = fontStream.ReadFully(); |
18 |
return BaseFont.CreateFont(name, encoding, embedded,cached, fontBytes, pfb); |
19 |
} |
20 |
|
21 |
public static BaseFont CreateBaseFont(System.Windows.Media.FontFamily fontFamily, string encoding, bool embedded, bool cached) |
22 |
{ |
23 |
var fileName = MarkusFont.FontDictionary.GetMarkusFont(fontFamily).ResourceName; |
24 |
|
25 |
return CreateBaseFont(fileName, encoding, embedded, cached); |
26 |
} |
27 |
|
28 |
public static BaseFont CreateBaseFont(string Name, string encoding, bool embedded, bool cached) |
29 |
{ |
30 |
try |
31 |
{ |
32 |
var fontStream = MarkusFont.FontHelper.GetFontStream(Name); |
33 |
|
34 |
var fontBytes = fontStream.ReadFully(); |
35 |
return BaseFont.CreateFont(Name, encoding, embedded, cached, fontBytes, null); |
36 |
} |
37 |
catch (Exception) |
38 |
{ |
39 |
throw new Exception($"CreateBaseFont Font {Name} Error" ); |
40 |
} |
41 |
} |
42 |
|
43 |
/// <summary> |
44 |
/// create a font with given parameters |
45 |
/// </summary> |
46 |
/// <param name="fontstyle"></param> |
47 |
/// <param name="fontweight"></param> |
48 |
/// <param name="isUnderline"></param> |
49 |
/// <param name="rect"></param> |
50 |
/// <param name="text"></param> |
51 |
/// <returns></returns> |
52 |
public static iTextSharp.text.Font CreateFont(FontFamily fontFamilly, FontStyle fontstyle, FontWeight fontweight, TextDecorationCollection isUnderline, iTextSharp.text.Rectangle rect, string text) |
53 |
{ |
54 |
///int iFonts = FontFactory.RegisterDirectory(Environment.GetFolderPath(Environment.SpecialFolder.Fonts)); |
55 |
//string sFontPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "Arial Unicode MS.TTF"); |
56 |
|
57 |
//bool bTextIsAscii = !text.Any(c => !Char.IsLetterOrDigit(c)); /// text가 ascii인지 확인 |
58 |
//if (bTextIsAscii) |
59 |
//{ |
60 |
// sFontPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "TIMES.TTF"); |
61 |
//} |
62 |
|
63 |
double dFontSize = double.MaxValue; |
64 |
|
65 |
BaseFont bf = CreateBaseFont(fontFamilly,BaseFont.IDENTITY_H, BaseFont.EMBEDDED, BaseFont.CACHED); |
66 |
|
67 |
string[] tokens = text.Split(Environment.NewLine.ToArray(), StringSplitOptions.RemoveEmptyEntries); |
68 |
foreach (var token in tokens) |
69 |
{ |
70 |
float width = bf.GetWidthPoint(token, rect.Height); |
71 |
double ratio = rect.Width / width; |
72 |
double size = rect.Height * ratio; |
73 |
dFontSize = size < dFontSize ? size : dFontSize; |
74 |
} |
75 |
|
76 |
iTextSharp.text.Font itextFont = new iTextSharp.text.Font(bf, (float)dFontSize - 0.8f); |
77 |
|
78 |
itextFont.SetStyle(0); |
79 |
if (fontstyle == FontStyles.Italic) |
80 |
{ |
81 |
//기울임 |
82 |
itextFont.SetStyle(itextFont.Style | iTextSharp.text.Font.ITALIC); |
83 |
} |
84 |
if (fontweight == FontWeights.Bold) |
85 |
{ |
86 |
//굵기 |
87 |
itextFont.SetStyle(itextFont.Style | iTextSharp.text.Font.BOLD); |
88 |
} |
89 |
|
90 |
if (isUnderline == TextDecorations.Underline) |
91 |
{ |
92 |
//밑줄; |
93 |
itextFont.SetStyle(itextFont.Style | iTextSharp.text.Font.UNDERLINE); |
94 |
} |
95 |
|
96 |
return itextFont; |
97 |
} |
98 |
|
99 |
public static byte[] ReadFully(this Stream input) |
100 |
{ |
101 |
using (MemoryStream ms = new MemoryStream()) |
102 |
{ |
103 |
input.Position = 0; |
104 |
|
105 |
input.CopyTo(ms); |
106 |
return ms.ToArray(); |
107 |
} |
108 |
} |
109 |
} |
110 |
} |