Debian Codegraph @

contrib/C#/NGraphite/Face.cs

raw
1 // SPDX-License-Identifier: MIT OR MPL-2.0 OR LGPL-2.1-or-later OR GPL-2.0-or-later
2 // Copyright (C) 2012 SIL International
3 using System;
4 using System.IO;
5
6 namespace NGraphite
7 {
8 public class Face : IDisposable
9 {
10 IntPtr _face;
11
12 /// <summary>
13 /// Initializes a new instance of the <see cref="NGraphite.Face"/> class.
14 /// </summary>
15 /// <param name='filename'>
16 /// fullpath to a graphite enabled font.
17 /// </param>
18 public Face(string filename, FaceOptions options)
19 {
20 if (!File.Exists(filename))
21 throw new ArgumentException("filename");
22
23 _face = Graphite2Api.MakeFileFace(filename, options);
24
25 if (_face == IntPtr.Zero)
26 throw new ArgumentException("filename is not a graphite font");
27 }
28
29 public Face(string filename, uint segCacheMaxSize, FaceOptions options)
30 {
31 if (!File.Exists(filename))
32 throw new ArgumentException("filename");
33
34 _face = Graphite2Api.MakeFileFaceWithSegCache(filename, segCacheMaxSize, options);
35
36 if (_face == IntPtr.Zero)
37 throw new ArgumentException("filename is not a graphite font");
38 }
39
40 /// <summary>
41 /// Determins if filename is a graphite font or not.
42 /// </summary>
43 public static bool IsGraphiteFont(string filename)
44 {
45 if (!File.Exists(filename))
46 return false;
47
48 IntPtr face = Graphite2Api.MakeFileFace(filename, FaceOptions.face_default);
49 if (face == IntPtr.Zero)
50 return false;
51
52 Graphite2Api.FaceDestroy(face);
53 return true;
54 }
55
56 internal IntPtr FacePtr
57 {
58 get { return _face; }
59 }
60
61 public Featureval FeaturevalForLang(string lang)
62 {
63 IntPtr ptr = Graphite2Api.FaceFeaturevalForLang(_face, Graphite2Api.StrToTag(lang));
64 if (ptr == IntPtr.Zero)
65 return null;
66 return new Featureval(ptr);
67 }
68
69 public FeatureRef FindFref(Int32 featid)
70 {
71 IntPtr ptr = Graphite2Api.FaceFindFref(_face, featid);
72 if (ptr == IntPtr.Zero)
73 return null;
74 return new FeatureRef(ptr);
75 }
76
77 public UInt16 NFref()
78 {
79 return Graphite2Api.FaceNFref(_face);
80 }
81
82 public FeatureRef Fref(UInt16 i)
83 {
84 IntPtr ptr = Graphite2Api.FaceFref(_face, i);
85 if (ptr == IntPtr.Zero)
86 return null;
87 return new FeatureRef(ptr);
88 }
89
90 public ushort NLanguages()
91 {
92 return Graphite2Api.FaceNLanguages(_face);
93 }
94
95 public UInt32 LangByIndex(UInt16 i)
96 {
97 return Graphite2Api.FaceLangByIndex(_face, i);
98 }
99
100 public short NGlyphs()
101 {
102 return Graphite2Api.FaceNGlyphs(_face);
103 }
104
105 public bool StartLogging(string filename)
106 {
107 int major, minor, bugfix;
108 Graphite2Api.EngineVersion(out major, out minor, out bugfix);
109
110 if (major <= 1 && minor < 2)
111 throw new NotSupportedException("A newer graphite engine is needed to support logging");
112
113 return Graphite2Api.StartLogging(_face, filename);
114 }
115
116 public void StopLogging()
117 {
118 int major, minor, bugfix;
119 Graphite2Api.EngineVersion(out major, out minor, out bugfix);
120
121 if (major <= 1 && minor < 2)
122 throw new NotSupportedException("A newer graphite engine is needed to support logging");
123
124 Graphite2Api.StopLogging(_face);
125 }
126
127 #region IDisposable implementation
128 public void Dispose()
129 {
130 Graphite2Api.FaceDestroy(_face);
131 }
132 #endregion
133 }
134 }