Skip to content

Commit

Permalink
adjust code according to grid 4.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jokergoo committed Mar 2, 2020
1 parent 47fad61 commit 0a2c5a5
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
1 change: 0 additions & 1 deletion NEWS
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
CHANGES in VERSION 2.3.2

* support gridtext
* `anno_simple()`: fixed a bug when pch are all NA in a slice

========================
Expand Down
20 changes: 17 additions & 3 deletions R/is_abs_unit.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@

# is_abs_unit = function(x) UseMethod("is_abs_unit")

# grid::absolute.size() treats grobwidth and grobheight as non-absolute units,
# thus, I write another function to test
.is_abs_unit.unit = function(x) {
unit = attr(x, "valid.unit")
if(all(unit %in% c(1:4, 7:24))) {

if(getRversion() >= "4.0.0") {
unitType = get("unitType", envir = asNamespace("grid"))
unit = unitType(x)
} else {
unit = attr(x, "unit")
}

if(all(unit %in% c("cm", "inches", "mm", "points", "picas", "bigpts", "dida", "cicero",
"scaledpts", "lines", "char", "strwidth", "strheight", "grobwidth",
"grobheight", "strascent", "strdescent", "mylines", "mychar",
"mystrwidth", "mystrheight", "centimetre", "centimetres", "centimeter",
"centimeters", "in", "inch", "line", "millimetre", "millimetres",
"millimeter", "millimeters", "point", "pt"))) {
return(TRUE)
} else {
return(FALSE)
Expand Down Expand Up @@ -54,7 +68,7 @@
# == example
# is_abs_unit(unit(1, "mm"))
# is_abs_unit(unit(1, "npc"))
# is_abs_unit(textGrob("foo"))
# is_abs_unit(grobWidth(textGrob("foo")))
# is_abs_unit(unit(1, "mm") + unit(1, "npc"))
#
is_abs_unit = function(u) {
Expand Down
17 changes: 13 additions & 4 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -522,25 +522,34 @@ unit.c = function(...) {
}

">.unit" = function(x, y) {
if(!identical(attr(x, "unit"), "mm")) {
if(!unit_in_mm(x)) {
stop_wrap("x should be in mm unit")
}
if(!identical(attr(y, "unit"), "mm")) {
if(!unit_in_mm(y)) {
stop_wrap("y should be in mm unit")
}
x[[1]] > y[[1]]
}

"<.unit" = function(x, y) {
if(!identical(attr(x, "unit"), "mm")) {
if(!unit_in_mm(x)) {
stop_wrap("x should be in mm unit")
}
if(!identical(attr(y, "unit"), "mm")) {
if(!unit_in_mm(y)) {
stop_wrap("y should be in mm unit")
}
x[[1]] < y[[1]]
}

unit_in_mm = function(x) {
if(getRversion() >= "4.0.0") {
unitType = get("unitType", envir = asNamespace("grid"))
identical(unitType(x), "mm")
} else {
identical(attr(x, "unit"), "mm")
}
}

normalize_graphic_param_to_mat = function(x, nc, nr, name) {
if(is.matrix(x)) {
if(nrow(x) == nr && ncol(x) == nc) {
Expand Down
2 changes: 1 addition & 1 deletion man/is_abs_unit.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ Zuguang Gu <[email protected]>
\examples{
is_abs_unit(unit(1, "mm"))
is_abs_unit(unit(1, "npc"))
is_abs_unit(textGrob("foo"))
is_abs_unit(grobWidth(textGrob("foo")))
is_abs_unit(unit(1, "mm") + unit(1, "npc"))
}
6 changes: 6 additions & 0 deletions tests/testthat/test-utiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ test_that("test ks_dist", {
expect_that(d2, is_identical_to(d3))
})

test_that("test is_absolute_unit", {
expect_that(is_abs_unit(unit(1, "mm")), is_identical_to(TRUE))
expect_that(is_abs_unit(unit(1, "npc")), is_identical_to(FALSE))
expect_that(is_abs_unit(grobWidth(textGrob("foo"))), is_identical_to(TRUE))
expect_that(is_abs_unit(unit(1, "mm") + unit(1, "npc")), is_identical_to(FALSE))
})

0 comments on commit 0a2c5a5

Please sign in to comment.