SetString processes the standard SVG-style transform strings
(str string)
| 314 | |
| 315 | // SetString processes the standard SVG-style transform strings |
| 316 | func (a *Matrix2) SetString(str string) error { |
| 317 | errmsg := "math32.Matrix2.SetString:" |
| 318 | str = strings.ToLower(strings.TrimSpace(str)) |
| 319 | *a = Identity2() |
| 320 | if str == "none" { |
| 321 | *a = Identity2() |
| 322 | return nil |
| 323 | } |
| 324 | // could have multiple transforms |
| 325 | for { |
| 326 | pidx := strings.IndexByte(str, '(') |
| 327 | if pidx < 0 { |
| 328 | err := fmt.Errorf("%s no params for transform: %v", errmsg, str) |
| 329 | return errors.Log(err) |
| 330 | } |
| 331 | cmd := str[:pidx] |
| 332 | vals := str[pidx+1:] |
| 333 | nxt := "" |
| 334 | eidx := strings.IndexByte(vals, ')') |
| 335 | if eidx > 0 { |
| 336 | nxt = strings.TrimSpace(vals[eidx+1:]) |
| 337 | if strings.HasPrefix(nxt, ";") { |
| 338 | nxt = strings.TrimSpace(strings.TrimPrefix(nxt, ";")) |
| 339 | } |
| 340 | vals = vals[:eidx] |
| 341 | } |
| 342 | pts := ReadPoints(vals) |
| 343 | switch cmd { |
| 344 | case "matrix": |
| 345 | if err := PointsCheckN(pts, 6, errmsg); err != nil { |
| 346 | errors.Log(err) |
| 347 | } else { |
| 348 | *a = Matrix2{pts[0], pts[1], pts[2], pts[3], pts[4], pts[5]} |
| 349 | } |
| 350 | case "translate": |
| 351 | if len(pts) == 1 { |
| 352 | *a = a.Translate(pts[0], 0) |
| 353 | } else if len(pts) == 2 { |
| 354 | *a = a.Translate(pts[0], pts[1]) |
| 355 | } else { |
| 356 | errors.Log(PointsCheckN(pts, 2, errmsg)) |
| 357 | } |
| 358 | case "translatex": |
| 359 | if err := PointsCheckN(pts, 1, errmsg); err != nil { |
| 360 | errors.Log(err) |
| 361 | } else { |
| 362 | *a = a.Translate(pts[0], 0) |
| 363 | } |
| 364 | case "translatey": |
| 365 | if err := PointsCheckN(pts, 1, errmsg); err != nil { |
| 366 | errors.Log(err) |
| 367 | } else { |
| 368 | *a = a.Translate(0, pts[0]) |
| 369 | } |
| 370 | case "scale": |
| 371 | if len(pts) == 1 { |
| 372 | *a = a.Scale(pts[0], pts[0]) |
| 373 | } else if len(pts) == 2 { |