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
|
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public static class FontLoader
{
public static FontData FromBinary(TextAsset dataSrc, FontExtensionData eData)
{
FontData fontData = new FontData();
using (MemoryStream memoryStream = new MemoryStream(dataSrc.bytes))
{
using (BinaryReader binaryReader = new BinaryReader(memoryStream))
{
if (memoryStream.ReadByte() != 66 || memoryStream.ReadByte() != 77 || memoryStream.ReadByte() != 70 || memoryStream.ReadByte() != 3)
{
throw new InvalidDataException("Wrong format font.");
}
long num = -1L;
while (memoryStream.Position < memoryStream.Length)
{
if (num == memoryStream.Position)
{
throw new InvalidDataException("Bad font.");
}
num = memoryStream.Position;
byte b = binaryReader.ReadByte();
int num2 = binaryReader.ReadInt32();
long position = memoryStream.Position;
switch (b)
{
default:
memoryStream.Position += (long)num2;
break;
case 2:
fontData.LineHeight = (float)binaryReader.ReadUInt16();
memoryStream.Position += 2L;
fontData.TextureSize = new Vector2((float)binaryReader.ReadUInt16(), (float)binaryReader.ReadUInt16());
memoryStream.Position = position + (long)num2;
break;
case 4:
{
int num3 = num2 / 20;
fontData.charMap = new Dictionary<int, int>(num3);
fontData.bounds.Capacity = num3;
fontData.offsets.Capacity = num3;
fontData.Channels.Capacity = num3;
fontData.kernings = new Dictionary<int, Dictionary<int, float>>(256);
for (int i = 0; i < num3; i++)
{
int key = binaryReader.ReadInt32();
int num4 = (int)binaryReader.ReadUInt16();
int num5 = (int)binaryReader.ReadUInt16();
int num6 = (int)binaryReader.ReadUInt16();
int num7 = (int)binaryReader.ReadUInt16();
int num8 = (int)binaryReader.ReadInt16();
int num9 = (int)binaryReader.ReadInt16();
int num10 = (int)binaryReader.ReadInt16();
binaryReader.ReadByte();
int input = (int)binaryReader.ReadByte();
fontData.charMap.Add(key, fontData.bounds.Count);
fontData.bounds.Add(new Vector4((float)num4, (float)num5, (float)num6, (float)num7));
fontData.offsets.Add(new Vector3((float)num8, (float)num9, (float)num10));
fontData.Channels.Add(FontLoader.IntToChannels(input));
}
break;
}
case 5:
while (memoryStream.Position < position + (long)num2)
{
int key2 = binaryReader.ReadInt32();
int key3 = binaryReader.ReadInt32();
int num11 = (int)binaryReader.ReadInt16();
Dictionary<int, float> dictionary;
if (!fontData.kernings.TryGetValue(key2, out dictionary))
{
fontData.kernings.Add(key2, dictionary = new Dictionary<int, float>(256));
}
dictionary.Add(key3, (float)num11);
}
break;
}
}
}
}
if (eData != null)
{
eData.AdjustKernings(fontData);
eData.AdjustOffsets(fontData);
}
return fontData;
}
private static Vector4 IntToChannels(int input)
{
Vector4 result = default(Vector4);
for (int i = 0; i < 4; i++)
{
if ((input >> i & 1) == 1)
{
result[i] = 1f;
}
}
return result;
}
}
|