Exemple #1
0
 public static extern int SetTextProperties(ref TextProperties textProps, out bool fontExists);
Exemple #2
0
        private void DrawString(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(TextInput.Text) || string.IsNullOrWhiteSpace(TextInput.Text))
            {
                return;
            }

            if (FontStretchInput.SelectedItem == null || FontStyleInput.SelectedItem == null || FontWeightInput.SelectedItem == null)
            {
                DiagnosticsTextBox.AppendText("You must select the font stretch, style, and weight\n");
                return;
            }

            TextProperties textProps = new TextProperties
            {
                FontBidiLevel = 0,
                FontEmSize    = (float)FontSizeInput.Value,
                FontName      = FontInput.SelectedItem.ToString(),
                FontStretch   = (DWRITE_FONT_STRETCH)Enum.Parse(typeof(DWRITE_FONT_STRETCH), FontStretchInput.SelectedItem.ToString()),
                FontStyle     = (DWRITE_FONT_STYLE)Enum.Parse(typeof(DWRITE_FONT_STYLE), FontStyleInput.SelectedItem.ToString()),
                FontWeight    = (DWRITE_FONT_WEIGHT)Enum.Parse(typeof(DWRITE_FONT_WEIGHT), FontWeightInput.SelectedItem.ToString())
            };

            Status = TextRenderer.SetTextProperties(ref textProps, out bool fontExists);

            if (Status < 0)
            {
                DiagnosticsTextBox.AppendText($"SetTextProperties failed with status {Status}\n");
                return;
            }

            if (!fontExists)
            {
                DiagnosticsTextBox.AppendText($"The requested font ({textProps.FontName}) is not available\n");
                return;
            }

            D2D1_RECT_F boundingRect = new D2D1_RECT_F
            {
                Left   = 0,
                Top    = 0,
                Right  = 500,
                Bottom = 500
            };

            Status = TextRenderer.GetRenderedTextInformation("abcedfghijklmnopqrstuvwxyz", ref boundingRect, out RenderedTextInformation textInformation);

            if (Status < 0)
            {
                DiagnosticsTextBox.AppendText($"GetRenderedTextInformation failed with status {Status}\n");
                return;
            }

            DiagnosticsTextBox.Clear();
            DiagnosticsTextBox.AppendText($"MaxGlyphsPerLine: {textInformation.MaxGlyphsPerLine}\n");
            DiagnosticsTextBox.AppendText($"MaxLines: {textInformation.MaxLines}\n");

            Bitmap     bmp     = new Bitmap(_imageProps.ImageHeight, _imageProps.ImageWidth, PixelFormat.Format32bppArgb);
            BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Height, bmp.Width), ImageLockMode.ReadWrite, bmp.PixelFormat);

            Stopwatch watch = new Stopwatch();

            watch.Start();
            Status = TextRenderer.RenderString(TextInput.Text, TextInput.Text.Length, ref boundingRect, true, DrawRectanglesInput.Checked, bmpData.Scan0, out IntPtr boundingBoxesPtr, out int boundingBoxesCount);
            watch.Stop();

            bmp.UnlockBits(bmpData);

            if (Status < 0)
            {
                DiagnosticsTextBox.AppendText($"RenderString failed with status {Status}\n");
                return;
            }

            DiagnosticsTextBox.AppendText(string.Format("Render time: {0:0.##}ms\n", watch.Elapsed.TotalMilliseconds));

            DiagnosticsTextBox.AppendText("Bounding boxes count: " + boundingBoxesCount + "\n");

            int boxPtrOffset = 0;

            for (int i = 0; i < boundingBoxesCount; i++)
            {
                D2D1_RECT_F box = Marshal.PtrToStructure <D2D1_RECT_F>(boundingBoxesPtr + boxPtrOffset);
                boxPtrOffset += Marshal.SizeOf <D2D1_RECT_F>();

                DiagnosticsTextBox.AppendText($"{{{box.Left}, {box.Top}, {box.Right}, {box.Bottom}}}\n");
            }

            TextRenderer.DeleteArray(boundingBoxesPtr);

            RenderBox.Image = bmp;
        }
Exemple #3
0
        public void RenderTest()
        {
            try
            {
                int hres = TextRenderer.Initialize();
                if (hres < 0)
                {
                    throw new Exception("Init function failed with code " + hres);
                }

                ImageProperties imageProps = new ImageProperties
                {
                    ImageHeight    = 500,
                    ImageWidth     = 500,
                    BitsPerPixel   = 32,
                    WICPixelFormat = GUID.WICPixelFormat32bppRGBA
                };

                hres = TextRenderer.SetImageProperties(ref imageProps);
                if (hres < 0)
                {
                    throw new Exception("SetImageSize function failed with code " + hres);
                }

                string[]     approvedFonts = File.ReadAllLines(@"D:\Programing Stuff\Mixed Projects\RedOrc\UnmanagedCodeTest\bin\Debug\ApprovedFonts.txt");
                FontFamily[] fonts         = new FontFamily[approvedFonts.Length];
                for (int i = 0; i < fonts.Length; i++)
                {
                    fonts[i] = new FontFamily(approvedFonts[i]);
                }

                foreach (FontFamily font in fonts)
                {
                    TextProperties textProps = new TextProperties
                    {
                        FontBidiLevel = 0,
                        FontEmSize    = 56,
                        FontName      = font.Name,
                        FontStretch   = DWRITE_FONT_STRETCH.DWRITE_FONT_STRETCH_NORMAL,
                        FontStyle     = DWRITE_FONT_STYLE.DWRITE_FONT_STYLE_NORMAL,
                        FontWeight    = DWRITE_FONT_WEIGHT.DWRITE_FONT_WEIGHT_NORMAL
                    };

                    hres = TextRenderer.SetTextProperties(ref textProps, out bool fontExists);
                    if (hres < 0)
                    {
                        throw new Exception("SetTextProperties function failed with code " + hres);
                    }

                    if (!fontExists)
                    {
                        continue;
                    }

                    D2D1_RECT_F boundingRect = new D2D1_RECT_F
                    {
                        Left   = 0,
                        Top    = 0,
                        Right  = 500,
                        Bottom = 500
                    };

                    TextRenderer.GetRenderedTextInformation("abcedfghijklmnopqrstuvwxyz", ref boundingRect, out RenderedTextInformation textInformation);

                    Bitmap     bmp     = new Bitmap(imageProps.ImageHeight, imageProps.ImageWidth, PixelFormat.Format32bppArgb);
                    BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Height, bmp.Width), ImageLockMode.ReadWrite, bmp.PixelFormat);

                    string text = "This is a very loooooooooooooong tessssssssssssst string";
                    hres = TextRenderer.RenderString(text, text.Length, ref boundingRect, true, true, bmpData.Scan0, out IntPtr boundingBoxesPtr, out int boundingBoxesCount);
                    if (hres < 0)
                    {
                        throw new Exception("RenderString function failed with code " + hres);
                    }

                    TextRenderer.DeleteArray(boundingBoxesPtr);
                    bmp.UnlockBits(bmpData);

                    //FontApprovalForm form = new FontApprovalForm(bmp, font.Name);
                    //form.ShowDialog();

                    //if (form.Approved)
                    //{
                    bmp.Save("Renders\\" + textProps.FontName + ".png");
                    //}
                }
            }
            catch (Exception e)
            {
                Assert.Fail(e.Message);
            }
            finally
            {
                TextRenderer.Uninitialize();
            }
        }