aboutsummaryrefslogtreecommitdiff
path: root/src/ink.c
blob: 9655d8ee3020f667aeea10f640c491a904f1edcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * ink.c
 *
 * Routines for handling Ink
 *
 * (c) 2002-2005 Thomas White <taw27@srcf.ucam.org>
 *  Part of TuxMessenger - GTK+-based MSN Messenger client
 *
 * This package is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 dated June, 1991. 
 *
 * This package is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details. 
 *
 * You should have received a copy of the GNU General Public License
 * along with this package; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA. 
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "ink.h"
#include "assert.h"
#include "routines.h"

/* Create a new Ink object. */
Ink *ink_new() {

	Ink *ink = malloc(sizeof(Ink));
	
	ink->strokes = NULL;
	ink->last_stroke = NULL;

	return ink;

}

static Ink *ink_new_from_isf_base64(const char *base64_isf) {

	Ink *ink;
	size_t isf_len;
	char *isf;
	FILE *fh = fopen("/home/weiss/isf.dat", "w");

	ink = ink_new();
	isf_len = routines_base64decode(base64_isf, &isf);

	printf("'%s' (%i bytes)\n", isf, isf_len);
	fwrite(isf, isf_len, 1, fh);
	fclose(fh);

	return ink;

}

/* Create a new Ink object and load it with data from an ISF string. */
Ink *ink_new_from_isf(const char *isf) {

	if ( strncmp(isf, "base64:", 7) == 0 ) {
		return ink_new_from_isf_base64(isf+7);
	}

	/* Can't load anything else at the moment.  Return a blank. */
	return ink_new();

}

/* Create a new stroke in a given Ink object. */
InkStroke *ink_stroke_new(Ink *ink) {

	InkStroke *stroke = malloc(sizeof(InkStroke));
	
	if ( ink->last_stroke != NULL ) {
		assert(ink->last_stroke->next == NULL);
		ink->last_stroke->next = stroke;
	} else {
		/* No strokes */
		ink->strokes = stroke;
	}	
	
	ink->last_stroke = stroke;
	
	stroke->next = NULL;
	stroke->points = NULL;
	stroke->last_point = NULL;
	
	return stroke;
	
}

/* Get last stroke - automatically creating the first one if there aren't any. */
InkStroke *ink_stroke_get_last(Ink *ink) {

	if ( ink->last_stroke != NULL ) {
		return ink->last_stroke;
	}
	
	return ink_stroke_new(ink);	/* Automatically becomes the last stroke. */
	
}

InkPoint *ink_point_get_last(InkStroke *stroke) {

	assert(stroke != NULL);
	return stroke->last_point;	/* Might be NULL. */

}

InkPoint *ink_point_get_previous(InkPoint *point) {

	if ( point == NULL ) {
		return NULL;
	}
	
	return point->prev;	/* Might be NULL. */

}

/* Create a new point in a given stroke. */
InkPoint *ink_point_add(InkStroke *stroke, double x, double y) {

	InkPoint *last_point = ink_point_get_last(stroke);
	InkPoint *point = malloc(sizeof(InkPoint));
	
	if ( last_point != NULL ) {
		assert(last_point->next == NULL);
		last_point->next = point;
		point->prev = last_point;
	} else {
		/* No points! */
		stroke->points = point;
		point->prev = NULL;
	}
	
	stroke->last_point = point;
	
	point->next = NULL;
	point->x = x;
	point->y = y;
	
	return point;
	
}

/* Destroy an Ink object. */
void ink_destroy(Ink *ink) {
	free(ink);
}