void EnsureResources(ICanvasResourceCreatorWithDpi resourceCreator, Size targetSize)
        {
            if (resourceRealizationSize == targetSize && !needsResourceRecreation)
            {
                return;
            }

            float canvasWidth  = (float)targetSize.Width;
            float canvasHeight = (float)targetSize.Height;

            sizeDim = Math.Min(canvasWidth, canvasHeight);

            textBrush = new CanvasSolidColorBrush(resourceCreator, Colors.Thistle);

            if (!defaultFontSizeSet)
            {
                if (ThumbnailGenerator.IsDrawingThumbnail)
                {
                    CurrentFontSize = 180;
                }
                else
                {
                    CurrentFontSize = sizeDim / 20;
                }
                CurrentFontSize      = Math.Max((float)fontSizeSlider.Minimum, CurrentFontSize);
                CurrentFontSize      = Math.Min((float)fontSizeSlider.Maximum, CurrentFontSize);
                fontSizeSlider.Value = CurrentFontSize;
                defaultFontSizeSet   = true;
            }

            string sampleText = null;

            switch (CurrentTextSampleOption)
            {
            case TextSampleOption.ChemicalFormula:
                sampleText =
                    "H2O is the chemical formula for water.\r\n\r\n" +
                    "And, the isotope Carbon-12 may be written as 12C.\r\n\r\n" +
                    "Often, chemical formulas make use of both superscript and subscript text.";
                break;

            case TextSampleOption.RightTriangle:
                sampleText =
                    "The side lengths of a right-angle triangle can be written as a2 + b2 = c2.\r\n\r\n" +
                    "If the triangle's shorter sides are lengths 3 and 4, the remaining side must be 5, since 32 + 42 = 52.";
                break;

            case TextSampleOption.ShortExpression:
                sampleText = "ax2by3";
                break;

            default:
                Debug.Assert(false, "Unexpected text sample option");
                break;
            }

            if (textLayout != null)
            {
                textLayout.Dispose();
            }
            textLayout = CreateTextLayout(sampleText, resourceCreator, canvasWidth, canvasHeight);

            switch (CurrentTextSampleOption)
            {
            case TextSampleOption.ChemicalFormula:
                SetSubscript(textLayout, sampleText.IndexOf("H2O") + 1, 1);
                SetSuperscript(textLayout, sampleText.IndexOf("12C"), 2);
                SetSubscript(textLayout, sampleText.IndexOf("subscript"), "subscript".Length);
                SetSuperscript(textLayout, sampleText.IndexOf("superscript"), "superscript".Length);
                break;

            case TextSampleOption.RightTriangle:
                for (int i = 0; i < sampleText.Length; ++i)
                {
                    if (sampleText[i] == '2')
                    {
                        SetSuperscript(textLayout, i, 1);
                    }
                }
                break;

            case TextSampleOption.ShortExpression:
                SetSubscript(textLayout, 1, 1);
                SetSuperscript(textLayout, 2, 1);
                SetSubscript(textLayout, 4, 1);
                SetSuperscript(textLayout, 5, 1);
                break;

            default:
                Debug.Assert(false, "Unexpected text sample option");
                break;
            }

            subscriptSuperscriptRenderer = new SubscriptSuperscriptRenderer();

            needsResourceRecreation = false;
            resourceRealizationSize = targetSize;
        }
        void EnsureResources(ICanvasResourceCreatorWithDpi resourceCreator, Size targetSize)
        {
            if (resourceRealizationSize == targetSize && !needsResourceRecreation)
                return;

            float canvasWidth = (float)targetSize.Width;
            float canvasHeight = (float)targetSize.Height;
            sizeDim = Math.Min(canvasWidth, canvasHeight);
            
            textBrush = new CanvasSolidColorBrush(resourceCreator, Colors.Thistle);

            if (!defaultFontSizeSet)
            {
                if (ThumbnailGenerator.IsDrawingThumbnail)
                {
                    CurrentFontSize = 180;
                }
                else
                {
                    CurrentFontSize = sizeDim / 20;
                }
                CurrentFontSize = Math.Max((float)fontSizeSlider.Minimum, CurrentFontSize);
                CurrentFontSize = Math.Min((float)fontSizeSlider.Maximum, CurrentFontSize);
                fontSizeSlider.Value = CurrentFontSize;
                defaultFontSizeSet = true;
            }
            
            string sampleText = null;
            switch (CurrentTextSampleOption)
            {
                case TextSampleOption.ChemicalFormula:
                    sampleText =
                        "H2O is the chemical formula for water.\r\n\r\n" +
                        "And, the isotope Carbon-12 may be written as 12C.\r\n\r\n" +
                        "Often, chemical formulas make use of both superscript and subscript text.";
                    break;
                case TextSampleOption.RightTriangle:
                    sampleText =
                        "The side lengths of a right-angle triangle can be written as a2 + b2 = c2.\r\n\r\n" +
                        "If the triangle's shorter sides are lengths 3 and 4, the remaining side must be 5, since 32 + 42 = 52.";
                    break;
                case TextSampleOption.ShortExpression:
                    sampleText = "ax2by3";
                    break;
                default:
                    Debug.Assert(false, "Unexpected text sample option");
                    break;
            }

            if (textLayout != null)
                textLayout.Dispose();
            textLayout = CreateTextLayout(sampleText, resourceCreator, canvasWidth, canvasHeight);

            switch (CurrentTextSampleOption)
            {
                case TextSampleOption.ChemicalFormula:
                    SetSubscript(textLayout, sampleText.IndexOf("H2O") + 1, 1);
                    SetSuperscript(textLayout, sampleText.IndexOf("12C"), 2);
                    SetSubscript(textLayout, sampleText.IndexOf("subscript"), "subscript".Length);
                    SetSuperscript(textLayout, sampleText.IndexOf("superscript"), "superscript".Length);
                    break;
                case TextSampleOption.RightTriangle:
                    for (int i = 0; i < sampleText.Length; ++i)
                    {
                        if (sampleText[i] == '2')
                            SetSuperscript(textLayout, i, 1);
                    }
                    break;
                case TextSampleOption.ShortExpression:
                    SetSubscript(textLayout, 1, 1);
                    SetSuperscript(textLayout, 2, 1);
                    SetSubscript(textLayout, 4, 1);
                    SetSuperscript(textLayout, 5, 1);
                    break;
                default:
                    Debug.Assert(false, "Unexpected text sample option");
                    break;
            }
            
            subscriptSuperscriptRenderer = new SubscriptSuperscriptRenderer();

            needsResourceRecreation = false;
            resourceRealizationSize = targetSize;
        }