Ejemplo n.º 1
0
        //public override void EndInit()
        //{
        //    base.EndInit();
        //    InitializeResources();
        //}

        //private void InitializeResources()
        //{
        //    resourcedTags = new TagCollection();
        //    foreach (TaggedFontFormat item in TaggedFontFormats)
        //    {
        //        //r = item as FontFormat;
        //        if (item == null) continue;
        //        resourcedTags.Add(item.Tag);
        //        item.PrepareTypeface(this);
        //    }
        //}

        private void InitializeDefaultFontFormat()
        {
            DefaultFontFormat = new FontFormat()
            {
                FontFamily  = FontFamily,
                FontSize    = FontSize,
                FontStretch = FontStretch,
                FontStyle   = FontStyle,
                FontWeight  = FontWeight,
                Foreground  = Foreground,
                Style       = Style,
            };
            DefaultFontFormat.PrepareTypeface(this);
            TryInvalidateDisplay();
        }
Ejemplo n.º 2
0
        private void RenderWordRequest(Size renderSize, string currentWord, FontFormat fontFormat, double renderingXPosition, double renderingYPosition, out string remainingWordCharacters, out double wordWidth, out double wordHeight, out bool isRendered, DrawingContext drawingContext = null)
        {
            isRendered = false;
            wordHeight = 0;
            remainingWordCharacters = string.Empty;
            if (string.IsNullOrEmpty(currentWord))
            {
                wordWidth  = 0;
                isRendered = true;
                return;
            }

            var drawingText = currentWord;

            var currentCharIndex = 0;
            var currentChar      = drawingText[currentCharIndex];

            var glyphIndexes  = new List <ushort>();
            var advanceWidths = new List <double>();

            //var currentTypeface = fontFormat.typeface;
            var currentGlyphTypeface = fontFormat.glyphTypeface;

            var currentLineWidth = renderingXPosition;

            wordWidth = 0d;

            while (currentCharIndex < drawingText.Length)
            {
                currentChar = drawingText[currentCharIndex];
                var isHorizontalSpaceAvailable = true;
                if (!UNAVAILABLE_GLYPHS.Contains(currentChar))
                {
                    if (!currentGlyphTypeface.CharacterToGlyphMap.ContainsKey(currentChar))
                    {
                        currentChar = '_';
                    }
                    var currentCharGlyphIndex = currentGlyphTypeface.CharacterToGlyphMap[currentChar];
                    var currentCharWidth      = currentGlyphTypeface.AdvanceWidths[currentCharGlyphIndex] * fontFormat.FontSize;
                    wordWidth        += currentCharWidth;
                    currentLineWidth += currentCharWidth;
                    wordHeight        = Math.Max(wordHeight, currentGlyphTypeface.AdvanceHeights[currentCharGlyphIndex] * fontFormat.FontSize);

                    isHorizontalSpaceAvailable = currentLineWidth < renderSize.Width;
                    if (currentCharIndex != 0 && renderingXPosition == 0 && !isHorizontalSpaceAvailable)
                    {
                        wordWidth              -= currentCharWidth;
                        currentLineWidth       -= currentCharWidth;
                        remainingWordCharacters = currentWord.Substring(currentCharIndex, currentWord.Length - currentCharIndex);
                        isRendered              = false;
                        break;
                    }
                    if (!isHorizontalSpaceAvailable)
                    {
                        isRendered = false;
                        return;
                    }
                    glyphIndexes.Add(currentCharGlyphIndex);
                    advanceWidths.Add(currentCharWidth);
                }

                currentCharIndex++;
            }

            if (drawingContext != null)
            {
                var origin = new Point(renderingXPosition, renderingYPosition + fontFormat.LineHeight.Value);

                var glyphRun = new GlyphRun(currentGlyphTypeface, 0, false, fontFormat.FontSize,
                                            glyphIndexes, origin, advanceWidths, null, null, null, null,
                                            null, null);
                // Draws the current line
                drawingContext.DrawGlyphRun(fontFormat.Foreground == null ? DefaultFontFormat.Foreground : fontFormat.Foreground, glyphRun);
            }
            if (string.IsNullOrEmpty(remainingWordCharacters))
            {
                isRendered = true;
            }
        }