コード例 #1
0
ファイル: FontMetrics.xaml.cs プロジェクト: ben-sheeran/Win2D
        void EnsureResources(ICanvasResourceCreatorWithDpi resourceCreator, Size targetSize)
        {
            if (resourceRealizationSize == targetSize && !needsResourceRecreation)
                return;

            float canvasWidth = (float)targetSize.Width;
            float canvasHeight = (float)targetSize.Height;
            sizeDim = Math.Min(canvasWidth, canvasHeight);

            if (!defaultFontSizeSet)
            {
                CurrentFontSize = sizeDim / 1.3f;
                CurrentFontSize = Math.Max((float)fontSizeSlider.Minimum, CurrentFontSize);
                CurrentFontSize = Math.Min((float)fontSizeSlider.Maximum, CurrentFontSize);
                fontSizeSlider.Value = CurrentFontSize;
                defaultFontSizeSet = true;
            }

            if (textLayout != null) 
                textLayout.Dispose();
            textLayout = CreateTextLayout(resourceCreator, canvasWidth, canvasHeight);

            if (!UniformStyle)
            {
                textLayout.SetFontSize(0, 1, CurrentUppercaseFontSize);
                textLayout.SetFontFamily(0, 1, uppercaseFontPicker.CurrentFontFamily);

                textLayout.SetFontSize(1, 2, CurrentLowercaseFontSize);
                textLayout.SetFontFamily(1, 2, lowercaseFontPicker.CurrentFontFamily);
            }

            needsResourceRecreation = false;
            resourceRealizationSize = targetSize;
        }
コード例 #2
0
        void EnsureResources(ICanvasResourceCreatorWithDpi resourceCreator, Size targetSize)
        {
            if (!needsResourceRecreation)
            {
                return;
            }

            if (textLayout != null)
            {
                textLayout.Dispose();
                textGeometry.Dispose();
            }

            textLayout = CreateTextLayout(resourceCreator, (float)targetSize.Width, (float)targetSize.Height);

            if (CurrentTextOutlineGranularityOption == TextOutlineGranularity.Layout)
            {
                textGeometry = CanvasGeometry.CreateText(textLayout);
            }
            else
            {
                GlyphRunsToGeometryConverter converter = new GlyphRunsToGeometryConverter(resourceCreator);

                textLayout.DrawToTextRenderer(converter, 0, 0);

                textGeometry = converter.GetGeometry();
            }

            needsResourceRecreation = false;
        }
コード例 #3
0
ファイル: DpiExample.xaml.cs プロジェクト: fikovacic/Win2D
        static CanvasBitmap CreateTestBitmap(ICanvasResourceCreatorWithDpi resourceCreator, float dpi)
        {
            int pixelSize = (int)Math.Round(testSize * dpi / defaultDpi);

            // Initialize to solid gray.
            Color[] colors = Enumerable.Repeat(Colors.Gray, pixelSize * pixelSize).ToArray();

            // Diagonal lines.
            for (int i = 0; i < pixelSize; i++)
            {
                colors[i * pixelSize + i] = Colors.Red;
                colors[i * pixelSize + (pixelSize - i - 1)] = Colors.Red;
            }

            // Single pixel border.
            for (int i = 0; i < pixelSize; i++)
            {
                colors[i]             = Colors.Blue;
                colors[i * pixelSize] = Colors.Blue;
                colors[i * pixelSize + pixelSize - 1]   = Colors.Blue;
                colors[pixelSize * (pixelSize - 1) + i] = Colors.Blue;
            }

            if (dpi == defaultDpi)
            {
                // We could always just use the "else" path, but want to test this default overload as well.
                return(CanvasBitmap.CreateFromColors(resourceCreator, colors, pixelSize, pixelSize));
            }
            else
            {
                return(CanvasBitmap.CreateFromColors(resourceCreator, colors, pixelSize, pixelSize, dpi));
            }
        }
コード例 #4
0
        void EnsureResources(ICanvasResourceCreatorWithDpi resourceCreator, Size targetSize)
        {
            if (resourceRealizationSize != targetSize && !needsResourceRecreation)
            {
                return;
            }

            float canvasWidth  = (float)targetSize.Width;
            float canvasHeight = (float)targetSize.Height;

            if (textLayout != null)
            {
                textLayout.Dispose();
            }
            textLayout = CreateTextLayout(resourceCreator, canvasWidth, canvasHeight);

            Rect layoutBounds = textLayout.LayoutBounds;

            textBrush            = new CanvasLinearGradientBrush(resourceCreator, Colors.Red, Colors.Green);
            textBrush.StartPoint = new System.Numerics.Vector2((float)(layoutBounds.Left + layoutBounds.Right) / 2, (float)layoutBounds.Top);
            textBrush.EndPoint   = new System.Numerics.Vector2((float)(layoutBounds.Left + layoutBounds.Right) / 2, (float)layoutBounds.Bottom);

            selectionTextBrush            = new CanvasLinearGradientBrush(resourceCreator, Colors.Green, Colors.Red);
            selectionTextBrush.StartPoint = textBrush.StartPoint;
            selectionTextBrush.EndPoint   = textBrush.EndPoint;

            needsResourceRecreation = false;
            resourceRealizationSize = targetSize;
        }
