// ImageWidth takes a file path (or sprite glob) and returns the // width in pixels of the image being referenced. func ImageWidth(mainctx context.Context, usv libsass.SassValue) (rsv *libsass.SassValue, err error) { var ( glob, name string ) comp, err := libsass.CompFromCtx(mainctx) if err != nil { return } err = libsass.Unmarshal(usv, &name) // Check for sprite-file override first if err != nil { var inf interface{} var infs []interface{} // Can't unmarshal to []interface{}, so unmarshal to // interface{} then reflect it into a []interface{} err = libsass.Unmarshal(usv, &inf) k := reflect.ValueOf(&infs).Elem() k.Set(reflect.ValueOf(inf)) if err != nil { return } glob = infs[0].(string) name = infs[1].(string) } paths := comp.(libsass.Pather) imgs := sw.New(&sw.Options{ ImageDir: paths.ImgDir(), BuildDir: paths.BuildDir(), GenImgDir: paths.ImgBuildDir(), }) loadctx := comp.Payload() var images payload.Payloader if len(glob) == 0 { images = payload.Image(loadctx) hit := images.Get(name) if hit != nil { imgs = hit } else { imgs.Decode(name) images.Set(name, imgs) } } else { // Glob present, look up in sprites sprites := payload.Sprite(loadctx) imgs = sprites.Get(glob) } w := imgs.SImageWidth(name) ww := libs.SassNumber{ Value: float64(w), Unit: "px", } res, err := libsass.Marshal(ww) return &res, err }
// ImageHeight takes a file path (or sprite glob) and returns the // height in pixels of the image being referenced. func ImageHeight(mainctx context.Context, usv libsass.SassValue) (*libsass.SassValue, error) { var ( glob string name string ) comp, err := libsass.CompFromCtx(mainctx) if err != nil { return nil, err } err = libsass.Unmarshal(usv, &name) // Check for sprite-file override first if err != nil { var inf interface{} var infs []interface{} // Can't unmarshal to []interface{}, so unmarshal to // interface{} then reflect it into a []interface{} err = libsass.Unmarshal(usv, &inf) k := reflect.ValueOf(&infs).Elem() k.Set(reflect.ValueOf(inf)) if err != nil { return nil, err } glob = infs[0].(string) name = infs[1].(string) } paths := comp.(libsass.Pather) imgs := sw.New(&sw.Options{ ImageDir: paths.ImgDir(), BuildDir: paths.BuildDir(), GenImgDir: paths.ImgBuildDir(), }) loadctx := comp.Payload() if loadctx == nil { return nil, ErrPayloadNil } images := payload.Image(loadctx) if images == nil { return nil, errors.New("inline payload not available") } if len(glob) == 0 { exst := images.Get(name) if exst != nil { imgs = exst } else { imgs.Decode(name) // Store images in global cache images.Set(name, imgs) } } else { sprites := payload.Sprite(loadctx) imgs = sprites.Get(glob) if imgs == nil { return nil, errors.New("Sprite not found") } } height := imgs.SImageHeight(name) Hheight := libs.SassNumber{ Value: float64(height), Unit: "px", } res, err := libsass.Marshal(Hheight) return &res, err }