Skip to content

Commit 8934a8b

Browse files
fonts can be loaded from windows
1 parent bca9666 commit 8934a8b

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

Flamui/Drawing/FontLoader.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,7 @@ public class FontLoader
220220
{
221221
public static unsafe Font LoadFont(string name)
222222
{
223-
var asm = Assembly.GetExecutingAssembly();
224-
225-
using var stream = asm.GetManifestResourceStream($"Flamui.Drawing.{name}");
226-
using MemoryStream ms = new MemoryStream();
227-
stream!.CopyTo(ms);
228-
var fontData = ms.ToArray();
223+
var fontData = GetFont(name);
229224

230225
// Console.WriteLine($"Allocating {((float)fontData.Length)/1000/1000} MB");
231226
var ptr = Marshal.AllocHGlobal(fontData.Length); //todo maybe also free again????
@@ -252,6 +247,22 @@ public static unsafe Font LoadFont(string name)
252247
};
253248
}
254249

250+
private static byte[] GetFont(string fileName)
251+
{
252+
var windowsFontLocation = Path.Combine(@"C:\Windows\Fonts\", fileName);
253+
if (File.Exists(windowsFontLocation))
254+
{
255+
return File.ReadAllBytes(windowsFontLocation);
256+
}
257+
258+
var asm = Assembly.GetExecutingAssembly();
259+
260+
using var stream = asm.GetManifestResourceStream($"Flamui.Drawing.{fileName}");
261+
using MemoryStream ms = new MemoryStream();
262+
stream!.CopyTo(ms);
263+
return ms.ToArray();
264+
}
265+
255266
public static FontAtlas CreateFontAtlas(ScaledFont scaledFont)
256267
{
257268
var table = new LRUCache<GlyphCacheHash, AtlasGlyphInfo>(10*10);

Flamui/Drawing/GlCanvas.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public void DrawText(ReadOnlySpan<char> text, float x, float y)
4646
// Console.WriteLine($"{c}: {fontAtlas.Font.Ascent}: {glyphInfo.YOff}, {glyphInfo.AtlasHeight}, {glyphInfo.Height}");
4747
DrawGlyph(fontAtlas, glyphInfo, fontAtlas.GpuTexture, xCoord + glyphInfo.LeftSideBearing, y + fontAtlas.Font.Ascent + glyphInfo.YOff);
4848
xCoord += glyphInfo.AdvanceWidth;
49+
Console.WriteLine($"Metrics: {c}:{glyphInfo.AdvanceWidth}:{glyphInfo.LeftSideBearing}");
4950
}
5051
}
5152
//an

Flamui/Ui.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public void ResetStuff()
149149

150150
CascadingStack.Clear();
151151
CascadingValues = new CascadingStuff();
152-
CascadingValues.Font = FontManager.GetFont("JetBrainsMono-Regular.ttf");
152+
CascadingValues.Font = FontManager.GetFont("segoeui.ttf");
153153
CascadingValues.TextColor = C.Black;
154154
CascadingValues.TextSize = 15;
155155

Sample.LayoutTest/Program.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33

44
/*
55
* Todo
6-
* - non ascii text
76
* - Scroll
87
* - Fix Border
9-
* - Only rerender when changed
108
*/
119

1210
var builder = FlamuiApp.CreateBuilder();
@@ -42,6 +40,11 @@ public override void Build(Ui ui)
4240
{
4341
ui.StyledInput(ref input2);
4442

43+
if (ui.Button("New Window", primary: true))
44+
{
45+
app.CreateWindow<LayoutTest>("Anoter Window");
46+
}
47+
4548
// var dropdown = ui.CreateDropDown(selectedOption);
4649
// dropdown.Component.Option("Hi");
4750
// dropdown.Component.Option("Anita");

0 commit comments

Comments
 (0)