Skip to content

Instantly share code, notes, and snippets.

@jokergoo
Created November 24, 2022 20:27
Show Gist options
  • Save jokergoo/2f238d50d6270ef364c50691617ca6d0 to your computer and use it in GitHub Desktop.
Save jokergoo/2f238d50d6270ef364c50691617ca6d0 to your computer and use it in GitHub Desktop.
text height
library(grid)
grid.newpage()
pushViewport(viewport())
str = "Hfgj"
gb = textGrob(str, 0.2, 0.5, gp = gpar(fontsize = 60, fontfamily = "mono"))
grid.draw(gb)
grid.points(0.2, 0.5, pch = 16, gp = gpar(col = "red"))
grid.rect(0.2, 0.5, width = grobWidth(gb), height = grobHeight(gb), gp = gpar(col = "black"))
grid.text("h = strHeight(gb)\nbottom_line = y - h/2", 0.2, 0.3, just = "top")
gb = textGrob(str, 0.5, 0.5, gp = gpar(fontsize = 60, fontfamily = "mono"))
grid.draw(gb)
grid.points(0.5, 0.5, pch = 16, gp = gpar(col = "red"))
grid.rect(0.5, unit(0.5, "npc") - 0.5*grobHeight(gb), width = grobWidth(gb), height = grobAscent(gb), gp = gpar(col = "red"), just = "bottom")
grid.text("h_A = grobAscent(gb)\nbottom_line = y - h/2", 0.5, 0.3, just = "top")
gb = textGrob(str, 0.8, 0.5, gp = gpar(fontsize = 60, fontfamily = "mono"))
grid.draw(gb)
grid.points(0.8, 0.5, pch = 16, gp = gpar(col = "red"))
grid.rect(0.8, unit(0.5, "npc") - 0.5*grobHeight(gb) - grobDescent(gb), width = grobWidth(gb), height = grobAscent(gb) + grobDescent(gb), gp = gpar(col = "blue"), just = "bottom")
grid.text("h_full = grobAscent(gb) + grobDescent(gb)\nh_full = h_A + h_D\nbotttm_line = y - h/2 - h_D", 0.8, 0.3, just = "top")
grid.text("y: y-position of the text center (the red dot)\ngb = textGrob('Hfgj', ...)", 0.5, 0.7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment