Esempio n. 1
0
        internal static void FitOneLine(IFlxGraphics Canvas, TFontCache FontCache, real Zoom100, TRichString Text, Font AFont, real w, int idx, ref int fit, ref real StrWidth)
        {
            if (StrWidth > w)
            {
                real md = 0;

                fit = Math.Max((int)Math.Round(fit * w / StrWidth), 1);
                CharUtils.SameOrMore(Text.Value, idx, ref fit);
                StrWidth = CalcTextExtent(Canvas, FontCache, Zoom100, AFont, Text.Substring(idx, fit), out md).Width;

                if (StrWidth > w)
                {
                    while (fit > 1 && StrWidth > w)
                    {
                        fit--;
                        CharUtils.SameOrLess(Text.Value, idx, ref fit);
                        StrWidth = CalcTextExtent(Canvas, FontCache, Zoom100, AFont, Text.Substring(idx, fit), out md).Width;
                    }
                }
                else
                {
                    while (StrWidth < w) //Will not overflow, as wc>w for fit+idx=MaxLength.
                    {
                        int fit1 = fit + 1;
                        CharUtils.SameOrMore(Text.Value, idx, ref fit1);
                        StrWidth = CalcTextExtent(Canvas, FontCache, Zoom100, AFont, Text.Substring(idx, fit1), out md).Width;
                        if (StrWidth < w)
                        {
                            fit = fit1;
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        private static void WriteJustText(ExcelFile Workbook, IFlxGraphics Canvas, TFontCache FontCache, real Zoom100, bool ReverseRightToLeftStrings, Font AFont, Color AFontColor, real y, real SubOfs, RectangleF CellRect, real Clp, TRichString OutText, real XExtent, real MaxDescent, bool Distributed, TAdaptativeFormats AdaptativeFormats)
        {
            List <TRichString> Words = new List <TRichString>();

            OutText = GetVisualString(OutText, ReverseRightToLeftStrings);
            string s = OutText.Value;
            int    p = 0; int p1 = 0;

            while ((p1 = s.IndexOf(' ', p)) >= 0)
            {
                Words.Add(OutText.Substring(p, p1 - p + 1));
                p = p1 + 1;
            }

            if (p < s.Length)
            {
                Words.Add(OutText.Substring(p));
            }

            real md;
            real wc = XExtent; // CalcTextExtent(AFont, OutText, out md).Width;

            real Spaces = 0;

            if (Words.Count - 1 > 0)
            {
                Spaces = (CellRect.Width - 2 - 2 * Clp - wc) / (Words.Count - 1);
            }

            real x = CellRect.Left + 1 + Clp;

            if (Words.Count == 1 && Distributed)  //Center when it is one word
            {
                x = (CellRect.Left + CellRect.Right - 2 - 2 * Clp - wc) / 2;
            }
            for (int i = 0; i < Words.Count; i++)
            {
                TRichString wo = Words[i];
                WriteText(Workbook, Canvas, FontCache, Zoom100, AFont, AFontColor, x, y, SubOfs, wo, 0, MaxDescent, AdaptativeFormats);
                x += RenderMetrics.CalcTextExtent(Canvas, FontCache, Zoom100, AFont, wo, out md).Width;
                if (Spaces > 0)
                {
                    x += Spaces;
                }
            }
        }
Esempio n. 3
0
        internal static void SplitText(IFlxGraphics Canvas, TFontCache FontCache, real Zoom100,
                                       TRichString Text, Font AFont, real w, TXRichStringList TextLines, out SizeF TextExtent, bool Vertical, TFloatList MaxDescent, TAdaptativeFormats AdaptFormat)
        {
            TextLines.Clear();
            MaxDescent.Clear();

            int idx = 0;
            int fit = 0;


            TextExtent = new SizeF(0, 0);
            if (w <= 0 || Text.Value.Length <= 0)
            {
                return;
            }

            while (idx < Text.Value.Length)
            {
                int Enter     = Text.Value.IndexOf((char)0x0A, idx);
                int MaxLength = Text.Value.Length - idx;
                if (Enter >= 0)
                {
                    MaxLength = Enter - idx;
                }

                if (Vertical)
                {
                    fit = 1;
                    CharUtils.SameOrMore(Text.Value, idx, ref fit);
                }
                else
                {
                    //Not a really efficient way, but...
                    //First Guess. whole string.
                    fit = MaxLength;

                    real md;
                    real wc = CalcTextExtent(Canvas, FontCache, Zoom100, AFont, Text.Substring(idx, fit), out md).Width;
                    FitOneLine(Canvas, FontCache, Zoom100, Text, AFont, w, idx, ref fit, ref wc);

                    if (fit <= 0)
                    {
                        fit = 1;                               //avoid infinite loop
                    }
                    CharUtils.SameOrMore(Text.Value, idx, ref fit);


                    if (Text.Value.IndexOf(' ', idx, fit) >= 0)
                    {
                        int minfit = 1;
                        CharUtils.SameOrMore(Text.Value, idx, ref minfit);
                        while (fit > minfit && fit < MaxLength && Text.Value[idx + fit] != ' ')
                        {
                            fit--;
                        }
                    }
                    while (fit < MaxLength && Text.Value[idx + fit] == ' ')
                    {
                        fit++;
                    }
                    //No need to adjust fit for surrogates. it will always be at the start of one.
                }

                //int Enter=Text.Value.IndexOf((char)0x0A, idx, fit);
                //if (Enter>0) fit=Enter-idx;
                int TextLen = Math.Min(MaxLength, fit);
                TextLines.Add(new TXRichString(Text.Substring(idx, TextLen), true, 0, 0, TAdaptativeFormats.CopyTo(AdaptFormat, idx, TextLen)));

                if (fit + idx < Text.Value.Length && Text.Value[idx + fit] == (char)0x0A)
                {
                    TextLines[TextLines.Count - 1].Split = false;
                    if (idx + fit < Text.Value.Length - 1)                  //An Enter at the end behaves different, it means we have a new empty line.
                    {
                        idx++;
                    }
                }
                if (fit + idx >= Text.Value.Length)
                {
                    TextLines[TextLines.Count - 1].Split = false;
                }

                idx += fit;

                //Recalculate dx
                real        mdx   = 0;
                TRichString sx    = TextLines[TextLines.Count - 1].s;
                SizeF       bSize = CalcTextExtent(Canvas, FontCache, Zoom100, AFont, sx, out mdx);
                if (bSize.Height == 0)
                {
                    real  mdx3;
                    SizeF bSize3 = CalcTextExtent(Canvas, FontCache, Zoom100, AFont, new TRichString("M"), out mdx3);
                    bSize.Height = bSize3.Height;
                }

                SizeF bSize2 = bSize;
                if (sx != null && sx.Length > 0 && sx.ToString()[sx.Length - 1] == ' ')             //This is to right align line with spaces at the end.
                {
                    real mdx2;
                    bSize2 = CalcTextExtent(Canvas, FontCache, Zoom100, AFont, sx.RightTrim(), out mdx2);
                }
                TextLines[TextLines.Count - 1].XExtent = bSize2.Width;
                TextLines[TextLines.Count - 1].YExtent = bSize.Height;                 // not bSize2.Height; This might be even 0.

                MaxDescent.Add(mdx);
                if (TextExtent.Width < bSize.Width)
                {
                    TextExtent.Width = bSize.Width;
                }
                TextExtent.Height += bSize.Height;
            }             //while
        }