-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathglyph.R
More file actions
268 lines (249 loc) · 8.83 KB
/
Copy pathglyph.R
File metadata and controls
268 lines (249 loc) · 8.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
## Generate object containing information about typeset glyphs
## All x/y and width/height and anchors should be in "big" pts (1/72 inch)
mapCharWeight <- function(x) {
if (is.na(x))
x
else
switch(as.character(x),
normal=400,
bold=700,
as.numeric(x))
}
mapWeight <- function(x) {
if (is.numeric(x)) {
if (min(x, na.rm=TRUE) < 0 || max(x, na.rm=TRUE) > 1000)
stop("Invalid span weight")
x
} else {
sapply(x, mapCharWeight, USE.NAMES=FALSE)
}
}
fontStyles <- c("normal", "italic", "oblique")
mapStyle <- function(x) {
## NA passes through
match(x, fontStyles)
}
invertStyle <- function(x) {
fontStyles[x]
}
################################################################################
## glyph dimensions, anchors, and justification
glyphWidth <- function(w, label="width", left="left") {
if (!length(w) ||
length(w) != length(label) ||
length(label) != length(left))
stop("length of arguments must match (and be greater than 0)")
w <- as.numeric(w)
if (any(!is.finite(w))) stop("Invalid glyph width(s)")
names(w) <- as.character(label)
attr(w, "anchor") <- as.character(left)
class(w) <- "GlyphWidth"
w
}
glyphWidthLeft <- function(w, label) {
if (!inherits(w, "GlyphWidth")) stop("Invalid glyph width")
if (!label %in% names(w)) {
warning("Unknown width; using left anchor")
return("left")
}
which <- match(label, names(w))
attr(w, "anchor")[which]
}
glyphHeight <- function(h, label="height", bottom="bottom") {
if (!length(h) ||
length(h) != length(label) ||
length(label) != length(bottom))
stop("length of arguments must match (and be greater than 0)")
h <- as.numeric(h)
if (any(!is.finite(h))) stop("Invalid glyph height(s)")
names(h) <- as.character(label)
attr(h, "anchor") <- as.character(bottom)
class(h) <- "GlyphHeight"
h
}
glyphHeightBottom <- function(h, label) {
if (!inherits(h, "GlyphHeight")) stop("Invalid glyph height")
if (!label %in% names(h)) {
warning("Unknown height; using bottom anchor")
return("bottom")
}
which <- match(label, names(h))
attr(h, "anchor")[which]
}
glyphAnchor <- function(value, label) {
if (!length(value) ||
length(value) != length(label))
stop("length of arguments must match (and be greater than 0)")
value <- as.numeric(value)
if (any(!is.finite(value))) stop("Invalid glyph anchor")
names(value) <- as.character(label)
class(value) <- "GlyphAnchor"
value
}
glyphJust <- function(just, ...) {
UseMethod("glyphJust")
}
glyphJust.GlyphJust <- function(just, ...) {
just
}
glyphJust.character <- function(just, ...) {
class(just) <- "GlyphJust"
just
}
glyphJust.numeric <- function(just, which=NULL, ...) {
if (is.null(which)) {
which <- names(just)
}
names(just) <- which
class(just) <- "GlyphJust"
just
}
################################################################################
## glyph font
glyphFont <- function(file, index,
family, weight, style,
PSname=NA) {
file <- as.character(file)
if ( !file.exists(file)) {
stop("Font file does not exist")
}
nafile <- is.na(file)
if (any(nchar(file[!nafile], "bytes") > 500))
warning("Font file longer than 500 will be truncated")
index <- as.integer(index)
family <- as.character(family)
nafamily <- is.na(family)
if (any(nchar(family[!nafamily], "bytes") > 200))
warning("Font family longer than 200 will be truncated")
weight <- mapWeight(weight)
style <- mapStyle(style)
PSname <- as.character(PSname)
## Missing PSname values are "estimated"
naPS <- is.na(PSname)
if (any(naPS)) {
PSbold <- ifelse(weight >= 700, "Bold", "")
PSstyle <- ifelse(style > 1,
ifelse(style > 2, "Oblique", "Italic"),
"")
face <- paste0(PSbold, PSstyle)
PSname[naPS] <-
ifelse(nchar(file[naPS]),
sub("([^.]+)\\.[[:alnum:]]+$", "\\1",
basename(file[naPS])),
paste0(family[naPS],
ifelse(nchar(PSstyle), paste0("-", PSstyle), "")))
}
if (any(nchar(PSname, "bytes") > 200))
warning("PostScript font name longer than 200 will be truncated")
## Check that family-weight-style and file and PSname all line up
families <- rle(paste0(family, weight, style))$lengths
files <- rle(file)$lengths
names <- rle(PSname)$lengths
if (!(all(families == files) && all(files == names)))
stop("Font information is inconsistent")
font <- list(file=file, index=index,
family=family, weight=weight, style=style,
PSname=PSname)
class(font) <- "RGlyphFont"
font
}
print.RGlyphFont <- function(x, ...) {
cat(paste0(x$family, " wgt: ", x$weight, " style: ", invertStyle(x$style),
"\n (", x$file, " [", x$index, "])\n"))
}
glyphFontList <- function(...) {
fonts <- list(...)
if (!length(fonts))
stop("List must include at least one font")
if (!all(sapply(fonts, function(x) inherits(x, "RGlyphFont"))))
stop("Invalid glyph font")
class(fonts) <- "RGlyphFontList"
fonts
}
################################################################################
## glyph information
glyphInfo <- function(id, x, y, font, size,
fontList,
width, height,
hAnchor, vAnchor,
col=NA, rot=0) {
id <- as.integer(id)
x <- as.numeric(x)
y <- as.numeric(y)
## Check font
font <- as.integer(font)
if (!inherits(fontList, "RGlyphFontList"))
stop("Invalid font list")
if (any(is.na(font)) || !all(font %in% seq_along(fontList)))
stop("Unknown font")
size <- as.numeric(size)
rot <- as.numeric(rot)
## Check colour (allow any R colour spec)
nacol <- is.na(col)
if (any(!nacol)) {
rgb <- col2rgb(col[!nacol], alpha=TRUE)
col[!nacol] <- rgb(rgb[1,], rgb[2,], rgb[3,], rgb[4,],
maxColorValue=255)
}
## Check width/height
if (!inherits(width, "GlyphWidth"))
width <- glyphWidth(width)
if (!inherits(height, "GlyphHeight"))
height <- glyphHeight(height)
## Check anchors (and provide defaults if missing)
if (missing(hAnchor))
hAnchor <- glyphAnchor(c(min(x), min(x) + width[1],
min(x) + width[1]/2),
label=c("left", "right", "centre"))
if (missing(vAnchor))
vAnchor <- glyphAnchor(c(min(y), min(y) + height[1],
min(y) + height[1]/2),
label=c("bottom", "top", "centre"))
if (!inherits(hAnchor, "GlyphAnchor"))
hAnchor <- glyphAnchor(hAnchor, names(hAnchor))
if (!inherits(vAnchor, "GlyphAnchor"))
vAnchor <- glyphAnchor(vAnchor, names(vAnchor))
hNames <- names(hAnchor)
vNames <- names(vAnchor)
if (!("left" %in% hNames && "bottom" %in% vNames))
stop('There must be anchors named "left" and "bottom"')
if (!"right" %in% hNames)
hAnchor <- c(hAnchor,
right=unname(hAnchor["left"]) + unname(width[1]))
if (!"top" %in% vNames)
vAnchor <- c(vAnchor,
top=unname(vAnchor["bottom"]) + unname(height[1]))
if (!"centre" %in% hNames)
hAnchor <- c(hAnchor,
centre=unname(hAnchor["left"]) + unname(width[1]/2))
if (!"centre" %in% vNames)
vAnchor <- c(vAnchor,
centre=unname(vAnchor["bottom"]) + unname(height[1]/2))
if (!"center" %in% hNames)
hAnchor <- c(hAnchor, center=unname(hAnchor["centre"]))
if (!"center" %in% vNames)
vAnchor <- c(vAnchor, center=unname(vAnchor["centre"]))
## Build glyph info
dropNA <- !(is.na(id) | is.na(x) | is.na(y) |
## is.na(font) already checked
is.na(size) | is.na(rot))
glyphs <- data.frame(id, x, y, font, size, rot)[dropNA, ]
if (nrow(glyphs) < 1)
stop("Invalid glyph info")
## Colour can be NA
if (inherits(glyphs, "omit")) {
glyphs$colour <- col[-attr(glyphs, "na.action")]
} else {
glyphs$colour <- col
}
## Reorder to ensure backwards compatibility with code
## where rot was not yet included.
col_order <- c("id", "x", "y", "font", "size", "colour", "rot")
glyphs <- glyphs[, col_order, drop = FALSE]
## Construct final structure
info <- list(glyphs=glyphs, fonts=fontList,
width=width, height=height,
hAnchor=hAnchor, vAnchor=vAnchor)
class(info) <- c("RGlyphInfo")
info
}