add TypefaceProto (holds font family/weight)

This commit is contained in:
John Colvin committed 2019-09-13 16:55:51 -05:00
1 parent 269b08e119
commit 69fdec2866
1 file changed
+61
@@ -0,0 +1,61 @@
using bts.utils;
using mbts.ui.utility.text;
using ProtoBuf;
using System;
using System.Windows;
using FontFamily = System.Windows.Media.FontFamily;
namespace bts.proto {
[ProtoContract]
readonly struct TypefaceProto : IEquatable<TypefaceProto> {
static readonly int _type_hash = typeof(TypefaceProto).GetHashCode();
[ProtoMember(1)]
public readonly string font_family;
[ProtoMember(2)]
public readonly int font_weight;
// TODO (maybe): add property for FontStyle (italic, underline, etc.)
public TypefaceProto(string font_family, int font_weight) {
this.font_family = font_family;
this.font_weight = font_weight;
}
public TypefaceProto with(string font_family = null, int? font_weight = null, double font_size = double.NaN) =>
new TypefaceProto(
font_family ?? this.font_family,
font_weight ?? this.font_weight);
public bool Equals(TypefaceProto other) =>
font_family == other.font_family &&
font_weight == other.font_weight;
public override bool Equals(object obj) =>
obj is TypefaceProto tfp && Equals(tfp);
public override int GetHashCode() =>
EqualityComparer.auto_phase(
font_family?.GetHashCode() ?? 0,
font_weight);
public static bool operator ==(TypefaceProto x, TypefaceProto y) => x.Equals(y);
public static bool operator !=(TypefaceProto x, TypefaceProto y) => !x.Equals(y);
}
static class TypefaceProtoExtensions {
public static (FontFamily family, FontWeight? weight) to_typeface_parts(this TypefaceProto proto) =>
(string.IsNullOrEmpty(proto.font_family) ? null : new FontFamily(proto.font_family),
proto.font_weight == 0 ? (FontWeight?)null : FontWeight.FromOpenTypeWeight(proto.font_weight));
public static (FontFamily family, FontWeight weight) to_typeface_parts_safe(
this TypefaceProto proto,
FontFamily fallback_family = null,
FontWeight? fallback_weight = null) {
//
var (family, weight) = proto.to_typeface_parts();
return (
family ?? fallback_family ?? TextUtils.StandardFontFamily,
weight ?? fallback_weight ?? FontWeights.Normal);
}
}
}