2021-09-07 17:36:50 +03:00
package rui
2024-08-13 13:52:47 +03:00
import (
"fmt"
"math"
"strings"
)
2024-09-12 14:05:11 +03:00
// Constants for [Transform] specific properties
2021-09-07 17:36:50 +03:00
const (
2024-11-13 12:56:39 +03:00
// TransformTag is the constant for "transform" property tag.
//
// Used by `View`.
// Specify translation, scale and rotation over x, y and z axes as well as a distorsion of a view along x and y axes.
//
// Supported types: `Transform`, `string`.
//
// See `Transform` description for more details.
//
// Conversion rules:
// `Transform` - stored as is, no conversion performed.
// `string` - string representation of `Transform` interface. Example: "_{translate-x = 10px, scale-y = 1.1}".
TransformTag PropertyName = "transform"
2024-09-18 13:50:06 +03:00
// Perspective is the constant for "perspective" property tag.
//
// Used by `View`.
2024-11-13 12:56:39 +03:00
// Distance between the z-plane and the user in order to give a 3D-positioned element some perspective. Each 3D element
2024-09-18 13:50:06 +03:00
// with z > 0 becomes larger, each 3D-element with z < 0 becomes smaller. The default value is 0 (no 3D effects).
//
// Supported types: `SizeUnit`, `SizeFunc`, `string`, `float`, `int`.
//
// Internal type is `SizeUnit`, other types converted to it during assignment.
// See `SizeUnit` description for more details.
2024-11-13 12:56:39 +03:00
Perspective PropertyName = "perspective"
2024-08-13 13:52:47 +03:00
2024-09-18 13:50:06 +03:00
// PerspectiveOriginX is the constant for "perspective-origin-x" property tag.
//
// Used by `View`.
2024-11-13 12:56:39 +03:00
// x-coordinate of the position at which the viewer is looking. It is used as the vanishing point by the "perspective"
2024-09-18 13:50:06 +03:00
// property. The default value is 50%.
//
// Supported types: `SizeUnit`, `SizeFunc`, `string`, `float`, `int`.
//
// Internal type is `SizeUnit`, other types converted to it during assignment.
// See `SizeUnit` description for more details.
2024-11-13 12:56:39 +03:00
PerspectiveOriginX PropertyName = "perspective-origin-x"
2024-08-13 13:52:47 +03:00
2024-09-18 13:50:06 +03:00
// PerspectiveOriginY is the constant for "perspective-origin-y" property tag.
//
// Used by `View`.
2024-11-13 12:56:39 +03:00
// y-coordinate of the position at which the viewer is looking. It is used as the vanishing point by the "perspective"
2024-09-18 13:50:06 +03:00
// property. The default value is 50%.
//
// Supported types: `SizeUnit`, `SizeFunc`, `string`, `float`, `int`.
//
// Internal type is `SizeUnit`, other types converted to it during assignment.
// See `SizeUnit` description for more details.
2024-11-13 12:56:39 +03:00
PerspectiveOriginY PropertyName = "perspective-origin-y"
2024-08-13 13:52:47 +03:00
2024-09-18 13:50:06 +03:00
// BackfaceVisible is the constant for "backface-visibility" property tag.
//
// Used by `View`.
// Controls whether the back face of a view is visible when turned towards the user. Default value is `true`.
//
// Supported types: `bool`, `int`, `string`.
//
// Values:
// `true` or `1` or "true", "yes", "on", "1" - Back face is visible when turned towards the user.
// `false` or `0` or "false", "no", "off", "0" - Back face is hidden, effectively making the view invisible when turned away from the user.
2024-11-13 12:56:39 +03:00
BackfaceVisible PropertyName = "backface-visibility"
2024-08-13 13:52:47 +03:00
2024-09-18 13:50:06 +03:00
// OriginX is the constant for "origin-x" property tag.
//
// Used by `View`.
// x-coordinate of the point around which a view transformation is applied. The default value is 50%.
//
// Supported types: `SizeUnit`, `SizeFunc`, `string`, `float`, `int`.
//
// Internal type is `SizeUnit`, other types converted to it during assignment.
// See `SizeUnit` description for more details.
2024-11-13 12:56:39 +03:00
OriginX PropertyName = "origin-x"
2024-08-13 13:52:47 +03:00
2024-09-18 13:50:06 +03:00
// OriginY is the constant for "origin-y" property tag.
//
// Used by `View`.
// y-coordinate of the point around which a view transformation is applied. The default value is 50%.
//
// Supported types: `SizeUnit`, `SizeFunc`, `string`, `float`, `int`.
//
// Internal type is `SizeUnit`, other types converted to it during assignment.
// See `SizeUnit` description for more details.
2024-11-13 12:56:39 +03:00
OriginY PropertyName = "origin-y"
2024-08-13 13:52:47 +03:00
2024-09-18 13:50:06 +03:00
// OriginZ is the constant for "origin-z" property tag.
//
// Used by `View`.
// z-coordinate of the point around which a view transformation is applied. The default value is 50%.
//
// Supported types: `SizeUnit`, `SizeFunc`, `string`, `float`, `int`.
//
// Internal type is `SizeUnit`, other types converted to it during assignment.
// See `SizeUnit` description for more details.
2024-11-13 12:56:39 +03:00
OriginZ PropertyName = "origin-z"
2024-08-13 13:52:47 +03:00
2024-09-18 13:50:06 +03:00
// TranslateX is the constant for "translate-x" property tag.
//
// Used by `View`, `Transform`.
//
// Usage in `View`:
// x-axis translation value of a 2D/3D translation.
//
// Supported types: `SizeUnit`, `SizeFunc`, `string`, `float`, `int`.
//
// Internal type is `SizeUnit`, other types converted to it during assignment.
// See `SizeUnit` description for more details.
//
// Usage in `Transform`:
// x-axis translation value of a 2D/3D translation.
//
// Supported types: `SizeUnit`, `SizeFunc`, `string`, `float`, `int`.
//
// Internal type is `SizeUnit`, other types converted to it during assignment.
// See `SizeUnit` description for more details.
2024-11-13 12:56:39 +03:00
TranslateX PropertyName = "translate-x"
2024-08-13 13:52:47 +03:00
2024-09-18 13:50:06 +03:00
// TranslateY is the constant for "translate-y" property tag.
//
// Used by `View`, `Transform`.
//
// Usage in `View`:
// y-axis translation value of a 2D/3D translation.
//
// Supported types: `SizeUnit`, `SizeFunc`, `string`, `float`, `int`.
//
// Internal type is `SizeUnit`, other types converted to it during assignment.
// See `SizeUnit` description for more details.
//
// Usage in `Transform`:
// x-axis translation value of a 2D/3D translation.
//
// Supported types: `SizeUnit`, `SizeFunc`, `string`, `float`, `int`.
//
// Internal type is `SizeUnit`, other types converted to it during assignment.
// See `SizeUnit` description for more details.
2024-11-13 12:56:39 +03:00
TranslateY PropertyName = "translate-y"
2024-08-13 13:52:47 +03:00
2024-09-18 13:50:06 +03:00
// TranslateZ is the constant for "translate-z" property tag.
//
// Used by `View`, `Transform`.
//
// Usage in `View`:
// z-axis translation value of a 3D translation.
//
// Supported types: `SizeUnit`, `SizeFunc`, `string`, `float`, `int`.
//
// Internal type is `SizeUnit`, other types converted to it during assignment.
// See `SizeUnit` description for more details.
//
// Usage in `Transform`:
// z-axis translation value of a 3D translation.
//
// Supported types: `SizeUnit`, `SizeFunc`, `string`, `float`, `int`.
//
// Internal type is `SizeUnit`, other types converted to it during assignment.
// See `SizeUnit` description for more details.
2024-11-13 12:56:39 +03:00
TranslateZ PropertyName = "translate-z"
2024-08-13 13:52:47 +03:00
2024-09-18 13:50:06 +03:00
// ScaleX is the constant for "scale-x" property tag.
//
// Used by `View`, `Transform`.
//
// Usage in `View`:
2024-11-13 12:56:39 +03:00
// x-axis scaling value of a 2D/3D scale. The original scale is 1. Values between 0 and 1 are used to decrease original
2024-09-18 13:50:06 +03:00
// scale, more than 1 - to increase. The default value is 1.
//
// Supported types: `float`, `int`, `string`.
//
// Internal type is `float`, other types converted to it during assignment.
//
// Usage in `Transform`:
2024-11-13 12:56:39 +03:00
// x-axis scaling value of a 2D/3D scale. The original scale is 1. Values between 0 and 1 are used to decrease original
2024-09-18 13:50:06 +03:00
// scale, more than 1 - to increase. The default value is 1.
//
// Supported types: `float`, `int`, `string`.
//
// Internal type is `float`, other types converted to it during assignment.
2024-11-13 12:56:39 +03:00
ScaleX PropertyName = "scale-x"
2024-08-13 13:52:47 +03:00
2024-09-18 13:50:06 +03:00
// ScaleY is the constant for "scale-y" property tag.
//
// Used by `View`, `Transform`.
//
// Usage in `View`:
2024-11-13 12:56:39 +03:00
// y-axis scaling value of a 2D/3D scale. The original scale is 1. Values between 0 and 1 are used to decrease original
2024-09-18 13:50:06 +03:00
// scale, more than 1 - to increase. The default value is 1.
//
// Supported types: `float`, `int`, `string`.
//
// Internal type is `float`, other types converted to it during assignment.
//
// Usage in `Transform`:
2024-11-13 12:56:39 +03:00
// y-axis scaling value of a 2D/3D scale. The original scale is 1. Values between 0 and 1 are used to decrease original
2024-09-18 13:50:06 +03:00
// scale, more than 1 - to increase. The default value is 1.
//
// Supported types: `float`, `int`, `string`.
//
// Internal type is `float`, other types converted to it during assignment.
2024-11-13 12:56:39 +03:00
ScaleY PropertyName = "scale-y"
2024-08-13 13:52:47 +03:00
2024-09-18 13:50:06 +03:00
// ScaleZ is the constant for "scale-z" property tag.
//
// Used by `View`, `Transform`.
//
// Usage in `View`:
2024-11-13 12:56:39 +03:00
// z-axis scaling value of a 3D scale. The original scale is 1. Values between 0 and 1 are used to decrease original
2024-09-18 13:50:06 +03:00
// scale, more than 1 - to increase. The default value is 1.
//
// Supported types: `float`, `int`, `string`.
//
// Internal type is `float`, other types converted to it during assignment.
//
// Usage in `Transform`:
2024-11-13 12:56:39 +03:00
// z-axis scaling value of a 3D scale. The original scale is 1. Values between 0 and 1 are used to decrease original
2024-09-18 13:50:06 +03:00
// scale, more than 1 - to increase. The default value is 1.
//
// Supported types: `float`, `int`, `string`.
//
// Internal type is `float`, other types converted to it during assignment.
2024-11-13 12:56:39 +03:00
ScaleZ PropertyName = "scale-z"
2024-08-13 13:52:47 +03:00
2024-09-18 13:50:06 +03:00
// Rotate is the constant for "rotate" property tag.
//
// Used by `View`, `Transform`.
//
// Usage in `View`:
// Angle of the view rotation. A positive angle denotes a clockwise rotation, a negative angle a counter-clockwise.
//
// Supported types: `AngleUnit`, `string`, `float`, `int`.
//
// Internal type is `AngleUnit`, other types will be converted to it during assignment.
// See `AngleUnit` description for more details.
//
// Conversion rules:
// `AngleUnit` - stored as is, no conversion performed.
// `string` - must contain string representation of `AngleUnit`. If numeric value will be provided without any suffix then `AngleUnit` with value and `Radian` value type will be created.
// `float` - a new `AngleUnit` value will be created with `Radian` as a type.
// `int` - a new `AngleUnit` value will be created with `Radian` as a type.
//
// Usage in `Transform`:
// Angle of the view rotation. A positive angle denotes a clockwise rotation, a negative angle a counter-clockwise.
//
// Supported types: `AngleUnit`, `string`, `float`, `int`.
//
// Internal type is `AngleUnit`, other types will be converted to it during assignment.
// See `AngleUnit` description for more details.
//
// Conversion rules:
// `AngleUnit` - stored as is, no conversion performed.
// `string` - must contain string representation of `AngleUnit`. If numeric value will be provided without any suffix then `AngleUnit` with value and `Radian` value type will be created.
// `float` - a new `AngleUnit` value will be created with `Radian` as a type.
// `int` - a new `AngleUnit` value will be created with `Radian` as a type.
2024-11-13 12:56:39 +03:00
Rotate PropertyName = "rotate"
2024-08-13 13:52:47 +03:00
2024-09-18 13:50:06 +03:00
// RotateX is the constant for "rotate-x" property tag.
//
// Used by `View`, `Transform`.
//
// Usage in `View`:
// x-coordinate of the vector denoting the axis of rotation in range 0 to 1.
//
// Supported types: `float`, `int`, `string`.
//
// Internal type is `float`, other types converted to it during assignment.
//
// Usage in `Transform`:
// x-coordinate of the vector denoting the axis of rotation in range 0 to 1.
//
// Supported types: `float`, `int`, `string`.
//
// Internal type is `float`, other types converted to it during assignment.
2024-11-13 12:56:39 +03:00
RotateX PropertyName = "rotate-x"
2024-08-13 13:52:47 +03:00
2024-09-18 13:50:06 +03:00
// RotateY is the constant for "rotate-y" property tag.
//
// Used by `View`, `Transform`.
//
// Usage in `View`:
// y-coordinate of the vector denoting the axis of rotation in range 0 to 1.
//
// Supported types: `float`, `int`, `string`.
//
// Internal type is `float`, other types converted to it during assignment.
//
// Usage in `Transform`:
// y-coordinate of the vector denoting the axis of rotation in range 0 to 1.
//
// Supported types: `float`, `int`, `string`.
//
// Internal type is `float`, other types converted to it during assignment.
2024-11-13 12:56:39 +03:00
RotateY PropertyName = "rotate-y"
2024-08-13 13:52:47 +03:00
2024-09-18 13:50:06 +03:00
// RotateZ is the constant for "rotate-z" property tag.
//
// Used by `View`, `Transform`.
//
// Usage in `View`:
// z-coordinate of the vector denoting the axis of rotation in range 0 to 1.
//
// Supported types: `float`, `int`, `string`.
//
// Internal type is `float`, other types converted to it during assignment.
//
// Usage in `Transform`:
// z-coordinate of the vector denoting the axis of rotation in range 0 to 1.
//
// Supported types: `float`, `int`, `string`.
//
// Internal type is `float`, other types converted to it during assignment.
2024-11-13 12:56:39 +03:00
RotateZ PropertyName = "rotate-z"
2024-08-13 13:52:47 +03:00
2024-09-18 13:50:06 +03:00
// SkewX is the constant for "skew-x" property tag.
//
// Used by `View`, `Transform`.
//
// Usage in `View`:
// Angle to use to distort the element along the abscissa. The default value is 0.
//
// Supported types: `AngleUnit`, `string`, `float`, `int`.
//
// Internal type is `AngleUnit`, other types will be converted to it during assignment.
// See `AngleUnit` description for more details.
//
// Conversion rules:
// `AngleUnit` - stored as is, no conversion performed.
// `string` - must contain string representation of `AngleUnit`. If numeric value will be provided without any suffix then `AngleUnit` with value and `Radian` value type will be created.
// `float` - a new `AngleUnit` value will be created with `Radian` as a type.
// `int` - a new `AngleUnit` value will be created with `Radian` as a type.
//
// Usage in `Transform`:
// Angle to use to distort the element along the abscissa. The default value is 0.
//
// Supported types: `AngleUnit`, `string`, `float`, `int`.
//
// Internal type is `AngleUnit`, other types will be converted to it during assignment.
// See `AngleUnit` description for more details.
//
// Conversion rules:
// `AngleUnit` - stored as is, no conversion performed.
// `string` - must contain string representation of `AngleUnit`. If numeric value will be provided without any suffix then `AngleUnit` with value and `Radian` value type will be created.
// `float` - a new `AngleUnit` value will be created with `Radian` as a type.
// `int` - a new `AngleUnit` value will be created with `Radian` as a type.
2024-11-13 12:56:39 +03:00
SkewX PropertyName = "skew-x"
2024-08-13 13:52:47 +03:00
2024-09-18 13:50:06 +03:00
// SkewY is the constant for "skew-y" property tag.
//
// Used by `View`, `Transform`.
//
// Usage in `View`:
// Angle to use to distort the element along the ordinate. The default value is 0.
//
// Supported types: `AngleUnit`, `string`, `float`, `int`.
//
// Internal type is `AngleUnit`, other types will be converted to it during assignment.
// See `AngleUnit` description for more details.
//
// Conversion rules:
// `AngleUnit` - stored as is, no conversion performed.
// `string` - must contain string representation of `AngleUnit`. If numeric value will be provided without any suffix then `AngleUnit` with value and `Radian` value type will be created.
// `float` - a new `AngleUnit` value will be created with `Radian` as a type.
// `int` - a new `AngleUnit` value will be created with `Radian` as a type.
//
// Usage in `Transform`:
// Angle to use to distort the element along the ordinate. The default value is 0.
//
// Supported types: `AngleUnit`, `string`, `float`, `int`.
//
// Internal type is `AngleUnit`, other types will be converted to it during assignment.
// See `AngleUnit` description for more details.
//
// Conversion rules:
// `AngleUnit` - stored as is, no conversion performed.
// `string` - must contain string representation of `AngleUnit`. If numeric value will be provided without any suffix then `AngleUnit` with value and `Radian` value type will be created.
// `float` - a new `AngleUnit` value will be created with `Radian` as a type.
// `int` - a new `AngleUnit` value will be created with `Radian` as a type.
2024-11-13 12:56:39 +03:00
SkewY PropertyName = "skew-y"
2021-09-07 17:36:50 +03:00
)
2024-08-13 13:52:47 +03:00
// Transform interface specifies view transformation parameters: the x-, y-, and z-axis translation values,
// the x-, y-, and z-axis scaling values, the angle to use to distort the element along the abscissa and ordinate,
// the angle of the view rotation.
2024-11-13 12:56:39 +03:00
// Valid property tags: Perspective ("perspective"), TranslateX ("translate-x"), TranslateY ("translate-y"), TranslateZ ("translate-z"),
2024-08-13 13:52:47 +03:00
// ScaleX ("scale-x"), ScaleY ("scale-y"), ScaleZ ("scale-z"), Rotate ("rotate"), RotateX ("rotate-x"),
// RotateY ("rotate-y"), RotateZ ("rotate-z"), SkewX ("skew-x"), and SkewY ("skew-y")
type Transform interface {
Properties
fmt . Stringer
stringWriter
2024-11-13 12:56:39 +03:00
transformCSS ( session Session ) string
2024-08-13 13:52:47 +03:00
}
type transformData struct {
2024-11-13 12:56:39 +03:00
dataProperty
2024-08-13 13:52:47 +03:00
}
2024-09-12 14:05:11 +03:00
// NewTransform creates a new transform property data and return its interface
2024-08-13 13:52:47 +03:00
func NewTransform ( params Params ) Transform {
transform := new ( transformData )
2024-11-13 12:56:39 +03:00
transform . init ( )
2024-08-13 13:52:47 +03:00
for tag , value := range params {
transform . Set ( tag , value )
}
return transform
}
2024-11-13 12:56:39 +03:00
func ( transform * transformData ) init ( ) {
transform . dataProperty . init ( )
transform . set = transformSet
transform . supportedProperties = [ ] PropertyName {
RotateX , RotateY , RotateZ , Rotate , SkewX , SkewY , ScaleX , ScaleY , ScaleZ ,
Perspective , TranslateX , TranslateY , TranslateZ ,
}
}
func ( transform * transformData ) String ( ) string {
buffer := allocStringBuilder ( )
defer freeStringBuilder ( buffer )
transform . writeString ( buffer , "" )
return buffer . String ( )
}
func ( transform * transformData ) writeString ( buffer * strings . Builder , indent string ) {
buffer . WriteString ( "_{ " )
comma := false
for _ , tag := range transform . supportedProperties {
if value , ok := transform . properties [ tag ] ; ok {
if comma {
buffer . WriteString ( ", " )
}
buffer . WriteString ( string ( tag ) )
buffer . WriteString ( " = " )
writePropertyValue ( buffer , tag , value , indent )
comma = true
}
}
buffer . WriteString ( " }" )
}
func transformSet ( properties Properties , tag PropertyName , value any ) [ ] PropertyName {
switch tag {
case RotateX , RotateY , RotateZ :
return setFloatProperty ( properties , tag , value , 0 , 1 )
case Rotate , SkewX , SkewY :
return setAngleProperty ( properties , tag , value )
case ScaleX , ScaleY , ScaleZ :
return setFloatProperty ( properties , tag , value , - math . MaxFloat64 , math . MaxFloat64 )
case Perspective , TranslateX , TranslateY , TranslateZ :
return setSizeProperty ( properties , tag , value )
}
return nil
}
func setTransformProperty ( properties Properties , value any ) bool {
2024-08-13 13:52:47 +03:00
setObject := func ( obj DataObject ) bool {
transform := NewTransform ( nil )
ok := true
for i := 0 ; i < obj . PropertyCount ( ) ; i ++ {
if prop := obj . Property ( i ) ; prop . Type ( ) == TextNode {
2024-11-13 12:56:39 +03:00
if ! transform . Set ( PropertyName ( prop . Tag ( ) ) , prop . Text ( ) ) {
2024-08-13 13:52:47 +03:00
ok = false
}
} else {
ok = false
}
}
2024-11-13 12:56:39 +03:00
if ! ok && transform . empty ( ) {
2024-08-13 13:52:47 +03:00
return false
}
2024-11-13 12:56:39 +03:00
properties . setRaw ( TransformTag , transform )
2024-08-13 13:52:47 +03:00
return true
}
switch value := value . ( type ) {
case Transform :
2024-11-13 12:56:39 +03:00
properties . setRaw ( TransformTag , value )
2024-08-13 16:27:54 +03:00
return true
2024-08-13 13:52:47 +03:00
case DataObject :
return setObject ( value )
case DataNode :
if obj := value . Object ( ) ; obj != nil {
return setObject ( obj )
}
notCompatibleType ( TransformTag , value )
return false
case string :
if obj := ParseDataText ( value ) ; obj != nil {
return setObject ( obj )
}
notCompatibleType ( TransformTag , value )
return false
}
return false
}
2024-11-13 12:56:39 +03:00
func getTransformProperty ( properties Properties ) Transform {
if val := properties . getRaw ( TransformTag ) ; val != nil {
2024-08-13 13:52:47 +03:00
if transform , ok := val . ( Transform ) ; ok {
return transform
}
}
return nil
}
2024-11-13 12:56:39 +03:00
func setTransformPropertyElement ( properties Properties , tag PropertyName , value any ) [ ] PropertyName {
2024-08-13 13:52:47 +03:00
switch tag {
2024-11-13 12:56:39 +03:00
case Perspective , RotateX , RotateY , RotateZ , Rotate , SkewX , SkewY , ScaleX , ScaleY , ScaleZ , TranslateX , TranslateY , TranslateZ :
if transform := getTransformProperty ( properties ) ; transform != nil {
if result := transformSet ( transform , tag , value ) ; result != nil {
result = append ( result , TransformTag )
}
} else {
transform := NewTransform ( nil )
if result := transformSet ( transform , tag , value ) ; result != nil {
properties . setRaw ( TransformTag , transform )
result = append ( result , TransformTag )
2024-08-13 13:52:47 +03:00
}
}
2024-11-13 12:56:39 +03:00
default :
ErrorLogF ( ` "Transform" interface does not support the "%s" property ` , tag )
2024-08-13 13:52:47 +03:00
}
2024-11-13 12:56:39 +03:00
return nil
2024-08-13 13:52:47 +03:00
}
2021-09-07 17:36:50 +03:00
func getTransform3D ( style Properties , session Session ) bool {
perspective , ok := sizeProperty ( style , Perspective , session )
return ok && perspective . Type != Auto && perspective . Value != 0
}
func getPerspectiveOrigin ( style Properties , session Session ) ( SizeUnit , SizeUnit ) {
x , _ := sizeProperty ( style , PerspectiveOriginX , session )
y , _ := sizeProperty ( style , PerspectiveOriginY , session )
return x , y
}
func getOrigin ( style Properties , session Session ) ( SizeUnit , SizeUnit , SizeUnit ) {
x , _ := sizeProperty ( style , OriginX , session )
y , _ := sizeProperty ( style , OriginY , session )
z , _ := sizeProperty ( style , OriginZ , session )
return x , y , z
}
2024-08-13 13:52:47 +03:00
func ( transform * transformData ) getSkew ( session Session ) ( AngleUnit , AngleUnit , bool ) {
skewX , okX := angleProperty ( transform , SkewX , session )
skewY , okY := angleProperty ( transform , SkewY , session )
2021-10-04 17:58:17 +03:00
return skewX , skewY , okX || okY
2021-09-07 17:36:50 +03:00
}
2024-08-13 13:52:47 +03:00
func ( transform * transformData ) getTranslate ( session Session ) ( SizeUnit , SizeUnit , SizeUnit ) {
x , _ := sizeProperty ( transform , TranslateX , session )
y , _ := sizeProperty ( transform , TranslateY , session )
z , _ := sizeProperty ( transform , TranslateZ , session )
2021-09-07 17:36:50 +03:00
return x , y , z
}
2024-11-13 12:56:39 +03:00
func ( transform * transformData ) transformCSS ( session Session ) string {
2021-09-07 17:36:50 +03:00
buffer := allocStringBuilder ( )
defer freeStringBuilder ( buffer )
2024-11-13 12:56:39 +03:00
if perspective , ok := sizeProperty ( transform , Perspective , session ) ; ok && perspective . Type != Auto && perspective . Value != 0 {
buffer . WriteString ( ` perspective( ` )
buffer . WriteString ( perspective . cssString ( "0" , session ) )
buffer . WriteString ( ") " )
}
2024-08-13 13:52:47 +03:00
skewX , skewY , skewOK := transform . getSkew ( session )
2021-10-04 17:58:17 +03:00
if skewOK {
2021-09-07 17:36:50 +03:00
buffer . WriteString ( ` skew( ` )
buffer . WriteString ( skewX . cssString ( ) )
buffer . WriteRune ( ',' )
buffer . WriteString ( skewY . cssString ( ) )
2024-11-13 12:56:39 +03:00
buffer . WriteString ( ") " )
2021-09-07 17:36:50 +03:00
}
2024-08-13 13:52:47 +03:00
x , y , z := transform . getTranslate ( session )
2024-11-13 12:56:39 +03:00
if z . Type != Auto && z . Value != 0 {
buffer . WriteString ( ` translate3d( ` )
buffer . WriteString ( x . cssString ( "0" , session ) )
buffer . WriteRune ( ',' )
buffer . WriteString ( y . cssString ( "0" , session ) )
buffer . WriteRune ( ',' )
buffer . WriteString ( z . cssString ( "0" , session ) )
buffer . WriteString ( ") " )
} else if ( x . Type != Auto && x . Value != 0 ) || ( y . Type != Auto && y . Value != 0 ) {
buffer . WriteString ( ` translate( ` )
buffer . WriteString ( x . cssString ( "0" , session ) )
buffer . WriteRune ( ',' )
buffer . WriteString ( y . cssString ( "0" , session ) )
buffer . WriteString ( ") " )
}
2022-08-18 18:18:36 +03:00
2024-08-13 13:52:47 +03:00
scaleX , okScaleX := floatTextProperty ( transform , ScaleX , session , 1 )
scaleY , okScaleY := floatTextProperty ( transform , ScaleY , session , 1 )
2024-11-13 12:56:39 +03:00
scaleZ , okScaleZ := floatTextProperty ( transform , ScaleZ , session , 1 )
if okScaleZ {
2022-08-18 18:18:36 +03:00
2024-11-13 12:56:39 +03:00
buffer . WriteString ( ` scale3d( ` )
buffer . WriteString ( scaleX )
buffer . WriteRune ( ',' )
buffer . WriteString ( scaleY )
buffer . WriteRune ( ',' )
buffer . WriteString ( scaleZ )
buffer . WriteString ( ") " )
2021-09-07 17:36:50 +03:00
2024-11-13 12:56:39 +03:00
} else if okScaleX || okScaleY {
2021-09-07 17:36:50 +03:00
2024-11-13 12:56:39 +03:00
buffer . WriteString ( ` scale( ` )
buffer . WriteString ( scaleX )
buffer . WriteRune ( ',' )
buffer . WriteString ( scaleY )
buffer . WriteString ( ") " )
}
if angle , ok := angleProperty ( transform , Rotate , session ) ; ok {
rotateX , xOK := floatTextProperty ( transform , RotateX , session , 1 )
rotateY , yOK := floatTextProperty ( transform , RotateY , session , 1 )
rotateZ , zOK := floatTextProperty ( transform , RotateZ , session , 1 )
if xOK || yOK || zOK {
2022-08-18 18:18:36 +03:00
2021-09-07 17:36:50 +03:00
buffer . WriteString ( ` rotate3d( ` )
2022-08-18 18:18:36 +03:00
buffer . WriteString ( rotateX )
2021-09-07 17:36:50 +03:00
buffer . WriteRune ( ',' )
2022-08-18 18:18:36 +03:00
buffer . WriteString ( rotateY )
2021-09-07 17:36:50 +03:00
buffer . WriteRune ( ',' )
2022-08-18 18:18:36 +03:00
buffer . WriteString ( rotateZ )
2021-09-07 17:36:50 +03:00
buffer . WriteRune ( ',' )
buffer . WriteString ( angle . cssString ( ) )
2024-11-13 12:56:39 +03:00
buffer . WriteString ( ") " )
2021-09-07 17:36:50 +03:00
2024-11-13 12:56:39 +03:00
} else {
2021-09-07 17:36:50 +03:00
buffer . WriteString ( ` rotate( ` )
buffer . WriteString ( angle . cssString ( ) )
2024-11-13 12:56:39 +03:00
buffer . WriteString ( ") " )
2021-09-07 17:36:50 +03:00
}
}
2024-11-13 12:56:39 +03:00
length := buffer . Len ( )
if length == 0 {
return ""
}
result := buffer . String ( )
return result [ : length - 1 ]
2021-09-07 17:36:50 +03:00
}
func ( style * viewStyle ) writeViewTransformCSS ( builder cssBuilder , session Session ) {
2024-11-13 12:56:39 +03:00
x , y := getPerspectiveOrigin ( style , session )
if x . Type != Auto || y . Type != Auto {
builder . addValues ( ` perspective-origin ` , ` ` , x . cssString ( "50%" , session ) , y . cssString ( "50%" , session ) )
}
2021-09-07 17:36:50 +03:00
2024-11-13 12:56:39 +03:00
if backfaceVisible , ok := boolProperty ( style , BackfaceVisible , session ) ; ok {
if backfaceVisible {
builder . add ( ` backface-visibility ` , ` visible ` )
} else {
builder . add ( ` backface-visibility ` , ` hidden ` )
2021-09-07 17:36:50 +03:00
}
2024-11-13 12:56:39 +03:00
}
2021-09-07 17:36:50 +03:00
2024-11-13 12:56:39 +03:00
x , y , z := getOrigin ( style , session )
if z . Type != Auto && z . Value != 0 {
builder . addValues ( ` transform-origin ` , ` ` , x . cssString ( "50%" , session ) , y . cssString ( "50%" , session ) , z . cssString ( "0" , session ) )
} else if x . Type != Auto || y . Type != Auto {
builder . addValues ( ` transform-origin ` , ` ` , x . cssString ( "50%" , session ) , y . cssString ( "50%" , session ) )
2021-09-07 17:36:50 +03:00
}
2024-11-13 12:56:39 +03:00
if transform := getTransformProperty ( style ) ; transform != nil {
builder . add ( ` transform ` , transform . transformCSS ( session ) )
2024-08-13 13:52:47 +03:00
}
2021-09-07 17:36:50 +03:00
}
2024-11-13 12:56:39 +03:00
/ *
func ( view * viewData ) updateTransformProperty ( tag PropertyName ) bool {
2021-09-07 17:36:50 +03:00
htmlID := view . htmlID ( )
session := view . session
switch tag {
case PerspectiveOriginX , PerspectiveOriginY :
if getTransform3D ( view , session ) {
2022-08-31 22:17:46 +03:00
x , y := GetPerspectiveOrigin ( view )
2021-09-07 17:36:50 +03:00
value := ""
if x . Type != Auto || y . Type != Auto {
2022-09-05 14:00:07 +03:00
value = x . cssString ( "50%" , session ) + " " + y . cssString ( "50%" , session )
2021-09-07 17:36:50 +03:00
}
2022-10-30 17:22:33 +03:00
session . updateCSSProperty ( htmlID , "perspective-origin" , value )
2021-09-07 17:36:50 +03:00
}
case BackfaceVisible :
if getTransform3D ( view , session ) {
2022-08-31 22:17:46 +03:00
if GetBackfaceVisible ( view ) {
2024-11-13 12:56:39 +03:00
session . updateCSSProperty ( htmlID , string ( BackfaceVisible ) , "visible" )
2021-09-07 17:36:50 +03:00
} else {
2024-11-13 12:56:39 +03:00
session . updateCSSProperty ( htmlID , string ( BackfaceVisible ) , "hidden" )
2021-09-07 17:36:50 +03:00
}
}
case OriginX , OriginY , OriginZ :
x , y , z := getOrigin ( view , session )
value := ""
2024-11-13 12:56:39 +03:00
if z . Type != Auto {
value = x . cssString ( "50%" , session ) + " " + y . cssString ( "50%" , session ) + " " + z . cssString ( "50%" , session )
} else if x . Type != Auto || y . Type != Auto {
value = x . cssString ( "50%" , session ) + " " + y . cssString ( "50%" , session )
2021-09-07 17:36:50 +03:00
}
2022-10-30 17:22:33 +03:00
session . updateCSSProperty ( htmlID , "transform-origin" , value )
2021-09-07 17:36:50 +03:00
2024-08-13 13:52:47 +03:00
case TransformTag , SkewX , SkewY , TranslateX , TranslateY , TranslateZ ,
ScaleX , ScaleY , ScaleZ , Rotate , RotateX , RotateY , RotateZ :
2024-11-13 12:56:39 +03:00
if transform := getTransformProperty ( view ) ; transform != nil {
session . updateCSSProperty ( htmlID , "transform" , transform . transformCSS ( session ) )
2024-08-13 13:52:47 +03:00
} else {
session . updateCSSProperty ( htmlID , "transform" , "" )
}
2021-09-07 17:36:50 +03:00
default :
return false
}
return true
}
2024-11-13 12:56:39 +03:00
* /