Beispiel #1
0
        private void DrawRenderLine(DrawingContext drawingContext, StringTextRenderLine bufferRenderLine)
        {
            if (bufferRenderLine.Data == null)
            {
                return;
            }

            var fontSize = FontSize;

            var startPosition = bufferRenderLine.StartPosition;

            startPosition.Y += GlyphTypeface.AdvanceHeights[0] * fontSize + CellMargin.Top + CellPadding.Top;

            var glyphIndexes  = new ushort[bufferRenderLine.Data.Length * 3];
            var advanceWidths = new double[bufferRenderLine.Data.Length * 3];

            for (int i = 0; i < bufferRenderLine.Data.Length; i++)
            {
                GlyphTypeface.CharacterToGlyphMap.TryGetValue(' ', out glyphIndexes[3 * i]);
                GlyphTypeface.CharacterToGlyphMap.TryGetValue(bufferRenderLine.Data[i], out glyphIndexes[3 * i + 1]);
                GlyphTypeface.CharacterToGlyphMap.TryGetValue(' ', out glyphIndexes[3 * i + 2]);

                advanceWidths[3 * i]     = CellMargin.Left + CellPadding.Right;
                advanceWidths[3 * i + 1] = CharSize.Width;
                advanceWidths[3 * i + 2] = CellPadding.Right + CellMargin.Right;
            }

#if NET47
            var glyph = new GlyphRun(GlyphTypeface, 0, false, fontSize, (float)PixelPerDip, glyphIndexes, startPosition, advanceWidths, null, null, null, null, null, null);
#endif

#if NET451
            var glyph = new GlyphRun(GlyphTypeface, 0, false, fontSize, glyphIndexes, startPosition, advanceWidths, null, null, null, null, null, null);
#endif
            drawingContext.DrawGlyphRun(bufferRenderLine.Foreground, glyph);
        }
Beispiel #2
0
        private IEnumerable <StringTextRenderLine> GetRenderLines()
        {
            if (BytesToCharEncoding == null)
            {
                yield break;
            }

            if (Data == null)
            {
                yield break;
            }


            var data = Data;
            var bytesToCharEncoding = BytesToCharEncoding;
            var bytePerLine         = BytePerLine;
            var foreground          = Foreground;
            var foregroundBlocks    = ForegroundBlocks;
            var fontSize            = FontSize;

            var cellSize            = GetCellSize();
            var firstVisibleBtIndex = (int)(bytesToCharEncoding.BytePerChar - PositionStartToShow % bytesToCharEncoding.BytePerChar) % bytesToCharEncoding.BytePerChar;
            var charCount           = (data.Length - firstVisibleBtIndex) / bytesToCharEncoding.BytePerChar;
            var row = -1;
            StringTextRenderLine lastRenderLine = default;
            var lineReturned = false;

            var charList = new List <char>();

            if (_drawCharBuffer == null || _drawCharBuffer.Length != bytesToCharEncoding.BytePerChar)
            {
                _drawCharBuffer = new byte[bytesToCharEncoding.BytePerChar];
            }

            for (int chIndex = 0; chIndex < charCount; chIndex++)
            {
                var btIndex        = bytesToCharEncoding.BytePerChar * chIndex;
                var thisCol        = btIndex % bytePerLine;
                var thisRow        = btIndex / bytePerLine;
                var thisForeground = foreground;

                //Refresh foreground;
                if (foregroundBlocks != null)
                {
                    foreach (var brushBlock in foregroundBlocks)
                    {
                        if (brushBlock.StartOffset <= btIndex && brushBlock.StartOffset + brushBlock.Length - 1 >= btIndex)
                        {
                            thisForeground = brushBlock.Brush;
                        }
                    }
                }

                Buffer.BlockCopy(data, btIndex + firstVisibleBtIndex, _drawCharBuffer, 0, bytesToCharEncoding.BytePerChar);

                var ch = bytesToCharEncoding.ConvertToChar(_drawCharBuffer);

                if (thisRow != row || !lineReturned || lastRenderLine.Foreground != thisForeground)
                {
                    var textPosition = this.GetCellPosition(btIndex);
                    if (textPosition == null)
                    {
                        continue;
                    }

                    if (lineReturned)
                    {
                        lastRenderLine.Data = charList.ToArray();
                        yield return(lastRenderLine);
                    }

                    charList.Clear();
                    lastRenderLine = new StringTextRenderLine {
                        Foreground    = thisForeground,
                        StartPosition = textPosition.Value
                    };

                    lineReturned = true;
                }

                charList.Add(ch);

                row = thisRow;
            }

            if (lineReturned)
            {
                lastRenderLine.Data = charList.ToArray();
                yield return(lastRenderLine);
            }
        }