MCPcopy Create free account
hub / github.com/cogentcore/core / SetString

Method SetString

math32/matrix2.go:316–430  ·  view source on GitHub ↗

SetString processes the standard SVG-style transform strings

(str string)

Source from the content-addressed store, hash-verified

314
315// SetString processes the standard SVG-style transform strings
316func (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 {

Callers 1

TestMatrix2SetStringFunction · 0.95

Calls 11

TranslateMethod · 0.95
ScaleMethod · 0.95
RotateMethod · 0.95
SkewMethod · 0.95
LogFunction · 0.92
Identity2Function · 0.85
ReadPointsFunction · 0.85
PointsCheckNFunction · 0.85
DegToRadFunction · 0.85
ErrorfMethod · 0.65
ContainsMethod · 0.45

Tested by 1

TestMatrix2SetStringFunction · 0.76