Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Subtitle Support for MediaElement #1918

Draft
wants to merge 146 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
146 commits
Select commit Hold shift + click to select a range
96fc21a
The most important changes involve the introduction of subtitle suppo…
ne0rrmatrix May 7, 2024
1667b8d
The primary focus of these code changes is to enhance the subtitle fu…
ne0rrmatrix May 7, 2024
5c70eb5
The code changes made primarily focus on improving code readability, …
ne0rrmatrix May 7, 2024
4e35194
The primary change made to the code involves the removal of a logging…
ne0rrmatrix May 7, 2024
a2b1ab1
The primary change involves making the `xamlTextBlock` field in the `…
ne0rrmatrix May 7, 2024
df4d9ee
The code changes primarily focus on enhancing the subtitle functional…
ne0rrmatrix May 7, 2024
3ec12b0
The primary change involves updating the null check syntax for the `c…
ne0rrmatrix May 7, 2024
64ac62b
The primary change involves the removal of code that added a subtitle…
ne0rrmatrix May 8, 2024
0cfbd6f
The primary change made to the code involves adjusting the font size …
ne0rrmatrix May 8, 2024
27d2af4
The modifications to the `SubtitleExtensions.android.cs` file within …
ne0rrmatrix May 8, 2024
8134fa5
The most important changes revolve around the introduction of subtitl…
ne0rrmatrix May 9, 2024
dc3b150
Fix spacing issue
ne0rrmatrix May 9, 2024
4de0281
The most important changes revolve around enhancing subtitle function…
ne0rrmatrix May 9, 2024
ef34b04
The code changes primarily focus on simplifying the subtitle display …
ne0rrmatrix May 10, 2024
48d357b
Change padding to relative
ne0rrmatrix May 11, 2024
9267ef1
Merge Main into Subtitle Support
ne0rrmatrix Jun 6, 2024
96f0d81
Merge Main into AddSubtitleSupport
ne0rrmatrix Jun 6, 2024
d9d1481
Fix merge error by adding using statement pointing to extensions
ne0rrmatrix Jun 6, 2024
b7f0acc
Merge branch 'main' into AddSubtitleSupport
ne0rrmatrix Jun 8, 2024
c4bc4ea
Merge branch 'main' into AddSubtitleSupport
ne0rrmatrix Jun 16, 2024
8af5835
Refactor UI and resource handling in media element
ne0rrmatrix Jun 17, 2024
6973513
Merge branch 'AddSubtitleSupport' of https://github.com/ne0rrmatrix/M…
ne0rrmatrix Jun 17, 2024
cb80c9c
Refactor SubtitleExtensions class
ne0rrmatrix Jun 17, 2024
4743987
Merge branch 'main' into AddSubtitleSupport
ne0rrmatrix Jun 17, 2024
c40cacf
Improve subtitle handling and code cleanup
ne0rrmatrix Jun 17, 2024
8086827
remove redundant string assignments
ne0rrmatrix Jun 17, 2024
261bfaa
Improve subtitle sizing in full-screen mode
ne0rrmatrix Jun 17, 2024
b76ed97
Merge branch 'main' into AddSubtitleSupport
ne0rrmatrix Jun 18, 2024
582c175
Merge branch 'main' into AddSubtitleSupport
ne0rrmatrix Jun 19, 2024
1b5dfda
1. remove uneeded using
ne0rrmatrix Jun 19, 2024
5c1f8ea
Added sample font to try and set fonts in Media Element
ne0rrmatrix Jun 20, 2024
665f629
remove old code
ne0rrmatrix Jun 20, 2024
9e0ceb2
Merge Main into AddSubtitleSupport
ne0rrmatrix Jun 20, 2024
e8b1720
remove empty spacing
ne0rrmatrix Jun 20, 2024
ea1d054
Restrict class visibility
ne0rrmatrix Jun 20, 2024
bf24dd3
Refactor subtitle parsers for code compliance. 'private' is not allow…
ne0rrmatrix Jun 20, 2024
61c6745
Refactor HttpClient usage in SubtitleExtensions
ne0rrmatrix Jun 20, 2024
cfb47ec
Refactor subtitle handling and UI adjustments
ne0rrmatrix Jun 21, 2024
d1f3133
Refactor subtitle update and cleanup logic
ne0rrmatrix Jun 21, 2024
9018a89
Merge branch 'main' into AddSubtitleSupport
ne0rrmatrix Jun 21, 2024
386ad50
Refactor and enhance subtitle handling
ne0rrmatrix Jun 21, 2024
8d1749d
Refactor and centralize parser logic
ne0rrmatrix Jun 21, 2024
fb5f22c
Make subtitle properties read-only in IMediaElement
ne0rrmatrix Jun 21, 2024
c98bf9f
Refactor subtitle parsing and HTTP handling
ne0rrmatrix Jun 21, 2024
06ad678
Refactor and improve `SubtitleExtensions`
ne0rrmatrix Jun 21, 2024
3d2fd49
Refactor SubtitleExtensions class
ne0rrmatrix Jun 21, 2024
6c2a667
Fix method naming inconsistencies
ne0rrmatrix Jun 21, 2024
0cbd66f
Renamed OnGridEventsChanged to FullScreenChanged
ne0rrmatrix Jun 21, 2024
8e46365
Merge branch 'main' into AddSubtitleSupport
ne0rrmatrix Jun 21, 2024
1b854e6
Refactor and fix subtitle extension bugs
ne0rrmatrix Jun 22, 2024
df5d603
Refactor subtitle parsers for efficiency
ne0rrmatrix Jun 22, 2024
38aa3e4
Created sample parser file and added ability of developer to use a cu…
ne0rrmatrix Jun 22, 2024
d2ccc90
Refine codebase and improve documentation
ne0rrmatrix Jun 22, 2024
8ff9947
Refactor subtitle parsing functionality
ne0rrmatrix Jun 22, 2024
b5f232f
Refactor subtitle management and error handling
ne0rrmatrix Jun 23, 2024
7ebee6a
Refactor PageExtensions and update Android refs
ne0rrmatrix Jun 23, 2024
233048c
Remove redundant null check
ne0rrmatrix Jun 23, 2024
799c967
Fonts working in Android/iOS. Not working in Windows yet. Needs to be…
ne0rrmatrix Jun 25, 2024
f8211a6
Added fonts to mac plist.
ne0rrmatrix Jun 25, 2024
98e1b46
Add subtitleFont string to mac in sample app
ne0rrmatrix Jun 25, 2024
d5c7a79
Windows font support added!
ne0rrmatrix Jun 25, 2024
b6ba3a7
Refactor font handling for subtitles
ne0rrmatrix Jun 25, 2024
f0bdc92
Refactor font spec handling and parsing
ne0rrmatrix Jun 25, 2024
7636777
Add multiple fonts to sample to show how to use them in media element.
ne0rrmatrix Jun 26, 2024
a7bb0d5
Refactor subtitle parsers for efficiency
ne0rrmatrix Jun 26, 2024
97702a3
Updated font exports behavior
ne0rrmatrix Jun 26, 2024
b3c7a1e
fix spacing
ne0rrmatrix Jun 26, 2024
6e79bf0
Improve subtitle loading and display
ne0rrmatrix Jun 27, 2024
8a367bd
Add destructor to SubtitleExtensions class
ne0rrmatrix Jun 27, 2024
b60beee
Improve subtitle parsing and error handling
ne0rrmatrix Jun 28, 2024
4e2348a
Update test for SrtParser invalid timestamps
ne0rrmatrix Jun 28, 2024
cd71c3d
Refactor subtitle handling across platforms
ne0rrmatrix Jun 28, 2024
d6fa737
Add FontExtensions unit tests
ne0rrmatrix Jun 28, 2024
24b9ad7
Refine font family test methods
ne0rrmatrix Jun 28, 2024
6144f4b
Update comments
ne0rrmatrix Jun 28, 2024
3eabcfb
Refactor and optimize subtitle handling
ne0rrmatrix Jun 28, 2024
22b5fdf
Remove many empty lines
ne0rrmatrix Jun 28, 2024
19875bb
Refactor test for NullReferenceException
ne0rrmatrix Jul 2, 2024
de2bbcb
Merge branch 'main' into AddSubtitleSupport
ne0rrmatrix Jul 3, 2024
3f2aca7
Fix merge Conflict
ne0rrmatrix Jul 4, 2024
b67a00e
Fix merge error
ne0rrmatrix Jul 4, 2024
ea38639
Merge branch 'main' into AddSubtitleSupport
ne0rrmatrix Jul 5, 2024
a19a523
Merge branch 'main' into AddSubtitleSupport
ne0rrmatrix Jul 9, 2024
cf89c37
Merge branch 'main' into AddSubtitleSupport
ne0rrmatrix Jul 9, 2024
5f972f3
Merge branch 'main' into AddSubtitleSupport
ne0rrmatrix Jul 17, 2024
91f1dbb
Refactor full-screen event handling
ne0rrmatrix Jul 19, 2024
e101e4d
Merge branch 'AddSubtitleSupport' of https://github.com/ne0rrmatrix/M…
ne0rrmatrix Jul 19, 2024
dfa0a4a
Refactor Android context access in Maui app
ne0rrmatrix Jul 19, 2024
16a7b9f
Refine visibility and structure of records
ne0rrmatrix Jul 19, 2024
715f808
Refactor FontExtensions and PageExtensions
ne0rrmatrix Jul 20, 2024
dbb3f59
Merge branch 'main' into AddSubtitleSupport
ne0rrmatrix Jul 22, 2024
2791bd6
Enhance subtitle parsing and display capabilities
ne0rrmatrix Jul 23, 2024
236e550
Merge branch 'AddSubtitleSupport' of https://github.com/ne0rrmatrix/M…
ne0rrmatrix Jul 23, 2024
79ae3c5
Revert "Enhance subtitle parsing and display capabilities"
ne0rrmatrix Jul 24, 2024
1872d04
Merge branch 'CommunityToolkit:main' into AddSubtitleSupport
ne0rrmatrix Jul 24, 2024
2c63665
Reorder SubtitleUrl assignment in ChangeSourceClicked method
ne0rrmatrix Jul 24, 2024
ab22367
Merge branch 'AddSubtitleSupport' of https://github.com/ne0rrmatrix/M…
ne0rrmatrix Jul 24, 2024
fb5b6af
In SubtitleExtensions.windows.cs:
ne0rrmatrix Jul 24, 2024
a7ee1be
Merge branch 'main' into AddSubtitleSupport
brminnick Jul 24, 2024
c993d38
Merge branch 'CommunityToolkit:main' into AddSubtitleSupport
ne0rrmatrix Jul 25, 2024
b0bc69b
Refactor SubtitleExtensions for better layout management
ne0rrmatrix Jul 25, 2024
269b512
Merge branch 'AddSubtitleSupport' of https://github.com/ne0rrmatrix/M…
ne0rrmatrix Jul 25, 2024
f0efafc
Refactor subtitleTextBlock initialization and usage
ne0rrmatrix Jul 26, 2024
ee297d8
Refactor SubtitleExtensions and update MediaManager
ne0rrmatrix Jul 26, 2024
206e650
Refactor SubtitleExtensions for better resource management
ne0rrmatrix Jul 26, 2024
fd173c2
Refactor SubtitleExtensions and MediaManager
ne0rrmatrix Jul 27, 2024
4262cd5
Refactor subtitle view management in SubtitleExtensions.macios.cs
ne0rrmatrix Jul 27, 2024
ca21bbd
Refactor subtitle view management in SubtitleExtensions.macios.cs
ne0rrmatrix Jul 27, 2024
2dfedae
Refactor SubtitleExtensions methods
ne0rrmatrix Jul 27, 2024
6869d5a
Refactor UpdateSubtitle method for clarity and efficiency
ne0rrmatrix Jul 27, 2024
6daff2d
Updated Spacing
ne0rrmatrix Jul 27, 2024
a942033
Refactor SubtitleExtensions.macios.cs for improved subtitle width cal…
ne0rrmatrix Jul 27, 2024
e7669bb
Refactor SubtitleExtensions.macios.cs for improved subtitle Size in F…
ne0rrmatrix Jul 27, 2024
ff4f6a6
Refactor subtitle width and font size calculations
ne0rrmatrix Jul 27, 2024
4414ae6
Add Dispose method to SubtitleExtensions for resource cleanup
ne0rrmatrix Jul 28, 2024
7dab55e
Refactor SubtitleExtensions and add CancellationToken support
ne0rrmatrix Jul 29, 2024
6ae8dea
Fix spacing issues
ne0rrmatrix Jul 29, 2024
22a964e
Refactor to remove early return in subtitle cue logic
ne0rrmatrix Jul 29, 2024
c885c94
Merge branch 'main' into AddSubtitleSupport
ne0rrmatrix Aug 2, 2024
02b6222
Merge branch 'main' into AddSubtitleSupport
ne0rrmatrix Aug 3, 2024
3bba920
Merge branch 'main' into AddSubtitleSupport
ne0rrmatrix Aug 4, 2024
c074229
Merge branch 'main' into AddSubtitleSupport
ne0rrmatrix Aug 8, 2024
ef100f0
Merge branch 'main' into AddSubtitleSupport
ne0rrmatrix Aug 9, 2024
a5d7383
Fix merge errors
ne0rrmatrix Aug 9, 2024
e6c62cf
Merge branch 'main' into AddSubtitleSupport
ne0rrmatrix Aug 9, 2024
74aeecc
Merge branch 'main' into AddSubtitleSupport
ne0rrmatrix Aug 19, 2024
20ba5b4
Merge branch 'main' into AddSubtitleSupport
ne0rrmatrix Aug 19, 2024
69e11a2
Merge branch 'main' into AddSubtitleSupport
ne0rrmatrix Aug 19, 2024
b418ee2
Merge branch 'CommunityToolkit:main' into AddSubtitleSupport
ne0rrmatrix Aug 21, 2024
ea906cd
Merge branch 'main' into AddSubtitleSupport
ne0rrmatrix Aug 25, 2024
66b752a
Merge branch 'CommunityToolkit:main' into AddSubtitleSupport
ne0rrmatrix Aug 29, 2024
739e9f8
Merge branch 'main' into AddSubtitleSupport
ne0rrmatrix Sep 2, 2024
21ae2c1
Merge branch 'CommunityToolkit:main' into AddSubtitleSupport
ne0rrmatrix Sep 9, 2024
478ca42
Merge branch 'main' into AddSubtitleSupport
ne0rrmatrix Sep 10, 2024
581dcdd
Fix merge bug
ne0rrmatrix Sep 10, 2024
6f39c17
Merge branch 'CommunityToolkit:main' into AddSubtitleSupport
ne0rrmatrix Sep 11, 2024
686c2d9
Merge branch 'CommunityToolkit:main' into AddSubtitleSupport
ne0rrmatrix Sep 16, 2024
bdb69eb
Merge branch 'main' into AddSubtitleSupport
ne0rrmatrix Sep 24, 2024
16d20ce
Merge branch 'CommunityToolkit:main' into AddSubtitleSupport
ne0rrmatrix Sep 27, 2024
a6b0b78
Merge branch 'main' into AddSubtitleSupport
ne0rrmatrix Oct 21, 2024
bddca10
Fix MediaElement source assignment in ChangeSourceClicked
ne0rrmatrix Oct 21, 2024
78b3291
Merge branch 'main' into AddSubtitleSupport
ne0rrmatrix Oct 31, 2024
29b5ffc
Merge branch 'main' into AddSubtitleSupport
ne0rrmatrix Nov 5, 2024
7709c24
Merge branch 'main' into AddSubtitleSupport
ne0rrmatrix Nov 7, 2024
d6a3b33
Merge branch 'main' into AddSubtitleSupport
ne0rrmatrix Nov 11, 2024
6eeee75
Merge branch 'main' into AddSubtitleSupport
ne0rrmatrix Nov 11, 2024
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: 2 additions & 0 deletions samples/CommunityToolkit.Maui.Sample/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
using Microsoft.Maui.Platform;
#endif

[assembly: ExportFont("Poppins-Regular.ttf", Alias = "Poppins")]
[assembly: ExportFont("PlaywriteSK-Regular.ttf", Alias = "PlaywriteSK")]
[assembly: XamlCompilation(XamlCompilationOptions.Compile)]

namespace CommunityToolkit.Maui.Sample;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<toolkit:MediaElement
x:Name="MediaElement"
ShouldAutoPlay="True"
SubtitleUrl=""
Source="https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
MetadataArtworkUrl="https://lh3.googleusercontent.com/pw/AP1GczNRrebWCJvfdIau1EbsyyYiwAfwHS0JXjbioXvHqEwYIIdCzuLodQCZmA57GADIo5iB3yMMx3t_vsefbfoHwSg0jfUjIXaI83xpiih6d-oT7qD_slR0VgNtfAwJhDBU09kS5V2T5ZML-WWZn8IrjD4J-g=w1792-h1024-s-no-gm"
MetadataTitle="Big Buck Bunny"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
using CommunityToolkit.Maui.Sample.ViewModels.Views;
using CommunityToolkit.Maui.Views;
using Microsoft.Extensions.Logging;
using CommunityToolkit.Maui.Sample.Resources.Fonts;
using LayoutAlignment = Microsoft.Maui.Primitives.LayoutAlignment;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Text;
using CommunityToolkit.Maui.Core;

namespace CommunityToolkit.Maui.Sample.Pages.Views;

Expand All @@ -15,6 +20,7 @@ public partial class MediaElementPage : BasePage<MediaElementViewModel>
const string loadHls = "Load HTTP Live Stream (HLS)";
const string loadLocalResource = "Load Local Resource";
const string resetSource = "Reset Source to null";
const string loadSubTitles = "Load sample with Subtitles";
const string loadMusic = "Load Music";

const string buckBunnyMp4Url = "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4";
Expand Down Expand Up @@ -161,14 +167,15 @@ void Button_Clicked(object? sender, EventArgs e)
async void ChangeSourceClicked(Object sender, EventArgs e)
{
var result = await DisplayActionSheet("Choose a source", "Cancel", null,
loadOnlineMp4, loadHls, loadLocalResource, resetSource, loadMusic);
loadOnlineMp4, loadHls, loadLocalResource, resetSource, loadSubTitles, loadMusic);

switch (result)
{
case loadOnlineMp4:
MediaElement.MetadataTitle = "Big Buck Bunny";
MediaElement.MetadataArtworkUrl = botImageUrl;
MediaElement.MetadataArtist = "Big Buck Bunny Album";
MediaElement.SubtitleUrl = string.Empty;
MediaElement.Source =
MediaSource.FromUri(buckBunnyMp4Url);
return;
Expand All @@ -177,21 +184,23 @@ async void ChangeSourceClicked(Object sender, EventArgs e)
MediaElement.MetadataArtist = "HLS Album";
MediaElement.MetadataArtworkUrl = botImageUrl;
MediaElement.MetadataTitle = "HLS Title";
MediaElement.SubtitleUrl = string.Empty;
MediaElement.Source = MediaSource.FromUri(hlsStreamTestUrl);
return;

case resetSource:
MediaElement.MetadataArtworkUrl = string.Empty;
MediaElement.MetadataTitle = string.Empty;
MediaElement.MetadataArtist = string.Empty;
MediaElement.SubtitleUrl = string.Empty;
MediaElement.Source = null;
return;

case loadLocalResource:
MediaElement.MetadataArtworkUrl = botImageUrl;
MediaElement.MetadataTitle = "Local Resource Title";
MediaElement.MetadataArtist = "Local Resource Album";

MediaElement.SubtitleUrl = string.Empty;
if (DeviceInfo.Platform == DevicePlatform.MacCatalyst
|| DeviceInfo.Platform == DevicePlatform.iOS)
{
Expand All @@ -207,6 +216,24 @@ async void ChangeSourceClicked(Object sender, EventArgs e)
}
return;

case loadSubTitles:
SrtParser srtParser = new();
MediaElement.CustomSubtitleParser = srtParser;
if (DevicePlatform.iOS == DeviceInfo.Platform || DevicePlatform.macOS == DeviceInfo.Platform)
{
MediaElement.SubtitleFont = @"PlaywriteSK-Regular.ttf#Playwrite SK";
MediaElement.SubtitleFontSize = 12;
}
else
{
MediaElement.SubtitleFont = @"Poppins-Regular.ttf#Poppins";
MediaElement.SubtitleFontSize = 16;
}

MediaElement.SubtitleUrl = "https://raw.githubusercontent.com/ne0rrmatrix/SampleVideo/main/SRT/WindowsVideo.srt";
MediaElement.Source = MediaSource.FromResource("WindowsVideo.mp4");
return;

case loadMusic:
MediaElement.MetadataTitle = "HAL 9000";
MediaElement.MetadataArtist = "HAL 9000 Album";
Expand Down Expand Up @@ -242,7 +269,6 @@ async void ChangeAspectClicked(object? sender, EventArgs e)

MediaElement.Aspect = (Aspect)aspectEnum;
}

