Skip to content

Commit

Permalink
Merge pull request #289 from character-map-uwp/Oct-23
Browse files Browse the repository at this point in the history
Oct 23
  • Loading branch information
EdiWang authored Oct 22, 2023
2 parents 4bc3f1a + 094c8ed commit 714184f
Show file tree
Hide file tree
Showing 27 changed files with 432 additions and 187 deletions.
2 changes: 1 addition & 1 deletion CharacterMap/CharacterMap.CX/CustomFontManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ ComPtr<IDWriteFontCollection3> CustomFontManager::GetFontCollectionFromPath(Plat
ComPtr<IDWriteFontCollection> tcollection;
ComPtr<IDWriteFontCollection3> collection;

auto factory = GetIsolatedFactory();
auto& factory = GetIsolatedFactory();
ThrowIfFailed(factory->CreateCustomFontCollection(m_customLoader.Get(), key, keySize, &tcollection));

tcollection.As<IDWriteFontCollection3>(&collection);
Expand Down
6 changes: 3 additions & 3 deletions CharacterMap/CharacterMap.CX/DWHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ template <class T> void SafeRelease(T** ppT)
namespace
{
/// <summary>
/// Opposite of DWRITE_MAKE_OPENTYPE_TAG, returns String
/// representation of OpenType tag.
/// </summary>
/// Opposite of DWRITE_MAKE_OPENTYPE_TAG, returns String
/// representation of OpenType tag.
/// </summary>
Platform::String^ GetOpenTypeFeatureTag(UINT32 value)
{
wchar_t buffer[] = L" ";
Expand Down
5 changes: 1 addition & 4 deletions CharacterMap/CharacterMap.CX/DWriteFontAxis.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,7 @@ namespace CharacterMapCX

DWRITE_FONT_AXIS_VALUE GetDWriteValue()
{
auto value = DWRITE_FONT_AXIS_VALUE();
value.axisTag = static_cast<DWRITE_FONT_AXIS_TAG>(m_tag_raw);
value.value = this->Value;
return value;
return { static_cast<DWRITE_FONT_AXIS_TAG>(m_tag_raw), this->Value };
}

private:
Expand Down
2 changes: 1 addition & 1 deletion CharacterMap/CharacterMap.CX/DWriteProperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ namespace CharacterMapCX

property Array<UINT8>^ Panose { Array<UINT8>^ get()
{
DWRITE_PANOSE* pan = new DWRITE_PANOSE();
DWRITE_PANOSE pan[10];
m_font->GetPanose(pan);

bool valid = false;
Expand Down
5 changes: 4 additions & 1 deletion CharacterMap/CharacterMap.CX/DirectText.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ Windows::Foundation::Size CharacterMapCX::Controls::DirectText::MeasureOverride(

/* Set flow direction */
if (this->FlowDirection == Windows::UI::Xaml::FlowDirection::RightToLeft)
idFormat->SetFlowDirection(DWRITE_FLOW_DIRECTION::DWRITE_FLOW_DIRECTION_RIGHT_TO_LEFT);
idFormat->SetReadingDirection(DWRITE_READING_DIRECTION_RIGHT_TO_LEFT);
else
idFormat->SetReadingDirection(DWRITE_READING_DIRECTION_LEFT_TO_RIGHT);


/* Set blank fallback font */
if (FallbackFont != nullptr)
Expand Down
4 changes: 3 additions & 1 deletion CharacterMap/CharacterMap.CX/NativeInterop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,10 @@ ComPtr<IDWriteTextFormat3> CharacterMapCX::NativeInterop::CreateIDWriteTextForma
L"en-us",
&tempFormat);

tempFormat->SetFlowDirection(DWRITE_FLOW_DIRECTION_TOP_TO_BOTTOM);
tempFormat->SetReadingDirection(DWRITE_READING_DIRECTION_LEFT_TO_RIGHT);

ComPtr<IDWriteTextFormat3> idFormat;
ThrowIfFailed(tempFormat.As(&idFormat));

return idFormat;
}
8 changes: 4 additions & 4 deletions CharacterMap/CharacterMap/Controls/SettingsPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ public object Title
DependencyProperty.Register(nameof(Title), typeof(object), typeof(SettingsPresenter), new PropertyMetadata(null));


public string Description
public object Description
{
get { return (string)GetValue(DescriptionProperty); }
get { return (object)GetValue(DescriptionProperty); }
set { SetValue(DescriptionProperty, value); }
}

public static readonly DependencyProperty DescriptionProperty =
DependencyProperty.Register(nameof(Description), typeof(string), typeof(SettingsPresenter), new PropertyMetadata(null, (d,e) =>
DependencyProperty.Register(nameof(Description), typeof(object), typeof(SettingsPresenter), new PropertyMetadata(null, (d,e) =>
{
((SettingsPresenter)d).UpdateDescriptionStates();
}));
Expand Down Expand Up @@ -219,7 +219,7 @@ void UpdateIconStates()

private void UpdateDescriptionStates()
{
string state = string.IsNullOrWhiteSpace(Description) ? "NoDescriptionState" : "DescriptionState";
string state = Description is null ? "NoDescriptionState" : "DescriptionState";
VisualStateManager.GoToState(this, state, true);
}

Expand Down
6 changes: 6 additions & 0 deletions CharacterMap/CharacterMap/Core/Converters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ public static GridLength GridLengthAorB(bool input, string a, string b)
public static string GetLocalizedEnumName(Enum a)
=> Localization.Get($"{a.GetType().Name}_{a}");

public static string GetFormat(string key, object arg)
=> Localization.Get(key, arg);

public static string GetFormat2(string key, object arg, object arg2)
=> Localization.Get(key, arg, arg2);

public static FontFamily GetPreviewFontSource(FontVariant variant)
{
if (_settings == null)
Expand Down
37 changes: 30 additions & 7 deletions CharacterMap/CharacterMap/Core/ExportManager.Font.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage.Streams;
using Windows.Storage;
Expand Down Expand Up @@ -46,13 +45,19 @@ public static async void RequestExportFontFile(FontVariant variant)
WeakReferenceMessenger.Default.Send(new AppNotificationMessage(true, new ExportFontFileResult(null, false)));
}

internal static Task ExportCollectionAsZipAsync(IList<InstalledFont> fontList, UserFontCollection selectedCollection)
internal static Task ExportCollectionAsZipAsync(
IList<InstalledFont> fontList,
UserFontCollection selectedCollection,
Action<string> callback = null)
{
var fonts = fontList.SelectMany(f => f.Variants).ToList();
return ExportFontsAsZipAsync(fonts, selectedCollection.Name);
return ExportFontsAsZipAsync(fonts, selectedCollection.Name, callback);
}

internal static async Task ExportFontsAsZipAsync(List<FontVariant> fonts, string name)
internal static async Task ExportFontsAsZipAsync(
List<FontVariant> fonts,
string name,
Action<string> callback = null)
{
if (await PickFileAsync(name, "ZIP", new[] { ".zip" }) is StorageFile file)
{
Expand All @@ -63,7 +68,10 @@ await Task.Run(async () =>
using var i = await file.OpenStreamForWriteAsync();
i.SetLength(0);

int c = 0;
using ZipArchive z = new(i, ZipArchiveMode.Create);
callback?.Invoke($"0%");

foreach (var font in fonts)
{
if (DirectWrite.IsFontLocal(font.Face))
Expand All @@ -73,6 +81,9 @@ await Task.Run(async () =>
using IOutputStream s = entry.Open().AsOutputStream();
await DirectWrite.WriteToStreamAsync(font.Face, s);
}

c++;
callback?.Invoke($"{((double)c / (double)fonts.Count) * 100:0}%");
}

await i.FlushAsync();
Expand All @@ -82,20 +93,26 @@ await Task.Run(async () =>
}
}

internal static Task ExportCollectionToFolderAsync(IList<InstalledFont> fontList)
internal static Task ExportCollectionToFolderAsync(
IList<InstalledFont> fontList,
Action<string> callback = null)
{
var fonts = fontList.SelectMany(f => f.Variants).ToList();
return ExportFontsToFolderAsync(fonts);
return ExportFontsToFolderAsync(fonts, callback);
}

internal static async Task ExportFontsToFolderAsync(List<FontVariant> fonts)
internal static async Task ExportFontsToFolderAsync(
List<FontVariant> fonts,
Action<string> callback = null)
{
if (await PickFolderAsync() is StorageFolder folder)
{
await Task.Run(async () =>
{
ExportNamingScheme scheme = ResourceHelper.AppSettings.ExportNamingScheme;
callback?.Invoke($"0%");

int c = 0;
foreach (var font in fonts)
{
if (DirectWrite.IsFontLocal(font.Face))
Expand All @@ -104,6 +121,9 @@ await Task.Run(async () =>
StorageFile file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting).AsTask().ConfigureAwait(false);
await TryWriteToFileAsync(font, file).ConfigureAwait(false);
}

c++;
callback?.Invoke($"{((double)c / (double)fonts.Count) * 100:0}%");
}
});

Expand Down Expand Up @@ -144,6 +164,9 @@ private static string GetFileName(FontVariant font, ExportNamingScheme scheme)
if (scheme == ExportNamingScheme.System && !string.IsNullOrWhiteSpace(src))
fileName = src;

if (scheme is ExportNamingScheme.Optimised && FontFinder.ImportFormats.Contains(ext) is false)
ext = ".ttf";

if (string.IsNullOrWhiteSpace(fileName))
fileName = $"{font.FamilyName.Trim()} {font.PreferredName.Trim()}{ext}";

Expand Down
16 changes: 16 additions & 0 deletions CharacterMap/CharacterMap/Core/FontFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ public static async Task<DWriteFontSet> InitialiseAsync()
return systemFonts;
}

public static int SystemFamilyCount { get; private set; }
public static int SystemFaceCount { get; private set; }

public static Task LoadFontsAsync(bool clearExisting = true)
{
// It's possible to go down this path if the font collection
Expand All @@ -119,8 +122,10 @@ public static Task LoadFontsAsync(bool clearExisting = true)
await _loadSemaphore.WaitAsync().ConfigureAwait(false);
NativeInterop interop = Utils.GetInterop();

#if DEBUG
Stopwatch s = new Stopwatch();
s.Start();
#endif

// Load SystemFonts and imported fonts in parallel
// 1.1. Load imported fonts
Expand Down Expand Up @@ -166,6 +171,9 @@ public static Task LoadFontsAsync(bool clearExisting = true)
var imports = resultList.ToDictionary(d => d.Key, v => v.Value.Clone());

/* Add all system fonts */
SystemFamilyCount = systemFonts.Families.Count;
SystemFaceCount = systemFonts.Fonts.Count;

foreach (var font in systemFonts.Fonts)
AddFont(resultList, font);

Expand All @@ -177,8 +185,10 @@ public static Task LoadFontsAsync(bool clearExisting = true)
if (Fallback == null)
Fallback = interop.CreateEmptyFallback();

#if DEBUG
s.Stop();
var elasped = s.Elapsed;
#endif

_loadSemaphore.Release();

Expand Down Expand Up @@ -217,6 +227,12 @@ internal static List<FontVariant> GetImportedVariants()
.ToList();
}

internal static List<FontVariant> GetSystemVariants()
{
return Fonts.SelectMany(f => f.Variants.Where(v => v.IsImported is false))
.ToList();
}

/*
* Helper method for adding fonts.
*/
Expand Down
32 changes: 32 additions & 0 deletions CharacterMap/CharacterMap/Core/Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1296,5 +1296,37 @@ public static void SetRenderScale(DependencyObject obj, double value)
}));

#endregion

#region Flyout

public static FlyoutBase GetFlyout(DependencyObject obj)
{
return (FlyoutBase)obj.GetValue(FlyoutProperty);
}

public static void SetFlyout(DependencyObject obj, FlyoutBase value)
{
obj.SetValue(FlyoutProperty, value);
}

public static readonly DependencyProperty FlyoutProperty =
DependencyProperty.RegisterAttached("Flyout", typeof(FlyoutBase), typeof(Properties), new PropertyMetadata(null, (d,e) =>
{
if (d is ButtonBase b)
{
b.Click -= ButtonFlyoutClick;
b.Click += ButtonFlyoutClick;
}
}));

private static void ButtonFlyoutClick(object sender, RoutedEventArgs e)
{
if (sender is ButtonBase b && GetFlyout(b) is FlyoutBase fly)
{
fly.ShowAt(b);
}
}

#endregion
}
}
3 changes: 3 additions & 0 deletions CharacterMap/CharacterMap/Helpers/ResourceHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@ private void Element_Unloaded(object sender, RoutedEventArgs e)

public void Update()
{
if (DesignMode.DesignModeEnabled)
return;

ResourceHelper.TryResolveThemeStyle3(_element);
return;

Expand Down
46 changes: 37 additions & 9 deletions CharacterMap/CharacterMap/Provider/SQLiteGlyphProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Windows.ApplicationModel;
using Windows.UI.Xaml.Controls;
using CharacterMap.Models;
using System.Diagnostics;

namespace CharacterMap.Provider
{
Expand Down Expand Up @@ -174,9 +175,9 @@ private Task<IReadOnlyList<IGlyphData>> InternalSearchAsync(string ftsTable, str

// 1.3. If the search is ambiguous we should still search for description matches,
// otherwise we can return right now
if (!ambiguous)
return hexresults;
else
//if (!ambiguous)
// return hexresults;
//else
{
hexResult = hexresults.Cast<GlyphDescription>().FirstOrDefault();
break;
Expand All @@ -187,10 +188,10 @@ private Task<IReadOnlyList<IGlyphData>> InternalSearchAsync(string ftsTable, str
// 1.4. If the search is ambiguous we should still search for description matches,
// otherwise we can return right now with no hex results
// If we are a generic symbol font, that's all folks. Time to leave.
if (!ambiguous)
{
return GlyphService.EMPTY_SEARCH;
}
//if (!ambiguous)
//{
// return GlyphService.EMPTY_SEARCH;
//}
}

// 2. If we're performing SQL, create the base query filter
Expand All @@ -204,12 +205,20 @@ private Task<IReadOnlyList<IGlyphData>> InternalSearchAsync(string ftsTable, str
/// We don't want to throw an exception if we ever hit this case, we'll just do our best.
foreach (var range in variant.UnicodeRanges.Take(995))
{
string qr = range.First != range.Last
? "Ix BETWEEN {0} AND {1}"
: "Ix == {0}";

if (next)
sb.AppendFormat(" OR Ix BETWEEN {0} AND {1}", range.First, range.Last);
sb.AppendFormat(range.First != range.Last
? " OR Ix BETWEEN {0} AND {1}"
: " OR Ix == {0}", range.First, range.Last);
else
{
next = true;
sb.AppendFormat("WHERE (Ix BETWEEN {0} AND {1}", range.First, range.Last);
sb.AppendFormat(range.First != range.Last
? "WHERE (Ix BETWEEN {0} AND {1}"
: "WHERE (Ix == {0}", range.First, range.Last);
}
}
sb.Append(")");
Expand Down Expand Up @@ -248,6 +257,25 @@ List<IGlyphData> InsertHex(List<IGlyphData> list)
extra = string.Format(" AND Ix NOT IN ({0})", string.Join(", ", results.Select(r => r.UnicodeIndex)));
}

/// BENCHMARKING FOR FTFS VS NORMAL TABLES
//Stopwatch sw = Stopwatch.StartNew();
//object res = null;
//for (int i = 0; i < 100; i++)
//{
// string bsql2 = $"SELECT * FROM {table} {sb.ToString()} AND Description LIKE ? LIMIT {limit}";
// res = _connection.GetGlyphData(table, bsql2, $"%{query}%");
//}
//var time = sw.ElapsedMilliseconds;
//sw.Restart();
//for (int i = 0; i < 100; i++)
//{
// string bsql = $"SELECT * FROM {ftsTable} {sb}{extra} AND Description MATCH ? LIMIT {limit}";
// res = _connection.GetGlyphData(ftsTable, bsql, $"{query}*");
//}
//var time2 = sw.ElapsedMilliseconds;
//Debugger.Break();


// 3.3. Execute!
string sql = $"SELECT * FROM {ftsTable} {sb}{extra} AND Description MATCH ? LIMIT {limit}";
results.AddRange(_connection.GetGlyphData(ftsTable, sql, $"{query}*"));
Expand Down
Loading

0 comments on commit 714184f

Please sign in to comment.