blob: aa38823b426324e8018c8962b18bc30b250652f6 [file] [log] [blame]
/*
* Copyright © 2008 Chris Wilson <chris@chris-wilson.co.uk>
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
* You should have received a copy of the MPL along with this library
* in the file COPYING-MPL-1.1
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
* OF ANY KIND, either express or implied. See the LGPL or the MPL for
* the specific language governing rights and limitations.
*
* The Original Code is the cairo graphics library.
*
* The Initial Developer of the Original Code is Chris Wilson.
*
* Contributor(s):
* Chris Wilson <chris@chris-wilson.co.uk>
*/
#include "config.h"
/* TODO real path type */
#include "cairo-script-private.h"
#if CAIRO_HAS_SCRIPT_SURFACE
#include "cairo-script.h"
#endif
#include <stdio.h> /* snprintf */
#include <stdlib.h> /* mkstemp */
#include <string.h>
#ifdef _MSC_VER
#define _USE_MATH_DEFINES /* for M_LN2, M_PI and M_SQRT2 on win32 */
#define snprintf _snprintf
#endif
#include <math.h>
#include <limits.h> /* INT_MAX */
#include <assert.h>
#if HAVE_ZLIB
#include <zlib.h>
#endif
#if HAVE_LZO
#include <lzo/lzo2a.h>
#endif
#ifdef HAVE_MMAP
# ifdef HAVE_UNISTD_H
# include <sys/mman.h>
# include <unistd.h>
# else
# undef HAVE_MMAP
# endif
#endif
typedef struct _csi_proxy {
csi_t *ctx;
void *ptr;
csi_dictionary_t *dictionary;
csi_destroy_func_t destroy_func;
void *destroy_data;
} csi_proxy_t;
typedef struct _csi_blob {
csi_list_t list;
unsigned long hash;
uint8_t *bytes;
unsigned int len;
} csi_blob_t;
static const cairo_user_data_key_t _csi_proxy_key;
static const cairo_user_data_key_t _csi_blob_key;
enum mime_type {
MIME_TYPE_NONE = 0,
MIME_TYPE_PNG
};
#define check(CNT) do {\
if (_csi_unlikely (! _csi_check_ostack (ctx, (CNT)))) \
return _csi_error (CSI_STATUS_INVALID_SCRIPT); \
} while (0)
#define pop(CNT) _csi_pop_ostack (ctx, (CNT))
#define push(OBJ) _csi_push_ostack (ctx, (OBJ))
static csi_proxy_t *
_csi_proxy_create (csi_t *ctx,
void *ptr,
csi_dictionary_t *dictionary,
csi_destroy_func_t destroy_func,
void *destroy_data)
{
csi_proxy_t *proxy;
proxy = _csi_slab_alloc (ctx, sizeof (csi_proxy_t));
if (proxy == NULL)
return NULL;
proxy->ctx = cairo_script_interpreter_reference (ctx);
proxy->ptr = ptr;
proxy->destroy_func = destroy_func;
proxy->destroy_data = destroy_data;
proxy->dictionary = dictionary;
if (dictionary != NULL)
dictionary->base.ref++;
return proxy;
}
static void
_csi_proxy_destroy (void *closure)
{
csi_proxy_t *proxy = closure;
csi_t *ctx = proxy->ctx;
/* XXX this doesn't work because user_data_destroy is called too late.
* Considering another hook into the (cairo internal) object system.
*/
if (proxy->destroy_func != NULL)
proxy->destroy_func (proxy->destroy_data, proxy->ptr);
if (proxy->dictionary != NULL && --proxy->dictionary->base.ref == 0)
csi_dictionary_free (ctx, proxy->dictionary);
_csi_slab_free (ctx, proxy, sizeof (csi_proxy_t));
cairo_script_interpreter_destroy (ctx);
}
static void
_csi_blob_hash (csi_blob_t *blob, const uint32_t *data, int len)
{
unsigned long hash = blob->hash;
/* very simple! */
while (len--) {
unsigned long c = *data++;
hash *= 33;
hash ^= c;
}
blob->hash = hash;
}
static csi_boolean_t
_csi_blob_equal (const csi_list_t *link, void *data)
{
csi_blob_t *A, *B;
A = csi_container_of (link, csi_blob_t, list);
B = data;
if (A->len != B->len)
return FALSE;
if (A->hash != B->hash)
return FALSE;
return memcmp (A->bytes, B->bytes, A->len) == 0;
}
static void
_csi_blob_init (csi_blob_t *blob, uint8_t *bytes, int len)
{
blob->hash = 5381;
blob->len = len;
blob->bytes = bytes;
}
static csi_list_t *
_csi_list_unlink (csi_list_t *head, csi_list_t *link)
{
if (link->next != NULL)
link->next->prev = link->prev;
if (link->prev != NULL)
link->prev->next = link->next;
else
head = link->next;
return head;
}
static csi_list_t *
_csi_list_prepend (csi_list_t *head, csi_list_t *link)
{
if (head != NULL)
head->prev = link;
link->next = head;
link->prev = NULL;
return link;
}
static csi_list_t *
_csi_list_find (csi_list_t *head,
csi_boolean_t (*predicate) (const csi_list_t *link, void *data),
void *data)
{
while (head != NULL) {
if (predicate (head, data))
return head;
head = head->next;
}
return NULL;
}
static csi_status_t
_csi_ostack_get_boolean (csi_t *ctx, unsigned int i, csi_boolean_t *out)
{
csi_object_t *obj;
int type;
obj = _csi_peek_ostack (ctx, i);
type = csi_object_get_type (obj);
switch (type) {
case CSI_OBJECT_TYPE_BOOLEAN:
*out = obj->datum.boolean;
break;
case CSI_OBJECT_TYPE_INTEGER:
*out = !! obj->datum.integer;
break;
case CSI_OBJECT_TYPE_REAL:
*out = obj->datum.real != 0.;
break;
default:
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
}
return CSI_STATUS_SUCCESS;
}
static csi_status_t
_csi_ostack_get_integer (csi_t *ctx, unsigned int i, csi_integer_t *out)
{
csi_object_t *obj;
int type;
obj = _csi_peek_ostack (ctx, i);
type = csi_object_get_type (obj);
switch (type) {
case CSI_OBJECT_TYPE_BOOLEAN:
*out = obj->datum.boolean;
break;
case CSI_OBJECT_TYPE_INTEGER:
*out = obj->datum.integer;
break;
case CSI_OBJECT_TYPE_REAL:
*out = obj->datum.real;
break;
default:
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
}
return CSI_STATUS_SUCCESS;
}
static csi_status_t
_csi_ostack_get_number (csi_t *ctx, unsigned int i, double *out)
{
csi_object_t *obj;
int type;
obj = _csi_peek_ostack (ctx, i);
type = csi_object_get_type (obj);
switch (type) {
case CSI_OBJECT_TYPE_BOOLEAN:
*out = obj->datum.boolean;
break;
case CSI_OBJECT_TYPE_INTEGER:
*out = obj->datum.integer;
break;
case CSI_OBJECT_TYPE_REAL:
*out = obj->datum.real;
break;
default:
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
}
return CSI_STATUS_SUCCESS;
}
static double
_csi_object_as_real (csi_object_t *obj)
{
int type;
type = csi_object_get_type (obj);
switch (type) {
case CSI_OBJECT_TYPE_BOOLEAN:
return obj->datum.boolean;
case CSI_OBJECT_TYPE_INTEGER:
return obj->datum.integer;
case CSI_OBJECT_TYPE_REAL:
return obj->datum.real;
default:
return 0;
}
}
static csi_status_t
_csi_ostack_get_name (csi_t *ctx, unsigned int i, csi_name_t *out)
{
csi_object_t *obj;
obj = _csi_peek_ostack (ctx, i);
if (_csi_unlikely (csi_object_get_type (obj) != CSI_OBJECT_TYPE_NAME))
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
*out = obj->datum.name;
return CSI_STATUS_SUCCESS;
}
static csi_status_t
_csi_ostack_get_context (csi_t *ctx, unsigned int i, cairo_t **out)
{
csi_object_t *obj;
obj = _csi_peek_ostack (ctx, i);
if (_csi_unlikely (csi_object_get_type (obj) != CSI_OBJECT_TYPE_CONTEXT))
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
*out = obj->datum.cr;
return CSI_STATUS_SUCCESS;
}
static csi_status_t
_csi_ostack_get_font_face (csi_t *ctx, unsigned int i, cairo_font_face_t **out)
{
csi_object_t *obj;
obj = _csi_peek_ostack (ctx, i);
if (_csi_unlikely (csi_object_get_type (obj) != CSI_OBJECT_TYPE_FONT))
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
*out = obj->datum.font_face;
return CSI_STATUS_SUCCESS;
}
static csi_status_t
_csi_ostack_get_pattern (csi_t *ctx, unsigned int i, cairo_pattern_t **out)
{
csi_object_t *obj;
obj = _csi_peek_ostack (ctx, i);
if (_csi_unlikely (csi_object_get_type (obj) != CSI_OBJECT_TYPE_PATTERN))
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
*out = obj->datum.pattern;
return CSI_STATUS_SUCCESS;
}
static csi_status_t
_csi_ostack_get_scaled_font (csi_t *ctx, unsigned int i,
cairo_scaled_font_t **out)
{
csi_object_t *obj;
obj = _csi_peek_ostack (ctx, i);
if (_csi_unlikely
(csi_object_get_type (obj) != CSI_OBJECT_TYPE_SCALED_FONT))
{
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
}
*out = obj->datum.scaled_font;
return CSI_STATUS_SUCCESS;
}
static csi_status_t
_csi_ostack_get_surface (csi_t *ctx, unsigned int i, cairo_surface_t **out)
{
csi_object_t *obj;
int type;
obj = _csi_peek_ostack (ctx, i);
type = csi_object_get_type (obj);
switch (type) {
case CSI_OBJECT_TYPE_CONTEXT:
*out = cairo_get_target (obj->datum.cr);
break;
case CSI_OBJECT_TYPE_SURFACE:
*out = obj->datum.surface;
break;
default:
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
}
return CSI_STATUS_SUCCESS;
}
static csi_status_t
_csi_ostack_get_array (csi_t *ctx, unsigned int i, csi_array_t **out)
{
csi_object_t *obj;
obj = _csi_peek_ostack (ctx, i);
if (_csi_unlikely (csi_object_get_type (obj) != CSI_OBJECT_TYPE_ARRAY))
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
*out = obj->datum.array;
return CSI_STATUS_SUCCESS;
}
static csi_status_t
_csi_ostack_get_procedure (csi_t *ctx, unsigned int i, csi_array_t **out)
{
csi_object_t *obj;
obj = _csi_peek_ostack (ctx, i);
if (_csi_unlikely (! csi_object_is_procedure (obj)))
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
*out = obj->datum.array;
return CSI_STATUS_SUCCESS;
}
static csi_status_t
_csi_ostack_get_dictionary (csi_t *ctx, unsigned int i, csi_dictionary_t **out)
{
csi_object_t *obj;
obj = _csi_peek_ostack (ctx, i);
if (_csi_unlikely
(csi_object_get_type (obj) != CSI_OBJECT_TYPE_DICTIONARY))
{
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
}
*out = obj->datum.dictionary;
return CSI_STATUS_SUCCESS;
}
static csi_status_t
_csi_ostack_get_matrix (csi_t *ctx, unsigned int i, cairo_matrix_t *out)
{
csi_object_t *obj;
int type;
obj = _csi_peek_ostack (ctx, i);
type = csi_object_get_type (obj);
switch (type) {
case CSI_OBJECT_TYPE_MATRIX:
*out = obj->datum.matrix->matrix;
return CSI_STATUS_SUCCESS;
case CSI_OBJECT_TYPE_ARRAY:
if (obj->datum.array->stack.len == 6) {
cairo_matrix_init (out,
csi_number_get_value (&obj->datum.array->stack.objects[0]),
csi_number_get_value (&obj->datum.array->stack.objects[1]),
csi_number_get_value (&obj->datum.array->stack.objects[2]),
csi_number_get_value (&obj->datum.array->stack.objects[3]),
csi_number_get_value (&obj->datum.array->stack.objects[4]),
csi_number_get_value (&obj->datum.array->stack.objects[5]));
return CSI_STATUS_SUCCESS;
}
default:
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
}
}
static csi_status_t
_csi_dictionary_get_integer (csi_t *ctx,
csi_dictionary_t *dict,
const char *name,
csi_boolean_t optional,
long *value)
{
csi_status_t status;
csi_object_t key, obj;
int type;
status = csi_name_new_static (ctx, &key, name);
if (_csi_unlikely (status))
return status;
if (optional && ! csi_dictionary_has (dict, key.datum.name))
return CSI_STATUS_SUCCESS;
status = csi_dictionary_get (ctx, dict, key.datum.name, &obj);
if (_csi_unlikely (status))
return status;
type = csi_object_get_type (&obj);
switch (type) {
case CSI_OBJECT_TYPE_BOOLEAN:
*value = obj.datum.boolean;
break;
case CSI_OBJECT_TYPE_INTEGER:
*value = obj.datum.integer;
break;
case CSI_OBJECT_TYPE_REAL:
*value = obj.datum.real;
break;
default:
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
}
return CSI_STATUS_SUCCESS;
}
static csi_status_t
_csi_dictionary_get_number (csi_t *ctx,
csi_dictionary_t *dict,
const char *name,
csi_boolean_t optional,
double *value)
{
csi_status_t status;
csi_object_t key, obj;
status = csi_name_new_static (ctx, &key, name);
if (_csi_unlikely (status))
return status;
if (optional && ! csi_dictionary_has (dict, key.datum.name))
return CSI_STATUS_SUCCESS;
status = csi_dictionary_get (ctx, dict, key.datum.name, &obj);
if (_csi_unlikely (status))
return status;
*value = csi_number_get_value (&obj);
return CSI_STATUS_SUCCESS;
}
static csi_status_t
_csi_ostack_get_string (csi_t *ctx, unsigned int i, csi_string_t **out)
{
csi_object_t *obj;
obj = _csi_peek_ostack (ctx, i);
if (_csi_unlikely (csi_object_get_type (obj) != CSI_OBJECT_TYPE_STRING))
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
*out = obj->datum.string;
return CSI_STATUS_SUCCESS;
}
static csi_status_t
_csi_ostack_get_string_constant (csi_t *ctx, unsigned int i, const char **out)
{
csi_object_t *obj;
int type;
obj = _csi_peek_ostack (ctx, i);
type = csi_object_get_type (obj);
switch (type) {
case CSI_OBJECT_TYPE_NAME:
*out = (const char *) obj->datum.name;
break;
case CSI_OBJECT_TYPE_STRING:
*out = obj->datum.string->string;
break;
default:
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
}
return CSI_STATUS_SUCCESS;
}
static csi_status_t
_do_cairo_op (csi_t *ctx, void (*op) (cairo_t *))
{
cairo_t *cr;
csi_status_t status;
check (1);
status = _csi_ostack_get_context (ctx, 0, &cr);
if (_csi_unlikely (status))
return status;
op (cr);
return CSI_STATUS_SUCCESS;
}
static csi_status_t
end_dict_construction (csi_t *ctx)
{
csi_object_t obj;
csi_dictionary_t *dict;
csi_status_t status;
status = csi_dictionary_new (ctx, &obj);
if (_csi_unlikely (status))
return status;
dict = obj.datum.dictionary;
do {
csi_object_t *name, *value;
check (1);
value = _csi_peek_ostack (ctx, 0);
if (csi_object_get_type (value) == CSI_OBJECT_TYPE_MARK) {
pop (1);
break;
}
check (2);
name = _csi_peek_ostack (ctx, 1);
if (_csi_unlikely
(csi_object_get_type (name) != CSI_OBJECT_TYPE_NAME))
{
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
}
status = csi_dictionary_put (ctx, dict, name->datum.name, value);
if (_csi_unlikely (status))
return status;
pop (2);
} while (TRUE);
return push (&obj);
}
static csi_status_t
end_array_construction (csi_t *ctx)
{
csi_object_t obj;
csi_status_t status;
int len = 0;
do {
check (len + 1);
if (csi_object_get_type (_csi_peek_ostack (ctx, len)) ==
CSI_OBJECT_TYPE_MARK)
{
break;
}
len++;
} while (TRUE);
status = csi_array_new (ctx, len, &obj);
if (_csi_unlikely (status))
return status;
if (len != 0) {
csi_array_t *array;
array = obj.datum.array;
memcpy (array->stack.objects,
_csi_peek_ostack (ctx, len - 1),
sizeof (csi_object_t) * len);
array->stack.len = len;
}
ctx->ostack.len -= len + 1;
return push (&obj);
}
static csi_status_t
_alpha (csi_t *ctx)
{
csi_object_t obj;
csi_status_t status;
double a;
check (1);
status = _csi_ostack_get_number (ctx, 0, &a);
if (_csi_unlikely (status))
return status;
pop (1);
obj.type = CSI_OBJECT_TYPE_PATTERN;
obj.datum.pattern = cairo_pattern_create_rgba (0, 0, 0, a);
return push (&obj);
}
static csi_status_t
_add (csi_t *ctx)
{
csi_object_t *A;
csi_object_t *B;
csi_object_type_t type_a, type_b;
check (2);
B = _csi_peek_ostack (ctx, 0);
A = _csi_peek_ostack (ctx, 1);
type_a = csi_object_get_type (A);
if (_csi_unlikely (! (type_a == CSI_OBJECT_TYPE_INTEGER ||
type_a == CSI_OBJECT_TYPE_REAL)))
{
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
}
type_b = csi_object_get_type (B);
if (_csi_unlikely (! (type_b == CSI_OBJECT_TYPE_INTEGER ||
type_b == CSI_OBJECT_TYPE_REAL)))
{
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
}
pop (2);
if (type_a == CSI_OBJECT_TYPE_REAL &&
type_b == CSI_OBJECT_TYPE_REAL)
{
return _csi_push_ostack_real (ctx, A->datum.real + B->datum.real);
}
else if (type_a == CSI_OBJECT_TYPE_INTEGER &&
type_b == CSI_OBJECT_TYPE_INTEGER)
{
return _csi_push_ostack_integer (ctx,
A->datum.integer + B->datum.integer);
}
else
{
double v;
if (type_a == CSI_OBJECT_TYPE_REAL)
v = A->datum.real;
else
v = A->datum.integer;
if (type_b == CSI_OBJECT_TYPE_REAL)
v += B->datum.real;
else
v += B->datum.integer;
return _csi_push_ostack_real (ctx, v);
}
}
static csi_status_t
_add_color_stop (csi_t *ctx)
{
csi_status_t status;
double offset, r, g, b, a;
cairo_pattern_t *pattern = NULL; /* silence the compiler */
check (6);
status = _csi_ostack_get_number (ctx, 0, &a);
if (_csi_unlikely (status))
return status;
status = _csi_ostack_get_number (ctx, 1, &b);
if (_csi_unlikely (status))
return status;
status = _csi_ostack_get_number (ctx, 2, &g);
if (_csi_unlikely (status))
return status;
status = _csi_ostack_get_number (ctx, 3, &r);
if (_csi_unlikely (status))
return status;
status = _csi_ostack_get_number (ctx, 4, &offset);
if (_csi_unlikely (status))
return status;
status = _csi_ostack_get_pattern (ctx, 5, &pattern);
if (_csi_unlikely (status))
return status;
cairo_pattern_add_color_stop_rgba (pattern, offset, r, g, b, a);
pop (5);
return CSI_STATUS_SUCCESS;
}
static csi_status_t
_and (csi_t *ctx)
{
csi_object_t *a, *b;
int type;
check (2);
a = _csi_peek_ostack (ctx, 0);
b = _csi_peek_ostack (ctx, 1);
if (_csi_unlikely (csi_object_get_type (a) != csi_object_get_type (b)))
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
pop (2);
type = csi_object_get_type (a);
switch (type) {
case CSI_OBJECT_TYPE_INTEGER:
return _csi_push_ostack_integer (ctx,
a->datum.integer & b->datum.integer);
case CSI_OBJECT_TYPE_BOOLEAN:
return _csi_push_ostack_boolean (ctx,
a->datum.boolean & b->datum.boolean);
default:
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
}
}
static csi_status_t
_arc (csi_t *ctx)
{
csi_status_t status;
double x, y, r;
double theta1, theta2;
cairo_t *cr;
check (6);
status = _csi_ostack_get_number (ctx, 0, &theta2);
if (_csi_unlikely (status))
return status;
status = _csi_ostack_get_number (ctx, 1, &theta1);
if (_csi_unlikely (status))
return status;
status = _csi_ostack_get_number (ctx, 2, &r);
if (_csi_unlikely (status))
return status;
status = _csi_ostack_get_number (ctx, 3, &y);
if (_csi_unlikely (status))
return status;
status = _csi_ostack_get_number (ctx, 4, &x);
if (_csi_unlikely (status))
return status;
status = _csi_ostack_get_context (ctx, 5, &cr);
if (_csi_unlikely (status))
return status;
/* XXX handle path object */
cairo_arc (cr, x, y, r, theta1, theta2);
pop (5);
return CSI_STATUS_SUCCESS;
}
static csi_status_t
_arc_negative (csi_t *ctx)
{
csi_status_t status;
double x, y, r;
double theta1, theta2;
cairo_t *cr;
check (6);
status = _csi_ostack_get_number (ctx, 0, &theta2);
if (_csi_unlikely (status))
return status;
status = _csi_ostack_get_number (ctx, 1, &theta1);
if (_csi_unlikely (status))
return status;
status = _csi_ostack_get_number (ctx, 2, &r);
if (_csi_unlikely (status))
return status;
status = _csi_ostack_get_number (ctx, 3, &y);
if (_csi_unlikely (status))
return status;
status = _csi_ostack_get_number (ctx, 4, &x);
if (_csi_unlikely (status))
return status;
status = _csi_ostack_get_context (ctx, 5, &cr);
if (_csi_unlikely (status))
return status;
/* XXX handle path object */
cairo_arc_negative (cr, x, y, r, theta1, theta2);
pop (5);
return CSI_STATUS_SUCCESS;
}
static csi_status_t
_array (csi_t *ctx)
{
csi_object_t obj;
csi_status_t status;
status = csi_array_new (ctx, 0, &obj);
if (_csi_unlikely (status))
return status;
return push (&obj);
}
static csi_status_t
_bind_substitute (csi_t *ctx, csi_array_t *array)
{
csi_status_t status;
csi_integer_t i, n;
csi_dictionary_t *dict;
/* perform operator substitution on the executable array (procedure) */
dict = ctx->dstack.objects[0].datum.dictionary;
n = array->stack.len;
for (i = 0; i < n; i++) {
csi_object_t *obj = &array->stack.objects[i];
if (obj->type == (CSI_OBJECT_TYPE_NAME | CSI_OBJECT_ATTR_EXECUTABLE)) {
csi_dictionary_entry_t *entry;
entry = _csi_hash_table_lookup (&dict->hash_table,
(csi_hash_entry_t *)
&obj->datum.name);
if (entry != NULL)
*obj = entry->value;
} else if (csi_object_is_procedure (obj)) {
status = _bind_substitute (ctx, obj->datum.array);
if (_csi_unlikely (status))
return status;
}
}
return CSI_STATUS_SUCCESS;
}
static csi_status_t
_idiom_substitute (csi_t *ctx, csi_array_t *array)
{
#if 0
csi_status_t status;
csi_integer_t i, j;
/* XXX substring search, build array once then search for
* longest matching idiom, repeat. */
/* scan the top-most array for sequences we can pre-compile */
/* now recurse for subroutines */
j = array->stack.len;
for (i = 0; i < j; i++) {
csi_object_t *obj = &array->stack.objects[i];
if (csi_object_is_procedure (obj)) {
status = _idiom_substitute (ctx, obj->datum.array);
if (_csi_unlikely (_cairo_is_error (status))
return status;
}
}
#endif
return CSI_STATUS_SUCCESS;
}
static csi_status_t
_bind (csi_t *ctx)
{
csi_array_t *array;
csi_status_t status;
check (1);
status = _csi_ostack_get_procedure (ctx, 0, &array);
if (_csi_unlikely (status))
return status;
status = _bind_substitute (ctx, array);
if (_csi_unlikely (status))
return status;
status = _idiom_substitute (ctx, array);
if (_csi_unlikely (status))
return status;
return CSI_STATUS_SUCCESS;
}
static csi_status_t
_bitshift (csi_t *ctx)
{
long v, shift;
csi_status_t status;
check (2);
status = _csi_ostack_get_integer (ctx, 0, &shift);
if (_csi_unlikely (status))
return status;
status = _csi_ostack_get_integer (ctx, 1, &v);
if (_csi_unlikely (status))
return status;
if (shift < 0) {
shift = -shift;
v >>= shift;
} else
v <<= shift;
pop (1);
_csi_peek_ostack (ctx, 0)->datum.integer = v;
return CSI_STATUS_SUCCESS;
}
static csi_status_t
_clip (csi_t *ctx)
{
return _do_cairo_op (ctx, cairo_clip);
}
static csi_status_t
_clip_preserve (csi_t *ctx)
{
return _do_cairo_op (ctx, cairo_clip_preserve);
}
static csi_status_t
_close_path (csi_t *ctx)
{
return _do_cairo_op (ctx, cairo_close_path);
}
static csi_status_t
_context (csi_t *ctx)
{
csi_object_t obj;
csi_status_t status;
cairo_surface_t *surface;
cairo_t *cr;
csi_context_create_func_t hook;
csi_proxy_t *proxy;
check (1);
status = _csi_ostack_get_surface (ctx, 0, &surface);
if (_csi_unlikely (status))
return status;
hook = ctx->hooks.context_create;
if (hook != NULL)
cr = hook (ctx->hooks.closure, surface);
else
cr = cairo_create (surface);
proxy = _csi_proxy_create (ctx, cr, NULL,
ctx->hooks.context_destroy,
ctx->hooks.closure);
if (_csi_unlikely (proxy == NULL)) {
cairo_destroy (cr);
return _csi_error (CSI_STATUS_NO_MEMORY);
}
status = cairo_set_user_data (cr, &_csi_proxy_key,
proxy, _csi_proxy_destroy);
if (_csi_unlikely (status)) {
_csi_proxy_destroy (proxy);
cairo_destroy (cr);
return status;
}
pop (1);
obj.type = CSI_OBJECT_TYPE_CONTEXT;
obj.datum.cr = cr;
return push (&obj);
}
static csi_status_t
_copy (csi_t *ctx)
{
csi_object_t *obj;
int type;
check (1);
obj = csi_object_reference (_csi_peek_ostack (ctx, 0));
pop (1);
type = csi_object_get_type (obj);
switch (type) {
/*XXX array, string, dictionary, etc */
case CSI_OBJECT_TYPE_INTEGER:
{
long i, n;
n = obj->datum.integer;
if (_csi_unlikely (n < 0))
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
check (n);
for (i = n; i--; ) {
csi_status_t status;
status = _csi_push_ostack_copy (ctx,
_csi_peek_ostack (ctx, n-1));
if (_csi_unlikely (status))
return status;
}
break;
}
default:
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
}
return CSI_STATUS_SUCCESS;
}
static csi_status_t
_copy_page (csi_t *ctx)
{
csi_object_t *obj;
int type;
check (1);
obj = _csi_peek_ostack (ctx, 0);
type = csi_object_get_type (obj);
switch (type) {
case CSI_OBJECT_TYPE_CONTEXT:
cairo_copy_page (obj->datum.cr);
if (ctx->hooks.copy_page != NULL)
ctx->hooks.copy_page (ctx->hooks.closure, obj->datum.cr);
break;
case CSI_OBJECT_TYPE_SURFACE:
cairo_surface_copy_page (obj->datum.surface);
/* XXX hook? */
break;
default:
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
}
return CSI_STATUS_SUCCESS;
}
static csi_status_t
_curve_to (csi_t *ctx)
{
csi_status_t status;
csi_object_t *obj;
int type;
double x1, y1;
double x2, y2;
double x3, y3;
check (7);
status = _csi_ostack_get_number (ctx, 0, &y3);
if (_csi_unlikely (status))
return status;
status = _csi_ostack_get_number (ctx, 1, &x3);
if (_csi_unlikely (status))
return status;
status = _csi_ostack_get_number (ctx, 2, &y2);
if (_csi_unlikely (status))
return status;
status = _csi_ostack_get_number (ctx, 3, &x2);
if (_csi_unlikely (status))
return status;
status = _csi_ostack_get_number (ctx, 4, &y1);
if (_csi_unlikely (status))
return status;
status = _csi_ostack_get_number (ctx, 5, &x1);
if (_csi_unlikely (status))
return status;
obj = _csi_peek_ostack (ctx, 6);
type = csi_object_get_type (obj);
switch (type) {
case CSI_OBJECT_TYPE_CONTEXT:
cairo_curve_to (obj->datum.cr, x1, y1, x2, y2, x3, y3);
break;
case CSI_OBJECT_TYPE_PATTERN:
cairo_mesh_pattern_curve_to (obj->datum.pattern,
x1, y1, x2, y2, x3, y3);
break;
/* XXX handle path object */
}
pop (6);
return CSI_STATUS_SUCCESS;
}
static csi_status_t
_cvi (csi_t *ctx)
{
csi_object_t *val, obj;
int type;
check (1);
val = _csi_peek_ostack (ctx, 0);
type = csi_object_get_type (val);
switch (type) {
case CSI_OBJECT_TYPE_INTEGER:
return CSI_STATUS_SUCCESS;
case CSI_OBJECT_TYPE_REAL:
pop (1);
return _csi_push_ostack_integer (ctx, val->datum.real);
case CSI_OBJECT_TYPE_STRING:
if (! _csi_parse_number (&obj,
val->datum.string->string,
val->datum.string->len))
{
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
}
pop (1);
if (csi_object_get_type (&obj) == CSI_OBJECT_TYPE_INTEGER)
return push (&obj);
else
return _csi_push_ostack_integer (ctx, obj.datum.real);
default:
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
}
}
static csi_status_t
_cvr (csi_t *ctx)
{
csi_object_t *val, obj;
int type;
check (1);
val = _csi_peek_ostack (ctx, 0);
type = csi_object_get_type (val);
switch (type) {
case CSI_OBJECT_TYPE_REAL:
return CSI_STATUS_SUCCESS;
case CSI_OBJECT_TYPE_INTEGER:
pop (1);
return _csi_push_ostack_real (ctx, val->datum.integer);
case CSI_OBJECT_TYPE_STRING:
if (! _csi_parse_number (&obj,
val->datum.string->string,
val->datum.string->len))
{
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
}
pop (1);
if (csi_object_get_type (&obj) == CSI_OBJECT_TYPE_REAL)
return push (&obj);
else
return _csi_push_ostack_real (ctx, obj.datum.integer);
default:
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
}
}
static csi_status_t
_def (csi_t *ctx)
{
csi_name_t name = 0; /* silence the compiler */
csi_status_t status;
check (2);
status = _csi_ostack_get_name (ctx, 1, &name);
if (_csi_unlikely (status))
return status;
status = _csi_name_define (ctx, name, _csi_peek_ostack (ctx, 0));
if (_csi_unlikely (status))
return status;
pop (2);
return CSI_STATUS_SUCCESS;
}
static csi_status_t
_dict (csi_t *ctx)
{
csi_object_t obj;
csi_status_t status;
status = csi_dictionary_new (ctx, &obj);
if (_csi_unlikely (status))
return status;
return push (&obj);
}
static csi_status_t
_div (csi_t *ctx)
{
csi_object_t *A;
csi_object_t *B;
csi_object_type_t type_a, type_b;
check (2);
B = _csi_peek_ostack (ctx, 0);
A = _csi_peek_ostack (ctx, 1);
type_a = csi_object_get_type (A);
if (_csi_unlikely (! (type_a == CSI_OBJECT_TYPE_INTEGER ||
type_a == CSI_OBJECT_TYPE_REAL)))
{
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
}
type_b = csi_object_get_type (B);
if (_csi_unlikely (! (type_b == CSI_OBJECT_TYPE_INTEGER ||
type_b == CSI_OBJECT_TYPE_REAL)))
{
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
}
pop (2);
if (type_a == CSI_OBJECT_TYPE_REAL &&
type_b == CSI_OBJECT_TYPE_REAL)
{
return _csi_push_ostack_real (ctx, A->datum.real / B->datum.real);
}
else if (type_a == CSI_OBJECT_TYPE_INTEGER &&
type_b == CSI_OBJECT_TYPE_INTEGER)
{
return _csi_push_ostack_integer (ctx,
A->datum.integer / B->datum.integer);
}
else
{
double v;
if (type_a == CSI_OBJECT_TYPE_REAL)
v = A->datum.real;
else
v = A->datum.integer;
if (type_b == CSI_OBJECT_TYPE_REAL)
v /= B->datum.real;
else
v /= B->datum.integer;
return _csi_push_ostack_real (ctx, v);
}
}
static csi_status_t
_duplicate (csi_t *ctx)
{
check (1);
return _csi_push_ostack_copy (ctx, _csi_peek_ostack (ctx, 0));
}
static csi_status_t
_eq (csi_t *ctx)
{
csi_object_t *a, *b;
csi_boolean_t v;
check (2);
b = _csi_peek_ostack (ctx, 0);
a = _csi_peek_ostack (ctx, 1);
v = csi_object_eq (a, b);
pop (2);
return _csi_push_ostack_boolean (ctx, v);
}
static csi_status_t
_exch (csi_t *ctx)
{
return _csi_stack_exch (&ctx->ostack);
}
static csi_status_t
_false (csi_t *ctx)
{
return _csi_push_ostack_boolean (ctx, FALSE);
}
static csi_status_t
_fill (csi_t *ctx)
{
return _do_cairo_op (ctx, cairo_fill);
}
static csi_status_t
_fill_preserve (csi_t *ctx)
{
return _do_cairo_op (ctx, cairo_fill_preserve);
}
static csi_status_t
_filter (csi_t *ctx)
{
csi_object_t *src;
csi_dictionary_t *dict = NULL;
csi_status_t status;
const char *name = NULL; /* silence the compiler */
const struct filters {
const char *name;
csi_status_t (*constructor) (csi_t *t,
csi_object_t *,
csi_dictionary_t *,
csi_object_t *);
} filters[] = {
{ "ascii85", csi_file_new_ascii85_decode },
#if HAVE_ZLIB
{ "deflate", csi_file_new_deflate_decode },
#endif
#if 0
{ "lzw", csi_file_new_lzw_decode },
#endif
{ NULL, NULL }
}, *filter;
int cnt;
check (2);
status = _csi_ostack_get_string_constant (ctx, 0, &name);
if (_csi_unlikely (status))
return status;
src = _csi_peek_ostack (ctx, 1);
cnt = 2;
if (csi_object_get_type (src) == CSI_OBJECT_TYPE_DICTIONARY) {
dict = src->datum.dictionary;
check (3);
src = _csi_peek_ostack (ctx, 2);
cnt = 3;
}
for (filter = filters; filter->name != NULL; filter++) {
if (strcmp (name, filter->name) == 0) {
csi_object_t file;
status = filter->constructor (ctx, &file, dict, src);
if (_csi_unlikely (status))
return status;
pop (cnt);
return push (&file);
}
}
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
}
static cairo_status_t
_type3_init (cairo_scaled_font_t *scaled_font,
cairo_t *cr,
cairo_font_extents_t *metrics)
{
cairo_font_face_t *face;
csi_proxy_t *proxy;
csi_t *ctx;
csi_dictionary_t *font;
csi_object_t key;
csi_object_t obj;
csi_array_t *array;
csi_status_t status;
face = cairo_scaled_font_get_font_face (scaled_font);
proxy = cairo_font_face_get_user_data (face, &_csi_proxy_key);
if (_csi_unlikely (proxy == NULL))
return CAIRO_STATUS_NO_MEMORY;
ctx = proxy->ctx;
font = proxy->dictionary;
status = csi_name_new_static (ctx, &key, "metrics");
if (_csi_unlikely (status))
return CAIRO_STATUS_NO_MEMORY;
if (! csi_dictionary_has (font, key.datum.name))
return CAIRO_STATUS_SUCCESS;
status = csi_dictionary_get (ctx, font, key.datum.name, &obj);
if (_csi_unlikely (status))
return status;
if (csi_object_get_type (&obj) != CSI_OBJECT_TYPE_ARRAY)
return CAIRO_STATUS_USER_FONT_ERROR;
array = obj.datum.array;
if (array->stack.len != 5)
return CAIRO_STATUS_USER_FONT_ERROR;
metrics->ascent = csi_number_get_value (&array->stack.objects[0]);
metrics->descent = csi_number_get_value (&array->stack.objects[1]);
metrics->height = csi_number_get_value (&array->stack.objects[2]);
metrics->max_x_advance = csi_number_get_value (&array->stack.objects[3]);
metrics->max_y_advance = csi_number_get_value (&array->stack.objects[4]);
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_type3_lookup (cairo_scaled_font_t *scaled_font,
unsigned long unicode,
unsigned long *glyph)
{
cairo_font_face_t *face;
csi_proxy_t *proxy;
csi_t *ctx;
csi_dictionary_t *font;
csi_object_t obj, key;
csi_array_t *array;
char buf[12];
csi_integer_t i;
cairo_status_t status;
face = cairo_scaled_font_get_font_face (scaled_font);
proxy = cairo_font_face_get_user_data (face, &_csi_proxy_key);
if (_csi_unlikely (proxy == NULL))
return CAIRO_STATUS_USER_FONT_ERROR;
ctx = proxy->ctx;
font = proxy->dictionary;
status = csi_name_new_static (ctx, &key, "encoding");
if (_csi_unlikely (status))
return CAIRO_STATUS_USER_FONT_ERROR;
if (! csi_dictionary_has (font, key.datum.name)) {
*glyph = unicode;
return CAIRO_STATUS_SUCCESS;
}
status = csi_dictionary_get (ctx, font, key.datum.name, &obj);
if (_csi_unlikely (status))
return CAIRO_STATUS_USER_FONT_ERROR;
if (_csi_unlikely (csi_object_get_type (&obj) != CSI_OBJECT_TYPE_ARRAY))
return CAIRO_STATUS_USER_FONT_ERROR;
snprintf (buf, sizeof (buf), "uni%04lu", unicode);
array = obj.datum.array;
for (i = 0; i < array->stack.len; i++) {
csi_object_t *name;
name = &array->stack.objects[i];
if (csi_object_get_type (name) != CSI_OBJECT_TYPE_NAME)
continue;
if (strcmp ((char *) name->datum.name, buf) == 0) {
*glyph = i;
return CAIRO_STATUS_SUCCESS;
}
}
return CAIRO_STATUS_USER_FONT_ERROR;
}
static cairo_status_t
_type3_render (cairo_scaled_font_t *scaled_font,
unsigned long glyph_index,
cairo_t *cr,
cairo_text_extents_t *metrics)
{
cairo_font_face_t *face;
csi_proxy_t *proxy;
csi_t *ctx;
csi_dictionary_t *font;
csi_array_t *glyphs;
csi_object_t *glyph;
csi_object_t key;
csi_object_t obj;
csi_object_t render;
csi_status_t status;
face = cairo_scaled_font_get_font_face (scaled_font);
proxy = cairo_font_face_get_user_data (face, &_csi_proxy_key);
if (_csi_unlikely (proxy == NULL))
return CAIRO_STATUS_USER_FONT_ERROR;
ctx = proxy->ctx;
font = proxy->dictionary;
status = csi_name_new_static (ctx, &key, "glyphs");
if (_csi_unlikely (status))
return CAIRO_STATUS_USER_FONT_ERROR;
status = csi_dictionary_get (ctx, font, key.datum.name, &obj);
if (_csi_unlikely (status))
return CAIRO_STATUS_USER_FONT_ERROR;
if (_csi_unlikely (csi_object_get_type (&obj) != CSI_OBJECT_TYPE_ARRAY))
return CAIRO_STATUS_USER_FONT_ERROR;
glyphs = obj.datum.array;
glyph = &glyphs->stack.objects[glyph_index];
if (csi_object_get_type (glyph) == CSI_OBJECT_TYPE_NULL)
return CAIRO_STATUS_SUCCESS; /* .notdef */
if (_csi_unlikely (csi_object_get_type (glyph) != CSI_OBJECT_TYPE_DICTIONARY))
return CAIRO_STATUS_USER_FONT_ERROR;
status = csi_name_new_static (ctx, &key, "metrics");
if (_csi_unlikely (status))
return CAIRO_STATUS_USER_FONT_ERROR;
font = glyph->datum.dictionary;
if (csi_dictionary_has (font, key.datum.name)) {
csi_array_t *array;
status = csi_dictionary_get (ctx, font, key.datum.name, &obj);
if (_csi_unlikely (status))
return CAIRO_STATUS_USER_FONT_ERROR;
if (_csi_unlikely (csi_object_get_type (&obj) !=
CSI_OBJECT_TYPE_ARRAY))
return CAIRO_STATUS_USER_FONT_ERROR;
array = obj.datum.array;
if (_csi_unlikely (array->stack.len != 6))
return CAIRO_STATUS_USER_FONT_ERROR;
metrics->x_bearing = csi_number_get_value (&array->stack.objects[0]);
metrics->y_bearing = csi_number_get_value (&array->stack.objects[1]);
metrics->width = csi_number_get_value (&array->stack.objects[2]);
metrics->height = csi_number_get_value (&array->stack.objects[3]);
metrics->x_advance = csi_number_get_value (&array->stack.objects[4]);
metrics->y_advance = csi_number_get_value (&array->stack.objects[5]);
}
status = csi_name_new_static (ctx, &key, "render");
if (_csi_unlikely (status))
return CAIRO_STATUS_USER_FONT_ERROR;
status = csi_dictionary_get (ctx, font, key.datum.name, &render);
if (_csi_unlikely (status))
return CAIRO_STATUS_USER_FONT_ERROR;
if (_csi_unlikely (! csi_object_is_procedure (&render)))
return CAIRO_STATUS_USER_FONT_ERROR;
obj.type = CSI_OBJECT_TYPE_CONTEXT;
obj.datum.cr = cairo_reference (cr);
status = push (&obj);
if (_csi_unlikely (status)) {
cairo_destroy (cr);
return CAIRO_STATUS_USER_FONT_ERROR;
}
status = csi_object_execute (ctx, &render);
pop (1);
return status ? CAIRO_STATUS_USER_FONT_ERROR : CAIRO_STATUS_SUCCESS;
}
static csi_status_t
_font_type3 (csi_t *ctx,
csi_dictionary_t *font,
cairo_font_face_t **font_face_out)
{
cairo_font_face_t *font_face;
font_face = cairo_user_font_face_create ();
cairo_user_font_face_set_init_func (font_face, _type3_init);
cairo_user_font_face_set_unicode_to_glyph_func (font_face, _type3_lookup);
cairo_user_font_face_set_render_glyph_func (font_face, _type3_render);
*font_face_out = font_face;
return CSI_STATUS_SUCCESS;
}
#if CAIRO_HAS_FT_FONT
#include <cairo-ft.h>
#include <ft2build.h>
#include FT_FREETYPE_H
static FT_Library _ft_lib;
struct _ft_face_data {
csi_t *ctx;
csi_blob_t blob;
FT_Face face;
csi_string_t *source;
void *bytes;
cairo_font_face_t *font_face;
};
static void
_ft_done_face (void *closure)
{
struct _ft_face_data *data = closure;
csi_t *ctx;
ctx = data->ctx;
if (data->face != NULL)
FT_Done_Face (data->face);
ctx->_faces = _csi_list_unlink (ctx->_faces, &data->blob.list);
if (data->source != NULL) {
if (--data->source->base.ref == 0)
csi_string_free (ctx, data->source);
} else {
#ifdef HAVE_MMAP
munmap (data->blob.bytes, data->blob.len);
#endif
}
if (data->bytes != NULL)
_csi_free (ctx, data->bytes);
_csi_slab_free (ctx, data, sizeof (*data));
cairo_script_interpreter_destroy (ctx);
}
struct mmap_vec {
const uint8_t *bytes;
size_t num_bytes;
};
#ifdef HAVE_MMAP
/* manual form of swapping for swapless systems like tiny */
static void *
_mmap_bytes (const struct mmap_vec *vec, int count)
{
char template[] = "/tmp/csi-font.XXXXXX";
void *ptr;
int fd;
int num_bytes;
fd = mkstemp (template);
if (fd == -1)
return MAP_FAILED;
unlink (template);
num_bytes = 0;
while (count--) {
const uint8_t *bytes = vec->bytes;
size_t len = vec->num_bytes;
while (len) {
int ret = write (fd, bytes, len);
if (ret < 0) {
close (fd);
return MAP_FAILED;
}
len -= ret;
bytes += ret;
}
num_bytes += vec->num_bytes;
vec++;
}
ptr = mmap (NULL, num_bytes, PROT_READ, MAP_SHARED, fd, 0);
close (fd);
return ptr;
}
#endif
static void *
inflate_string (csi_t *ctx, csi_string_t *src)
{
uLongf len;
uint8_t *bytes;
len = src->deflate;
bytes = _csi_alloc (ctx, len + 1);
if (bytes == NULL)
return NULL;
switch (src->method) {
default:
case NONE:
free (bytes);
return NULL;
case ZLIB:
#if HAVE_ZLIB
if (uncompress ((Bytef *) bytes, &len,
(Bytef *) src->string, src->len) != Z_OK)
#endif
{
_csi_free (ctx, bytes);
return NULL;
}
break;
case LZO:
#if HAVE_LZO
if (lzo2a_decompress ((Bytef *) src->string, src->len,
(Bytef *) bytes, &len,
NULL))
#endif
{
_csi_free (ctx, bytes);
return NULL;
}
break;
}
bytes[len] = '\0';
return bytes;
}
static csi_status_t
_ft_create_for_source (csi_t *ctx,
csi_string_t *source,
int index, int load_flags,
cairo_font_face_t **font_face_out)
{
csi_blob_t tmpl;
struct _ft_face_data *data;
csi_list_t *link;
FT_Error err;
cairo_font_face_t *font_face;
csi_status_t status;
struct mmap_vec vec[2];
int vec_count;
void *bytes;
int len;
/* check for an existing FT_Face (kept alive by the font cache) */
/* XXX index/flags */
_csi_blob_init (&tmpl, (uint8_t *) source->string, source->len);
_csi_blob_hash (&tmpl, (uint32_t *) source->string, source->len / sizeof (uint32_t));
link = _csi_list_find (ctx->_faces, _csi_blob_equal, &tmpl);
if (link) {
if (--source->base.ref == 0)
csi_string_free (ctx, source);
data = csi_container_of (link, struct _ft_face_data, blob.list);
*font_face_out = cairo_font_face_reference (data->font_face);
return CSI_STATUS_SUCCESS;
}
/* no existing font_face, create new FT_Face */
if (_ft_lib == NULL) {
err = FT_Init_FreeType (&_ft_lib);
if (_csi_unlikely (err != FT_Err_Ok))
return _csi_error (CSI_STATUS_NO_MEMORY);
}
data = _csi_slab_alloc (ctx, sizeof (*data));
data->bytes = NULL;
data->source = source;
vec[0].bytes = tmpl.bytes;
vec[0].num_bytes = tmpl.len;
if (source->deflate) {
len = source->deflate;
bytes = inflate_string (ctx, source);
if (_csi_unlikely (bytes == NULL))
return _csi_error (CSI_STATUS_NO_MEMORY);
vec[1].bytes = bytes;
vec[1].num_bytes = len;
data->bytes = bytes;
vec_count = 2;
} else {
bytes = tmpl.bytes;
len = tmpl.len;
vec_count = 1;
}
data->face = NULL;
ctx->_faces = _csi_list_prepend (ctx->_faces, &data->blob.list);
data->ctx = cairo_script_interpreter_reference (ctx);
data->blob.hash = tmpl.hash;
data->blob.len = tmpl.len;
#ifdef HAVE_MMAP
data->blob.bytes = _mmap_bytes (vec, vec_count);
if (data->blob.bytes != MAP_FAILED) {
if (--source->base.ref == 0)
csi_string_free (ctx, source);
if (source->deflate) {
_csi_free (ctx, bytes);
bytes = data->blob.bytes + vec[0].num_bytes;
} else
bytes = data->blob.bytes;
data->source = NULL;
data->bytes = NULL;
} else {
data->blob.bytes = tmpl.bytes;
}
#else
data->blob.bytes = tmpl.bytes;
#endif
err = FT_New_Memory_Face (_ft_lib,
bytes, len,
index,
&data->face);
if (_csi_unlikely (err != FT_Err_Ok)) {
_ft_done_face (data);
if (err == FT_Err_Out_Of_Memory)
return _csi_error (CSI_STATUS_NO_MEMORY);
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
}
font_face = cairo_ft_font_face_create_for_ft_face (data->face, load_flags);
status = cairo_font_face_set_user_data (font_face,
&_csi_blob_key,
data, _ft_done_face);
if (_csi_unlikely (status)) {
_ft_done_face (data);
cairo_font_face_destroy (font_face);
return status;
}
data->font_face = font_face;
*font_face_out = font_face;
return CSI_STATUS_SUCCESS;
}
static csi_status_t
_ft_create_for_pattern (csi_t *ctx,
csi_string_t *string,
cairo_font_face_t **font_face_out)
{
#if CAIRO_HAS_FC_FONT
csi_blob_t tmpl;
struct _ft_face_data *data;
csi_list_t *link;
cairo_font_face_t *font_face;
FcPattern *pattern, *resolved;
csi_status_t status;
struct mmap_vec vec;
void *bytes;
_csi_blob_init (&tmpl, (uint8_t *) string->string, string->len);
_csi_blob_hash (&tmpl, (uint32_t *) string->string, string->len / sizeof (uint32_t));
link = _csi_list_find (ctx->_faces, _csi_blob_equal, &tmpl);
if (link) {
if (--string->base.ref == 0)
csi_string_free (ctx, string);
data = csi_container_of (link, struct _ft_face_data, blob.list);
*font_face_out = cairo_font_face_reference (data->font_face);
return CSI_STATUS_SUCCESS;
}
if (string->deflate) {
bytes = inflate_string (ctx, string);
if (_csi_unlikely (bytes == NULL))
return _csi_error (CSI_STATUS_NO_MEMORY);
} else {
bytes = tmpl.bytes;
}
pattern = FcNameParse (bytes);
if (!pattern)
{
/* Fontconfig's representation of charset changed mid 2014;
* We used to record charset before that. Remove everything
* after charset if that's present, and try again. */
char *s = strstr ((char *) bytes, ":charset=");
if (s)
{
*s = '\0';
pattern = FcNameParse (bytes);
}
}
if (bytes != tmpl.bytes)
_csi_free (ctx, bytes);
retry:
resolved = pattern;
if (cairo_version () < CAIRO_VERSION_ENCODE (1, 9, 0)) {
/* prior to 1.9, you needed to pass a resolved pattern */
resolved = FcFontMatch (NULL, pattern, NULL);
if (_csi_unlikely (resolved == NULL)) {
FcPatternDestroy (pattern);
return _csi_error (CSI_STATUS_NO_MEMORY);
}
}
font_face = cairo_ft_font_face_create_for_pattern (resolved);
if (resolved != pattern)
FcPatternDestroy (resolved);
if (cairo_font_face_status (font_face)) {
char *filename = NULL;
/* Try a manual fallback process by eliminating specific requests */
if (FcPatternGetString (pattern,
FC_FILE, 0,
(FcChar8 **) &filename) == FcResultMatch) {
FcPatternDel (pattern, FC_FILE);
goto retry;
}
}
FcPatternDestroy (pattern);
data = _csi_slab_alloc (ctx, sizeof (*data));
ctx->_faces = _csi_list_prepend (ctx->_faces, &data->blob.list);
data->ctx = cairo_script_interpreter_reference (ctx);
data->blob.hash = tmpl.hash;
data->blob.len = tmpl.len;
data->bytes = NULL;
data->face = NULL;
#ifdef HAVE_MMAP
vec.bytes = tmpl.bytes;
vec.num_bytes = tmpl.len;
data->blob.bytes = _mmap_bytes (&vec, 1);
if (data->blob.bytes != MAP_FAILED) {
data->source = NULL;
if (--string->base.ref == 0)
csi_string_free (ctx, string);
} else {
data->blob.bytes = tmpl.bytes;
data->source = string;
}
#else
data->blob.bytes = tmpl.bytes;
data->source = string;
#endif
status = cairo_font_face_set_user_data (font_face,
&_csi_blob_key,
data, _ft_done_face);
if (_csi_unlikely (status)) {
_ft_done_face (data);
cairo_font_face_destroy (font_face);
return status;
}
data->font_face = font_face;
*font_face_out = font_face;
return CSI_STATUS_SUCCESS;
#else
if (--string->base.ref == 0)
csi_string_free (ctx, string);
return CSI_INT_STATUS_UNSUPPORTED;
#endif
}
static csi_status_t
_ft_type42_create (csi_t *ctx,
csi_dictionary_t *font,
cairo_font_face_t **font_face_out)
{
csi_object_t key;
csi_status_t status;
/* two basic sub-types, either an FcPattern or embedded font */
status = csi_name_new_static (ctx, &key, "pattern");
if (_csi_unlikely (status))
return status;
if (csi_dictionary_has (font, key.datum.name)) {
csi_object_t obj;
int type;
status = csi_dictionary_get (ctx, font, key.datum.name, &obj);
if (_csi_unlikely (status))
return status;
type = csi_object_get_type (&obj);
switch (type) {
case CSI_OBJECT_TYPE_FILE:
status = _csi_file_as_string (ctx, obj.datum.file, &obj);
if (_csi_unlikely (status))
return status;
break;
case CSI_OBJECT_TYPE_STRING:
obj.datum.object->ref++;
break;
default:
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
}
return _ft_create_for_pattern (ctx,
obj.datum.string,
font_face_out);
}
status = csi_name_new_static (ctx, &key, "source");
if (_csi_unlikely (status))
return status;
if (csi_dictionary_has (font, key.datum.name)) {
csi_object_t obj;
long index, flags;
int type;
index = 0;
status = _csi_dictionary_get_integer (ctx, font, "index", TRUE, &index);
if (_csi_unlikely (status))
return status;
flags = 0;
status = _csi_dictionary_get_integer (ctx, font, "flags", TRUE, &flags);
if (_csi_unlikely (status))
return status;
status = csi_name_new_static (ctx, &key, "source");
if (_csi_unlikely (status))
return status;
status = csi_dictionary_get (ctx, font, key.datum.name, &obj);
if (_csi_unlikely (status))
return status;
type = csi_object_get_type (&obj);
switch (type) {
case CSI_OBJECT_TYPE_FILE:
status = _csi_file_as_string (ctx, obj.datum.file, &obj);
if (_csi_unlikely (status))
return status;
break;
case CSI_OBJECT_TYPE_STRING:
obj.datum.object->ref++;
break;
default:
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
}
return _ft_create_for_source (ctx, obj.datum.string,
index, flags,
font_face_out);
}
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
}
#else
#define _ft_type42_create(ctx, font, face_out) CSI_INT_STATUS_UNSUPPORTED
#endif
static char *
_fc_strcpy (csi_t *ctx, const char *str)
{
char *ret;
int len;
ret = strchr (str, ':');
if (ret != NULL)
len = ret - str;
else
len = strlen (str);
ret = _csi_alloc (ctx, len+1);
if (_csi_unlikely (ret == NULL))
return NULL;
memcpy (ret, str, len);
ret[len] = '\0';
return ret;
}
static cairo_font_face_t *
_select_font (const char *name)
{
cairo_surface_t *surface;
cairo_font_face_t *face;
cairo_t *cr;
/* create a dummy context to choose a font */
surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 0, 0);
cr = cairo_create (surface);
cairo_surface_destroy (surface);
cairo_select_font_face (cr, name,
CAIRO_FONT_SLANT_NORMAL,
CAIRO_FONT_WEIGHT_NORMAL);
face = cairo_font_face_reference (cairo_get_font_face (cr));
cairo_destroy (cr);
return face;
}
static csi_status_t
_ft_fallback_create_for_pattern (csi_t *ctx,
csi_string_t *string,
cairo_font_face_t **font_face_out)
{
char *str, *name;
str = string->string;
#if 0
name = strstr (str, "fullname=");
if (name != NULL)
str = name + 9;
#endif
name = _fc_strcpy (ctx, str);
if (_csi_unlikely (name == NULL))
return _csi_error (CSI_STATUS_NO_MEMORY);
*font_face_out = _select_font (name);
_csi_free (ctx, name);
return CSI_STATUS_SUCCESS;
}
static csi_status_t
_ft_type42_fallback_create (csi_t *ctx,
csi_dictionary_t *font,
cairo_font_face_t **font_face_out)
{
csi_object_t key;
csi_status_t status;
/* attempt to select a similar font */
/* two basic sub-types, either an FcPattern or embedded font */
status = csi_name_new_static (ctx, &key, "pattern");
if (_csi_unlikely (status))
return status;
if (csi_dictionary_has (font, key.datum.name)) {
csi_object_t obj;
int type;
status = csi_dictionary_get (ctx, font, key.datum.name, &obj);
if (_csi_unlikely (status))
return status;
type = csi_object_get_type (&obj);
switch (type) {
case CSI_OBJECT_TYPE_FILE:
status = _csi_file_as_string (ctx, obj.datum.file, &obj);
if (_csi_unlikely (status))
return status;
break;
case CSI_OBJECT_TYPE_STRING:
obj.datum.object->ref++;
break;
default:
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
}
return _ft_fallback_create_for_pattern (ctx,
obj.datum.string,
font_face_out);
}
/* XXX: enable the trace to run */
*font_face_out = _select_font ("Sans");
return CSI_STATUS_SUCCESS;
}
static csi_status_t
_font_type42 (csi_t *ctx, csi_dictionary_t *font, cairo_font_face_t **font_face)
{
csi_status_t status;
status = _ft_type42_create (ctx, font, font_face);
if (_csi_likely (status != CSI_INT_STATUS_UNSUPPORTED))
return status;
return _ft_type42_fallback_create (ctx, font, font_face);
}
static csi_status_t
_font (csi_t *ctx)
{
csi_dictionary_t *font;
csi_status_t status;
cairo_font_face_t *font_face = NULL; /* silence the compiler */
csi_proxy_t *proxy;
csi_object_t obj;
long type;
check (1);
status = _csi_ostack_get_dictionary (ctx, 0, &font);
if (_csi_unlikely (status))
return status;
status = _csi_dictionary_get_integer (ctx, font, "type", FALSE, &type);
if (_csi_unlikely (status))
return status;
switch (type) {
case 3:
status = _font_type3 (ctx, font, &font_face);
break;
case 42:
status = _font_type42 (ctx, font, &font_face);
break;
default:
status = _csi_error (CSI_STATUS_INVALID_SCRIPT);
break;
}
if (_csi_unlikely (status))
return status;
/* transfer ownership of dictionary to cairo_font_face_t */
proxy = _csi_proxy_create (ctx, font_face, font, NULL, NULL);
if (_csi_unlikely (proxy == NULL)) {
cairo_font_face_destroy (font_face);
return _csi_error (CSI_STATUS_NO_MEMORY);
}
status = cairo_font_face_set_user_data (font_face,
&_csi_proxy_key,
proxy, _csi_proxy_destroy);
if (_csi_unlikely (status)) {
_csi_proxy_destroy (proxy);
cairo_font_face_destroy (font_face);
return status;
}
obj.type = CSI_OBJECT_TYPE_FONT;
obj.datum.font_face = font_face;
pop (1);
status = push (&obj);
if (_csi_unlikely (status)) {
cairo_font_face_destroy (font_face);
return status;
}
return CSI_STATUS_SUCCESS;
}
static csi_status_t
_for (csi_t *ctx)
{
csi_array_t *proc;
csi_status_t status;
long i, inc, limit;
check (4);
status = _csi_ostack_get_procedure (ctx, 0, &proc);
if (_csi_unlikely (status))
return status;
status = _csi_ostack_get_integer (ctx, 1, &limit);
if (_csi_unlikely (status))
return status;
status = _csi_ostack_get_integer (ctx, 2, &inc);
if (_csi_unlikely (status))
return status;
status = _csi_ostack_get_integer (ctx, 3, &i);
if (_csi_unlikely (status))
return status;
proc->base.ref++;
pop (4);
for (; i <= limit; i += inc) {
status = _csi_push_ostack_integer (ctx, i);
if (_csi_unlikely (status))
break;
status = _csi_array_execute (ctx, proc);
if (_csi_unlikely (status))
break;
}
if (--proc->base.ref == 0)
csi_array_free (ctx, proc);
return status;
}
static csi_status_t
_ge (csi_t *ctx)
{
csi_status_t status;
csi_object_t *a, *b;
int cmp;
check (2);
b = _csi_peek_ostack (ctx, 0);
a = _csi_peek_ostack (ctx, 1);
status = csi_object_compare (a, b, &cmp);
if (_csi_unlikely (status))
return status;
pop (2);
return _csi_push_ostack_boolean (ctx, cmp >= 0);
}
static csi_status_t
_proxy_get (csi_proxy_t *proxy,
csi_name_t key)
{
csi_object_t obj;
csi_status_t status;
if (_csi_unlikely (proxy == NULL || proxy->dictionary == NULL))
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
status = csi_dictionary_get (proxy->ctx, proxy->dictionary, key, &obj);
if (_csi_unlikely (status))
return status;
return _csi_push_ostack_copy (proxy->ctx, &obj);
}
static csi_status_t
_context_get (csi_t *ctx,
cairo_t *cr,
csi_name_t key)
{
csi_status_t status;
csi_object_t obj;
if (strcmp ((char *) key, "current-point") == 0) {
double x, y;
cairo_get_current_point (cr, &x, &y);
status = _csi_push_ostack_real (ctx, x);
if (_csi_unlikely (status))
return status;
status = _csi_push_ostack_real (ctx, y);
if (_csi_unlikely (status))
return status;
return CSI_STATUS_SUCCESS;
} else if (strcmp ((char *) key, "source") == 0) {
obj.type = CSI_OBJECT_TYPE_PATTERN;
obj.datum.pattern = cairo_pattern_reference (cairo_get_source (cr));
} else if (strcmp ((char *) key, "target") == 0) {
obj.type = CSI_OBJECT_TYPE_SURFACE;
obj.datum.surface = cairo_surface_reference (cairo_get_target (cr));
} else if (strcmp ((char *) key, "group-target") == 0) {
obj.type = CSI_OBJECT_TYPE_SURFACE;
obj.datum.surface = cairo_surface_reference (cairo_get_group_target (cr));
} else if (strcmp ((char *) key, "scaled-font") == 0) {
obj.type = CSI_OBJECT_TYPE_SCALED_FONT;
obj.datum.scaled_font = cairo_scaled_font_reference (cairo_get_scaled_font (cr));
} else if (strcmp ((char *) key, "font-face") == 0) {
obj.type = CSI_OBJECT_TYPE_FONT;
obj.datum.font_face = cairo_font_face_reference (cairo_get_font_face (cr));
} else
return _proxy_get (cairo_get_user_data (cr, &_csi_proxy_key), key);
return push (&obj);
}
static csi_status_t
_font_get (csi_t *ctx,
cairo_font_face_t *font_face,
csi_name_t key)
{
return _proxy_get (cairo_font_face_get_user_data (font_face,
&_csi_proxy_key),
key);
}
static csi_status_t
_pattern_get (csi_t *ctx,
cairo_pattern_t *pattern,
csi_name_t key)
{
csi_status_t status;
if (strcmp ((char *) key, "type") == 0)
return _csi_push_ostack_integer (ctx, cairo_pattern_get_type (pattern));
if (strcmp ((char *) key, "filter") == 0)
return _csi_push_ostack_integer (ctx, cairo_pattern_get_filter (pattern));
if (strcmp ((char *) key, "extend") == 0)
return _csi_push_ostack_integer (ctx, cairo_pattern_get_extend (pattern));
if (strcmp ((char *) key, "matrix") == 0) {
csi_object_t obj;
cairo_matrix_t m;
cairo_pattern_get_matrix (pattern, &m);
status = csi_matrix_new_from_matrix (ctx, &obj, &m);
if (_csi_unlikely (status))
return status;
return push (&obj);
}
return _proxy_get (cairo_pattern_get_user_data (pattern, &_csi_proxy_key),
key);
}
static csi_status_t
_scaled_font_get (csi_t *ctx,
cairo_scaled_font_t *font,
csi_name_t key)
{
return _proxy_get (cairo_scaled_font_get_user_data (font, &_csi_proxy_key),
key);
}
static csi_status_t
_surface_get (csi_t *ctx,
cairo_surface_t *surface,
csi_name_t key)
{
if (strcmp ((char *) key, "type") == 0) {
return _csi_push_ostack_integer (ctx, cairo_surface_get_type (surface));
}
if (strcmp ((char *) key, "content") == 0) {
return _csi_push_ostack_integer (ctx,
cairo_surface_get_content (surface));
}
return _proxy_get (cairo_surface_get_user_data (surface, &_csi_proxy_key),
key);
}
static csi_status_t
_get (csi_t *ctx)
{
csi_object_t *key, *src, obj;
csi_status_t status;
int type;
check (2);
key = _csi_peek_ostack (ctx, 0);
src = _csi_peek_ostack (ctx, 1);
pop (1);
type = csi_object_get_type (src);
switch (type) {
case CSI_OBJECT_TYPE_DICTIONARY:
if (_csi_unlikely (csi_object_get_type (key) != CSI_OBJECT_TYPE_NAME))
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
status = csi_dictionary_get (ctx,
src->datum.dictionary,
key->datum.name,
&obj);
break;
case CSI_OBJECT_TYPE_ARRAY:
if (_csi_unlikely (csi_object_get_type (key) != CSI_OBJECT_TYPE_INTEGER))
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
status = csi_array_get (ctx,
src->datum.array,
key->datum.integer,
&obj);
break;
#if 0
case CSI_OBJECT_TYPE_STRING:
status = csi_string_get (src, key, &obj);
break;
#endif
case CSI_OBJECT_TYPE_CONTEXT:
if (_csi_unlikely (csi_object_get_type (key) != CSI_OBJECT_TYPE_NAME))
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
return _context_get (ctx, src->datum.cr, key->datum.name);
case CSI_OBJECT_TYPE_FONT:
if (_csi_unlikely (csi_object_get_type (key) != CSI_OBJECT_TYPE_NAME))
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
return _font_get (ctx, src->datum.font_face, key->datum.name);
case CSI_OBJECT_TYPE_PATTERN:
if (_csi_unlikely (csi_object_get_type (key) != CSI_OBJECT_TYPE_NAME))
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
return _pattern_get (ctx, src->datum.pattern, key->datum.name);
case CSI_OBJECT_TYPE_SCALED_FONT:
if (_csi_unlikely (csi_object_get_type (key) != CSI_OBJECT_TYPE_NAME))
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
return _scaled_font_get (ctx, src->datum.scaled_font, key->datum.name);
case CSI_OBJECT_TYPE_SURFACE:
if (_csi_unlikely (csi_object_get_type (key) != CSI_OBJECT_TYPE_NAME))
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
return _surface_get (ctx, src->datum.surface, key->datum.name);
default:
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
}
if (_csi_unlikely (status))
return status;
return _csi_push_ostack_copy (ctx, &obj);
}
struct glyph_advance_cache {
csi_t *ctx;
double glyph_advance[256][2];
unsigned long have_glyph_advance[256];
};
static void
glyph_advance_cache_destroy (void *closure)
{
struct glyph_advance_cache *cache = closure;
_csi_free (cache->ctx, cache);
}
static int
_glyph_string (csi_t *ctx,
csi_array_t *array,
cairo_scaled_font_t *scaled_font,
cairo_glyph_t *glyphs)
{
struct glyph_advance_cache uncached;
struct glyph_advance_cache *cache;
csi_integer_t nglyphs, i, j;
double x, y, dx;
cairo_status_t status;
if (cairo_scaled_font_status (scaled_font))
return 0;
cache = cairo_scaled_font_get_user_data (scaled_font,
(cairo_user_data_key_t *) ctx);
if (cache == NULL) {
cache = _csi_alloc (ctx, sizeof (*cache));
if (_csi_likely (cache != NULL)) {
cache->ctx = ctx;
memset (cache->have_glyph_advance, 0xff,
sizeof (cache->have_glyph_advance));
status = cairo_scaled_font_set_user_data (scaled_font,
(cairo_user_data_key_t *) ctx,
cache,
glyph_advance_cache_destroy);
if (_csi_unlikely (status)) {
_csi_free (ctx, cache);
cache = NULL;
}
}
}
if (_csi_unlikely (cache == NULL)) {
cache = &uncached;
cache->ctx = ctx;
memset (cache->have_glyph_advance, 0xff,
sizeof (cache->have_glyph_advance));
}
nglyphs = 0;
x = y = 0;
for (i = 0; i < array->stack.len; i++) {
const csi_object_t *obj = &array->stack.objects[i];
int type = csi_object_get_type (obj);
switch (type) {
case CSI_OBJECT_TYPE_ARRAY: {
const csi_array_t *glyph_array = obj->datum.array;
for (j = 0; j < glyph_array->stack.len; j++) {
unsigned long g;
int gi;
obj = &glyph_array->stack.objects[j];
if (csi_object_get_type (obj) != CSI_OBJECT_TYPE_INTEGER)
break;
g = obj->datum.integer;
glyphs[nglyphs].index = g;
glyphs[nglyphs].x = x;
glyphs[nglyphs].y = y;
gi = g % ARRAY_LENGTH (cache->have_glyph_advance);
if (cache->have_glyph_advance[gi] != g) {
cairo_text_extents_t extents;
cairo_scaled_font_glyph_extents (scaled_font,
&glyphs[nglyphs], 1,
&extents);
cache->glyph_advance[gi][0] = extents.x_advance;
cache->glyph_advance[gi][1] = extents.y_advance;
cache->have_glyph_advance[gi] = g;
}
x += cache->glyph_advance[gi][0];
y += cache->glyph_advance[gi][1];
nglyphs++;
}
break;
}
case CSI_OBJECT_TYPE_STRING: {
const csi_string_t *glyph_string = obj->datum.string;
for (j = 0; j < glyph_string->len; j++) {
uint8_t g;
g = glyph_string->string[j];
glyphs[nglyphs].index = g;
glyphs[nglyphs].x = x;
glyphs[nglyphs].y = y;
if (cache->have_glyph_advance[g] != g) {
cairo_text_extents_t extents;
cairo_scaled_font_glyph_extents (scaled_font,
&glyphs[nglyphs], 1,
&extents);
cache->glyph_advance[g][0] = extents.x_advance;
cache->glyph_advance[g][1] = extents.y_advance;
cache->have_glyph_advance[g] = g;
}
x += cache->glyph_advance[g][0];
y += cache->glyph_advance[g][1];
nglyphs++;
}
break;
}
case CSI_OBJECT_TYPE_INTEGER:
case CSI_OBJECT_TYPE_REAL: /* dx or x*/
dx = csi_number_get_value (obj);
if (i+1 == array->stack.len)
break;
type = csi_object_get_type (&array->stack.objects[i+1]);
switch (type) {
case CSI_OBJECT_TYPE_INTEGER:
case CSI_OBJECT_TYPE_REAL: /* y */
y = csi_number_get_value (&array->stack.objects[i+1]);
x = dx;
i++;
break;
default:
x += dx;
}
}
}
return nglyphs;
}
static csi_status_t
_glyph_path (csi_t *ctx)
{
csi_array_t *array;
csi_status_t status;
cairo_t *cr;
cairo_glyph_t stack_glyphs[256], *glyphs;
csi_integer_t nglyphs, i;
check (2);
status = _csi_ostack_get_array (ctx, 0, &array);
if (_csi_unlikely (status))
return status;
status = _csi_ostack_get_context (ctx, 1, &cr);
if (_csi_unlikely (status))
return status;
/* count glyphs */
nglyphs = 0;
for (i = 0; i < array->stack.len; i++) {
csi_object_t *obj = &array->stack.objects[i];
int type = csi_object_get_type (obj);
switch (type) {
case CSI_OBJECT_TYPE_ARRAY: