Fallback font and font substitution for presentations
Tags: beginners, api, programming, csharp
Published: 2019-11-04 16:56:39

First, I want to explain you why this topic is important:
Imagine, you have developed an application for automated presentations generation.  You used, **[Aspose.Slides API](https://products.aspose.com/slides?utm_source=https://dev.to/aspose_slides/fallback-font-and-font-substitution-for-presentations-173o&utm_medium=email)**, for the [known reasons](https://dev.to/aspose/aspose-apis-save-you-from-ms-office-automation-hell-59dh), with all it wealth of opportunities. Your application is cool: it uses nice UI templates, AI/ML-based generation from text, finds the right images from internet, showes complicated 3D graphics and statistics data, sends rockets into space...🚀
You run the application and generate hundreds of presentation. But, suddenly you see a small annoying problem with presentations text... - BLACK BOXES instead of characters in text.

_**"What’s that? Why? Is that aliens trying to talk to me?"**_ - you think.

<img width="500" src="https://media.giphy.com/media/kKBY5n9D5LTuE/giphy.gif">



<h2>Fallback font and Font substitution</h2>
The answer is: _"No. Your computer is just saying he doesnt know the font of your text."_
But there are two backup plans: **fallback font** and **font substitution**.

<h4>Fallback font</h4>
When your system meets a Unicode character, it checks if it's in the specified font. In case it’s not - it starts to look through all the fonts to find the suitable one, based on the character glyph. The last font - **fallback font** - has glyphs for all the characters, and it will be checked if the character needed was not found in all other fonts.
Glyphs - are just visual representations of any Unicode character. They are something more meaningful than a black box for missing characters. Systems that don’t have a fallback font usually display black or white rectangles, question marks, the Unicode Replacement Character (U+FFFD), or nothing at all, in place of unfound characters.

<h4>Fonts substitution</h4>
**Font substitution** is the process of using one font instead of another one, when the initial font either can not be found or does not contain glyphs for the required characters. Font is choosen by similarity, based on: font family, system's font configuration, etc.

_**How can I use fallback fonts and font substitution to save the situation?**_ - you ask.


<h2>How to use it</h2>
**[Aspose.Slides API](https://products.aspose.com/slides?utm_source=https://dev.to/aspose_slides/fallback-font-and-font-substitution-for-presentations-173o&utm_medium=email)** has a FontsManager, which allows to flexibily manage fonts in presentation: set, embed, replace, substitute them. In order to control the fallback font replacement, you may define the rules for that, setting font used with the characters Unicode range:

```csharp
IFontFallBackRule firstRule = new FontFallBackRule(startUnicodeIndex, endUnicodeIndex, "Vijaya");
IFontFallBackRule secondRule = new FontFallBackRule(0x3040, 0x309F, "MS Mincho, MS Gothic");
```

Add them to rules collection and set this collection to FontsManager’s:
```csharp
IFontFallBackRulesCollection rules = new FontFallBackRulesCollection();
rules.Add(firstRule);
rules.Add(secondRule);

presentation.FontsManager.FontFallBackRulesCollection = rules;
```

Now let’s try to render thumbnail with new rules and save to PNG:
```csharp
presentation.Slides[0].GetThumbnail(1f, 1f).Save(dataDir + "Slide_0.png", ImageFormat.Png);
```

Let us see the result achieved. The first one - is the image without  using the fallback font, the second - uses it.

Without fallback font:
<img width="500" src="https://i.imgur.com/EpO75vC.png">

With fallback font:
<img width="500" src="https://i.imgur.com/L859j3I.png">

Font substitution is the same easy to use, and you can find more **[here](https://docs.aspose.com/display/slidesnet/managing+fonts?utm_source=https://dev.to/aspose_slides/fallback-font-and-font-substitution-for-presentations-173o&utm_medium=email)** and **[here](https://docs.aspose.com/display/slidesnet/manipulating+text?utm_source=https://dev.to/aspose_slides/fallback-font-and-font-substitution-for-presentations-173o&utm_medium=email)**.👈

<hr/>
**How many of you know which language is on the images? Please, comment.**😁👇👇👇