Skip to content

Commit

Permalink
version 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
clauswilke authored and cran-robot committed Jan 24, 2020
0 parents commit 88d3845
Show file tree
Hide file tree
Showing 79 changed files with 7,076 additions and 0 deletions.
33 changes: 33 additions & 0 deletions DESCRIPTION
@@ -0,0 +1,33 @@
Package: gridtext
Type: Package
Title: Improved Text Rendering Support for 'Grid' Graphics
Version: 0.1.0
Authors@R:
person(
given = "Claus O.",
family = "Wilke",
role = c("aut", "cre"),
email = "wilke@austin.utexas.edu",
comment = c(ORCID = "0000-0002-7470-9261")
)
Description: Provides support for rendering of formatted text using 'grid' graphics. Text can be
formatted via a minimal subset of 'Markdown', 'HTML', and inline 'CSS' directives, and it can be
rendered both with and without word wrap.
URL: https://wilkelab.org/gridtext
BugReports: https://github.com/wilkelab/gridtext/issues
License: MIT + file LICENSE
Depends: R (>= 3.5)
Imports: grid, grDevices, markdown, rlang, Rcpp, RCurl, png, jpeg,
stringr, xml2
Suggests: covr, knitr, rmarkdown, testthat, vdiffr
LinkingTo: Rcpp, testthat
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.0.2
SystemRequirements: C++11
NeedsCompilation: yes
Packaged: 2020-01-16 18:37:10 UTC; clauswilke
Author: Claus O. Wilke [aut, cre] (<https://orcid.org/0000-0002-7470-9261>)
Maintainer: Claus O. Wilke <wilke@austin.utexas.edu>
Repository: CRAN
Date/Publication: 2020-01-24 17:30:02 UTC
2 changes: 2 additions & 0 deletions LICENSE
@@ -0,0 +1,2 @@
YEAR: 2020
COPYRIGHT HOLDER: Claus O. Wilke
78 changes: 78 additions & 0 deletions MD5
@@ -0,0 +1,78 @@
f5708889ca4f0a9a29ebb53c6f9e6037 *DESCRIPTION
39276fab68fb6f733821e2f243383213 *LICENSE
0857ec650ec6eaebb76f061ff2d67a68 *NAMESPACE
aa7a4cb0eeecc0281a2dacf613938f2a *NEWS.md
b35088f9df099335362baa60c94fabb9 *R/RcppExports.R
bfbf62c0341836a598bf9c4efc728199 *R/catch-routine-registration.R
4527d6b21d7c1bb4691dde35ee928398 *R/drawing-context.R
33ddf7bf682842d3d1e18a29302f3c38 *R/grid-utils.R
c75f2b410d48d061281f07fa280c3120 *R/gridtext.R
d441b7c0ec23dc1e813e4efc421e4c62 *R/grob-zero.R
0c7639a9d088da143da79eccc7d103e7 *R/parse-css.R
6f2fdb69ac961039af6d814b61dae5d0 *R/process-tags.R
ce37c3f6ac753f22d2a04ec8c9d4a965 *R/read-image.R
14f402b6578dc90c2ccc237fd5985d9a *R/recycle-gpar.R
de07accef40aea5fde11d6f795d4c4c6 *R/richtext-grob.R
993e909e3ca88ea9454c1b6c307fd560 *R/text-details.R
ff0ace7f0b430bf47c7eb84f47478f45 *R/textbox-grob.R
ab3e39d895f6fd0e7234a68d66f445ab *README.md
a424816ce3f3342cdd6254853ef2ff55 *inst/extdata/Rlogo.png
e3095ff8a05aa032723f89f2573c2096 *man/figures/README-unnamed-chunk-4-1.png
fd7571699e1734d3cfcf7aa8ad844e92 *man/figures/README-unnamed-chunk-5-1.png
732201d39f2ad5a97cf8e7be2282b60b *man/figures/README-unnamed-chunk-6-1.png
3401578633e13b48f62e4354b1eb710b *man/figures/README-unnamed-chunk-7-1.png
f77f52e2d6dd2ef49e2092d8cecfb2cc *man/figures/README-unnamed-chunk-8-1.png
5c51af6b80be93402d056d83ef476473 *man/gridtext.Rd
c7faefc907f7a7d886c5bbf8d8595352 *man/richtext_grob.Rd
725c355183ac984c7c998f776f344a7e *man/textbox_grob.Rd
ababd53978f446316efe1ba7e39a04c7 *src/RcppExports.cpp
b768a47be0be837939ac29e35472f98e *src/bl-r-bindings.cpp
d269719f4d854f1631ef0855544996c2 *src/glue.h
37903129a8e47d97fe238f007c62fd75 *src/grid-renderer.cpp
86aeb8f8c4adcbde508769c3489518aa *src/grid-renderer.h
e9003b9fa59db65cb4021f1c4ee1b8e2 *src/grid.cpp
0f83d99b161de0acd132a795b22061be *src/grid.h
b2687846d89a7cf50e9981e1b027780e *src/gridtext_types.h
fe9e6d3d255267f5c38d99d167b434e9 *src/layout.h
33db19a560625010d4974dc4ba5912fa *src/length.h
53d65bd42a588396dd17d073def9a721 *src/line-breaker.h
2fca8c188ccdc7e31da70a673d793992 *src/null-box.h
52a50bc433ecc83670967956e45f6f09 *src/par-box.h
cb46811dca7dc0d664c1c215184dcefd *src/penalty.h
63c950ab0c0fbba33290ff8c42d25a1a *src/raster-box.h
2693123ffb9befabf5ca09312c049e93 *src/rect-box.h
1d066d2d4807a3f0ddc8802153fd5d32 *src/test-layout.cpp
8ecc900f2e32beb627ac8380d69fecc7 *src/test-line-breaker.cpp
c2cb0c30e10da611f9fbe56aea52f7a9 *src/test-runner.cpp
089683ed503534bc7875101e82df704c *src/text-box.h
1231c98d2f4345736a6460869b5341b6 *src/vbox.h
b0ab17eb094c0a55dc40999f1f0c666a *tests/figs/deps.txt
8cde36bedfbb0387360e28927c14c6f3 *tests/figs/grid-renderer/mixing-text-and-boxes.svg
e3b297cab87e270a47e6c1ef66c254a4 *tests/figs/grid-renderer/rendering-raster-data.svg
c54d3410efb2abfb584a3df1898a1149 *tests/figs/grid-renderer/text-in-different-stylings.svg
69101d09beea13709591a9933de86b29 *tests/figs/rich-text-grob/aligned-heights.svg
b693385997bec7757f486fc9587d34aa *tests/figs/rich-text-grob/aligned-widths.svg
b6dc6571ddad9bd0893269af602d0d47 *tests/figs/rich-text-grob/various-text-boxes.svg
69101d09beea13709591a9933de86b29 *tests/figs/richtext-grob/aligned-heights.svg
b693385997bec7757f486fc9587d34aa *tests/figs/richtext-grob/aligned-widths.svg
2b84957ca9a04074e23f6fdb76bc888a *tests/figs/richtext-grob/various-text-boxes-w-debug.svg
b6dc6571ddad9bd0893269af602d0d47 *tests/figs/richtext-grob/various-text-boxes.svg
7c261bce68dff9b34bf9d38343bcf4c3 *tests/figs/text-box-grob/box-spanning-entire-viewport-with-margins.svg
29c1ec20c4fa45fd1701764204804eb2 *tests/figs/textbox-grob/box-spanning-entire-viewport-with-margins.svg
ac3d25a8bb15c8576e1e1be197e5b0ad *tests/figs/textbox-grob/multiple-boxes-internal-alignment.svg
f140c71bab32e5854d2eb9800abd1e49 *tests/figs/textbox-grob/multiple-boxes-inverted-internal-alignment.svg
f2d116c67913c1bbb2c946c4fd631ffd *tests/figs/textbox-grob/multiple-boxes-left-rotated-internal-alignment.svg
8d90a80333398e45985b4afd082bd7dc *tests/figs/textbox-grob/multiple-boxes-right-rotated-internal-alignment.svg
ac47bab71f229d8f1348f10a00972b0e *tests/figs/textbox-grob/rotation-around-fixed-point.svg
7cd33292d3bce2d48077d0ecc3b2a73e *tests/testthat.R
e1d4c0c7540aef60afbb54b54835aa55 *tests/testthat/Rplots.pdf
f514b2d400b37fef7ed436fbdb47d1d2 *tests/testthat/test-cpp.R
a407aef718691f5b73125ae61bcffeee *tests/testthat/test-grid-constructors.R
67d02a9078f7f82db1af5ed8b2fc2962 *tests/testthat/test-grid-renderer.R
f0b68517b0513767dc1eeec8b0211211 *tests/testthat/test-null-box.R
75c2bfc19d05d0489d7c889c6b0080c2 *tests/testthat/test-raster-box.R
d7a122809ddab111394319374c15d4ed *tests/testthat/test-rect-box.R
7f34b456a0f39084c6ed47c8d296b03b *tests/testthat/test-richtext-grob.R
db72fa0f82e6c74c7cedc8ddb6ba9f4c *tests/testthat/test-text-details.R
aa8d6e3decfec890a8dff2e20e02c343 *tests/testthat/test-textbox-grob.R
499e255bd1e6bf22cfbfe465194a03a9 *tests/testthat/test-vbox.R
19 changes: 19 additions & 0 deletions NAMESPACE
@@ -0,0 +1,19 @@
# Generated by roxygen2: do not edit by hand

S3method(ascentDetails,richtext_grob)
S3method(ascentDetails,textbox_grob)
S3method(descentDetails,richtext_grob)
S3method(descentDetails,textbox_grob)
S3method(heightDetails,richtext_grob)
S3method(heightDetails,textbox_grob)
S3method(makeContent,textbox_grob)
S3method(makeContext,textbox_grob)
S3method(widthDetails,richtext_grob)
S3method(widthDetails,textbox_grob)
export(richtext_grob)
export(textbox_grob)
import(grid)
import(rlang)
importFrom(Rcpp,sourceCpp)
importFrom(xml2,read_html)
useDynLib(gridtext, .registration = TRUE)
3 changes: 3 additions & 0 deletions NEWS.md
@@ -0,0 +1,3 @@
# gridtext 0.1.0

First public release. Provides the two grobs `richtext_grob()` and `textbox_grob()` for formatted text rendering without and with word wrapping, respectively.
123 changes: 123 additions & 0 deletions R/RcppExports.R
@@ -0,0 +1,123 @@
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

bl_make_null_box <- function(width_pt = 0, height_pt = 0) {
.Call(`_gridtext_bl_make_null_box`, width_pt, height_pt)
}

bl_make_par_box <- function(node_list, vspacing_pt, width_policy = "native", hjust = NULL) {
.Call(`_gridtext_bl_make_par_box`, node_list, vspacing_pt, width_policy, hjust)
}

bl_make_rect_box <- function(content, width_pt, height_pt, margin, padding, gp, content_hjust = 0, content_vjust = 1, width_policy = "fixed", height_policy = "fixed", r = 0) {
.Call(`_gridtext_bl_make_rect_box`, content, width_pt, height_pt, margin, padding, gp, content_hjust, content_vjust, width_policy, height_policy, r)
}

bl_make_text_box <- function(label, gp, voff_pt = 0) {
.Call(`_gridtext_bl_make_text_box`, label, gp, voff_pt)
}

bl_make_raster_box <- function(image, width_pt = 0, height_pt = 0, width_policy = "native", height_policy = "native", respect_aspect = TRUE, interpolate = TRUE, dpi = 150, gp = NULL) {
.Call(`_gridtext_bl_make_raster_box`, image, width_pt, height_pt, width_policy, height_policy, respect_aspect, interpolate, dpi, gp)
}

bl_make_vbox <- function(node_list, width_pt = 0, hjust = 0, vjust = 1, width_policy = "native") {
.Call(`_gridtext_bl_make_vbox`, node_list, width_pt, hjust, vjust, width_policy)
}

bl_make_regular_space_glue <- function(gp, stretch_ratio = 0.5, shrink_ratio = 0.333333) {
.Call(`_gridtext_bl_make_regular_space_glue`, gp, stretch_ratio, shrink_ratio)
}

bl_make_forced_break_penalty <- function() {
.Call(`_gridtext_bl_make_forced_break_penalty`)
}

bl_make_never_break_penalty <- function() {
.Call(`_gridtext_bl_make_never_break_penalty`)
}

bl_box_width <- function(node) {
.Call(`_gridtext_bl_box_width`, node)
}

bl_box_height <- function(node) {
.Call(`_gridtext_bl_box_height`, node)
}

bl_box_ascent <- function(node) {
.Call(`_gridtext_bl_box_ascent`, node)
}

bl_box_descent <- function(node) {
.Call(`_gridtext_bl_box_descent`, node)
}

bl_box_voff <- function(node) {
.Call(`_gridtext_bl_box_voff`, node)
}

bl_calc_layout <- function(node, width_pt = 0, height_pt = 0) {
invisible(.Call(`_gridtext_bl_calc_layout`, node, width_pt, height_pt))
}

bl_place <- function(node, x_pt, y_pt) {
invisible(.Call(`_gridtext_bl_place`, node, x_pt, y_pt))
}

bl_render <- function(node, x_pt = 0, y_pt = 0) {
.Call(`_gridtext_bl_render`, node, x_pt, y_pt)
}

grid_renderer <- function() {
.Call(`_gridtext_grid_renderer`)
}

grid_renderer_text <- function(gr, label, x, y, gp) {
invisible(.Call(`_gridtext_grid_renderer_text`, gr, label, x, y, gp))
}

grid_renderer_text_details <- function(label, gp) {
.Call(`_gridtext_grid_renderer_text_details`, label, gp)
}

grid_renderer_raster <- function(gr, image, x, y, width, height, interpolate = TRUE) {
invisible(.Call(`_gridtext_grid_renderer_raster`, gr, image, x, y, width, height, interpolate))
}

grid_renderer_rect <- function(gr, x, y, width, height, gp, r = 0L) {
invisible(.Call(`_gridtext_grid_renderer_rect`, gr, x, y, width, height, gp, r))
}

grid_renderer_collect_grobs <- function(gr) {
.Call(`_gridtext_grid_renderer_collect_grobs`, gr)
}

unit_pt <- function(x) {
.Call(`_gridtext_unit_pt`, x)
}

gpar_empty <- function() {
.Call(`_gridtext_gpar_empty`)
}

text_grob <- function(label, x_pt = 0L, y_pt = 0L, gp = NULL, name = NULL) {
.Call(`_gridtext_text_grob`, label, x_pt, y_pt, gp, name)
}

raster_grob <- function(image, x_pt = 0L, y_pt = 0L, width_pt = 0L, height_pt = 0L, interpolate = TRUE, gp = NULL, name = NULL) {
.Call(`_gridtext_raster_grob`, image, x_pt, y_pt, width_pt, height_pt, interpolate, gp, name)
}

rect_grob <- function(x_pt = 0L, y_pt = 0L, width_pt = 0L, height_pt = 0L, gp = NULL, name = NULL) {
.Call(`_gridtext_rect_grob`, x_pt, y_pt, width_pt, height_pt, gp, name)
}

roundrect_grob <- function(x_pt = 0L, y_pt = 0L, width_pt = 0L, height_pt = 0L, r_pt = 5L, gp = NULL, name = NULL) {
.Call(`_gridtext_roundrect_grob`, x_pt, y_pt, width_pt, height_pt, r_pt, gp, name)
}

set_grob_coords <- function(grob, x, y) {
.Call(`_gridtext_set_grob_coords`, grob, x, y)
}

6 changes: 6 additions & 0 deletions R/catch-routine-registration.R
@@ -0,0 +1,6 @@
# This dummy function definition is included with the package to ensure that
# 'tools::package_native_routine_registration_skeleton()' generates the required
# registration info for the 'run_testthat_tests' symbol.
(function() {
.Call("run_testthat_tests", PACKAGE = "gridtext")
})
86 changes: 86 additions & 0 deletions R/drawing-context.R
@@ -0,0 +1,86 @@
# create drawing context with defined state
# halign defines horizontal text alignment (0 = left aligned, 0.5 = centered, 1 = right aligned)
setup_context <- function(fontsize = 12, fontfamily = "", fontface = "plain", color = "black",
lineheight = 1.2, halign = 0, word_wrap = TRUE, gp = NULL) {
if (is.null(gp)) {
gp <- gpar(
fontsize = fontsize, fontfamily = fontfamily, fontface = fontface,
col = color, cex = 1, lineheight = lineheight
)
}
gp <- update_gpar(get.gpar(), gp)

set_context_gp(list(yoff_pt = 0, halign = halign, word_wrap = word_wrap), gp)
}

# update a given drawing context with the values provided via ...
update_context <- function(drawing_context, ...) {
dc_new <- list(...)
names_new <- names(dc_new)
names_old <- names(drawing_context)
drawing_context[intersect(names_old, names_new)] <- NULL
c(drawing_context, dc_new)
}

set_style <- function(drawing_context, style = NULL) {
if (is.null(style)) return(drawing_context)

css <- parse_css(style)

if (!is.null(css$`font-size`)) {
font_size = convert_css_unit_pt(css$`font-size`)
} else {
font_size = NULL
}

drawing_context <- set_context_gp(
drawing_context,
gpar(col = css$color, fontfamily = css$`font-family`, fontsize = font_size)
)
}


# helper functions --------------------------------------------------------

# update a gpar object with new values
update_gpar <- function(gp, gp_new) {
names_new <- names(gp_new)
names_old <- names(gp)
gp[c(intersect(names_old, names_new), "font")] <- NULL
gp_new["font"] <- NULL
do.call(gpar, c(gp, gp_new))
}

# update the gpar object of a drawing context
set_context_gp <- function(drawing_context, gp = NULL) {
gp <- update_gpar(drawing_context$gp, gp)
font_info <- text_details("", gp)
linespacing_pt <- gp$lineheight * gp$fontsize
em_pt <- gp$fontsize

update_context(
drawing_context,
gp = gp,
ascent_pt = font_info$ascent_pt,
descent_pt = font_info$descent_pt,
linespacing_pt = linespacing_pt,
em_pt = em_pt
)
}

# update the fontface of a drawing context
set_context_fontface <- function(drawing_context, fontface = "plain", overwrite = FALSE) {
fontface_old <- drawing_context$gp$fontface

# combine bold and italic if needed
if (!isTRUE(overwrite)) {
if (isTRUE(fontface == "italic") && isTRUE(fontface_old == "bold")) {
fontface <- "bold.italic"
} else if (isTRUE(fontface == "bold") && isTRUE(fontface_old == "italic")) {
fontface <- "bold.italic"
}
}

set_context_gp(drawing_context, gpar(fontface = fontface))
}

0 comments on commit 88d3845

Please sign in to comment.