rds1983 / StbSharp

C# port of the famous C framework
69 stars 8 forks source link

StpSharp.MonoGame.WindowsDX.Test - no UTF8 support for text? #3

Closed Krakean closed 6 years ago

Krakean commented 6 years ago

Hello Trying to test stb_truetype for unicode text output:

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            _spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);

            _spriteBatch.Draw(_image, new Vector2(0, 0));
            _spriteBatch.Draw(_fontTexture, new Vector2(_image.Width + 10, 0));

            DrawTTFString(_spriteBatch, "E: The quick brown fox jumps over the lazy dog, I: An ḃfuil do ċroí ag bualaḋ ó ḟaitíos",
                new Vector2(0, _image.Height + 30), Color.White);
            DrawTTFString(_spriteBatch, "G: Üben quält finſteren Jagdſchloß höf‌liche Bäcker größeren, N: Blåbærsyltetøy",
                new Vector2(0, _image.Height + 60), Color.White);
            DrawTTFString(_spriteBatch, "D: Høj bly gom vandt fræk sexquiz på wc, S: bäckasiner söka",
                new Vector2(0, _image.Height + 90), Color.White);
            DrawTTFString(_spriteBatch, "I: Sævör grét áðan því úlpan var ónýt, P: Pchnąć w tę łódź jeża lub osiem skrzyń fig",
                new Vector2(0, _image.Height + 120), Color.White);
            DrawTTFString(_spriteBatch, "C: Příliš žluťoučký kůň úpěl ďábelské kódy, R: В чащах юга жил-был цитрус? Да, но фальшивый экземпляр! ёъ.",
                new Vector2(0, _image.Height + 160), Color.White);
            DrawTTFString(_spriteBatch, "S: kilómetros y frío, añoraba, P: vôo à noite, F: Les naïfs ægithales hâtifs pondant à Noël où",
                new Vector2(0, _image.Height + 190), Color.White);
            DrawTTFString(_spriteBatch, "J: いろはにほへど",
                new Vector2(0, _image.Height + 220), Color.White);

            _spriteBatch.End();

            base.Draw(gameTime);
        }

But see this: https://i.imgur.com/Kc3cmNu.png Tried different fonts, no result. Can you please tell me what I do wrong, or is it a restriction of StbSharp?

rds1983 commented 6 years ago

Hi, Take a look at this string in the Game1.cs: StbTrueType.stbtt_BakeFontBitmap(buffer, 0, 48, tempBitmap, FontBitmapWidth, FontBitmapHeight, 32, 96, charData);

It tells StbTrueType to bake a font bitmap with 96 characters starting from index 32(space). Probably that range doesnt include accented characters. So if you replace that string with say StbTrueType.stbtt_BakeFontBitmap(buffer, 0, 48, tempBitmap, FontBitmapWidth, FontBitmapHeight, 32, 224, charData); Then at least accented characters will show up. Unsure how to add ranges with cyrillic & chinese characters as I dont know original stb_truetype well.

rds1983 commented 6 years ago

I've added FontBaker class. It allows to bake glyphs from multiple ttfs with arbitrary character ranges. I've updated the sample we were discussing. Now it bakes font atlas from two different ttf(one regular and one japanese). Now the sample has following look: image

I've removed that string: "I: An ḃfuil do ċroí ag bualaḋ ó ḟaitíos" as it uses characters from this range: http://jrgraphix.net/r/Unicode/1E00-1EFF

And I couldn't find ttf containing all characters from that range. Thanks for reporting this issue. I am closing it.