如何为cairo_pdf_surface_create_for_stream()创建正确的回调?

时间:2011-03-16 05:43:52

标签: c pdf-generation cairo

我需要将PDF输出到文件描述符,我正在考虑使用Cairo和cairo_pdf_surface_create_for_stream()

我有一个写函数,它有这个签名:

static cairo_status_t cairowrite(void *fp, unsigned char const *data, unsigned int length);

返回CAIRO_STATUS_SUCCESS

现在,如何将此功能作为回调处理?

我试过了:

cairo_write_func_t (*wfunc)(void *, unsigned char const *, unsigned int);
wfunc = (cairo_write_func_t)&cairowrite;
surface = cairo_pdf_surface_create_for_stream((cairo_write_func_t)wfunc, fp, (double)realwidth, (double)realwidth);

但是我收到了编译器警告:warning: assignment from incompatible pointer type这就是我所做的wfunc = &cairowrite;

我做错了什么?

回调函数提供此gcc错误:cairo_status_t但必须是cairo_write_func_t类型。

使用clang我收到此错误:

qrencode.c:250:11: warning: incompatible pointer types assigning to 'cairo_write_func_t (*)(void *, unsigned char const *, unsigned int)' from
      'cairo_write_func_t' (aka 'cairo_status_t (*)(void *, unsigned char const *, unsigned int)')
    wfunc = (cairo_write_func_t)&cairowrite;
          ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 warning generated.

我该怎么办?

2 个答案:

答案 0 :(得分:0)

您可以尝试使用

定义函数指针
cairo_status_t (*wfunc)(FILE *, unsigned char *, unsigned int) = &cairowrite;

示例:

#include <iostream>

int funp(int a, int b) {
  return a + b;
}

int call(int a, int b, int (*ptr)(int, int)) {
  int result = ptr(a, b);
  return result;
}

int main() {
  int (*ptr)(int, int) = &funp;
  int r = call(3, 5, ptr);
  std::cout << "Result " << r << std::endl;
  return 0;
}

输出:

Result 8

答案 1 :(得分:0)

问题解决了!

我有函数定义:

static cairo_status_t cairowrite(void *fh, unsigned char const *data, unsigned int length);

这就是我通过回调的方式:

surface = cairo_pdf_surface_create_for_stream((cairo_write_func_t)cairowrite, fp, (double)realwidth, (double)realwidth);

我不再使用中间函数指针了。 clang和gcc不会给出任何错误。