You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

356 lines
16 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ASCII Canvas"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(using Reportlab to export it as a PDF)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"width = 75\n",
"height = 65"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"multipage = False"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxx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xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n"
]
}
],
"source": [
"# Simple fill-up of a page, to set the reach of our canvas\n",
"lines = [] # All the lines will be stored here\n",
"\n",
"for linenumber in range(height):\n",
" line = 'x' * width\n",
" lines.append(line)\n",
"\n",
"print('\\n'.join(lines))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"TuringcompleteTuringcompleteTuringcompleteTuringcompleteTuringcomplete\n",
"isisisisisisisisisisisisisisisisisisisisisisisisisisisisisisisisisisisisis\n",
"thanthanthanthanthanthanthanthanthanthanthanthanthanthanthanthanthanthan\n",
"givinggivinggivinggivinggivinggivinggivinggivinggivinggivinggivinggiving\n",
"OulipoOulipoOulipoOulipoOulipoOulipoOulipoOulipoOulipoOulipoOulipoOulipo\n",
"(or(or(or(or(or(or(or(or(or(or(or(or(or(or(or(or(or(or(or(or(or(or(or(or(or\n",
"ideographic-pictorialideographic-pictorialideographic-pictorial\n",
"andandandandandandandandandandandandandandandandandandandandandandandandand\n",
"parts.parts.parts.parts.parts.parts.parts.parts.parts.parts.parts.parts.\n",
"converseconverseconverseconverseconverseconverseconverseconverseconverse\n",
"forforforforforforforforforforforforforforforforforforforforforforforforfor\n",
"MyMyMyMyMyMyMyMyMyMyMyMyMyMyMyMyMyMyMyMyMyMyMyMyMyMyMyMyMyMyMyMyMyMyMyMyMy\n",
"performperformperformperformperformperformperformperformperformperform\n",
"“a“a“a“a“a“a“a“a“a“a“a“a“a“a“a“a“a“a“a“a“a“a“a“a“a“a“a“a“a“a“a“a“a“a“a“a“a\n",
"whateverwhateverwhateverwhateverwhateverwhateverwhateverwhateverwhatever\n",
"becomebecomebecomebecomebecomebecomebecomebecomebecomebecomebecomebecome\n",
"complicatecomplicatecomplicatecomplicatecomplicatecomplicatecomplicate\n",
"typicallytypicallytypicallytypicallytypicallytypicallytypicallytypically\n",
"ButButButButButButButButButButButButButButButButButButButButButButButButBut\n",
"sincesincesincesincesincesincesincesincesincesincesincesincesincesincesince\n",
"ofofofofofofofofofofofofofofofofofofofofofofofofofofofofofofofofofofofofof\n",
"likelikelikelikelikelikelikelikelikelikelikelikelikelikelikelikelikelike\n",
"notnotnotnotnotnotnotnotnotnotnotnotnotnotnotnotnotnotnotnotnotnotnotnotnot\n",
"ofofofofofofofofofofofofofofofofofofofofofofofofofofofofofofofofofofofofof\n",
"ititititititititititititititititititititititititititititititititititititit\n",
"computercomputercomputercomputercomputercomputercomputercomputercomputer\n",
"misperceivesmisperceivesmisperceivesmisperceivesmisperceivesmisperceives\n",
"Java,Java,Java,Java,Java,Java,Java,Java,Java,Java,Java,Java,Java,Java,Java,\n",
"anotheranotheranotheranotheranotheranotheranotheranotheranotheranother\n",
"tototototototototototototototototototototototototototototototototototototo\n",
"controlcontrolcontrolcontrolcontrolcontrolcontrolcontrolcontrolcontrol\n",
"computercomputercomputercomputercomputercomputercomputercomputercomputer\n",
"metaphoricallymetaphoricallymetaphoricallymetaphoricallymetaphorically\n",
"andandandandandandandandandandandandandandandandandandandandandandandandand\n",
"thethethethethethethethethethethethethethethethethethethethethethethethethe\n",
"significance.significance.significance.significance.significance.\n",
"ininininininininininininininininininininininininininininininininininininin\n",
"sincesincesincesincesincesincesincesincesincesincesincesincesincesincesince\n",
"bebebebebebebebebebebebebebebebebebebebebebebebebebebebebebebebebebebebebe\n",
"purepurepurepurepurepurepurepurepurepurepurepurepurepurepurepurepurepure\n",
"asasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasas\n",
"inputinputinputinputinputinputinputinputinputinputinputinputinputinputinput\n",
"lesslesslesslesslesslesslesslesslesslesslesslesslesslesslesslesslessless\n",
"structures—semanticstructures—semanticstructures—semantic\n",
"cantcantcantcantcantcantcantcantcantcantcantcantcantcantcant\n",
"isisisisisisisisisisisisisisisisisisisisisisisisisisisisisisisisisisisisis\n",
"isisisisisisisisisisisisisisisisisisisisisisisisisisisisisisisisisisisisis\n",
"functionsfunctionsfunctionsfunctionsfunctionsfunctionsfunctionsfunctions\n",
"shape,shape,shape,shape,shape,shape,shape,shape,shape,shape,shape,shape,\n",
"namenamenamenamenamenamenamenamenamenamenamenamenamenamenamenamenamename\n",
"atatatatatatatatatatatatatatatatatatatatatatatatatatatatatatatatatatatatat\n",
"ininininininininininininininininininininininininininininininininininininin\n",
"isisisisisisisisisisisisisisisisisisisisisisisisisisisisisisisisisisisisis\n",
"ofofofofofofofofofofofofofofofofofofofofofofofofofofofofofofofofofofofofof\n",
"thethethethethethethethethethethethethethethethethethethethethethethethethe\n",
"humanshumanshumanshumanshumanshumanshumanshumanshumanshumanshumanshumans\n",
"terminals,terminals,terminals,terminals,terminals,terminals,terminals,\n",
"meaningmeaningmeaningmeaningmeaningmeaningmeaningmeaningmeaningmeaning\n",
"appropriatedappropriatedappropriatedappropriatedappropriatedappropriated\n",
"asasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasas\n",
"thatthatthatthatthatthatthatthatthatthatthatthatthatthatthatthatthatthat\n",
"bebebebebebebebebebebebebebebebebebebebebebebebebebebebebebebebebebebebebe\n",
"ononononononononononononononononononononononononononononononononononononon\n",
"DigitalDigitalDigitalDigitalDigitalDigitalDigitalDigitalDigitalDigital\n",
"ofofofofofofofofofofofofofofofofofofofofofofofofofofofofofofofofofofofofof\n"
]
}
],
"source": [
"# Another way to fill-up the page\n",
"import random \n",
"\n",
"lines = []\n",
"\n",
"txt = open('txt/language.txt', 'r').read()\n",
"words = txt.split()\n",
"\n",
"for linenumber in range(height):\n",
" word = random.choice(words)\n",
" length_of_word = len(word)\n",
" nr_of_words_fit_in_line = width / length_of_word\n",
" line = word * int(nr_of_words_fit_in_line)\n",
" lines.append(line)\n",
" \n",
"print('\\n'.join(lines))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# Same as above + a \"switch\" to make multipaged PDFs\n",
"import random \n",
"\n",
"multipage = True\n",
"number_of_pages = 100\n",
"\n",
"lines = []\n",
"pages = []\n",
"\n",
"txt = open('txt/language.txt', 'r').read()\n",
"words = txt.split()\n",
"\n",
"for page in range(number_of_pages):\n",
" \n",
" for linenumber in range(height):\n",
" word = random.choice(words)\n",
" length_of_word = len(word)\n",
" nr_of_words_fit_in_line = width / length_of_word\n",
" line = word * int(nr_of_words_fit_in_line)\n",
" lines.append(line)\n",
" \n",
" # Add a page\n",
" pages.append(lines)\n",
" lines = []\n",
"\n",
"for page in pages:\n",
" print('\\n'.join(page))\n",
" print('-' * width)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exporting with Reportlab"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"from reportlab.pdfgen.canvas import Canvas\n",
"from reportlab.lib.pagesizes import A4\n",
"from reportlab.lib.units import mm\n",
"\n",
"pagewidth = 210*mm\n",
"pageheight = 297*mm\n",
"\n",
"c = Canvas(\"ASCII-canvas-to-PDF.pdf\", pagesize=(pagewidth, pageheight), bottomup=0)\n",
"c.setFont('Courier', 12)\n",
"\n",
"start_y = 10*mm # start position of the lines\n",
"\n",
"y = start_y\n",
"lineheight = 4*mm\n",
"\n",
"if multipage == True:\n",
" for page in pages:\n",
" for line in page:\n",
" c.drawCentredString(pagewidth/2, y, line)\n",
" y += lineheight\n",
" c.showPage()\n",
" y = start_y\n",
"else:\n",
" for line in lines:\n",
" c.drawCentredString(pagewidth/2, y, line)\n",
" y += lineheight\n",
" \n",
"c.save()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}