Skip to content

Commit a68c7e7

Browse files
committed
docs(Font): add documenation
1 parent 74ba181 commit a68c7e7

1 file changed

Lines changed: 130 additions & 2 deletions

File tree

ZenKit/Font.cs

Lines changed: 130 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,132 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Numerics;
45
using System.Runtime.InteropServices;
56
using ZenKit.Util;
67

78
namespace ZenKit
89
{
10+
/// <summary>
11+
/// A single font glyph.
12+
/// </summary>
913
[Serializable]
1014
[StructLayout(LayoutKind.Sequential)]
1115
public struct FontGlyph
1216
{
17+
/// <summary>
18+
/// The width of the glyph in pixels.
19+
/// </summary>
1320
[MarshalAs(UnmanagedType.U1)] public byte width;
21+
22+
/// <summary>
23+
/// <b>The position of the top left corner of the glyph in the font texture.</b>
24+
/// This value is not stored as absolute pixels but rather in percent of the width and
25+
/// height of the image. Thus to calculate the real pixel position of the top left corner,
26+
/// one multiplies `topLeft.x` by the width of the font texture and `topLeft.y` by its height.
27+
/// </summary>
28+
/// <seealso href="https://zk.gothickit.dev/library/api/font/#dealing-with-glyphs" />
1429
public Vector2 topLeft;
30+
31+
/// <summary>
32+
/// <b>The position of the bottom right corner of the glyph in the font texture.</b>
33+
/// This value is not stored as absolute pixels but rather in percent of the width and
34+
/// height of the image. Thus to calculate the real pixel position of the bottom right corner,
35+
/// one multiplies `bottomRight.x` by the width of the font texture and `bottomRight.y` by its height.
36+
/// </summary>
37+
/// <seealso href="https://zk.gothickit.dev/library/api/font/#dealing-with-glyphs" />
1538
public Vector2 bottomRight;
1639
}
1740

41+
/// <summary>
42+
/// <b>Represents a ZenGin font file.</b>
43+
/// Fonts in the <i>ZenGin</i> consist of a font definition file and a font texture. This class represents the former.
44+
/// Font
45+
/// definition files contain a set of glyphs which define the extents of a
46+
/// <a href="https://en.wikipedia.org/wiki/Windows-1252">Windows-1252</a> encoded character within the font texture
47+
/// file. Font files can be identified most easily by their <c>.FNT</c> extension or alternatively through the
48+
/// <c>"1\n"</c>
49+
/// string at the beginning of the file.
50+
/// </summary>
51+
/// <seealso href="https://zk.gothickit.dev/library/api/font/" />
1852
public interface IFont : ICacheable<IFont>
1953
{
54+
/// <summary>
55+
/// The name of this font.
56+
/// </summary>
2057
public string Name { get; }
58+
59+
/// <summary>
60+
/// The height of each glyph of this font in pixels.
61+
/// </summary>
2162
public int Height { get; }
63+
64+
/// <summary>
65+
/// <b>All glyphs of this font.</b>
66+
/// <i>ZenGin</i> fonts contain characters present in the
67+
/// <a href="https://en.wikipedia.org/wiki/Windows-1252">Windows-1252 character encoding</a>
68+
/// which is generally used by <i>Gothic</i> and <i>Gothic II</i>. The returned list contains these glyphs in order.
69+
/// Some glyphs may not actually be present in the font texture. For those glyphs, the stored UV-coordinates will be
70+
/// invalid in some way (ie. they may point to a pixel not actually in the texture or be negative).
71+
/// </summary>
72+
/// <remarks>
73+
/// Repeated access to this property will lead to poor performance if access to a native ZenKit object is
74+
/// required. Either cache the value in a variable or use <see cref="GetGlyph" />.
75+
/// </remarks>
76+
/// <seealso href="https://zk.gothickit.dev/library/api/font/#dealing-with-glyphs" />
2277
public List<FontGlyph> Glyphs { get; }
78+
79+
/// <summary>
80+
/// The total number of glyphs stored in this font.
81+
/// </summary>
2382
public int GlyphCount { get; }
2483

84+
/// <summary>
85+
/// Get a single glyph of this font.
86+
/// </summary>
87+
/// <param name="index">
88+
/// The index of the glyph to get. Corresponds to a
89+
/// <a href="https://en.wikipedia.org/wiki/Windows-1252">Windows-1252</a> code point.
90+
/// </param>
91+
/// <returns>The font glyph at the given <paramref name="index" /></returns>
92+
/// <seealso cref="GlyphCount" />
93+
/// <seealso cref="Glyphs" />
94+
/// <seealso href="https://zk.gothickit.dev/library/api/font/#dealing-with-glyphs" />
2595
public FontGlyph GetGlyph(int index);
2696
}
2797

98+
/// <summary>
99+
/// A ZenKit font object which has been fully loaded into C#. An object of this type is independent from any native
100+
/// object and incurs none of its disadvantages.
101+
/// </summary>
28102
[Serializable]
29103
public class CachedFont : IFont
30104
{
105+
/// <inheritdoc />
31106
public string Name { get; set; }
107+
108+
/// <inheritdoc />
32109
public int Height { get; set; }
110+
111+
/// <inheritdoc />
33112
public List<FontGlyph> Glyphs { get; set; }
113+
114+
/// <inheritdoc />
34115
public int GlyphCount => Glyphs.Count;
35116

117+
/// <inheritdoc />
36118
public IFont Cache()
37119
{
38120
return this;
39121
}
40122

123+
/// <inheritdoc />
41124
public bool IsCached()
42125
{
43126
return true;
44127
}
45128

129+
/// <inheritdoc />
46130
public FontGlyph GetGlyph(int index)
47131
{
48132
return Glyphs[index];
@@ -53,36 +137,77 @@ public class Font : IFont
53137
{
54138
private readonly UIntPtr _handle;
55139

140+
/// <summary>
141+
/// Loads a ZenGin font from the given file on disk using ZenKit.
142+
/// </summary>
143+
/// <example>
144+
/// For example:
145+
/// <code>
146+
/// var font = new Font("FONT_OLD_20.FNT");
147+
/// </code>
148+
/// </example>
149+
/// <param name="path">The path of the font file (usually ends in <c>.FNT</c>.</param>
150+
/// <exception cref="IOException">Thrown if loading the font fails for any reason.</exception>
56151
public Font(string path)
57152
{
58153
_handle = Native.ZkFont_loadPath(path);
59-
if (_handle == UIntPtr.Zero) throw new Exception("Failed to load font");
154+
if (_handle == UIntPtr.Zero) throw new IOException("Failed to load font");
60155
}
61156

157+
/// <summary>
158+
/// Loads a ZenGin font from the given <see cref="Read" /> stream.
159+
/// </summary>
160+
/// <example>
161+
/// For example:
162+
/// <code>
163+
/// var rd = new Read("FONT_OLD_20.FNT");
164+
/// var font = new Font(rd);
165+
/// </code>
166+
/// </example>
167+
/// <param name="r">The stream to read from.</param>
168+
/// <exception cref="IOException">Thrown if loading the font fails for any reason.</exception>
62169
public Font(Read r)
63170
{
64171
_handle = Native.ZkFont_load(r.Handle);
65172
if (_handle == UIntPtr.Zero) throw new Exception("Failed to load font");
66173
}
67174

175+
/// <summary>
176+
/// Loads a ZenGin font from the given file on disk using ZenKit.
177+
/// </summary>
178+
/// <example>
179+
/// For example:
180+
/// <code>
181+
/// var vfs = new Vfs();
182+
/// vfs.MountDisk("Textures.vdf", VfsOverwriteBehavior.Older);
183+
/// var font = new Font(vfs, "FONT_OLD_20.FNT");
184+
/// </code>
185+
/// </example>
186+
/// <param name="vfs"></param>
187+
/// <param name="name"></param>
188+
/// <exception cref="IOException">Thrown if loading the font fails for any reason.</exception>
68189
public Font(Vfs vfs, string name)
69190
{
70191
_handle = Native.ZkFont_loadVfs(vfs.Handle, name);
71192
if (_handle == UIntPtr.Zero) throw new Exception("Failed to load font");
72193
}
73194

195+
/// <inheritdoc />
74196
public string Name => Native.ZkFont_getName(_handle).MarshalAsString() ??
75197
throw new Exception("Failed to load font name");
76198

199+
/// <inheritdoc />
77200
public int Height => (int)Native.ZkFont_getHeight(_handle);
78201

202+
/// <inheritdoc />
79203
public int GlyphCount => (int)Native.ZkFont_getGlyphCount(_handle);
80204

205+
/// <inheritdoc />
81206
public List<FontGlyph> Glyphs
82207
{
83208
get
84209
{
85-
var glyphs = new List<FontGlyph>(256);
210+
var glyphs = new List<FontGlyph>(GlyphCount);
86211

87212
Native.ZkFont_enumerateGlyphs(_handle, (_, glyph) =>
88213
{
@@ -94,6 +219,7 @@ public List<FontGlyph> Glyphs
94219
}
95220
}
96221

222+
/// <inheritdoc />
97223
public IFont Cache()
98224
{
99225
return new CachedFont
@@ -104,11 +230,13 @@ public IFont Cache()
104230
};
105231
}
106232

233+
/// <inheritdoc />
107234
public bool IsCached()
108235
{
109236
return false;
110237
}
111238

239+
/// <inheritdoc />
112240
public FontGlyph GetGlyph(int index)
113241
{
114242
return Native.ZkFont_getGlyph(_handle, (ulong)index);

0 commit comments

Comments
 (0)