void DisplayPopup(object sender, EventArgs e)
{
MediaElement.Pause();
Expand Down Expand Up @@ -274,4 +300,87 @@ void DisplayPopup(object sender, EventArgs e)
popupMediaElement.Handler?.DisconnectHandler();
};
}
}

/// <summary>
/// Sample implementation of an SRT parser.
/// </summary>
partial class SrtParser : IParser
{
static readonly Regex timecodePatternSRT = SRTRegex();

public List<SubtitleCue> ParseContent(string content)
{
var cues = new List<SubtitleCue>();
if (string.IsNullOrEmpty(content))
{
return cues;
}

var lines = content.Split(SubtitleParser.Separator, StringSplitOptions.RemoveEmptyEntries);

SubtitleCue? currentCue = null;
var textBuffer = new StringBuilder();

foreach (var line in lines)
{
if (int.TryParse(line, out _))
{
continue;
}

var match = timecodePatternSRT.Match(line);
if (match.Success)
{
if (currentCue is not null)
{
currentCue.Text = textBuffer.ToString().TrimEnd('\r', '\n');
cues.Add(currentCue);
textBuffer.Clear();
}

currentCue = CreateCue(match);
}
else if (currentCue is not null && !string.IsNullOrWhiteSpace(line))
{
textBuffer.AppendLine(line.Trim().TrimEnd('\r', '\n'));
}
}

if (currentCue is not null)
{
currentCue.Text = textBuffer.ToString().TrimEnd('\r', '\n');
cues.Add(currentCue);
}
if (cues.Count == 0)
{
throw new FormatException("Invalid SRT format");
}
return cues;
}

static SubtitleCue CreateCue(Match match)
{
var StartTime = ParseTimecode(match.Groups[1].Value);
var EndTime = ParseTimecode(match.Groups[2].Value);
var Text = string.Empty;
if (StartTime > EndTime)
{
throw new FormatException("Start time cannot be greater than end time.");
}
return new SubtitleCue
{
StartTime = StartTime,
EndTime = EndTime,
Text = Text
};
}

static TimeSpan ParseTimecode(string timecode)
{
return TimeSpan.ParseExact(timecode, @"hh\:mm\:ss\,fff", CultureInfo.InvariantCulture);
}

[GeneratedRegex(@"(\d{2}\:\d{2}\:\d{2}\,\d{3}) --> (\d{2}\:\d{2}\:\d{2}\,\d{3})", RegexOptions.Compiled)]
private static partial Regex SRTRegex();
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,9 @@
<string>bluetooth-central</string>
<string>audio</string>
</array>
<key>UIAppFonts</key>
<array>
<string>Resources/Fonts/PlaywriteSK-Regular.ttf</string>
</array>
</dict>
</plist>
4 changes: 4 additions & 0 deletions samples/CommunityToolkit.Maui.Sample/Platforms/iOS/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,9 @@
<array>
<string>audio</string>
</array>
<key>UIAppFonts</key>
<array>
<string>Resources/Fonts/PlaywriteSK-Regular.ttf</string>
</array>
</dict>
</plist>
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using CommunityToolkit.Maui.Core.Handlers;
using CommunityToolkit.Maui.Core.Views;
using CommunityToolkit.Maui.Views;
using CommunityToolkit.Maui.Core;

namespace CommunityToolkit.Maui;

Expand All @@ -16,9 +16,18 @@ public static class AppBuilderExtensions
/// <returns><see cref="MauiAppBuilder"/> initialized for <see cref="MediaElement"/>.</returns>
public static MauiAppBuilder UseMauiCommunityToolkitMediaElement(this MauiAppBuilder builder)
{
var importedFonts = FontHelper.GetExportedFonts();

builder.ConfigureMauiHandlers(h =>
{
h.AddHandler<MediaElement, MediaElementHandler>();
})
.ConfigureFonts(fonts =>
{
foreach (var (FontFileName, Alias) in importedFonts)
{
fonts.AddFont(FontFileName, Alias);
}
});

#if ANDROID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
<WarningsAsErrors>$(WarningsAsErrors);CS1591</WarningsAsErrors>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
<AndroidResource Include="Assets\*"/>
</ItemGroup>

<ItemGroup>
<None Include="..\..\build\nuget.png" PackagePath="icon.png" Pack="true"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System.Reflection;
using System.Text.RegularExpressions;

namespace CommunityToolkit.Maui.Core;

sealed class FontExtensions
{
public record struct FontFamily(string input)
{
static readonly string ttfPattern = @"(.+\.ttf)#(.+)";
static readonly string otfPattern = @"(.+\.otf)#(.+)";
readonly Match ttfMatch = Regex.Match(input, ttfPattern);
readonly Match otfMatch = Regex.Match(input, otfPattern);
public readonly string Android
{
get
{
if (ttfMatch.Success)
{
return ttfMatch.Groups[1].Value;
}
if (otfMatch.Success)
{
return otfMatch.Groups[1].Value;
}
else
{
System.Diagnostics.Trace.TraceError("The input string is not in the expected format.");
return string.Empty;
}
}
}
public readonly string WindowsFont
{
get
{
if (ttfMatch.Success)
{
return $"ms-appx:///{ttfMatch.Groups[1].Value}#{ttfMatch.Groups[2].Value}";
}
if (otfMatch.Success)
{
return $"ms-appx:///{otfMatch.Groups[1].Value}#{otfMatch.Groups[2].Value}";
}
else
{
System.Diagnostics.Trace.TraceError("The input string is not in the expected format.");
return string.Empty;
} }
}
public readonly string MacIOS
{
get
{
if (ttfMatch.Success)
{
return ttfMatch.Groups[2].Value;
}
if (otfMatch.Success)
{
return otfMatch.Groups[2].Value;
}
else
{
System.Diagnostics.Trace.TraceError("The input string is not in the expected format.");
return string.Empty;
}
}
}
}
}

static class FontHelper
{
/// <summary>
/// Returns the list of exported fonts from the assembly.
/// </summary>
/// <returns></returns>
public static IEnumerable<(string FontFileName, string Alias)> GetExportedFonts()
{
var assembly = typeof(FontHelper).Assembly;
var exportedFonts = new List<(string FontFileName, string Alias)>();
var customAttributes = assembly.GetCustomAttributes<ExportFontAttribute>();
foreach (var attribute in customAttributes)
{
exportedFonts.Add((attribute.FontFileName, attribute.Alias));
}
return exportedFonts;
}
}
Loading
Loading