public MyShaper(SKTypeface typeface) { if (typeface == null) { throw new ArgumentNullException(nameof(typeface)); } ; Typeface = typeface; int index; using (var blob = Typeface.OpenStream(out index).ToHarfBuzzBlob()) using (var face = new Face(blob, (uint)index)) { face.Index = (uint)index; face.UnitsPerEm = (uint)Typeface.UnitsPerEm; font = new Font(face); font.SetScale(FONT_SIZE_SCALE, FONT_SIZE_SCALE); #if __MAC__ font.SetFunctionsOpenType(); #endif } buffer = new HarfBuzzSharp.Buffer(); }
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(); } }
private void LoadedDocument_SubstituteFont(object sender, PdfFontEventArgs args) { //Get the font name string fontName = args.FontName.Split(',')[0]; //Get the font style PdfFontStyle fontStyle = args.FontStyle; SKTypefaceStyle sKFontStyle = SKTypefaceStyle.Normal; if (fontStyle != PdfFontStyle.Regular) { if (fontStyle == PdfFontStyle.Bold) { sKFontStyle = SKTypefaceStyle.Bold; } else if (fontStyle == PdfFontStyle.Italic) { sKFontStyle = SKTypefaceStyle.Italic; } else if (fontStyle == (PdfFontStyle.Italic | PdfFontStyle.Bold)) { sKFontStyle = SKTypefaceStyle.BoldItalic; } } SKTypeface typeface = SKTypeface.FromFamilyName(fontName, sKFontStyle); SKStreamAsset typeFaceStream = typeface.OpenStream(); MemoryStream memoryStream = null; if (typeFaceStream != null && typeFaceStream.Length > 0) { //Create fontData from type face stream. byte[] fontData = new byte[typeFaceStream.Length - 1]; typeFaceStream.Read(fontData, typeFaceStream.Length); typeFaceStream.Dispose(); //Create the new memory stream from font data. memoryStream = new MemoryStream(fontData); } //set the font stream to the event args. args.FontStream = memoryStream; }
/// <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]; } } }