Ejemplo n.º 1
0
            public static IElement Inside(XdShapeJson shape, ElementParameter parameter, float?shapeR)
            {
                var strokeWidth = parameter.StrokeWidth ?? 1f;

                return(new GroupElement
                {
                    Parameter = parameter, Children = new IElement[]
                    {
                        new CircleElement
                        {
                            Parameter = new ElementParameter
                            {
                                EnableStroke = true,
                            },
                            Cx = shape.Cx,
                            Cy = shape.Cy,
                            R = shapeR ?? 1f,
                        },
                        new CircleElement
                        {
                            Parameter = new ElementParameter
                            {
                                EnableFill = true,
                            },
                            Cx = shape.Cx,
                            Cy = shape.Cy,
                            R = (shapeR ?? 1f) - strokeWidth / 2f,
                        },
                    }
                });
            }
Ejemplo n.º 2
0
            public static IElement Inside(XdShapeJson shape, ElementParameter parameter)
            {
                var strokeWidth = parameter.StrokeWidth ?? 1f;

                return(new GroupElement
                {
                    Parameter = parameter, Children = new IElement[]
                    {
                        new EllipseElement
                        {
                            Parameter = new ElementParameter
                            {
                                EnableStroke = true,
                            },
                            Cx = shape.Cx,
                            Cy = shape.Cy,
                            Rx = shape.Rx,
                            Ry = shape.Ry,
                        },
                        new EllipseElement
                        {
                            Parameter = new ElementParameter
                            {
                                EnableFill = true,
                            },
                            Cx = shape.Cx,
                            Cy = shape.Cy,
                            Rx = shape.Rx - strokeWidth / 2f,
                            Ry = shape.Ry - strokeWidth / 2f,
                        },
                    }
                });
            }
Ejemplo n.º 3
0
            public static IElement Inside(XdShapeJson shape, ElementParameter parameter)
            {
                var strokeWidth = parameter.StrokeWidth ?? 1f;
                var rx          = parameter.Rx;

                parameter.Rx = null;
                return(new GroupElement
                {
                    Parameter = parameter, Children = new IElement[]
                    {
                        new RectElement
                        {
                            Parameter = new ElementParameter
                            {
                                EnableStroke = true,
                                Rx = rx,
                            },
                            Width = shape.Width,
                            Height = shape.Height,
                        },
                        new RectElement
                        {
                            Parameter = new ElementParameter
                            {
                                X = strokeWidth / 2f,
                                Y = strokeWidth / 2f,
                                EnableFill = true,
                                Rx = rx - strokeWidth / 2f,
                            },
                            Width = shape.Width - strokeWidth,
                            Height = shape.Height - strokeWidth,
                        },
                    }
                });
            }
Ejemplo n.º 4
0
        public static ElementParameter GetParameter(this Parameter p, Document doc)
        {
            var param = new ElementParameter
            {
                Id    = p.Id.IntegerValue,
                Name  = p.Definition.Name,
                Value = ParameterUtils.ParameterToString(doc, p)
            };

            if (p.IsShared)
            {
                param.Guid = p.GUID.ToString().TrimStart('{').TrimEnd('}');
            }
            return(param);
        }
Ejemplo n.º 5
0
 public static IElement Inside(XdShapeJson shape, ElementParameter parameter)
 {
     parameter.Rx = null;
     return(new GroupElement
     {
         Parameter = parameter, Children = new IElement[]
         {
             new PolygonElement
             {
                 Parameter = new ElementParameter(),
                 Points = shape.Points,
             },
         }
     });
 }
