Skip to content

[font-types] Fixed types: round towards infinity, not away from zero #1738

Description

@behdad

There are multiple hints in the OpenType spec that rounding of fixed coordinates should be done towards infinity. Currently font-types implements round away from zero.

https://learn.microsoft.com/en-us/typography/opentype/otspec181/otvaroverview

"for fractional values of 0.5 and higher, take the next higher integer; for other fractional values, truncate"

I think this should do the trick:

diff --git a/font-types/src/fixed.rs b/font-types/src/fixed.rs
index af00c765..4033738b 100644
--- a/font-types/src/fixed.rs
+++ b/font-types/src/fixed.rs
@@ -180,7 +180,7 @@ macro_rules! fixed_mul_div {
             #[inline(always)]
             fn mul(self, other: Self) -> Self::Output {
                 let ab = self.0 as i64 * other.0 as i64;
-                Self(((ab + 0x8000 - i64::from(ab < 0)) >> 16) as i32)
+                Self(((ab + 0x8000) >> 16) as i32)
             }
         }
 

Both HarfBuzz and FontTools round towards infinity and don't rely on system round.

Please discuss. @rsheeter @dfrg @cmyr @anthrotype

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions