Skip to content

Instantly share code, notes, and snippets.

@stiltr
Created August 23, 2018 20:51
Show Gist options
  • Save stiltr/856a5fa5e58f83b812555722e1cee93c to your computer and use it in GitHub Desktop.
Save stiltr/856a5fa5e58f83b812555722e1cee93c to your computer and use it in GitHub Desktop.
// Cobbled together from code found at:
// https://www.cairographics.org/documentation/using_the_postscript_surface/
// http://www.cs.rpi.edu/~moorthy/Courses/os98/Pgms/client.c
// Don't forget to set the hostname in setup_socket()! (line 70)
// Build with:
// gcc simple_print.c -l cairo -o simple_print
// Can be tested with:
// netcat -l -p 9100 | less
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <cairo/cairo.h>
#include <cairo/cairo-ps.h>
#define PAGE_WIDTH 612
#define PAGE_HEIGHT 792
#define BORDER 50
void error(char *msg)
{
perror(msg);
exit(0);
}
void draw (cairo_t *cr, const char *text, int width, int height)
{
char buf[100];
cairo_rectangle (cr, BORDER, BORDER, width - 2*BORDER, height - 2*BORDER);
cairo_set_line_width (cr, 2);
cairo_stroke (cr);
cairo_select_font_face (cr, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
cairo_set_font_size (cr, 60);
cairo_move_to (cr, 200, height/3);
cairo_show_text (cr, text);
sprintf (buf, "Width: %d points Height: %d points", width, height);
cairo_set_font_size (cr, 18);
cairo_move_to (cr, 120, height*2/3);
cairo_show_text (cr, buf);
sprintf (buf, "This is a test! = D", width, height);
cairo_set_font_size (cr, 18);
cairo_move_to (cr, 150, height*5/6);
cairo_show_text (cr, buf);
}
int setup_socket()
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
portno = 9100;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname("myprinter");
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0 )
error("ERROR connecting");
return sockfd;
}
static cairo_status_t cairowrite(void *fh, unsigned char const *data, unsigned int length)
{
if (write(*(int*)fh,data,length) < 0)
error("ERROR writing to socket");
return CAIRO_STATUS_SUCCESS;
}
int main(int argc, char *argv[])
{
int fd = setup_socket();
cairo_surface_t *surface;
cairo_t *cr;
cairo_matrix_t matrix;
surface = cairo_ps_surface_create_for_stream((cairo_write_func_t)cairowrite, &fd, PAGE_WIDTH, PAGE_HEIGHT);
cr = cairo_create (surface);
/* Print portrait page */
cairo_ps_surface_dsc_begin_page_setup (surface);
cairo_ps_surface_dsc_comment (surface, "%%PageOrientation: Portrait");
draw (cr, "Portrait", PAGE_WIDTH, PAGE_HEIGHT);
cairo_surface_show_page (surface);
cairo_destroy (cr);
cairo_surface_finish (surface);
cairo_surface_destroy (surface);
close(fd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment