Example #1
0
        protected override Glyph GetGlyph(char character, ref Vector2 fontSize, bool uploadGpuResources)
        {
            // Add a safe guard to prevent the system to generate characters too big for the dynamic font cache texture
            fontSize.X = Math.Min(fontSize.X, 1024);
            fontSize.Y = Math.Min(fontSize.Y, 1024);

            // get the character data associated to the provided character and size
            var characterData = GetOrCreateCharacterData(fontSize, character);

            // generate the bitmap if it does not exist
            if (characterData.Bitmap == null)
            {
                FontManager.GenerateBitmap(characterData, false);
            }

            // upload the character to the GPU font texture and create the glyph if does not exists
            if (uploadGpuResources && characterData.Bitmap != null && !characterData.IsBitmapUploaded)
            {
                FontCacheManager.UploadCharacterBitmap(characterData);
            }

            // update the character usage info
            FontCacheManager.NotifyCharacterUtilization(characterData);

            return(characterData.Glyph);
        }
Example #2
0
        public void TestGenerateBitmap()
        {
            var fontManager = new FontManager();
            const int waitTime = 250;
            const int defaultSize = 4;

            // test that a simple bitmap generation success
            var characterA = new CharacterSpecification('a', "Arial", new Vector2(1.73f, 3.57f), FontStyle.Regular, FontAntiAliasMode.Default);
            fontManager.GenerateBitmap(characterA, false);
            WaitAndCheck(characterA, waitTime);
            Assert.AreEqual(4, characterA.Bitmap.Width);
            Assert.AreEqual(6, characterA.Bitmap.Rows);
            
            // test that rendering an already existing character to a new size works
            var characterA2 = new CharacterSpecification('a', "Arial", 10f * Vector2.One, FontStyle.Regular, FontAntiAliasMode.Default);
            fontManager.GenerateBitmap(characterA2, false);
            WaitAndCheck(characterA2, waitTime);
            Assert.AreNotEqual(2, characterA2.Bitmap.Width);
            Assert.AreNotEqual(4, characterA2.Bitmap.Rows);

            // test that trying to render a character that does not exist does not crash the system
            var characterTo = new CharacterSpecification('都', "Arial", defaultSize * Vector2.One, FontStyle.Regular, FontAntiAliasMode.Default);
            var characterB = new CharacterSpecification('b', "Arial", defaultSize * Vector2.One, FontStyle.Regular, FontAntiAliasMode.Default);
            fontManager.GenerateBitmap(characterTo, false);
            fontManager.GenerateBitmap(characterB, false);
            WaitAndCheck(characterB, 2 * waitTime);
            Assert.AreEqual(null, characterTo.Bitmap);

            // test that trying to render a character that does not exist does not crash the system
            var characterC = new CharacterSpecification('c', "Arial", -1 * Vector2.One, FontStyle.Regular, FontAntiAliasMode.Default);
            var characterD = new CharacterSpecification('d', "Arial", defaultSize * Vector2.One, FontStyle.Regular, FontAntiAliasMode.Default);
            fontManager.GenerateBitmap(characterC, false);
            fontManager.GenerateBitmap(characterD, false);
            WaitAndCheck(characterD, 2 * waitTime);
            Assert.AreEqual(null, characterC.Bitmap);
            
            fontManager.Dispose();
        }
Example #3
0
        internal override void PreGenerateGlyphs(ref StringProxy text, ref Vector2 size)
        {
            for (int i = 0; i < text.Length; i++)
            {
                // get the character data associated to the provided character and size
                var characterData = GetOrCreateCharacterData(size, text[i]);

                // force asynchronous generation of the bitmap if it does not exist
                if (characterData.Bitmap == null)
                {
                    FontManager.GenerateBitmap(characterData, true);
                }
            }
        }