Exemple #1
0
        public void MergeElementsWithSkippingRules()
        {
            List <TextElement> elements = new List <TextElement>
            {
                new TextElement(new PdfRectangle(15, 0, 25, 20), UtilsForTesting.HELVETICA_BOLD, 1f, "A", 1f, 6f),         // 0f, 15f, 10f, 20f,
                new TextElement(new PdfRectangle(17, 0, 27, 20), UtilsForTesting.HELVETICA_BOLD, 1f, "A", 1f, 6f),         // 0f, 17f, 10f, 20f,
                new TextElement(new PdfRectangle(25, 0, 35, 20), UtilsForTesting.HELVETICA_BOLD, 1f, "B", 1f, 6f),         // 0f, 25f, 10f, 20f,
                new TextElement(new PdfRectangle(25, 0.001, 35, 20.001), UtilsForTesting.HELVETICA_BOLD, 1f, " ", 1f, 6f), // 0.001f, 25f, 10f, 20f,
                new TextElement(new PdfRectangle(35, 0, 45, 20), UtilsForTesting.HELVETICA_BOLD, 1f, "C", 1f, 6f),         // 0f, 35f, 10f, 20f,
                new TextElement(new PdfRectangle(45, 0, 55, 20), UtilsForTesting.HELVETICA_BOLD, 10f, "D", 1f, 6f)         // 0f, 45f, 10f, 20f,
            };

            List <TextChunk> words = TextElement.MergeWords(elements);

            TextChunk textChunk = new TextChunk(new TextElement(new PdfRectangle(15, 0, 25, 20), UtilsForTesting.HELVETICA_BOLD, 1f, "A", 1f, 6f)); // 0f, 15f, 10f, 20f

            textChunk.Add(new TextElement(new PdfRectangle(25, 0, 35, 20), UtilsForTesting.HELVETICA_BOLD, 1f, "B", 1f, 6f));                       // 0f, 25f, 10f, 20f
            textChunk.Add(new TextElement(new PdfRectangle(35, 0, 45, 20), UtilsForTesting.HELVETICA_BOLD, 1f, "C", 1f, 6f));                       // 0f, 35f, 10f, 20f
            textChunk.Add(new TextElement(new PdfRectangle(45, 0, 55, 20), UtilsForTesting.HELVETICA_BOLD, 10f, "D", 1f, 6f));                      // 0f, 45f, 10f, 20f
            List <TextChunk> expectedWords = new List <TextChunk>
            {
                textChunk
            };

            Assert.Equal(expectedWords, words);
        }
Exemple #2
0
        public void MergeFourElementsIntoOneWord()
        {
            List <TextElement> elements = new List <TextElement>
            {
                new TextElement(new PdfRectangle(15, 0, 15 + 10, 0 + 20), UtilsForTesting.HELVETICA_BOLD, 1f, "A", 1f, 6f), // 0f, 15f, 10f, 20f,
                new TextElement(new PdfRectangle(25, 0, 25 + 10, 0 + 20), UtilsForTesting.HELVETICA_BOLD, 1f, "B", 1f, 6f), // 0f, 25f, 10f, 20f,
                new TextElement(new PdfRectangle(35, 0, 35 + 10, 0 + 20), UtilsForTesting.HELVETICA_BOLD, 1f, "C", 1f, 6f), // 0f, 35f, 10f, 20f,
                new TextElement(new PdfRectangle(45, 0, 45 + 10, 0 + 20), UtilsForTesting.HELVETICA_BOLD, 1f, "D", 1f, 6f)  // 0f, 45f, 10f, 20f,
            };

            List <TextChunk> words = TextElement.MergeWords(elements);

            List <TextChunk> expectedWords = new List <TextChunk>();
            TextChunk        textChunk     = new TextChunk(new TextElement(new PdfRectangle(15, 0, 15 + 10, 0 + 20), UtilsForTesting.HELVETICA_BOLD, 1f, "A", 1f, 6f)); // 0f, 15f, 10f, 20f,

            textChunk.Add(new TextElement(new PdfRectangle(25, 0, 25 + 10, 0 + 20), UtilsForTesting.HELVETICA_BOLD, 1f, "B", 1f, 6f));                                  // 0f, 25f, 10f, 20f,
            textChunk.Add(new TextElement(new PdfRectangle(35, 0, 35 + 10, 0 + 20), UtilsForTesting.HELVETICA_BOLD, 1f, "C", 1f, 6f));                                  // 0f, 35f, 10f, 20f,
            textChunk.Add(new TextElement(new PdfRectangle(45, 0, 45 + 10, 0 + 20), UtilsForTesting.HELVETICA_BOLD, 1f, "D", 1f, 6f));                                  // 0f, 45f, 10f, 20f,
            expectedWords.Add(textChunk);

            Assert.Equal(expectedWords, words);
        }
