// RichTextKit
// Copyright © 2019-2020 Topten Software. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this product except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Text;
namespace Topten.RichTextKit
{
///
/// The FontMapper class is responsible for mapping style typeface information
/// to an SKTypeface.
///
public class FontMapper
{
///
/// Constructs a new FontMapper instnace
///
public FontMapper()
{
}
///
/// Maps a given style to a specific typeface
///
/// The style to be mapped
/// Indicates the mapping should ignore font variants (use to get font for ellipsis)
/// A mapped typeface
public virtual SKTypeface TypefaceFromStyle(IStyle style, bool ignoreFontVariants)
{
// Extra weight for superscript/subscript
int extraWeight = 0;
if (!ignoreFontVariants && (style.FontVariant == FontVariant.SuperScript || style.FontVariant == FontVariant.SubScript))
{
extraWeight += 100;
}
// Get the typeface
return SKTypeface.FromFamilyName(
style.FontFamily,
(SKFontStyleWeight)(style.FontWeight + extraWeight),
style.FontWidth,
style.FontItalic ? SKFontStyleSlant.Italic : SKFontStyleSlant.Upright
) ?? SKTypeface.CreateDefault();
}
///
/// The default font mapper instance.
///
///
/// The default font mapper is used by any TextBlocks that don't
/// have an explicit font mapper set (see the property).
///
/// Replacing the default font mapper allows changing the font mapping
/// for all text blocks that don't have an explicit mapper assigned.
///
public static FontMapper Default = new FontMapper();
}
}