Exemplo n.º 1
0
        /// <summary>
        /// Parse a text
        /// </summary>
        private GraphicVisual ParseText(XElement textElement,
                                        Matrix currentTransformationMatrix)
        {
            GraphicGroup graphicGroup = new GraphicGroup();


            var colorBlocks    = new List <ColorBlock>();
            var positionBlocks = new List <PositionBlock>();
            var position       = new CharacterPositions();

            var textColorBlock = new ColorBlock();

            colorBlocks.Add(textColorBlock);
            textColorBlock.Characters         = new List <PositionBlockCharacter>();
            textColorBlock.AdjustFillGlobal   = true;
            textColorBlock.AdjustStrokeGlobal = true;

            var xList = GetLengthPercentList(textElement, "x", PercentBaseSelector.ViewBoxWidth);

            if (xList == null)
            {
                xList = new List <double> {
                    0.0
                };
            }

            var dxList = GetLengthPercentList(textElement, "dx", PercentBaseSelector.ViewBoxWidth);

            position.X.SetParentValues(xList, dxList);

            var yList = GetLengthPercentList(textElement, "y", PercentBaseSelector.ViewBoxHeight);

            if (yList == null)
            {
                yList = new List <double> {
                    0.0
                };
            }

            var dyList = GetLengthPercentList(textElement, "dy", PercentBaseSelector.ViewBoxHeight);

            position.Y.SetParentValues(yList, dyList);

            var fontSize   = GetFontSize();
            var typeface   = GetTypeface();
            var textAnchor = GetTextAnchor();

            var rotation = new ParentChildPriorityList();

            rotation.ParentValues = GetRotate(textElement);

            var textOpacity       = cssStyleCascade.GetNumberPercentFromTop("opacity", 1);
            var textFillOpacity   = cssStyleCascade.GetNumberPercentFromTop("fill-opacity", 1);
            var textStrokeOpacity = cssStyleCascade.GetNumberPercentFromTop("stroke-opacity", 1);

            if (false && !textElement.HasElements)
            {
                var geometry = ParseTextGeometry(textElement, currentTransformationMatrix);

                var graphicPath = new GraphicPath();
                graphicPath.Geometry          = geometry;
                graphicPath.Geometry.FillRule = GraphicFillRule.NoneZero;

                brushParser.SetFillAndStroke(textElement, graphicPath, currentTransformationMatrix);

                return(graphicPath);
            }

            XNode node        = textElement.FirstNode;
            bool  beginOfLine = true;

            while (node != null)
            {
                var nextNode     = node.NextNode;
                var hasSuccessor = nextNode != null;

                switch (node)
                {
                case XElement embededElement:
                {
                    if (!PresentationAttribute.IsElementVisible(embededElement))
                    {
                        continue;
                    }

                    switch (embededElement.Name.LocalName)
                    {
                    case "tspan":
                    {
                        var tspanElement     = embededElement;
                        var isTspanDisplayed = PresentationAttribute.IsElementDisplayed(tspanElement);

                        cssStyleCascade.PushStyles(tspanElement);

                        var xChildList  = GetLengthPercentList(tspanElement, "x", PercentBaseSelector.ViewBoxWidth);
                        var dxChildList = GetLengthPercentList(tspanElement, "dx", PercentBaseSelector.ViewBoxWidth);
                        position.X.SetChildValues(xChildList, dxChildList);

                        var yChildList  = GetLengthPercentList(tspanElement, "y", PercentBaseSelector.ViewBoxHeight);
                        var dyChildList = GetLengthPercentList(tspanElement, "dy", PercentBaseSelector.ViewBoxHeight);
                        position.Y.SetChildValues(yChildList, dyChildList);

                        var hasOwnFill   = ExistsAttributeOnTop("fill");
                        var hasOwnStroke = ExistsAttributeOnTop("stroke");

                        var tspanFontSize = GetFontSize();
                        var tspanTypeface = GetTypeface();
                        var tspanAnchor   = GetTextAnchor();
                        rotation.ChildValues = GetRotate(tspanElement);

                        var charBlock = Vectorize(positionBlocks,
                                                  tspanElement.Value,
                                                  tspanAnchor,
                                                  position,
                                                  beginOfLine,
                                                  hasSuccessor,
                                                  tspanTypeface,
                                                  tspanFontSize,
                                                  rotation,
                                                  currentTransformationMatrix);

                        rotation.ChildValues = null;
                        position.X.SetChildValues(null, null);
                        position.Y.SetChildValues(null, null);

                        if (isTspanDisplayed)
                        {
                            ColorBlock colorBlock;

                            if (hasOwnFill || hasOwnStroke)
                            {
                                colorBlock = new ColorBlock();
                                colorBlocks.Add(colorBlock);
                                colorBlock.Characters         = charBlock;
                                colorBlock.AdjustFillGlobal   = !hasOwnFill;
                                colorBlock.AdjustStrokeGlobal = !hasOwnStroke;
                            }
                            else
                            {
                                colorBlock = textColorBlock;
                                textColorBlock.Characters.AddRange(charBlock);
                            }

                            for (int i = 0; i < charBlock.Count; i++)
                            {
                                var path = new GraphicPath();
                                colorBlock.Paths.Add(path);

                                brushParser.SetFillAndStroke(tspanElement, path, currentTransformationMatrix, textOpacity, textFillOpacity, textStrokeOpacity);
                            }
                        }

                        cssStyleCascade.Pop();
                    }
                    break;

                    case "textpath":
                    {
                        break;
                    }
                    }

                    break;
                }

                case XText textContentElement:
                {
                    var charBlock = Vectorize(positionBlocks,
                                              textContentElement.Value,
                                              textAnchor,
                                              position,
                                              beginOfLine,
                                              hasSuccessor,
                                              typeface,
                                              fontSize,
                                              rotation,
                                              currentTransformationMatrix);
                    textColorBlock.Characters.AddRange(charBlock);

                    for (int i = 0; i < charBlock.Count; i++)
                    {
                        var path = new GraphicPath();
                        textColorBlock.Paths.Add(path);

                        brushParser.SetFillAndStroke(textElement, path, currentTransformationMatrix);
                    }
                    break;
                }
                }

                beginOfLine = false;
                node        = nextNode;
            }

            AdjustPosition(positionBlocks);

            if (GetTextLength(textElement, out double textLength))
            {
                textLength = MatrixUtilities.TransformScale(textLength, currentTransformationMatrix);
                AdjustLength(positionBlocks, textLength, GetTextAdjust(textElement));
            }

            UpdateColorBlockPathGeometry(colorBlocks);
            AdjustGradients(colorBlocks);

            foreach (var block in colorBlocks)
            {
                graphicGroup.Children.AddRange(block.Paths);
            }

            if (clipping.IsClipPathSet())
            {
                clipping.SetClipPath(graphicGroup, currentTransformationMatrix);
            }

            return(graphicGroup);
        }
Exemplo n.º 2
0
 public OneDimensionPositions()
 {
     AbsoluteValue = new ParentChildPriorityList();
     RelativeValue = new ParentChildPriorityList();
     Current       = 0;
 }