/usr/lib/python2.7/dist-packages/FontTools/fontTools/misc/fixedTools.py is in fonttools 3.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | """fontTools.misc.fixedTools.py -- tools for working with fixed numbers.
"""
from __future__ import print_function, division, absolute_import
from fontTools.misc.py23 import *
__all__ = [
"fixedToFloat",
"floatToFixed",
]
def fixedToFloat(value, precisionBits):
"""Converts a fixed-point number to a float, choosing the float
that has the shortest decimal reprentation. Eg. to convert a
fixed number in a 2.14 format, use precisionBits=14. This is
pretty slow compared to a simple division. Use sporadically.
precisionBits is only supported up to 16.
"""
if not value: return 0.0
scale = 1 << precisionBits
value /= scale
eps = .5 / scale
lo = value - eps
hi = value + eps
# If the range of valid choices spans an integer, return the integer.
if int(lo) != int(hi):
return float(round(value))
fmt = "%.8f"
lo = fmt % lo
hi = fmt % hi
assert len(lo) == len(hi) and lo != hi
for i in range(len(lo)):
if lo[i] != hi[i]:
break
period = lo.find('.')
assert period < i
fmt = "%%.%df" % (i - period)
value = fmt % value
return float(value)
def floatToFixed(value, precisionBits):
"""Converts a float to a fixed-point number given the number of
precisionBits. Ie. int(round(value * (1<<precisionBits))).
"""
return int(round(value * (1<<precisionBits)))
|