Пример #1
0
        private void reflowParagraph(ParagraphNode node, InkAnalyzer inkAnalyzer, InkCanvas inkCanvas)
        {
            ContextNodeCollection lines = node.SubNodes;
            Rect bounds = node.Strokes.GetBounds();
            List <InkWordNode> resultWords = new List <InkWordNode>();
            double             lineHeight  = 0;
            double             spacing     = 30;

            //Collect all strokes
            foreach (ContextNode line in lines)
            {
                ContextNodeCollection words = line.SubNodes;
                foreach (ContextNode word in words)
                {
                    lineHeight += word.Strokes.GetBounds().Height;
                    InkWordNode wordNode = word as InkWordNode;
                    resultWords.Add(wordNode);
                }
            }
            lineHeight /= resultWords.Count;

            List <List <InkWordNode> > resultLines = new List <List <InkWordNode> >();
            List <double> lineMaxBaseline          = new List <double>();
            //Reflow strokes
            double x    = 0;
            double maxX = inkCanvas.ActualWidth - bounds.X;

            resultLines.Add(new List <InkWordNode>());
            lineMaxBaseline.Add(0);
            foreach (InkWordNode word in resultWords)
            {
                //Does word fit?
                Rect wordBound = word.Strokes.GetBounds();
                if (x + wordBound.Width + spacing > maxX && multiline)
                {
                    //Not fitting! Newline
                    x = 0;
                    resultLines.Add(new List <InkWordNode>());
                    lineMaxBaseline.Add(0);
                }
                x += spacing + wordBound.Width;
                PointCollection baseline = word.GetBaseline();
                if (baseline != null && baseline.Count > 0)
                {
                    double baselineFromTop = baseline[0].Y - wordBound.Y;
                    resultLines[resultLines.Count - 1].Add(word);
                    if (baselineFromTop > lineMaxBaseline[resultLines.Count - 1])
                    {
                        lineMaxBaseline[resultLines.Count - 1] = baselineFromTop;
                    }
                }
            }

            double y          = 0;
            int    lineNumber = 0;

            foreach (List <InkWordNode> line in resultLines)
            {
                double lineBaseline = lineMaxBaseline[lineNumber];
                x = 0;
                foreach (InkWordNode word in line)
                {
                    Rect            wordBound       = word.Strokes.GetBounds();
                    PointCollection baseline        = word.GetBaseline();
                    double          baselineFromTop = baseline[0].Y - wordBound.Y;
                    double          destX           = (x + bounds.X);
                    double          dx = destX - (wordBound.X);
                    //Match mid
                    double dy = (y + lineBaseline + bounds.Y) - (wordBound.Y + baselineFromTop);
                    InkUtils.transposeStrokes(inkAnalyzer, word.Strokes, dx, dy);
                    x += spacing + wordBound.Width;
                }
                y += lineHeight + spacing;
                lineNumber++;
            }
        }
Пример #2
0
        private void findAndDeleteStrikethrough(InkAnalyzer inkAnalyzer, InkCanvas canvas,
                                                List <Stroke> horizontalLines, ContextNodeCollection contextNodeCollection)
        {
            List <ContextNode> deletedNodes           = new List <ContextNode>();
            List <Stroke>      removedHorizontalLines = new List <Stroke>();

            //Find things to apply gestures to
            foreach (ContextNode node in contextNodeCollection)
            {
                if (node.Strokes.Count == 0)
                {
                    continue;
                }
                Rect strikethroughBounds = node.Strokes.GetBounds();
                strikethroughBounds.Height *= 0.75d;
                if (node is InkWordNode)
                {
                    PointCollection bl = (node as InkWordNode).GetBaseline();
                    if (bl != null &&
                        bl.Count() > 0)
                    {
                        double baseline = bl[0].Y;
                        strikethroughBounds.Height = baseline - strikethroughBounds.Y;
                    }
                }

                for (int j = 0; j < horizontalLines.Count; j++)
                {
                    if (node.Strokes[0] == horizontalLines[j])
                    {
                        break;
                    }
                    Stroke horizontalLine          = horizontalLines[j];
                    Rect   horizontalLineBounds    = horizontalLine.GetBounds();
                    double sideBuffer              = (1 - Constants.LINE_WORD_OVERLAPSE_RATIO) / 2;
                    double strikethroughBoundLeft  = strikethroughBounds.X + strikethroughBounds.Width * sideBuffer;
                    double strikethroughBoundRight = strikethroughBounds.X + strikethroughBounds.Width * (1 - sideBuffer);
                    if (strikethroughBounds.IntersectsWith(horizontalLineBounds) &&
                        strikethroughBoundLeft > horizontalLineBounds.X &&
                        strikethroughBoundRight < horizontalLineBounds.X + horizontalLineBounds.Width)
                    {
                        //Delete strikethrough
                        deletedNodes.Add(node);
                        removedHorizontalLines.Add(horizontalLine);
                    }
                }
            }

            foreach (Stroke stroke in removedHorizontalLines)
            {
                horizontalLines.Remove(stroke);
                canvas.Strokes.Remove(stroke);
                inkAnalyzer.RemoveStroke(stroke);
            }

            //Final step to apply the gestures, commit changes
            for (int i = deletedNodes.Count - 1; i >= 0; i--)
            {
                ContextNode node = deletedNodes[i];
                try
                {
                    Rect        bounds   = node.Strokes.GetBounds();
                    double      nodeX    = bounds.X;
                    ContextNode parent   = node.ParentNode;
                    double      closestX = double.MaxValue;
                    foreach (ContextNode sibling in parent.SubNodes)
                    {
                        double siblingX = sibling.Strokes.GetBounds().X;
                        if (siblingX > nodeX && siblingX < closestX)
                        {
                            closestX = siblingX;
                        }
                    }
                    double dx = nodeX - closestX;
                    foreach (ContextNode sibling in parent.SubNodes)
                    {
                        //Nodes right side of current
                        if (sibling.Strokes.GetBounds().X > nodeX)
                        {
                            InkUtils.transposeStrokes(inkAnalyzer, sibling.Strokes, dx, 0d);
                        }
                    }
                    canvas.Strokes.Remove(node.Strokes);
                    inkAnalyzer.RemoveStrokes(node.Strokes);
                }
                catch (Exception e)
                {
                    //Ignore already deleted error
                }
            }
        }