Last active
January 1, 2016 15:19
-
-
Save Glench/8163777 to your computer and use it in GitHub Desktop.
Generate a web page of all fonts installed on your Mac. Sample here: http://glench.com/all-fonts.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
from jinja2 import Template | |
template = Template(u""" | |
<html> | |
<head> | |
<title>All Fonts Preview</title> | |
<style> | |
html, body { | |
margin: 0; | |
padding: 0; | |
} | |
h2 { | |
font-weight: normal; | |
margin: 0; | |
color: #aaa; | |
font-size: 1em; | |
margin-bottom: -8px; | |
} | |
p { | |
margin: 2px 0; | |
} | |
p:hover { | |
box-shadow: 0px 0px 2px 1px #ccc inset; | |
} | |
.font { | |
font-size: 1.2em; | |
border-bottom: 10px solid #ccc; | |
padding-left: 80px; | |
position: relative; | |
} | |
.font:hover p { | |
cursor: text; | |
} | |
.font.selected { | |
background-color: rgb(211, 234, 255); | |
} | |
.big { | |
font-size: 1.5em; | |
} | |
.medium { | |
font-size: 1em; | |
} | |
.small { | |
font-size: .75em; | |
} | |
.italic { | |
font-style: italic; | |
} | |
.bold { | |
font-weight: bold; | |
} | |
[contenteditable] { | |
outline: 0; | |
} | |
.star { | |
display: inline-block; | |
width: 40px; | |
position: absolute; | |
top: 2px; | |
left: 21px; | |
color: #DBDBDB; | |
text-decoration: none; | |
font-size: 15px; | |
font-family: Helvetica; | |
} | |
.star:hover { | |
text-decoration: underline; | |
color: #999; | |
} | |
</style> | |
</head> | |
<body> | |
{% for font_name in font_families %} | |
<div class="font" data-font-name="{{ font_name }}" style="font-family: '{{ font_name }}'"> | |
<a href="#null" class="star">Move to top</a> | |
<h2>{{ font_name }}</h2> | |
<p class="big" contenteditable="true">{{ default_sentence }}</p> | |
<p class="bold" contenteditable="true">{{ default_sentence }}</p> | |
<p class="medium" contenteditable="true">{{ default_sentence }}</p> | |
<p class="italic" contenteditable="true">{{ default_sentence }}</p> | |
<p class="small" contenteditable="true">{{ default_sentence }}</p> | |
</div> | |
{% endfor %} | |
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> | |
<script> | |
$(document).on('ready', function() { | |
// change text in all boxes | |
$('p').on('input', function(evt) { | |
$('p').each(function() { | |
if (this !== evt.target) { | |
$(this).text($(evt.target).text()) | |
} | |
}) | |
}) | |
$('.star').on('click', function(evt) { | |
evt.preventDefault(); | |
var $font = $(evt.target).closest('.font'); | |
if (!$font.hasClass('selected')) { | |
// move to top | |
$font.slideUp('slow', function() { | |
$('body').prepend($font); | |
$font.slideDown('slow') | |
}) | |
} | |
$font.toggleClass('selected') | |
}) | |
}); | |
</script> | |
</body> | |
</html> | |
""") | |
manager = Cocoa.NSFontManager.sharedFontManager() | |
font_families = list(manager.availableFontFamilies()) | |
html = template.render( | |
font_families=font_families, | |
default_sentence="""The "quick", 'brown' fox jumps over the lazy dog.?!0123456789""") | |
with open(u'all-fonts.html', 'wb') as f: | |
f.write(html.encode("iso-8859-1", "replace")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment