The Cairo graphics tutorial -------Cairo backends

19 篇文章 0 订阅

The Cairo library supports various backends. In this section of the Cairo graphics tutorial, we will use Cairo to create a PNG image, PDF file, SVG file and we will draw on a GTK window.


PNG image

In the first example, we will create a PNG image.

#include <cairo.h>

int main (int argc, char *argv[])
{
  cairo_surface_t *surface;
  cairo_t *cr;

  surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 390, 60);
  cr = cairo_create(surface);

  cairo_set_source_rgb(cr, 0, 0, 0);
  cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL,
      CAIRO_FONT_WEIGHT_NORMAL);
  cairo_set_font_size(cr, 40.0);

  cairo_move_to(cr, 10.0, 50.0);
  cairo_show_text(cr, "Disziplin ist Macht.");

  cairo_surface_write_to_png(surface, "image.png");

  cairo_destroy(cr);
  cairo_surface_destroy(surface);

  return 0;
}

This example is a small console application, that will create a PNG image.

 #include <cairo.h>

In this header file, we will find declarations of our functions and constants.

 cairo_surface_t *surface;
 cairo_t *cr;

Here we declare a surface and a Cairo context.

 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 390, 60);
 cr = cairo_create(surface);

We create a surface and a Cairo context. The surface is an 390x60 px image.

 cairo_set_source_rgb(cr, 0, 0, 0);

We will draw in black ink.

 cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL,
      CAIRO_FONT_WEIGHT_NORMAL);
 cairo_set_font_size(cr, 40.0);

We choose a font type and set it's size.

 cairo_move_to(cr, 10.0, 50.0);
 cairo_show_text(cr, "Disziplin ist Macht.");

We move to a (10.0, 50.0) position within the image and draw the text.

 cairo_surface_write_to_png(surface, "image.png");

This function call creates the PNG image.

 cairo_destroy(cr);
 cairo_surface_destroy(surface);

In the end, we clean the resources.


PNG image
PNG image

PDF file

In the second example, we will use the Cairo library to create a simple PDF file.

#include <cairo/cairo.h>
#include <cairo/cairo-pdf.h>

int main() {

  cairo_surface_t *surface;
  cairo_t *cr;

  surface = cairo_pdf_surface_create("pdffile.pdf", 504, 648);
  cr = cairo_create(surface);

  cairo_set_source_rgb(cr, 0, 0, 0);
  cairo_select_font_face (cr, "Sans", CAIRO_FONT_SLANT_NORMAL,
      CAIRO_FONT_WEIGHT_NORMAL);
  cairo_set_font_size (cr, 40.0);

  cairo_move_to(cr, 10.0, 50.0);
  cairo_show_text(cr, "Disziplin ist Macht.");

  cairo_show_page(cr);

  cairo_surface_destroy(surface);
  cairo_destroy(cr);

  return 0;
}

We must open the pdf file in a pdf viewer. Linux users can use KPDF or Evince viewers.

 surface = cairo_pdf_surface_create("pdffile.pdf", 504, 648);

To render a pdf file, we must create a pdf surface using the cairo_pdf_surface_create() funcion call.The size of the pdf file is specified in points, which is a standard in typesetting.

 cairo_show_page(cr);

The cairo_show_page() finishes rendering of the pdf file.


PDF file in Evince
PDF file in Evince

SVG file

The next example creates a simple SVG (Scalable Vector Graphics) file. The SVG is one of the hottest technologies these days.

#include <cairo.h>
#include <cairo-svg.h> 
 
int main() {

  cairo_surface_t *surface;
  cairo_t *cr;

  surface = cairo_svg_surface_create("svgfile.svg", 390, 60);
  cr = cairo_create(surface);

  cairo_set_source_rgb(cr, 0, 0, 0);
  cairo_select_font_face (cr, "Sans", CAIRO_FONT_SLANT_NORMAL,
      CAIRO_FONT_WEIGHT_NORMAL);
  cairo_set_font_size (cr, 40.0);

  cairo_move_to(cr, 10.0, 50.0);
  cairo_show_text(cr, "Disziplin ist Macht.");

  cairo_surface_destroy(surface);
  cairo_destroy(cr);

  return 0;
}

We can use Firefox, Opera or Inkscape programs to open the svgfile.svg file.

 surface = cairo_svg_surface_create("svgfile.svg", 390, 60);