コード例 #5
0
        /// <summary>
        /// Renders a texture that contains surface normals
        /// in the R, G and B channels and height information in
        /// the A channel based on the specified height map.
        /// </summary>
        /// <param name="resourceCreator">Resource creator</param>
        /// <param name="heightMap">Height map</param>
        /// <returns>A combination of a normal map and the height map</returns>
        private static async Task <CanvasBitmap> RenderNormalHeightMapAsync(ICanvasResourceCreatorWithDpi resourceCreator, CanvasBitmap heightMap, float heightDifference)
        {
            var bytes = await Utilities.ReadBytesFromUriAsync(new Uri("ms-appx:///Shaders/NormalMapFromHeightMapShader.bin"));

            var heightMapConverterEffect = new PixelShaderEffect(bytes)
            {
                Source1          = heightMap,
                Source1Mapping   = SamplerCoordinateMapping.Offset,
                MaxSamplerOffset = 1,
            };

            heightMapConverterEffect.Properties["dpi"]    = resourceCreator.Dpi;
            heightMapConverterEffect.Properties["height"] = heightDifference;

            var normalHeightMap = new CanvasRenderTarget(
                resourceCreator,
                (float)heightMap.Size.Width,
                (float)heightMap.Size.Height,
                heightMap.Dpi);

            using (var g = normalHeightMap.CreateDrawingSession())
            {
                g.DrawImage(heightMapConverterEffect);
            }

            return(normalHeightMap);
        }
コード例 #6
0
ファイル: DpiExample.xaml.cs プロジェクト: N500/Win2D
        static async Task <CanvasVirtualBitmap> CreateTestVirtualBitmap(ICanvasResourceCreatorWithDpi resourceCreator)
        {
            var canvasBitmap = CreateTestBitmap(resourceCreator, 96);

            var stream = new InMemoryRandomAccessStream();
            await canvasBitmap.SaveAsync(stream, CanvasBitmapFileFormat.Png);

            return(await CanvasVirtualBitmap.LoadAsync(resourceCreator, stream));
        }
コード例 #7
0
        private static Color GetAverageColor(ICanvasResourceCreatorWithDpi resourceCreator, CanvasBitmap image)
        {
            var miniImage = new CanvasRenderTarget(resourceCreator, 1, 1, 96);

            using (var g = miniImage.CreateDrawingSession())
                g.DrawImage(image, new Rect(0, 0, 1, 1));

            return(miniImage.GetPixelColors(0, 0, 1, 1)[0]);
        }
コード例 #8
0
        public LayerCanvas(ICanvasResourceCreatorWithDpi resourceCreator, Size size, int count)
        {
            Canvas = new CanvasRenderTarget[count];
            for (int i = 0; i < Canvas.Length; i++)
            {
                Canvas[i] = new CanvasRenderTarget(resourceCreator, size);
                using (var ds = Canvas[i].CreateDrawingSession())
                {
                    ds.Clear(Colors.Transparent);
                }
            }

            Foreground = new CanvasRenderTarget(resourceCreator, size);
        }
コード例 #9
0
        public bool PointerPressed(ICanvasResourceCreatorWithDpi creator, VirtualKeyModifiers keyModifiers, PointerPoint pointerPoint)
        {
            /*var position = pointerPoint.Position;
             *
             * seeker.MoveTo(position.ToVector2());*/

            /*var position = pointerPoint.Position;
             * var tower = new LaserTower(enemyProvider, position.ToVector2(), Colors.Yellow, 0.7d, 150.0d);
             *
             * tower.CreateResources(creator, CanvasCreateResourcesReason.FirstTime);
             * scene.Children.Add(tower);*/

            return(true);
        }
コード例 #10
0
        static async Task <CanvasVirtualBitmap> CreateTestVirtualBitmap(ICanvasResourceCreatorWithDpi resourceCreator)
        {
#if WINDOWS_UWP
            var canvasBitmap = CreateTestBitmap(resourceCreator, 96);

            var stream = new InMemoryRandomAccessStream();
            await canvasBitmap.SaveAsync(stream, CanvasBitmapFileFormat.Png);

            return(await CanvasVirtualBitmap.LoadAsync(resourceCreator, stream));
#else
            await Task.Yield(); // prevent warning about no await in async method

            return(new CanvasVirtualBitmap());
#endif
        }
コード例 #11
0
ファイル: DpiExample.xaml.cs プロジェクト: fikovacic/Win2D
            public PerDeviceResources(ICanvasResourceCreatorWithDpi resourceCreator)
            {
                ResourceCreator = resourceCreator;

                DefaultDpiBitmap = CreateTestBitmap(resourceCreator, defaultDpi);
                HighDpiBitmap    = CreateTestBitmap(resourceCreator, highDpi);
                LowDpiBitmap     = CreateTestBitmap(resourceCreator, lowDpi);

                AutoDpiRenderTarget = new CanvasRenderTarget(resourceCreator, testSize, testSize);
                HighDpiRenderTarget = new CanvasRenderTarget(resourceCreator, testSize, testSize, highDpi);
                LowDpiRenderTarget  = new CanvasRenderTarget(resourceCreator, testSize, testSize, lowDpi);

                SaturationEffect = new SaturationEffect {
                    Saturation = 0
                };
            }
コード例 #12
0
        public static async Task <SoftwareBitmap> RenderToSoftwareBitmap(ICanvasResourceCreatorWithDpi resourceCreator, TextRenderSettings settings)
        {
            var cbi = Render(resourceCreator, settings, true);
            CanvasRenderTarget renderTarget = new CanvasRenderTarget(resourceCreator, cbi.Size);

            using (var session = renderTarget.CreateDrawingSession())
            {
                session.DrawImage(cbi);
            }

            return(SoftwareBitmap.Convert(
                       await SoftwareBitmap.CreateCopyFromSurfaceAsync(renderTarget),
                       BitmapPixelFormat.Bgra8,
                       BitmapAlphaMode.Premultiplied
                       ));
        }
