cairo draw chinese text on windows

來源:互聯網
上載者:User
/* * 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_VERstatic 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#endifint 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;}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.