To create a SVG file in Cairo, we must create a svg surface using the cairo_svg_surface_create() function call.

The Rest of the code is identical to the previous examples.


SVG file in Opera
SVG file in Opera

GTK Window

In the last example, we will draw on the GTK window. This backend will be used throughout the rest of the tutorial

#include <cairo.h>
#include <gtk/gtk.h>

static gboolean
on_expose_event(GtkWidget      *widget,
                  GdkEventExpose *event,
                  gpointer        data)
{
  cairo_t *cr;

  cr = gdk_cairo_create(widget->window);

  cairo_set_source_rgb(cr, 0, 0, 0);
  cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL,
      CAIRO_FONT_WEIGHT_NORMAL);
  cairo_set_font_size(cr, 40.0);

  cairo_move_to(cr, 10.0, 50.0);
  cairo_show_text(cr, "Disziplin ist Macht.");

  cairo_destroy(cr);

  return FALSE;
}


int
main (int argc, char *argv[])
{

  GtkWidget *window;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  g_signal_connect(window, "expose-event",
                    G_CALLBACK (on_expose_event), NULL);
  g_signal_connect(window, "destroy",
                    G_CALLBACK (gtk_main_quit), NULL);

  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_window_set_default_size(GTK_WINDOW(window), 400, 90); 
  gtk_widget_set_app_paintable(window, TRUE);

  gtk_widget_show_all(window);

  gtk_main();

  return 0;
}

The example pops up a centered GTK+ window, on which we draw the "Disziplin ist Macht" text.

#include <cairo.h>
#include <gtk/gtk.h>

We include the necessary Cairo and GTK headers.

 g_signal_connect(window, "expose-event",
     G_CALLBACK(on_expose_event), NULL);

When the window is redrawn, an expose-event signal is emitted. We connect that signal to theon_expose_event() callback.

y gtk, part of a toolkits responsiveness is to redraw exposed regions whichfor normal windows will just 'clear' the background. if you want to drawyou own things in such a window, you need to inform gtk about that with thegtk_widget_set_app_paintable () function.
 gtk_widget_set_app_paintable(window, TRUE)

If we want to draw in GTK+, we can draw on a GtkDrawingArea widget or on a simple GtkWindow widget. I chose to draw on the latter. In order to draw on the GtkWindow widget, we must inform the GTK+ by calling the gtk_widget_set_app_paintable() call. The function call will suppress the default themed drawing of the widget's background. In case of the GtkDrawingArea, we do not need to call the function.

 cairo_t *cr;

 cr = gdk_cairo_create(widget->window);

The drawing is done inside the on_expose_event() callback function. We create a Cairo context for the GTK+ system and draw our text as usual on that context.


GTK window
GTK window

In this chapter we have covered supported Cairo backends.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。
Cairo是一个功能强大的2D图形库,它提供了各种绘图功能,包括图形渲染、路径绘制、文字绘制等。Cairo在许多Linux发行版中被广泛使用,并且它还支持多种操作系统,包括Windows和Mac OS。 cups-libs是CUPS(通用UNIX打印系统)的库文件。CUPS是一个开源的打印系统,它为用户提供了管理和控制打印机的功能。cups-libs包含了CUPS的核心库文件,这些库文件提供了与打印机进行通信的接口,以及一些用于配置和管理打印队列的工具。 libsm是一个X Window系统库,它为X客户端程序提供了会话管理功能。libsm提供了一种机制,使得程序可以在同一会话中相互通信,并且可以共享资源。libsm在安装和运行许多X Window系统的程序时都是必需的。 rpm是Red Hat Package Manager的简称,它是一种在许多Linux发行版中使用的软件包管理工具。rpm可以用来安装、卸载、查询和更新软件包。通过rpm下载软件包,可以方便地获取需要的软件包,并且可以在系统中进行安装和管理。 总结起来,cairo、cups-libs、libsm和rpm都是在Linux系统中使用的重要软件和库文件。Cairo提供了强大的图形绘制功能,cups-libs提供了打印机管理功能,libsm提供了X Window系统的会话管理功能,而rpm是一种常用的软件包管理工具。通过下载这些软件和库文件,我们可以获得更好的图形和打印功能,并且能够更方便地管理软件包。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值