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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
|
/*
* tool_text.c
*
* Colloquium - A tiny presentation program
*
* Copyright (c) 2011 Thomas White <taw@bitwiz.org.uk>
*
* This program 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, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "presentation.h"
#include "objects.h"
#include "mainwindow.h"
#include "slide_render.h"
struct text_toolinfo
{
struct toolinfo base;
PangoContext *pc;
};
struct text_object
{
struct object base;
char *text;
size_t text_len;
int insertion_point;
int insertion_trail;
PangoLayout *layout;
PangoFontDescription *fontdesc;
double offs_x;
double offs_y;
};
static void calculate_size_from_style(struct text_object *o,
double *peright, double *pebottom,
double *pmw, double *pmh)
{
double max_width, max_height;
double ebottom, eright, mw, mh;
eright = o->base.parent->parent->slide_width
- o->base.style->margin_right;
ebottom = o->base.parent->parent->slide_height
- o->base.style->margin_bottom;
mw = o->base.parent->parent->slide_width;
mh = o->base.parent->parent->slide_height;
*peright = eright; *pebottom = ebottom;
*pmw = mw; *pmh = mh;
max_width = mw - o->base.style->margin_left
- o->base.style->margin_right;
/* Use the provided maximum width if it exists and is smaller */
if ( o->base.style->use_max_width
&& (o->base.style->max_width < max_width) )
{
max_width = o->base.style->max_width;
}
max_height = mh - o->base.style->margin_top
- o->base.style->margin_bottom;
pango_layout_set_width(o->layout, max_width*PANGO_SCALE);
pango_layout_set_height(o->layout, max_height*PANGO_SCALE);
pango_layout_set_wrap(o->layout, PANGO_WRAP_WORD_CHAR);
pango_layout_set_ellipsize(o->layout, PANGO_ELLIPSIZE_MIDDLE);
switch ( o->base.style->halign ) {
case J_LEFT :
pango_layout_set_alignment(o->layout, PANGO_ALIGN_LEFT);
break;
case J_RIGHT :
pango_layout_set_alignment(o->layout, PANGO_ALIGN_RIGHT);
break;
case J_CENTER :
pango_layout_set_alignment(o->layout, PANGO_ALIGN_CENTER);
break;
}
}
static void calculate_position_from_style(struct text_object *o,
double eright, double ebottom,
double mw, double mh)
{
double xo, yo;
xo = o->base.bb_width; yo = o->base.bb_height;
switch ( o->base.style->halign ) {
case J_LEFT :
o->base.x = o->base.style->margin_left;
break;
case J_RIGHT :
o->base.x = eright - xo;
break;
case J_CENTER :
o->base.x = mw/2.0 - xo/2.0 + o->base.style->offset_x;
break;
}
/* Correct if centering crashes into opposite margin */
if ( o->base.style->halign == J_CENTER )
{
if ( o->base.x < o->base.style->margin_left ) {
o->base.x = o->base.style->margin_left;
}
if ( o->base.x + xo > eright ) {
o->base.x = eright - xo;
}
}
switch ( o->base.style->valign ) {
case V_TOP :
o->base.y = o->base.style->margin_top;
break;
case V_BOTTOM :
o->base.y = ebottom - yo;
break;
case V_CENTER :
o->base.y = mh/2.0 - yo/2.0 + o->base.style->offset_y;
break;
}
if ( o->base.style->valign == V_CENTER ) {
if ( o->base.y < o->base.style->margin_top ) {
o->base.y = o->base.style->margin_top;
}
if ( o->base.y+yo + yo > ebottom )
{
o->base.y = ebottom - yo;
}
}
}
static void update_text(struct text_object *o)
{
PangoRectangle logical;
double eright = 0.0;
double ebottom = 0.0;
double mw = 0.0;
double mh = 0.0;
int furniture = 0;
furniture = o->base.style != o->base.parent->parent->ss->styles[0];
pango_layout_set_text(o->layout, o->text, -1);
o->fontdesc = pango_font_description_from_string(o->base.style->font);
pango_layout_set_font_description(o->layout, o->fontdesc);
if ( furniture ) {
calculate_size_from_style(o, &eright, &ebottom, &mw, &mh);
} else {
pango_layout_set_alignment(o->layout, PANGO_ALIGN_LEFT);
}
pango_layout_get_extents(o->layout, NULL, &logical);
o->base.bb_width = logical.width / PANGO_SCALE;
o->base.bb_height = logical.height / PANGO_SCALE;
o->offs_x = logical.x / PANGO_SCALE;
o->offs_y = logical.y / PANGO_SCALE;
if ( furniture ) {
calculate_position_from_style(o, eright, ebottom, mw, mh);
}
}
void insert_text(struct object *op, char *t)
{
struct text_object *o = (struct text_object *)op;
char *tmp;
size_t tlen, olen;
int i;
assert(o->base.type == TEXT);
tlen = strlen(t);
olen = strlen(o->text);
if ( tlen + olen + 1 > o->text_len ) {
char *try;
try = realloc(o->text, o->text_len + tlen + 64);
if ( try == NULL ) return; /* Failed to insert */
o->text = try;
o->text_len += 64;
o->text_len += tlen;
}
tmp = malloc(o->text_len);
if ( tmp == NULL ) return;
for ( i=0; i<o->insertion_point; i++ ) {
tmp[i] = o->text[i];
}
for ( i=0; i<tlen; i++ ) {
tmp[i+o->insertion_point] = t[i];
}
for ( i=0; i<olen-o->insertion_point; i++ ) {
tmp[i+o->insertion_point+tlen] = o->text[i+o->insertion_point];
}
tmp[olen+tlen] = '\0';
memcpy(o->text, tmp, o->text_len);
free(tmp);
update_text(o);
o->insertion_point += tlen;
o->base.parent->object_seq++;
o->base.empty = 0;
}
void move_cursor(struct object *op, int dir)
{
struct text_object *o = (struct text_object *)op;
int new_idx, new_trail;
pango_layout_move_cursor_visually(o->layout, TRUE, o->insertion_point,
o->insertion_trail,
dir, &new_idx, &new_trail);
if ( (new_idx >= 0) && (new_idx < G_MAXINT) ) {
o->insertion_point = new_idx;
o->insertion_trail = new_trail;
}
}
void move_cursor_left(struct object *op)
{
move_cursor(op, -1);
}
void move_cursor_right(struct object *op)
{
move_cursor(op, +1);
}
void handle_text_backspace(struct object *op)
{
int old_idx, new_idx;
struct text_object *o = (struct text_object *)op;
assert(o->base.type == TEXT);
if ( o->insertion_point == 0 ) return; /* Nothing to delete */
old_idx = o->insertion_point;
move_cursor_left(op);
new_idx = o->insertion_point;
memmove(o->text+new_idx, o->text+old_idx,
o->text_len-new_idx);
if ( strlen(o->text) == 0 ) o->base.empty = 1;
update_text(o);
o->base.parent->object_seq++;
}
static void render_text_object(cairo_t *cr, struct object *op)
{
struct text_object *o = (struct text_object *)op;
GdkColor col;
cairo_move_to(cr, o->base.x - o->offs_x, o->base.y - o->offs_y);
gdk_color_parse(o->base.style->colour, &col);
gdk_cairo_set_source_color(cr, &col); /* FIXME: Honour alpha as well */
pango_cairo_update_layout(cr, o->layout);
pango_cairo_show_layout(cr, o->layout);
}
static void draw_caret(cairo_t *cr, struct object *op)
{
double xposd, yposd, cx;
double clow, chigh;
PangoRectangle pos;
const double t = 1.8;
struct text_object *o = (struct text_object *)op;
assert(o->base.type == TEXT);
pango_layout_get_cursor_pos(o->layout,
o->insertion_point+o->insertion_trail,
&pos, NULL);
xposd = pos.x/PANGO_SCALE;
cx = o->base.x - o->offs_x + xposd;
yposd = pos.y/PANGO_SCALE;
clow = o->base.y - o->offs_y + yposd;
chigh = clow + (pos.height/PANGO_SCALE);
cairo_move_to(cr, cx, clow);
cairo_line_to(cr, cx, chigh);
cairo_move_to(cr, cx-t, clow-t);
cairo_line_to(cr, cx, clow);
cairo_move_to(cr, cx+t, clow-t);
cairo_line_to(cr, cx, clow);
cairo_move_to(cr, cx-t, chigh+t);
cairo_line_to(cr, cx, chigh);
cairo_move_to(cr, cx+t, chigh+t);
cairo_line_to(cr, cx, chigh);
cairo_set_source_rgb(cr, 0.86, 0.0, 0.0);
cairo_set_line_width(cr, 1.0);
cairo_stroke(cr);
}
static void delete_text_object(struct object *op)
{
struct text_object *o = (struct text_object *)op;
if ( o->layout != NULL ) g_object_unref(o->layout);
if ( o->fontdesc != NULL ) pango_font_description_free(o->fontdesc);
}
static struct object *add_text_object(struct slide *s, double x, double y,
struct style *sty,
struct text_toolinfo *ti)
{
struct text_object *new;
new = calloc(1, sizeof(*new));
if ( new == NULL ) return NULL;
/* Base properties */
new->base.x = x; new->base.y = y;
new->base.bb_width = 10.0;
new->base.bb_height = 40.0;
new->base.type = TEXT;
new->base.empty = 1;
new->base.parent = NULL;
new->base.style = sty;
/* Text-specific stuff */
new->text = malloc(1);
new->text[0] = '\0';
new->text_len = 1;
new->insertion_point = 0;
new->insertion_trail = 0;
new->layout = pango_layout_new(ti->pc);
new->fontdesc = NULL;
/* Methods for this object */
new->base.render_object = render_text_object;
new->base.delete_object = delete_text_object;
if ( add_object_to_slide(s, (struct object *)new) ) {
delete_object((struct object *)new);
return NULL;
}
s->object_seq++;
return (struct object *)new;
}
static void click_create(struct presentation *p, struct toolinfo *tip,
double x, double y)
{
struct object *n;
struct text_toolinfo *ti = (struct text_toolinfo *)tip;
/* FIXME: Insert ESP here and possibly select a different style */
n = add_text_object(p->view_slide, x, y, p->ss->styles[0], ti);
p->editing_object = n;
}
static void click_select(struct presentation *p, struct toolinfo *tip,
double x, double y)
{
int xp, yp;
gboolean v;
struct text_object *o = (struct text_object *)p->editing_object;
int idx, trail;
assert(o->base.type == TEXT);
xp = (x - o->base.x + o->offs_x)*PANGO_SCALE;
yp = (y - o->base.y + o->offs_y)*PANGO_SCALE;
v = pango_layout_xy_to_index(o->layout, xp, yp, &idx, &trail);
o->insertion_point = idx+trail;
}
static void drag_object(struct toolinfo *tip, struct presentation *p,
struct object *o, double x, double y)
{
/* Do nothing */
}
static void create_default(struct presentation *p, struct style *sty,
struct toolinfo *tip)
{
struct object *n;
struct text_toolinfo *ti = (struct text_toolinfo *)tip;
n = add_text_object(p->view_slide, 0.0, 0.0, sty, ti);
p->editing_object = n;
}
static void select_object(struct object *o,struct toolinfo *tip)
{
/* Do nothing */
}
static void deselect_object(struct object *o,struct toolinfo *tip)
{
if ( (o != NULL) && o->empty ) {
delete_object(o);
}
}
static void draw_overlay(cairo_t *cr, struct object *o)
{
draw_editing_box(cr, o->x, o->y,
o->bb_width, o->bb_height);
draw_caret(cr, o);
}
struct toolinfo *initialise_text_tool(GtkWidget *w)
{
struct text_toolinfo *ti;
ti = malloc(sizeof(*ti));
ti->pc = gtk_widget_get_pango_context(w);
ti->base.click_create = click_create;
ti->base.click_select = click_select;
ti->base.create_default = create_default;
ti->base.select = select_object;
ti->base.deselect = deselect_object;
ti->base.drag_object = drag_object;
ti->base.draw_editing_overlay = draw_overlay;
return (struct toolinfo *)ti;
}
|