# [Dev-luatex] [PATCH] Bug 487 (negative protrusion)

Dominic Jänichen d.newsgroups at gmail.com
Wed Apr 23 11:08:57 CEST 2014

```I was affected by bug 487 using microtype's protrusion=true.
Digging around, I found that the error lies in round_xn_over_d (pdfgen.w).
Please find attached a patch to correct the issue.

Details: round_xn_over_d calculates (x*n / d). It casts to unsinged
integers, guarding the sign of x but not of n. Negative protrusions,
however, are fed into n...

Aside: I wonder whether its method to calculate still makes sense, given
modern compiler optimizations. What about something along the lines of..?
unsinged long long t = x * n;
t = t % d;
if (t > INT_MAX) arith_error = true;
return (scaled) t;

Dominic

--- a/luatexdir/pdf/pdfgen.w	2014-04-23 10:46:24.485686773 +0200
+++ b/luatexdir/pdf/pdfgen.w	2014-04-23 10:49:03.354059350 +0200
@@ -751,13 +751,15 @@
@c
scaled round_xn_over_d(scaled x, int n, unsigned int d)
{
-    boolean positive;           /* was |x>=0|? */
+    boolean positive = true;           /* was |x>=0|, |n>=0| ? */
unsigned t, u, v;           /* intermediate quantities */
-    if (x >= 0) {
-        positive = true;
-    } else {
+    if (x < 0) {
+        positive = !positive;
x = -(x);
-        positive = false;
+    }
+    if (n < 0) {
+        positive = !positive;
+        n = -(n);
}
t = (unsigned) ((x % 0100000) * n);//printf("t=%d\n",t);
u = (unsigned) (((unsigned) (x) / 0100000) * (unsigned) n + (t /
0100000));//printf("u=%d\n",u);

```