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

May 24 #328

Merged
merged 15 commits into from
May 24, 2024
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
1 change: 0 additions & 1 deletion CharacterMap/CharacterMap.CX/DWriteFontFace.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ namespace CharacterMapCX
{
m_font = font;
m_dwProperties = properties;

};

ComPtr<IDWriteFontCollection3> GetFontCollection()
Expand Down
66 changes: 47 additions & 19 deletions CharacterMap/CharacterMap.CX/FontAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ namespace CharacterMapCX
{
public:

property bool HasBitmapGlyphs { bool get() { return m_hasBitmap; } }
property bool HasBitmapGlyphs { bool get() { ReadTables(); return m_hasBitmap; } }

property bool HasCOLRGlyphs { bool get() { return m_hasCOLR; } }
property bool HasCOLRGlyphs { bool get() { ReadTables(); return m_hasCOLR; } }

property bool HasSVGGlyphs { bool get() { return m_hasSVG; } }
property bool HasSVGGlyphs { bool get() { ReadTables(); return m_hasSVG; } }

property bool ContainsVectorColorGlyphs { bool get() { return m_hasSVG || m_hasCOLR; } }
property bool ContainsVectorColorGlyphs { bool get() { ReadTables(); return m_hasSVG || m_hasCOLR; } }

property bool HasGlyphNames { bool get() { return m_hasGlyphNames; } }
property bool HasGlyphNames { bool get() { ReadTables(); return m_hasGlyphNames; } }

property int COLRVersion { int get() { return m_colrVersion; } }
property int COLRVersion { int get() { ReadTables(); return m_colrVersion; } }

property bool SupportsCOLRv1 { bool get() { return m_colrVersion >= 1; } }
property bool SupportsCOLRv1 { bool get() { ReadTables(); return m_colrVersion >= 1; } }

property bool HasVariationAxis { bool get() { return m_variableAxis != nullptr && m_variableAxis->Size > 0; } }
property bool HasVariationAxis { bool get() { GetVariableProperties(); return m_variableAxis != nullptr && m_variableAxis->Size > 0; } }

property bool IsRemote { bool get() { return m_isRemote; } }

Expand All @@ -52,33 +52,35 @@ namespace CharacterMapCX

property IVectorView<DWriteFontAxis^>^ Axis
{
IVectorView<DWriteFontAxis^>^ get() { return m_axis; }
IVectorView<DWriteFontAxis^>^ get() { GetVariableProperties(); return m_axis; }
}

property IVectorView<DWriteFontAxis^>^ VariableAxis
{
IVectorView<DWriteFontAxis^>^ get() { return m_variableAxis; }
IVectorView<DWriteFontAxis^>^ get() { GetVariableProperties(); return m_variableAxis; }
}

void ResetVariableAxis()
{
if (m_variableAxis == nullptr)
return;

for each (auto a in m_variableAxis)
a->Value = a->DefaultValue;
}

/// <summary>
/// Mappings of glyph index to font-provided glyph names
/// </summary>
property IMapView<int, String^>^ GlyphNameMappings;
property IMapView<int, String^>^ GlyphNameMappings { IMapView<int, String^>^ get() { ReadTables(); return m_mappings; } }


FontAnalysis() { }

FontAnalysis(DWriteFontFace^ fontFace)
{
ComPtr<IDWriteFontFaceReference> ref = fontFace->GetReference();
AnalyseTables(ref);
GetFileProperties(ref);
m_ref = fontFace->GetReference();
GetFileProperties(m_ref);
}

private:
Expand All @@ -97,9 +99,30 @@ namespace CharacterMapCX
IVectorView<DWriteFontAxis^>^ m_variableAxis;
IVectorView<DWriteFontAxis^>^ m_axis;

void GetFileProperties(ComPtr<IDWriteFontFaceReference> faceRef)
IMapView<int, String^>^ m_mappings;
ComPtr<IDWriteFontFaceReference> m_ref;

bool m_tables = false;
bool m_var = false;

void ReadTables()
{
m_axis = DirectWrite::GetAxis(faceRef);
if (m_tables)
return;

m_tables = true;
AnalyseTables();
}


void GetVariableProperties()
{
if (m_var)
return;

m_var = true;

m_axis = DirectWrite::GetAxis(m_ref);

// Check for variable font axis
Vector<DWriteFontAxis^>^ variable = ref new Vector<DWriteFontAxis^>();
Expand All @@ -109,7 +132,10 @@ namespace CharacterMapCX
variable->Append(a);
}
m_variableAxis = variable->GetView();
}

void GetFileProperties(ComPtr<IDWriteFontFaceReference> faceRef)
{
// Get File Size
m_fileSize = faceRef->GetFileSize();

Expand Down Expand Up @@ -138,6 +164,8 @@ namespace CharacterMapCX
{
m_filePath = ref new Platform::String(buffer);
}

delete[] buffer;
}
}
}
Expand All @@ -148,10 +176,10 @@ namespace CharacterMapCX
}
}

void AnalyseTables(ComPtr<IDWriteFontFaceReference> faceRef)
void AnalyseTables()
{
ComPtr<IDWriteFontFace3> f3;
faceRef->CreateFontFace(&f3);
m_ref->CreateFontFace(&f3);

ComPtr<IDWriteFontFace5> face;
f3.As(&face);
Expand Down Expand Up @@ -228,7 +256,7 @@ namespace CharacterMapCX
if (exists)
{
auto reader = ref new PostTableReader(tableData, tableSize);
GlyphNameMappings = reader->Mapping;
m_mappings = reader->Mapping;
m_hasGlyphNames = GlyphNameMappings != nullptr && GlyphNameMappings->Size > 0;
delete reader;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static class GeneratorExtensions

public static AttributeArgumentSyntax GetArgument(this AttributeSyntax attribute, string name)
{
return attribute.ArgumentList.Arguments.FirstOrDefault(a => a.NameEquals?.Name.Identifier.ValueText == name);
return attribute.ArgumentList?.Arguments.FirstOrDefault(a => a.NameEquals?.Name?.Identifier.ValueText == name);
}

public static PropertyDeclarationSyntax GetProperty(this AttributeSyntax attribute, string name)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CharacterMap.Generators.Readers;

public class AttachedPropertyReader : SyntaxReader
{
string TEMPLATE =
" public static {2} Get{0}(DependencyObject obj) => ({2})obj.GetValue({0}Property);\r\n\r\n" +
" public static void Set{0}(DependencyObject obj, {2} value) => obj.SetValue({0}Property, value);\r\n\r\n" +
" public static readonly DependencyProperty {0}Property =\r\n" +
" DependencyProperty.RegisterAttached(\"{0}\", typeof({2}), typeof({1}), new PropertyMetadata({3}, (d,e) => On{0}Changed(d,e)));\r\n\r\n" +
" static partial void On{0}Changed(DependencyObject d, DependencyPropertyChangedEventArgs e);\r\n";

List<DPData> data = [];

public override void Read(IEnumerable<SyntaxNode> nodes)
{
foreach (var n in nodes.OfType<ClassDeclarationSyntax>()
.Where(c => c.HasGenericAttribute("AttachedProperty")))
{
DPData src = new()
{
ParentClass = n.Identifier.ValueText,
ParentNamespace = n.GetNamespace(),
Usings = (n.Parent?.Parent as CompilationUnitSyntax)?.Usings.Select(u => $"using {u.Name.ToString()};")?.ToList() ?? new()
};

foreach (var a in n.AttributeLists
.SelectMany(s => s.Attributes)
.Where(a => a.Name.ToString().StartsWith("AttachedProperty")))
{
string type = null;
if (a.Name is GenericNameSyntax gen)
type = gen.TypeArgumentList.Arguments[0].ToString();

var d = a.GetArgument("Name") is { } na && na.NameEquals is { } ne // Attribute property path,
? src with
{
Name = a.GetArgument("Name")?.GetValue(),
Default = a.GetArgument("Default")?.GetValue() ?? "default",
Type = type ?? a.GetArgument("Type")?.GetValue()?.Replace("typeof(", string.Empty).Replace(")", string.Empty) ?? "object"
}
: src with // Constructor path - preferred
{
Name = a.ArgumentList?.Arguments[0].GetValue() ?? type,
Type = type ?? "object",
Default = a.ArgumentList?.Arguments.Skip(1)?.FirstOrDefault()?.GetValue() ?? "default"
};

data.Add(d);
}
}
}

public override void Write(GeneratorExecutionContext context)
{
if (data.Count == 0)
return;

foreach (var group in data.GroupBy(d => $"{d.ParentNamespace}.{d.ParentClass}.g.ap.cs"))
{
string file = group.Key;
string ns = group.First().ParentNamespace;
string target = group.First().ParentClass;
List<string> usings = group.First().Usings;

StringBuilder sb = new();

foreach (DPData dp in group)
sb.AppendLine(
string.Format(TEMPLATE, dp.Name, dp.ParentClass, dp.Type, dp.Default, dp.GetCastType("e.OldValue"), dp.GetCastType("e.NewValue")));

var s = sb.ToString();
context.AddSource(file, SourceText.From(
$@"{string.Join("\r\n", usings)}

namespace {ns};

partial class {target}
{{
{sb}
}}", Encoding.UTF8));
}
}
}
Loading