示例#1
0
        public void VerticalAlignmentTests(
            VerticalAlignment vertical,
            HorizontalAlignment horizental,
            float top, float left)
        {
            string text = "hello world\nhello";
            Font   font = CreateFont(text);

            int scaleFactor = 72 * font.EmSize; // 72 * emSize means 1 point = 1px
            var span        = new RendererOptions(font, scaleFactor)
            {
                HorizontalAlignment = horizental,
                VerticalAlignment   = vertical
            };

            IReadOnlyList <GlyphLayout> glyphsToRender = new TextLayout().GenerateLayout(text.AsSpan(), span);
            IFontInstance fontInst   = span.Font.Instance;
            float         lineHeight = (fontInst.LineHeight * span.Font.Size) / (fontInst.EmSize * 72);

            lineHeight *= scaleFactor;
            FontRectangle bound = TextMeasurer.GetBounds(glyphsToRender, new Vector2(span.DpiX, span.DpiY));

            Assert.Equal(310, bound.Width, 3);
            Assert.Equal(40, bound.Height, 3);
            Assert.Equal(left, bound.Left, 3);
            Assert.Equal(top, bound.Top, 3);
        }
示例#2
0
        internal FontFamily Install(IFontInstance instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }

            if (instance.Description == null)
            {
                throw new ArgumentException("IFontInstance must have a Description.", nameof(instance));
            }

            lock (this.instances)
            {
                if (!this.instances.ContainsKey(instance.Description.FontFamily))
                {
                    this.instances.Add(instance.Description.FontFamily, new List <IFontInstance>(4));
                }

                if (!this.families.ContainsKey(instance.Description.FontFamily))
                {
                    this.families.Add(instance.Description.FontFamily, new FontFamily(instance.Description.FontFamily, this));
                }

                this.instances[instance.Description.FontFamily].Add(instance);
            }

            return(this.families[instance.Description.FontFamily]);
        }
示例#3
0
        /// <summary>
        /// Creates a <see cref="FontGlyphSource"/> instance.
        /// </summary>
        public FontGlyphSource(IFontInstance fontInstance, float size, string name, char firstChar = ' ', char lastChar = '~')
        {
            if (!float.IsFinite(size) || float.IsNegative(size))
            {
                throw new ArgumentOutOfRangeException(nameof(size), size, nameof(size) + " must be finite and positive.");
            }

            if (lastChar < firstChar)
            {
                throw new ArgumentException(nameof(LastChar) + " can't be lower than " + nameof(firstChar));
            }

            FontInstance = fontInstance ?? throw new ArgumentNullException(nameof(fontInstance));

            FirstChar = firstChar;
            LastChar  = lastChar;
            Size      = size;
            Name      = name;

            glyphPaths = CreatePaths(out pathColors, out glyphSizes, out renderOffsets);

            ShapeGraphicsOptions = new ShapeGraphicsOptions
            {
                ShapeOptions = { IntersectionRule = IntersectionRule.Nonzero },
            };
        }
示例#4
0
        /// <summary>
        /// Creates a <see cref="FontGlyphSource"/> instance.
        /// </summary>
        public FontGlyphSource(IFontInstance fontInstance)
        {
            FontInstance = fontInstance ?? throw new ArgumentNullException(nameof(fontInstance));

            DrawingOptions = new DrawingOptions
            {
                ShapeOptions = { IntersectionRule = IntersectionRule.Nonzero },
            };
        }
示例#5
0
        public void LoadFontWoff()
        {
            IFontInstance font = FontInstance.LoadFont(TestFonts.SimpleFontFileWoffData());

            Assert.Equal("SixLaborsSampleAB regular", font.Description.FontName);
            Assert.Equal("Regular", font.Description.FontSubFamilyName);

            GlyphInstance glyph = font.GetGlyph('a');
            var           r     = new GlyphRenderer();

            glyph.RenderTo(r, 12, System.Numerics.Vector2.Zero, new System.Numerics.Vector2(72), 0);
            // the test font only has characters .notdef, 'a' & 'b' defined
            Assert.Equal(6, r.ControlPoints.Distinct().Count());
        }
示例#6
0
        internal FontFamily Install(IFontInstance instance, CultureInfo culture)
        {
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }

            if (instance.Description == null)
            {
                throw new ArgumentException("IFontInstance must have a Description.", nameof(instance));
            }

            lock (this.instances)
            {
                this.instances.Add(instance);
            }

            return(new FontFamily(instance.Description.FontFamily(culture), this, culture));
        }
示例#7
0
        private IFontInstance LoadInstanceInternal()
        {
            IFontInstance instance = this.Family.Find(this.requestedStyle);

            if (instance == null && this.requestedStyle.HasFlag(FontStyle.Italic))
            {
                // can find style requested and they want one thats atleast partial itallic try the regual italic
                instance = this.Family.Find(FontStyle.Italic);
            }

            if (instance == null && this.requestedStyle.HasFlag(FontStyle.Bold))
            {
                // can find style requested and they want one thats atleast partial bold try the regular bold
                instance = this.Family.Find(FontStyle.Bold);
            }

            if (instance == null)
            {
                // cant find style requested and lets just try returning teh default
                instance = this.Family.Find(this.Family.DefaultStyle);
            }

            return(instance);
        }
示例#8
0
        internal FontFamily Install(IFontInstance instance)
        {
            if (instance != null && instance.Description != null)
            {
                lock (this.instances)
                {
                    if (!this.instances.ContainsKey(instance.Description.FontFamily))
                    {
                        this.instances.Add(instance.Description.FontFamily, new List <IFontInstance>(4));
                    }

                    if (!this.families.ContainsKey(instance.Description.FontFamily))
                    {
                        this.families.Add(instance.Description.FontFamily, new FontFamily(instance.Description.FontFamily, this));
                    }

                    this.instances[instance.Description.FontFamily].Add(instance);
                }

                return(this.families[instance.Description.FontFamily]);
            }

            return(null);
        }
示例#9
0
 /// <summary>
 /// Creates a <see cref="FontGlyphSource"/> instance.
 /// </summary>
 public FontGlyphSource(IFontInstance fontInstance, float size, char firstChar = ' ', char lastChar = '~')
     : this(fontInstance, size, fontInstance.Description.FontNameInvariantCulture, firstChar, lastChar)
 {
 }
示例#10
0
 /// <summary>
 /// Creates a <see cref="TrippyFontFile"/> holding information for a single font.
 /// </summary>
 public static TrippyFontFile CreateFontFile(IFontInstance font, float size, char firstChar = ' ', char lastChar = '~', Color?backgroundColor = null)
 {
     return(FontBuilder.CreateFontFile(new FontGlyphSource(font, size, firstChar, lastChar), backgroundColor));
 }