Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #181 center label when angles are used #182

Merged
merged 1 commit into from
Oct 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 54 additions & 51 deletions R/font.R
Original file line number Diff line number Diff line change
@@ -1,51 +1,54 @@
#' @title Font
#' @description R6 class defining font properties
#' @field size numeric defining the size of font
#' @field color character defining the color of font
#' @field fontFamily character defining the family of font
#' @field fontFace character defining the face of font
#' @field angle numeric defining the angle of font
#' @export
Font <- R6::R6Class(
"Font",
public = list(
size = 12,
color = "black",
fontFamily = "",
fontFace = "plain",
angle = 0,

#' @description Create a new `Font` object.
#' Default font properties are defined directly in the object field,
#' so `NULL` input is allowed will lead to default properties.
#' @param size numeric defining the size of font
#' @param color character defining the color of font
#' @param fontFamily character defining the family of font
#' @param fontFace character defining the face of font
#' @param angle numeric defining the angle of font
#' @return A new `Font` object
initialize = function(size = NULL,
color = NULL,
fontFamily = NULL,
fontFace = NULL,
angle = NULL) {
validateIsString(c(color, fontFamily, fontFace), nullAllowed = TRUE)
validateIsNumeric(c(size, angle), nullAllowed = TRUE)
eval(parseVariableToObject("self", c("size", "color", "fontFace", "fontFamily", "angle"), keepIfNull = TRUE))
},

#' @description Create a `ggplot2::element_text` directly convertible by `ggplot2::theme`.
#' @return An `element_text` object.
createPlotFont = function() {
ggplot2::element_text(
colour = self$color,
size = self$size,
face = self$fontFace,
# TODO: check why I get the following error messages
# "font family not found in Windows font database"
# family = self$fontFamily,
angle = self$angle
)
}
)
)
#' @title Font
#' @description R6 class defining font properties
#' @field size numeric defining the size of font
#' @field color character defining the color of font
#' @field fontFamily character defining the family of font
#' @field fontFace character defining the face of font
#' @field angle numeric defining the angle of font
#' @export
Font <- R6::R6Class(
"Font",
public = list(
size = 12,
color = "black",
fontFamily = "",
fontFace = "plain",
angle = 0,

#' @description Create a new `Font` object.
#' Default font properties are defined directly in the object field,
#' so `NULL` input is allowed will lead to default properties.
#' @param size numeric defining the size of font
#' @param color character defining the color of font
#' @param fontFamily character defining the family of font
#' @param fontFace character defining the face of font
#' @param angle numeric defining the angle of font
#' @return A new `Font` object
initialize = function(size = NULL,
color = NULL,
fontFamily = NULL,
fontFace = NULL,
angle = NULL) {
validateIsString(c(color, fontFamily, fontFace), nullAllowed = TRUE)
validateIsNumeric(c(size, angle), nullAllowed = TRUE)
eval(parseVariableToObject("self", c("size", "color", "fontFace", "fontFamily", "angle"), keepIfNull = TRUE))
},

#' @description Create a `ggplot2::element_text` directly convertible by `ggplot2::theme`.
#' @return An `element_text` object.
createPlotFont = function() {
ggplot2::element_text(
colour = self$color,
size = self$size,
face = self$fontFace,
# TODO: check why I get the following error messages
# "font family not found in Windows font database"
# family = self$fontFamily,
angle = self$angle,
# Center the label even with angle
vjust = 0.5,
hjust = 0.5
)
}
)
)