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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
|
# frozen_string_literal: true
module TTFunk
class Table
class Cff < TTFunk::Table
# CFF Charstring.
class Charstring
# Type 2 charstring operators
CODE_MAP = {
1 => :hstem,
3 => :vstem,
4 => :vmoveto,
5 => :rlineto,
6 => :hlineto,
7 => :vlineto,
8 => :rrcurveto,
10 => :callsubr,
12 => :flex_select,
14 => :endchar,
18 => :hstemhm,
19 => :hintmask,
20 => :cntrmask,
21 => :rmoveto,
22 => :hmoveto,
23 => :vstemhm,
24 => :rcurveline,
25 => :rlinecurve,
26 => :vvcurveto,
27 => :hhcurveto,
28 => :shortint,
29 => :callgsubr,
30 => :vhcurveto,
31 => :hvcurveto,
}.freeze
# Type 2 Flex operators.
FLEX_CODE_MAP = {
35 => :flex,
34 => :hflex,
36 => :hflex1,
37 => :flex1,
}.freeze
# Glyph ID.
# @return [Integer]
attr_reader :glyph_id
# Encoded charstring.
# @return [String]
attr_reader :raw
# @param glyph_id [Integer]
# @param top_dict [TTFunk::Table:Cff::TopDict]
# @param font_dict [TTFunk::Table:Cff::FontDict]
# @param raw [String]
def initialize(glyph_id, top_dict, font_dict, raw)
@glyph_id = glyph_id
@top_dict = top_dict
@font_dict = font_dict
@raw = raw
default_width_x = (@font_dict || @top_dict).private_dict.default_width_x
@nominal_width_x = (@font_dict || @top_dict).private_dict.nominal_width_x
@subrs = (@font_dict || @top_dict).private_dict.subr_index
@gsubrs = @top_dict.cff.global_subr_index
@subrs_bias = @subrs.bias if @subrs
@gsubrs_bias = @gsubrs.bias if @gsubrs
@stack = []
@data = raw.bytes
@index = 0
@n_stems = 0
@have_width = false
@open = false
@width = default_width_x
@x = 0
@y = 0
end
# Get path representation of this charstring.
#
# @return [TTFunk::Table::Cff::Path]
def path
@path || begin
@path = Path.new
parse!
@path
end
end
# Get a TrueType-compatible glyph representation of this charstring.
#
# @return [TTFunk::Table::Glyf::PathBased]
def glyph
@glyph ||=
begin
horizontal_metrics = @top_dict.file.horizontal_metrics.for(glyph_id)
Glyf::PathBased.new(path, horizontal_metrics)
end
end
# Get path representation of this charstring at the specified font size.
#
# @param x [Integer, Float] new horizontal position.
# @param y [Integer, Float] new vertical position.
# @param font_size [Integer, Float] font size.
# @return [TTFunk::Table::Cff::Path]
def render(x: 0, y: 0, font_size: 72)
path.render(
x: x,
y: y,
font_size: font_size,
units_per_em: @top_dict.file.header.units_per_em,
)
end
private
def parse!
until @index >= @data.size
code = @data[@index]
@index += 1
# return from callgsubr - do nothing since we inline subrs
next if code == 11
if code >= 32 && code <= 246
@stack << (code - 139)
elsif (m = CODE_MAP[code])
__send__(m)
elsif code >= 247 && code <= 250
b0 = code
b1 = @data[@index]
@index += 1
@stack << (((b0 - 247) * 256) + b1 + 108)
elsif code >= 251 && code <= 254
b0 = code
b1 = @data[@index]
@index += 1
@stack << ((-(b0 - 251) * 256) - b1 - 108)
else
b1, b2, b3, b4 = read_bytes(4)
@stack << (((b1 << 24) | (b2 << 16) | (b3 << 8) | b4) / 65_536)
end
end
end
def read_bytes(length)
bytes = @data[@index, length]
@index += length
bytes
end
def hstem
stem
end
def vstem
stem
end
# rubocop:disable Style/EvenOdd
def stem
# The number of stem operators on the stack is always even.
# If the value is uneven, that means a width is specified.
has_width_arg = @stack.size % 2 == 1
if has_width_arg && !@have_width
@width = @stack.shift + @nominal_width_x
end
@n_stems += @stack.length >> 1
@stack.clear
@have_width = true
end
# rubocop:enable Style/EvenOdd
def vmoveto
if @stack.size > 1 && !@have_width
@width = @stack.shift + @nominal_width_x
@have_width = true
end
@y += @stack.pop
add_contour(@x, @y)
end
def add_contour(x, y)
if @open
@path.close_path
end
@path.move_to(x, y)
@open = true
end
def rlineto
until @stack.empty?
@x += @stack.shift
@y += @stack.shift
@path.line_to(@x, @y)
end
end
def hlineto
until @stack.empty?
@x += @stack.shift
@path.line_to(@x, @y)
break if @stack.empty?
@y += @stack.shift
@path.line_to(@x, @y)
end
end
def vlineto
until @stack.empty?
@y += @stack.shift
@path.line_to(@x, @y)
break if @stack.empty?
@x += @stack.shift
@path.line_to(@x, @y)
end
end
def rrcurveto
until @stack.empty?
c1x = @x + @stack.shift
c1y = @y + @stack.shift
c2x = c1x + @stack.shift
c2y = c1y + @stack.shift
@x = c2x + @stack.shift
@y = c2y + @stack.shift
@path.curve_to(c1x, c1y, c2x, c2y, @x, @y)
end
end
def callsubr
code_index = @stack.pop + @subrs_bias
subr_code = @subrs[code_index].bytes
@data[@index, 0] = subr_code
end
def flex_select
flex_code = @data[@index]
@index += 1
__send__(FLEX_CODE_MAP[flex_code])
end
def flex
c1x = @x + @stack.shift # dx1
c1y = @y + @stack.shift # dy1
c2x = c1x + @stack.shift # dx2
c2y = c1y + @stack.shift # dy2
jpx = c2x + @stack.shift # dx3
jpy = c2y + @stack.shift # dy3
c3x = jpx + @stack.shift # dx4
c3y = jpy + @stack.shift # dy4
c4x = c3x + @stack.shift # dx5
c4y = c3y + @stack.shift # dy5
@x = c4x + @stack.shift # dx6
@y = c4y + @stack.shift # dy6
@stack.shift # flex depth
@path.curve_to(c1x, c1y, c2x, c2y, jpx, jpy)
@path.curve_to(c3x, c3y, c4x, c4y, @x, @y)
end
def hflex
c1x = @x + @stack.shift # dx1
c1y = @y # dy1
c2x = c1x + @stack.shift # dx2
c2y = c1y + @stack.shift # dy2
jpx = c2x + @stack.shift # dx3
jpy = c2y # dy3
c3x = jpx + stack.shift # dx4
c3y = c2y # dy4
c4x = c3x + stack.shift # dx5
c4y = @y # dy5
@x = c4x + stack.shift # dx6
@path.curve_to(c1x, c1y, c2x, c2y, jpx, jpy)
@path.curve_to(c3x, c3y, c4x, c4y, @x, @y)
end
def hflex1
c1x = @x + @stack.shift # dx1
c1y = @y + @stack.shift # dy1
c2x = c1x + @stack.shift # dx2
c2y = c1y + @stack.shift # dy2
jpx = c2x + @stack.shift # dx3
jpy = c2y # dy3
c3x = jpx + @stack.shift # dx4
c3y = c2y # dy4
c4x = c3x + @stack.shift # dx5
c4y = c3y + @stack.shift # dy5
@x = c4x + @stack.shift # dx6
@path.curve_to(c1x, c1y, c2x, c2y, jpx, jpy)
@path.curve_to(c3x, c3y, c4x, c4y, @x, @y)
end
def flex1
c1x = @x + @stack.shift # dx1
c1y = @y + @stack.shift # dy1
c2x = c1x + @stack.shift # dx2
c2y = c1y + @stack.shift # dy2
jpx = c2x + @stack.shift # dx3
jpy = c2y + @stack.shift # dy3
c3x = jpx + @stack.shift # dx4
c3y = jpy + @stack.shift # dy4
c4x = c3x + @stack.shift # dx5
c4y = c3y + @stack.shift # dy5
if (c4x - @x).abs > (c4y - @y).abs
@x = c4x + @stack.shift
else
@y = c4y + @stack.shift
end
@path.curve_to(c1x, c1y, c2x, c2y, jpx, jpy)
@path.curve_to(c3x, c3y, c4x, c4y, @x, @y)
end
def endchar
if !@stack.empty? && !@have_width
@width = @stack.shift + @nominal_width_x
@have_width = true
end
if @open
@path.close_path
@open = false
end
end
def hstemhm
stem
end
def hintmask
cntrmask
end
def cntrmask
stem
# skip past hinting data, no need to worry about it since we're not
# very concerned about rendering glyphs
read_bytes((@n_stems + 7) >> 3)
end
def rmoveto
if @stack.size > 2 && !@have_width
@width = @stack.shift + @nominal_width_x
@have_width = true
end
@y += @stack.pop
@x += @stack.pop
add_contour(@x, @y)
end
def hmoveto
if @stack.size > 1 && !@have_width
@width = @stack.shift + @nominal_width_x
@have_width = true
end
@x += @stack.pop
add_contour(@x, @y)
end
def vstemhm
stem
end
def rcurveline
while @stack.size > 2
c1x = @x + @stack.shift
c1y = @y + @stack.shift
c2x = c1x + @stack.shift
c2y = c1y + @stack.shift
@x = c2x + @stack.shift
@y = c2y + @stack.shift
@path.curve_to(c1x, c1y, c2x, c2y, @x, @y)
end
@x += @stack.shift
@y += @stack.shift
@path.line_to(@x, @y)
end
def rlinecurve
while @stack.size > 6
@x += @stack.shift
@y += @stack.shift
@path.line_to(@x, @y)
end
c1x = @x + @stack.shift
c1y = @y + @stack.shift
c2x = c1x + @stack.shift
c2y = c1y + @stack.shift
@x = c2x + @stack.shift
@y = c2y + @stack.shift
@path.curve_to(c1x, c1y, c2x, c2y, @x, @y)
end
# rubocop:disable Style/EvenOdd
def vvcurveto
if @stack.size % 2 == 1
@x += @stack.shift
end
until @stack.empty?
c1x = @x
c1y = @y + @stack.shift
c2x = c1x + @stack.shift
c2y = c1y + @stack.shift
@x = c2x
@y = c2y + @stack.shift
@path.curve_to(c1x, c1y, c2x, c2y, @x, @y)
end
end
def hhcurveto
if @stack.size % 2 == 1
@y += @stack.shift
end
until @stack.empty?
c1x = @x + @stack.shift
c1y = @y
c2x = c1x + @stack.shift
c2y = c1y + @stack.shift
@x = c2x + @stack.shift
@y = c2y
@path.curve_to(c1x, c1y, c2x, c2y, @x, @y)
end
end
# rubocop:enable Style/EvenOdd
def shortint
b1, b2 = read_bytes(2)
@stack << (((b1 << 24) | (b2 << 16)) >> 16)
end
def callgsubr
code_index = @stack.pop + @gsubrs_bias
subr_code = @gsubrs[code_index].bytes
@data[@index, 0] = subr_code
end
def vhcurveto
until @stack.empty?
c1x = @x
c1y = @y + @stack.shift
c2x = c1x + @stack.shift
c2y = c1y + @stack.shift
@x = c2x + @stack.shift
@y = c2y + (@stack.size == 1 ? @stack.shift : 0)
@path.curve_to(c1x, c1y, c2x, c2y, @x, @y)
break if @stack.empty?
c1x = @x + @stack.shift
c1y = @y
c2x = c1x + @stack.shift
c2y = c1y + @stack.shift
@y = c2y + @stack.shift
@x = c2x + (@stack.size == 1 ? @stack.shift : 0)
@path.curve_to(c1x, c1y, c2x, c2y, @x, @y)
end
end
def hvcurveto
until @stack.empty?
c1x = @x + @stack.shift
c1y = @y
c2x = c1x + @stack.shift
c2y = c1y + @stack.shift
@y = c2y + @stack.shift
@x = c2x + (@stack.size == 1 ? @stack.shift : 0)
@path.curve_to(c1x, c1y, c2x, c2y, @x, @y)
break if @stack.empty?
c1x = @x
c1y = @y + @stack.shift
c2x = c1x + @stack.shift
c2y = c1y + @stack.shift
@x = c2x + @stack.shift
@y = c2y + (@stack.size == 1 ? @stack.shift : 0)
@path.curve_to(c1x, c1y, c2x, c2y, @x, @y)
end
end
end
end
end
end
|