Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding fontmgr #233

Merged
merged 6 commits into from
Feb 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions binding/Binding/Binding.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<Compile Include="$(MSBuildThisFileDirectory)SKPictureRecorder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SKString.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SKTypeface.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SKFontManager.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SKStream.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SKManagedStream.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SKBitmap.cs" />
Expand Down
66 changes: 59 additions & 7 deletions binding/Binding/SKCanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,18 @@ public void DrawText (string text, float x, float y, SKPaint paint)
if (paint == null)
throw new ArgumentNullException (nameof (paint));

var bytes = Util.GetEncodedText (text, paint.TextEncoding);
SkiaApi.sk_canvas_draw_text (Handle, bytes, bytes.Length, x, y, paint.Handle);
var bytes = StringUtilities.GetEncodedText (text, paint.TextEncoding);
DrawText (bytes, x, y, paint);
}

public void DrawText (byte[] text, float x, float y, SKPaint paint)
{
if (text == null)
throw new ArgumentNullException (nameof (text));
if (paint == null)
throw new ArgumentNullException (nameof (paint));

SkiaApi.sk_canvas_draw_text (Handle, text, text.Length, x, y, paint.Handle);
}

[Obsolete ("Use DrawPositionedText instead.")]
Expand All @@ -400,11 +410,29 @@ public void DrawPositionedText (string text, SKPoint [] points, SKPaint paint)
if (points == null)
throw new ArgumentNullException (nameof (points));

var bytes = Util.GetEncodedText (text, paint.TextEncoding);
SkiaApi.sk_canvas_draw_pos_text (Handle, bytes, bytes.Length, points, paint.Handle);
var bytes = StringUtilities.GetEncodedText (text, paint.TextEncoding);
DrawPositionedText (bytes, points, paint);
}

public void DrawPositionedText (byte[] text, SKPoint [] points, SKPaint paint)
{
if (text == null)
throw new ArgumentNullException (nameof (text));
if (paint == null)
throw new ArgumentNullException (nameof (paint));
if (points == null)
throw new ArgumentNullException (nameof (points));

SkiaApi.sk_canvas_draw_pos_text (Handle, text, text.Length, points, paint.Handle);
}

[Obsolete ("Use DrawTextOnPath instead.")]
public void DrawText (IntPtr buffer, int length, SKPath path, float hOffset, float vOffset, SKPaint paint)
{
DrawTextOnPath (buffer, length, path, hOffset, vOffset, paint);
}

