Skip to content

Instantly share code, notes, and snippets.

@ellisonbg
Last active December 21, 2015 21:48
Show Gist options
  • Save ellisonbg/6370424 to your computer and use it in GitHub Desktop.
Save ellisonbg/6370424 to your computer and use it in GitHub Desktop.
Text for kerning fonts
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We are working on the kerning of the xkcd font being developed in this GitHub repo:\n",
"\n",
"https://github.com/ipython/xkcd-font"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The purpose of this IPython notebook is to create text that we can use to help us kern the font. To do this we generate random text that covers as many of the letter digraphs as possible. The approach used here is rather pedestrian, but it works fairly well if you are OK with random text.\n",
"\n",
"First let's enable the xkcd font we are working on kerning. This loads the current version of the font into the Notebook:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from IPython.display import xkcdify"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"xkcdify(borders=False)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
" <style>\n",
" @font-face {\n",
" font-family: \"xkcd\";\n",
" src: url('http://xkcd.com/fonts/xkcd-Regular.otf');\n",
" }\n",
" \n",
" .CodeMirror {font-family: xkcd; font-size: 120%}\n",
" \n",
" .rendered_html {font-family: xkcd; font-size: 120%}\n",
" pre, code {font-family: xkcd; font-size: 120%}\n",
" .rendered_html table {border: 2px solid black;}\n",
" .rendered_html tr {border: 2px solid black;}\n",
" .rendered_html th {border: 2px solid black; padding: 0.45em 1em;}\n",
" .rendered_html td {border: 2px solid black; padding: 0.45em 1em;}\n",
" \n",
" div.prompt {font-family: xkcd; font-size: 120%}\n",
" \n",
" div.output_area pre {font-family: xkcd; font-size: 120%}\n",
" </style>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.HTML at 0x100f79210>"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's also adjust the font size and line height of the text we are going to view:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from IPython.display import HTML"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"HTML(\"\"\"\n",
"<style>\n",
"div.output_area pre {font-size: 150%; line-height: 1.5;}\n",
"</style>\n",
"\"\"\")"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
"<style>\n",
"div.output_area pre {font-size: 150%; line-height: 1.5;}\n",
"</style>\n"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 8,
"text": [
"<IPython.core.display.HTML at 0x100f791d0>"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, define some functions for generating our kerning text:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import random\n",
"from IPython.utils.text import wrap_paragraphs"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 64
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"alpha = 'abcdefghijklmnopqrstuvwxyz'.upper()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 98
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def random_word(size):\n",
" \"\"\"Generate a random word of size.\"\"\"\n",
" return ''.join(random.sample(alpha, size))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 99
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def random_text(nwords, min_size=2, max_size=15, ncols=80):\n",
" \"\"\"Generate random text with n words of length (min_size, max_size).\"\"\"\n",
" words = []\n",
" for i in range(nwords):\n",
" size = random.randint(min_size,max_size)\n",
" word = random_word(size)\n",
" words.append(word)\n",
" return wrap_paragraphs(' '.join(words), ncols=ncols)[0]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 100
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def count_digraphs(text):\n",
" \"\"\"Count the total number of unique digraphs in a text.\"\"\"\n",
" digraphs = set()\n",
" words = text.split(' ')\n",
" for word in words:\n",
" for i in range(len(word)-1):\n",
" digraphs.add(word[i:i+2])\n",
" return len(digraphs)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 101
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Generate some text:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"text = random_text(200)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 102
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here is the sample text we can use for kerning:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print text"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"BOLMCX GM FGJLNVCWYR ER GRVBQMZPTLI XQ IV OA ACBJUKRFYZDVTNP CQM MGFIYPK\n",
"XCVBGDOSNAE MNHLOYBESID KDHEJCPA HISNFCQA XRHYDGM KNXIT XEIMTQRGHJYO\n",
"THQEYJOPRLUFS UCFIBSWVNMZ SATXWUGLEPQN KJMVWNECSHGIP KAQDJEXUT TDYJGC\n",
"XGDPRHSLYVAOEB VTZBPLNRFK ZHAGVMUNIJBK ZPLDWOXEQIAH HEBUSTIQPGDWZJA RYZPWCJ\n",
"BCWEUXPJYSLT ASCKL CJUSVQTRGMFIOLN IQTX LQCEHAYF WZIAGVPXENR VLSKCFGAB VQIR\n",
"XYQGLKR WHGJCRDFY QTM FJPW ONZJITHC NDCQLKSJEFTX PDTHISXZEN MJ SLRZYIDPFMEWNCO\n",
"ZQLUCIRXJMADTK BKYGEP CVSN DYJMWGEV QUHMF BWCZTGIKNSHJ EFIVW QPKSVINJDOU\n",
"ISQBAGLWHZUK NR RG IWHPBXMNAYTKC YRQZFGKMCWLP XI DIRYSLVXH PXWUENBRIVTJZ FWJMKY\n",
"XMLEKDTF YKM QK SMCBPVXNOAH VGUJWCAINETRZ HCQPNAWM KZE GVYXRDLCQEJI XOUTSZD\n",
"KNCESXRQ QMYABHOJE IPM PBNGJMSZXRLHC TPDLIUVZ PO QMDXSBNTCPG HVQJYWNRBEMD VM\n",
"OPYSWMTFQZJG PU OHAZ FKNZTBGXRPJ RMBDCNGW DR KMYIWJHAFCSBET NDYGJUMKHFSPBC\n",
"LUWTYVBPIAXE UPIOGK SDGX HJGPWQZBKENX CALSDRHIOB VEAPOXLUWQ JL KEUQWRAFTXB\n",
"LJZHNUQCMBXW RECHY AJVP QLK IRQLYGZSHD AJIGKBF QHEAFDSXLZTCG ZWCUEY FHJAPNWV\n",
"GMXVZ ROP QBK WAGLUH WQM TXPLNBYU DMB PR FJ IJDLZUHMG AOIHFYWXPGE LMSQRAPDWBT\n",
"LDYCN OQDAREN DREVLQKIYJT VHSIMAORTG EQCOJPLFIKVMGZT OQZFEJRLIAWVXP VBIMF IUQ\n",
"GOJ QDE IHR JMXNZ AEX IRQKCON UBYMNTQELWRIJFG XRLNMGAQSCTW RWPKYLOFVTAMG\n",
"VTHKSAZYPGBU DOA MNBKG ZECYOFJVD QO FXPJ SKYLC ZOQEU ZKRINBOPDXGHJAS\n",
"HLEBKRGODJUXSWN GMS NWUDJQR HNWOUKJ EHSCZKTFOU YGW VLPMNTFQA QUJEMZCRXNDFBKT\n",
"ISEVGNYOHWUJ RL ZSBIEJFXKO JNQZUYLPAVCS QAM FBRKJY TLGJUXSFHMZV XKVFDZI RQH\n",
"ALUFZCDI WLCMSDTV HIFGYXCRMBO PDX IAVPFMHSXQNGBTZ JZVEDT OKAXQCEPGYVDZT\n",
"OBNQVRLXTDSW OUWFCSQLPR XUPAQYC TUZIDVWFKSPAMRC ULVPBOAEMWTY NR ARVLUMKJT\n",
"PGQJZDE MYIPOFEVNQJBRTC IXQ JDVFZPWIX WZINDK LWJXABQYHPGTK PVGLUHCKRWN FJWZ\n",
"UTIZVJSBCLNG CYPGAVTIUF ILB UVSJIBNTKHDEX FVKULXIJZYPQRD VRNIBHUGZAKYLP UCNQIS\n",
"ZAJUC PXEHNT VGKAOQJUFBP JPGCYUO DPSI YGZVOQM DFVJXAMZURNSLP DHGOYBQWZSAF\n",
"VCWXFPYTABLSK\n"
]
}
],
"prompt_number": 103
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This sample has a good fraction of the total number of digraphs:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"count = count_digraphs(text); count"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 116,
"text": [
"611"
]
}
],
"prompt_number": 116
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"total = 26.0**2; total"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 117,
"text": [
"676.0"
]
}
],
"prompt_number": 117
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"fraction = count/total; fraction"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 119,
"text": [
"0.9038461538461539"
]
}
],
"prompt_number": 119
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"len(text)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 97,
"text": [
"1833"
]
}
],
"prompt_number": 97
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment