示例#1
0
        private void paintImageSpan(PaintingContext context, Offset offset)
        {
            var canvas = context.canvas;
            var bounds = offset & size;

            canvas.save();
            var textOffset = 0;

            foreach (var span in text.children)
            {
                if (span is ImageSpan imageSpan)
                {
                    var offsetForCaret = getOffsetForCaret(
                        new TextPosition(offset: textOffset),
                        bounds
                        );
                    if (textOffset != 0 &&
                        offsetForCaret.dx == 0F &&
                        offsetForCaret.dy == 0F)
                    {
                        return;
                    }

                    var topLeftOffset = new Offset(
                        offset.dx + offsetForCaret.dx - (textOffset == 0 ? 0 : 0),
                        offset.dy + offsetForCaret.dy
                        );
                    if (imageSpan.imageResolver.image == null)
                    {
                        canvas.drawRect(
                            rect: Rect.fromLTWH(
                                topLeftOffset.dx + imageSpan.margin.left,
                                topLeftOffset.dy + imageSpan.margin.top,
                                imageSpan.innerWidth,
                                imageSpan.innerHeight
                                ),
                            paint: ImagePlaceholderBackgroundPaint);
                        var textBlobBuilder = new TextBlobBuilder();
                        var text            = $"{(char) Icons.MaterialImage.codePoint}";
                        var iconSize        = imageSpan.innerWidth > imageSpan.innerHeight * 3
                            ? imageSpan.innerHeight * 3 / 4
                            : imageSpan.innerWidth / 4;
                        textBlobBuilder.allocRunPos(
                            style: new TextStyle(
                                fontFamily: Icons.MaterialImage.fontFamily,
                                fontSize: iconSize
                                ),
                            text: text,
                            offset: 0,
                            size: text.Length);
                        var rect = topLeftOffset & new Size(imageSpan.innerWidth, imageSpan.innerHeight);
                        textBlobBuilder.setBounds(rect.toRect());
                        canvas.drawTextBlob(
                            textBlobBuilder.make(),
                            new Offset(
                                rect.left + (rect.width - iconSize) / 2,
                                rect.top + (rect.height + iconSize) / 2),
                            ImagePlaceholderPaint);

                        imageSpan.imageResolver.Resolve((imageInfo, synchronousCall) =>
                        {
                            if (synchronousCall)
                            {
                                ImageUtils.paintImage(
                                    canvas: canvas,
                                    rect: topLeftOffset & new Size(imageSpan.width, imageSpan.height),
                                    image: imageSpan.imageResolver.image,
                                    fit: BoxFit.scaleDown,
                                    alignment: Alignment.center
                                    );
                            }
                            else
                            {
                                if (owner == null || !owner.debugDoingPaint)
                                {
                                    markNeedsPaint();
                                }
                            }
                        });
                        textOffset += imageSpan.toPlainText().Length;

                        continue;
                    }

                    ImageUtils.paintImage(
                        canvas: canvas,
                        rect: Rect.fromLTWH(
                            topLeftOffset.dx + imageSpan.margin.left,
                            topLeftOffset.dy + imageSpan.margin.top,
                            imageSpan.innerWidth,
                            imageSpan.innerHeight
                            ),
                        image: imageSpan.imageResolver.image,
                        fit: BoxFit.scaleDown,
                        alignment: Alignment.center
                        );
                }

                textOffset += span.toPlainText().Length;
            }

            canvas.restore();
        }