Configure Font Substitution in Presentations on Android
Overview
Font substitution allows Aspose.Slides to use an available font in place of a font that cannot be accessed when a presentation is rendered or converted. The substitution affects the rendered output; it does not change the font assigned to the presentation content.
You can define the font to use when a particular font is unavailable, and you can inspect the substitutions that Aspose.Slides will make during rendering. This helps keep output consistent across Android devices and environments with different available fonts.
Get Font Substitutions
Use the IFontsManager.getSubstitutions method to determine which fonts will be substituted when the presentation is rendered. The method returns FontSubstitutionInfo objects that identify the original and substituted font names.
The following Java example lists all font substitutions for a presentation:
import com.aspose.slides.FontSubstitutionInfo;
import com.aspose.slides.Presentation;
Presentation presentation = new Presentation("Presentation.pptx");
try {
for (FontSubstitutionInfo substitution : presentation.getFontsManager().getSubstitutions()) {
System.out.println(substitution.getOriginalFontName() + " -> " + substitution.getSubstitutedFontName());
}
} finally {
presentation.dispose();
}
Get Font Substitutions for Selected Slides
Use the IFontsManager.getSubstitutions overload with an int[] slides argument to inspect only the substitutions required to render specific slides. This is useful when you are rendering or exporting part of a presentation, checking a large presentation incrementally, locating slides that depend on unavailable fonts, preparing a minimal font package for an Android app, or diagnosing rendering differences without processing unrelated slides.
The slides array contains one-based slide indexes: 1 identifies the first slide. By contrast, the Presentation.getSlides collection accessor uses zero-based indexing, so that same slide is accessed as presentation.getSlides().get_Item(0). Keep this difference in mind when building the array to avoid off-by-one errors.
Call the overload through the Presentation.getFontsManager method. It returns only the substitutions determined while rendering the selected slides. Each result is a FontSubstitutionInfo object containing the original and substituted font names. The result reflects the current font environment, configured fallback rules, substitution rules stored in an IFontSubstRuleCollection, and externally loaded fonts.
The same substitution can be required by more than one selected slide. Deduplicate the results when you create a font inventory or preflight report. The following example reports every returned substitution and then creates a sorted list of unique font mappings:
import com.aspose.slides.FontSubstitutionInfo;
import com.aspose.slides.Presentation;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
Presentation presentation = new Presentation("Presentation.pptx");
try {
int[] selectedSlides = { 1, 3, 5 };
List<FontSubstitutionInfo> substitutions = new ArrayList<>();
for (FontSubstitutionInfo substitution : presentation.getFontsManager().getSubstitutions(selectedSlides)) {
substitutions.add(substitution);
}
System.out.println("Substitutions for the selected slides:");
for (FontSubstitutionInfo substitution : substitutions) {
System.out.println(substitution.getOriginalFontName() + " -> " + substitution.getSubstitutedFontName());
}
Set<String> sortedPreflightEntries = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
for (FontSubstitutionInfo substitution : substitutions) {
String entry = substitution.getOriginalFontName() + " -> " + substitution.getSubstitutedFontName();
sortedPreflightEntries.add(entry);
}
System.out.println("Deduplicated font preflight report:");
for (String entry : sortedPreflightEntries) {
System.out.println(entry);
}
} finally {
presentation.dispose();
}
The IFontsManager interface provides both overloads. Choose one according to the scope of the rendering operation:
| Overload | Use it when |
|---|---|
| getSubstitutions with no arguments | You need substitutions for the entire presentation. |
getSubstitutions with int[] slides |
You need substitutions for a selected range, incremental check, or partial export. |
Set Font Substitution Rules
To specify the font that Aspose.Slides should use when a source font is unavailable:
- Load the presentation.
- Create font definitions for the source and substitute fonts.
- Create a FontSubstRule with the WhenInaccessible condition.
- Add the rule to a FontSubstRuleCollection.
- Assign the collection by using the FontsManager.setFontSubstRuleList method.
- Render or convert the presentation.
The following Java example substitutes Arial for SomeRareFont when SomeRareFont is unavailable, and then renders the first slide to verify the result. The substitute font must be available to Aspose.Slides.
import com.aspose.slides.FontData;
import com.aspose.slides.FontSubstCondition;
import com.aspose.slides.FontSubstRule;
import com.aspose.slides.FontSubstRuleCollection;
import com.aspose.slides.IFontData;
import com.aspose.slides.IFontSubstRule;
import com.aspose.slides.IFontSubstRuleCollection;
import com.aspose.slides.IImage;
import com.aspose.slides.ImageFormat;
import com.aspose.slides.Presentation;
Presentation presentation = new Presentation("Fonts.pptx");
try {
IFontData sourceFont = new FontData("SomeRareFont");
IFontData substituteFont = new FontData("Arial");
IFontSubstRule substitutionRule = new FontSubstRule(sourceFont, substituteFont, FontSubstCondition.WhenInaccessible);
IFontSubstRuleCollection substitutionRules = new FontSubstRuleCollection();
substitutionRules.add(substitutionRule);
presentation.getFontsManager().setFontSubstRuleList(substitutionRules);
IImage image = presentation.getSlides().get_Item(0).getImage(1f, 1f);
try {
image.save("slide.jpg", ImageFormat.Jpeg);
} finally {
image.dispose();
}
} finally {
presentation.dispose();
}
Note
For an unconditional change to the fonts used throughout a presentation, see Font Replacement.Limitations for Math Equation Fonts
Font substitution rules are part of the standard font selection process used during rendering and conversion. They work for regular text when Aspose.Slides can replace an inaccessible font with the available font specified by a rule.
Office Math equations have an additional requirement. If an equation uses Cambria Math, Aspose.Slides may need that exact font to calculate and render the equation layout. A rule that substitutes another math font, such as STIX Two Math, cannot replace Cambria Math for this purpose, and rendering may still report that Cambria Math is required.
To render or convert such a presentation, make Cambria Math available to Aspose.Slides. Load it as an external font so the application can use it during rendering and conversion.
This limitation applies to equation layout. The substitution rules described above still apply to regular presentation text.
FAQ
What is the difference between font replacement and font substitution?
Font replacement intentionally changes one font to another throughout the presentation. Font substitution selects a font for rendered output when the configured condition is met, such as when the original font is unavailable.
When are substitution rules applied?
The rules participate in the font selection sequence during rendering and conversion. With WhenInaccessible, a rule is used only when Aspose.Slides cannot access the source font.
What happens when a font is missing and no substitution rule is configured?
Aspose.Slides selects the closest available font according to its font selection process. The result depends on the fonts available in the runtime environment.
Can I load external fonts to avoid substitution?
Yes. You can load external fonts so Aspose.Slides can use them during rendering and conversion.
Does Aspose distribute fonts with the library?
No. You are responsible for providing fonts and complying with their licenses.
Can substitution results differ between Android devices?
Yes. Available system fonts can differ between Android versions, devices, and vendors, so a font available in one environment may require substitution in another.
How can I make font selection consistent across Android devices?
Package the same required font files with the application, load them as external fonts, and embed fonts when licensing permits. You can also call IFontsManager.getSubstitutions before export to identify unexpected substitutions.