Beispiel #1
0
        static CTFont CTFontWithFamilyName(string family, CTFontSymbolicTraits straits, float size, float weight)
        {
            var bold   = Math.Abs(weight - CTFontWeight.Bold) < 0.01f;
            var traits = new NSMutableDictionary();

            if (Math.Abs(weight) > 0.01 && !bold)
            {
                traits[CTFontTraitKey.Weight] = NSNumber.FromFloat(weight);
            }

            if (bold)
            {
                straits |= CTFontSymbolicTraits.Bold;
            }

            if (0 != (straits & (CTFontSymbolicTraits.Bold | CTFontSymbolicTraits.Italic)))
            {
                traits[CTFontTraitKey.Symbolic] = NSNumber.FromUInt32((UInt32)straits);
            }

            var attrs = new NSMutableDictionary();

            attrs[CTFontDescriptorAttributeKey.FamilyName] = (NSString)family;
            attrs[CTFontDescriptorAttributeKey.Traits]     = traits;

            var desc = new CTFontDescriptor(new CTFontDescriptorAttributes(attrs));
            var font = new CTFont(desc, size);

            return(font);
        }
Beispiel #2
0
 private CTFontSymbolicTraits getTraits(FontStyle style)
 {
     CTFontSymbolicTraits traits = CTFontSymbolicTraits.None;
     if (style.HasFlag(FontStyle.Bold))
     {
         traits |= CTFontSymbolicTraits.Bold;
     }
     if (style.HasFlag(FontStyle.Italic))
     {
         traits |= CTFontSymbolicTraits.Italic;
     }
     return traits;
 }
        private CTFont FindMatchingFont(CTFont reference, CTFontSymbolicTraits trait)
        {
            CTFontSymbolicTraits symbolic;
            CTFontTraits traits = reference.GetTraits ();
            if (traits.SymbolicTraits.HasValue)
                symbolic = traits.SymbolicTraits.Value | trait;
            else
                symbolic = trait;

            CTFont newFont = reference.WithSymbolicTraits (reference.Size, symbolic, symbolic);
            if (newFont != null)
                return newFont;
            else
                return reference;
        }
        public Font(string familyName, float emSize, FontStyle style,
                    GraphicsUnit unit, byte gdiCharSet, bool gdiVerticalFont)
        {
            if (emSize <= 0)
            {
                throw new ArgumentException("emSize is less than or equal to 0, evaluates to infinity, or is not a valid number.", "emSize");
            }


            // convert to 96 Dpi to be consistent with Windows
            var dpiSize = emSize * dpiScale;

            try {
                nativeFont = new CTFont(familyName, dpiSize);
            }
            catch
            {
                //nativeFont = new CTFont("Lucida Grande",emSize);
                nativeFont = new CTFont("Helvetica", dpiSize);
            }

            CTFontSymbolicTraits tMask = CTFontSymbolicTraits.None;

            if ((style & FontStyle.Bold) == FontStyle.Bold)
            {
                tMask |= CTFontSymbolicTraits.Bold;
            }
            if ((style & FontStyle.Italic) == FontStyle.Italic)
            {
                tMask |= CTFontSymbolicTraits.Italic;
            }
            strikeThrough = (style & FontStyle.Strikeout) == FontStyle.Strikeout;
            underLine     = (style & FontStyle.Underline) == FontStyle.Underline;

            var nativeFont2 = nativeFont.WithSymbolicTraits(dpiSize, tMask, tMask);

            if (nativeFont2 != null)
            {
                nativeFont = nativeFont2;
            }

            sizeInPoints = emSize;
            this.unit    = unit;

            // FIXME
            // I do not like the hard coded 72 but am just trying to boot strap the Font class right now
            size = ConversionHelpers.GraphicsUnitConversion(GraphicsUnit.Point, unit, 72.0f, sizeInPoints);
        }
Beispiel #5
0
        static CTFont CTFontWithFamily(FontFamily family, CTFontSymbolicTraits traits, float size)
        {
            // Semibold font hack
            if (FontFamily.RemoveSemiboldSuffix(family.Name, out string familyName))
            {
                if (CTFontWithFamilyName(familyName, traits, size, CTFontWeight.Semibold) is CTFont semibold)
                {
                    return(semibold);
                }
            }

            var font           = CTFontWithFamily(family, size);
            var mask           = (CTFontSymbolicTraits)uint.MaxValue;
            var fontWithTraits = font.WithSymbolicTraits(size, traits, mask);

            return(fontWithTraits ?? font);
        }
Beispiel #6
0
        private void CreateNativeFont(FontFamily familyName, float emSize, FontStyle style,
                                      GraphicsUnit unit, byte gdiCharSet, bool gdiVerticalFont)
        {
            // convert to 96 Dpi to be consistent with Windows
            var dpiSize = emSize * dpiScale;

            try
            {
                nativeFont = new CTFont(familyName.NativeDescriptor, dpiSize);
            }
            catch
            {
                nativeFont = new CTFont("Helvetica", dpiSize);
            }

            CTFontSymbolicTraits tMask = CTFontSymbolicTraits.None;

            if ((style & FontStyle.Bold) == FontStyle.Bold)
            {
                tMask |= CTFontSymbolicTraits.Bold;
            }
            if ((style & FontStyle.Italic) == FontStyle.Italic)
            {
                tMask |= CTFontSymbolicTraits.Italic;
            }
            strikeThrough = (style & FontStyle.Strikeout) == FontStyle.Strikeout;
            underLine     = (style & FontStyle.Underline) == FontStyle.Underline;

            var nativeFont2 = nativeFont.WithSymbolicTraits(dpiSize, tMask, tMask);

            if (nativeFont2 != null)
            {
                nativeFont = nativeFont2;
            }

            bold         = (nativeFont.SymbolicTraits & CTFontSymbolicTraits.Bold) == CTFontSymbolicTraits.Bold;
            italic       = (nativeFont.SymbolicTraits & CTFontSymbolicTraits.Italic) == CTFontSymbolicTraits.Italic;
            sizeInPoints = emSize;
            this.unit    = unit;

            // FIXME
            // I do not like the hard coded 72 but am just trying to boot strap the Font class right now
            size = ConversionHelpers.GraphicsUnitConversion(GraphicsUnit.Point, unit, 72.0f, sizeInPoints);
        }
        static internal CTFont CreateFont(string familyName, float emSize, FontStyle style,
                                          byte gdiCharSet, bool gdiVerticalFont)
        {
            if (emSize <= 0)
            {
                throw new ArgumentException("emSize is less than or equal to 0, evaluates to infinity, or is not a valid number.", "emSize");
            }

            CTFont nativeFont;

            // convert to 96 Dpi to be consistent with Windows
            var dpiSize = emSize * dpiScale;

            try {
                nativeFont = new CTFont(familyName, dpiSize);
            }
            catch
            {
                nativeFont = new CTFont("Helvetica", dpiSize);
            }

            CTFontSymbolicTraits tMask = CTFontSymbolicTraits.None;

            if ((style & FontStyle.Bold) == FontStyle.Bold)
            {
                tMask |= CTFontSymbolicTraits.Bold;
            }
            if ((style & FontStyle.Italic) == FontStyle.Italic)
            {
                tMask |= CTFontSymbolicTraits.Italic;
            }
            strikeThrough = (style & FontStyle.Strikeout) == FontStyle.Strikeout;
            underLine     = (style & FontStyle.Underline) == FontStyle.Underline;

            var nativeFont2 = nativeFont.WithSymbolicTraits(dpiSize, tMask, tMask);

            if (nativeFont2 != null)
            {
                nativeFont = nativeFont2;
            }

            return(nativeFont);
        }
        private int GetNativeMetric(Metric metric, FontStyle style)
        {
            // we are going to actually have to create a font object here
            // will not create an actual variable for this yet.  We may
            // want to do this in the future so that we do not have to keep
            // creating it over and over again.
            CTFontSymbolicTraits tMask = CTFontSymbolicTraits.None;

            if ((style & FontStyle.Bold) == FontStyle.Bold)
            {
                tMask |= CTFontSymbolicTraits.Bold;
            }
            if ((style & FontStyle.Italic) == FontStyle.Italic)
            {
                tMask |= CTFontSymbolicTraits.Italic;
            }

            var font = new CTFont(nativeFontDescriptor, 0);

            font = font.WithSymbolicTraits(0, tMask, tMask);

            switch (metric)
            {
            case Metric.EMHeight:
                return((int)font.UnitsPerEmMetric);

            case Metric.CellAscent:
                return((int)Math.Round(font.AscentMetric / font.Size * font.UnitsPerEmMetric));

            case Metric.CellDescent:
                return((int)Math.Round(font.DescentMetric / font.Size * font.UnitsPerEmMetric));

            case  Metric.LineSpacing:
                float lineHeight = 0;
                lineHeight += (float)font.AscentMetric;
                lineHeight += (float)font.DescentMetric;
                lineHeight += (float)font.LeadingMetric;
                return((int)Math.Round(lineHeight / font.Size * font.UnitsPerEmMetric));
            }

            return(0);
        }
Beispiel #9
0
 public static bool IsItalic(this CTFontSymbolicTraits traits)
 {
     return(0 != (traits & CTFontSymbolicTraits.Italic));
 }
Beispiel #10
0
 public static bool IsBold(this CTFontSymbolicTraits traits)
 {
     return(0 != (traits & CTFontSymbolicTraits.Bold));
 }
 public CTFont WithSymbolicTraits(float size, CTFontSymbolicTraits symTraitValue, CTFontSymbolicTraits symTraitMask, ref CGAffineTransform matrix)
 {
     return(CreateFont(
                CTFontCreateCopyWithSymbolicTraits(handle, size, ref matrix, symTraitValue, symTraitMask)));
 }
 static extern IntPtr CTFontCreateCopyWithSymbolicTraits(IntPtr font, float size, ref CGAffineTransform matrix, CTFontSymbolicTraits symTraitValue, CTFontSymbolicTraits symTraitMask);
 public CTFont WithSymbolicTraits(float size, CTFontSymbolicTraits symTraitValue, CTFontSymbolicTraits symTraitMask)
 {
     return(CreateFont(
                CTFontCreateCopyWithSymbolicTraits(handle, size, IntPtr.Zero, symTraitValue, symTraitMask)));
 }
 static extern IntPtr CTFontCreateCopyWithSymbolicTraits(IntPtr font, float size, IntPtr matrix, CTFontSymbolicTraits symTraitValue, CTFontSymbolicTraits symTraitMask);
		// Listing 2-6
		static CTFontDescriptor CreateFontDescriptorFromFamilyAndTraits (string familyName, CTFontSymbolicTraits traits, float size)
		{
			return new CTFontDescriptor (new CTFontDescriptorAttributes () {
				FamilyName  = familyName,
				Size        = size,
				Traits      = new CTFontTraits () {
					SymbolicTraits = traits,
				},
			});
		}
Beispiel #16
0
        static internal CTFont CreateFont(string familyName, float emSize, FontStyle style,
                                          byte gdiCharSet, bool gdiVerticalFont)
        {
            if (emSize <= 0)
            {
                throw new ArgumentException("emSize is less than or equal to 0, evaluates to infinity, or is not a valid number.", "emSize");
            }

            CTFont nativeFont;
            // convert to 96 Dpi to be consistent with Windows
            var dpiSize = emSize * dpiScale;

            var ext = System.IO.Path.GetExtension(familyName);

            if (!String.IsNullOrEmpty(ext) && ext.ToLower() == ".ttf")
            {
                var fontName        = familyName.Substring(0, familyName.Length - ext.Length);
                var path            = CCApplication.SharedApplication.Game.Content.RootDirectory + Path.DirectorySeparatorChar + fontName;
                var pathForResource = NSBundle.MainBundle.PathForResource(path, ext.Substring(1));

                try {
                    var dataProvider = new CGDataProvider(pathForResource);
                    var cgFont       = CGFont.CreateFromProvider(dataProvider);

                    try {
                        nativeFont = new CTFont(cgFont, dpiSize, null);
                    }
                    catch
                    {
                        nativeFont = new CTFont("Helvetica", dpiSize);
                    }
                }
                catch (Exception)
                {
                    try {
                        nativeFont = new CTFont(Path.GetFileNameWithoutExtension(familyName), dpiSize);
                    }
                    catch
                    {
                        nativeFont = new CTFont("Helvetica", dpiSize);
                    }
                    CCLog.Log(string.Format("Could not load font: {0} so will use default {1}.", familyName, nativeFont.DisplayName));
                }
            }
            else
            {
                try {
                    nativeFont = new CTFont(familyName, dpiSize);
                }
                catch
                {
                    nativeFont = new CTFont("Helvetica", dpiSize);
                }
            }


            CTFontSymbolicTraits tMask = CTFontSymbolicTraits.None;

            if ((style & FontStyle.Bold) == FontStyle.Bold)
            {
                tMask |= CTFontSymbolicTraits.Bold;
            }
            if ((style & FontStyle.Italic) == FontStyle.Italic)
            {
                tMask |= CTFontSymbolicTraits.Italic;
            }
            strikeThrough = (style & FontStyle.Strikeout) == FontStyle.Strikeout;
            underLine     = (style & FontStyle.Underline) == FontStyle.Underline;

            var nativeFont2 = nativeFont.WithSymbolicTraits(dpiSize, tMask, tMask);

            if (nativeFont2 != null)
            {
                nativeFont = nativeFont2;
            }

            return(nativeFont);
        }