Beispiel #1
0
        public void XyToIndexShouldWorkCorrectly()
        {
            ITextContext context = new TypographyTextContext("0123456", FontFile, 36,
                                                             FontStretch.Normal, FontStyle.Normal, FontWeight.Normal,
                                                             1000, 100,
                                                             TextAlignment.Leading);

            // prepare debug contexts
            {
                surface = new Cairo.ImageSurface(Cairo.Format.Argb32, 2000, 2000);
                g       = new Cairo.Context(surface);
                g.SetSourceRGBA(1, 1, 1, 1);
                g.Paint();
                g.SetSourceRGBA(0, 0, 0, 1);
                g.LineWidth = 1;
            }

            // build path
            CairoPathBuilder cairoPathBuilder;

            cairoPathBuilder = new CairoPathBuilder(g, 0, 0, 1);
            context.Build(Point.Zero, cairoPathBuilder);

            bool isInside  = false;
            uint charIndex = context.XyToIndex(-1, 0, out isInside);

            Assert.False(isInside);
            Assert.Equal(0u, charIndex);

            charIndex = context.XyToIndex(13, 0, out isInside);
            Assert.True(isInside);
            Assert.Equal(0u, charIndex);

            charIndex = context.XyToIndex(37, 0, out isInside);
            Assert.True(isInside);
            Assert.Equal(1u, charIndex);

            charIndex = context.XyToIndex(64, 0, out isInside);
            Assert.True(isInside);
            Assert.Equal(2u, charIndex);

            charIndex = context.XyToIndex(89, 0, out isInside);
            Assert.True(isInside);
            Assert.Equal(3u, charIndex);
        }
