| 1 |
from __future__ import print_function |
| 2 |
__license__ = """ |
| 3 |
This file is part of GNU FreeFont. |
| 4 |
|
| 5 |
GNU FreeFont is free software: you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License as published by the Free Software |
| 7 |
Foundation, either version 3 of the License, or (at your option) any later |
| 8 |
version. |
| 9 |
|
| 10 |
GNU FreeFont is distributed in the hope that it will be useful, but WITHOUT |
| 11 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 12 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License along with |
| 15 |
GNU FreeFont. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
""" |
| 17 |
__author__ = "Stevan White" |
| 18 |
__email__ = "[email protected]" |
| 19 |
__copyright__ = "Copyright 2010, 2018 Stevan White" |
| 20 |
__date__ = "$Date:: 2018-06-03 22:18:13 +0200#$" |
| 21 |
__version__ = "$Revision: 3919 $" |
| 22 |
|
| 23 |
import fontforge |
| 24 |
import psMat |
| 25 |
from sys import stdout |
| 26 |
|
| 27 |
__doc__ = """ |
| 28 |
Replaces the Braille Pattern range in a font. There must already be |
| 29 |
characters defined there. |
| 30 |
|
| 31 |
Two auxiliar glyphs, in variables glyphOff and glyphOn below, represent the |
| 32 |
off and on state of the Braille dots, respectively. |
| 33 |
|
| 34 |
One also needs to set the font file path, the width between columns of dots, |
| 35 |
and the width between rows of dots, as well as the width of the glyphs. |
| 36 |
|
| 37 |
The first 64 Braille Patterns consist of two columns of four dots, |
| 38 |
the bottom two of which are all zero. The other 6 dots are represented |
| 39 |
by the bit patterns of the octal digits of the offset from the range start. |
| 40 |
|
| 41 |
The remaining three sets of 64 patterns repeat the first set, with |
| 42 |
the bottom two dots being the bit pattern for the numbers 1 to 4 in binary. |
| 43 |
|
| 44 |
There are standards for the *punching* of patterns, e.g. |
| 45 |
National Library of Congress |
| 46 |
National Library Service for the Blind and Physically Handicapped |
| 47 |
Specification #800, 2008 |
| 48 |
Braille Books and Pamphlets |
| 49 |
http://loc.gov/nls/specs/800_march5_2008.pdf |
| 50 |
Among other things, it specifies: |
| 51 |
base dot diameter 0.057 |
| 52 |
distance center-to-center in a cell 0.092 |
| 53 |
distance center-to-center in adjacent cells 0.254 |
| 54 |
which ratios with a fixed width of 600EM should result in |
| 55 |
center-to-center in cell = 160EM |
| 56 |
center-to-center adjacent = 440EM |
| 57 |
diameter = 99EM |
| 58 |
|
| 59 |
""" |
| 60 |
|
| 61 |
font = fontforge.open( '../../../sfd/FreeMono.sfd' ) |
| 62 |
|
| 63 |
glyphOff = 'braille_off' |
| 64 |
glyphOn = 'braille_on' |
| 65 |
colwidth = 225 |
| 66 |
rowheight = -225 |
| 67 |
glyphwidth = 600 |
| 68 |
|
| 69 |
def drawdot( g, col, row, on ): |
| 70 |
move = psMat.translate( col * colwidth, row * rowheight ) |
| 71 |
if on: |
| 72 |
g.addReference( glyphOn, move ) |
| 73 |
else: |
| 74 |
g.addReference( glyphOff, move ) |
| 75 |
|
| 76 |
def createAndName( font, off ): |
| 77 |
return font.createChar( 0x2800 + off, 'braille%0.2X' % off ) |
| 78 |
|
| 79 |
def drawtopsix( g, off ): |
| 80 |
print( 'created', 'braille%0.2X' % off ) |
| 81 |
g.clear() |
| 82 |
g.right_side_bearing = glyphwidth |
| 83 |
for col in range ( 0, 2 ): |
| 84 |
for row in range ( 0, 3 ): |
| 85 |
#print( 'shift', ( 3 * col + row ) ) |
| 86 |
state = ( 1 << ( 3 * col + row ) ) & off |
| 87 |
drawdot( g, col, row, state ) |
| 88 |
|
| 89 |
# Contrary to the FontForge docs, font.createChar does *not* create a |
| 90 |
# glyph if one doesn't exist, but *does* re-name it if it already exists. |
| 91 |
for off in range ( 0, 0x0100 ): |
| 92 |
g = createAndName( font, off ) |
| 93 |
drawtopsix( g, off ) |
| 94 |
drawdot( g, 0, 3, ( off / 0x40 ) % 2 != 0 ) |
| 95 |
drawdot( g, 1, 3, off / 0x80 != 0 ) |
| 96 |
|
| 97 |
font.save() |