Skip to content

Commit

Permalink
Temporary solution to read woff2
Browse files Browse the repository at this point in the history
  • Loading branch information
emako committed Dec 29, 2024
1 parent ffecab9 commit 4eb4251
Show file tree
Hide file tree
Showing 91 changed files with 34,874 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//MIT, 2016-present, WinterDev
using System;
using System.IO;

using Typography.OpenFont.WebFont;
using BrotliSharpLib;
using System.IO.Compression;

namespace SampleWinForms;

public static class OurOpenFontSystemSetup
{
public static void SetupWoffDecompressFunctions()
{
//
//Woff
// WoffDefaultZlibDecompressFunc.DecompressHandler = (byte[] compressedBytes, byte[] decompressedResult) =>
// {
// //ZLIB
// //****
// //YOU can change to your prefer decode libs***
// //****

// bool result = false;
// try
// {
// var inflater = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater();
// inflater.SetInput(compressedBytes);
// inflater.Inflate(decompressedResult);
//#if DEBUG
// long outputLen = inflater.TotalOut;
// if (outputLen != decompressedResult.Length)
// {
// }
//#endif

// result = true;
// }
// catch (Exception ex)
// {
// }
// return result;
// };
//Woff2

Woff2DefaultBrotliDecompressFunc.DecompressHandler = (byte[] compressedBytes, Stream output) =>
{
//BROTLI
//****
//YOU can change to your prefer decode libs***
//****

bool result = false;
try
{
using (var ms = new MemoryStream(compressedBytes))
{
ms.Position = 0;//set to start pos
Decompress(ms, output);
}
result = true;
}
catch (Exception ex)
{
}
return result;
};
}

static void Decompress(Stream input, Stream output)
{
try
{
using (var bs = new BrotliStream(input, CompressionMode.Decompress))
using (var ms = new MemoryStream())
{
bs.CopyTo(output);
}
}
catch (IOException ex)
{
throw ex;
}
}
}
52 changes: 48 additions & 4 deletions QuickLook.Plugin/QuickLook.Plugin.FontViewer/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using QuickLook.Common.Helpers;
using QuickLook.Common.Plugin;
using QuickLook.Plugin.HtmlViewer;
using SampleWinForms;
using System;
using System.IO;
using System.IO.Packaging;
Expand All @@ -26,6 +27,7 @@
using System.Text;
using System.Windows;
using System.Windows.Resources;
using Typography.OpenFont.WebFont;

namespace QuickLook.Plugin.FontViewer;

Expand All @@ -44,7 +46,8 @@ public void Init()
public bool CanHandle(string path)
{
// The `*.eot` and `*.svg` font types are not supported
return !Directory.Exists(path) && new string[] { ".ttf", ".otf", ".woff", ".woff2" }.Any(path.ToLower().EndsWith);
// TODO: Check `*.otc` type
return !Directory.Exists(path) && new string[] { ".ttf", ".otf", ".woff", ".woff2", ".ttc" }.Any(path.ToLower().EndsWith);
}

public void Prepare(string path, ContextObject context)
Expand Down Expand Up @@ -109,6 +112,7 @@ private string GenerateFontHtml(string path)
// url('xxx.ttf') format('truetype'),
// url('xxx.svg#xxx') format('svg');
var fileName = Path.GetFileName(path);
var fileNameWithoutExt = Path.GetFileNameWithoutExtension(path);
var fileExt = Path.GetExtension(fileName);
static string GenerateRandomString(int length)
{
Expand All @@ -124,9 +128,8 @@ static string GenerateRandomString(int length)
return new string(result);
}

string cssUrl = $"src: url('{fileName}'), url('{fileName.Substring(0, fileExt.Length)}?#{GenerateRandomString(5)}{fileExt}')"
+ Path.GetExtension(path)
switch
string cssUrl = $"src: url('{fileName}'), url('{fileNameWithoutExt}?#{GenerateRandomString(5)}{fileExt}')"
+ fileExt switch
{
".eot" => " format('embedded-opentype');",
".woff" => " format('woff');",
Expand All @@ -136,6 +139,47 @@ static string GenerateRandomString(int length)
_ => ";",
};

if (string.IsNullOrEmpty(fontFamilyName))
{
string GetWoff2FontFamilyName()
{
OurOpenFontSystemSetup.SetupWoffDecompressFunctions();
using var fs = new FileStream(path, FileMode.Open, FileAccess.Read);
using var input = new BinaryReader(fs);
var woffReader = new Woff2Reader
{
DecompressHandler = Woff2DefaultBrotliDecompressFunc.DecompressHandler
};
input.BaseStream.Position = 0;
var info = woffReader.ReadPreview(input);

return info?.Name;
}
//string GetWoffFontFamilyName()
//{
// OurOpenFontSystemSetup.SetupWoffDecompressFunctions();
// using var fs = new FileStream(path, FileMode.Open, FileAccess.Read);
// using var input = new BinaryReader(fs);
// var woffReader = new WoffReader
// {
// DecompressHandler = WoffDefaultZlibDecompressFunc.DecompressHandler
// };
// input.BaseStream.Position = 0;
// var info = woffReader.ReadPreview(input);

// return info?.Name;
//}

if (fileExt.ToLower().Equals(".woff2"))
{
fontFamilyName = GetWoff2FontFamilyName();
}
//else if (fileExt.ToLower().Equals(".woff"))
//{
// fontFamilyName = GetWoffFontFamilyName();
//}
}

html = html.Replace("--font-family;", $"font-family: '{fontFamilyName}';")
.Replace("--font-url;", cssUrl)
.Replace("{{h1}}", fontFamilyName ?? fileName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@

<ItemGroup>
<PackageReference Include="FreeTypeSharp" Version="3.0.0" />
<!--<Reference Include="Typography.OpenFont">
<HintPath>.\Typography.OpenFont.dll</HintPath>
</Reference>-->
<!--<PackageReference Include="SharpZipLib" Version="1.4.2" />-->
<PackageReference Include="BrotliSharpLib" Version="0.3.3" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 4eb4251

Please sign in to comment.