public void DrawTextOnPath (IntPtr buffer, int length, SKPath path, float hOffset, float vOffset, SKPaint paint)
{
if (buffer == IntPtr.Zero)
throw new ArgumentNullException (nameof (buffer));
Expand Down Expand Up @@ -444,7 +472,32 @@ public void DrawPositionedText (IntPtr buffer, int length, SKPoint[] points, SKP
SkiaApi.sk_canvas_draw_pos_text (Handle, buffer, length, points, paint.Handle);
}

[Obsolete ("Use DrawTextOnPath instead.")]
public void DrawText (string text, SKPath path, float hOffset, float vOffset, SKPaint paint)
{
DrawTextOnPath (text, path, hOffset, vOffset, paint);
}

public void DrawTextOnPath (string text, SKPath path, float hOffset, float vOffset, SKPaint paint)
{
if (text == null)
throw new ArgumentNullException (nameof (text));
if (paint == null)
throw new ArgumentNullException (nameof (paint));
if (paint == null)
throw new ArgumentNullException (nameof (paint));

var bytes = StringUtilities.GetEncodedText (text, paint.TextEncoding);
DrawTextOnPath (bytes, path, hOffset, vOffset, paint);
}

[Obsolete ("Use DrawTextOnPath instead.")]
public void DrawText (byte[] text, SKPath path, float hOffset, float vOffset, SKPaint paint)
{
DrawTextOnPath (text, path, hOffset, vOffset, paint);
}

public void DrawTextOnPath (byte[] text, SKPath path, float hOffset, float vOffset, SKPaint paint)
{
if (text == null)
throw new ArgumentNullException (nameof (text));
Expand All @@ -453,8 +506,7 @@ public void DrawText (string text, SKPath path, float hOffset, float vOffset, SK
if (paint == null)
throw new ArgumentNullException (nameof (paint));

var bytes = Util.GetEncodedText (text, paint.TextEncoding);
SkiaApi.sk_canvas_draw_text_on_path (Handle, bytes, bytes.Length, path.Handle, hOffset, vOffset, paint.Handle);
SkiaApi.sk_canvas_draw_text_on_path (Handle, text, text.Length, path.Handle, hOffset, vOffset, paint.Handle);
}

public void Flush ()
Expand All @@ -464,7 +516,7 @@ public void Flush ()

public void DrawAnnotation (SKRect rect, string key, SKData value)
{
SkiaApi.sk_canvas_draw_annotation (Handle, ref rect, Util.GetEncodedText (key, SKTextEncoding.Utf8), value == null ? IntPtr.Zero : value.Handle);
SkiaApi.sk_canvas_draw_annotation (Handle, ref rect, StringUtilities.GetEncodedText (key, SKTextEncoding.Utf8), value == null ? IntPtr.Zero : value.Handle);
}

public void DrawUrlAnnotation (SKRect rect, SKData value)
Expand Down
105 changes: 105 additions & 0 deletions binding/Binding/SKFontManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
//
// Bindings for SKTypeface
//
// Author:
// Miguel de Icaza
//
// Copyright 2016 Xamarin Inc
//
using System;
using System.Runtime.InteropServices;

namespace SkiaSharp
{
public class SKFontManager : SKObject
{
[Preserve]
internal SKFontManager (IntPtr handle, bool owns)
: base (handle, owns)
{
}

protected override void Dispose (bool disposing)
{
if (Handle != IntPtr.Zero && OwnsHandle) {
SkiaApi.sk_fontmgr_unref (Handle);
}

base.Dispose (disposing);
}

public static SKFontManager Default => GetObject<SKFontManager> (SkiaApi.sk_fontmgr_ref_default ());

public int FontFamilyCount => SkiaApi.sk_fontmgr_count_families (Handle);

public string GetFamilyName (int index)
{
using (var str = new SKString ()) {
SkiaApi.sk_fontmgr_get_family_name (Handle, index, str.Handle);
return (string)str;
}
}

public string[] GetFontFamilies ()
{
var count = FontFamilyCount;
var families = new string [count];
for (int i = 0; i < count; i++) {
families [i] = GetFamilyName (i);
}
return families;
}

public SKTypeface MatchCharacter (char character)
{
return MatchCharacter ((int)character);
}

public SKTypeface MatchCharacter (int character)
{
return MatchCharacter (null, character);
}

public SKTypeface MatchCharacter (string familyName, char character)
{
return MatchCharacter (familyName, (int)character);
}

public SKTypeface MatchCharacter (string familyName, int character)
{
return MatchCharacter (familyName, null, character);
}

public SKTypeface MatchCharacter (string familyName, string[] bcp47, char character)
{
return MatchCharacter (familyName, bcp47, (int)character);
}

public SKTypeface MatchCharacter (string familyName, string[] bcp47, int character)
{
return MatchCharacter (familyName, SKFontStyleWeight.Normal, SKFontStyleWidth.Normal, SKFontStyleSlant.Upright, bcp47, character);
}

public SKTypeface MatchCharacter (string familyName, SKFontStyleWeight weight, SKFontStyleWidth width, SKFontStyleSlant slant, string[] bcp47, char character)
{
return MatchCharacter (familyName, weight, width, slant, bcp47, (int)character);
}

public SKTypeface MatchCharacter (string familyName, SKFontStyleWeight weight, SKFontStyleWidth width, SKFontStyleSlant slant, string[] bcp47, int character)
{
return MatchCharacter (familyName, (int)weight, (int)width, slant, bcp47, character);
}

public SKTypeface MatchCharacter (string familyName, int weight, int width, SKFontStyleSlant slant, string[] bcp47, int character)
{
// TODO: work around for https://bugs.chromium.org/p/skia/issues/detail?id=6196
if (familyName == null)
{
familyName = string.Empty;
}

return GetObject<SKTypeface> (SkiaApi.sk_fontmgr_match_family_style_character (Handle, familyName, weight, width, slant, bcp47, bcp47?.Length ?? 0, character));
}
}
}

74 changes: 55 additions & 19 deletions binding/Binding/SKPaint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,16 @@ public float MeasureText (string text)
if (text == null)
throw new ArgumentNullException (nameof (text));

var bytes = Util.GetEncodedText (text, TextEncoding);
return SkiaApi.sk_paint_measure_text (Handle, bytes, (IntPtr) bytes.Length, IntPtr.Zero);
var bytes = StringUtilities.GetEncodedText (text, TextEncoding);
return MeasureText (bytes);
}

public float MeasureText (byte[] text)
{
if (text == null)
throw new ArgumentNullException (nameof (text));

return SkiaApi.sk_paint_measure_text (Handle, text, (IntPtr)text.Length, IntPtr.Zero);
}

public float MeasureText (IntPtr buffer, IntPtr length)
Expand All @@ -367,8 +375,16 @@ public float MeasureText (string text, ref SKRect bounds)
if (text == null)
throw new ArgumentNullException (nameof (text));

var bytes = Util.GetEncodedText (text, TextEncoding);
return SkiaApi.sk_paint_measure_text(Handle, bytes, (IntPtr) bytes.Length, ref bounds);
var bytes = StringUtilities.GetEncodedText (text, TextEncoding);
return MeasureText (bytes, ref bounds);
}

