Example #1
0
        internal static void WriteText(ExcelFile Workbook, IFlxGraphics Canvas, TFontCache FontCache, real Zoom100, Font AFont, Color AFontColor, real x, real y, real SubOfs, TRichString OutText, real Alpha, real MaxDescent, TAdaptativeFormats AdaptativeFormats)
        {
            if (OutText.Length == 0)
            {
                return;
            }

            if (Alpha != 0)
            {
                Canvas.SaveTransform();
            }
            try
            {
                if (Alpha != 0)
                {
                    Canvas.Rotate(x, y, Alpha);
                }

                using (Brush TextBrush = new SolidBrush(AFontColor))
                {
                    if (OutText.RTFRunCount == 0)
                    {
                        if (AdaptativeFormats == null || AdaptativeFormats.Separators == null || AdaptativeFormats.Separators.Length == 0) //formats are only applied to non rich text cells.
                        {
                            Canvas.DrawString(OutText.Value, AFont, TextBrush, x, y + SubOfs);
                        }
                        else
                        {
                            DrawAdaptativeString(Canvas, AdaptativeFormats, OutText.Value, AFont, TextBrush, x, y + SubOfs);
                        }
                    }
                    else
                    {
                        SizeF Result;

                        string s1 = OutText.Value.Substring(0, OutText.RTFRun(0).FirstChar);
                        if (s1.Length > 0)
                        {
                            Result = Canvas.MeasureString(s1, AFont, new TPointF(x, y));
                            Canvas.DrawString(s1, AFont, TextBrush, x, y + SubOfs - (MaxDescent - Canvas.FontDescent(AFont)));
                            x += Result.Width;
                        }

                        for (int i = 0; i < OutText.RTFRunCount - 1; i++)
                        {
                            TFlxFont       Fx     = OutText.GetFont(OutText.RTFRun(i).FontIndex);
                            TSubscriptData Sub    = new TSubscriptData(Fx.Style);
                            Font           MyFont = FontCache.GetFont(Fx, Zoom100 * Sub.Factor);
                            {
                                using (Brush MyBrush = new SolidBrush(GetColor(Workbook, Fx.Color)))
                                {
                                    int Start = OutText.RTFRun(i).FirstChar;
                                    if (Start >= OutText.Length)
                                    {
                                        Start = OutText.Length;
                                    }
                                    int Len = OutText.RTFRun(i + 1).FirstChar;
                                    if (Len >= OutText.Length)
                                    {
                                        Len = OutText.Length;
                                    }
                                    Len -= Start;

                                    string s2 = OutText.Value.Substring(Start, Len);
                                    Result = Canvas.MeasureString(s2, MyFont, new TPointF(x, y));
                                    Canvas.DrawString(s2, MyFont, MyBrush, x, y + Sub.Offset(Canvas, MyFont) - (MaxDescent - Canvas.FontDescent(MyFont)));
                                    x += Result.Width;
                                }
                            }
                        }
                        TFlxFont       Fy      = OutText.GetFont(OutText.RTFRun(OutText.RTFRunCount - 1).FontIndex);
                        TSubscriptData Suby    = new TSubscriptData(Fy.Style);
                        Font           MyFont2 = FontCache.GetFont(Fy, Zoom100 * Suby.Factor);
                        {
                            using (Brush MyBrush = new SolidBrush(GetColor(Workbook, Fy.Color)))
                            {
                                int Start = OutText.RTFRun(OutText.RTFRunCount - 1).FirstChar;
                                if (Start >= OutText.Length)
                                {
                                    Start = OutText.Length;
                                }

                                string s3 = OutText.Value.Substring(Start);
                                Result = Canvas.MeasureString(s3, MyFont2, new TPointF(x, y));
                                Canvas.DrawString(s3, MyFont2, MyBrush, x, y + Suby.Offset(Canvas, MyFont2) - (MaxDescent - Canvas.FontDescent(MyFont2)));
                            }
                        }
                    }
                }
            }
            finally
            {
                if (Alpha != 0)
                {
                    Canvas.ResetTransform();
                }
            }
        }
Example #2
0
        internal static void DrawRichText(ExcelFile Workbook, IFlxGraphics Canvas, TFontCache FontCache, real Zoom100, bool ReverseRightToLeftStrings,
                                          ref RectangleF CellRect, ref RectangleF PaintClipRect, ref RectangleF TextRect, ref RectangleF ContainingRect,
                                          real Clp, THFlxAlignment HJustify, TVFlxAlignment VJustify, real Alpha,
                                          Color DrawFontColor, TSubscriptData SubData, TRichString OutText, SizeF TextExtent,
                                          TXRichStringList TextLines,
                                          Font AFont, TFloatList MaxDescent, real[] X, real[] Y)
        {
            RectangleF FinalRect = CellRect;

            if (ContainingRect.Right > PaintClipRect.Left &&
                ContainingRect.Left < PaintClipRect.Right &&
                Intersect(TextRect, PaintClipRect, out FinalRect))
            {
                real dy             = 0;
                int  TextLinesCount = TextLines.Count;

                if (VJustify == TVFlxAlignment.justify || VJustify == TVFlxAlignment.distributed)
                {
                    if (TextLinesCount > 1)
                    {
                        dy = (CellRect.Height - TextExtent.Height - 2 * Clp) / (TextLinesCount - 1);
                    }
                }

                float eps = 0.0001F; //Tolerance for the comparison, these are floats, not doubles, and we get rounding errors soon.
                if (TextRect.Left - eps <= ContainingRect.Left && TextRect.Right + eps >= ContainingRect.Right && TextRect.Top - eps <= ContainingRect.Top && TextRect.Bottom + eps >= ContainingRect.Bottom)
                {
                    Canvas.SetClipReplace(PaintClipRect);                      //This will improve pdf export a lot, since we can now join all the BT/ET tags inside one, and then we can also join fonts inside the ET/BT tags.
                }
                else
                {
                    if (Alpha == 0 || Alpha == 90)
                    {
                        Canvas.SetClipReplace(FinalRect);
                    }
                    else
                    {
                        Canvas.SetClipReplace(RectangleF.FromLTRB(PaintClipRect.Left, FinalRect.Top, PaintClipRect.Right, FinalRect.Bottom));                         //rotated text can move to other used cells horizontally.
                    }
                }

                real AcumDy = 0;
                for (int i = 0; i < TextLinesCount; i++)
                {
                    TXRichString TextLine = TextLines[i];
                    if ((Alpha == 0) &&
                        (HJustify == THFlxAlignment.justify && TextLine.Split) ||
                        (HJustify == THFlxAlignment.distributed)
                        )
                    {
                        Canvas.SetClipReplace(FinalRect);
                        WriteJustText(Workbook, Canvas, FontCache, Zoom100, ReverseRightToLeftStrings,
                                      AFont, DrawFontColor, Y[i] + AcumDy, SubData.Offset(Canvas, AFont), CellRect, Clp, TextLine.s,
                                      TextLine.XExtent, MaxDescent[i], HJustify == THFlxAlignment.distributed, TextLine.AdaptFormat);
                    }
                    else
                    {
                        WriteText(Workbook, Canvas, FontCache, Zoom100, AFont, DrawFontColor, X[i],
                                  Y[i] + AcumDy, SubData.Offset(Canvas, AFont), GetVisualString(TextLine.s, ReverseRightToLeftStrings),
                                  Alpha, MaxDescent[i], TextLine.AdaptFormat);
                    }
                    AcumDy += dy;
                }
            }
        }
Example #3
0
        internal static SizeF CalcTextExtent(IFlxGraphics Canvas, TFontCache FontCache, real Zoom100,
                                             Font AFont, TRichString Text, out real MaxDescent)
        {
            MaxDescent = Canvas.FontDescent(AFont);
            if (Text.RTFRunCount == 0)
            {
                return(Canvas.MeasureStringEmptyHasHeight(Text.Value, AFont));
            }

            real  x = 0;
            real  y = 0;
            SizeF Result;

            Result = Canvas.MeasureString(Text.Value.Substring(0, Text.RTFRun(0).FirstChar), AFont, new TPointF(0, 0));
            x      = Result.Width; y = Result.Height;


            for (int i = 0; i < Text.RTFRunCount - 1; i++)
            {
                TFlxFont       Fx     = Text.GetFont(Text.RTFRun(i).FontIndex);
                TSubscriptData Sub    = new TSubscriptData(Fx.Style);
                Font           MyFont = FontCache.GetFont(Fx, Zoom100 * Sub.Factor);
                {
                    int Start = Text.RTFRun(i).FirstChar;
                    if (Start >= Text.Length)
                    {
                        Start = Text.Length;
                    }
                    int Len = Text.RTFRun(i + 1).FirstChar;
                    if (Len >= Text.Length)
                    {
                        Len = Text.Length;
                    }
                    Len -= Start;
                    if (Len < 0)
                    {
                        continue;       //wrong file, (i+1)FirstChar < (i).FirstChar
                    }
                    Result     = Canvas.MeasureString(Text.Value.Substring(Start, Len), MyFont, new TPointF(0, 0));
                    x         += Result.Width; y = Math.Max(y + Sub.Offset(Canvas, MyFont), Result.Height);
                    MaxDescent = Math.Max(MaxDescent, Canvas.FontDescent(MyFont) + Sub.Offset(Canvas, MyFont));
                }
            }


            TFlxFont       Fy      = Text.GetFont(Text.RTFRun(Text.RTFRunCount - 1).FontIndex);
            TSubscriptData Suby    = new TSubscriptData(Fy.Style);
            Font           MyFont2 = FontCache.GetFont(Fy, Zoom100 * Suby.Factor);

            {
                int Start = Text.RTFRun(Text.RTFRunCount - 1).FirstChar;
                if (Start >= Text.Length)
                {
                    Start = Text.Length;
                }
                Result     = Canvas.MeasureStringEmptyHasHeight(Text.Value.Substring(Start), MyFont2);
                x         += Result.Width; y = Math.Max(y + Suby.Offset(Canvas, MyFont2), Result.Height);
                MaxDescent = Math.Max(MaxDescent, Canvas.FontDescent(MyFont2) + Suby.Offset(Canvas, MyFont2));
            }

            return(new SizeF(x, y));
        }