Skip to content

Commit

Permalink
Merge pull request #542 from trekonom/issue-443-165
Browse files Browse the repository at this point in the history
Add support for all allowed line border styles to fp_border
  • Loading branch information
davidgohel authored Jan 20, 2024
2 parents 312daa5 + 1d1ad5d commit 930cc93
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 6 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# officer 0.6.3.9000

## Features

- `fp_border()` gains support for all line border styles listed in ECMA-376
§ 17.18.2 and allowed CSS border styles. Closes #165 and #443.

## Changes

- the dataframe returned by `docx_comments()` gains a list column `para_id` containing
Expand Down
55 changes: 53 additions & 2 deletions R/formatting_properties.R
Original file line number Diff line number Diff line change
Expand Up @@ -359,15 +359,66 @@ update.fp_text <- function(object, color, font.size,
}

# fp_border ----
border_styles = c("none", "solid", "dotted", "dashed")
border_styles = c(
"nil", "none", "solid", "single", "thick", "double", "dotted", "dashed",
"dotDash", "dotDotDash", "triple", "thinThickSmallGap", "thickThinSmallGap",
"thinThickThinSmallGap", "thinThickMediumGap", "thickThinMediumGap",
"thinThickThinMediumGap", "thinThickLargeGap", "thickThinLargeGap",
"thinThickThinLargeGap", "wave", "doubleWave", "dashSmallGap",
"dashDotStroked", "threeDEmboss", "ridge", "threeDEngrave", "groove",
"outset", "inset"
)

#' @title Border properties object
#'
#' @description create a border properties object.
#'
#' @param color border color - single character value (e.g. "#000000" or "black")
#' @param style border style - single character value : "none" or "solid" or "dotted" or "dashed"
#' @param style border style - single character value : See Details for supported
#' border styles.
#' @param width border width - an integer value : 0>= value
#' @details For Word output the following border styles are supported:
#'
#' * "none" or "nil" - No Border
#' * "solid" or "single" - Single Line Border
#' * "thick" - Single Line Border
#' * "double" - Double Line Border
#' * "dotted" - Dotted Line Border
#' * "dashed" - Dashed Line Border
#' * "dotDash" - Dot Dash Line Border
#' * "dotDotDash" - Dot Dot Dash Line Border
#' * "triple" - Triple Line Border
#' * "thinThickSmallGap" - Thin, Thick Line Border
#' * "thickThinSmallGap" - Thick, Thin Line Border
#' * "thinThickThinSmallGap" - Thin, Thick, Thin Line Border
#' * "thinThickMediumGap" - Thin, Thick Line Border
#' * "thickThinMediumGap" - Thick, Thin Line Border
#' * "thinThickThinMediumGap" - Thin, Thick, Thin Line Border
#' * "thinThickLargeGap" - Thin, Thick Line Border
#' * "thickThinLargeGap" - Thick, Thin Line Border
#' * "thinThickThinLargeGap" - Thin, Thick, Thin Line Border
#' * "wave" - Wavy Line Border
#' * "doubleWave" - Double Wave Line Border
#' * "dashSmallGap" - Dashed Line Border
#' * "dashDotStroked" - Dash Dot Strokes Line Border
#' * "threeDEmboss" or "ridge" - 3D Embossed Line Border
#' * "threeDEngrave" or "groove" - 3D Engraved Line Border
#' * "outset" - Outset Line Border
#' * "inset" - Inset Line Border
#'
#' For HTML output only a limited amount of border styles are supported:
#'
#' * "none" or "nil" - No Border
#' * "solid" or "single" - Single Line Border
#' * "double" - Double Line Border
#' * "dotted" - Dotted Line Border
#' * "dashed" - Dashed Line Border
#' * "threeDEmboss" or "ridge" - 3D Embossed Line Border
#' * "threeDEngrave" or "groove" - 3D Engraved Line Border
#' * "outset" - Outset Line Border
#' * "inset" - Inset Line Border
#'
#' Non-supported Word border styles will default to "solid".
#' @examples
#' fp_border()
#' fp_border(color="orange", style="solid", width=1)
Expand Down
13 changes: 10 additions & 3 deletions R/ooxml.R
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,15 @@ border_pml <- function(x, side){
border_wml <- function(x, side){
tagname <- paste0("w:", side)
x$style[x$style %in% "solid"] <- "single"
if( !x$style %in% c("dotted", "dashed", "single") ){
x$style[x$style %in% "ridge"] <- "threeDEmboss"
x$style[x$style %in% "groove"] <- "threeDEngrave"
if( !x$style %in% border_styles ){
x$style <- "single"
}
if( x$width < 0.0001 || is_transparent(x$color) ){
x$style <- "none"
}


style_ <- sprintf("w:val=\"%s\"", x$style)
width_ <- sprintf("w:sz=\"%.0f\"", x$width*8)
color_ <- sprintf("w:color=\"%s\"", hex_color(x$color))
Expand All @@ -236,7 +237,13 @@ border_css <- function(x, side){

width_ <- sprintf("%.02fpt", x$width)

if( !x$style %in% c("dotted", "dashed", "solid") ){
x$style[x$style %in% "threeDEmboss"] <- "ridge"
x$style[x$style %in% "threeDEngrave"] <- "groove"
x$style[x$style %in% "nil"] <- "none"

if( !x$style %in% c("dotted", "dashed", "solid",
"double", "inset", "outset",
"ridge", "groove", "none") ){
x$style <- "solid"
}
paste0("border-", side, ": ", width_, " ", x$style, " ", color_, ";")
Expand Down
49 changes: 48 additions & 1 deletion man/fp_border.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 94 additions & 0 deletions tests/testthat/test-fp_border.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,60 @@ test_that("wml fp_border", {
node <- wml_border_node(fp_border(style = "dashed"))
expect_equal(xml_attr(node, "val"), "dashed")

node <- wml_border_node(fp_border(style = "nil"))
expect_equal(xml_attr(node, "val"), "nil")
node <- wml_border_node(fp_border(style = "none"))
expect_equal(xml_attr(node, "val"), "none")
node <- wml_border_node(fp_border(style = "single"))
expect_equal(xml_attr(node, "val"), "single")
node <- wml_border_node(fp_border(style = "thick"))
expect_equal(xml_attr(node, "val"), "thick")
node <- wml_border_node(fp_border(style = "double"))
expect_equal(xml_attr(node, "val"), "double")
node <- wml_border_node(fp_border(style = "dotDash"))
expect_equal(xml_attr(node, "val"), "dotDash")
node <- wml_border_node(fp_border(style = "dotDotDash"))
expect_equal(xml_attr(node, "val"), "dotDotDash")
node <- wml_border_node(fp_border(style = "triple"))
expect_equal(xml_attr(node, "val"), "triple")
node <- wml_border_node(fp_border(style = "thinThickSmallGap"))
expect_equal(xml_attr(node, "val"), "thinThickSmallGap")
node <- wml_border_node(fp_border(style = "thickThinSmallGap"))
expect_equal(xml_attr(node, "val"), "thickThinSmallGap")
node <- wml_border_node(fp_border(style = "thinThickThinSmallGap"))
expect_equal(xml_attr(node, "val"), "thinThickThinSmallGap")
node <- wml_border_node(fp_border(style = "thinThickMediumGap"))
expect_equal(xml_attr(node, "val"), "thinThickMediumGap")
node <- wml_border_node(fp_border(style = "thickThinMediumGap"))
expect_equal(xml_attr(node, "val"), "thickThinMediumGap")
node <- wml_border_node(fp_border(style = "thinThickThinMediumGap"))
expect_equal(xml_attr(node, "val"), "thinThickThinMediumGap")
node <- wml_border_node(fp_border(style = "thinThickLargeGap"))
expect_equal(xml_attr(node, "val"), "thinThickLargeGap")
node <- wml_border_node(fp_border(style = "thickThinLargeGap"))
expect_equal(xml_attr(node, "val"), "thickThinLargeGap")
node <- wml_border_node(fp_border(style = "thinThickThinLargeGap"))
expect_equal(xml_attr(node, "val"), "thinThickThinLargeGap")
node <- wml_border_node(fp_border(style = "wave"))
expect_equal(xml_attr(node, "val"), "wave")
node <- wml_border_node(fp_border(style = "doubleWave"))
expect_equal(xml_attr(node, "val"), "doubleWave")
node <- wml_border_node(fp_border(style = "dashSmallGap"))
expect_equal(xml_attr(node, "val"), "dashSmallGap")
node <- wml_border_node(fp_border(style = "dashDotStroked"))
expect_equal(xml_attr(node, "val"), "dashDotStroked")
node <- wml_border_node(fp_border(style = "threeDEmboss"))
expect_equal(xml_attr(node, "val"), "threeDEmboss")
node <- wml_border_node(fp_border(style = "threeDEngrave"))
expect_equal(xml_attr(node, "val"), "threeDEngrave")
node <- wml_border_node(fp_border(style = "ridge"))
expect_equal(xml_attr(node, "val"), "threeDEmboss")
node <- wml_border_node(fp_border(style = "groove"))
expect_equal(xml_attr(node, "val"), "threeDEngrave")
node <- wml_border_node(fp_border(style = "outset"))
expect_equal(xml_attr(node, "val"), "outset")
node <- wml_border_node(fp_border(style = "inset"))
expect_equal(xml_attr(node, "val"), "inset")
})


Expand All @@ -91,6 +145,10 @@ test_that("css fp_border", {
expect_true( rb )

col <- "#00FF0099"
x <- fp_border(width = 4, color = col, style = "single")
rb <- regexp_border(4, "solid", "rgba\\(0,255,0,0.60\\)", x)
expect_true( rb )

x <- fp_border(width = 4, color = col, style = "dashed")
rb <- regexp_border(4, "dashed", "rgba\\(0,255,0,0.60\\)", x)
expect_true( rb )
Expand All @@ -99,6 +157,42 @@ test_that("css fp_border", {
rb <- regexp_border(4, "dotted", "rgba\\(0,255,0,0.60\\)", x)
expect_true( rb )

x <- fp_border(width = 4, color = col, style = "double")
rb <- regexp_border(4, "double", "rgba\\(0,255,0,0.60\\)", x)
expect_true( rb )

x <- fp_border(width = 4, color = col, style = "inset")
rb <- regexp_border(4, "inset", "rgba\\(0,255,0,0.60\\)", x)
expect_true( rb )

x <- fp_border(width = 4, color = col, style = "outset")
rb <- regexp_border(4, "outset", "rgba\\(0,255,0,0.60\\)", x)
expect_true( rb )

x <- fp_border(width = 4, color = col, style = "none")
rb <- regexp_border(4, "none", "rgba\\(0,255,0,0.60\\)", x)
expect_true( rb )

x <- fp_border(width = 4, color = col, style = "nil")
rb <- regexp_border(4, "none", "rgba\\(0,255,0,0.60\\)", x)
expect_true( rb )

x <- fp_border(width = 4, color = col, style = "threeDEmboss")
rb <- regexp_border(4, "ridge", "rgba\\(0,255,0,0.60\\)", x)
expect_true( rb )

x <- fp_border(width = 4, color = col, style = "ridge")
rb <- regexp_border(4, "ridge", "rgba\\(0,255,0,0.60\\)", x)
expect_true( rb )

x <- fp_border(width = 4, color = col, style = "threeDEngrave")
rb <- regexp_border(4, "groove", "rgba\\(0,255,0,0.60\\)", x)
expect_true( rb )

x <- fp_border(width = 4, color = col, style = "groove")
rb <- regexp_border(4, "groove", "rgba\\(0,255,0,0.60\\)", x)
expect_true( rb )

x <- fp_border(color = "transparent")
rb <- regexp_border(1, "solid", "transparent", x)
expect_true( rb )
Expand Down

0 comments on commit 930cc93

Please sign in to comment.