/// <summary> /// Maps a given style to a specific typeface /// </summary> /// <param name="style">The style to be mapped</param> /// <param name="ignoreFontVariants">Indicates the mapping should ignore font variants (use to get font for ellipsis)</param> /// <returns>A mapped typeface</returns> 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), 0, style.FontItalic ? SKFontStyleSlant.Italic : SKFontStyleSlant.Upright ) ?? SKTypeface.CreateDefault()); }
static Font() { //TODO:编译条件确认是否使用默认中文字体 var typeface = SKFontManager.Default.MatchCharacter('中'); if (typeface == null) { //使用默认Noto字体 typeface = SKFontManager.Default.CreateTypeface(NotoFont); if (typeface == null) { typeface = SKTypeface.CreateDefault(); } else { DefaultFontIsNoto = true; } } DefaultFontFamilyName = typeface.FamilyName; typeface.Dispose(); }
private static string CreateDefaultFamilyName() { var defaultTypeface = SKTypeface.CreateDefault(); return(defaultTypeface.FamilyName); }
private SKBitmap BuildThumbCollageBitmap(string[] paths, int width, int height, string?libraryName) { var bitmap = new SKBitmap(width, height); using var canvas = new SKCanvas(bitmap); canvas.Clear(SKColors.Black); using var backdrop = GetNextValidImage(paths, 0, out _); if (backdrop == null) { return(bitmap); } // resize to the same aspect as the original var backdropHeight = Math.Abs(width * backdrop.Height / backdrop.Width); using var residedBackdrop = SkiaEncoder.ResizeImage(backdrop, new SKImageInfo(width, backdropHeight, backdrop.ColorType, backdrop.AlphaType, backdrop.ColorSpace)); // draw the backdrop canvas.DrawImage(residedBackdrop, 0, 0); // draw shadow rectangle var paintColor = new SKPaint { Color = SKColors.Black.WithAlpha(0x78), Style = SKPaintStyle.Fill }; canvas.DrawRect(0, 0, width, height, paintColor); var typeFace = SKTypeface.FromFamilyName("sans-serif", SKFontStyleWeight.Bold, SKFontStyleWidth.Normal, SKFontStyleSlant.Upright) ?? SKTypeface.CreateDefault(); // use the system fallback to find a typeface for the given CJK character var nonCjkPattern = @"[^\p{IsCJKUnifiedIdeographs}\p{IsCJKUnifiedIdeographsExtensionA}\p{IsKatakana}\p{IsHiragana}\p{IsHangulSyllables}\p{IsHangulJamo}]"; var filteredName = Regex.Replace(libraryName ?? string.Empty, nonCjkPattern, string.Empty); if (!string.IsNullOrEmpty(filteredName)) { typeFace = SKFontManager.Default.MatchCharacter(null, SKFontStyleWeight.Bold, SKFontStyleWidth.Normal, SKFontStyleSlant.Upright, null, filteredName[0]); } // draw library name var textPaint = new SKPaint { Color = SKColors.White, Style = SKPaintStyle.Fill, TextSize = 112, TextAlign = SKTextAlign.Center, Typeface = typeFace, IsAntialias = true }; // scale down text to 90% of the width if text is larger than 95% of the width var textWidth = textPaint.MeasureText(libraryName); if (textWidth > width * 0.95) { textPaint.TextSize = 0.9f * width * textPaint.TextSize / textWidth; } canvas.DrawText(libraryName, width / 2f, (height / 2f) + (textPaint.FontMetrics.XHeight / 2), textPaint); return(bitmap); }