Graphviz 13.0.0~dev.20250210.0415
Loading...
Searching...
No Matches
gvdevice_kitty.c
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11#include "config.h"
12
13#include <assert.h>
14#include <stdbool.h>
15#include <stdio.h>
16#include <stdlib.h>
17
18#include <common/types.h>
19#include <gvc/gvio.h>
20#include <gvc/gvplugin_device.h>
21#include <util/alloc.h>
22#include <util/base64.h>
23#include <util/gv_math.h>
24
25#ifdef HAVE_LIBZ
26#include <zlib.h>
27#endif
28
29static void kitty_write(unsigned char *data, size_t data_size, unsigned width,
30 unsigned height, bool is_compressed) {
31 const size_t chunk_size = 4096;
32 char *output = gv_base64(data, data_size);
33 size_t offset = 0;
34 size_t size = gv_base64_size(data_size);
35
36 while (offset < size) {
37 int has_next_chunk = offset + chunk_size <= size;
38 if (offset == 0) {
39 printf("\033_Ga=T,f=32,s=%u,v=%u%s%s;", width, height,
40 chunk_size < size ? ",m=1" : "", is_compressed ? ",o=z" : "");
41 } else {
42 printf("\033_Gm=%d;", has_next_chunk);
43 }
44
45 size_t this_chunk_size = has_next_chunk ? chunk_size : size - offset;
46 fwrite(output + offset, this_chunk_size, 1, stdout);
47 printf("\033\\");
48 offset += chunk_size;
49 }
50 printf("\n");
51
52 free(output);
53}
54
55static void kitty_format(GVJ_t *job) {
56 unsigned char *imagedata = job->imagedata;
57 size_t imagedata_size = job->width * job->height * BYTES_PER_PIXEL;
58 argb2rgba(job->width, job->height, imagedata);
59
60 kitty_write(imagedata, imagedata_size, job->width, job->height, false);
61}
62
64 GVDEVICE_DOES_TRUECOLOR, /* flags */
65 {0., 0.}, /* default margin - points */
66 {0., 0.}, /* default page width, height - points */
67 {96., 96.}, /* dpi */
68};
69
71
72#ifdef HAVE_LIBZ
73static int zlib_compress(unsigned char *source, uLong source_len,
74 unsigned char **dest, size_t *dest_len) {
75 uLong dest_cap = compressBound(source_len);
76 *dest = gv_alloc(dest_cap);
77
78 const int ret = compress(*dest, &dest_cap, source, source_len);
79 *dest_len = dest_cap;
80 return ret;
81}
82
83static void zkitty_format(GVJ_t *job) {
84 unsigned char *imagedata = job->imagedata;
85 const uLong imagedata_size = job->width * job->height * BYTES_PER_PIXEL;
86 argb2rgba(job->width, job->height, imagedata);
87
88 unsigned char *zbuf;
89 size_t zsize;
90 int ret = zlib_compress(imagedata, imagedata_size, &zbuf, &zsize);
91 assert(ret == Z_OK);
92 (void)ret;
93
94 kitty_write(zbuf, zsize, job->width, job->height, true);
95
96 free(zbuf);
97}
98
99static gvdevice_features_t device_features_zkitty = {
100 GVDEVICE_DOES_TRUECOLOR, /* flags */
101 {0., 0.}, /* default margin - points */
102 {0., 0.}, /* default page width, height - points */
103 {96., 96.}, /* dpi */
104};
105
106static gvdevice_engine_t device_engine_zkitty = {.format = zkitty_format};
107#endif
108
110 {0, "kitty:cairo", 0, &device_engine_kitty, &device_features_kitty},
111#ifdef HAVE_LIBZ
112 {1, "kittyz:cairo", 1, &device_engine_zkitty, &device_features_zkitty},
113#endif
114 {0, NULL, 0, NULL, NULL}};
Memory allocation wrappers that exit on failure.
static void * gv_alloc(size_t size)
Definition alloc.h:47
char * gv_base64(const unsigned char *source, size_t size)
Definition base64.c:14
size_t gv_base64_size(size_t source_size)
Definition base64.c:12
Base64 encoding.
static double compress(info *nl, int nn)
Definition constraint.c:646
void free(void *)
node NULL
Definition grammar.y:163
Arithmetic helper functions.
@ BYTES_PER_PIXEL
Definition gv_math.h:87
static void argb2rgba(size_t width, size_t height, unsigned char *data)
Definition gv_math.h:105
swig_ptr_object_handlers offset
Definition gv_php.cpp:5907
#define GVDEVICE_DOES_TRUECOLOR
Definition gvcjob.h:90
static void kitty_format(GVJ_t *job)
static gvdevice_engine_t device_engine_kitty
gvplugin_installed_t gvdevice_types_kitty[]
static gvdevice_features_t device_features_kitty
static void kitty_write(unsigned char *data, size_t data_size, unsigned width, unsigned height, bool is_compressed)
unsigned char * imagedata
location of imagedata
Definition gvcjob.h:297
unsigned int width
Definition gvcjob.h:327
unsigned int height
Definition gvcjob.h:328
Definition legal.c:50
void(* format)(GVJ_t *firstjob)
ingroup plugin_api
Definition gvplugin.h:35
graphs, nodes and edges info: Agraphinfo_t, Agnodeinfo_t and Agedgeinfo_t