コード例 #13
0
        void EnsureResources(ICanvasResourceCreatorWithDpi resourceCreator, Size targetSize)
        {
            if (!needsResourceRecreation)
            {
                return;
            }

            if (textLayout != null)
            {
                textLayout.Dispose();
                textGeometry.Dispose();
            }

            textLayout   = CreateTextLayout(resourceCreator, (float)targetSize.Width, (float)targetSize.Height);
            textGeometry = CanvasGeometry.CreateText(textLayout);

            needsResourceRecreation = false;
        }
コード例 #14
0
ファイル: HyperBanner.cs プロジェクト: MewX/wenku10
        public Task LoadTextures(CanvasAnimatedControl Canvas, TextureLoader Textures)
        {
            // ParaBg
            ResCreator = Canvas;

            Color MaskColor = LayoutSettings.MajorBackgroundColor;

            MaskColor.A = ( byte )Math.Floor(255 * 0.8);
            MaskBrush   = new CanvasSolidColorBrush(ResCreator, MaskColor);

            ReloadBackground();

            // RippleEx
            CoverBmp   = CanvasBitmap.CreateFromColors(Canvas, new Color[] { Colors.Transparent }, 1, 1);
            RingBrush  = new CanvasSolidColorBrush(Canvas, LayoutSettings.Shades90);
            TextBrush  = new CanvasSolidColorBrush(ResCreator, Colors.White);
            CoverBrush = new CanvasSolidColorBrush(Canvas, Colors.Transparent);

            UpdateText(Canvas);
            return(ReloadCover());
        }
コード例 #15
0
        void EnsureResources(ICanvasResourceCreatorWithDpi resourceCreator, Size targetSize)
        {
            if (resourceRealizationSize == targetSize && !needsResourceRecreation)
            {
                return;
            }

            float canvasWidth  = (float)targetSize.Width;
            float canvasHeight = (float)targetSize.Height;

            if (textLayout != null)
            {
                textLayout.Dispose();
            }
            textLayout = CreateTextLayout(resourceCreator, canvasWidth, canvasHeight);

            typographyChanged         = true;
            needRefreshTypographyList = true;

            needsResourceRecreation = false;
            resourceRealizationSize = targetSize;
        }
コード例 #16
0
        void EnsureResources(ICanvasResourceCreatorWithDpi resourceCreator, Size targetSize)
        {
            if (resourceRealizationSize != targetSize && !needsResourceRecreation)
            {
                return;
            }

            float canvasWidth  = (float)targetSize.Width;
            float canvasHeight = (float)targetSize.Height;

            if (textLayout != null)
            {
                textLayout.Dispose();
                textGeometry.Dispose();
            }

            textLayout   = CreateTextLayout(resourceCreator, canvasWidth, canvasHeight);
            textGeometry = CanvasGeometry.CreateText(textLayout);

            needsResourceRecreation = false;
            resourceRealizationSize = targetSize;
        }
コード例 #17
0
        void EnsureResources(ICanvasResourceCreatorWithDpi resourceCreator, Size targetSize)
        {
            if (resourceRealizationSize == targetSize && !needsResourceRecreation)
            {
                return;
            }

            float canvasWidth  = (float)targetSize.Width;
            float canvasHeight = (float)targetSize.Height;

            sizeDim = Math.Min(canvasWidth, canvasHeight);

            if (!defaultFontSizeSet)
            {
                CurrentFontSize      = sizeDim / 1.3f;
                CurrentFontSize      = Math.Max((float)fontSizeSlider.Minimum, CurrentFontSize);
                CurrentFontSize      = Math.Min((float)fontSizeSlider.Maximum, CurrentFontSize);
                fontSizeSlider.Value = CurrentFontSize;
                defaultFontSizeSet   = true;
            }

            if (textLayout != null)
            {
                textLayout.Dispose();
            }
            textLayout = CreateTextLayout(resourceCreator, canvasWidth, canvasHeight);

            if (!UniformStyle)
            {
                textLayout.SetFontSize(0, 1, CurrentUppercaseFontSize);
                textLayout.SetFontFamily(0, 1, uppercaseFontPicker.CurrentFontFamily);

                textLayout.SetFontSize(1, 2, CurrentLowercaseFontSize);
                textLayout.SetFontFamily(1, 2, lowercaseFontPicker.CurrentFontFamily);
            }

            needsResourceRecreation = false;
            resourceRealizationSize = targetSize;
        }
コード例 #18
0
ファイル: GlyphRotation.xaml.cs プロジェクト: N500/Win2D
        void EnsureResources(ICanvasResourceCreatorWithDpi resourceCreator, Size targetSize)
        {
            if (!needsResourceRecreation)
            {
                return;
            }

            if (textLayout != null)
            {
                textLayout.Dispose();
                textReference.Dispose();
                textBrush.Dispose();
            }

            textLayout = CreateTextLayout(resourceCreator, (float)targetSize.Width, (float)targetSize.Height);

            textReference = CanvasGeometry.CreateText(textLayout);

            textBrush = new CanvasSolidColorBrush(resourceCreator, Colors.LightBlue);

            needsResourceRecreation = false;
        }
