public TypefaceTextShaper(SKTypeface typeface) { Typeface = typeface ?? throw new ArgumentNullException(nameof(typeface)); int index; using (var blob = Typeface.OpenStream(out index).ToHarfBuzzBlob()) using (var face = new Face(blob, index)) { face.Index = index; face.UnitsPerEm = Typeface.UnitsPerEm; font = new HarfBuzzSharp.Font(face); font.SetScale(FONT_SIZE_SCALE, FONT_SIZE_SCALE); font.SetFunctionsOpenType(); } }
/// <summary> /// Constructs a new TextShaper /// </summary> /// <param name="typeface">The typeface of this shaper</param> private TextShaper(SKTypeface typeface) { // Load the typeface stream to a HarfBuzz font int index; using (var blob = typeface.OpenStream(out index).ToHarfBuzzBlob()) using (var face = new Face(blob, (uint)index)) { face.UnitsPerEm = (uint)typeface.UnitsPerEm; _font = new HarfBuzzSharp.Font(face); _font.SetScale(overScale, overScale); _font.SetFunctionsOpenType(); } // Get font metrics for this typeface using (var paint = new SKPaint()) { paint.Typeface = typeface; paint.TextSize = overScale; _fontMetrics = paint.FontMetrics; } }
/// <summary> /// Constructs a new TextShaper /// </summary> /// <param name="typeface">The typeface of this shaper</param> private TextShaper(SKTypeface typeface) { // Store typeface _typeface = typeface; // Load the typeface stream to a HarfBuzz font int index; using (var blob = GetHarfBuzzBlob(typeface.OpenStream(out index))) using (var face = new Face(blob, (uint)index)) { face.UnitsPerEm = typeface.UnitsPerEm; _font = new HarfBuzzSharp.Font(face); _font.SetScale(overScale, overScale); _font.SetFunctionsOpenType(); } // Get font metrics for this typeface using (var paint = new SKPaint()) { paint.Typeface = typeface; paint.TextSize = overScale; _fontMetrics = paint.FontMetrics; // This is a temporary hack until SkiaSharp exposes // a way to check if a font is fixed pitch. For now // we just measure and `i` and a `w` and see if they're // the same width. float[] widths = paint.GetGlyphWidths("iw", out var rects); _isFixedPitch = widths != null && widths.Length > 1 && widths[0] == widths[1]; if (_isFixedPitch) { _fixedCharacterWidth = widths[0]; } } }