public float MeasureText (byte[] text, ref SKRect bounds)
{
if (text == null)
throw new ArgumentNullException (nameof (text));

return SkiaApi.sk_paint_measure_text (Handle, text, (IntPtr)text.Length, ref bounds);
}

public float MeasureText (IntPtr buffer, IntPtr length, ref SKRect bounds)
Expand All @@ -389,48 +405,68 @@ public long BreakText (string text, float maxWidth, out float measuredWidth)
{
if (text == null)
throw new ArgumentNullException (nameof (text));
var bytes = Util.GetEncodedText (text, TextEncoding);
var bytes = StringUtilities.GetEncodedText (text, TextEncoding);
return (long) SkiaApi.sk_paint_break_text (Handle, bytes, (IntPtr) bytes.Length, maxWidth, out measuredWidth);
}

public long BreakText (byte[] text, float maxWidth, out float measuredWidth)
{
if (text == null)
throw new ArgumentNullException (nameof (text));
return (long) SkiaApi.sk_paint_break_text (Handle, text, (IntPtr) text.Length, maxWidth, out measuredWidth);
}

public long BreakText (IntPtr buffer, IntPtr length, float maxWidth, out float measuredWidth)
{
if (buffer == IntPtr.Zero)
throw new ArgumentNullException (nameof (buffer));

return (long)SkiaApi.sk_paint_break_text (Handle, buffer, length, maxWidth, out measuredWidth);
return (long) SkiaApi.sk_paint_break_text (Handle, buffer, length, maxWidth, out measuredWidth);
}

public SKPath GetTextPath(string text, float x, float y)
public SKPath GetTextPath (string text, float x, float y)
{
if (text == null)
throw new ArgumentNullException(nameof(text));
var bytes = Util.GetEncodedText (text, TextEncoding);
return GetObject<SKPath>(SkiaApi.sk_paint_get_text_path(Handle, bytes, (IntPtr) bytes.Length, x, y));
throw new ArgumentNullException (nameof (text));
var bytes = StringUtilities.GetEncodedText (text, TextEncoding);
return GetTextPath (bytes, x, y);
}

public SKPath GetTextPath (byte[] text, float x, float y)
{
if (text == null)
throw new ArgumentNullException (nameof (text));
return GetObject<SKPath> (SkiaApi.sk_paint_get_text_path(Handle, text, (IntPtr) text.Length, x, y));
}