コード例 #19
0
        /// <summary>
        ///  Loads a <see cref="CanvasBitmap"/> from an XElement.
        /// </summary>
        /// <param name="canvasResource"> The canvas-resource. </param>
        /// <param name="element"> The source XElement. </param>
        /// <returns> The loaded <see cref="CanvasBitmap"/>. </returns>
        public static CanvasBitmap LoadBitmap(ICanvasResourceCreatorWithDpi canvasResource, XElement element)
        {
            int width = 1024, height = 1024;

            if (element.Element("PixelWidth") is XElement pixelWidth)
            {
                width = (int)pixelWidth;
            }
            if (element.Element("PixelHeight") is XElement pixelHeight)
            {
                height = (int)pixelHeight;
            }
            CanvasRenderTarget bitmap = new CanvasRenderTarget(canvasResource, width, height);

            if (element.Element("PixelBytes") is XElement pixelBytes)
            {
                string strings = pixelBytes.Value;
                byte[] bytes   = Convert.FromBase64String(strings);
                bitmap.SetPixelBytes(bytes);
            }

            return(null);
        }
コード例 #20
0
ファイル: PaintCanvas.cs プロジェクト: tmyt/PaintCanvas
 public static CanvasRenderTarget CreateEmpty(ICanvasResourceCreatorWithDpi device, Size size)
 {
     return(CreateEmpty(device, size, device.Dpi));
 }
コード例 #21
0
        public override Task CreateResourcesAsync(ICanvasResourceCreatorWithDpi creator, CanvasCreateResourcesReason reason)
        {
            gridColorBrush = new CanvasSolidColorBrush(creator, gridColor);

            return(Task.CompletedTask);
        }
コード例 #22
0
ファイル: DpiExample.xaml.cs プロジェクト: fengweijp/Win2D
        static CanvasBitmap CreateTestBitmap(ICanvasResourceCreatorWithDpi resourceCreator, float dpi)
        {
            int pixelSize = (int)Math.Round(testSize * dpi / defaultDpi);

            // Initialize to solid gray.
            Color[] colors = Enumerable.Repeat(Colors.Gray, pixelSize * pixelSize).ToArray();

            // Diagonal lines.
            for (int i = 0; i < pixelSize; i++)
            {
                colors[i * pixelSize + i] = Colors.Red;
                colors[i * pixelSize + (pixelSize - i - 1)] = Colors.Red;
            }

            // Single pixel border.
            for (int i = 0; i < pixelSize; i++)
            {
                colors[i] = Colors.Blue;
                colors[i * pixelSize] = Colors.Blue;
                colors[i * pixelSize + pixelSize - 1] = Colors.Blue;
                colors[pixelSize * (pixelSize - 1) + i] = Colors.Blue;
            }

            if (dpi == defaultDpi)
            {
                // We could always just use the "else" path, but want to test this default overload as well.
                return CanvasBitmap.CreateFromColors(resourceCreator, colors, pixelSize, pixelSize);
            }
            else
            {
                return CanvasBitmap.CreateFromColors(resourceCreator, colors, pixelSize, pixelSize, dpi);
            }
        }
コード例 #23
0
        public static Matrix3x2 GetDisplayTransform(Size controlSize, ICanvasResourceCreatorWithDpi canvas, int designWidth, int designHeight)
        {
            var sourceSize = new Vector2(canvas.ConvertPixelsToDips(designWidth), canvas.ConvertPixelsToDips(designHeight));

            return(GetDisplayTransform(controlSize.ToVector2(), sourceSize));
        }
コード例 #24
0
 public IAsyncAction CreateResourcesAsync(ICanvasResourceCreatorWithDpi creator)
 {
     throw new System.NotImplementedException();
 }
コード例 #25
0
ファイル: TextEditor.cs プロジェクト: liquidboy/UwpEdit
        private void EnsureResources(ICanvasResourceCreatorWithDpi resourceCreator, Size targetSize)
        {
            if (_needsTextFormatRecreation)
            {
                _textFormat?.Dispose();
                _textFormat = new CanvasTextFormat()
                {
                    HorizontalAlignment = _horizontalAlignment,
                    FontFamily          = FontFamily.Source,
                    FontSize            = (float)FontSize,
                    FontStretch         = FontStretch,
                    FontStyle           = FontStyle,
                    FontWeight          = FontWeight,
                };
            }

            if (_needsTextLayoutRecreation)
            {
                _textLayout?.Dispose();
                _textLayout = new CanvasTextLayout(resourceCreator, Text, _textFormat, (float)targetSize.Width - _marginWidth - _marginPadding, (float)targetSize.Height);
            }

            if (_needsForegroundBrushRecreation)
            {
                _foregroundBrush?.Dispose();
                _foregroundBrush = new CanvasSolidColorBrush(resourceCreator, (Foreground as SolidColorBrush).Color);
            }

            if (_needsBackgroundBrushRecreation)
            {
                _backgroundBrush?.Dispose();
                _backgroundBrush = new CanvasSolidColorBrush(resourceCreator, (Background as SolidColorBrush).Color);
            }

            if (_needsSelectionHighlightColorBrushRecreation)
            {
                _selectionHighlightColorBrush?.Dispose();
                _selectionHighlightColorBrush = new CanvasSolidColorBrush(resourceCreator, SelectionHighlightColor.Color);
            }

            if (_needsCursorColorBrushRecreation)
            {
                _cursorColorBrush?.Dispose();
                _cursorColorBrush = new CanvasSolidColorBrush(resourceCreator, CursorColor.Color);
            }

            if (_needsSelectionForegroundBrushRecreation)
            {
                _selectionForegroundBrush?.Dispose();
                _selectionForegroundBrush = new CanvasSolidColorBrush(resourceCreator, Colors.White);
            }

            if (_needsLineNumberTextFormatRecreation)
            {
                _lineNumberTextFormat?.Dispose();
                _lineNumberTextFormat = new CanvasTextFormat()
                {
                    HorizontalAlignment = CanvasHorizontalAlignment.Right,
                    FontFamily          = FontFamily.Source,
                    FontSize            = (float)FontSize,
                };
            }

            if (_needsLineNumberTextLayoutRecreation)
            {
                _lineNumberTextLayout?.Dispose();
                var lineNumberSb   = new StringBuilder();
                var characterCount = 0;
                var lineNumber     = 1;
                lineNumberSb.AppendLine(lineNumber++.ToString());
                foreach (var lineMetric in _textLayout.LineMetrics)
                {
                    characterCount += lineMetric.CharacterCount;
                    if (Text[characterCount - 1] == '\n' || Text[characterCount - 1] == '\r')
                    {
                        lineNumberSb.AppendLine(lineNumber++.ToString());
                    }
                    else
                    {
                        lineNumberSb.AppendLine();
                    }
                }
                _lineNumberTextLayout = new CanvasTextLayout(resourceCreator, lineNumberSb.ToString(), _lineNumberTextFormat, _marginWidth - _marginPadding, (float)targetSize.Height);
            }
        }
