Skip to content

Commit 11414de

Browse files
author
murrell
committed
fix for PR#18915; thanks to Trevor Davis
git-svn-id: https://svn.r-project.org/R/trunk@88387 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent bd97ae6 commit 11414de

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

doc/NEWS.Rd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@
165165
documented to work in concordance with \code{==} and as an exception
166166
to the typical \code{match()} behaviour which relies on
167167
\dQuote{univariate} \code{mtfrm()} alone.
168+
169+
\item It is now possible to convert \emph{to} \code{"snpc"} units
170+
via, e.g., \code{grid::convertWidth()}, fixing \PR{18915}. Thanks to
171+
\I{Trevor Davis}.
168172
}
169173
}
170174
}

src/library/grid/src/unit.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1255,6 +1255,17 @@ double transformFromINCHES(double value, int unit,
12551255
case L_LINES:
12561256
result = (result*72)/(gc->ps*gc->cex*gc->lineheight);
12571257
break;
1258+
case L_SNPC:
1259+
if (thisCM < 1e-6 || otherCM < 1e-6) {
1260+
if (result != 0)
1261+
error(_("Viewport has zero dimension(s)"));
1262+
} else {
1263+
if (thisCM <= otherCM)
1264+
result = result/(thisCM/2.54);
1265+
else
1266+
result = result/(otherCM/2.54);
1267+
}
1268+
break;
12581269
case L_MM:
12591270
result = result*2.54*10;
12601271
break;
@@ -1282,7 +1293,6 @@ double transformFromINCHES(double value, int unit,
12821293
* I'm not sure the remaining ones makes any sense.
12831294
* For simplicity, these are just forbidden for now.
12841295
*/
1285-
case L_SNPC:
12861296
case L_MYCHAR:
12871297
case L_MYLINES:
12881298
case L_STRINGWIDTH:

src/library/grid/tests/units.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,24 @@ unitCheck(sum(u1, u2),
178178
sum(unit(c(0.4, .4, .1, .1), c("in", "mm", "in", "mm"))))
179179
unitCheck(sum(unit.c(u1, u2)),
180180
sum(unit(c(0.4, .4, .1, .1), c("in", "mm", "in", "mm"))))
181+
182+
# Convert to snpc units
183+
pushViewport(viewport(width = unit(1, "inch"), height = unit(2, "inch")))
184+
unitCheck(unit(1, "inch") |> convertWidth("snpc") |> convertWidth("inch"),
185+
unit(1, "inch"))
186+
unitCheck(unit(0.5, "npc") |> convertY("snpc") |> convertY("npc"),
187+
unit(0.5, "npc"))
188+
popViewport()
189+
# Zero-dimension viewport edge cases
190+
pushViewport(viewport(width = unit(1, "inch"), height = unit(0, "inch")))
191+
unitCheck(unit(0, "cm") |> convertX("snpc") |> convertX("cm"),
192+
unit(0, "cm"))
193+
unitCheck(unit(0, "npc") |> convertHeight("snpc") |> convertHeight("npc"),
194+
unit(0, "npc"))
195+
uw <- try(unit(1, "in") |> convertWidth("snpc"),
196+
silent = TRUE)
197+
stopifnot(inherits(uw, "try-error"))
198+
uh <- try(unit(1, "in") |> convertHeight("snpc"),
199+
silent = TRUE)
200+
stopifnot(inherits(uh, "try-error"))
201+
popViewport()

0 commit comments

Comments
 (0)