Exemple #1
0
        public void ShouldThrowInvalidOperationExceptionOnAddUtfWhenBufferIsShaped()
        {
            using (var typeface = SKTypeface.FromFile(Path.Combine(PathToFonts, "content-font.ttf")))
                using (var blob = typeface.OpenStream(out var index).ToHarfBuzzBlob())
                    using (var face = new Face(blob, index))
                        using (var font = new Font(face))
                            using (var buffer = new Buffer())
                            {
                                buffer.AddUtf8(SimpleText);

                                font.Shape(buffer);

                                Assert.Throws <InvalidOperationException>(() => { buffer.AddUtf8("A"); });
                            }
        }
Exemple #2
0
        public void CanCreateFaceShaperFromTypeface()
        {
            var skiaTypeface = SKTypeface.FromFile(Path.Combine(PathToFonts, "content-font.ttf"));

            var clusters   = new uint[] { 4, 2, 0 };
            var codepoints = new uint[] { 629, 668, 891 };

            using (var face = new Face(GetFaceBlob, () => skiaTypeface.Dispose()))
                using (var font = new Font(face))
                    using (var buffer = new HarfBuzzSharp.Buffer())
                    {
                        buffer.AddUtf8("متن");
                        buffer.GuessSegmentProperties();

                        font.Shape(buffer);

                        Assert.Equal(clusters, buffer.GlyphInfos.Select(i => i.Cluster));
                        Assert.Equal(codepoints, buffer.GlyphInfos.Select(i => i.Codepoint));
                    }

            Blob GetFaceBlob(Face face, Tag tag)
            {
                var size = skiaTypeface.GetTableSize(tag);
                var data = Marshal.AllocCoTaskMem(size);

                skiaTypeface.TryGetTableData(tag, 0, size, data);
                return(new Blob(data, size, MemoryMode.Writeable, () => Marshal.FreeCoTaskMem(data)));
            }
        }
Exemple #3
0
        public new Result Shape(string text, float xOffset, float yOffset, SKPaint paint)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(new Result());
            }

            using var buffer = new Buffer();
            switch (paint.TextEncoding)
            {
            case SKTextEncoding.Utf8:
                buffer.AddUtf8(text);
                break;

            case SKTextEncoding.Utf16:
                buffer.AddUtf16(text);
                break;

            case SKTextEncoding.Utf32:
                buffer.AddUtf32(text);
                break;

            default:
                throw new NotSupportedException("TextEncoding of type GlyphId is not supported.");
            }

            buffer.GuessSegmentProperties();
            return(Shape(buffer, xOffset, yOffset, paint));
        }
Exemple #4
0
        public void ShouldAddUtfByString()
        {
            using (var buffer = new Buffer())
            {
                buffer.AddUtf8("A");

                Assert.Equal(1, buffer.Length);

                buffer.AddUtf8("B");

                Assert.Equal(2, buffer.Length);

                buffer.AddUtf8("C");

                Assert.Equal(3, buffer.Length);
            }
        }
Exemple #5
0
        public void ShouldThrowInvalidOperationExceptionOnDeserializeGlyphsWhenBufferIsNonEmpty()
        {
            using (var buffer = new Buffer())
            {
                buffer.AddUtf8("A");

                Assert.Throws <InvalidOperationException>(() => { buffer.DeserializeGlyphs(SerializedSimpleText); });
            }
        }
Exemple #6
0
        public void ShouldThrowInvalidOperationExceptionOnSerializeGlyphsWhenBufferIsUnShaped()
        {
            using (var buffer = new Buffer())
            {
                buffer.AddUtf8("A");

                Assert.Throws <InvalidOperationException>(() => { buffer.SerializeGlyphs(); });
            }
        }
Exemple #7
0
        public void ShouldAppendBuffer()
        {
            using (var buffer = new Buffer())
                using (var source = new Buffer())
                {
                    source.AddUtf8("123");

                    buffer.Append(source, 0, source.Length);
                }
        }
Exemple #8
0
        public void ShouldClearContents()
        {
            using (var buffer = new Buffer())
            {
                buffer.AddUtf8(SimpleText);

                Assert.Equal(SimpleText.Length, buffer.GlyphInfos.Length);

                buffer.ClearContents();

                Assert.Empty(buffer.GlyphInfos);
            }
        }
Exemple #9
0
        public void ShouldHaveDefaultStateAfterReset()
        {
            using (var buffer = new Buffer())
            {
                buffer.AddUtf8(SimpleText);

                buffer.Reset();

                Assert.Equal(ContentType.Invalid, buffer.ContentType);

                Assert.Equal(0, buffer.Length);
            }
        }
Exemple #10
0
        public void ShouldReverseClusters()
        {
            using (var buffer = new Buffer())
            {
                buffer.AddUtf8("12");

                buffer.ReverseClusters();

                Assert.Equal(50u, buffer.GlyphInfos[0].Codepoint);
                Assert.Equal(1u, buffer.GlyphInfos[0].Cluster);

                Assert.Equal(49u, buffer.GlyphInfos[1].Codepoint);
                Assert.Equal(0u, buffer.GlyphInfos[1].Cluster);
            }
        }
Exemple #11
0
        public void ShouldHaveCorrectContentType()
        {
            using (var typeface = SKTypeface.FromFile(Path.Combine(PathToFonts, "content-font.ttf")))
                using (var blob = typeface.OpenStream(out var index).ToHarfBuzzBlob())
                    using (var face = new Face(blob, index))
                        using (var font = new Font(face))
                            using (var buffer = new Buffer())
                            {
                                Assert.Equal(ContentType.Invalid, buffer.ContentType);

                                buffer.AddUtf8(SimpleText);

                                Assert.Equal(ContentType.Unicode, buffer.ContentType);

                                font.Shape(buffer);

                                Assert.Equal(ContentType.Glyphs, buffer.ContentType);
                            }
        }
Exemple #12
0
        public void ShouldAddUtfBySpan()
        {
            using (var buffer = new Buffer())
            {
                var utf8 = Encoding.UTF8.GetBytes("A").AsSpan();

                buffer.AddUtf8(utf8);

                Assert.Equal(1, buffer.Length);

                var utf16 = "B".AsSpan();

                buffer.AddUtf16(utf16);

                Assert.Equal(2, buffer.Length);

                var utf32 = new[] { char.ConvertToUtf32("C", 0) };

                buffer.AddUtf32(utf32);

                Assert.Equal(3, buffer.Length);
            }
        }