Example #1
0
        public void Reset(Canvas target, Device2D device2D)
        {
            ShapedText shapedOutput = new ShapedText();

            const string plainText = "Hello world!";

            device2D.TextShaper.Begin(shapedOutput, plainText);

            device2D.TextShaper.AnalyzeScripts();

            device2D.TextShaper.SetPointSize(plainText, 95.0f);

            device2D.TextShaper.End();

            List<GlyphOutline> outlines = new List<GlyphOutline>();

            foreach(TextShaper.Span span in shapedOutput.Spans)
            {
                foreach(int clusterIndex in span.Clusters)
                {
                    TextShaper.Cluster cluster = shapedOutput.Clusters[clusterIndex];

                    outlines.Add(
                        device2D.Resources.GetGlyphOutline(
                            cluster.Glyphs,
                            false,
                            false,
                            span.FontMetrics.FontId,
                            shapedOutput.Glyphs));
                }
            }

            device2D.Painter.Begin(target);

            device2D.Painter.SetBrush(Resources.Foreground);

            foreach(TextShaper.Span span in shapedOutput.Spans)
            {
                float emSize = span.FontMetrics.MeasureEm(span.PointSize);

                foreach(int clusterIndex in span.Clusters)
                {
                    float advance = shapedOutput.Clusters[clusterIndex].Advance;

                    if (_AreRegionsDisplayed)
                    {
                        device2D.Painter.SaveState();

                        device2D.Painter.StrokeWidth = 0.5f;
                        device2D.Painter.LineStyle = LineStyle.Dash;

                        device2D.Painter.SetBrush(Color.Red);
                        device2D.Painter.StrokeRectangle(0, 0, advance, emSize);
                        device2D.Painter.SetBrush(Resources.Foreground);

                        device2D.Painter.RestoreState();
                    }

                    float baseline = outlines[clusterIndex].Baseline;

                    device2D.Painter.SaveState();

                    device2D.Painter.Scale(emSize, emSize);
                    device2D.Painter.Translate(0, baseline);

                    if(outlines[clusterIndex].Shape != null)
                    {
                        device2D.Painter.Fill(outlines[clusterIndex].Shape);
                    }

                    device2D.Painter.RestoreState();

                    device2D.Painter.Translate(advance, 0);
                }
            }

            device2D.Painter.End();
        }
Example #2
0
        private static void DrawText(Point location, string text, Device2D device2D)
        {
            ShapedText shapedOutput = new ShapedText();

            device2D.TextShaper.Begin(shapedOutput, text);
            device2D.TextShaper.AnalyzeScripts();
            device2D.TextShaper.SetFamily(text, "Lucida Console");
            device2D.TextShaper.SetPointSize(text, 12.0f);
            device2D.TextShaper.End();

            List<GlyphOutline> outlines = new List<GlyphOutline>();

            foreach(TextShaper.Span span in shapedOutput.Spans)
            {
                foreach(int clusterIndex in span.Clusters)
                {
                    TextShaper.Cluster cluster = shapedOutput.Clusters[clusterIndex];

                    outlines.Add(
                        device2D.Resources.GetGlyphOutline(
                            cluster.Glyphs,
                            false,
                            false,
                            span.FontMetrics.FontId,
                            shapedOutput.Glyphs));
                }
            }

            device2D.Painter.SaveState();
            device2D.Painter.Translate(location);

            foreach(TextShaper.Span span in shapedOutput.Spans)
            {
                float emSize = span.FontMetrics.MeasureEm(span.PointSize);

                foreach(int clusterIndex in span.Clusters)
                {
                    device2D.Painter.SaveState();

                    device2D.Painter.Scale(emSize, emSize);
                    device2D.Painter.Translate(0, outlines[clusterIndex].Baseline);

                    if(outlines[clusterIndex].Shape != null)
                    {
                        device2D.Painter.Fill(outlines[clusterIndex].Shape);
                    }

                    device2D.Painter.RestoreState();

                    device2D.Painter.Translate(
                        shapedOutput.Clusters[clusterIndex].Advance, 0);
                }
            }

            device2D.Painter.RestoreState();
        }
