Exemple #1
0
        // compute the best dimensions fitting within the given size
        private Specific_TextLayout ComputeDimensions(Size availableSize, bool debug)
        {
            DateTime start = DateTime.Now;

            numComputations++;

            FormattedParagraph formattedText = this.formatText(availableSize.Width, debug, this.FontSize);
            Size desiredSize = formattedText.Size;

            if (desiredSize.Width < 0 || desiredSize.Height < 0)
            {
                ErrorReporter.ReportParadox("Illegal size " + desiredSize + " returned by textFormatter.FormatText");
            }
            bool   cropped = false;
            double width, height;

            // assign the width, height, and score
            if (desiredSize.Width <= availableSize.Width && desiredSize.Height <= availableSize.Height)
            {
                // no cropping is necessary
                width  = desiredSize.Width;
                height = desiredSize.Height;
            }
            else
            {
                // cropping
                cropped = true;
                if (this.ScoreIfCropped)
                {
                    width  = Math.Min(desiredSize.Width, availableSize.Width);
                    height = Math.Min(desiredSize.Height, availableSize.Height);
                }
                else
                {
                    width = height = 0;
                }
            }
            Specific_TextLayout specificLayout = new Specific_TextLayout(this.TextItem_Configurer, width, height, this.FontSize,
                                                                         this.ComputeScore(desiredSize, availableSize, this.TextToFit, formattedText.Text),
                                                                         formattedText.Text, desiredSize);

            specificLayout.Cropped = cropped;

            // diagnostics
            if (this.LoggingEnabled)
            {
                DateTime end      = DateTime.Now;
                TimeSpan duration = end.Subtract(start);
                System.Diagnostics.Debug.WriteLine("spent " + duration + " to measure '" + this.Summarize(this.TextToFit) + "' in " + availableSize +
                                                   "; desired " + desiredSize + " (formatted = " + this.Summarize(formattedText.Text) + "); requesting " + specificLayout.Size);
            }

            return(specificLayout);
        }
Exemple #2
0
        // Tells the required size for a block of text that's supposed to fit it into a column of the given width
        // The returned size might have larger width than desiredWidth if needed for the text to fit
        public FormattedParagraph FormatText(String text, double desiredWidth, bool allowSplittingWords, bool debug, double fontSize)
        {
            FormattedParagraph result;

            if (text == null || text == "")
            {
                result = new FormattedParagraph(new Size(), text);
                if (debug)
                {
                    System.Diagnostics.Debug.WriteLine("Formatted empty text '" + text + "' into size " + result);
                }
                return(result);
            }
            string[]      blocks = text.Split('\n');
            double        maxWidth = 0, totalHeight = 0;
            List <string> formattedStrings = new List <string>();

            for (int i = 0; i < blocks.Length; i++)
            {
                string block       = blocks[i];
                string formatBlock = block;
                if (formatBlock == "")
                {
                    formatBlock = "M";
                }

                FormattedParagraph formattedBlock = this.FormatParagraph(formatBlock, desiredWidth, allowSplittingWords, fontSize);
                if (block == "")
                {
                    formattedBlock.Text = block;
                }
                formattedStrings.Add(formattedBlock.Text);
                Size blockSize = formattedBlock.Size;
                maxWidth     = Math.Max(maxWidth, blockSize.Width);
                totalHeight += blockSize.Height;
            }
            string formattedText = String.Join("\n", formattedStrings);

            result = new FormattedParagraph(new Size(maxWidth, totalHeight), formattedText);
            if (debug)
            {
                System.Diagnostics.Debug.WriteLine("Formatted text '" + text + "'" + " using desiredWith = " + desiredWidth +
                                                   " and allowSplittingWords = " + allowSplittingWords + " into size " + result);
            }
            return(result);
        }