public SKPath GetTextPath(IntPtr buffer, IntPtr length, float x, float y)
public SKPath GetTextPath (IntPtr buffer, IntPtr length, float x, float y)
{
if (buffer == IntPtr.Zero)
throw new ArgumentNullException(nameof(buffer));
return GetObject<SKPath>(SkiaApi.sk_paint_get_text_path(Handle, buffer, length, x, y));
throw new ArgumentNullException (nameof (buffer));
return GetObject<SKPath> (SkiaApi.sk_paint_get_text_path (Handle, buffer, length, x, y));

}

public SKPath GetTextPath(string text, SKPoint[] points)
public SKPath GetTextPath (string text, SKPoint[] points)
{
if (text == null)
throw new ArgumentNullException(nameof(text));
var bytes = Util.GetEncodedText (text, TextEncoding);
return GetObject<SKPath>(SkiaApi.sk_paint_get_pos_text_path(Handle, bytes, (IntPtr) bytes.Length, points));
var bytes = StringUtilities.GetEncodedText (text, TextEncoding);
return GetObject<SKPath> (SkiaApi.sk_paint_get_pos_text_path (Handle, bytes, (IntPtr) bytes.Length, points));
}

public SKPath GetTextPath(IntPtr buffer, IntPtr length, SKPoint[] points)
public SKPath GetTextPath(byte[] text, SKPoint[] points)
{
if (text == null)
throw new ArgumentNullException (nameof (text));
return GetObject<SKPath> (SkiaApi.sk_paint_get_pos_text_path (Handle, text, (IntPtr) text.Length, points));
}

public SKPath GetTextPath (IntPtr buffer, IntPtr length, SKPoint[] points)
{
if (buffer == IntPtr.Zero)
throw new ArgumentNullException(nameof(buffer));
return GetObject<SKPath>(SkiaApi.sk_paint_get_pos_text_path(Handle, buffer, length, points));
throw new ArgumentNullException (nameof (buffer));
return GetObject<SKPath> (SkiaApi.sk_paint_get_pos_text_path (Handle, buffer, length, points));
}

public bool GetFillPath(SKPath src, SKPath dst, SKRect cullRect, float resScale = 1)
Expand Down
4 changes: 2 additions & 2 deletions binding/Binding/SKString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ public SKString (byte [] src)
}

public SKString (string str)
: this (Util.GetEncodedText (str, SKTextEncoding.Utf8))
: this (StringUtilities.GetEncodedText (str, SKTextEncoding.Utf8))
{
}

public override string ToString ()
{
var cstr = SkiaApi.sk_string_get_c_str (Handle);
var clen = SkiaApi.sk_string_get_size (Handle);
return Util.GetString (cstr, (int)clen, SKTextEncoding.Utf8);
return StringUtilities.GetString (cstr, (int)clen, SKTextEncoding.Utf8);
}

public static explicit operator string (SKString skString)
Expand Down
13 changes: 13 additions & 0 deletions binding/Binding/SkiaApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using sk_string_t = System.IntPtr;
using sk_picture_recorder_t = System.IntPtr;
using sk_typeface_t = System.IntPtr;
using sk_fontmgr_t = System.IntPtr;
using sk_font_table_tag_t = System.UInt32;
using sk_stream_t = System.IntPtr;
using sk_stream_filestream_t = System.IntPtr;
Expand Down Expand Up @@ -885,6 +886,18 @@ internal static class SkiaApi
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
public extern static SKTypefaceStyle sk_typeface_get_style(sk_typeface_t typeface);

// FontMgr
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
public extern static sk_fontmgr_t sk_fontmgr_ref_default();
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
public extern static void sk_fontmgr_unref(sk_fontmgr_t fontmgr);
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
public extern static int sk_fontmgr_count_families(sk_fontmgr_t fontmgr);
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
public extern static void sk_fontmgr_get_family_name(sk_fontmgr_t fontmgr, int index, sk_string_t familyName);
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
public extern static sk_typeface_t sk_fontmgr_match_family_style_character(sk_fontmgr_t fontmgr, [MarshalAs(UnmanagedType.LPStr)] string familyName, int weight, int width, SKFontStyleSlant slant, [In] [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPStr)] string[] bcp47, int bcp47Count, int character);

// Streams
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
public extern static void sk_memorystream_destroy(sk_stream_memorystream_t stream);
Expand Down
Loading