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

发布于 2024-10-22 09:51:46 字数 1216 浏览 5 评论 0原文

我需要将 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);

但我收到编译器警告:警告:来自不兼容指针类型的赋值这位于我执行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.

我应该做什么?

I need to output a PDF to a file descriptor, and I was thinking of using Cairo and cairo_pdf_surface_create_for_stream():

I have a write function, that has this signature:

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

It returns CAIRO_STATUS_SUCCESS.

Now, how do I hand this function over as callback?

I tried this:

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

But I'm getting compiler warnings: warning: assignment from incompatible pointer type Which is on the line where I do wfunc = &cairowrite;.

What am I doing wrong?

The callback function gives this gcc error: cairo_status_t but has to be of type cairo_write_func_t.

With clang I get this error:

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.

What should I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

压抑⊿情绪 2024-10-29 09:51:46

您可以尝试使用示例定义函数指针

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

You could try to define the function pointer with

cairo_status_t (*wfunc)(FILE *, unsigned char *, unsigned int) = &cairowrite;

Example:

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

Output:

Result 8
冰雪梦之恋 2024-10-29 09:51:46

问题解决了!

我有函数定义:

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 不会给出任何错误。

Problem solved!

I have the function definition:

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

And this is how I pass the callback:

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

I don't use the intermediate function pointer anymore. clang and gcc don't give any errors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文