private static SKPaint ApplyWrapperTo(ref DrawingContextImpl.PaintWrapper wrapper,
                                       ref IDisposable curr, SKPaint paint)
 {
     curr?.Dispose();
     curr = wrapper.ApplyTo(paint);
     return(wrapper.Paint);
 }
Beispiel #2
0
 private static void ApplyWrapperTo(ref SKPaint current, DrawingContextImpl.PaintWrapper wrapper,
                                    ref IDisposable curr, SKPaint paint)
 {
     if (current == wrapper.Paint)
     {
         return;
     }
     curr?.Dispose();
     curr = wrapper.ApplyTo(paint);
 }
        internal void Draw(DrawingContextImpl context,
                           SKCanvas canvas, SKPoint origin,
                           DrawingContextImpl.PaintWrapper foreground)
        {
            /* TODO: This originated from Native code, it might be useful for debugging character positions as
             * we improve the FormattedText support. Will need to port this to C# obviously. Rmove when
             * not needed anymore.
             *
             *  SkPaint dpaint;
             *  ctx->Canvas->save();
             *  ctx->Canvas->translate(origin.fX, origin.fY);
             *  for (int c = 0; c < Lines.size(); c++)
             *  {
             *      dpaint.setARGB(255, 0, 0, 0);
             *      SkRect rc;
             *      rc.fLeft = 0;
             *      rc.fTop = Lines[c].Top;
             *      rc.fRight = Lines[c].Width;
             *      rc.fBottom = rc.fTop + LineOffset;
             *      ctx->Canvas->drawRect(rc, dpaint);
             *  }
             *  for (int c = 0; c < Length; c++)
             *  {
             *      dpaint.setARGB(255, c % 10 * 125 / 10 + 125, (c * 7) % 10 * 250 / 10, (c * 13) % 10 * 250 / 10);
             *      dpaint.setStyle(SkPaint::kFill_Style);
             *      ctx->Canvas->drawRect(Rects[c], dpaint);
             *  }
             *  ctx->Canvas->restore();
             */
            SKPaint     paint          = _paint;
            IDisposable currd          = null;
            var         currentWrapper = foreground;

            try
            {
                SKPaint currFGPaint       = ApplyWrapperTo(ref foreground, ref currd, paint);
                bool    hasCusomFGBrushes = _foregroundBrushes.Any();

                for (int c = 0; c < _skiaLines.Count; c++)
                {
                    AvaloniaFormattedTextLine line = _skiaLines[c];

                    float x = TransformX(origin.X, 0, paint.TextAlign);

                    if (!hasCusomFGBrushes)
                    {
                        var subString = _text.Substring(line.Start, line.Length);
                        canvas.DrawText(subString, x, origin.Y + line.Top + _lineOffset, paint);
                    }
                    else
                    {
                        float  currX = x;
                        string subStr;
                        int    len;

                        for (int i = line.Start; i < line.Start + line.Length;)
                        {
                            var fb = GetNextForegroundBrush(ref line, i, out len);

                            if (fb != null)
                            {
                                //TODO: figure out how to get the brush size
                                currentWrapper = context.CreatePaint(fb, new Size());
                            }
                            else
                            {
                                if (!currentWrapper.Equals(foreground))
                                {
                                    currentWrapper.Dispose();
                                }
                                currentWrapper = foreground;
                            }

                            subStr = _text.Substring(i, len);

                            if (currFGPaint != currentWrapper.Paint)
                            {
                                currFGPaint = ApplyWrapperTo(ref currentWrapper, ref currd, paint);
                            }

                            canvas.DrawText(subStr, currX, origin.Y + line.Top + _lineOffset, paint);

                            i     += len;
                            currX += paint.MeasureText(subStr);
                        }
                    }
                }
            }
            finally
            {
                if (!currentWrapper.Equals(foreground))
                {
                    currentWrapper.Dispose();
                }
                currd?.Dispose();
            }
        }