[ElmSharp] Cannnot load custom font files in app
shyunMin opened this issue · comments
Custom font files included in the app are not loaded when the app first runs.
I tried to use an custom font file using an app with font as a embedded resource.
However, custom font was not applied during the app running. it is applied after I terminated and re-launched the app.
This issue seems to occur since 5.5 wearable and TV, it didn't occur on 4.0.
The behavior of the app I tested is as below.
- creates a font folder at launch time.
- calls
AppendGlobalFontPath()
to add created folder. - copies the custom font file into the folder.
- create Label using the custom font
Launched | Re-launched |
---|---|
![]() |
![]() |
It seems that the font file copied after calling AppendGlobalFontPath()
is not loaded.
I attached test code and font file.
- font file - https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Controls/Fonts/CuteFont-Regular.ttf
using ElmSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Tizen.Applications;
namespace ElmSharpTemplate2
{
class App : CoreUIApplication
{
protected override void OnCreate()
{
base.OnCreate();
LoadFont();
Initialize();
}
void LoadFont()
{
var dataPath = DirectoryInfo.Data;
var resourcePath = DirectoryInfo.Resource;
var fontDir = Directory.CreateDirectory(Path.Combine(dataPath, "fonts"));
Utility.AppendGlobalFontPath(fontDir.FullName);
Console.WriteLine($"### AppendGlobalFontPath path={fontDir.FullName}");
var fontName = "CuteFont-Regular.ttf";
var fileStream = GetEmbeddedResourceStream(GetType().GetTypeInfo().Assembly, fontName);
var filePath = Path.Combine(fontDir.FullName, fontName);
if (File.Exists(filePath))
{
Console.WriteLine($"### Font file({fontName}) already exists");
return;
}
try
{
using (var stream = File.Create(filePath))
{
fileStream.CopyTo(stream);
}
Console.WriteLine($"### [LoadFont] copied filePath={filePath} / File.Exists(filePath)={File.Exists(filePath)}");
}
catch (Exception ex)
{
File.Delete(filePath);
}
}
void Initialize()
{
Window window = new Window("ElmSharpApp")
{
AvailableRotations = DisplayRotation.Degree_0 | DisplayRotation.Degree_180 | DisplayRotation.Degree_270 | DisplayRotation.Degree_90
};
window.BackButtonPressed += (s, e) =>
{
Exit();
};
window.Show();
var box = new Box(window)
{
AlignmentX = -1,
AlignmentY = -1,
WeightX = 1,
WeightY = 1,
};
box.Show();
var bg = new Background(window)
{
Color = Color.Black
};
bg.SetContent(box);
var conformant = new Conformant(window);
conformant.Show();
conformant.SetContent(bg);
var labels = new List<Label>();
for (var i = 0; i < 5; i++)
{
var label = new Label(window)
{
AlignmentX = -1,
AlignmentY = -1,
WeightX = 1,
WeightY = 1,
Text = "<span align=center> Font Test < /span>",
};
label.Show();
box.PackEnd(label);
labels.Add(label);
}
labels[2].Text = "<span align=center font='CuteFont'> CuteFont < /span>";
}
Stream GetEmbeddedResourceStream(Assembly assembly, string resourceFileName)
{
var resourceNames = assembly.GetManifestResourceNames();
var resourcePaths = resourceNames
.Where(x => x.EndsWith(resourceFileName, StringComparison.CurrentCultureIgnoreCase))
.ToArray();
if (!resourcePaths.Any())
{
throw new Exception(string.Format("Resource ending with {0} not found.", resourceFileName));
}
if (resourcePaths.Length > 1)
{
resourcePaths = resourcePaths.Where(x => IsFile(x, resourceFileName)).ToArray();
}
return assembly.GetManifestResourceStream(resourcePaths.FirstOrDefault());
}
bool IsFile(string path, string file)
{
if (!path.EndsWith(file, StringComparison.Ordinal))
return false;
return path.Replace(file, "").EndsWith(".", StringComparison.Ordinal);
}
static void Main(string[] args)
{
Elementary.Initialize();
Elementary.ThemeOverlay();
App app = new App();
app.Run(args);
}
}
}
Please call evas_font_reinit() when custom font is added in path directory.