We loaded three different font streams into a PrivateFontCollection, printed the names of each font family, and counted the number of fonts once after adding all the font streams.
We repeated this process 10 times. When adding these font streams continuously to the PrivateFontCollection for each iteration, sometimes a font was missing from the collection. For example, the collection sometimes showed only two fonts added.
Please provide me with a solution to have a consistent output using PrivateFontCollection.
Note: Our requirement is to add font stream into the private font collection.
Code snippet:
|string[] fontList = new string[] { @"arial.ttf", @"calibri.ttf", @"verdana.ttf" };
PrivateFontCollection fontCollection = null;
for (int i = 0; i < 10; i++)
{
fontCollection = new PrivateFontCollection();
foreach (string fontFile in fontList)
{
if (!File.Exists(fontFile)) continue;
Console.WriteLine("Font name: " + fontFile);
Stream fontStream = File.OpenRead(fontFile);
fontStream.Position = 0;
byte[] fontdata = new byte[fontStream.Length];
fontStream.Position = 0;
//read the font data from the ttf stream
fontStream.Read(fontdata,0, (int)fontStream.Length);
IntPtr data = Marshal.AllocCoTaskMem((int)fontStream.Length);
Marshal.Copy(fontdata,0, data, (int)fontStream.Length);
fontCollection.AddMemoryFont(data,(int)fontStream.Length);
}
Console.WriteLine("Total Number of fonts added in the
PrivateFontCollection: " + fontCollection.Families.Length);
for (int k = 0; k < fontCollection.Families.Length; k++)
Console.WriteLine(i
- " Iteration and
families Name: " + k +
" - " + fontCollection.Families[k].Name);
fontCollection.Dispose();
fontCollection
= null;
}|
| -------- |
||
Actual Output:
Total Number of fonts added in the PrivateFontCollection: 3
0 Iteration and families Name: 0 - Arial
0 Iteration and families Name: 1 - Calibri
0 Iteration and families Name: 2 - Verdana
Total Number of fonts added in the PrivateFontCollection: 3
1 Iteration and families Name: 0 - Arial
1 Iteration and families Name: 1 - Calibri
1 Iteration and families Name: 2 - Verdana
Total Number of fonts added in the PrivateFontCollection: 3
2 Iteration and families Name: 0 - Arial
2 Iteration and families Name: 1 - Calibri
2 Iteration and families Name: 2 - Verdana
Total Number of fonts added in the PrivateFontCollection: 3
3 Iteration and families Name: 0 - Arial
3 Iteration and families Name: 1 - Calibri
3 Iteration and families Name: 2 - Verdana
Total Number of fonts added in the PrivateFontCollection: 3
4 Iteration and families Name: 0 - Arial
4 Iteration and families Name: 1 - Calibri
4 Iteration and families Name: 2 - Verdana
Total Number of fonts added in the PrivateFontCollection: 3
5 Iteration and families Name: 0 - Arial
5 Iteration and families Name: 1 - Calibri
5 Iteration and families Name: 2 - Verdana
Total Number of fonts added in the PrivateFontCollection: 3
6 Iteration and families Name: 0 - Arial
6 Iteration and families Name: 1 - Calibri
6 Iteration and families Name: 2 - Verdana
Total Number of fonts added in the PrivateFontCollection: 3
7 Iteration and families Name: 0 - Arial
7 Iteration and families Name: 1 - Calibri
7 Iteration and families Name: 2 - Verdana
Total Number of fonts added in the PrivateFontCollection: 2
8 Iteration and families Name: 0 - Calibri
8 Iteration and families Name: 1 - Verdana
Total Number of fonts added in the PrivateFontCollection: 2
9 Iteration and families Name: 0 - Arial
9 Iteration and families Name: 1 - Calibri
Expected output:
Total Number of fonts added in the PrivateFontCollection: 3
0 Iteration and families Name: 0 - Arial
0 Iteration and families Name: 1 - Calibri
0 Iteration and families Name: 2 - Verdana
Total Number of fonts added in the PrivateFontCollection: 3
1 Iteration and families Name: 0 - Arial
1 Iteration and families Name: 1 - Calibri
1 Iteration and families Name: 2 - Verdana
Total Number of fonts added in the PrivateFontCollection: 3
2 Iteration and families Name: 0 - Arial
2 Iteration and families Name: 1 - Calibri
2 Iteration and families Name: 2 - Verdana
Total Number of fonts added in the PrivateFontCollection: 3
3 Iteration and families Name: 0 - Arial
3 Iteration and families Name: 1 - Calibri
3 Iteration and families Name: 2 - Verdana
Total Number of fonts added in the PrivateFontCollection: 3
4 Iteration and families Name: 0 - Arial
4 Iteration and families Name: 1 - Calibri
4 Iteration and families Name: 2 - Verdana
Total Number of fonts added in the PrivateFontCollection: 3
5 Iteration and families Name: 0 - Arial
5 Iteration and families Name: 1 - Calibri
5 Iteration and families Name: 2 - Verdana
Total Number of fonts added in the PrivateFontCollection: 3
6 Iteration and families Name: 0 - Arial
6 Iteration and families Name: 1 - Calibri
6 Iteration and families Name: 2 - Verdana
Total Number of fonts added in the PrivateFontCollection: 3
7 Iteration and families Name: 0 - Arial
7 Iteration and families Name: 1 - Calibri
7 Iteration and families Name: 2 - Verdana
Total Number of fonts added in the PrivateFontCollection: 3
8 Iteration and families Name: 0 - Arial
8 Iteration and families Name: 1 - Calibri
8 Iteration and families Name: 2 - Verdana
Total Number of fonts added in the PrivateFontCollection: 3
9 Iteration and families Name: 0 - Arial
9 Iteration and families Name: 1 - Calibri
9 Iteration and families Name: 2 - Verdana