/// <summary> /// Computes the width and height of a specified line of text to fit inside a Rectangle /// </summary> /// <param name="font">The <see cref="MikroFont"/> used to ComputeExtentInRect.</param> /// <param name="text">The text you want to measure.</param> /// <param name="width">[OutAttribute] The width of the specified text.</param> /// <param name="height">[OutAttribute] The height of the specified text.</param> /// <param name="availableWidth">The Available Width of the Rectangle</param> /// <example>Example usage: /// <code language = "C#"> /// int height = 0, width = 0; /// _font1.ComputeExtentInRect("MikroBus.Net", out width, out height, 96); /// Debug.Print("The string size is " + width + "pixels by " + height + " pixels"); /// </code> /// <code language = "VB"> /// Dim height As Integer = 0, width As Integer = 0 /// _font1.ComputeExtentInRect("MikroBus.Net", width, height, 96) /// Debug.Print("The string size is " <![CDATA[&]]> width <![CDATA[&]]> "pixels by " <![CDATA[&]]> height <![CDATA[&]]> " pixels") /// </code> /// </example> public static void ComputeExtentInRect(this MikroFont font, string text, out int width, out int height, int availableWidth) { var addLines = 0; var sz = new Size(); var lines = text.Split('\n'); for (var i = 0; i < lines.Length; i++) { var w = MeasureString(font, lines[i]).Width; if (w > availableWidth) { addLines += w / availableWidth; w = availableWidth; } if (w > sz.Width) { sz.Width = w; } } width = sz.Width; height = font.FontHeight * (lines.Length + addLines); }
/// <summary> /// Computes the width and height of a specified line of text /// </summary> /// <param name="font">The <see cref="MikroFont"/> used to ComputeExtent.</param> /// <param name="text">The text you want to measure.</param> /// <param name="width">[OutAttribute] The width of the specified text.</param> /// <param name="height">[OutAttribute] The height of the specified text.</param> /// <example>Example usage: /// <code language = "C#"> /// int height, width; /// _font1.ComputeExtent("MikroBus.Net", out width, out height); /// Debug.Print("The string size is " + width + "pixels by " + height + " pixels"); /// </code> /// <code language = "VB"> /// Dim height As Integer, width As Integer /// _font1.ComputeExtent("MikroBus.Net", width, height) /// Debug.Print("The string size is " <![CDATA[&]]> width <![CDATA[&]]> "pixels by " <![CDATA[&]]> height <![CDATA[&]]> " pixels") /// </code> /// </example> public static void ComputeExtent(this MikroFont font, string text, out int width, out int height) { var sz = MeasureString(font, text); width = sz.Width; height = sz.Height; }
/// <summary> /// Computes the width and height of a specified line of text to fit inside a Rectangle /// </summary> /// <param name="font">The <see cref="MikroFont"/> used to ComputeExtentInRect.</param> /// <param name="text">The text you want to measure.</param> /// <param name="width">[OutAttribute] The width of the specified text.</param> /// <param name="height">[OutAttribute] The height of the specified text.</param> /// <param name="availableWidth">The Available Width of the Rectangle</param> /// <example>Example usage: /// <code language = "C#"> /// int height = 0, width = 0; /// _font1.ComputeExtentInRect("MikroBus.Net", out width, out height, 96); /// Debug.Print("The string size is " + width + "pixels by " + height + " pixels"); /// </code> /// <code language = "VB"> /// Dim height As Integer = 0, width As Integer = 0 /// _font1.ComputeExtentInRect("MikroBus.Net", width, height, 96) /// Debug.Print("The string size is " <![CDATA[&]]> width <![CDATA[&]]> "pixels by " <![CDATA[&]]> height <![CDATA[&]]> " pixels") /// </code> /// </example> public static void ComputeExtentInRect(this MikroFont font, String text, out Int32 width, out Int32 height, Int32 availableWidth) { Int32 addLines = 0; Size sz = new Size(); String[] lines = text.Split('\n'); for (Int32 i = 0; i < lines.Length; i++) { Int32 w = MeasureString(font, lines[i]).Width; if (w > availableWidth) { addLines += w / availableWidth; w = availableWidth; } if (w > sz.Width) { sz.Width = w; } } width = sz.Width; height = font.FontHeight * (lines.Length + addLines); }
/// <summary> /// Computes the width and height of a specified line of text /// </summary> /// <param name="font">The <see cref="MikroFont"/> used to ComputeExtent.</param> /// <param name="text">The text you want to measure.</param> /// <param name="width">[OutAttribute] The width of the specified text.</param> /// <param name="height">[OutAttribute] The height of the specified text.</param> /// <example>Example usage: /// <code language = "C#"> /// int height, width; /// _font1.ComputeExtent("MikroBus.Net", out width, out height); /// Debug.Print("The string size is " + width + "pixels by " + height + " pixels"); /// </code> /// <code language = "VB"> /// Dim height As Integer, width As Integer /// _font1.ComputeExtent("MikroBus.Net", width, height) /// Debug.Print("The string size is " <![CDATA[&]]> width <![CDATA[&]]> "pixels by " <![CDATA[&]]> height <![CDATA[&]]> " pixels") /// </code> /// </example> public static void ComputeExtent(this MikroFont font, String text, out Int32 width, out Int32 height) { Size sz = MeasureString(font, text); width = sz.Width; height = sz.Height; }
/// <summary> /// Returns the height and width the specified character will occupy when rendered with this MikroFont. /// </summary> /// <param name="font">The <see cref="MikroFont"/> used to measure a single character with.</param> /// <param name="character">The character to measure.</param> /// <returns>The <see cref="Size"/> that the character will occupy when rendered with this MikroFont.</returns> /// <example>Example usage: /// <code language = "C#"> /// var sz = _font1.MeasureCharacter('M'); /// Debug.Print("The size of the character is " + sz.Width + "pixels by " + sz.Height + " pixels"); /// </code> /// <code language = "VB"> /// Dim sz = _font1.MeasureCharacter("M"c) /// Debug.Print("The size of the character is " <![CDATA[&]]> sz.Width <![CDATA[&]]> "pixels by " <![CDATA[&]]> sz.Height <![CDATA[&]]> " pixels") /// </code> /// </example> public static Size MeasureCharacter(this MikroFont font, char character) { if (character < 32 || character > (font.ExtendedCharacterSet ? 255 : 126)) { return(new Size(font.Sizes[23] * (8 / font.FontHeight), font.FontHeight)); } return(new Size(font.Sizes[character - 32] * (8 / font.FontHeight), font.FontHeight)); }
/// <summary> /// Returns the height and width the specified string will occupy when rendered with this MikroFont. /// </summary> /// <param name="font">The <see cref="MikroFont"/> used to measure a string with..</param> /// <param name="text">The textural string of characters to measure.</param> /// <returns>The <see cref="Size"/> that the string will occupy when rendered with this MikroFont.</returns> /// <example>Example usage: /// <code language = "C#"> /// var sz = _font1.MeasureString("MikroBus.Net"); /// Debug.Print("The size of the string is " + sz.Width + "pixels by " + sz.Height + " pixels"); /// </code> /// <code language = "VB"> /// Dim sz = _font1.MeasureString("MikroBus.Net") /// Debug.Print("The size of the string is " <![CDATA[&]]> sz.Width <![CDATA[&]]> "pixels by " <![CDATA[&]]> sz.Height <![CDATA[&]]> " pixels") /// </code> /// </example> public static Size MeasureString(this MikroFont font, string text) { var w = 0; for (var i = 0; i < text.Length; i++) { if (text[i] < 32 || text[i] > (font.ExtendedCharacterSet ? 255 : 126)) { w += (font.Sizes[23] * 8) / font.FontHeight; } else { w += ((font.Sizes[text[i] - 32] * 8) / font.FontHeight) + 1; } } return(new Size(w - 1, font.FontHeight)); }
/// <summary> /// Computes the width and height of a specified line of text /// </summary> /// <remarks>This method takes into account multi-line strings with lines separated by an ASCII linefeed character (hex 0A).</remarks> /// <param name="font">The <see cref="MikroFont"/> used to ComputeExtentEx.</param> /// <param name="text">The text you want to measure.</param> /// <returns>The Size structure of the measured text.</returns> /// <example>Example usage: /// <code language = "C#"> /// var sz = _font1.ComputeExtentEx("MikroBus.Net"); /// Debug.Print("The string size is " + sz.Width + "pixels by " + sz.Height + " pixels"); /// </code> /// <code language = "VB"> /// Dim sz = _font1.ComputeExtentEx("MikroBus.Net") /// Debug.Print("The string size is " <![CDATA[&]]> sz.Width <![CDATA[&]]> "pixels by " <![CDATA[&]]> sz.Height <![CDATA[&]]> " pixels") /// </code> /// </example> public static Size ComputeExtentEx(this MikroFont font, string text) { var sz = new Size(); var lines = text.Split('\n'); for (var i = 0; i < lines.Length; i++) { var w = MeasureString(font, lines[i]).Width; if (w > sz.Width) { sz.Width = w; } } sz.Height = font.FontHeight * lines.Length; return(sz); }
public static void Main() { try { _oled = new OLEDCClick(Hardware.SocketFour) { FrameRate = OLEDCClick.FrameRates.OCS_140Hz }; } catch (PinInUseException ex) { Debug.Print(ex.Message); } while (true) { _oled.Canvas.Clear(); // Show some intro text Debug.Print("Show some intro text"); _oled.Canvas.DrawText(str1, _font6, KnownColors.Red, 0, 0, 96, _font6.FontHeight, true); _oled.Canvas.DrawText(str2, _font6, KnownColors.Yellow, 0, _font6.FontHeight * 2, 96, _font6.FontHeight, true); _oled.Canvas.DrawText(str3, _font6, KnownColors.Blue, 0, (_font6.FontHeight * 4) - 4, 96, _font6.FontHeight, true); _oled.Flush(); Thread.Sleep(3000); _oled.Canvas.Clear(); _oled.Canvas.DrawText(str4, _font7, KnownColors.White, 0, 20, 96, 36, true); _oled.Canvas.DrawText(str5, _font2, KnownColors.Red, 0, 60, 96, 46, true); _oled.Flush(); Thread.Sleep(3000); // Draw Logo1 Debug.Print("Draw Logo1"); _oled.Canvas.Clear(KnownColors.White); _oled.Canvas.DrawImage(logo, (_oled.CanvasWidth - logo.Width) / 2, (_oled.CanvasHeight - logo.Height) / 2); _oled.Flush(); Thread.Sleep(3000); // Draw an image from and embedded RGB 888 Bitmap and then overlay an image from a byte array over top of it. Debug.Print("Draw an image from and embedded RGB 888 Bitmap and then overlay an image from a byte array over top of it."); using (var bmp = new Bitmap(Resources.GetBytes(Resources.BinaryResources.mbn_logo), Bitmap.BitmapImageType.Bmp)) { _oled.Canvas.Clear(KnownColors.White); var img = bmp.ToMikroBitmap(); _oled.Canvas.DrawImage(img, (_oled.CanvasWidth - bmp.Width) / 2, (_oled.CanvasHeight - bmp.Height) / 2); _oled.Flush(); Thread.Sleep(1000); _oled.Canvas.DrawImage(new MikroBitmap(questionMark), 96 - 24, 96 - 24); _oled.Flush(); } Thread.Sleep(3000); // ScreenSaver Test Debug.Print("ScreenSaver Test"); for (byte x = 0; x < 8; x++) { _oled.StartScreenSaver((OLEDCClick.ScreenSaverMode)x, x == 7 ? (byte)0x02 : (byte)0x00); Thread.Sleep(5000); _oled.StopScreenSaver(); } // Draw some rectangles // Draw Rectangle passing a Rect Structure Debug.Print("Draw Rectangle passing a Rect Structure"); _oled.Canvas.DrawRectangle(KnownColors.Red, KnownColors.Red, new Rect(0, 0, 96, 16)); _oled.Canvas.DrawRectangle(KnownColors.Yellow, KnownColors.Yellow, new Rect(0, 16, 96, 16)); _oled.Canvas.DrawRectangle(KnownColors.Purple, KnownColors.Purple, new Rect(0, 32, 96, 16)); _oled.Canvas.DrawRectangle(KnownColors.Blue, KnownColors.Blue, new Rect(0, 48, 96, 16)); _oled.Canvas.DrawRectangle(KnownColors.DarkSeaGreen, KnownColors.DarkSeaGreen, new Rect(0, 64, 96, 16)); _oled.Canvas.DrawRectangle(KnownColors.CadetBlue, KnownColors.CadetBlue, new Rect(0, 80, 96, 16)); _oled.Flush(); Thread.Sleep(2000); // Draw Rectangle passing X, Y, Width and Height Debug.Print("Draw Rectangle passing X, Y, Width and Height"); _oled.Canvas.DrawRectangle(KnownColors.White, KnownColors.CadetBlue, 0, 0, 16, 96); _oled.Canvas.DrawRectangle(KnownColors.White, KnownColors.DarkSeaGreen, 16, 0, 16, 96); _oled.Canvas.DrawRectangle(KnownColors.White, KnownColors.Blue, 32, 0, 16, 96); _oled.Canvas.DrawRectangle(KnownColors.White, KnownColors.Purple, 48, 0, 16, 96); _oled.Canvas.DrawRectangle(KnownColors.White, KnownColors.Yellow, 64, 0, 16, 96); _oled.Canvas.DrawRectangle(KnownColors.White, KnownColors.PapayaWhip, 80, 0, 16, 96); _oled.Flush(); Thread.Sleep(2000); // Fill the screen with a Random colors a few times Debug.Print("Fill the screen with a Random colors a few times"); for (int x = 0; x < 50; x++) { _oled.Canvas.Clear(GenerateRandomColor()); _oled.Flush(); } // Fill Canvas with gradient Debug.Print("Fill Canvas with gradient"); _oled.Canvas.DrawRectangle(KnownColors.White, 2, KnownColors.Blue, KnownColors.Red, 0, 0, _oled.CanvasWidth, _oled.CanvasHeight, true); _oled.Flush(); Thread.Sleep(1500); // Draw some random Gradients, first 25 horizontally and the next 25 vertically. Debug.Print("Draw some random Gradients, first 25 horizontally and the next 25 vertically."); var vertical = true; for (int x = 0; x < 50; x++) { if (x > 25) { vertical = false; } _oled.Canvas.DrawRectangle(GenerateRandomColor(), 2, GenerateRandomColor(), GenerateRandomColor(), 0, 0, _oled.CanvasWidth, _oled.CanvasHeight, vertical); _oled.Flush(); } // Draw some randomly placed hollow circles Debug.Print("Draw some randomly placed hollow circles"); _oled.Canvas.Clear(); for (int x = 0; x < 50; x++) { var point = GenerateRandomPoint(); var color = GenerateRandomColor(); var radius = GenerateRandomRadius(); _oled.Canvas.DrawEllipse(color, point.X, point.Y, radius, radius); _oled.Flush(); } // Draw some randomly placed filled circles Debug.Print("Draw some randomly placed filled circles"); _oled.Canvas.Clear(); for (int x = 0; x < 50; x++) { var point = GenerateRandomPoint(); var color = GenerateRandomColor(); var radius = GenerateRandomRadius(); _oled.Canvas.DrawEllipse(color, color, point.X, point.Y, radius, radius); _oled.Flush(); } // Draw some randomly placed gradient filled circles Debug.Print("Draw some randomly placed gradient filled circles"); _oled.Canvas.Clear(); for (int x = 0; x < 50; x++) { var point = GenerateRandomPoint(); var color1 = GenerateRandomColor(); var color2 = GenerateRandomColor(); var color3 = GenerateRandomColor(); var radius1 = GenerateRandomRadius(); var radius2 = GenerateRandomRadius(); _oled.Canvas.DrawEllipse(color1, color2, color3, point.X, point.Y, radius1, radius2); _oled.Flush(); } // Draw some randomly placed lines Debug.Print("Draw some randomly placed lines"); _oled.Canvas.Clear(KnownColors.Black); for (int x = 0; x < 100; x++) { var point1 = GenerateRandomPoint(); var point2 = GenerateRandomPoint(); var color = GenerateRandomColor(); _oled.Canvas.DrawLine(color, point1.X, point1.Y, point2.X, point2.Y); _oled.Flush(); } // Draw some randomly place hollow rectangles Debug.Print("Draw some randomly place hollow rectangles"); _oled.Canvas.Clear(KnownColors.Black); for (int x = 0; x < 50; x++) { var point1 = GenerateRandomPoint(); var point2 = GenerateRandomPoint(); var color = GenerateRandomColor(); _oled.Canvas.DrawRectangle(color, point1.X, point1.Y, point2.X, point2.Y); _oled.Flush(); } // Draw some randomly place filled rectangles Debug.Print("Draw some randomly place filled rectangles"); _oled.Canvas.Clear(KnownColors.Black); for (int x = 0; x < 50; x++) { var point1 = GenerateRandomPoint(); var point2 = GenerateRandomPoint(); var color = GenerateRandomColor(); _oled.Canvas.DrawRectangle(color, color, point1.X, point1.Y, point2.X, point2.Y); _oled.Flush(); } // Draw some randomly place gradient filled rectangles Debug.Print("Draw some randomly place gradient filled rectangles"); _oled.Canvas.Clear(KnownColors.Black); vertical = true; for (int x = 0; x < 50; x++) { if (x > 25) { vertical = false; } var point1 = GenerateRandomPoint(); var point2 = GenerateRandomPoint(); var color = GenerateRandomColor(); _oled.Canvas.DrawRectangle(color, color, point1.X, point1.Y, point2.X, point2.Y); _oled.Canvas.DrawRectangle(GenerateRandomColor(), 2, GenerateRandomColor(), GenerateRandomColor(), point1.X, point1.Y, point2.X, point2.Y, vertical); _oled.Flush(); } // Draw some text Debug.Print("Draw some text"); _oled.Canvas.Clear(KnownColors.Black); var color4 = GenerateRandomColor(); var color5 = GenerateRandomColor(); var color6 = GenerateRandomColor(); var color7 = GenerateRandomColor(); var color8 = GenerateRandomColor(); var color9 = GenerateRandomColor(); _oled.Canvas.DrawText("0123AaBb", _font1, color4, 0, 0, 96, _font1.FontHeight); _oled.Canvas.DrawText("0123AaBb", _font2, color5, 0, _font1.FontHeight + 1, 96, _font2.FontHeight); _oled.Canvas.DrawText("0123AaBb", _font3, color6, 0, _font1.FontHeight + _font2.FontHeight + 1, 96, _font3.FontHeight); _oled.Canvas.DrawText("0123AaBb", _font4, color7, 0, _font1.FontHeight + _font2.FontHeight + _font3.FontHeight + 1, 96, _font4.FontHeight); _oled.Canvas.DrawText("0123AaBb", _font5, color8, 0, _font1.FontHeight + _font2.FontHeight + _font3.FontHeight + _font4.FontHeight + 1, 96, _font5.FontHeight); _oled.Canvas.DrawText("0123AaBb", _font6, color9, 0, _font1.FontHeight + _font2.FontHeight + _font2.FontHeight + _font4.FontHeight + _font5.FontHeight + 1, 96, _font6.FontHeight); _oled.Flush(_oled.Canvas); Thread.Sleep(2000); // Draw some text in a rectangle Debug.Print("Draw some text in a rectangle"); var r1 = new Rect(10, 20, 76, 28); var r2 = new Rect(10, r1.Height + r1.Y + 10, 76, 28); _oled.Canvas.Clear(KnownColors.White); _oled.Canvas.DrawRectangle(KnownColors.Red, KnownColors.White, r1); r1 = r1.Inflate(0, 6, 0, 0); _oled.Canvas.DrawText("MikroBus.Net", _font3, KnownColors.Blue, r1, true); _oled.Canvas.DrawRectangle(KnownColors.Red, KnownColors.White, r2); r2 = r2.Inflate(0, 6, 0, 0); _oled.Canvas.DrawText("Rocks", _font4, KnownColors.Blue, r2, true); _oled.Flush(); Thread.Sleep(5000); Debug.GC(true); // just about at the memory limit here. // PowerMode Debug.Print("PowerMode Test"); _oled.Canvas.Clear(); _oled.Canvas.DrawText("PowerMode Test", _font3, KnownColors.White, 0, 0, 96, 48); _oled.Canvas.DrawText("Low Power for 5 secs", _font1, KnownColors.White, 0, 49, 96, 48); _oled.Flush(); Thread.Sleep(5000); _oled.PowerMode = PowerModes.Low; Thread.Sleep(5000); _oled.PowerMode = PowerModes.On; _oled.Canvas.DrawText("PowerMode Test", _font3, KnownColors.White, 0, 0, 96, 48); _oled.Canvas.DrawText("Low Power for 5 secs", _font1, KnownColors.Black, 0, 49, 96, 48); _oled.Canvas.DrawText("Power back On", _font1, KnownColors.White, 0, 49, 96, 48); _oled.Flush(); Thread.Sleep(2000); // Reset Mode Debug.Print("Reset Test"); _oled.Canvas.Clear(); _oled.Canvas.DrawText("Reset Test", _font3, KnownColors.White, 0, 0, 96, 48); _oled.Canvas.DrawText("Reset in 5 secs", _font1, KnownColors.White, 0, 49, 96, 48); _oled.Flush(); Thread.Sleep(5000); _oled.Reset(ResetModes.Hard); _oled.Canvas.DrawText("Reset Test", _font3, KnownColors.White, 0, 0, 95, 48); _oled.Canvas.DrawText("Reset in 5 secs", _font1, KnownColors.Black, 0, 49, 96, 48); _oled.Canvas.DrawText("Resume from reset", _font1, KnownColors.White, 0, 49, 96, 48); _oled.Flush(); Thread.Sleep(2000); // Display On/Off Test Debug.Print("Display On/Off Test"); _oled.Canvas.Clear(); _oled.Canvas.DrawText("Display Off", _font3, KnownColors.White, 0, 0, 96, _font3.FontHeight); _oled.Flush(); Thread.Sleep(2000); _oled.SetDisplayOn(false); Thread.Sleep(1000); _oled.SetDisplayOn(true); _oled.Canvas.DrawText("Display On", _font3, KnownColors.White, 0, _font3.FontHeight + 1, 96, _font3.FontHeight); _oled.Flush(); Thread.Sleep(2000); Debug.Print("Memory - " + Debug.GC(true)); // Now for a really large font? Debug.Print("Now for a really large font"); var numbers = new MikroFont(Resources.GetBytes(Resources.BinaryResources.tahoma_30e)); _oled.Canvas.Clear(); for (int x = 10; x >= 0; x += -1) { _oled.Canvas.Clear(); var sz = numbers.MeasureString(x.ToString()); _oled.Canvas.DrawText(x.ToString(), numbers, KnownColors.LightSkyBlue, ((96 - sz.Width) / 2) - 1, ((96 - sz.Height) / 2) - 1, 96, 96); _oled.Canvas.DrawText(x.ToString(), numbers, KnownColors.Blue, (96 - sz.Width) / 2, (96 - sz.Height) / 2, 96, 96); _oled.Flush(); Thread.Sleep(1000); } numbers = null; Debug.Print("Memory - " + Debug.GC(true)); } }