Exemple #3
0
        public void MergeTenElementsIntoTwoWords()
        {
            List <TextElement> elements = new List <TextElement>
            {
                new TextElement(new PdfRectangle(0, 0, 10, 20), UtilsForTesting.HELVETICA_BOLD, 1f, "H", 1f, 6f),   // 0f, 0f, 10f, 20f,
                new TextElement(new PdfRectangle(10, 0, 20, 20), UtilsForTesting.HELVETICA_BOLD, 1f, "O", 1f, 6f),  // 0f, 10f, 10f, 20f,
                new TextElement(new PdfRectangle(20, 0, 30, 20), UtilsForTesting.HELVETICA_BOLD, 1f, "L", 1f, 6f),  // 0f, 20f, 10f, 20f,
                new TextElement(new PdfRectangle(30, 0, 40, 20), UtilsForTesting.HELVETICA_BOLD, 1f, "A", 1f, 6f),  // 0f, 30f, 10f, 20f,

                new TextElement(new PdfRectangle(60, 0, 70, 20), UtilsForTesting.HELVETICA_BOLD, 1f, "M", 1f, 6f),  // 0f, 60f, 10f, 20f,
                new TextElement(new PdfRectangle(70, 0, 80, 20), UtilsForTesting.HELVETICA_BOLD, 1f, "U", 1f, 6f),  // 0f, 70f, 10f, 20f,
                new TextElement(new PdfRectangle(80, 0, 90, 20), UtilsForTesting.HELVETICA_BOLD, 1f, "N", 1f, 6f),  // 0f, 80f, 10f, 20f,
                new TextElement(new PdfRectangle(90, 0, 100, 20), UtilsForTesting.HELVETICA_BOLD, 1f, "D", 1f, 6f), // 0f, 90f, 10f, 20f,
                new TextElement(new PdfRectangle(100, 0, 110, 20), UtilsForTesting.HELVETICA_BOLD, 1f, "O", 1f, 6f) // 0f, 100f, 10f, 20f,
            };

            List <TextChunk> words = TextElement.MergeWords(elements);

            List <TextChunk> expectedWords = new List <TextChunk>();
            TextChunk        textChunk     = new TextChunk(new TextElement(new PdfRectangle(0, 0, 10, 20), UtilsForTesting.HELVETICA_BOLD, 1f, "H", 1f, 6f)); // 0f, 10f, 10f, 20f,

            textChunk.Add(new TextElement(new PdfRectangle(10, 0, 20, 20), UtilsForTesting.HELVETICA_BOLD, 1f, "O", 1f, 6f));                                 // 0f, 20f, 10f, 20f,
            textChunk.Add(new TextElement(new PdfRectangle(20, 0, 30, 20), UtilsForTesting.HELVETICA_BOLD, 1f, "L", 1f, 6f));                                 // 0f, 30f, 10f, 20f,
            textChunk.Add(new TextElement(new PdfRectangle(30, 0, 40, 20), UtilsForTesting.HELVETICA_BOLD, 1f, "A", 1f, 6f));                                 // 0f, 30f, 10.5f, 20f,
            textChunk.Add(new TextElement(new PdfRectangle(30, 0, 40.5, 20), UtilsForTesting.HELVETICA_BOLD, 1f, " ", 1f, 0));                                //Check why width=10.5?  0f, 30f, 10.5f, 20f
            expectedWords.Add(textChunk);
            TextChunk textChunk2 = new TextChunk(new TextElement(new PdfRectangle(60, 0, 70, 20), UtilsForTesting.HELVETICA_BOLD, 1f, "M", 1f, 6f));          // 0f, 60f, 10f, 20f,

            textChunk2.Add(new TextElement(new PdfRectangle(70, 0, 80, 20), UtilsForTesting.HELVETICA_BOLD, 1f, "U", 1f, 6f));                                // 0f, 70f, 10f, 20f,
            textChunk2.Add(new TextElement(new PdfRectangle(80, 0, 90, 20), UtilsForTesting.HELVETICA_BOLD, 1f, "N", 1f, 6f));                                // 0f, 80f, 10f, 20f,
            textChunk2.Add(new TextElement(new PdfRectangle(90, 0, 100, 20), UtilsForTesting.HELVETICA_BOLD, 1f, "D", 1f, 6f));                               // 0f, 90f, 10f, 20f,
            textChunk2.Add(new TextElement(new PdfRectangle(100, 0, 110, 20), UtilsForTesting.HELVETICA_BOLD, 1f, "O", 1f, 6f));                              // 0f, 100f, 10f, 20f,
            expectedWords.Add(textChunk2);

            Assert.Equal(2, words.Count);
            Assert.Equal(expectedWords, words);
        }
Exemple #4
0
        public void MergeTenElementsIntoTwoLines()
        {
            List <TextElement> elements = new List <TextElement>
            {
                new TextElement(new PdfRectangle(0, 0, 10, 20), UtilsForTesting.HELVETICA_BOLD, 1f, "H", 1f, 6f),   // 0f, 0f, 10f, 20f,
                new TextElement(new PdfRectangle(10, 0, 20, 20), UtilsForTesting.HELVETICA_BOLD, 1f, "O", 1f, 6f),  // 0f, 10f, 10f, 20f,
                new TextElement(new PdfRectangle(20, 0, 30, 20), UtilsForTesting.HELVETICA_BOLD, 1f, "L", 1f, 6f),  // 0f, 20f, 10f, 20f,
                new TextElement(new PdfRectangle(30, 0, 40, 20), UtilsForTesting.HELVETICA_BOLD, 1f, "A", 1f, 6f),  // 0f, 30f, 10f, 20f,

                new TextElement(new PdfRectangle(0, 20, 10, 40), UtilsForTesting.HELVETICA_BOLD, 1f, "M", 1f, 6f),  // 20f, 0f, 10f, 20f,
                new TextElement(new PdfRectangle(10, 20, 20, 40), UtilsForTesting.HELVETICA_BOLD, 1f, "U", 1f, 6f), // 20f, 10f, 10f, 20f,
                new TextElement(new PdfRectangle(20, 20, 30, 40), UtilsForTesting.HELVETICA_BOLD, 1f, "N", 1f, 6f), // 20f, 20f, 10f, 20f,
                new TextElement(new PdfRectangle(30, 20, 40, 40), UtilsForTesting.HELVETICA_BOLD, 1f, "D", 1f, 6f), // 20f, 30f, 10f, 20f,
                new TextElement(new PdfRectangle(40, 20, 50, 40), UtilsForTesting.HELVETICA_BOLD, 1f, "O", 1f, 6f)  // 20f, 40f, 10f, 20f,
            };

            List <TextChunk> words = TextElement.MergeWords(elements);

            List <TextChunk> expectedWords = new List <TextChunk>();
            TextChunk        textChunk     = new TextChunk(new TextElement(new PdfRectangle(0, 0, 10, 20), UtilsForTesting.HELVETICA_BOLD, 1f, "H", 1f, 6f)); // 0f, 0f, 10f, 20f,

            textChunk.Add(new TextElement(new PdfRectangle(10, 0, 20, 20), UtilsForTesting.HELVETICA_BOLD, 1f, "O", 1f, 6f));                                 // 0f, 10f, 10f, 20f,
            textChunk.Add(new TextElement(new PdfRectangle(20, 0, 30, 20), UtilsForTesting.HELVETICA_BOLD, 1f, "L", 1f, 6f));                                 // 0f, 20f, 10f, 20f,
            textChunk.Add(new TextElement(new PdfRectangle(30, 0, 40, 20), UtilsForTesting.HELVETICA_BOLD, 1f, "A", 1f, 6f));                                 // 0f, 30f, 10f, 20f,
            expectedWords.Add(textChunk);
            TextChunk textChunk2 = new TextChunk(new TextElement(new PdfRectangle(0, 20, 10, 40), UtilsForTesting.HELVETICA_BOLD, 1f, "M", 1f, 6f));          // 20f, 0f, 10f, 20f,

            textChunk2.Add(new TextElement(new PdfRectangle(10, 20, 20, 40), UtilsForTesting.HELVETICA_BOLD, 1f, "U", 1f, 6f));                               // 20f, 10f, 10f, 20f,
            textChunk2.Add(new TextElement(new PdfRectangle(20, 20, 30, 40), UtilsForTesting.HELVETICA_BOLD, 1f, "N", 1f, 6f));                               // 20f, 20f, 10f, 20f,
            textChunk2.Add(new TextElement(new PdfRectangle(30, 20, 40, 40), UtilsForTesting.HELVETICA_BOLD, 1f, "D", 1f, 6f));                               // 20f, 30f, 10f, 20f,
            textChunk2.Add(new TextElement(new PdfRectangle(40, 20, 50, 40), UtilsForTesting.HELVETICA_BOLD, 1f, "O", 1f, 6f));                               // 20f, 40f, 10f, 20f,
            expectedWords.Add(textChunk2);

            Assert.Equal(2, words.Count);
            Assert.Equal(expectedWords, words);
        }
