Skip to content

Instantly share code, notes, and snippets.

@tomekziel
Last active June 17, 2022 18:02
Show Gist options
  • Save tomekziel/05c72bb986d24eb54d58de3e6e146b46 to your computer and use it in GitHub Desktop.
Save tomekziel/05c72bb986d24eb54d58de3e6e146b46 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
// PUBLIC DOMAIN
//
// If you need to know exact license, it is the
// Creative Commons Zero
// https://creativecommons.org/publicdomain/zero/1.0/
namespace LitwoOjczyzno
{
class LitwoOjczyzno
{
public static Dictionary<char, int[]> letters = new Dictionary<char, int[]> {
['a'] = new int[] { 0, 1, 0, 1, 1, 1, 1, 0, 1 },
['b'] = new int[] { 1, 1, 0, 1, 1, 1, 1, 1, 1 },
['c'] = new int[] { 1, 1, 1, 0, 1, 1 },
['d'] = new int[] { 1, 1, 0, 1, 0, 1, 1, 1, 0 },
['e'] = new int[] { 1, 1, 1, 1, 1, 0, 1, 1, 1 },
['f'] = new int[] { 1, 1, 1, 1, 1, 0, 1, 0, 0 },
['g'] = new int[] { 1, 0, 0, 1, 0, 1, 1, 1, 1 },
['h'] = new int[] { 1, 0, 1, 1, 1, 1, 1, 0, 1 },
['i'] = new int[] { 1, 1, 1 },
['j'] = new int[] { 0, 0, 1, 1, 0, 1, 1, 1, 1 },
['k'] = new int[] { 1, 0, 1, 1, 1, 0, 1, 0, 1 },
['l'] = new int[] { 1, 0, 1, 0, 1, 1 },
['m'] = new int[] { 1, 1, 0, 0, 1, 1, 0, 0, 1 },
['n'] = new int[] { 1, 1, 1, 1, 0, 1, 1, 0, 1 },
['o'] = new int[] { 1, 1, 1, 1, 0, 1, 1, 1, 1 },
['p'] = new int[] { 1, 1, 1, 1, 1, 1, 1, 0, 0 },
['q'] = new int[] { 1, 1, 1, 1, 1, 1, 0, 0, 1 },
['r'] = new int[] { 1, 1, 1, 0, 1, 0 },
['s'] = new int[] { 0, 1, 1, 0, 1, 0, 1, 1, 0 },
['t'] = new int[] { 1, 1, 1, 0, 1, 0, 0, 1, 0 },
['u'] = new int[] { 1, 0, 1, 1, 0, 1, 1, 1, 1 },
['v'] = new int[] { 1, 0, 1, 1, 0, 1, 0, 1, 0 },
['w'] = new int[] { 1, 0, 0, 1, 1, 0, 0, 1, 1 },
['x'] = new int[] { 1, 0, 1, 0, 1, 0, 1, 0, 1 },
['y'] = new int[] { 1, 0, 1, 1, 1, 1, 0, 1, 0 },
['z'] = new int[] { 1, 1, 0, 0, 1, 0, 0, 1, 1 },
['.'] = new int[] { 0, 0, 1 },
[','] = new int[] { 0, 1, 1 },
['"'] = new int[] { 1, 0, 1, 0, 0, 0, 0, 0, 0 },
['('] = new int[] { 0, 1, 1, 0, 0, 1 },
[')'] = new int[] { 1, 0, 0, 1, 1, 0 },
['!'] = new int[] { 1, 1, 0 },
['?'] = new int[] { 1, 1, 0, 0, 0, 1 },
[':'] = new int[] { 1, 0, 1 },
['-'] = new int[] { 0, 0, 1, 1, 0, 0 },
['1'] = new int[] { 1, 1, 0, 1, 0, 1 },
['2'] = new int[] { 1, 1, 0, 0, 1, 0, 0, 1, 1 },
['8'] = new int[] { 0, 1, 1, 1, 1, 1, 1, 1, 0 }
};
const int letterH = 3;
const int mariginY = 8;
const int mariginX = 45;
static void Main(string[] args)
{
drawPixels();
drawSubpixels();
}
static void drawPixels()
{
String tekst = File.ReadAllText("pantadeusz.txt").ToLower();
tekst = tekst.Replace("…", "...");
tekst = tekst.Replace(";", ",");
tekst = tekst.Replace("«", "\"");
tekst = tekst.Replace("»", "\"");
tekst = tekst.Replace("—", "-");
tekst = tekst.Replace("à", "a");
tekst = tekst.Replace("ę", "e");
tekst = tekst.Replace("æ", "ae");
tekst = tekst.Replace("é", "e");
tekst = tekst.Replace("ó", "o");
tekst = tekst.Replace("ł", "l");
tekst = tekst.Replace("ś", "s");
tekst = tekst.Replace("ą", "a");
tekst = tekst.Replace("ż", "z");
tekst = tekst.Replace("ź", "z");
tekst = tekst.Replace("ć", "c");
tekst = tekst.Replace("ń", "n");
tekst = tekst.Replace("*", "");
String[] linie = tekst.Split("\n");
Bitmap bmp = new Bitmap(3840, 2160);
using (Graphics g = Graphics.FromImage(bmp))
{
g.Clear(Color.White);
g.DrawRectangle(Pens.Black, 1, 1, bmp.Width-3, bmp.Height-3);
g.DrawRectangle(Pens.Black, 3, 3, bmp.Width - 7, bmp.Height - 7);
}
int currentX = mariginX;
int currentY = mariginY;
DrawText(bmp, linie[0], currentX, currentY);
currentY += letterH+1;
DrawText(bmp, linie[1], currentX, currentY);
currentY += letterH+2;
int chapterStartY = currentY;
int chapterRecordW = 0;
for (int i=4; i<linie.Length; i++)
{
var l = linie[i].Trim();
if (l.Length == 0)
{
currentY += 1;
continue;
}
if ( currentY + letterH > bmp.Height - mariginY)
{
currentX = currentX + 2 + chapterRecordW;
currentY = chapterStartY;
chapterRecordW = 0;
}
int lineW = DrawText(bmp, l, currentX, currentY);
chapterRecordW = Math.Max(chapterRecordW, lineW);
currentY += letterH + 1;
}
bmp.Save("out.png", ImageFormat.Png);
}
public static int DrawText(Bitmap bmp, String text, int x, int y)
{
int currentX = x;
for (int i=0; i<text.Length; i++)
{
char c = text[i];
if (c == ' ')
{
currentX += 2;
continue;
}
if (c == '\r')
{
continue;
}
if (false == letters.ContainsKey(c))
{
Console.Write(c);
continue;
}
int[] matrix = letters[c];
for (int xx = 0; xx < matrix.Length/3; xx++)
{
for (int yy = 0; yy < letterH; yy++)
{
int px = matrix[ (matrix.Length / 3) * yy + xx];
bmp.SetPixel(currentX + xx, y + yy, px == 1 ? Color.Black : Color.White);
}
}
currentX += (matrix.Length / 3) + 1;
}
return currentX - x;
}
static void drawSubpixels()
{
String tekst = File.ReadAllText("pantadeusz.txt").ToLower();
tekst = tekst.Replace("…", "...");
tekst = tekst.Replace(";", ",");
tekst = tekst.Replace("«", "\"");
tekst = tekst.Replace("»", "\"");
tekst = tekst.Replace("—", "-");
tekst = tekst.Replace("à", "a");
tekst = tekst.Replace("ę", "e");
tekst = tekst.Replace("æ", "ae");
tekst = tekst.Replace("é", "e");
tekst = tekst.Replace("ó", "o");
tekst = tekst.Replace("ł", "l");
tekst = tekst.Replace("ś", "s");
tekst = tekst.Replace("ą", "a");
tekst = tekst.Replace("ż", "z");
tekst = tekst.Replace("ź", "z");
tekst = tekst.Replace("ć", "c");
tekst = tekst.Replace("ń", "n");
tekst = tekst.Replace("*", "");
String[] linie = tekst.Split("\n");
Bitmap bmp = new Bitmap(3840, 2160);
using (Graphics g = Graphics.FromImage(bmp))
{
g.Clear(Color.Black);
g.DrawRectangle(Pens.White, 1, 1, bmp.Width - 3, bmp.Height - 3);
g.DrawRectangle(Pens.White, 3, 3, bmp.Width - 7, bmp.Height - 7);
}
int currentX = mariginX;
int currentY = mariginY;
DrawSubpixelText(bmp, linie[0], currentX, currentY);
currentY += letterH + 1;
DrawSubpixelText(bmp, linie[1], currentX, currentY);
currentY += letterH + 2;
int chapterStartY = currentY;
int chapterRecordW = 0;
for (int i = 4; i < linie.Length; i++)
{
var l = linie[i].Trim();
if (l.Length == 0)
{
currentY += 1;
continue;
}
if (currentY + letterH > bmp.Height - mariginY)
{
currentX = currentX + 2 + chapterRecordW;
currentY = chapterStartY;
chapterRecordW = 0;
}
int lineW = DrawSubpixelText(bmp, l, currentX, currentY);
chapterRecordW = Math.Max(chapterRecordW, lineW);
currentY += letterH + 1;
}
bmp.Save("subout.png", ImageFormat.Png);
}
public static int DrawSubpixelText(Bitmap bmp, String text, int x, int y)
{
int currentX = x;
for (int i = 0; i < text.Length; i++)
{
char c = text[i];
if (c == ' ')
{
currentX += 2;
continue;
}
if (c == '\r')
{
continue;
}
if (false == letters.ContainsKey(c))
{
Console.Write(c);
continue;
}
int[] matrix = letters[c];
int[] rgb = new int[3];
for (int yy = 0; yy < letterH; yy++)
{
for (int xx = 0; xx < matrix.Length / 3; xx++)
{
int px = matrix[(matrix.Length / 3) * yy + xx];
rgb[xx] = (px == 1 ? 0xff : 0);
}
bmp.SetPixel(currentX, y + yy, Color.FromArgb(rgb[0], rgb[1], rgb[2]));
}
currentX += 2;
}
return currentX - x;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment