0
点赞
收藏
分享

微信扫一扫

cairo draw chinese text on windows


/*
* cheungmine
* 2013-5-5
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#ifdef _MSC_VER
#include <windows.h>
#pragma warning (disable : 4996)
#endif

/* cairo api */

#include "../lib/cairo/include/cairo.h"
#include "../lib/cairo/include/cairo-features.h"
#include "../lib/cairo/include/cairo-svg.h"
#include "../lib/cairo/include/cairo-pdf.h"
#include "../lib/cairo/include/cairo-ps.h"
#include "../lib/cairo/include/cairo-ft.h"
#include "../lib/cairo/include/cairo-version.h"
#include "../lib/cairo/include/cairo-win32.h"

#pragma comment (lib, "../../lib/cairo/lib/cairo.lib")

#ifdef _MSC_VER
static int bstr2utf8(BSTR bstrIn, int bstrLen, byte *pOut)
{
int cbSize;

if (!bstrIn) {
return 0;
}

cbSize = WideCharToMultiByte(CP_UTF8, 0, bstrIn, bstrLen, 0, 0, 0, 0);
if (pOut) {
cbSize = WideCharToMultiByte(CP_UTF8, 0, bstrIn, bstrLen, (LPSTR) pOut, cbSize, 0, 0);
assert(pOut[cbSize-1]==0);
}

return cbSize;
}

static int bytes2bstr(UINT uCodePage, byte* pBytesIn, DWORD cbSizeIn, BSTR *pBstrOut)
{
int cchNeeded;
LPSTR pstr;

*pBstrOut = 0;

cchNeeded = MultiByteToWideChar(uCodePage, 0, (LPCSTR)pBytesIn, cbSizeIn, 0, 0);
if (0 == cchNeeded) {
return (0);
}

pstr = (LPSTR) CoTaskMemAlloc (sizeof(DWORD) + sizeof(WCHAR)*cchNeeded);
if (!pstr) {
return (-1);
}

if (cchNeeded != MultiByteToWideChar(uCodePage, 0, (LPCSTR)pBytesIn, cbSizeIn,
(LPWSTR)(pstr+sizeof(DWORD)), cchNeeded)) {
CoTaskMemFree(pstr);
return (-2);
}

*((DWORD*)pstr) = sizeof(WCHAR)*(cchNeeded-1);
*pBstrOut = (BSTR)(pstr + sizeof(DWORD));

return cchNeeded;
}

char * ansi2utf8(char *str)
{
BSTR bstr = 0;
byte *utf8 = 0;
int len, cb;

len = bytes2bstr(CP_ACP, (byte*) str, strlen(str)+1, &bstr);
if (len > 0) {
cb = bstr2utf8(bstr, len, utf8);
if (cb > 0) {
utf8 = (byte*) malloc(cb);
bstr2utf8(bstr, len, utf8);
}

SysFreeString(bstr);
return (char*) utf8;
}

return 0;
}
#else

#endif

int drawChinaText(char *shapefile, char *shapeid, int width, int length, gde_drawsurface_format format)
{
cairo_surface_t *surface;
cairo_t *cr;

char *font;
char *text;

font = ansi2utf8("文泉驿等宽正黑"); //"Arial Unicode MS"
text = ansi2utf8("Hello, Chinese! 你好,中国人!");

surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, length);
cr = cairo_create (surface);


cairo_select_font_face (cr, font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size (cr, 32.0);
cairo_set_source_rgb (cr, 0.0, 0.0, 1.0);
cairo_move_to (cr, 10.0, 50.0);

cairo_show_text (cr, text);

cairo_destroy (cr);

cairo_surface_write_to_png (surface, "c:/temp/hello-cairo.png");
cairo_surface_destroy (surface);

free(font);
free(text);

return 0;
}



举报

相关推荐

0 条评论