Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use special characters like "é" or "è" #9

Open
Tethyr79Mb opened this issue Mar 25, 2024 · 4 comments
Open

Cannot use special characters like "é" or "è" #9

Tethyr79Mb opened this issue Mar 25, 2024 · 4 comments

Comments

@Tethyr79Mb
Copy link

I have a exception KeyNotFoundException when i using a special character like "é" or "è". I have set the spritefont end character region to 383. Monogame displays them however by modifying the spritefont.

Help would be appreciated thank you very much.

@Videogamers0
Copy link
Owner

Videogamers0 commented Mar 25, 2024

You're right, this seems to be due to the Start/End CharacterRegions of the spritefonts that MGUI comes with. I will add fallback logic to use the SpriteFont's DefaultCharacter if the desired Glyph isn't found.

If you want to use your own SpriteFonts in MGUI, which you can set their CharacterRegions to anything you wish, you can do the following in your game's initialize method right after instantiating an MGDesktop:

protected override void Initialize()
{
    //  Create your MGDesktop
    this.MGUIRenderer = new MainRenderer(new GameRenderHost<Game1>(this));
    this.Desktop = new MGDesktop(MGUIRenderer);

    //  Add a FontSet to the desktop. FontSets require a collection of SpriteFonts belonging to the same font family, but at different sizes and styles (bold/italic)
    //  The more SpriteFonts you define, the better MGUI will be at rendering text of varying sizes and styles for that family. This is a simple example with just 2 font sizes
    //  See also: SpriteFontGenerator.Generate(...), which can quickly create a set of .spritefont files for you to add to your game's content
    Dictionary<SpriteFont, FontMetadata> CalibriSpriteFonts = new Dictionary<SpriteFont, FontMetadata>
    {
        { Content.Load<SpriteFont>("Calibri 12pt"), new FontMetadata(12, false, false) },
        { Content.Load<SpriteFont>("Calibri 12pt bold"), new FontMetadata(12, true, false) },
        { Content.Load<SpriteFont>("Calibri 12pt italic"), new FontMetadata(12, false, true) },
        { Content.Load<SpriteFont>("Calibri 14pt"), new FontMetadata(14, false, false) },
        { Content.Load<SpriteFont>("Calibri 14pt bold"), new FontMetadata(14, true, false) },
        { Content.Load<SpriteFont>("Calibri 14pt italic"), new FontMetadata(14, false, true) },
    };
    Desktop.FontManager.AddFontSet(new Shared.Text.FontSet("Calibri", CalibriSpriteFonts));

    //  Make the new FontSet the default for TextBlocks to use when no FontFamily is specified
    Desktop.Theme.FontSettings.DefaultFontFamily = "Calibri";

    base.Initialize();
}

Added fallback logic for missing glyphs: 96a4db5

@orosbogdan
Copy link

orosbogdan commented Oct 9, 2024

You're right, this seems to be due to the Start/End CharacterRegions of the spritefonts that MGUI comes with. I will add fallback logic to use the SpriteFont's DefaultCharacter if the desired Glyph isn't found.

If you want to use your own SpriteFonts in MGUI, which you can set their CharacterRegions to anything you wish, you can do the following in your game's initialize method right after instantiating an MGDesktop:

protected override void Initialize()
{
    //  Create your MGDesktop
    this.MGUIRenderer = new MainRenderer(new GameRenderHost<Game1>(this));
    this.Desktop = new MGDesktop(MGUIRenderer);

    //  Add a FontSet to the desktop. FontSets require a collection of SpriteFonts belonging to the same font family, but at different sizes and styles (bold/italic)
    //  The more SpriteFonts you define, the better MGUI will be at rendering text of varying sizes and styles for that family. This is a simple example with just 2 font sizes
    //  See also: SpriteFontGenerator.Generate(...), which can quickly create a set of .spritefont files for you to add to your game's content
    Dictionary<SpriteFont, FontMetadata> CalibriSpriteFonts = new Dictionary<SpriteFont, FontMetadata>
    {
        { Content.Load<SpriteFont>("Calibri 12pt"), new FontMetadata(12, false, false) },
        { Content.Load<SpriteFont>("Calibri 12pt bold"), new FontMetadata(12, true, false) },
        { Content.Load<SpriteFont>("Calibri 12pt italic"), new FontMetadata(12, false, true) },
        { Content.Load<SpriteFont>("Calibri 14pt"), new FontMetadata(14, false, false) },
        { Content.Load<SpriteFont>("Calibri 14pt bold"), new FontMetadata(14, true, false) },
        { Content.Load<SpriteFont>("Calibri 14pt italic"), new FontMetadata(14, false, true) },
    };
    Desktop.FontManager.AddFontSet(new Shared.Text.FontSet("Calibri", CalibriSpriteFonts));

    //  Make the new FontSet the default for TextBlocks to use when no FontFamily is specified
    Desktop.Theme.FontSettings.DefaultFontFamily = "Calibri";

    base.Initialize();
}

Added fallback logic for missing glyphs: 96a4db5

Can we use .ttf fonts (like from https://www.dafont.com/) with MGUI ? Is it any option to render text at runtime based on ttf files ?

Also can we use Unicode with MGUI ? Would adding FontStashSharp support in the future be an option ?

@Videogamers0
Copy link
Owner

Videogamers0 commented Oct 9, 2024

MGUI only supports text rendering via MonoGame's SpriteFont objects.

If you want to use a .ttf font file, you'll have to generate a SpriteFont from each specific font size and font style of the family that you want to use. To do this at runtime, I'd recommend using the SpriteFontPlus library (https://github.com/rds1983/SpriteFontPlus, https://www.nuget.org/packages/SpriteFontPlus).

26c1604 I've added a sample of this to MGUI.Samples/Game1.cs Initialize method:

lines 54 to 113

To use unicode, you'd have to add all character ranges to the SpriteFonts. Obviously the size of the SpriteFont's texture would be absolutely massive and unfeasible if you did that since it would include too many characters. You should probably pick and choose only the character regions you're sure that you need: https://www.ssec.wisc.edu/~tomw/java/unicode.html

@orosbogdan
Copy link

orosbogdan commented Oct 9, 2024

MGUI only supports text rendering via MonoGame's SpriteFont objects.

If you want to use a .ttf font file, you'll have to generate a SpriteFont from each specific font size and font style of the family that you want to use. To do this at runtime, I'd recommend using the SpriteFontPlus library (https://github.com/rds1983/SpriteFontPlus, https://www.nuget.org/packages/SpriteFontPlus).

26c1604 I've added a sample of this to MGUI.Samples/Game1.cs Initialize method:

lines 54 to 113
To use unicode, you'd have to add all character ranges to the SpriteFonts. Obviously the size of the SpriteFont's texture would be absolutely massive and unfeasible if you did that since it would include too many characters. You should probably pick and choose only the character regions you're sure that you need: https://www.ssec.wisc.edu/~tomw/java/unicode.html

Thank you so much for the detailed response and the great work with this library. I always gain so much insights from these issues on git and your amazing answers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants