Ein Zeichen eines Fonts in Konsole zeichnen

  • C#

Es gibt 5 Antworten in diesem Thema. Der letzte Beitrag () ist von simpelSoft.

    Ein Zeichen eines Fonts in Konsole zeichnen

    Hallo liebe Leute,

    ich bin kein Experte in Byte/Bit Manipulationen, gebe ich gern zu, darum erbitte ich Hilfe.
    Das Ziel ist es, ein Zeichen einer Fontdatei, die als C-Header vorliegt, in die Konsole zu malen.
    Es geht um diese Zeichensätze hier: github.com/greiman/SSD1306Ascii/tree/master/src/fonts

    Wenn ich das richtig verstanden habe, werden die Bits des Bytes pro Spalte nach unten geschrieben ?( .

    Ich habe mir mal den einfachen 8x8 Font geschnappt und das Zeichen "A" als ByteArray rauskopiert und lasse es in der Konsole "malen".
    Das geht auch gut und es kommt das Gewünschte:



    Nun interessiert mich ein gößerer Zeichensatz aus dieser Sammlung, z.B. "lcdnums14x24.h".
    Und da bin ich raus mit meiner Logik. Ich schaffe es einfach nicht, so zu denken, dass ich es umsetzen könnte || .

    Für den 8x8 Font habe ich es so gemacht:

    C#-Quellcode

    1. using System;
    2. using System.Collections;
    3. namespace ConsoleGLCD {
    4. class Program {
    5. static byte[] b = { 0x7C, 0x12, 0x12, 0x12, 0x12, 0x7C, 0x00, 0x00 };
    6. static void Main(string[] args) {
    7. PrintValues(b);
    8. Console.ReadKey();
    9. }
    10. private static void PrintValues(byte[] fontChar) {
    11. for (int x = 0; x < 8; x++) {
    12. BitArray bits = new BitArray(new byte[] { fontChar[x] });
    13. for (int y = 0; y < 8; y++) {
    14. Console.SetCursorPosition(x, y);
    15. Console.WriteLine("{0}", bits[y] ? "█" : "·");
    16. }
    17. }
    18. }
    19. }
    20. }


    Könnt ihr mir bitte ein wenig helfen?
    Also wie eine '3" sieht das nicht aus :D :



    Ich bin scheinbar zu blöd dafür.

    C#-Quellcode

    1. using System;
    2. using System.Collections;
    3. namespace CFont {
    4. class Program {
    5. static void Main(string[] args) {
    6. /*
    7. * File Name : Verdana_digits_24
    8. * Font width : 10
    9. * Font height : 24
    10. * Char : '3'
    11. */
    12. byte[] q = {
    13. 0x00, 0x1E, 0x0E, 0x0E, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x0F, 0x1E, 0xFE,
    14. 0xFC, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1C, 0x1C, 0x1C, 0x1C, 0x1E, 0x36,
    15. 0x77, 0xF3, 0xE1, 0xC0, 0x78, 0x70, 0x70, 0xF0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0,
    16. 0xF0, 0x70, 0x78, 0x3F, 0x1F, 0x0F
    17. };
    18. PrintValues(q);
    19. Console.ReadKey();
    20. }
    21. private static void PrintValues(byte[] fontChars) {
    22. int col = 0, row = 0;
    23. var bits = new BitArray(fontChars);
    24. for (int i = 0; i < bits.Length; i++) {
    25. Console.SetCursorPosition(col, row);
    26. Console.Write("{0}", bits[i] ? "█" : "·");
    27. if (++row > 23) {
    28. row = 0;
    29. col++;
    30. }
    31. }
    32. }
    33. }
    34. }

    simpelSoft schrieb:

    Also wie eine '3" sieht das nicht aus
    Fang mit einfachen Zeichen an und erkunde das Koordinatensystem.
    Mit nem x-y-Tausch erzeugst Du eine Spiegelung an der Diagonale.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    So langsam wirds.
    Jetzt noch optimieren und verallgemeinern, dann passt es.



    C#-Quellcode

    1. using System;
    2. using System.Collections;
    3. namespace ConsoleGLCD {
    4. class Program {
    5. static void Main(string[] args) {
    6. byte[] b0 = {
    7. 0x00, 0xfc, 0xfa, 0xf6, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0xf6, 0xfa, 0xfc,
    8. 0x00, 0xef, 0xc7, 0x83, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x83, 0xc7, 0xef,
    9. 0x00, 0x7f, 0xbf, 0xdf, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xdf, 0xbf, 0x7f
    10. };
    11. byte[] b1 = {
    12. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0xf8, 0xfc,
    13. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x83, 0xc7, 0xef,
    14. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x3f, 0x7f
    15. };
    16. byte[] b2 = {
    17. 0x00, 0x00, 0x02, 0x06, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0xf6, 0xfa, 0xfc,
    18. 0x00, 0xe0, 0xd0, 0xb8, 0x38, 0x38, 0x38, 0x38, 0x38, 0x38, 0x3b, 0x17, 0x0f,
    19. 0x00, 0x7f, 0xbf, 0xdf, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xc0, 0x80, 0x00
    20. };
    21. byte[] b3 = {
    22. 0x00, 0x00, 0x02, 0x06, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0xf6, 0xfa, 0xfc,
    23. 0x00, 0x00, 0x10, 0x38, 0x38, 0x38, 0x38, 0x38, 0x38, 0x38, 0xbb, 0xd7, 0xef,
    24. 0x00, 0x00, 0x80, 0xc0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xdf, 0xbf, 0x7f
    25. };
    26. PrintValues(b0, 13, 0);
    27. PrintValues(b1, 13, 15);
    28. PrintValues(b2, 13, 30);
    29. PrintValues(b3, 13, 45);
    30. Console.ReadKey();
    31. }
    32. private static void PrintValues(byte[] fontChars, int fontWidth, int col) {
    33. int row = 0;
    34. int loops = fontChars.Length / fontWidth;
    35. for (int h = 0; h < loops; h++) {
    36. for (int x = 0; x < fontWidth; x++) {
    37. BitArray bits = new BitArray(new byte[] { fontChars[fontWidth * h + x] });
    38. for (int y = 0; y < 8; y++) {
    39. Console.SetCursorPosition(col + x, row + y);
    40. Console.Write("{0}", bits[y] ? "█" : "·");
    41. }
    42. }
    43. row += 8;
    44. }
    45. }
    46. }
    47. }
    Finale :D :



    Vielleicht braucht ja irgendwann jemand so etwas. Lässt sich als Basis fürs Zeichnen via GDI+ benutzen (die Konsole ist natürlich nur zum Test gedacht).
    Testprogramm als Anhang...
    Bilder
    • Console_Einstellungen.png

      9,07 kB, 399×491, 56 mal angesehen
    Dateien
    • ConsoleGLCD.zip

      (27,9 kB, 49 mal heruntergeladen, zuletzt: )
    Feddich :thumbsup:
    Das ist das Endziel. Ein eigenes Control, dass dem original Arduino OLED-Display entspricht, genau die Zeichensätze verwendet, die ich auch benutze und sich an die Syntax der verwendeten Arduino-Library hält.
    Nur bei den Farben der Ausgabe habe ich mir künstlerische Freiheit gegönnt. Danke an alle Mitleser, die nicht eingeschlafen sind ^^ .