Example #3
0
        private static Rectangle MeasureText(string text, Device2D device2D)
        {
            ShapedText shapedOutput = new ShapedText();

            device2D.TextShaper.Begin(shapedOutput, text);
            device2D.TextShaper.AnalyzeScripts();
            device2D.TextShaper.SetFamily(text, "Lucida Console");
            device2D.TextShaper.SetPointSize(text, 12.0f);
            device2D.TextShaper.End();

            float maxWidth = 0.0f;
            float maxHeight = 0.0f;

            foreach(TextShaper.Span span in shapedOutput.Spans)
            {
                maxHeight = Math.Max(
                    maxHeight,
                    span.FontMetrics.MeasureEm(span.PointSize));

                foreach(int clusterIndex in span.Clusters)
                {
                    maxWidth += shapedOutput.Clusters[clusterIndex].Advance;
                }
            }

            return new Rectangle(Point.Empty, maxWidth, maxHeight);
        }
Example #4
0
        public void Reset(Canvas target, Device2D device2D)
        {
            if(device2D.Resources.FindEffect<DistanceEffectSettings>() == null)
            {
                device2D.Resources.RegisterEffect<DistanceFieldEffect>();
            }

            ShapedText output = new ShapedText();

            const string text = "M";

            device2D.TextShaper.Begin(output, text);
            device2D.TextShaper.AnalyzeScripts();
            device2D.TextShaper.SetFamily(text, "Arno Pro");
            device2D.TextShaper.SetPointSize(text, 12.0f);
            device2D.TextShaper.SetFeatures(text, new FontFeatureCollection(new[] {new FontFeature("swsh")}));
            device2D.TextShaper.End();

            GlyphOutline outline =
                device2D.Resources.GetGlyphOutline(
                    new IndexedRange(0, 1),
                    false,
                    false,
                    output.Spans[0].FontMetrics.FontId,
                    output.Glyphs);

            GC.Collect(4);

            Canvas test2 = new Canvas(new Size(128, 128), SurfaceUsage.Normal);

            Stopwatch watch = new Stopwatch();
            watch.Start();
            Canvas test = _Distance.CreateField(
                outline.Shape, outline.Baseline, device2D);
            watch.Stop();

            //mForm.Text = string.Format("Time: {0}", watch.ElapsedMilliseconds);

            Debug.WriteLine("Time: {0}", watch.ElapsedMilliseconds);

            Rectangle reg = device2D.Geometry.MeasureRegion(outline.Shape);

            device2D.Painter.Begin(target);
            device2D.Painter.Translate(test.Region.X, test.Region.Y);

            device2D.Painter.Translate((0.5f - (reg.Width / 2.0f)) * 400, (0.5f - (reg.Height / 2.0f)) * 400);

            // translate the glyph to the left corner of the EM square
            device2D.Painter.Translate(-reg.X * 400, -reg.Y * 400);
            device2D.Painter.SetBrush(Color.Black);
            device2D.Painter.Scale(400, 400);
            device2D.Painter.Fill(outline.Shape);
            device2D.Painter.End(); //*/

            device2D.Compositor.Begin(target, Retention.RetainData);

            DistanceEffectSettings settings;
            device2D.Compositor.Translate(400, 0);
            device2D.Compositor.Scale(3.125f, 3.125f);
            device2D.Compositor.ApplyEffect(settings);
            device2D.Compositor.Composite(test);
            device2D.Compositor.End();

            device2D.Painter.Begin(target, Retention.RetainData);
            device2D.Painter.SetBrush(Color.IndianRed);
            device2D.Painter.IsAntialiased = Antialiasing.Aliased;
            device2D.Painter.StrokeWidth = DistanceField.EmLength / 400;
            device2D.Painter.Scale(1.0f / DistanceField.EmLength, 1.0f / DistanceField.EmLength);
            device2D.Painter.Scale(400, 400);
            TestTest(_Distance.Sample, device2D);
            device2D.Painter.End();

            //device2D.Resources.DumpToFiles(null, SurfaceUsage.Normal); //*/
        }