Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[Tizen] Supports Custom/Embedded and System fonts #9138

Merged
merged 6 commits into from
Jan 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion GitInfo.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.5.0
4.6.0
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public override SizeRequest GetDesiredSize(int widthConstraint, int heightConstr

protected override SearchView CreateNativeControl()
{
var context = (Context as ContextThemeWrapper).BaseContext ?? Context;
var context = (Context as ContextThemeWrapper)?.BaseContext ?? Context;
return new SearchView(context);
}

Expand Down
41 changes: 41 additions & 0 deletions Xamarin.Forms.Platform.Tizen/EmbeddedFontLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.IO;
using ElmSharp;
using TApplication = Tizen.Applications.Application;

namespace Xamarin.Forms.Platform.Tizen
{
public class EmbeddedFontLoader : IEmbeddedFontLoader, IRegisterable
{
const string _fontCacheFolderName = "fonts";

public DirectoryInfo FontCacheDirectory { get; private set; }

public EmbeddedFontLoader()
{
FontCacheDirectory = Directory.CreateDirectory(Path.Combine(TApplication.Current.DirectoryInfo.Data, _fontCacheFolderName));
Utility.AppendGlobalFontPath(FontCacheDirectory.FullName);
}

public (bool success, string filePath) LoadFont(EmbeddedFont font)
{
var filePath = Path.Combine(FontCacheDirectory.FullName, font.FontName);
if (File.Exists(filePath))
return (true, filePath);
try
{
using (var fileStream = File.Create(filePath))
{
font.ResourceStream.CopyTo(fileStream);
}
return (true, filePath);
}
catch (Exception ex)
{
Log.Error(ex.Message);
File.Delete(filePath);
}
return (false, null);
}
}
}
50 changes: 50 additions & 0 deletions Xamarin.Forms.Platform.Tizen/Extensions/FontExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Xamarin.Forms.Core;
using Xamarin.Forms.Internals;

namespace Xamarin.Forms.Platform.Tizen
{
public static class FontExtensions
{
public static string ToNativeFontFamily(this string self)
{
if (string.IsNullOrEmpty(self))
return null;

var cleansedFont = CleanseFontName(self);
int index = cleansedFont.LastIndexOf('-');
if (index != -1)
{
string font = cleansedFont.Substring(0, index);
string style = cleansedFont.Substring(index+1);
return $"{font}:style={style}";
}
else
{
return cleansedFont;
}
}

static string CleanseFontName(string fontName)
{
var fontFile = FontFile.FromString(fontName);

if (!string.IsNullOrWhiteSpace(fontFile.Extension))
{
var (hasFont, _) = FontRegistrar.HasFont(fontFile.FileNameWithExtension());
if (hasFont)
return fontFile.PostScriptName;
}
else
{
foreach (var ext in FontFile.Extensions)
{
var formated = fontFile.FileNameWithExtension(ext);
var (hasFont, filePath) = FontRegistrar.HasFont(formated);
if (hasFont)
return fontFile.PostScriptName;
}
}
return fontFile.PostScriptName;
}
}
}
10 changes: 7 additions & 3 deletions Xamarin.Forms.Platform.Tizen/Forms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ static void SetupInit(CoreApplication application, InitializationOptions options

Elementary.Initialize();
Elementary.ThemeOverlay();
Utility.AppendGlobalFontPath(@"/usr/share/fonts");
}

Device.PlatformServices = new TizenPlatformServices();
Expand Down Expand Up @@ -394,7 +395,8 @@ static void SetupInit(CoreApplication application, InitializationOptions options
typeof(ExportRendererAttribute),
typeof(ExportImageSourceHandlerAttribute),
typeof(ExportCellAttribute),
typeof(ExportHandlerAttribute)
typeof(ExportHandlerAttribute),
typeof(ExportFontAttribute)
});
}
}
Expand All @@ -405,7 +407,8 @@ static void SetupInit(CoreApplication application, InitializationOptions options
typeof(ExportRendererAttribute),
typeof(ExportImageSourceHandlerAttribute),
typeof(ExportCellAttribute),
typeof(ExportHandlerAttribute)
typeof(ExportHandlerAttribute),
typeof(ExportFontAttribute)
});
}
}
Expand Down Expand Up @@ -439,7 +442,8 @@ static void SetupInit(CoreApplication application, InitializationOptions options
typeof(ExportRendererAttribute),
typeof(ExportImageSourceHandlerAttribute),
typeof(ExportCellAttribute),
typeof(ExportHandlerAttribute)
typeof(ExportHandlerAttribute),
typeof(ExportFontAttribute)
});
}
}
Expand Down
2 changes: 2 additions & 0 deletions Xamarin.Forms.Platform.Tizen/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
[assembly: ExportCell(typeof(EntryCell), typeof(EntryCellRenderer))]
[assembly: ExportCell(typeof(ViewCell), typeof(ViewCellRenderer))]