Exemple #5
0
        private void Labels()
        {
            //int fontSize = (int)(xInterval + yInterval) / 2 /6;
            var          fontSize = 14;
            const double offSetX  = 15;
            string       labelText;

            if (fontSize == 0)
            {
                fontSize = 1;
            }

            var textChunk = new TextChunk {
                FontSize = fontSize
            };
            var yAxisLabelText = new TextBlock {
                Text = _yAxisLabel
            };
            var xAxisLabelText = new TextBlock {
                Text = _xAxisLabel
            };

            //Set position for axis labels.
            if (_origin.X + MeasureString(_yAxisLabel, fontSize).Width * 2 < _width)
            {
                yAxisLabelText.Margin = new Thickness(_origin.X + 5
                                                      , MeasureString(_yAxisLabel, fontSize).Height * 1.5
                                                      , 0, 0);
            }
            else
            {
                yAxisLabelText.Margin = new Thickness(_origin.X - MeasureString(_yAxisLabel, fontSize).Width * 2
                                                      , MeasureString(_yAxisLabel, fontSize).Height * 1.5
                                                      , 0, 0);
            }

            if (_origin.Y - MeasureString(_yAxisLabel, fontSize).Height * 2 > 0)
            {
                xAxisLabelText.Margin = new Thickness(_width - MeasureString(_xAxisLabel, fontSize).Width
                                                      , _origin.Y - MeasureString(_xAxisLabel, fontSize).Height * 2
                                                      , 0, 0);
            }
            else
            {
                xAxisLabelText.Margin = new Thickness(_width - MeasureString(_xAxisLabel, fontSize).Width
                                                      , _origin.Y + MeasureString(_xAxisLabel, fontSize).Height * 2
                                                      , 0, 0);
            }

            textChunk.Add(yAxisLabelText);
            textChunk.Add(xAxisLabelText);

            var labelincrementY     = GetLabelIncrementY(fontSize);
            var dynamicLabelOffSetY = _yStart % labelincrementY;


            for (var i = _yRange; i >= -labelincrementY; i -= labelincrementY)
            {
                labelText = (_yStart - dynamicLabelOffSetY + (_yRange - i)).ToString("0.######");
                var textBlock = new TextBlock {
                    Text = labelText
                };

                if (Convert.ToDouble(labelText) == 0)
                {
                    continue;
                }
                if ((dynamicLabelOffSetY + i) / _incrementY * _yInterval -
                    MeasureString(labelText, fontSize).Height >= 0 &&
                    (dynamicLabelOffSetY + i) / _incrementY * _yInterval +
                    MeasureString(labelText, fontSize).Height <= _height)
                {
                    if (_origin.X - offSetX - MeasureString(labelText, fontSize).Width <= 0)
                    {
                        textBlock.Margin = new Thickness(_origin.X + offSetX
                                                         , (dynamicLabelOffSetY + i) / _incrementY * _yInterval -
                                                         MeasureString(labelText, fontSize).Height * 0.5
                                                         , 0, 0);
                        textChunk.Add(textBlock);
                    }
                    else
                    {
                        textBlock.Margin = new Thickness(
                            _origin.X - offSetX - MeasureString(labelText, fontSize).Width
                            , (dynamicLabelOffSetY + i) / _incrementY * _yInterval -
                            MeasureString(labelText, fontSize).Height * 0.5
                            , 0, 0);
                        textChunk.Add(textBlock);
                    }
                }
                else if ((dynamicLabelOffSetY + i) / _incrementY * _yInterval -
                         MeasureString(labelText, fontSize).Height <= 0 &&
                         (dynamicLabelOffSetY + i) / _incrementY * _yInterval -
                         MeasureString(labelText, fontSize).Height >= -MeasureString(labelText, fontSize).Height)
                {
                    if (_origin.X - offSetX - MeasureString(labelText, fontSize).Width <= 0)
                    {
                        textBlock.Margin = new Thickness(_origin.X + offSetX
                                                         , (_startOffSetY + i) / _incrementY * _yInterval +
                                                         MeasureString(labelText, fontSize).Height * 0.5
                                                         - Math.Abs((dynamicLabelOffSetY + i) / _incrementY * _yInterval)
                                                         , 0, 0);
                        textChunk.Add(textBlock);
                    }
                    else
                    {
                        textBlock.Margin = new Thickness(
                            _origin.X - offSetX - MeasureString(labelText, fontSize).Width
                            , (dynamicLabelOffSetY + i) / _incrementY * _yInterval -
                            MeasureString(labelText, fontSize).Height * 0.5
                            + (-((dynamicLabelOffSetY + i) / _incrementY) * _yInterval +
                               MeasureString(labelText, fontSize).Height)
                            , 0, 0);
                        textChunk.Add(textBlock);
                    }
                }
                else if ((dynamicLabelOffSetY + i) / _incrementY * _yInterval +
                         MeasureString(labelText, fontSize).Height >= _height &&
                         (dynamicLabelOffSetY + i) / _incrementY * _yInterval +
                         MeasureString(labelText, fontSize).Height <=
                         _height + MeasureString(labelText, fontSize).Height)
                {
                    if (_origin.X - offSetX - MeasureString(labelText, fontSize).Width <= 0)
                    {
                        textBlock.Margin = new Thickness(_origin.X + offSetX
                                                         , (dynamicLabelOffSetY + i) / _incrementY * _yInterval -
                                                         MeasureString(labelText, fontSize).Height * 0.5 -
                                                         ((dynamicLabelOffSetY + i) / _incrementY * _yInterval +
                                                          MeasureString(labelText, fontSize).Height
                                                          - _height)
                                                         , 0, 0);
                        textChunk.Add(textBlock);
                    }
                    else
                    {
                        textBlock.Margin = new Thickness(
                            _origin.X - offSetX - MeasureString(labelText, fontSize).Width
                            , (dynamicLabelOffSetY + i) / _incrementY * _yInterval -
                            MeasureString(labelText, fontSize).Height * 0.5 -
                            ((dynamicLabelOffSetY + i) / _incrementY * _yInterval +
                             MeasureString(labelText, fontSize).Height
                             - _height)
                            , 0, 0);
                        textChunk.Add(textBlock);
                    }
                }
            }
            var labelincrementX     = GetLabelIncrementX(fontSize);
            var dynamicLabelOffSetX = _xStart % labelincrementX;


            for (double i = 0; i <= _xRange + labelincrementX; i += labelincrementX)
            {
                labelText = (_xStart - dynamicLabelOffSetX + i).ToString("0.######");

                if (Convert.ToDouble(labelText) == 0)
                {
                    continue;
                }
                if ((-dynamicLabelOffSetX + i) / _incrementX * _xInterval -
                    0.5f * MeasureString(labelText, fontSize).Width >= 0 &&
                    (-dynamicLabelOffSetX + i) / _incrementX * _xInterval +
                    0.5f * MeasureString(labelText, fontSize).Width <= _width)
                {
                    if (_origin.Y + MeasureString(labelText, fontSize).Height * 1.5f >= _height)
                    {
                        var textBlock = new TextBlock
                        {
                            Text   = labelText,
                            Margin = new Thickness(
                                (-dynamicLabelOffSetX + i) / _incrementX * _xInterval -
                                0.5f * MeasureString(labelText, fontSize).Width
                                , _origin.Y - MeasureString(labelText, fontSize).Height * 1.5f
                                , 0, 0)
                        };
                        textChunk.Add(textBlock);
                    }
                    else
                    {
                        var textBlock = new TextBlock
                        {
                            Text   = labelText,
                            Margin = new Thickness((-dynamicLabelOffSetX + i) / _incrementX * _xInterval
                                                   - 0.5f * MeasureString(labelText, fontSize).Width
                                                   , _origin.Y + MeasureString(labelText, fontSize).Height * 0.5f
                                                   , 0, 0)
                        };
                        textChunk.Add(textBlock);
                    }
                }
                else if ((-dynamicLabelOffSetX + i) / _incrementX * _xInterval -
                         0.5f * MeasureString(labelText, fontSize).Width <= 0 &&
                         (-dynamicLabelOffSetX + i) / _incrementX * _xInterval -
                         0.5f * MeasureString(labelText, fontSize).Width
                         >= -0.5f * MeasureString(labelText, fontSize).Width)
                {
                    if (_origin.Y + MeasureString(labelText, fontSize).Height * 1.5f >= _height)
                    {
                        var textBlock = new TextBlock
                        {
                            Text   = labelText,
                            Margin = new Thickness((-dynamicLabelOffSetX + i) / _incrementX * _xInterval
                                                   - Math.Abs(
                                                       (-dynamicLabelOffSetX + i) / _incrementX * _xInterval +
                                                       0.5f * MeasureString(labelText, fontSize).Width)
                                                   + 0.5f * MeasureString(labelText, fontSize).Width
                                                   , _origin.Y - MeasureString(labelText, fontSize).Height * 1.5f
                                                   , 0, 0)
                        };


                        textChunk.Add(textBlock);
                    }
                    else
                    {
                        var textBlock = new TextBlock
                        {
                            Text   = labelText,
                            Margin = new Thickness((-dynamicLabelOffSetX + i) / _incrementX * _xInterval
                                                   - Math.Abs(
                                                       (-dynamicLabelOffSetX + i) / _incrementX * _xInterval +
                                                       0.5f * MeasureString(labelText, fontSize).Width)
                                                   + 0.5f * MeasureString(labelText, fontSize).Width
                                                   , _origin.Y + MeasureString(labelText, fontSize).Height * 0.5f
                                                   , 0, 0)
                        };

                        textChunk.Add(textBlock);
                    }
                }


                else if ((-dynamicLabelOffSetX + i) / _incrementX * _xInterval +
                         0.5f * MeasureString(labelText, fontSize).Width >= _width &&
                         (-dynamicLabelOffSetX + i) / _incrementX * _xInterval +
                         0.5f * MeasureString(labelText, fontSize).Width <=
                         _width + 0.5f * MeasureString(labelText, fontSize).Width)
                {
                    if (_origin.Y + MeasureString(labelText, fontSize).Height * 1.5f >= _height)
                    {
                        var textBlock = new TextBlock
                        {
                            Text   = labelText,
                            Margin = new Thickness((-dynamicLabelOffSetX + i) / _incrementX * _xInterval
                                                   - Math.Abs(
                                                       (-dynamicLabelOffSetX + i) / _incrementX * _xInterval +
                                                       0.5f * MeasureString(labelText, fontSize).Width -
                                                       _width)
                                                   - 0.5f * MeasureString(labelText, fontSize).Width
                                                   , _origin.Y - MeasureString(labelText, fontSize).Height * 1.5f
                                                   , 0, 0)
                        };
                        textChunk.Add(textBlock);
                    }
                    else
                    {
                        var textBlock = new TextBlock
                        {
                            Text   = labelText,
                            Margin = new Thickness((-dynamicLabelOffSetX + i) / _incrementX * _xInterval
                                                   - Math.Abs(
                                                       (-dynamicLabelOffSetX + i) / _incrementX * _xInterval +
                                                       0.5f * MeasureString(labelText, fontSize).Width -
                                                       _width)
                                                   - 0.5f * MeasureString(labelText, fontSize).Width
                                                   , _origin.Y + MeasureString(labelText, fontSize).Height * 0.5f
                                                   , 0, 0)
                        };
                        textChunk.Add(textBlock);
                    }
                }
            }
            _chunks.Add(textChunk);
        }