-
Notifications
You must be signed in to change notification settings - Fork 82
/
FormTestTrimmableFeature.cs
119 lines (98 loc) · 4.1 KB
/
FormTestTrimmableFeature.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//MIT, 2016-present, WinterDev
using System;
using System.IO;
using System.Windows.Forms;
using Typography.OpenFont;
using Typography.OpenFont.Extensions;
using Typography.OpenFont.Trimmable;
namespace SampleWinForms
{
public partial class FormTestTrimmableFeature : Form
{
public FormTestTrimmableFeature()
{
InitializeComponent();
}
private void FormTestTrimmableFeature_Load(object sender, EventArgs e)
{
}
static void TestLoadAndReload(string filename)
{
//Trimmable feature tests:
//[A] read the font file as usual => get full information about the font
Typeface typeface = null;
using (FileStream fs = new FileStream(filename, FileMode.Open))
{
//read in full mode
OpenFontReader openFontReader = new OpenFontReader();
typeface = openFontReader.Read(fs);
}
//before
bool hasColor1 = typeface.HasColorTable();
bool hasSvg1 = typeface.HasSvgTable();
bool hasCff1 = typeface.IsCffFont;
TrimMode glyphMode1 = typeface.GetTrimMode();
Glyph g1_1 = typeface.GetGlyph(1);
//---------------------------------------------------------
//[B] if you create paths from glyphs, or atlas from glyph
// and you don't want any glyph-building-detail (eg. to reduce memory usuage)
// but you still want to use the typeface for text-layout
// you can trim it down
RestoreTicket ticket = typeface.TrimDown();//***
//[C] you can GetGlyph() but this is ANOTHER NEW GLYPH
//without building instruction( eg. no cff,ttf,svg data,bitmap)
Glyph g1_2 = typeface.GetGlyph(1);
//** if you cache the the old version of 'full-info' glyph**
// the info is still cache on the old glyph and it can be used as 'full-info' glyph
// TrimDown() DOES NOT go to delete that glyph.
bool hasColor2 = typeface.HasColorTable();
bool hasSvg2 = typeface.HasSvgTable();
bool hasCff2 = typeface.IsCffFont;
TrimMode glyphMode2 = typeface.GetTrimMode();
//---------------------------------------------------------
//[D] can we load glyph detail again?
//yes=> this need 'ticket' from latest TrimDown()
//if you don't have it, you can't restore it.
using (FileStream fs = new FileStream(filename, FileMode.Open))
{
typeface.RestoreUp(ticket, fs);
}
}
//-------
//test incorrect restore
void TestIncorrectRestore(string file1, string file2)
{
Typeface typeface = null;
using (FileStream fs = new FileStream(file1, FileMode.Open))
{
//read in full mode
OpenFontReader openFontReader = new OpenFontReader();
typeface = openFontReader.Read(fs);
}
RestoreTicket ticket = typeface.TrimDown();
//test restore with incorrect file
using (FileStream fs = new FileStream(file2, FileMode.Open))
{
typeface.RestoreUp(ticket, fs);
}
}
private void cmdTestReloadGlyphs_Click(object sender, EventArgs e)
{
// string filename = "Test/SourceSansPro-Regular.ttf";
string[] files = new string[]
{
"Test/TwitterColorEmoji-SVGinOT.ttf" ,//svg in opentype font
"Test/NotoColorEmoji.ttf", //embeded bitmap font
"Test/SourceSansPro-Regular.ttf", //ttf
"Test/latinmodern-math.otf", //otf (cff)
"Test/Sarabun-Regular.woff2", //woff2
"Test/Sarabun-Regular.woff", //woff
};
TestIncorrectRestore(files[0], files[1]);
foreach (string filename in files)
{
TestLoadAndReload(filename);
}
}
}
}