[assembly: ExportRenderer(typeof(EmbeddedFont), typeof(EmbeddedFontLoader))]

[assembly: ExportHandler(typeof(TapGestureRecognizer), typeof(TapGestureHandler))]
[assembly: ExportHandler(typeof(PinchGestureRecognizer), typeof(PinchGestureHandler))]
[assembly: ExportHandler(typeof(PanGestureRecognizer), typeof(PanGestureHandler))]
Expand Down
2 changes: 1 addition & 1 deletion Xamarin.Forms.Platform.Tizen/Renderers/ButtonRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ void UpdateFontFamily()
{
if (Control is IButton ib)
{
ib.FontFamily = Element.FontFamily;
ib.FontFamily = Element.FontFamily.ToNativeFontFamily();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void UpdateFontSize()

void UpdateFontFamily()
{
Control.FontFamily = Element.FontFamily;
Control.FontFamily = Element.FontFamily.ToNativeFontFamily();
}

void UpdateFontAttributes()
Expand Down
2 changes: 1 addition & 1 deletion Xamarin.Forms.Platform.Tizen/Renderers/EditorRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ void UpdateFontSize()

void UpdateFontFamily()
{
Control.FontFamily = Element.FontFamily;
Control.FontFamily = Element.FontFamily.ToNativeFontFamily();
}

void UpdateFontAttributes()
Expand Down
2 changes: 1 addition & 1 deletion Xamarin.Forms.Platform.Tizen/Renderers/EntryRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ void UpdateFontFamily()
{
if (Control is IEntry ie)
{
ie.FontFamily = Element.FontFamily;
ie.FontFamily = Element.FontFamily.ToNativeFontFamily();
}
}

Expand Down
4 changes: 3 additions & 1 deletion Xamarin.Forms.Platform.Tizen/Renderers/LabelRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Xamarin.Forms.Core;
using Xamarin.Forms.Internals;
using Xamarin.Forms.Platform.Tizen.Native;
using Specific = Xamarin.Forms.PlatformConfiguration.TizenSpecific.Label;

Expand Down Expand Up @@ -106,7 +108,7 @@ void UpdateFontProperties()

Control.FontSize = Element.FontSize;
Control.FontAttributes = Element.FontAttributes;
Control.FontFamily = Element.FontFamily;
Control.FontFamily = Element.FontFamily.ToNativeFontFamily();

Control.BatchCommit();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void FontAttributesPropertyHandler()
/// </summary>
void FontFamilyPropertyHandler()
{
Control.FontFamily = Element.FontFamily;
Control.FontFamily = Element.FontFamily.ToNativeFontFamily();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ void UpdateFontSize()

void UpdateFontFamily()
{
Control.FontFamily = Element.FontFamily;
Control.FontFamily = Element.FontFamily.ToNativeFontFamily();
}

void UpdateFontAttributes()
Expand Down
9 changes: 6 additions & 3 deletions Xamarin.Forms.Platform.Tizen/StaticRegistrar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ static StaticRegistrar()

public static void RegisterHandlers(Dictionary<Type, Func<IRegisterable>> customHandlers)
{
////Renderers
//Renderers
Registered.Register(typeof(Layout), () => new LayoutRenderer());
Registered.Register(typeof(ScrollView), () => new ScrollViewRenderer());
Registered.Register(typeof(CarouselPage), () => new CarouselPageRenderer());
Expand Down Expand Up @@ -102,18 +102,21 @@ public static void RegisterHandlers(Dictionary<Type, Func<IRegisterable>> custom
Registered.Register(typeof(SwipeView), () => new SwipeViewRenderer());
Registered.Register(typeof(RefreshView), () => new RefreshViewRenderer());

////ImageSourceHandlers
//ImageSourceHandlers
Registered.Register(typeof(FileImageSource), () => new FileImageSourceHandler());
Registered.Register(typeof(StreamImageSource), () => new StreamImageSourceHandler());
Registered.Register(typeof(UriImageSource), () => new UriImageSourceHandler());

////Cell Renderers
//Cell Renderers
Registered.Register(typeof(TextCell), () => new TextCellRenderer());
Registered.Register(typeof(ImageCell), () => new ImageCellRenderer());
Registered.Register(typeof(SwitchCell), () => new SwitchCellRenderer());
Registered.Register(typeof(EntryCell), () => new EntryCellRenderer());
Registered.Register(typeof(ViewCell), () => new ViewCellRenderer());

//Font Loaders
Registered.Register(typeof(EmbeddedFont), () => new EmbeddedFontLoader());

//Dependencies
DependencyService.Register<ISystemResourcesProvider, ResourcesProvider>();
DependencyService.Register<IDeserializer, Deserializer>();
Expand Down