| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package iconvg |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"errors" |
| 10 |
"image/color" |
| 11 |
) |
| 12 |
|
| 13 |
var ( |
| 14 |
errInconsistentMetadataChunkLength = errors.New("iconvg: inconsistent metadata chunk length") |
| 15 |
errInvalidColor = errors.New("iconvg: invalid color") |
| 16 |
errInvalidMagicIdentifier = errors.New("iconvg: invalid magic identifier") |
| 17 |
errInvalidMetadataChunkLength = errors.New("iconvg: invalid metadata chunk length") |
| 18 |
errInvalidMetadataIdentifier = errors.New("iconvg: invalid metadata identifier") |
| 19 |
errInvalidNumber = errors.New("iconvg: invalid number") |
| 20 |
errInvalidNumberOfMetadataChunks = errors.New("iconvg: invalid number of metadata chunks") |
| 21 |
errInvalidSuggestedPalette = errors.New("iconvg: invalid suggested palette") |
| 22 |
errInvalidViewBox = errors.New("iconvg: invalid view box") |
| 23 |
errUnsupportedDrawingOpcode = errors.New("iconvg: unsupported drawing opcode") |
| 24 |
errUnsupportedMetadataIdentifier = errors.New("iconvg: unsupported metadata identifier") |
| 25 |
errUnsupportedStylingOpcode = errors.New("iconvg: unsupported styling opcode") |
| 26 |
errUnsupportedUpgrade = errors.New("iconvg: unsupported upgrade") |
| 27 |
) |
| 28 |
|
| 29 |
var midDescriptions = [...]string{ |
| 30 |
midViewBox: "viewBox", |
| 31 |
midSuggestedPalette: "suggested palette", |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
type Destination interface { |
| 40 |
Reset(m Metadata) |
| 41 |
|
| 42 |
SetCSel(cSel uint8) |
| 43 |
SetNSel(nSel uint8) |
| 44 |
SetCReg(adj uint8, incr bool, c Color) |
| 45 |
SetNReg(adj uint8, incr bool, f float32) |
| 46 |
SetLOD(lod0, lod1 float32) |
| 47 |
|
| 48 |
StartPath(adj uint8, x, y float32) |
| 49 |
ClosePathEndPath() |
| 50 |
ClosePathAbsMoveTo(x, y float32) |
| 51 |
ClosePathRelMoveTo(x, y float32) |
| 52 |
|
| 53 |
AbsHLineTo(x float32) |
| 54 |
RelHLineTo(x float32) |
| 55 |
AbsVLineTo(y float32) |
| 56 |
RelVLineTo(y float32) |
| 57 |
AbsLineTo(x, y float32) |
| 58 |
RelLineTo(x, y float32) |
| 59 |
AbsSmoothQuadTo(x, y float32) |
| 60 |
RelSmoothQuadTo(x, y float32) |
| 61 |
AbsQuadTo(x1, y1, x, y float32) |
| 62 |
RelQuadTo(x1, y1, x, y float32) |
| 63 |
AbsSmoothCubeTo(x2, y2, x, y float32) |
| 64 |
RelSmoothCubeTo(x2, y2, x, y float32) |
| 65 |
AbsCubeTo(x1, y1, x2, y2, x, y float32) |
| 66 |
RelCubeTo(x1, y1, x2, y2, x, y float32) |
| 67 |
AbsArcTo(rx, ry, xAxisRotation float32, largeArc, sweep bool, x, y float32) |
| 68 |
RelArcTo(rx, ry, xAxisRotation float32, largeArc, sweep bool, x, y float32) |
| 69 |
} |
| 70 |
|
| 71 |
type printer func(b []byte, format string, args ...interface{}) |
| 72 |
|
| 73 |
|
| 74 |
type DecodeOptions struct { |
| 75 |
|
| 76 |
|
| 77 |
Palette *Palette |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
func DecodeMetadata(src []byte) (m Metadata, err error) { |
| 82 |
m.ViewBox = DefaultViewBox |
| 83 |
m.Palette = DefaultPalette |
| 84 |
if err = decode(nil, nil, &m, true, src, nil); err != nil { |
| 85 |
return Metadata{}, err |
| 86 |
} |
| 87 |
return m, nil |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
func Decode(dst Destination, src []byte, opts *DecodeOptions) error { |
| 92 |
m := Metadata{ |
| 93 |
ViewBox: DefaultViewBox, |
| 94 |
Palette: DefaultPalette, |
| 95 |
} |
| 96 |
if opts != nil && opts.Palette != nil { |
| 97 |
m.Palette = *opts.Palette |
| 98 |
} |
| 99 |
return decode(dst, nil, &m, false, src, opts) |
| 100 |
} |
| 101 |
|
| 102 |
func decode(dst Destination, p printer, m *Metadata, metadataOnly bool, src buffer, opts *DecodeOptions) (err error) { |
| 103 |
if !bytes.HasPrefix(src, magicBytes) { |
| 104 |
|
| 105 |
|
| 106 |
return errInvalidMagicIdentifier |
| 107 |
} |
| 108 |
if p != nil { |
| 109 |
p(src[:len(magic)], "IconVG Magic identifier\n") |
| 110 |
} |
| 111 |
src = src[len(magic):] |
| 112 |
|
| 113 |
nMetadataChunks, n := src.decodeNatural() |
| 114 |
if n == 0 { |
| 115 |
return errInvalidNumberOfMetadataChunks |
| 116 |
} |
| 117 |
if p != nil { |
| 118 |
p(src[:n], "Number of metadata chunks: %d\n", nMetadataChunks) |
| 119 |
} |
| 120 |
src = src[n:] |
| 121 |
|
| 122 |
for ; nMetadataChunks > 0; nMetadataChunks-- { |
| 123 |
src, err = decodeMetadataChunk(p, m, src, opts) |
| 124 |
if err != nil { |
| 125 |
return err |
| 126 |
} |
| 127 |
} |
| 128 |
if metadataOnly { |
| 129 |
return nil |
| 130 |
} |
| 131 |
if dst != nil { |
| 132 |
dst.Reset(*m) |
| 133 |
} |
| 134 |
|
| 135 |
mf := modeFunc(decodeStyling) |
| 136 |
for len(src) > 0 { |
| 137 |
mf, src, err = mf(dst, p, src) |
| 138 |
if err != nil { |
| 139 |
return err |
| 140 |
} |
| 141 |
} |
| 142 |
return nil |
| 143 |
} |
| 144 |
|
| 145 |
func decodeMetadataChunk(p printer, m *Metadata, src buffer, opts *DecodeOptions) (src1 buffer, err error) { |
| 146 |
length, n := src.decodeNatural() |
| 147 |
if n == 0 { |
| 148 |
return nil, errInvalidMetadataChunkLength |
| 149 |
} |
| 150 |
if p != nil { |
| 151 |
p(src[:n], "Metadata chunk length: %d\n", length) |
| 152 |
} |
| 153 |
src = src[n:] |
| 154 |
lenSrcWant := int64(len(src)) - int64(length) |
| 155 |
|
| 156 |
mid, n := src.decodeNatural() |
| 157 |
if n == 0 { |
| 158 |
return nil, errInvalidMetadataIdentifier |
| 159 |
} |
| 160 |
if mid >= uint32(len(midDescriptions)) { |
| 161 |
return nil, errUnsupportedMetadataIdentifier |
| 162 |
} |
| 163 |
if p != nil { |
| 164 |
p(src[:n], "Metadata Identifier: %d (%s)\n", mid, midDescriptions[mid]) |
| 165 |
} |
| 166 |
src = src[n:] |
| 167 |
|
| 168 |
switch mid { |
| 169 |
case midViewBox: |
| 170 |
if m.ViewBox.Min[0], src, err = decodeNumber(p, src, buffer.decodeCoordinate); err != nil { |
| 171 |
return nil, errInvalidViewBox |
| 172 |
} |
| 173 |
if m.ViewBox.Min[1], src, err = decodeNumber(p, src, buffer.decodeCoordinate); err != nil { |
| 174 |
return nil, errInvalidViewBox |
| 175 |
} |
| 176 |
if m.ViewBox.Max[0], src, err = decodeNumber(p, src, buffer.decodeCoordinate); err != nil { |
| 177 |
return nil, errInvalidViewBox |
| 178 |
} |
| 179 |
if m.ViewBox.Max[1], src, err = decodeNumber(p, src, buffer.decodeCoordinate); err != nil { |
| 180 |
return nil, errInvalidViewBox |
| 181 |
} |
| 182 |
if m.ViewBox.Min[0] > m.ViewBox.Max[0] || m.ViewBox.Min[1] > m.ViewBox.Max[1] || |
| 183 |
isNaNOrInfinity(m.ViewBox.Min[0]) || isNaNOrInfinity(m.ViewBox.Min[1]) || |
| 184 |
isNaNOrInfinity(m.ViewBox.Max[0]) || isNaNOrInfinity(m.ViewBox.Max[1]) { |
| 185 |
return nil, errInvalidViewBox |
| 186 |
} |
| 187 |
|
| 188 |
case midSuggestedPalette: |
| 189 |
if len(src) == 0 { |
| 190 |
return nil, errInvalidSuggestedPalette |
| 191 |
} |
| 192 |
length, format := 1+int(src[0]&0x3f), src[0]>>6 |
| 193 |
decode := buffer.decodeColor4 |
| 194 |
switch format { |
| 195 |
case 0: |
| 196 |
decode = buffer.decodeColor1 |
| 197 |
case 1: |
| 198 |
decode = buffer.decodeColor2 |
| 199 |
case 2: |
| 200 |
decode = buffer.decodeColor3Direct |
| 201 |
} |
| 202 |
if p != nil { |
| 203 |
p(src[:1], " %d palette colors, %d bytes per color\n", length, 1+format) |
| 204 |
} |
| 205 |
src = src[1:] |
| 206 |
|
| 207 |
for i := 0; i < length; i++ { |
| 208 |
c, n := decode(src) |
| 209 |
if n == 0 { |
| 210 |
return nil, errInvalidSuggestedPalette |
| 211 |
} |
| 212 |
rgba := c.rgba() |
| 213 |
if c.typ != ColorTypeRGBA || !validAlphaPremulColor(rgba) { |
| 214 |
rgba = color.RGBA{0x00, 0x00, 0x00, 0xff} |
| 215 |
} |
| 216 |
if p != nil { |
| 217 |
p(src[:n], " RGBA %02x%02x%02x%02x\n", rgba.R, rgba.G, rgba.B, rgba.A) |
| 218 |
} |
| 219 |
src = src[n:] |
| 220 |
if opts == nil || opts.Palette == nil { |
| 221 |
m.Palette[i] = rgba |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
default: |
| 226 |
return nil, errUnsupportedMetadataIdentifier |
| 227 |
} |
| 228 |
|
| 229 |
if int64(len(src)) != lenSrcWant { |
| 230 |
return nil, errInconsistentMetadataChunkLength |
| 231 |
} |
| 232 |
return src, nil |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
type modeFunc func(dst Destination, p printer, src buffer) (modeFunc, buffer, error) |
| 242 |
|
| 243 |
func decodeStyling(dst Destination, p printer, src buffer) (modeFunc, buffer, error) { |
| 244 |
switch opcode := src[0]; { |
| 245 |
case opcode < 0x80: |
| 246 |
if opcode < 0x40 { |
| 247 |
opcode &= 0x3f |
| 248 |
if p != nil { |
| 249 |
p(src[:1], "Set CSEL = %d\n", opcode) |
| 250 |
} |
| 251 |
src = src[1:] |
| 252 |
if dst != nil { |
| 253 |
dst.SetCSel(opcode) |
| 254 |
} |
| 255 |
} else { |
| 256 |
opcode &= 0x3f |
| 257 |
if p != nil { |
| 258 |
p(src[:1], "Set NSEL = %d\n", opcode) |
| 259 |
} |
| 260 |
src = src[1:] |
| 261 |
if dst != nil { |
| 262 |
dst.SetNSel(opcode) |
| 263 |
} |
| 264 |
} |
| 265 |
return decodeStyling, src, nil |
| 266 |
case opcode < 0xa8: |
| 267 |
return decodeSetCReg(dst, p, src, opcode) |
| 268 |
case opcode < 0xc0: |
| 269 |
return decodeSetNReg(dst, p, src, opcode) |
| 270 |
case opcode < 0xc7: |
| 271 |
return decodeStartPath(dst, p, src, opcode) |
| 272 |
case opcode == 0xc7: |
| 273 |
return decodeSetLOD(dst, p, src) |
| 274 |
} |
| 275 |
return nil, nil, errUnsupportedStylingOpcode |
| 276 |
} |
| 277 |
|
| 278 |
func decodeSetCReg(dst Destination, p printer, src buffer, opcode byte) (modeFunc, buffer, error) { |
| 279 |
nBytes, directness, adj := 0, "", opcode&0x07 |
| 280 |
var decode func(buffer) (Color, int) |
| 281 |
incr := adj == 7 |
| 282 |
if incr { |
| 283 |
adj = 0 |
| 284 |
} |
| 285 |
|
| 286 |
switch (opcode - 0x80) >> 3 { |
| 287 |
case 0: |
| 288 |
nBytes, directness, decode = 1, "", buffer.decodeColor1 |
| 289 |
case 1: |
| 290 |
nBytes, directness, decode = 2, "", buffer.decodeColor2 |
| 291 |
case 2: |
| 292 |
nBytes, directness, decode = 3, " (direct)", buffer.decodeColor3Direct |
| 293 |
case 3: |
| 294 |
nBytes, directness, decode = 4, "", buffer.decodeColor4 |
| 295 |
case 4: |
| 296 |
nBytes, directness, decode = 3, " (indirect)", buffer.decodeColor3Indirect |
| 297 |
} |
| 298 |
if p != nil { |
| 299 |
if incr { |
| 300 |
p(src[:1], "Set CREG[CSEL-0] to a %d byte%s color; CSEL++\n", nBytes, directness) |
| 301 |
} else { |
| 302 |
p(src[:1], "Set CREG[CSEL-%d] to a %d byte%s color\n", adj, nBytes, directness) |
| 303 |
} |
| 304 |
} |
| 305 |
src = src[1:] |
| 306 |
|
| 307 |
c, n := decode(src) |
| 308 |
if n == 0 { |
| 309 |
return nil, nil, errInvalidColor |
| 310 |
} |
| 311 |
|
| 312 |
if p != nil { |
| 313 |
printColor(src[:n], p, c, "") |
| 314 |
} |
| 315 |
src = src[n:] |
| 316 |
|
| 317 |
if dst != nil { |
| 318 |
dst.SetCReg(adj, incr, c) |
| 319 |
} |
| 320 |
|
| 321 |
return decodeStyling, src, nil |
| 322 |
} |
| 323 |
|
| 324 |
func printColor(src []byte, p printer, c Color, prefix string) { |
| 325 |
switch c.typ { |
| 326 |
case ColorTypeRGBA: |
| 327 |
if rgba := c.rgba(); validAlphaPremulColor(rgba) { |
| 328 |
p(src, " %sRGBA %02x%02x%02x%02x\n", prefix, rgba.R, rgba.G, rgba.B, rgba.A) |
| 329 |
} else if rgba.A == 0 && rgba.B&0x80 != 0 { |
| 330 |
p(src, " %sgradient (NSTOPS=%d, CBASE=%d, NBASE=%d, %s, %s)\n", |
| 331 |
prefix, |
| 332 |
rgba.R&0x3f, |
| 333 |
rgba.G&0x3f, |
| 334 |
rgba.B&0x3f, |
| 335 |
gradientShapeNames[(rgba.B>>6)&0x01], |
| 336 |
gradientSpreadNames[rgba.G>>6], |
| 337 |
) |
| 338 |
} else { |
| 339 |
p(src, " %snonsensical color\n", prefix) |
| 340 |
} |
| 341 |
case ColorTypePaletteIndex: |
| 342 |
p(src, " %scustomPalette[%d]\n", prefix, c.paletteIndex()) |
| 343 |
case ColorTypeCReg: |
| 344 |
p(src, " %sCREG[%d]\n", prefix, c.cReg()) |
| 345 |
case ColorTypeBlend: |
| 346 |
t, c0, c1 := c.blend() |
| 347 |
p(src[:1], " blend %d:%d c0:c1\n", 0xff-t, t) |
| 348 |
printColor(src[1:2], p, decodeColor1(c0), " c0: ") |
| 349 |
printColor(src[2:3], p, decodeColor1(c1), " c1: ") |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
func decodeSetNReg(dst Destination, p printer, src buffer, opcode byte) (modeFunc, buffer, error) { |
| 354 |
decode, typ, adj := buffer.decodeZeroToOne, "zero-to-one", opcode&0x07 |
| 355 |
incr := adj == 7 |
| 356 |
if incr { |
| 357 |
adj = 0 |
| 358 |
} |
| 359 |
|
| 360 |
switch (opcode - 0xa8) >> 3 { |
| 361 |
case 0: |
| 362 |
decode, typ = buffer.decodeReal, "real" |
| 363 |
case 1: |
| 364 |
decode, typ = buffer.decodeCoordinate, "coordinate" |
| 365 |
} |
| 366 |
if p != nil { |
| 367 |
if incr { |
| 368 |
p(src[:1], "Set NREG[NSEL-0] to a %s number; NSEL++\n", typ) |
| 369 |
} else { |
| 370 |
p(src[:1], "Set NREG[NSEL-%d] to a %s number\n", adj, typ) |
| 371 |
} |
| 372 |
} |
| 373 |
src = src[1:] |
| 374 |
|
| 375 |
f, n := decode(src) |
| 376 |
if n == 0 { |
| 377 |
return nil, nil, errInvalidNumber |
| 378 |
} |
| 379 |
if p != nil { |
| 380 |
p(src[:n], " %g\n", f) |
| 381 |
} |
| 382 |
src = src[n:] |
| 383 |
|
| 384 |
if dst != nil { |
| 385 |
dst.SetNReg(adj, incr, f) |
| 386 |
} |
| 387 |
|
| 388 |
return decodeStyling, src, nil |
| 389 |
} |
| 390 |
|
| 391 |
func decodeStartPath(dst Destination, p printer, src buffer, opcode byte) (modeFunc, buffer, error) { |
| 392 |
adj := opcode & 0x07 |
| 393 |
if p != nil { |
| 394 |
p(src[:1], "Start path, filled with CREG[CSEL-%d]; M (absolute moveTo)\n", adj) |
| 395 |
} |
| 396 |
src = src[1:] |
| 397 |
|
| 398 |
x, src, err := decodeNumber(p, src, buffer.decodeCoordinate) |
| 399 |
if err != nil { |
| 400 |
return nil, nil, err |
| 401 |
} |
| 402 |
y, src, err := decodeNumber(p, src, buffer.decodeCoordinate) |
| 403 |
if err != nil { |
| 404 |
return nil, nil, err |
| 405 |
} |
| 406 |
|
| 407 |
if dst != nil { |
| 408 |
dst.StartPath(adj, x, y) |
| 409 |
} |
| 410 |
|
| 411 |
return decodeDrawing, src, nil |
| 412 |
} |
| 413 |
|
| 414 |
func decodeSetLOD(dst Destination, p printer, src buffer) (modeFunc, buffer, error) { |
| 415 |
if p != nil { |
| 416 |
p(src[:1], "Set LOD\n") |
| 417 |
} |
| 418 |
src = src[1:] |
| 419 |
|
| 420 |
lod0, src, err := decodeNumber(p, src, buffer.decodeReal) |
| 421 |
if err != nil { |
| 422 |
return nil, nil, err |
| 423 |
} |
| 424 |
lod1, src, err := decodeNumber(p, src, buffer.decodeReal) |
| 425 |
if err != nil { |
| 426 |
return nil, nil, err |
| 427 |
} |
| 428 |
|
| 429 |
if dst != nil { |
| 430 |
dst.SetLOD(lod0, lod1) |
| 431 |
} |
| 432 |
return decodeStyling, src, nil |
| 433 |
} |
| 434 |
|
| 435 |
func decodeDrawing(dst Destination, p printer, src buffer) (mf modeFunc, src1 buffer, err error) { |
| 436 |
var coords [6]float32 |
| 437 |
|
| 438 |
switch opcode := src[0]; { |
| 439 |
case opcode < 0xe0: |
| 440 |
op, nCoords, nReps := "", 0, 1+int(opcode&0x0f) |
| 441 |
switch opcode >> 4 { |
| 442 |
case 0x00, 0x01: |
| 443 |
op = "L (absolute lineTo)" |
| 444 |
nCoords = 2 |
| 445 |
nReps = 1 + int(opcode&0x1f) |
| 446 |
case 0x02, 0x03: |
| 447 |
op = "l (relative lineTo)" |
| 448 |
nCoords = 2 |
| 449 |
nReps = 1 + int(opcode&0x1f) |
| 450 |
case 0x04: |
| 451 |
op = "T (absolute smooth quadTo)" |
| 452 |
nCoords = 2 |
| 453 |
case 0x05: |
| 454 |
op = "t (relative smooth quadTo)" |
| 455 |
nCoords = 2 |
| 456 |
case 0x06: |
| 457 |
op = "Q (absolute quadTo)" |
| 458 |
nCoords = 4 |
| 459 |
case 0x07: |
| 460 |
op = "q (relative quadTo)" |
| 461 |
nCoords = 4 |
| 462 |
case 0x08: |
| 463 |
op = "S (absolute smooth cubeTo)" |
| 464 |
nCoords = 4 |
| 465 |
case 0x09: |
| 466 |
op = "s (relative smooth cubeTo)" |
| 467 |
nCoords = 4 |
| 468 |
case 0x0a: |
| 469 |
op = "C (absolute cubeTo)" |
| 470 |
nCoords = 6 |
| 471 |
case 0x0b: |
| 472 |
op = "c (relative cubeTo)" |
| 473 |
nCoords = 6 |
| 474 |
case 0x0c: |
| 475 |
op = "A (absolute arcTo)" |
| 476 |
nCoords = 0 |
| 477 |
case 0x0d: |
| 478 |
op = "a (relative arcTo)" |
| 479 |
nCoords = 0 |
| 480 |
} |
| 481 |
|
| 482 |
if p != nil { |
| 483 |
p(src[:1], "%s, %d reps\n", op, nReps) |
| 484 |
} |
| 485 |
src = src[1:] |
| 486 |
|
| 487 |
for i := 0; i < nReps; i++ { |
| 488 |
if p != nil && i != 0 { |
| 489 |
p(nil, "%s, implicit\n", op) |
| 490 |
} |
| 491 |
var largeArc, sweep bool |
| 492 |
if op[0] != 'A' && op[0] != 'a' { |
| 493 |
src, err = decodeCoordinates(coords[:nCoords], p, src) |
| 494 |
if err != nil { |
| 495 |
return nil, nil, err |
| 496 |
} |
| 497 |
} else { |
| 498 |
|
| 499 |
src, err = decodeCoordinates(coords[:2], p, src) |
| 500 |
if err != nil { |
| 501 |
return nil, nil, err |
| 502 |
} |
| 503 |
coords[2], src, err = decodeAngle(p, src) |
| 504 |
if err != nil { |
| 505 |
return nil, nil, err |
| 506 |
} |
| 507 |
largeArc, sweep, src, err = decodeArcToFlags(p, src) |
| 508 |
if err != nil { |
| 509 |
return nil, nil, err |
| 510 |
} |
| 511 |
src, err = decodeCoordinates(coords[4:6], p, src) |
| 512 |
if err != nil { |
| 513 |
return nil, nil, err |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
if dst == nil { |
| 518 |
continue |
| 519 |
} |
| 520 |
switch op[0] { |
| 521 |
case 'L': |
| 522 |
dst.AbsLineTo(coords[0], coords[1]) |
| 523 |
case 'l': |
| 524 |
dst.RelLineTo(coords[0], coords[1]) |
| 525 |
case 'T': |
| 526 |
dst.AbsSmoothQuadTo(coords[0], coords[1]) |
| 527 |
case 't': |
| 528 |
dst.RelSmoothQuadTo(coords[0], coords[1]) |
| 529 |
case 'Q': |
| 530 |
dst.AbsQuadTo(coords[0], coords[1], coords[2], coords[3]) |
| 531 |
case 'q': |
| 532 |
dst.RelQuadTo(coords[0], coords[1], coords[2], coords[3]) |
| 533 |
case 'S': |
| 534 |
dst.AbsSmoothCubeTo(coords[0], coords[1], coords[2], coords[3]) |
| 535 |
case 's': |
| 536 |
dst.RelSmoothCubeTo(coords[0], coords[1], coords[2], coords[3]) |
| 537 |
case 'C': |
| 538 |
dst.AbsCubeTo(coords[0], coords[1], coords[2], coords[3], coords[4], coords[5]) |
| 539 |
case 'c': |
| 540 |
dst.RelCubeTo(coords[0], coords[1], coords[2], coords[3], coords[4], coords[5]) |
| 541 |
case 'A': |
| 542 |
dst.AbsArcTo(coords[0], coords[1], coords[2], largeArc, sweep, coords[4], coords[5]) |
| 543 |
case 'a': |
| 544 |
dst.RelArcTo(coords[0], coords[1], coords[2], largeArc, sweep, coords[4], coords[5]) |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
case opcode == 0xe1: |
| 549 |
if p != nil { |
| 550 |
p(src[:1], "z (closePath); end path\n") |
| 551 |
} |
| 552 |
src = src[1:] |
| 553 |
if dst != nil { |
| 554 |
dst.ClosePathEndPath() |
| 555 |
} |
| 556 |
return decodeStyling, src, nil |
| 557 |
|
| 558 |
case opcode == 0xe2: |
| 559 |
if p != nil { |
| 560 |
p(src[:1], "z (closePath); M (absolute moveTo)\n") |
| 561 |
} |
| 562 |
src = src[1:] |
| 563 |
src, err = decodeCoordinates(coords[:2], p, src) |
| 564 |
if err != nil { |
| 565 |
return nil, nil, err |
| 566 |
} |
| 567 |
if dst != nil { |
| 568 |
dst.ClosePathAbsMoveTo(coords[0], coords[1]) |
| 569 |
} |
| 570 |
|
| 571 |
case opcode == 0xe3: |
| 572 |
if p != nil { |
| 573 |
p(src[:1], "z (closePath); m (relative moveTo)\n") |
| 574 |
} |
| 575 |
src = src[1:] |
| 576 |
src, err = decodeCoordinates(coords[:2], p, src) |
| 577 |
if err != nil { |
| 578 |
return nil, nil, err |
| 579 |
} |
| 580 |
if dst != nil { |
| 581 |
dst.ClosePathRelMoveTo(coords[0], coords[1]) |
| 582 |
} |
| 583 |
|
| 584 |
case opcode == 0xe6: |
| 585 |
if p != nil { |
| 586 |
p(src[:1], "H (absolute horizontal lineTo)\n") |
| 587 |
} |
| 588 |
src = src[1:] |
| 589 |
src, err = decodeCoordinates(coords[:1], p, src) |
| 590 |
if err != nil { |
| 591 |
return nil, nil, err |
| 592 |
} |
| 593 |
if dst != nil { |
| 594 |
dst.AbsHLineTo(coords[0]) |
| 595 |
} |
| 596 |
|
| 597 |
case opcode == 0xe7: |
| 598 |
if p != nil { |
| 599 |
p(src[:1], "h (relative horizontal lineTo)\n") |
| 600 |
} |
| 601 |
src = src[1:] |
| 602 |
src, err = decodeCoordinates(coords[:1], p, src) |
| 603 |
if err != nil { |
| 604 |
return nil, nil, err |
| 605 |
} |
| 606 |
if dst != nil { |
| 607 |
dst.RelHLineTo(coords[0]) |
| 608 |
} |
| 609 |
|
| 610 |
case opcode == 0xe8: |
| 611 |
if p != nil { |
| 612 |
p(src[:1], "V (absolute vertical lineTo)\n") |
| 613 |
} |
| 614 |
src = src[1:] |
| 615 |
src, err = decodeCoordinates(coords[:1], p, src) |
| 616 |
if err != nil { |
| 617 |
return nil, nil, err |
| 618 |
} |
| 619 |
if dst != nil { |
| 620 |
dst.AbsVLineTo(coords[0]) |
| 621 |
} |
| 622 |
|
| 623 |
case opcode == 0xe9: |
| 624 |
if p != nil { |
| 625 |
p(src[:1], "v (relative vertical lineTo)\n") |
| 626 |
} |
| 627 |
src = src[1:] |
| 628 |
src, err = decodeCoordinates(coords[:1], p, src) |
| 629 |
if err != nil { |
| 630 |
return nil, nil, err |
| 631 |
} |
| 632 |
if dst != nil { |
| 633 |
dst.RelVLineTo(coords[0]) |
| 634 |
} |
| 635 |
|
| 636 |
default: |
| 637 |
return nil, nil, errUnsupportedDrawingOpcode |
| 638 |
} |
| 639 |
return decodeDrawing, src, nil |
| 640 |
} |
| 641 |
|
| 642 |
type decodeNumberFunc func(buffer) (float32, int) |
| 643 |
|
| 644 |
func decodeNumber(p printer, src buffer, dnf decodeNumberFunc) (float32, buffer, error) { |
| 645 |
x, n := dnf(src) |
| 646 |
if n == 0 { |
| 647 |
return 0, nil, errInvalidNumber |
| 648 |
} |
| 649 |
if p != nil { |
| 650 |
p(src[:n], " %+g\n", x) |
| 651 |
} |
| 652 |
return x, src[n:], nil |
| 653 |
} |
| 654 |
|
| 655 |
func decodeCoordinates(coords []float32, p printer, src buffer) (src1 buffer, err error) { |
| 656 |
for i := range coords { |
| 657 |
coords[i], src, err = decodeNumber(p, src, buffer.decodeCoordinate) |
| 658 |
if err != nil { |
| 659 |
return nil, err |
| 660 |
} |
| 661 |
} |
| 662 |
return src, nil |
| 663 |
} |
| 664 |
|
| 665 |
func decodeCoordinatePairs(coords [][2]float32, p printer, src buffer) (src1 buffer, err error) { |
| 666 |
for i := range coords { |
| 667 |
coords[i][0], src, err = decodeNumber(p, src, buffer.decodeCoordinate) |
| 668 |
if err != nil { |
| 669 |
return nil, err |
| 670 |
} |
| 671 |
coords[i][1], src, err = decodeNumber(p, src, buffer.decodeCoordinate) |
| 672 |
if err != nil { |
| 673 |
return nil, err |
| 674 |
} |
| 675 |
} |
| 676 |
return src, nil |
| 677 |
} |
| 678 |
|
| 679 |
func decodeAngle(p printer, src buffer) (float32, buffer, error) { |
| 680 |
x, n := src.decodeZeroToOne() |
| 681 |
if n == 0 { |
| 682 |
return 0, nil, errInvalidNumber |
| 683 |
} |
| 684 |
if p != nil { |
| 685 |
p(src[:n], " %v × 360 degrees (%v degrees)\n", x, x*360) |
| 686 |
} |
| 687 |
return x, src[n:], nil |
| 688 |
} |
| 689 |
|
| 690 |
func decodeArcToFlags(p printer, src buffer) (bool, bool, buffer, error) { |
| 691 |
x, n := src.decodeNatural() |
| 692 |
if n == 0 { |
| 693 |
return false, false, nil, errInvalidNumber |
| 694 |
} |
| 695 |
if p != nil { |
| 696 |
p(src[:n], " %#x (largeArc=%d, sweep=%d)\n", x, (x>>0)&0x01, (x>>1)&0x01) |
| 697 |
} |
| 698 |
return (x>>0)&0x01 != 0, (x>>1)&0x01 != 0, src[n:], nil |
| 699 |
} |