cairo: Fix text API calls

The native functions require a NULL-terminated string of text encoded in
UTF-8.

This change is merged from Mono.Cairo in mono 3.2.
This commit is contained in:
Wolfgang Steffens 2013-07-26 16:47:51 -05:00 committed by Bertrand Lorentz
parent 21ce761271
commit 796cd1e8a1
2 changed files with 44 additions and 7 deletions

View File

@ -36,6 +36,7 @@
using System;
using System.Runtime.InteropServices;
using System.Text;
using Cairo;
namespace Cairo {
@ -833,20 +834,56 @@ namespace Cairo {
NativeMethods.cairo_show_page (handle);
}
private static byte[] TerminateUtf8(byte[] utf8)
{
if (utf8.Length > 0 && utf8[utf8.Length - 1] == 0)
return utf8;
var termedArray = new byte[utf8.Length + 1];
Array.Copy(utf8, termedArray, utf8.Length);
termedArray[utf8.Length] = 0;
return termedArray;
}
private static byte[] TerminateUtf8(string s)
{
// compute the byte count including the trailing \0
var byteCount = Encoding.UTF8.GetMaxByteCount(s.Length + 1);
var bytes = new byte[byteCount];
Encoding.UTF8.GetBytes(s, 0, s.Length, bytes, 0);
return bytes;
}
public void ShowText (string str)
{
NativeMethods.cairo_show_text (handle, str);
NativeMethods.cairo_show_text (handle, TerminateUtf8 (str));
}
public void ShowText (byte[] utf8)
{
NativeMethods.cairo_show_text (handle, TerminateUtf8 (utf8));
}
public void TextPath (string str)
{
NativeMethods.cairo_text_path (handle, str);
NativeMethods.cairo_text_path (handle, TerminateUtf8 (str));
}
public TextExtents TextExtents (string utf8)
public void TextPath (byte[] utf8)
{
NativeMethods.cairo_text_path (handle, TerminateUtf8 (utf8));
}
public TextExtents TextExtents (string s)
{
TextExtents extents;
NativeMethods.cairo_text_extents (handle, utf8, out extents);
NativeMethods.cairo_text_extents (handle, TerminateUtf8 (s), out extents);
return extents;
}
public TextExtents TextExtents(byte[] utf8)
{
TextExtents extents;
NativeMethods.cairo_text_extents (handle, TerminateUtf8 (utf8), out extents);
return extents;
}

View File

@ -821,7 +821,7 @@ namespace Cairo
internal static extern void cairo_show_page (IntPtr cr);
[DllImport (cairo, CallingConvention=CallingConvention.Cdecl)]
internal static extern void cairo_show_text (IntPtr cr, string utf8);
internal static extern void cairo_show_text (IntPtr cr, byte[] utf8);
//[DllImport (cairo, CallingConvention=CallingConvention.Cdecl)]
//internal static extern void cairo_show_text_glyphs (IntPtr cr, IntPtr utf8, int utf8_len, IntPtr glyphs, int num_glyphs, IntPtr clusters, int num_clusters, ClusterFlags cluster_flags);
@ -944,10 +944,10 @@ namespace Cairo
//internal static extern void cairo_text_cluster_free (IntPtr clusters);
[DllImport (cairo, CallingConvention=CallingConvention.Cdecl)]
internal static extern void cairo_text_extents (IntPtr cr, string utf8, out TextExtents extents);
internal static extern void cairo_text_extents (IntPtr cr, byte[] utf8, out TextExtents extents);
[DllImport (cairo, CallingConvention=CallingConvention.Cdecl)]
internal static extern void cairo_text_path (IntPtr ct, string utf8);
internal static extern void cairo_text_path (IntPtr ct, byte[] utf8);
//[DllImport (cairo, CallingConvention=CallingConvention.Cdecl)]
//internal static extern IntPtr cairo_toy_font_face_create (IntPtr face, FontSlant slant, FontWeight weight);