Пример #1
0
        public static Font ToFont(this FigmaTypeStyle style)
        {
            string family = style.fontFamily;

            if (family == "SF UI Text")
            {
                family = ".SF NS Text";
            }
            else if (family == "SF Mono")
            {
                family = ".SF NS Display";
            }
            else
            {
                Console.WriteLine("FONT: {0} - {1}", family, style.fontPostScriptName);
            }

            var size   = style.fontSize - 3;
            var isBold = style.fontPostScriptName != null && style.fontPostScriptName.EndsWith("-Bold");
            var font   = new Font(family, size, isBold ? FontStyle.Bold : FontStyle.Regular);

            if (font == null)
            {
                Console.WriteLine($"[ERROR] Font not found :{family}");
                font = new Font("Times New Roman", style.fontSize);
            }
            return(font);
        }
Пример #2
0
        public static void ConfigureStyle(this TextElement textElement, FigmaTypeStyle style)
        {
            string family = style.fontFamily;

            if (family == "SF UI Text")
            {
                family = ".SF NS Text";
            }
            else if (family == "SF Mono")
            {
                family = ".SF NS Display";
            }
            else
            {
                Console.WriteLine("FONT: {0} - {1}", family, style.fontPostScriptName);
            }
            textElement.FontFamily = new FontFamily(family);

            if (style.fontSize > 0)
            {
                textElement.FontSize = style.fontSize;// -3 ;
            }
            textElement.FontWeight = FontWeight.FromOpenTypeWeight(style.fontWeight);
            if (style.letterSpacing != default)
            {
                textElement.FontStretch = FontStretch.FromOpenTypeStretch(style.letterSpacing > 9 ? 9 : (int)style.letterSpacing);
            }

            var fill = style.fills.FirstOrDefault();

            if (fill != null)
            {
                textElement.Foreground = fill.color.ToColor();
            }
        }
Пример #3
0
        public static string ToNSFontDesignerString(this FigmaTypeStyle style)
        {
            var font   = style.ToNSFont();
            var family = font.FamilyName;
            var size   = font.PointSize;
            var w      = NSFontManager.SharedFontManager.WeightOfFont(font);
            var traits = NSFontManager.SharedFontManager.TraitsOfFont(font);

            return(string.Format("NSFontManager.SharedFontManager.FontWithFamily(\"{0}\", {1}, {2}, {3})", family, traits.ToDesignerString(), w, style.fontSize));
        }
Пример #4
0
        public static NSTextAlignment GetNSTextAlignment(FigmaText text)
        {
            FigmaTypeStyle style = text.style;

            if (style.textAlignHorizontal == "RIGHT")
            {
                return(NSTextAlignment.Right);
            }

            if (style.textAlignHorizontal == "CENTER")
            {
                return(NSTextAlignment.Center);
            }

            return(NSTextAlignment.Left);
        }
Пример #5
0
        public static UIFont ToUIFont(this FigmaTypeStyle style)
        {
            string family = style.fontFamily;

            if (family == "SF UI Text")
            {
                family = ".SF NS Text";
            }
            else if (family == "SF Mono")
            {
                family = ".SF NS Display";
            }
            else
            {
                Console.WriteLine("FONT: {0} - {1}", family, style.fontPostScriptName);
            }

            var font = UIFont.FromName(family, style.fontSize);

            //var w = ToAppKitFontWeight(style.fontWeight);
            //NSFontTraitMask traits = default(NSFontTraitMask);
            //if (style.fontPostScriptName != null && style.fontPostScriptName.EndsWith("-Bold"))
            //{
            //    traits = NSFontTraitMask.Bold;
            //}
            //else
            //{

            //}
            //if (font != null)
            //{
            //    var w = NSFontManager.SharedFontManager.WeightOfFont(font);
            //    var traits = NSFontManager.SharedFontManager.TraitsOfFont(font);

            //}

            //font = CTFontManager. SharedFontManager.FontWithFamily(family, traits, w, style.fontSize);
            //var font = NSFont.FromFontName(".SF NS Text", 12);

            if (font == null)
            {
                Console.WriteLine($"[ERROR] Font not found :{family}");
                font = UIFont.SystemFontOfSize(style.fontSize);
            }
            return(font);
        }
Пример #6
0
        static string GetFontName(FigmaTypeStyle style)
        {
            string family = style.fontFamily;

            if (family == "SF UI Text")
            {
                family = ".SF NS Text";
            }
            else if (family == "SF Mono")
            {
                family = ".SF NS Display";
            }
            else
            {
                Console.WriteLine("FONT: {0} - {1}", family, style.fontPostScriptName);
            }
            return(family);
        }
Пример #7
0
 public static void FillEmptyStylePropertiesWithDefaults(this FigmaTypeStyle style, FigmaText text)
 {
     if (style.fills == default)
     {
         style.fills = text.fills;
     }
     if (style.fontFamily == default)
     {
         style.fontFamily = text.style.fontFamily;
     }
     if (style.fontPostScriptName == default)
     {
         style.fontPostScriptName = text.style.fontPostScriptName;
     }
     if (style.fontSize == default)
     {
         style.fontSize = text.style.fontSize;
     }
     if (style.fontWeight == default)
     {
         style.fontWeight = text.style.fontWeight;
     }
     if (style.letterSpacing == default)
     {
         style.letterSpacing = text.style.letterSpacing;
     }
     if (style.lineHeightPercent == default)
     {
         style.lineHeightPercent = text.style.lineHeightPercent;
     }
     if (style.lineHeightPx == default)
     {
         style.lineHeightPx = text.style.lineHeightPx;
     }
     if (style.textAlignHorizontal == default)
     {
         style.textAlignHorizontal = text.style.textAlignHorizontal;
     }
     if (style.textAlignVertical == default)
     {
         style.textAlignVertical = text.style.textAlignVertical;
     }
 }
Пример #8
0
        public static NSFont ToNSFont(this FigmaTypeStyle style)
        {
            string family = style.fontFamily;

            if (FontConversion.TryGetValue(family, out string newFamilyName))
            {
                Console.WriteLine("{0} font was in the conversion dicctionary and was replaced by {1}.", family, newFamilyName);
                family = newFamilyName;
            }

            var fontDefault = NSFont.SystemFontOfSize(style.fontSize, GetFontWeight(style));
            var traits      = NSFontManager.SharedFontManager.TraitsOfFont(fontDefault);
            var weight      = Math.Max(LiteForms.Cocoa.ViewsHelper.ToAppKitFontWeight(style.fontWeight) - 2, 1);

            NSFont font = null;

            try
            {
                font = NSFontManager.SharedFontManager.FontWithFamily(family, traits, weight, style.fontSize);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            if (font == null)
            {
                try
                {
                    font = NSFontManager.SharedFontManager.FontWithFamily(fontDefault.FamilyName, traits, weight, style.fontSize);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }
            return(font);
        }
Пример #9
0
        static nfloat GetFontWeight(FigmaTypeStyle style)
        {
            if (style.fontPostScriptName != null)
            {
                if (style.fontPostScriptName.EndsWith("-Bold"))
                {
                    return(NSFontWeight.Regular);
                }
                if (style.fontPostScriptName.EndsWith("-Light"))
                {
                    return(NSFontWeight.Light);
                }
                if (style.fontPostScriptName.EndsWith("-Thin"))
                {
                    return(NSFontWeight.Thin);
                }
                if (style.fontPostScriptName.EndsWith("-SemiBold"))
                {
                    return(NSFontWeight.Semibold);
                }
            }

            return(NSFontWeight.Regular);
        }
Пример #10
0
        public static void ConfigureStyle(this Label label, FigmaTypeStyle style)
        {
            string family = style.fontFamily;

            if (family == "SF UI Text")
            {
                family = ".SF NS Text";
            }
            else if (family == "SF Mono")
            {
                family = ".SF NS Display";
            }
            else
            {
                Console.WriteLine("FONT: {0} - {1}", family, style.fontPostScriptName);
            }

            var size   = style.fontSize - 3;
            var isBold = style.fontPostScriptName != null && style.fontPostScriptName.EndsWith("-Bold");

            label.FontSize   = size;
            label.FontFamily = new FontFamily(family);
            label.FontWeight = isBold ?  FontWeights.Bold : FontWeights.Regular;
        }