[cairo] 回复: 回复: 回复: How to show chinese text using cairo

快乐2013 350137278 at qq.com
Mon May 6 23:16:15 PDT 2013


Thanks you all, and I implement drawing chinese text as below:
================================
#include <windows.h>

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;
}

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;
}
================================


------------------ 原始邮件 ------------------
发件人: "快乐2013"<350137278 at qq.com>;
发送时间: 2013年5月6日(星期一) 晚上9:43
收件人: "Simon Sapin"<simon.sapin at exyr.org>; "cairo"<cairo at cairographics.org>; 

主题: [cairo] 回复:  回复:  How to show chinese text using cairo



thanks for your advise. but I fail to compile pango on win7.
can freetype do it job?
pango with GTK+ is so torrible on win7. 




------------------ 原始邮件 ------------------
发件人: "Simon Sapin"<simon.sapin at exyr.org>;
发送时间: 2013年5月6日(星期一) 晚上9:37
收件人: "cairo"<cairo at cairographics.org>; 

主题: Re: [cairo] 回复:  How to show chinese text using cairo



Le 06/05/2013 15:13, 快乐2013 a écrit :
> Hi, what I have my code as below:
>
>     cairo_select_font_face (cr, "Arial Unicode MS",
> 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, "Hello, 中国人  "); // this clause cannot work
>
> I wonder if the following can work at all.
>
>      // does this clause can work?
>      cairo_show_text (cr, toUtf8("Hello, 中国人  "));
>
> here toUtf8 is a functioin doing convertion from ansi to utf8

Hi,

I don’t know what is the character encoding of string literals on your 
system (perhaps just the encoding your editor is saving as), but yes, 
cairo_show_text does support Unicode and expects a NUL-terminated UTF-8 
string.

http://cairographics.org/manual/cairo-text.html#cairo-show-text

But this is a the "toy" text API, you might get better results with Pango.

Cheers,
-- 
Simon Sapin
-- 
cairo mailing list
cairo at cairographics.org
http://lists.cairographics.org/mailman/listinfo/cairo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cairographics.org/archives/cairo/attachments/20130507/ea35c2e8/attachment.html>


More information about the cairo mailing list