コード例 #26
0
 public static CanvasBitmap Render(ICanvasResourceCreatorWithDpi resourceCreator, TextRenderSettings settings, bool clip = false)
 {
     return(Render(resourceCreator, settings, new Size(400, 80), clip));
 }
コード例 #27
0
        void EnsureResources(ICanvasResourceCreatorWithDpi resourceCreator, Size targetSize)
        {
            if (!needsResourceRecreation)
                return;

            if (textLayout != null)
            {
                textLayout.Dispose();
                textReference.Dispose();
                textBrush.Dispose();
            }

            textLayout = CreateTextLayout(resourceCreator, (float)targetSize.Width, (float)targetSize.Height);

            textReference = CanvasGeometry.CreateText(textLayout);

            textBrush = new CanvasSolidColorBrush(resourceCreator, Colors.LightBlue);

            needsResourceRecreation = false;
        }
コード例 #28
0
        void EnsureResources(ICanvasResourceCreatorWithDpi resourceCreator, Size targetSize)
        {
            if (resourceRealizationSize == targetSize && !needsResourceRecreation)
            {
                return;
            }

            float canvasWidth  = (float)targetSize.Width;
            float canvasHeight = (float)targetSize.Height;

            sizeDim = Math.Min(canvasWidth, canvasHeight);

            textBrush = new CanvasSolidColorBrush(resourceCreator, Colors.Thistle);

            if (!defaultFontSizeSet)
            {
                if (ThumbnailGenerator.IsDrawingThumbnail)
                {
                    CurrentFontSize = 180;
                }
                else
                {
                    CurrentFontSize = sizeDim / 20;
                }
                CurrentFontSize      = Math.Max((float)fontSizeSlider.Minimum, CurrentFontSize);
                CurrentFontSize      = Math.Min((float)fontSizeSlider.Maximum, CurrentFontSize);
                fontSizeSlider.Value = CurrentFontSize;
                defaultFontSizeSet   = true;
            }

            string sampleText = null;

            switch (CurrentTextSampleOption)
            {
            case TextSampleOption.ChemicalFormula:
                sampleText =
                    "H2O is the chemical formula for water.\r\n\r\n" +
                    "And, the isotope Carbon-12 may be written as 12C.\r\n\r\n" +
                    "Often, chemical formulas make use of both superscript and subscript text.";
                break;

            case TextSampleOption.RightTriangle:
                sampleText =
                    "The side lengths of a right-angle triangle can be written as a2 + b2 = c2.\r\n\r\n" +
                    "If the triangle's shorter sides are lengths 3 and 4, the remaining side must be 5, since 32 + 42 = 52.";
                break;

            case TextSampleOption.ShortExpression:
                sampleText = "ax2by3";
                break;

            default:
                Debug.Assert(false, "Unexpected text sample option");
                break;
            }

            if (textLayout != null)
            {
                textLayout.Dispose();
            }
            textLayout = CreateTextLayout(sampleText, resourceCreator, canvasWidth, canvasHeight);

            switch (CurrentTextSampleOption)
            {
            case TextSampleOption.ChemicalFormula:
                SetSubscript(textLayout, sampleText.IndexOf("H2O") + 1, 1);
                SetSuperscript(textLayout, sampleText.IndexOf("12C"), 2);
                SetSubscript(textLayout, sampleText.IndexOf("subscript"), "subscript".Length);
                SetSuperscript(textLayout, sampleText.IndexOf("superscript"), "superscript".Length);
                break;

            case TextSampleOption.RightTriangle:
                for (int i = 0; i < sampleText.Length; ++i)
                {
                    if (sampleText[i] == '2')
                    {
                        SetSuperscript(textLayout, i, 1);
                    }
                }
                break;

            case TextSampleOption.ShortExpression:
                SetSubscript(textLayout, 1, 1);
                SetSuperscript(textLayout, 2, 1);
                SetSubscript(textLayout, 4, 1);
                SetSuperscript(textLayout, 5, 1);
                break;

            default:
                Debug.Assert(false, "Unexpected text sample option");
                break;
            }

            subscriptSuperscriptRenderer = new SubscriptSuperscriptRenderer();

            needsResourceRecreation = false;
            resourceRealizationSize = targetSize;
        }
コード例 #29
0
ファイル: DpiExample.xaml.cs プロジェクト: fengweijp/Win2D
            PerDeviceResources(ICanvasResourceCreatorWithDpi resourceCreator, CanvasVirtualBitmap virtualBitmap)
            {
                ResourceCreator = resourceCreator;

                DefaultDpiBitmap = CreateTestBitmap(resourceCreator, defaultDpi);
                HighDpiBitmap = CreateTestBitmap(resourceCreator, highDpi);
                LowDpiBitmap = CreateTestBitmap(resourceCreator, lowDpi);
                VirtualBitmap = virtualBitmap;

                AutoDpiRenderTarget = new CanvasRenderTarget(resourceCreator, testSize, testSize);
                HighDpiRenderTarget = new CanvasRenderTarget(resourceCreator, testSize, testSize, highDpi);
                LowDpiRenderTarget = new CanvasRenderTarget(resourceCreator, testSize, testSize, lowDpi);

                SaturationEffect = new SaturationEffect { Saturation = 0 };
            }
コード例 #30
0
        private async Task LoadSpritesAsync(ICanvasResourceCreatorWithDpi resourceCreator)
        {
            _spriteSheet = await SpriteSheet.LoadFromApplicationUriAsync(new Uri("ms-appx:///Assets/Sprites/SpriteSheet.png"), resourceCreator);

            // Tile sprites
            _spriteSheet.AddAlias("ButtonTile-Entities-On", "ButtonTile-Sensitive");
            _spriteSheet.AddAlias("ButtonTile-Entities-Off", "ButtonTile-Sensitive");
            _spriteSheet.AddAlias("ButtonTile-EntitiesExceptPlayer-On", "ButtonTile");
            _spriteSheet.AddAlias("ButtonTile-EntitiesExceptPlayer-Off", "ButtonTile");
            _spriteSheet.AddAlias("ButtonTile-Player-On", "ButtonTile-Sensitive");
            _spriteSheet.AddAlias("ButtonTile-Player-Off", "ButtonTile-Sensitive");
        }
コード例 #31
0
ファイル: DpiExample.xaml.cs プロジェクト: fengweijp/Win2D
        static async Task<CanvasVirtualBitmap> CreateTestVirtualBitmap(ICanvasResourceCreatorWithDpi resourceCreator)
        {
#if WINDOWS_UWP
            var canvasBitmap = CreateTestBitmap(resourceCreator, 96);

            var stream = new InMemoryRandomAccessStream();
            await canvasBitmap.SaveAsync(stream, CanvasBitmapFileFormat.Png);

            return await CanvasVirtualBitmap.LoadAsync(resourceCreator, stream);
#else
            await Task.Yield(); // prevent warning about no await in async method
            return new CanvasVirtualBitmap();
#endif
        }
コード例 #32
0
ファイル: PaintCanvas.cs プロジェクト: tmyt/PaintCanvas
 public static CanvasRenderTarget CreateEmpty(ICanvasResourceCreatorWithDpi device, Size size)
 {
     return CreateEmpty(device, size, device.Dpi);
 }
コード例 #33
0
        void EnsureResources(ICanvasResourceCreatorWithDpi resourceCreator, Size targetSize)
        {
            if (resourceRealizationSize == targetSize && !needsResourceRecreation)
                return;

            float canvasWidth = (float)targetSize.Width;
            float canvasHeight = (float)targetSize.Height;

            if (textLayout != null) 
                textLayout.Dispose();
            textLayout = CreateTextLayout(resourceCreator, canvasWidth, canvasHeight);

            inlineObject.SetLayout(textLayout);

            Rect layoutBounds = textLayout.LayoutBounds;

            textBrush = new CanvasLinearGradientBrush(resourceCreator, Colors.Red, Colors.Green);
            textBrush.StartPoint = new System.Numerics.Vector2((float)(layoutBounds.Left + layoutBounds.Right) / 2, (float)layoutBounds.Top);
            textBrush.EndPoint = new System.Numerics.Vector2((float)(layoutBounds.Left + layoutBounds.Right) / 2, (float)layoutBounds.Bottom);

            selectionTextBrush = new CanvasLinearGradientBrush(resourceCreator, Colors.Green, Colors.Red);
            selectionTextBrush.StartPoint = textBrush.StartPoint;
            selectionTextBrush.EndPoint = textBrush.EndPoint;

            inlineObjectsEnabledChanged = true;

            needsResourceRecreation = false;
            resourceRealizationSize = targetSize;
        }
コード例 #34
0
        public static CanvasBitmap Render(ICanvasResourceCreatorWithDpi resourceCreator, TextRenderSettings settings, Size renderSize, bool clip)
        {
            Rect geometryBounds;
            ICanvasResourceCreator device;
            CanvasRenderTarget     geometryRender;
            float dpi = 96;

            if (resourceCreator == null)
            {
                device         = new CanvasDevice();
                geometryRender = new CanvasRenderTarget(device, (float)renderSize.Width, (float)renderSize.Height, dpi);
            }
            else
            {
                device         = resourceCreator;
                dpi            = resourceCreator.Dpi;
                geometryRender = new CanvasRenderTarget(resourceCreator, (float)renderSize.Width, (float)renderSize.Height);
            }

            // получаем сессию для рисования текста
            using (var geometrySession = geometryRender.CreateDrawingSession())
            {
                geometrySession.Clear(Color.FromArgb(0, 0, 0, 0)); // прозрачность

                var format = new CanvasTextFormat()
                {
                    FontSize            = settings.FontSize,
                    FontFamily          = settings.FontFamily.Source,
                    HorizontalAlignment = CanvasHorizontalAlignment.Center,
                    VerticalAlignment   = CanvasVerticalAlignment.Center,
                };

                // создаем тектовый слой
                using (CanvasTextLayout textLayout = new CanvasTextLayout(device, settings.Text, format, (float)renderSize.Width, (float)renderSize.Height))
                {
                    geometrySession.DrawTextLayout(textLayout, 0, 0, settings.TextColor);

                    CanvasGeometry    geometry = CanvasGeometry.CreateText(textLayout);
                    CanvasStrokeStyle stroke   = new CanvasStrokeStyle()
                    {
                        DashStyle = settings.OutlineTextSettings.DashStyle
                    };

                    geometryBounds = geometry.ComputeStrokeBounds(settings.OutlineTextSettings.Enabled ? settings.OutlineTextSettings.StrokeWidth : 0.0f);

                    if (settings.OutlineTextSettings.Enabled)
                    {
                        geometrySession.DrawGeometry(
                            geometry,
                            settings.OutlineTextSettings.StrokeColor,
                            settings.OutlineTextSettings.StrokeWidth,
                            stroke);
                    }
                }
            }

            CanvasBitmap bitmap = CanvasBitmap.CreateFromDirect3D11Surface(device, geometryRender, dpi);


            var computedWidth  = (float)renderSize.Width;
            var computedHeight = (float)renderSize.Height;

            Matrix3x2 clipMatrix = Matrix3x2.Identity;

            // Если требуется обрезка
            if (clip)
            {
                var shadowOffset = settings.ShadowEffectSettings.BlurRadius * 2;
                computedWidth  = (float)Math.Floor(geometryBounds.Width + shadowOffset + Math.Abs(settings.ShadowEffectSettings.TranslationX));
                computedHeight = (float)Math.Floor(geometryBounds.Height + shadowOffset + Math.Abs(settings.ShadowEffectSettings.TranslationY));

                var offsetX = -(float)Math.Truncate(geometryBounds.Left);
                var offsetY = -(float)Math.Truncate(geometryBounds.Top);

                clipMatrix = Matrix3x2.CreateTranslation(new Vector2(offsetX, offsetY));
            }
            CanvasRenderTarget resultRender;

            if (resourceCreator == null)
            {
                resultRender = new CanvasRenderTarget(device, computedWidth, computedHeight, dpi);
            }
            else
            {
                resultRender = new CanvasRenderTarget(resourceCreator, computedWidth, computedHeight);
            }

            // получаем сессию для рисования текста
            using (var resultSession = resultRender.CreateDrawingSession())
            {
                resultSession.Clear(Color.FromArgb(0, 0, 0, 0));

                var shadow = new ShadowEffect
                {
                    Source     = bitmap,
                    BlurAmount = settings.ShadowEffectSettings.BlurRadius
                };

                Transform2DEffect shadowTranslation = new Transform2DEffect
                {
                    Source          = shadow,
                    TransformMatrix = clipMatrix * Matrix3x2.CreateTranslation(new Vector2(
                                                                                   settings.ShadowEffectSettings.TranslationX,
                                                                                   settings.ShadowEffectSettings.TranslationY
                                                                                   ))
                };
                resultSession.DrawImage(shadowTranslation);

                Transform2DEffect imageTranslation = new Transform2DEffect
                {
                    Source          = bitmap,
                    TransformMatrix = clipMatrix
                };

                resultSession.DrawImage(imageTranslation);
            }

            return(CanvasBitmap.CreateFromDirect3D11Surface(device, resultRender));
        }
コード例 #35
0
            static async public Task <PerDeviceResources> Create(ICanvasResourceCreatorWithDpi resourceCreator)
            {
                var virtualBitmap = await CreateTestVirtualBitmap(resourceCreator);

                return(new PerDeviceResources(resourceCreator, virtualBitmap));
            }
コード例 #36
0
        void EnsureResources(ICanvasResourceCreatorWithDpi resourceCreator, Size targetSize)
        {
            if (resourceRealizationSize == targetSize && !needsResourceRecreation)
                return;

            float canvasWidth = (float)targetSize.Width;
            float canvasHeight = (float)targetSize.Height;

            if (textLayout != null) 
                textLayout.Dispose();
            textLayout = CreateTextLayout(resourceCreator, canvasWidth, canvasHeight);
            
            typographyChanged = true;
            needRefreshTypographyList = true;

            needsResourceRecreation = false;
            resourceRealizationSize = targetSize;
        }
コード例 #37
0
        void EnsureResources(ICanvasResourceCreatorWithDpi resourceCreator, Size targetSize)
        {
            if (resourceRealizationSize == targetSize && !needsResourceRecreation)
                return;

            float canvasWidth = (float)targetSize.Width;
            float canvasHeight = (float)targetSize.Height;
            sizeDim = Math.Min(canvasWidth, canvasHeight);
            
            textBrush = new CanvasSolidColorBrush(resourceCreator, Colors.Thistle);

            if (!defaultFontSizeSet)
            {
                if (ThumbnailGenerator.IsDrawingThumbnail)
                {
                    CurrentFontSize = 180;
                }
                else
                {
                    CurrentFontSize = sizeDim / 20;
                }
                CurrentFontSize = Math.Max((float)fontSizeSlider.Minimum, CurrentFontSize);
                CurrentFontSize = Math.Min((float)fontSizeSlider.Maximum, CurrentFontSize);
                fontSizeSlider.Value = CurrentFontSize;
                defaultFontSizeSet = true;
            }
            
            string sampleText = null;
            switch (CurrentTextSampleOption)
            {
                case TextSampleOption.ChemicalFormula:
                    sampleText =
                        "H2O is the chemical formula for water.\r\n\r\n" +
                        "And, the isotope Carbon-12 may be written as 12C.\r\n\r\n" +
                        "Often, chemical formulas make use of both superscript and subscript text.";
                    break;
                case TextSampleOption.RightTriangle:
                    sampleText =
                        "The side lengths of a right-angle triangle can be written as a2 + b2 = c2.\r\n\r\n" +
                        "If the triangle's shorter sides are lengths 3 and 4, the remaining side must be 5, since 32 + 42 = 52.";
                    break;
                case TextSampleOption.ShortExpression:
                    sampleText = "ax2by3";
                    break;
                default:
                    Debug.Assert(false, "Unexpected text sample option");
                    break;
            }

            if (textLayout != null)
                textLayout.Dispose();
            textLayout = CreateTextLayout(sampleText, resourceCreator, canvasWidth, canvasHeight);

            switch (CurrentTextSampleOption)
            {
                case TextSampleOption.ChemicalFormula:
                    SetSubscript(textLayout, sampleText.IndexOf("H2O") + 1, 1);
                    SetSuperscript(textLayout, sampleText.IndexOf("12C"), 2);
                    SetSubscript(textLayout, sampleText.IndexOf("subscript"), "subscript".Length);
                    SetSuperscript(textLayout, sampleText.IndexOf("superscript"), "superscript".Length);
                    break;
                case TextSampleOption.RightTriangle:
                    for (int i = 0; i < sampleText.Length; ++i)
                    {
                        if (sampleText[i] == '2')
                            SetSuperscript(textLayout, i, 1);
                    }
                    break;
                case TextSampleOption.ShortExpression:
                    SetSubscript(textLayout, 1, 1);
                    SetSuperscript(textLayout, 2, 1);
                    SetSubscript(textLayout, 4, 1);
                    SetSuperscript(textLayout, 5, 1);
                    break;
                default:
                    Debug.Assert(false, "Unexpected text sample option");
                    break;
            }
            
            subscriptSuperscriptRenderer = new SubscriptSuperscriptRenderer();

            needsResourceRecreation = false;
            resourceRealizationSize = targetSize;
        }
コード例 #38
0
ファイル: TextOutlines.xaml.cs プロジェクト: hss1737/Win2D
        void EnsureResources(ICanvasResourceCreatorWithDpi resourceCreator, Size targetSize)
        {
            if (resourceRealizationSize != targetSize && !needsResourceRecreation)
                return;

            float canvasWidth = (float)targetSize.Width;
            float canvasHeight = (float)targetSize.Height;

            if (textLayout != null)
            {
                textLayout.Dispose();
                textGeometry.Dispose();
            }

            textLayout = CreateTextLayout(resourceCreator, canvasWidth, canvasHeight);
            textGeometry = CanvasGeometry.CreateText(textLayout);

            needsResourceRecreation = false;
            resourceRealizationSize = targetSize;
        }
コード例 #39
0
ファイル: TextOutlines.xaml.cs プロジェクト: sethbattin/Win2D
        void EnsureResources(ICanvasResourceCreatorWithDpi resourceCreator, Size targetSize)
        {
            if (!needsResourceRecreation)
                return;

            if (textLayout != null)
            {
                textLayout.Dispose();
                textGeometry.Dispose();
            }

            textLayout = CreateTextLayout(resourceCreator, (float)targetSize.Width, (float)targetSize.Height);
            textGeometry = CanvasGeometry.CreateText(textLayout);

            needsResourceRecreation = false;
        }
コード例 #40
0
ファイル: TextOutlines.xaml.cs プロジェクト: fengweijp/Win2D
        void EnsureResources(ICanvasResourceCreatorWithDpi resourceCreator, Size targetSize)
        {
            if (!needsResourceRecreation)
                return;

            if (textLayout != null)
            {
                textLayout.Dispose();
                textGeometry.Dispose();
            }

            textLayout = CreateTextLayout(resourceCreator, (float)targetSize.Width, (float)targetSize.Height);

            if (CurrentTextOutlineGranularityOption == TextOutlineGranularity.Layout)
            {
                textGeometry = CanvasGeometry.CreateText(textLayout);
            }
            else
            {
                GlyphRunsToGeometryConverter converter = new GlyphRunsToGeometryConverter(resourceCreator);

                textLayout.DrawToTextRenderer(converter, 0, 0);

                textGeometry = converter.GetGeometry();
            }

            needsResourceRecreation = false;
        }
コード例 #41
0
ファイル: DpiExample.xaml.cs プロジェクト: fengweijp/Win2D
 static async public Task<PerDeviceResources> Create(ICanvasResourceCreatorWithDpi resourceCreator)
 {                
     var virtualBitmap = await CreateTestVirtualBitmap(resourceCreator);
     return new PerDeviceResources(resourceCreator, virtualBitmap);
 }