Beispiel #2
0
        public void ShouldGetCorrectOutline()
        {
            ITextContext textContext = new TypographyTextContext(
                "0123456", FontFile, 36,
                FontStretch.Normal, FontStyle.Normal, FontWeight.Normal,
                1000, 100,
                TextAlignment.Leading);

            // prepare debug contexts
            {
                surface = new Cairo.ImageSurface(Cairo.Format.Argb32, 2000, 2000);
                g       = new Cairo.Context(surface);
                g.SetSourceRGBA(1, 1, 1, 1);
                g.Paint();
                g.SetSourceRGBA(0, 0, 0, 1);
                g.LineWidth = 1;
            }

            // build path
            CairoPathBuilder cairoPathBuilder;
            {
                cairoPathBuilder = new CairoPathBuilder(g, 0, 0, 1);
                textContext.Build(Point.Zero, cairoPathBuilder);
            }

            // show debug results
            // image
            {
                surface.WriteToPng(PathImagePath);
                g.Dispose();

                Assert.False(PathImagePath.Contains(" "));
                // open the image in Windows Photo Viewer
                Process.Start(@"C:\WINDOWS\System32\rundll32.exe", @"""C:\Program Files\Windows Photo Viewer\PhotoViewer.dll"", ImageView_Fullscreen " + PathImagePath);
            }
            //text
            {
                var text = cairoPathBuilder.Result;
                File.WriteAllText(PathTextPath, text);
                Process.Start(@"notepad", PathTextPath);
            }

            // Now, check if the output text and image shows a right outline of the text.
        }
Beispiel #3
0
        public void Setup(int canvasW, int canvasH)
        {
            //--------------------------------------
            //TODO: review here again

            DrawingGL.Text.Utility.SetLoadFontDel(
                fontfile =>
            {
                return(null);
            });

            //--------------------------------------
            simpleCanvas = new SimpleCanvas(canvasW, canvasH);
            var text = "Typography";

            //optional ....
            //var directory = AndroidOS.Environment.ExternalStorageDirectory;
            //var fullFileName = Path.Combine(directory.ToString(), "TypographyTest.txt");
            //if (File.Exists(fullFileName))
            //{
            //    text = File.ReadAllText(fullFileName);
            //}
            //--------------------------------------------------------------------------
            //we want to create a prepared visual object ***
            textContext = new TypographyTextContext()
            {
                FontFamily  = "DroidSans.ttf", //corresponding to font file Assets/DroidSans.ttf
                FontSize    = 64,              //size in Points
                FontStretch = FontStretch.Normal,
                FontStyle   = FontStyle.Normal,
                FontWeight  = FontWeight.Normal,
                Alignment   = DrawingGL.Text.TextAlignment.Leading
            };
            //--------------------------------------------------------------------------
            //create blank text run
            textRun = new TextRun();
            //generate glyph run inside text text run
            textContext.GenerateGlyphRuns(textRun, text);
            //--------------------------------------------------------------------------
        }
Beispiel #4
0
        public void ShouldGetARightMeshFromTypography()
        {
            var style = GUIStyle.Default;

            style.Set <double>(GUIStyleName.FontSize, 8);

            var state         = GUIState.Normal;
            var fontFamily    = style.Get <string>(GUIStyleName.FontFamily, state);
            var fontSize      = style.Get <double>(GUIStyleName.FontSize, state);
            var fontStretch   = (FontStretch)style.Get <int>(GUIStyleName.FontStretch, state);
            var fontStyle     = (FontStyle)style.Get <int>(GUIStyleName.FontStyle, state);
            var fontWeight    = (FontWeight)style.Get <int>(GUIStyleName.FontWeight, state);
            var textAlignment = (TextAlignment)style.Get <int>(GUIStyleName.TextAlignment, state);

            var rect        = new Rect(0, 0, 200, 200);
            var textContext = new TypographyTextContext(
                "8",
                fontFamily, (int)fontSize, fontStretch, fontStyle, fontWeight,
                (int)Math.Ceiling(rect.Size.Width), (int)Math.Ceiling(rect.Size.Height),
                textAlignment);

            var textMesh = new TextMesh();

            textMesh.Build(new Point(0, fontSize), style, textContext);

            //PathUtil.SaveToPng(paths, @"D:\TypographyTextPath.png");

            DrawList drawList = new DrawList();

            drawList.Append(textMesh, Vector.Zero);

            var objFilePath = "D:\\TextRenderingTest_ShouldGetARightMeshFromTypography.obj";

            Utility.SaveToObjFile(objFilePath, drawList.DrawBuffer.VertexBuffer, drawList.DrawBuffer.IndexBuffer);
            Process.Start(ModelViewerPath, objFilePath);
        }
Beispiel #5
0
        public void ShouldGetARightMeshAfterAppendingATextMesh()
        {
            var style = GUIStyle.Default;

            style.Set <double>(GUIStyleName.FontSize, 36);

            var state         = GUIState.Normal;
            var fontFamily    = style.Get <string>(GUIStyleName.FontFamily, state);
            var fontSize      = style.Get <double>(GUIStyleName.FontSize, state);
            var fontStretch   = (FontStretch)style.Get <int>(GUIStyleName.FontStretch, state);
            var fontStyle     = (FontStyle)style.Get <int>(GUIStyleName.FontStyle, state);
            var fontWeight    = (FontWeight)style.Get <int>(GUIStyleName.FontWeight, state);
            var textAlignment = (TextAlignment)style.Get <int>(GUIStyleName.TextAlignment, state);

            var rect        = new Rect(0, 0, 200, 200);
            var textContext = new TypographyTextContext(
                "A",
                fontFamily, (int)fontSize, fontStretch, fontStyle, fontWeight,
                (int)Math.Ceiling(rect.Size.Width), (int)Math.Ceiling(rect.Size.Height),
                textAlignment);

            var textMesh = new TextMesh();

            textMesh.Build(Point.Zero, style, textContext);

            var anotherTextContext = new TypographyTextContext(
                "auto-sized",
                fontFamily, (int)fontSize, fontStretch, fontStyle, fontWeight,
                (int)Math.Ceiling(rect.Size.Width), (int)Math.Ceiling(rect.Size.Height),
                textAlignment);

            var anotherTextMesh = new TextMesh();

            anotherTextMesh.Build(new Point(50, 100), style, anotherTextContext);

            DrawList drawList            = new DrawList();
            var      expectedVertexCount = 0;
            var      expectedIndexCount  = 0;

            drawList.AddRectFilled(Point.Zero, new Point(200, 100), Color.Metal);
            expectedVertexCount += 4;
            expectedIndexCount  += 6;
            Assert.Equal(drawList.DrawBuffer.VertexBuffer.Count, expectedVertexCount);
            Assert.Equal(drawList.DrawBuffer.IndexBuffer.Count, expectedIndexCount);

            drawList.Append(textMesh, Vector.Zero);
            expectedVertexCount += textMesh.VertexBuffer.Count;
            expectedIndexCount  += textMesh.IndexBuffer.Count;
            Assert.Equal(drawList.DrawBuffer.VertexBuffer.Count, expectedVertexCount);
            Assert.Equal(drawList.DrawBuffer.IndexBuffer.Count, expectedIndexCount);

            drawList.AddRectFilled(new Point(0, 110), new Point(200, 150), Color.Metal);
            expectedVertexCount += 4;
            expectedIndexCount  += 6;
            Assert.Equal(drawList.DrawBuffer.VertexBuffer.Count, expectedVertexCount);
            Assert.Equal(drawList.DrawBuffer.IndexBuffer.Count, expectedIndexCount);

            drawList.AddRectFilled(new Point(0, 160), new Point(200, 200), Color.Metal);
            expectedVertexCount += 4;
            expectedIndexCount  += 6;
            Assert.Equal(drawList.DrawBuffer.VertexBuffer.Count, expectedVertexCount);
            Assert.Equal(drawList.DrawBuffer.IndexBuffer.Count, expectedIndexCount);

            drawList.Append(anotherTextMesh, Vector.Zero);
            expectedVertexCount += anotherTextMesh.VertexBuffer.Count;
            expectedIndexCount  += anotherTextMesh.IndexBuffer.Count;
            Assert.Equal(drawList.DrawBuffer.VertexBuffer.Count, expectedVertexCount);
            Assert.Equal(drawList.DrawBuffer.IndexBuffer.Count, expectedIndexCount);

            var objFilePath = "D:\\Typography_ShouldGetARightMeshAfterAppendingATextMesh.obj";

            Utility.SaveToObjFile(objFilePath, drawList.DrawBuffer.VertexBuffer, drawList.DrawBuffer.IndexBuffer);
            Process.Start(ModelViewerPath, objFilePath);
        }