Ejemplo n.º 6
0
        bool Function(ElementBaseJson func, out FunctionExpression expr)
        {
            if (!Match(Kw(func.Name), false))
            {
                expr = null;
                return(false);
            }

            // Get the parameter values.
            List <ITTEExpression> values = new List <ITTEExpression>();

            if (Match("("))
            {
                int currentParameter = 0;
                do
                {
                    ElementParameter parameter = null;
                    if (func.Parameters != null && currentParameter < func.Parameters.Length)
                    {
                        parameter = func.Parameters[currentParameter];
                    }

                    // Variable reference
                    if (parameter != null && parameter.IsVariableReference)
                    {
                        // Match the variable parameter.
                        if (!Identifier(out string identifier))
                        {
                            throw new Exception("Failed to retrieve identifier of variable parameter.");
                        }

                        AddIfOmitted(identifier, parameter.VariableReferenceIsGlobal.Value);
                        values.Add(new AnonymousVariableExpression(identifier, parameter.VariableReferenceIsGlobal.Value));
                    }
                    // Enumerator
                    else if (parameter?.Type != null && ElementRoot.Instance.TryGetEnum(parameter.Type, out var enumerator))
                    {
                        // Match enum member
                        foreach (var member in enumerator.Members.OrderByDescending(m => m.Name.Length))
                        {
                            if (Match(Kw(member.DecompileName()), false))
                            {
                                values.Add(new ConstantEnumeratorExpression(member));
                                break;
                            }
                        }
                    }
                    // Normal parameter
                    else
                    {
                        if (ContainExpression(out ITTEExpression value))
                        {
                            values.Add(value);
                        }
                    }

                    // Increment the current parameter.
                    currentParameter++;
                }while (Match(","));
                Match(")");
            }

            expr = new FunctionExpression(func, values.ToArray());
            return(true);
        }
Ejemplo n.º 7
0
        private static IElement CreateSvgLine(XdObjectJson xdObject, List <IDefElement> defs)
        {
            var id       = xdObject.GetSimpleName().Replace(" ", "_");
            var dataName = xdObject.GetSimpleName();
            var shape    = xdObject.Shape;

            var parameter = new ElementParameter
            {
                Id = id
            };

            parameter.Transform = new Transform {
                Value = xdObject.Transform
            };

            var opacity = xdObject.Style?.Opacity;

            parameter.Opacity = opacity;

            if (xdObject.Group != null)
            {
                if (MaskGroupParser.Is(xdObject))
                {
                    var clipPathId = $"clip-path{defs.Count}";
                    parameter.ClipPath = $"url(#{clipPathId})";
                    var clipPathChildren = xdObject.Meta.Ux.ClipPathResources.Children.Select(x => CreateSvgLine(x, defs));
                    defs.Add(new ClipPathDefElement {
                        Id = clipPathId, Children = clipPathChildren.ToArray()
                    });
                }

                parameter.DataName = dataName;
                var blendMode = xdObject.Style?.BlendMode;
                var isolation = xdObject.Style?.Isolation;
                var children  = xdObject.Group.Children.Select(x => CreateSvgLine(x, defs)).ToArray();
                return(new GroupElement {
                    Parameter = parameter, Children = children, BlendMode = blendMode, Isolation = isolation
                });
            }

            XdStyleFillPatternJson image = null;
            var fill = xdObject.Style?.Fill;

            parameter.EnableFill = true;
            if (fill != null && fill.Type != "none")
            {
                var color = xdObject.Style.Fill.ToUnityColor();
                parameter.Fill = color;

                if (fill.Type == "solid" || fill.Type == "gradient")
                {
                    // nothing to do
                    // gradientはサポートしていないが、知らないタイプというわけではないのでスルー
                }
                else if (fill.Type == "pattern")
                {
                    image          = fill.Pattern;
                    parameter.Fill = null;
                }
                else
                {
                    Debug.LogWarning($"Unknown fill type {fill.Type} in {xdObject.Name}");
                }

                if (!string.IsNullOrWhiteSpace(shape.Winding))
                {
                    parameter.FillRule = shape.Winding;
                }
            }

            float?shapeR = null;

            if (shape.R != null)
            {
                if (shape.R is List <object> list)
                {
                    shapeR = (float)(double)list[0];
                }
                else if (shape.R is double d)
                {
                    shapeR = (float)d;
                }
                else
                {
                    throw new NotSupportedException($"Unknown shape.r type {shape.R.GetType()}");
                }
            }

            if (shapeR != null && shape.Type != CircleElement.Name)
            {
                parameter.Rx = shapeR;
                if (parameter.Rx > shape.Width / 2f)
                {
                    parameter.Rx = shape.Width / 2f;
                }
                if (parameter.Rx > shape.Height / 2f)
                {
                    parameter.Rx = shape.Height / 2f;
                }
            }

            var    stroke      = xdObject.Style?.Stroke;
            string strokeAlign = null;

            if (stroke != null && stroke.Type != "none")
            {
                parameter.EnableStroke     = true;
                parameter.Stroke           = stroke.ToUnityColor();
                parameter.StrokeWidth      = stroke.Width;
                parameter.StrokeMiterLimit = stroke.MiterLimit;

                if (!string.IsNullOrWhiteSpace(stroke.Join))
                {
                    parameter.StrokeLinejoin = stroke.Join;
                }

                if (!string.IsNullOrWhiteSpace(stroke.Cap))
                {
                    parameter.StrokeLinecap = stroke.Cap;
                }

                if (stroke.Dash != null)
                {
                    parameter.StrokeDasharray = stroke.Dash;
                }

                if (stroke.Align == null)
                {
                    strokeAlign = null;
                }
                else if (stroke.Align == "outside")
                {
                    strokeAlign = "outside";
                }
                else if (stroke.Align == "inside")
                {
                    strokeAlign = "inside";
                }
                else
                {
                    throw new NotSupportedException($"{xdObject} has unknown align type {stroke.Align}");
                }
            }

            if (image != null)
            {
                var imageBytes = XdImporter.XdFile.GetResource(fill.Pattern.Meta);
                return(new ImageElement {
                    Parameter = parameter, ImageBytes = imageBytes, Width = shape.Width, Height = shape.Height
                });
            }

            if (shape.Type == PathElement.Name)
            {
                return new PathElement {
                           Parameter = parameter, D = shape.Path
                }
            }
            ;

            if (shape.Type == CompoundElement.Name)
            {
                return new CompoundElement {
                           Parameter = parameter, D = shape.Path
                }
            }
            ;

            if (shape.Type == LineElement.Name)
            {
                return new LineElement {
                           Parameter = parameter, X1 = shape.X1, Y1 = shape.Y1, X2 = shape.X2, Y2 = shape.Y2
                }
            }
            ;

            if (shape.Type == RectElement.Name)
            {
                if (strokeAlign == "outside")
                {
                    return(RectElement.Outside(shape, parameter));
                }
                if (strokeAlign == "inside")
                {
                    return(RectElement.Inside(shape, parameter));
                }
                return(new RectElement {
                    Parameter = parameter, Width = shape.Width, Height = shape.Height
                });
            }

            if (shape.Type == CircleElement.Name)
            {
                if (strokeAlign == "outside")
                {
                    return(CircleElement.Outside(shape, parameter, shapeR));
                }
                if (strokeAlign == "inside")
                {
                    return(CircleElement.Inside(shape, parameter, shapeR));
                }
                return(new CircleElement {
                    Parameter = parameter, Cx = shape.Cx, Cy = shape.Cy, R = shapeR.Value
                });
            }

            if (shape.Type == EllipseElement.Name)
            {
                if (strokeAlign == "outside")
                {
                    return(EllipseElement.Outside(shape, parameter));
                }
                if (strokeAlign == "inside")
                {
                    return(EllipseElement.Inside(shape, parameter));
                }
                return(new EllipseElement {
                    Parameter = parameter, Cx = shape.Cx, Cy = shape.Cy, Rx = shape.Rx, Ry = shape.Ry
                });
            }

            throw new NotSupportedException($"Unknown type {shape.Type}");
        }
Ejemplo n.º 8
0
        private static IElement CreateSvgLine(XdObjectJson xdObject, List <IDefElement> defs)
        {
            var id       = xdObject.Name.Replace(" ", "_");
            var dataName = xdObject.Name;
            var shape    = xdObject.Shape;

            var parameter = new ElementParameter
            {
                Id = id
            };

            var tx = xdObject.Transform?.Tx ?? 0f;
            var ty = xdObject.Transform?.Ty ?? 0f;

            parameter.Transform.X = tx;
            parameter.Transform.Y = ty;

            var opacity = xdObject.Style?.Opacity;

            parameter.Opacity = opacity;

            if (xdObject.Group != null)
            {
                parameter.DataName = dataName;
                if (xdObject.Meta?.Ux?.ClipPathResources?.Type == "clipPath")
                {
                    var clipPath = xdObject.Meta.Ux.ClipPathResources.Children[0];
                    parameter.ClipPath = "url(#clip-path)";

                    var clipPathPath = new PathElement
                    {
                        Parameter = new ElementParameter
                        {
                            Id        = "_Clipping_Path_",
                            DataName  = "Clipping Path",
                            Transform = new Transform
                            {
                                X = clipPath.Transform.Tx,
                                Y = clipPath.Transform.Ty,
                            },
                        },
                        D = clipPath.Shape.Path,
                    };
                    defs.Add(new ClipPathDefElement {
                        Id = "clip-path", Path = clipPathPath
                    });
                }

                var children = xdObject.Group.Children.Select(x => CreateSvgLine(x, defs)).ToArray();
                return(new GroupElement {
                    Parameter = parameter, Children = children
                });
            }

            var fill = xdObject.Style?.Fill;

            parameter.EnableFill = true;
            if (fill != null && fill.Type != "none")
            {
                var color = xdObject.GetFillColor();
                parameter.Fill = color;

                if (!string.IsNullOrWhiteSpace(shape.Winding))
                {
                    parameter.FillRule = shape.Winding;
                }
            }

            float?shapeR = null;

            if (shape.R != null)
            {
                if (shape.R is Newtonsoft.Json.Linq.JValue jValue)
                {
                    shapeR = (float)jValue;
                }
                else if (shape.R is Newtonsoft.Json.Linq.JArray jArray)
                {
                    shapeR = (float)jArray[0];
                }
                else if (shape.R is long l)
                {
                    shapeR = l;
                }
                else if (shape.R is double d)
                {
                    shapeR = (float)d;
                }
                else
                {
                    throw new NotSupportedException($"Unknown shape.r type {shape.R.GetType()}");
                }
            }

            if (shapeR != null && shape.Type != CircleElement.Name)
            {
                parameter.Rx = shapeR;
                if (parameter.Rx > shape.Width / 2f)
                {
                    parameter.Rx = shape.Width / 2f;
                }
                if (parameter.Rx > shape.Height / 2f)
                {
                    parameter.Rx = shape.Height / 2f;
                }
            }

            var    stroke      = xdObject.Style?.Stroke;
            string strokeAlign = null;

            if (stroke != null && stroke.Type != "none")
            {
                parameter.EnableStroke     = true;
                parameter.Stroke           = stroke.Color.Value;
                parameter.StrokeWidth      = stroke.Width;
                parameter.StrokeMiterLimit = stroke.MiterLimit;

                if (!string.IsNullOrWhiteSpace(stroke.Join))
                {
                    parameter.StrokeLinejoin = stroke.Join;
                }

                if (!string.IsNullOrWhiteSpace(stroke.Cap))
                {
                    parameter.StrokeLinecap = stroke.Cap;
                }

                if (stroke.Dash != null)
                {
                    parameter.StrokeDasharray = stroke.Dash;
                }

                if (stroke.Align == null)
                {
                    strokeAlign = null;
                }
                else if (stroke.Align == "outside")
                {
                    strokeAlign = "outside";
                }
                else if (stroke.Align == "inside")
                {
                    strokeAlign = "inside";
                }
                else
                {
                    throw new NotSupportedException($"{xdObject} has unknown align type {stroke.Align}");
                }
            }

            if (shape.Type == PathElement.Name)
            {
                return new PathElement {
                           Parameter = parameter, D = shape.Path
                }
            }
            ;

            if (shape.Type == CompoundElement.Name)
            {
                return new CompoundElement {
                           Parameter = parameter, D = shape.Path
                }
            }
            ;

            if (shape.Type == LineElement.Name)
            {
                return new LineElement {
                           Parameter = parameter, X1 = shape.X1, Y1 = shape.Y1, X2 = shape.X2, Y2 = shape.Y2
                }
            }
            ;

            if (shape.Type == RectElement.Name)
            {
                if (strokeAlign == "outside")
                {
                    return(RectElement.Outside(shape, parameter));
                }
                if (strokeAlign == "inside")
                {
                    return(RectElement.Inside(shape, parameter));
                }
                return(new RectElement {
                    Parameter = parameter, Width = shape.Width, Height = shape.Height
                });
            }

            if (shape.Type == CircleElement.Name)
            {
                if (strokeAlign == "outside")
                {
                    return(CircleElement.Outside(shape, parameter, shapeR));
                }
                if (strokeAlign == "inside")
                {
                    return(CircleElement.Inside(shape, parameter, shapeR));
                }
                return(new CircleElement {
                    Parameter = parameter, Cx = shape.Cx, Cy = shape.Cy, R = shapeR.Value
                });
            }

            if (shape.Type == EllipseElement.Name)
            {
                if (strokeAlign == "outside")
                {
                    return(EllipseElement.Outside(shape, parameter));
                }
                if (strokeAlign == "inside")
                {
                    return(EllipseElement.Inside(shape, parameter));
                }
                return(new EllipseElement {
                    Parameter = parameter, Cx = shape.Cx, Cy = shape.Cy, Rx = shape.Rx, Ry = shape.Ry
                });
            }

            throw new NotSupportedException($"Unknown type {shape.Type}");
        }
