Blame Lib/fontTools/misc/fixedTools.py

rpm-build 6a2e4c
"""fontTools.misc.fixedTools.py -- tools for working with fixed numbers.
rpm-build 6a2e4c
"""
rpm-build 6a2e4c
rpm-build 6a2e4c
from __future__ import print_function, division, absolute_import
rpm-build 6a2e4c
from fontTools.misc.py23 import *
rpm-build 6a2e4c
import math
rpm-build 6a2e4c
import logging
rpm-build 6a2e4c
rpm-build 6a2e4c
log = logging.getLogger(__name__)
rpm-build 6a2e4c
rpm-build 6a2e4c
__all__ = [
rpm-build 6a2e4c
	"otRound",
rpm-build 6a2e4c
	"fixedToFloat",
rpm-build 6a2e4c
	"floatToFixed",
rpm-build 6a2e4c
    "floatToFixedToFloat",
rpm-build 6a2e4c
	"ensureVersionIsLong",
rpm-build 6a2e4c
	"versionToFixed",
rpm-build 6a2e4c
]
rpm-build 6a2e4c
rpm-build 6a2e4c
rpm-build 6a2e4c
def otRound(value):
rpm-build 6a2e4c
	"""Round float value to nearest integer towards +Infinity.
rpm-build 6a2e4c
	For fractional values of 0.5 and higher, take the next higher integer;
rpm-build 6a2e4c
	for other fractional values, truncate.
rpm-build 6a2e4c
rpm-build 6a2e4c
	https://docs.microsoft.com/en-us/typography/opentype/spec/otvaroverview
rpm-build 6a2e4c
	https://github.com/fonttools/fonttools/issues/1248#issuecomment-383198166
rpm-build 6a2e4c
	"""
rpm-build 6a2e4c
	return int(math.floor(value + 0.5))
rpm-build 6a2e4c
rpm-build 6a2e4c
rpm-build 6a2e4c
def fixedToFloat(value, precisionBits):
rpm-build 6a2e4c
	"""Converts a fixed-point number to a float, choosing the float
rpm-build 6a2e4c
	that has the shortest decimal reprentation.  Eg. to convert a
rpm-build 6a2e4c
	fixed number in a 2.14 format, use precisionBits=14.  This is
rpm-build 6a2e4c
	pretty slow compared to a simple division.  Use sporadically.
rpm-build 6a2e4c
rpm-build 6a2e4c
	precisionBits is only supported up to 16.
rpm-build 6a2e4c
	"""
rpm-build 6a2e4c
	if not value: return 0.0
rpm-build 6a2e4c
rpm-build 6a2e4c
	scale = 1 << precisionBits
rpm-build 6a2e4c
	value /= scale
rpm-build 6a2e4c
	eps = .5 / scale
rpm-build 6a2e4c
	lo = value - eps
rpm-build 6a2e4c
	hi = value + eps
rpm-build 6a2e4c
	# If the range of valid choices spans an integer, return the integer.
rpm-build 6a2e4c
	if int(lo) != int(hi):
rpm-build 6a2e4c
		return float(round(value))
rpm-build 6a2e4c
	fmt = "%.8f"
rpm-build 6a2e4c
	lo = fmt % lo
rpm-build 6a2e4c
	hi = fmt % hi
rpm-build 6a2e4c
	assert len(lo) == len(hi) and lo != hi
rpm-build 6a2e4c
	for i in range(len(lo)):
rpm-build 6a2e4c
		if lo[i] != hi[i]:
rpm-build 6a2e4c
			break
rpm-build 6a2e4c
	period = lo.find('.')
rpm-build 6a2e4c
	assert period < i
rpm-build 6a2e4c
	fmt = "%%.%df" % (i - period)
rpm-build 6a2e4c
	value = fmt % value
rpm-build 6a2e4c
	return float(value)
rpm-build 6a2e4c
rpm-build 6a2e4c
def floatToFixed(value, precisionBits):
rpm-build 6a2e4c
	"""Converts a float to a fixed-point number given the number of
rpm-build 6a2e4c
	precisionBits.  Ie. round(value * (1<
rpm-build 6a2e4c
	"""
rpm-build 6a2e4c
	return otRound(value * (1<
rpm-build 6a2e4c
rpm-build 6a2e4c
def floatToFixedToFloat(value, precisionBits):
rpm-build 6a2e4c
	"""Converts a float to a fixed-point number given the number of
rpm-build 6a2e4c
	precisionBits, round it, then convert it back to float again.
rpm-build 6a2e4c
	Ie. round(value * (1<
rpm-build 6a2e4c
	Note: this is *not* equivalent to fixedToFloat(floatToFixed(value)),
rpm-build 6a2e4c
	which would return the shortest representation of the rounded value.
rpm-build 6a2e4c
	"""
rpm-build 6a2e4c
	scale = 1<
rpm-build 6a2e4c
	return otRound(value * scale) / scale
rpm-build 6a2e4c
rpm-build 6a2e4c
def ensureVersionIsLong(value):
rpm-build 6a2e4c
	"""Ensure a table version is an unsigned long (unsigned short major,
rpm-build 6a2e4c
	unsigned short minor) instead of a float."""
rpm-build 6a2e4c
	if value < 0x10000:
rpm-build 6a2e4c
		newValue = floatToFixed(value, 16)
rpm-build 6a2e4c
		log.warning(
rpm-build 6a2e4c
			"Table version value is a float: %.4f; "
rpm-build 6a2e4c
			"fix to use hex instead: 0x%08x", value, newValue)
rpm-build 6a2e4c
		value = newValue
rpm-build 6a2e4c
	return value
rpm-build 6a2e4c
rpm-build 6a2e4c
rpm-build 6a2e4c
def versionToFixed(value):
rpm-build 6a2e4c
	"""Converts a table version to a fixed"""
rpm-build 6a2e4c
	value = int(value, 0) if value.startswith("0") else float(value)
rpm-build 6a2e4c
	value = ensureVersionIsLong(value)
rpm-build 6a2e4c
	return value