/// <summary> /// Update Value for the passed font /// </summary> /// <param name="font">Font to be used</param> public void CalculateValue(Font font) { using (Bitmap bmp = FontFunctions.DrawText(Character.ToString(), font)) { if (bmp == null) { Value = -1; } else { Width = bmp.Width; int total = 0; for (int y = 0; y < bmp.Height; y++) { for (int x = 0; x < Width; x++) { total += bmp.GetPixel(x, y).R; } } Value = (int)(((float)total / (float)(Width * bmp.Height)) + 0.5); } } }
public void ShouldSetGlyphExtentsDelegate() { using (var font = new Font(Font)) using (var fontFuncs = new FontFunctions()) { var expected = new GlyphExtents { Height = 1337 }; fontFuncs.SetGlyphExtentsDelegate((Font f, object fd, uint g, out GlyphExtents e) => { e = expected; return(true); }); fontFuncs.MakeImmutable(); font.SetFontFunctions(fontFuncs, "FontData"); var result = font.TryGetGlyphExtents('H', out var extents); Assert.True(result); Assert.Equal(expected, extents); } }
public void TryGetGlyphNameIsCorrectWithDelegate() { // get an array and fill it with things var buffer = ArrayPool <byte> .Shared.Rent(Font.NameBufferLength); for (int i = 0; i < buffer.Length; i++) { buffer[i] = (byte)i; } ArrayPool <byte> .Shared.Return(buffer); using (var font = new Font(Font)) using (var fontFuncs = new FontFunctions()) { fontFuncs.SetGlyphNameDelegate((Font f, object fd, uint g, out string n) => { n = ((char)g).ToString(); return(true); }); fontFuncs.MakeImmutable(); font.SetFontFunctions(fontFuncs, "FontData"); var result = font.TryGetGlyphName('H', out var name); Assert.True(result); Assert.Equal("H", name); } }
public void ShouldSetHorizontalFontExtentsDelegate() { using (var font = new Font(Font)) using (var fontFuncs = new FontFunctions()) { var expected = new FontExtents { Ascender = 1337 }; fontFuncs.SetHorizontalFontExtentsDelegate((Font f, object fd, out FontExtents e) => { e = expected; return(true); }); fontFuncs.MakeImmutable(); font.SetFontFunctions(fontFuncs, "FontData"); var result = font.TryGetHorizontalFontExtents(out var extents); Assert.True(result); Assert.Equal(expected, extents); } }
public void ImmutableFunctionsShouldNotChange() { using (var fontFuncs = new FontFunctions()) { fontFuncs.MakeImmutable(); Assert.Throws <InvalidOperationException>(() => fontFuncs.SetHorizontalGlyphAdvanceDelegate((a, b, c) => 1337)); } }
/// <summary> /// Calculates the size of one character in the current font. /// </summary> private void UpdateCharacterSize() { Size size = FontFunctions.MeasureText("W", Font); if (!this.IsFixedWidth) { size.Width = ValuesToVariableWidthTextConverter.CharacterWidth; } this.characterSize = size; }
public void ShouldSetHorizontalGlyphKerningDelegate() { using (var font = new Font(Font)) using (var fontFuncs = new FontFunctions()) { fontFuncs.SetHorizontalGlyphKerningDelegate((_, __, f, s) => 1337); fontFuncs.MakeImmutable(); font.SetFontFunctions(fontFuncs, "FontData"); var kerning = font.GetHorizontalGlyphKerning(49, 50); Assert.Equal(1337, kerning); } }
public void ShouldSetHorizontalGlyphAdvanceDelegate() { using (var font = new Font(Font)) using (var fontFuncs = new FontFunctions()) { fontFuncs.SetHorizontalGlyphAdvanceDelegate((f, fd, g) => 1337); fontFuncs.MakeImmutable(); font.SetFontFunctions(fontFuncs, "FontData"); var advance = font.GetHorizontalGlyphAdvance(49); Assert.Equal(1337, advance); } }
/// <summary> /// Handles the DropDown event of the cmbCharacters control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> private void CmbCharacters_DropDown(object sender, System.EventArgs e) { int width = this.cmbCharacters.Width; foreach (string characters in this.cmbCharacters.Items) { Size size = FontFunctions.MeasureText(characters + " ", this.cmbCharacters.Font); if (size.Width > width) { width = size.Width; } } this.cmbCharacters.DropDownWidth = width; }
/// <summary> /// calculate the values for the current character /// </summary> /// <param name="font">font to be used</param> public void CalculateValues(Font font) { int width = 4; int height = 4; using (Bitmap bmp = FontFunctions.DrawText(Character.ToString(), font)) { using (Bitmap shrunk = new Bitmap(width, height)) { using (Graphics g = Graphics.FromImage(shrunk)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.Clear(Color.White); g.DrawImage(bmp, 0, 0, width, height); } bmp.Dispose(); int total = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { total += shrunk.GetPixel(x, y).R; } } Value = (int)(((float)total / (float)(width * height)) + 0.5); Score = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { Score += Math.Max(Value, shrunk.GetPixel(x, y).R) - Math.Min(Value, shrunk.GetPixel(x, y).R); } } Score = (int)(((float)Score / (float)(width * height)) + 0.5); } } }
public void ShouldSetVariationGlyphDelegate() { using (var font = new Font(Font)) using (var fontFuncs = new FontFunctions()) { fontFuncs.SetVariationGlyphDelegate((Font _, object __, uint u, uint v, out uint g) => { g = 1337; return(true); }); fontFuncs.MakeImmutable(); font.SetFontFunctions(fontFuncs, "FontData"); var result = font.TryGetVariationGlyph(49, 0, out var glyph); Assert.True(result); Assert.Equal(1337u, glyph); } }
public void ShouldSetNominalGlyphsDelegate() { using (var font = new Font(Font)) using (var fontFuncs = new FontFunctions()) { fontFuncs.SetNominalGlyphsDelegate((_, __, c, u, g) => { g[0] = 1337; return(1); }); fontFuncs.MakeImmutable(); font.SetFontFunctions(fontFuncs, "FontData"); var result = font.TryGetNominalGlyph(49, out var glyph); Assert.True(result); Assert.Equal(1337u, glyph); } }
public void ShouldSetGlyphNameDelegate() { using (var font = new Font(Font)) using (var fontFuncs = new FontFunctions()) { fontFuncs.SetGlyphNameDelegate((Font f, object fd, uint g, out string n) => { n = ((char)g).ToString(); return(true); }); fontFuncs.MakeImmutable(); font.SetFontFunctions(fontFuncs, "FontData"); var result = font.TryGetGlyphName('H', out var name); Assert.True(result); Assert.Equal("H", name); } }
public void ShouldSetGlyphFromNameDelegate() { using (var font = new Font(Font)) using (var fontFuncs = new FontFunctions()) { fontFuncs.SetGlyphFromNameDelegate((Font f, object fd, string n, out uint g) => { g = n[0]; return(true); }); fontFuncs.MakeImmutable(); font.SetFontFunctions(fontFuncs, "FontData"); var result = font.TryGetGlyphFromName("H", out var glyph); Assert.True(result); Assert.Equal('H', glyph); } }
public void ShouldSetHorizontalGlyphOriginDelegate() { using (var font = new Font(Font)) using (var fontFuncs = new FontFunctions()) { fontFuncs.SetHorizontalGlyphOriginDelegate((Font f, object fd, uint g, out int px, out int py) => { px = 1337; py = 1337; return(true); }); fontFuncs.MakeImmutable(); font.SetFontFunctions(fontFuncs, "FontData"); var result = font.TryGetHorizontalGlyphOrigin(49, out var x, out _); Assert.True(result); Assert.Equal(1337, x); } }
public void ShouldSetGlyphContourPointDelegate() { using (var font = new Font(Font)) using (var fontFuncs = new FontFunctions()) { var expected = 1337; fontFuncs.SetGlyphContourPointDelegate((Font f, object fd, uint g, uint p, out int px, out int py) => { px = expected; py = expected; return(true); }); fontFuncs.MakeImmutable(); font.SetFontFunctions(fontFuncs, "FontData"); var result = font.TryGetGlyphContourPoint('H', 0, out var x, out var y); Assert.True(result); Assert.Equal(expected, x); } }
/// <summary> /// Save the text as an image file, will overwrite it if the file already exists /// </summary> /// <param name="text">The text to save</param> /// <param name="filename">Filename to save</param> /// <param name="font">Font to use for the text</param> /// <param name="textcolor">Color of the text</param> /// <param name="backgroundcolor">Color of the background</param> /// <param name="scale">Percentage scale of the image, 1.0-100.0</param> /// <param name="greyscale">Save the image as greyscale?</param> /// <returns>did the file save without errors?</returns> public static bool SaveTextAsImage(string text, string filename, Font font, Color textcolor, Color backgroundcolor, float scale, bool greyscale) { System.Guid format; switch (System.IO.Path.GetExtension(filename).ToLower()) { case ".png": format = ImageFormat.Png.Guid; break; case ".jpg": case ".jpeg": case ".jpe": format = ImageFormat.Jpeg.Guid; break; case ".gif": format = ImageFormat.Gif.Guid; break; case ".tif": case ".tiff": format = ImageFormat.Tiff.Guid; break; case ".bmp": case ".rle": case ".dib": default: format = ImageFormat.Bmp.Guid; break; } using (Bitmap bmpFullSize = FontFunctions.DrawText(text, font, textcolor, backgroundcolor)) { if (scale < 100f) { float fMagnification = scale / 100f; Size newSize = new Size((int)((bmpFullSize.Width * fMagnification) + 0.5), (int)((bmpFullSize.Height * fMagnification) + 0.5)); if (newSize.Width < 1) { newSize.Width = 1; } if (newSize.Height < 1) { newSize.Height = 1; } using (Bitmap bmpOutput = new Bitmap(newSize.Width, newSize.Height)) { using (ImageAttributes ia = new ImageAttributes()) { ia.SetColorMatrix(greyscale ? Matrices.Grayscale() : Matrices.Identity()); using (Graphics g = Graphics.FromImage(bmpOutput)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.DrawImage(bmpFullSize, new Rectangle(0, 0, newSize.Width, newSize.Height), 0, 0, bmpFullSize.Width, bmpFullSize.Height, GraphicsUnit.Pixel, ia); } } bmpOutput.Save(filename, new ImageFormat(format)); } } else { bmpFullSize.Save(filename, new ImageFormat(format)); } } return(true); }