Ejemplo n.º 9
0
        private Model GetFamilyData()
        {
            var model = new FamilyModel {
                Name = _name, Filepath = _filePath
            };

            //todo
            var fam      = _fam;
            var doc      = _doc;
            var paramSet = fam.Parameters.ForwardIterator();

            //var paramList = new List<ElementParameter>();
            while (paramSet.MoveNext())
            {
                var p     = (Parameter)paramSet.Current;
                var name  = p.Definition.Name;
                var value = ParameterUtils.ParameterToString(doc, p);
                if (String.IsNullOrWhiteSpace(value))
                {
                    value = "空";
                }
                var mfP = new ElementParameter {
                    Id = p.Id.IntegerValue, Name = name, Value = value
                };
                if (p.IsShared)
                {
                    mfP.Guid = p.GUID.ToString();
                }
                model.Parameters.Add(mfP);
            }
            var type      = doc.FamilyManager.CurrentType;
            var famParams = doc.FamilyManager.Parameters;

            //var typeParamList = new List<ElementParameter>();
            if (famParams != null)
            {
                var famIter = famParams.ForwardIterator();

                while (famIter.MoveNext())
                {
                    var p     = (FamilyParameter)famIter.Current;
                    var pDef  = p.Definition;
                    var name  = pDef.Name;
                    var value = ParameterUtils.ParameterToString(doc, p, type);
                    if (String.IsNullOrWhiteSpace(value))
                    {
                        value = "空";
                    }
                    var param = new ElementParameter {
                        Id = p.Id.IntegerValue, Name = name, Value = value
                    };
                    if (p.IsShared)
                    {
                        param.Guid = p.GUID.ToString();
                    }
                    model.FamParameters.Add(param);
                }
            }

            Category cate = null;

            if (fam.Category != null)
            {
                cate = fam.Category;
            }
            if (fam.FamilyCategory != null)
            {
                cate = fam.FamilyCategory;
            }
            if (cate != null)
            {
                model.Category = new ElementCategory {
                    Id = cate.Id.IntegerValue, Name = cate.Name
                };
            }

            model.Family = fam.GetFamily(doc);

            return(_model = model);
        }