1 /* Copyright (C) 2008 Vincent Penquerc'h.
2 This file is part of the Kate codec library.
3 Written by Vincent Penquerc'h.
4
5 Use, distribution and reproduction of this library is governed
6 by a BSD style source license included with this source in the
7 file 'COPYING'. Please read these terms before distributing. */
8
9
10 #define KATE_INTERNAL
11 #include "kate_internal.h"
12
13 #ifdef HAVE_STDLIB_H
14 #include <stdlib.h>
15 #endif
16 #ifdef HAVE_STRING_H
17 #include <string.h>
18 #endif
19 #include "kate/kate.h"
20 #include "kate_encode_state.h"
21 #include "kate_fp.h"
22 #include "kate_rle.h"
23 #include "kate_meta.h"
24
25 #define NUM_HEADERS 9
26
27 /**
28 \ingroup encoding
29 Initializes a kate_state structure for encoding using the supplied kate_info structure.
30 When done, the kate_state structure should be cleared using kate_clear.
31 \param k the kate_state structure to initialize for encoding
32 \param ki the kate_info structure containing the encoding parameters
33 \returns 0 success
34 \return KATE_E_* error
35 */
kate_encode_init(kate_state * k,kate_info * ki)36 int kate_encode_init(kate_state *k,kate_info *ki)
37 {
38 if (!k || !ki) return KATE_E_INVALID_PARAMETER;
39
40 k->ki=ki;
41 k->kds=NULL;
42 k->ki->num_headers=NUM_HEADERS;
43 k->kes=kate_encode_state_create(ki);
44 if (!k->kes) return KATE_E_OUT_OF_MEMORY;
45
46 return 0;
47 }
48
kate_pack_write1(kate_pack_buffer * kpb,long value)49 static inline void kate_pack_write1(kate_pack_buffer *kpb,long value)
50 {
51 kate_pack_write(kpb,value,1);
52 }
53
kate_writebuf(kate_pack_buffer * kpb,const char * s,int len)54 static void kate_writebuf(kate_pack_buffer *kpb,const char *s,int len)
55 {
56 while (len--) kate_pack_write(kpb,*s++,8);
57 }
58
kate_write32(kate_pack_buffer * kpb,kate_int32_t v)59 static void kate_write32(kate_pack_buffer *kpb,kate_int32_t v)
60 {
61 kate_pack_write(kpb,v&0xff,8);
62 v>>=8;
63 kate_pack_write(kpb,v&0xff,8);
64 v>>=8;
65 kate_pack_write(kpb,v&0xff,8);
66 v>>=8;
67 kate_pack_write(kpb,v&0xff,8);
68 }
69
kate_write32v(kate_pack_buffer * kpb,kate_int32_t v)70 static void kate_write32v(kate_pack_buffer *kpb,kate_int32_t v)
71 {
72 if (v>=0 && v<=14) {
73 kate_pack_write(kpb,v,4);
74 }
75 else {
76 int bits=0;
77 kate_int32_t tmp;
78 kate_pack_write(kpb,15,4);
79 if (v&0x80000000) {
80 kate_pack_write1(kpb,1);
81 v=-v;
82 }
83 else {
84 kate_pack_write1(kpb,0);
85 }
86 tmp=v;
87 while (tmp) {
88 ++bits;
89 tmp>>=1;
90 }
91 if (bits==0) bits=1;
92 kate_pack_write(kpb,bits-1,5);
93 kate_pack_write(kpb,v,bits);
94 }
95 }
96
kate_write64(kate_pack_buffer * kpb,kate_int64_t v)97 static void kate_write64(kate_pack_buffer *kpb,kate_int64_t v)
98 {
99 kate_write32(kpb,(kate_int32_t)v);
100 v>>=32;
101 kate_write32(kpb,(kate_int32_t)v);
102 }
103
kate_open_warp(kate_pack_buffer * warp)104 static void kate_open_warp(kate_pack_buffer *warp)
105 {
106 kate_pack_writeinit(warp);
107 }
108
kate_close_warp(kate_pack_buffer * warp,kate_pack_buffer * kpb)109 static void kate_close_warp(kate_pack_buffer *warp,kate_pack_buffer *kpb)
110 {
111 int bits=kate_pack_bits(warp);
112 unsigned char *buffer=kate_pack_get_buffer(warp);
113 kate_write32v(kpb,bits);
114 while (bits>0) {
115 kate_pack_writecopy(kpb,buffer,bits>32?32:bits);
116 buffer+=32/8;
117 bits-=32;
118 }
119 kate_pack_writeclear(warp);
120 }
121
kate_warp(kate_pack_buffer * kpb)122 static void kate_warp(kate_pack_buffer *kpb)
123 {
124 kate_pack_buffer warp;
125 kate_open_warp(&warp);
126 kate_close_warp(&warp,kpb);
127 }
128
kate_write_metadata(kate_pack_buffer * kpb,const kate_meta * km)129 static void kate_write_metadata(kate_pack_buffer *kpb,const kate_meta *km)
130 {
131 size_t n;
132
133 kate_pack_write1(kpb,km?1:0);
134 if (km) {
135 kate_meta_leaf *kml=km->meta;
136 kate_write32v(kpb,km->nmeta);
137 for (n=0;n<km->nmeta;++n,++kml) {
138 size_t len=strlen(kml->tag);
139 kate_write32v(kpb,len);
140 kate_writebuf(kpb,kml->tag,len);
141 kate_write32v(kpb,kml->len);
142 kate_writebuf(kpb,kml->value,kml->len);
143 kate_warp(kpb);
144 }
145
146 kate_warp(kpb);
147 }
148 }
149
kate_finalize_packet_buffer(kate_pack_buffer * kpb,kate_packet * kp,kate_state * k)150 static int kate_finalize_packet_buffer(kate_pack_buffer *kpb,kate_packet *kp,kate_state *k)
151 {
152 if (!kpb || !kp || !k) return KATE_E_INVALID_PARAMETER;
153 if (!k->kes) return KATE_E_INIT;
154
155 /* fill up any remaining bits in the last byte with zeros */
156 kate_pack_writealign(kpb);
157
158 kp->nbytes=kate_pack_bytes(kpb);
159 kp->data=kate_malloc(kp->nbytes);
160 if (!kp->data) return KATE_E_OUT_OF_MEMORY;
161
162 memcpy(kp->data,kate_pack_get_buffer(kpb),kp->nbytes);
163
164 /* reset the buffer so we're ready for next packet */
165 kate_pack_writeclear(kpb);
166 kate_pack_writeinit(kpb);
167
168 ++k->kes->packetno;
169
170 /* clear any overrides */
171 return kate_encode_state_clear_overrides(k->kes);
172 }
173
kate_encode_start_header(kate_pack_buffer * kpb,int headerid)174 static int kate_encode_start_header(kate_pack_buffer *kpb,int headerid)
175 {
176 if (!kpb || !(headerid&0x80)) return KATE_E_INVALID_PARAMETER;
177
178 kate_pack_write(kpb,headerid,8);
179 kate_writebuf(kpb,"kate\0\0\0",7);
180 kate_pack_write(kpb,0,8); /* reserved - 0 */
181
182 return 0;
183 }
184
kate_encode_write_canvas_size(kate_pack_buffer * kpb,size_t size)185 static int kate_encode_write_canvas_size(kate_pack_buffer *kpb,size_t size)
186 {
187 size_t base=size;
188 size_t shift=0;
189
190 if (!kpb) return KATE_E_INVALID_PARAMETER;
191
192 while (base&~((1<<12)-1)) {
193 /* we have a high bit we can't fit, increase shift if we wouldn't lose low bits */
194 if ((size>>shift)&1) return KATE_E_LIMIT;
195 ++shift;
196 base>>=1;
197 }
198 if (shift>=16) return KATE_E_LIMIT;
199
200 /* the size can be represented in our encoding */
201 kate_pack_write(kpb,shift,4);
202 kate_pack_write(kpb,base&0x0f,4);
203 kate_pack_write(kpb,base>>4,8);
204
205 return 0;
206 }
207
kate_encode_info(kate_state * k,kate_packet * kp)208 static int kate_encode_info(kate_state *k,kate_packet *kp)
209 {
210 kate_pack_buffer *kpb;
211 kate_info *ki;
212 size_t len;
213 int ret;
214
215 if (!k || !kp) return KATE_E_INVALID_PARAMETER;
216 if (!k->kes) return KATE_E_INIT;
217
218 kpb=&k->kes->kpb;
219
220 ret=kate_encode_start_header(kpb,0x80);
221 if (ret<0) return ret;
222
223 ki=k->ki;
224 kate_pack_write(kpb,KATE_BITSTREAM_VERSION_MAJOR,8);
225 kate_pack_write(kpb,KATE_BITSTREAM_VERSION_MINOR,8);
226 kate_pack_write(kpb,ki->num_headers,8);
227 kate_pack_write(kpb,ki->text_encoding,8);
228 kate_pack_write(kpb,ki->text_directionality,8);
229 kate_pack_write(kpb,0,8); /* reserved - 0 */
230 kate_pack_write(kpb,kate_granule_shift(k->ki),8);
231 ret=kate_encode_write_canvas_size(kpb,ki->original_canvas_width);
232 if (ret<0) return ret;
233 ret=kate_encode_write_canvas_size(kpb,ki->original_canvas_height);
234 if (ret<0) return ret;
235 kate_write32(kpb,0); /* reserved - 0 */
236 kate_write32(kpb,ki->gps_numerator);
237 kate_write32(kpb,ki->gps_denominator);
238
239 /* language is a 15+1 character max null terminated string */
240 if (ki->language) {
241 len=strlen(ki->language);
242 if (len>15) return KATE_E_LIMIT;
243 kate_writebuf(kpb,ki->language,len);
244 }
245 else len=0;
246 while (len++<16) kate_pack_write(kpb,0,8);
247
248 /* category is a 15+1 character max null terminated string */
249 if (ki->category) {
250 len=strlen(ki->category);
251 if (len>15) return KATE_E_LIMIT;
252 kate_writebuf(kpb,ki->category,len);
253 }
254 else len=0;
255 while (len++<16) kate_pack_write(kpb,0,8);
256
257 return kate_finalize_packet_buffer(kpb,kp,k);
258 }
259
kate_encode_comment(kate_state * k,kate_comment * kc,kate_packet * kp)260 static int kate_encode_comment(kate_state *k,kate_comment *kc,kate_packet *kp)
261 {
262 kate_pack_buffer *kpb;
263 const char *vendor;
264 int vendor_len;
265 int ret;
266
267 if (!k || !kc || !kp) return KATE_E_INVALID_PARAMETER;
268 if (!k->kes) return KATE_E_INIT;
269
270 kpb=&k->kes->kpb;
271
272 ret=kate_encode_start_header(kpb,0x81);
273 if (ret<0) return ret;
274
275 vendor=kate_get_version_string();
276 if (!vendor) return KATE_E_INIT; /* wtf ??? */
277 vendor_len=strlen(vendor);
278
279 /* mostly copied from theora encoder_toplevel.c */
280 kate_write32(kpb,vendor_len);
281 kate_writebuf(kpb,vendor,vendor_len);
282
283 if (kc->comments<0) return KATE_E_INIT;
284 kate_write32(kpb,kc->comments);
285 if (kc->comments) {
286 int i;
287 for(i=0;i<kc->comments;++i) {
288 if (kc->user_comments[i]) {
289 if (kc->comment_lengths[i]<0) return KATE_E_INIT;
290 kate_write32(kpb,kc->comment_lengths[i]);
291 ret=kate_text_validate(kate_utf8,kc->user_comments[i],kc->comment_lengths[i]);
292 if (ret<0) return ret;
293 kate_writebuf(kpb,kc->user_comments[i],kc->comment_lengths[i]);
294 }
295 else {
296 kate_write32(kpb,0);
297 }
298 }
299 }
300
301 return kate_finalize_packet_buffer(kpb,kp,k);
302 }
303
kate_encode_region(const kate_region * kr,kate_pack_buffer * kpb)304 static int kate_encode_region(const kate_region *kr,kate_pack_buffer *kpb)
305 {
306 if (!kr || !kpb) return KATE_E_INVALID_PARAMETER;
307
308 kate_pack_write(kpb,kr->metric,8);
309 kate_write32v(kpb,kr->x);
310 kate_write32v(kpb,kr->y);
311 kate_write32v(kpb,kr->w);
312 kate_write32v(kpb,kr->h);
313 kate_write32v(kpb,kr->style);
314
315 {
316 /* bitstream 0.2: add clip */
317 kate_pack_buffer warp;
318 kate_open_warp(&warp);
319 kate_pack_write1(&warp,kr->clip);
320 kate_close_warp(&warp,kpb);
321 }
322
323 {
324 /* bitstream 0.6: add metadata */
325 kate_pack_buffer warp;
326 kate_open_warp(&warp);
327 kate_write_metadata(&warp,kr->meta);
328 kate_close_warp(&warp,kpb);
329 }
330
331 kate_warp(kpb);
332
333 return 0;
334 }
335
kate_encode_regions(kate_state * k,kate_packet * kp)336 static int kate_encode_regions(kate_state *k,kate_packet *kp)
337 {
338 kate_pack_buffer *kpb;
339 kate_info *ki;
340 size_t n;
341 int ret;
342
343 if (!k || !kp) return KATE_E_INVALID_PARAMETER;
344 if (!k->kes) return KATE_E_INIT;
345
346 kpb=&k->kes->kpb;
347
348 ret=kate_encode_start_header(kpb,0x82);
349 if (ret<0) return ret;
350
351 ki=k->ki;
352 if (!ki) return KATE_E_INIT;
353
354 kate_write32v(kpb,ki->nregions);
355
356 for(n=0;n<ki->nregions;++n) {
357 ret=kate_encode_region(ki->regions[n],kpb);
358 if (ret<0) return ret;
359 }
360
361 kate_warp(kpb);
362
363 return kate_finalize_packet_buffer(kpb,kp,k);
364 }
365
kate_encode_color(const kate_color * kc,kate_pack_buffer * kpb)366 static int kate_encode_color(const kate_color *kc,kate_pack_buffer *kpb)
367 {
368 if (!kc || !kpb) return KATE_E_INVALID_PARAMETER;
369 kate_pack_write(kpb,kc->r,8);
370 kate_pack_write(kpb,kc->g,8);
371 kate_pack_write(kpb,kc->b,8);
372 kate_pack_write(kpb,kc->a,8);
373 return 0;
374 }
375
kate_encode_style(const kate_style * ks,kate_pack_buffer * kpb)376 static int kate_encode_style(const kate_style *ks,kate_pack_buffer *kpb)
377 {
378 kate_float d[8];
379 size_t idx;
380
381 if (!ks || !kpb) return KATE_E_INVALID_PARAMETER;
382
383 idx=0;
384 d[idx++]=ks->halign;
385 d[idx++]=ks->valign;
386 d[idx++]=ks->font_width;
387 d[idx++]=ks->font_height;
388 d[idx++]=ks->left_margin;
389 d[idx++]=ks->top_margin;
390 d[idx++]=ks->right_margin;
391 d[idx++]=ks->bottom_margin;
392 kate_fp_encode_kate_float(sizeof(d)/sizeof(d[0]),d,1,kpb);
393 kate_encode_color(&ks->text_color,kpb);
394 kate_encode_color(&ks->background_color,kpb);
395 kate_encode_color(&ks->draw_color,kpb);
396 kate_pack_write(kpb,ks->font_metric,8);
397 kate_pack_write(kpb,ks->margin_metric,8);
398 kate_pack_write1(kpb,ks->bold);
399 kate_pack_write1(kpb,ks->italics);
400 kate_pack_write1(kpb,ks->underline);
401 kate_pack_write1(kpb,ks->strike);
402
403 {
404 /* bitstream 0.2: add justify and font */
405 kate_pack_buffer warp;
406 kate_open_warp(&warp);
407 kate_pack_write1(&warp,ks->justify);
408 if (ks->font) {
409 size_t len=strlen(ks->font);
410 kate_write32v(&warp,len);
411 kate_writebuf(&warp,ks->font,len);
412 }
413 else {
414 kate_write32v(&warp,0);
415 }
416 kate_close_warp(&warp,kpb);
417 }
418
419 {
420 /* bitstream 0.4: add nowrap */
421 kate_pack_buffer warp;
422 kate_open_warp(&warp);
423 kate_write32v(&warp,ks->wrap_mode);
424 kate_close_warp(&warp,kpb);
425 }
426
427 {
428 /* bitstream 0.6: add metadata */
429 kate_pack_buffer warp;
430 kate_open_warp(&warp);
431 kate_write_metadata(&warp,ks->meta);
432 kate_close_warp(&warp,kpb);
433 }
434
435 kate_warp(kpb);
436
437 return 0;
438 }
439
kate_encode_styles(kate_state * k,kate_packet * kp)440 static int kate_encode_styles(kate_state *k,kate_packet *kp)
441 {
442 kate_pack_buffer *kpb;
443 kate_info *ki;
444 size_t n;
445 int ret;
446
447 if (!k || !kp) return KATE_E_INVALID_PARAMETER;
448 if (!k->kes) return KATE_E_INIT;
449
450 kpb=&k->kes->kpb;
451
452 ret=kate_encode_start_header(kpb,0x83);
453 if (ret<0) return ret;
454
455 ki=k->ki;
456 if (!ki) return KATE_E_INIT;
457
458 kate_write32v(kpb,ki->nstyles);
459
460 for(n=0;n<ki->nstyles;++n) {
461 ret=kate_encode_style(ki->styles[n],kpb);
462 if (ret<0) return ret;
463 }
464
465 kate_warp(kpb);
466
467 return kate_finalize_packet_buffer(kpb,kp,k);
468 }
469
kate_encode_curve(const kate_curve * kc,kate_pack_buffer * kpb)470 static int kate_encode_curve(const kate_curve *kc,kate_pack_buffer *kpb)
471 {
472 if (!kc || !kpb) return KATE_E_INVALID_PARAMETER;
473
474 kate_pack_write(kpb,kc->type,8);
475 kate_write32v(kpb,kc->npts);
476 kate_warp(kpb);
477 if (kc->npts) kate_fp_encode_kate_float(kc->npts,kc->pts,2,kpb);
478
479 return 0;
480 }
481
kate_encode_curves(kate_state * k,kate_packet * kp)482 static int kate_encode_curves(kate_state *k,kate_packet *kp)
483 {
484 kate_pack_buffer *kpb;
485 kate_info *ki;
486 size_t n;
487 int ret;
488
489 if (!k || !kp) return KATE_E_INVALID_PARAMETER;
490 if (!k->kes) return KATE_E_INIT;
491
492 kpb=&k->kes->kpb;
493
494 ret=kate_encode_start_header(kpb,0x84);
495 if (ret<0) return ret;
496
497 ki=k->ki;
498 if (!ki) return KATE_E_INIT;
499
500 kate_write32v(kpb,ki->ncurves);
501
502 for(n=0;n<ki->ncurves;++n) {
503 ret=kate_encode_curve(ki->curves[n],kpb);
504 if (ret<0) return ret;
505 }
506
507 kate_warp(kpb);
508
509 return kate_finalize_packet_buffer(kpb,kp,k);
510 }
511
kate_encode_motion(const kate_info * ki,const kate_motion * km,kate_pack_buffer * kpb)512 static int kate_encode_motion(const kate_info *ki,const kate_motion *km,kate_pack_buffer *kpb)
513 {
514 size_t n;
515 int ret;
516
517 if (!ki || !km || !kpb) return KATE_E_INVALID_PARAMETER;
518
519 kate_write32v(kpb,km->ncurves);
520 for (n=0;n<km->ncurves;++n) {
521 int idx=kate_find_curve(ki,km->curves[n]);
522 if (idx<0) {
523 kate_pack_write1(kpb,0);
524 ret=kate_encode_curve(km->curves[n],kpb);
525 if (ret<0) return ret;
526 }
527 else {
528 kate_pack_write1(kpb,1);
529 kate_write32v(kpb,idx);
530 }
531 }
532 kate_fp_encode_kate_float(km->ncurves,km->durations,1,kpb);
533 kate_pack_write(kpb,km->x_mapping,8);
534 kate_pack_write(kpb,km->y_mapping,8);
535 kate_pack_write(kpb,km->semantics,8);
536 kate_pack_write1(kpb,km->periodic);
537
538 {
539 /* bitstream 0.6: add metadata */
540 kate_pack_buffer warp;
541 kate_open_warp(&warp);
542 kate_write_metadata(&warp,km->meta);
543 kate_close_warp(&warp,kpb);
544 }
545
546
547 kate_warp(kpb);
548
549 return 0;
550 }
551
kate_encode_motions(kate_state * k,kate_packet * kp)552 static int kate_encode_motions(kate_state *k,kate_packet *kp)
553 {
554 kate_pack_buffer *kpb;
555 kate_info *ki;
556 size_t n;
557 int ret;
558
559 if (!k || !kp) return KATE_E_INVALID_PARAMETER;
560 if (!k->kes) return KATE_E_INIT;
561
562 kpb=&k->kes->kpb;
563
564 ret=kate_encode_start_header(kpb,0x85);
565 if (ret<0) return ret;
566
567 ki=k->ki;
568 if (!ki) return KATE_E_INIT;
569
570 kate_write32v(kpb,ki->nmotions);
571
572 for(n=0;n<ki->nmotions;++n) {
573 ret=kate_encode_motion(ki,ki->motions[n],kpb);
574 if (ret<0) return ret;
575 }
576
577 kate_warp(kpb);
578
579 return kate_finalize_packet_buffer(kpb,kp,k);
580 }
581
kate_encode_palette(const kate_palette * kp,kate_pack_buffer * kpb)582 static int kate_encode_palette(const kate_palette *kp,kate_pack_buffer *kpb)
583 {
584 size_t n;
585
586 if (!kp || !kpb) return KATE_E_INVALID_PARAMETER;
587 if (kp->ncolors<=0 || kp->ncolors>256) return KATE_E_LIMIT;
588
589 kate_pack_write(kpb,kp->ncolors-1,8);
590 for (n=0;n<kp->ncolors;++n) {
591 int ret=kate_encode_color(kp->colors+n,kpb);
592 if (ret<0) return ret;
593 }
594
595 {
596 /* bitstream 0.6: add metadata */
597 kate_pack_buffer warp;
598 kate_open_warp(&warp);
599 kate_write_metadata(&warp,kp->meta);
600 kate_close_warp(&warp,kpb);
601 }
602
603
604 kate_warp(kpb);
605
606 return 0;
607 }
608
kate_encode_palettes(kate_state * k,kate_packet * kp)609 static int kate_encode_palettes(kate_state *k,kate_packet *kp)
610 {
611 kate_pack_buffer *kpb;
612 kate_info *ki;
613 size_t n;
614 int ret;
615
616 if (!k || !kp) return KATE_E_INVALID_PARAMETER;
617 if (!k->kes) return KATE_E_INIT;
618
619 kpb=&k->kes->kpb;
620
621 ret=kate_encode_start_header(kpb,0x86);
622 if (ret<0) return ret;
623
624 ki=k->ki;
625 if (!ki) return KATE_E_INIT;
626
627 kate_write32v(kpb,ki->npalettes);
628
629 for(n=0;n<ki->npalettes;++n) {
630 ret=kate_encode_palette(ki->palettes[n],kpb);
631 if (ret<0) return ret;
632 }
633
634 kate_warp(kpb);
635
636 return kate_finalize_packet_buffer(kpb,kp,k);
637 }
638
639 #if 0
640 static int kate_encode_paletted_bitmap(const kate_bitmap *kb,kate_pack_buffer *kpb)
641 {
642 size_t w,h,n;
643 unsigned int maxpixel;
644
645 if (kb->bpp>8) return KATE_E_LIMIT;
646
647 kate_write32v(kpb,kb->palette);
648 n=0;
649 maxpixel=(1<<kb->bpp)-1;
650 for (h=0;h<kb->height;++h) {
651 for (w=0;w<kb->width;++w) {
652 unsigned int pixel=kb->pixels[n++];
653 if (pixel>maxpixel) return KATE_E_LIMIT;
654 kate_pack_write(kpb,pixel,kb->bpp);
655 }
656 }
657
658 return 0;
659 }
660 #endif
661
kate_encode_rle_bitmap(const kate_bitmap * kb,kate_pack_buffer * kpb)662 static int kate_encode_rle_bitmap(const kate_bitmap *kb,kate_pack_buffer *kpb)
663 {
664 if (kb->bpp>8) return KATE_E_LIMIT;
665
666 kate_pack_write(kpb,kate_bitmap_type_paletted,8);
667 kate_pack_write(kpb,1,8); /* RLE encoding */
668 kate_write32v(kpb,kb->bpp);
669 kate_write32v(kpb,kb->palette);
670
671 return kate_rle_encode(kb->width,kb->height,kb->pixels,kb->bpp,kpb);
672 }
673
kate_encode_png_bitmap(const kate_bitmap * kb,kate_pack_buffer * kpb)674 static int kate_encode_png_bitmap(const kate_bitmap *kb,kate_pack_buffer *kpb)
675 {
676 kate_pack_write(kpb,kate_bitmap_type_png,8);
677 kate_write32(kpb,kb->size);
678 kate_writebuf(kpb,(const char*)kb->pixels,kb->size);
679
680 return 0;
681 }
682
kate_encode_bitmap(const kate_bitmap * kb,kate_pack_buffer * kpb)683 static int kate_encode_bitmap(const kate_bitmap *kb,kate_pack_buffer *kpb)
684 {
685 int ret;
686
687 if (!kb || !kpb) return KATE_E_INVALID_PARAMETER;
688
689 kate_write32v(kpb,kb->width);
690 kate_write32v(kpb,kb->height);
691 #if 0
692 /* paletted bitmaps are now written compressed */
693 kate_pack_write(kpb,kb->bpp,8); /* 0 marks a raw bitmap */
694 #else
695 kate_pack_write(kpb,0,8); /* 0 marks a raw bitmap */
696 #endif
697
698 switch (kb->type) {
699 case kate_bitmap_type_paletted:
700 ret=kate_encode_rle_bitmap(kb,kpb);
701 break;
702 case kate_bitmap_type_png:
703 ret=kate_encode_png_bitmap(kb,kpb);
704 break;
705 default:
706 ret=KATE_E_INVALID_PARAMETER;
707 break;
708 }
709
710 if (ret<0) return ret;
711
712 {
713 /* bitstream 0.4: x/y offsets */
714 kate_pack_buffer warp;
715 kate_open_warp(&warp);
716 kate_write32v(&warp,kb->x_offset);
717 kate_write32v(&warp,kb->y_offset);
718 kate_close_warp(&warp,kpb);
719 }
720
721 {
722 /* bitstream 0.6: add metadata */
723 kate_pack_buffer warp;
724 kate_open_warp(&warp);
725 /* for backward binary compatiblity, old code did not initialize 'meta', but had 'internal' set to 0 */
726 kate_write_metadata(&warp,kb->internal?kb->meta:NULL);
727 kate_close_warp(&warp,kpb);
728 }
729
730 kate_warp(kpb);
731
732 return 0;
733 }
734
kate_encode_bitmaps(kate_state * k,kate_packet * kp)735 static int kate_encode_bitmaps(kate_state *k,kate_packet *kp)
736 {
737 kate_pack_buffer *kpb;
738 kate_info *ki;
739 size_t n;
740 int ret;
741
742 if (!k || !kp) return KATE_E_INVALID_PARAMETER;
743 if (!k->kes) return KATE_E_INIT;
744
745 kpb=&k->kes->kpb;
746
747 ret=kate_encode_start_header(kpb,0x87);
748 if (ret<0) return ret;
749
750 ki=k->ki;
751 if (!ki) return KATE_E_INIT;
752
753 kate_write32v(kpb,ki->nbitmaps);
754
755 for(n=0;n<ki->nbitmaps;++n) {
756 ret=kate_encode_bitmap(ki->bitmaps[n],kpb);
757 if (ret<0) return ret;
758 }
759
760 kate_warp(kpb);
761
762 return kate_finalize_packet_buffer(kpb,kp,k);
763 }
764
kate_encode_font_range(const kate_info * ki,const kate_font_range * kfr,kate_pack_buffer * kpb)765 static int kate_encode_font_range(const kate_info *ki,const kate_font_range *kfr,kate_pack_buffer *kpb)
766 {
767 if (!ki || !kfr || !kpb) return KATE_E_INVALID_PARAMETER;
768
769 if (!kate_is_valid_code_point(kfr->first_code_point)) return KATE_E_TEXT;
770 if (!kate_is_valid_code_point(kfr->last_code_point)) return KATE_E_TEXT;
771 if (kfr->first_bitmap<0) return KATE_E_LIMIT;
772 if ((size_t)(kfr->first_bitmap+(kfr->last_code_point-kfr->first_code_point))>=ki->nbitmaps) return KATE_E_LIMIT;
773
774 kate_write32v(kpb,kfr->first_code_point);
775 kate_write32v(kpb,kfr->last_code_point);
776 kate_write32v(kpb,kfr->first_bitmap);
777 kate_warp(kpb);
778
779 return 0;
780 }
781
kate_encode_font_ranges(kate_state * k,kate_packet * kp)782 static int kate_encode_font_ranges(kate_state *k,kate_packet *kp)
783 {
784 kate_pack_buffer *kpb;
785 kate_info *ki;
786 size_t n,l;
787 int ret;
788
789 if (!k || !kp) return KATE_E_INVALID_PARAMETER;
790 if (!k->kes) return KATE_E_INIT;
791
792 kpb=&k->kes->kpb;
793
794 ret=kate_encode_start_header(kpb,0x88);
795 if (ret<0) return ret;
796
797 ki=k->ki;
798 if (!ki) return KATE_E_INIT;
799
800 kate_write32v(kpb,ki->nfont_ranges);
801 for(n=0;n<ki->nfont_ranges;++n) {
802 ret=kate_encode_font_range(ki,ki->font_ranges[n],kpb);
803 if (ret<0) return ret;
804 }
805
806 kate_write32v(kpb,ki->nfont_mappings);
807 for(n=0;n<ki->nfont_mappings;++n) {
808 const kate_font_mapping *kfm=ki->font_mappings[n];
809 kate_write32v(kpb,kfm->nranges);
810 for (l=0;l<kfm->nranges;++l) {
811 const kate_font_range *kfr=kfm->ranges[l];
812 int idx=kate_find_font_range(ki,kfr);
813 if (idx>=0) {
814 kate_pack_write1(kpb,1);
815 kate_write32v(kpb,idx);
816 }
817 else {
818 kate_pack_write1(kpb,0);
819 ret=kate_encode_font_range(ki,kfr,kpb);
820 if (ret<0) return ret;
821 }
822 }
823 }
824
825 kate_warp(kpb);
826
827 return kate_finalize_packet_buffer(kpb,kp,k);
828 }
829
kate_check_granule(kate_state * k,kate_int64_t * granulepos)830 static inline int kate_check_granule(kate_state *k,kate_int64_t *granulepos)
831 {
832 if (*granulepos<k->kes->granulepos) return -1;
833 return 0;
834 }
835
836 #define WRITE_OVERRIDE(kpb,var,def,write) \
837 do \
838 if (ret==0 && (kes->overrides.var!=(def))) { \
839 kate_pack_write1(kpb,1); \
840 write; \
841 } \
842 else kate_pack_write1(kpb,0); \
843 while(0)
844
kate_encode_overrides(kate_state * k,kate_pack_buffer * kpb)845 static int kate_encode_overrides(kate_state *k,kate_pack_buffer *kpb)
846 {
847 kate_encode_state *kes;
848 size_t n;
849 int ret=0;
850
851 if (!k || !kpb) return KATE_E_INVALID_PARAMETER;
852 kes=k->kes;
853 if (!kes) return KATE_E_INIT;
854
855 if (kes->overrides.language
856 || kes->overrides.text_encoding!=k->ki->text_encoding
857 || kes->overrides.text_directionality!=k->ki->text_directionality
858 || kes->overrides.region_index>=0
859 || kes->overrides.region
860 || kes->overrides.style_index>=0
861 || kes->overrides.style
862 || kes->overrides.secondary_style_index>=0
863 || kes->overrides.secondary_style
864 || kes->overrides.font_mapping_index>=0
865 ) {
866 kate_pack_write1(kpb,1);
867 WRITE_OVERRIDE(kpb,text_encoding,k->ki->text_encoding,kate_pack_write(kpb,kes->overrides.text_encoding,8));
868 WRITE_OVERRIDE(kpb,text_directionality,k->ki->text_directionality,kate_pack_write(kpb,kes->overrides.text_directionality,8));
869 WRITE_OVERRIDE(kpb,language,NULL,
870 do {
871 size_t len=strlen(kes->overrides.language);
872 kate_write32v(kpb,len);
873 kate_writebuf(kpb,kes->overrides.language,len);
874 } while(0));
875 WRITE_OVERRIDE(kpb,region_index,-1,kate_write32v(kpb,kes->overrides.region_index));
876 WRITE_OVERRIDE(kpb,region,NULL,ret=kate_encode_region(kes->overrides.region,kpb));
877 WRITE_OVERRIDE(kpb,style_index,-1,kate_write32v(kpb,kes->overrides.style_index));
878 WRITE_OVERRIDE(kpb,style,NULL,ret=kate_encode_style(kes->overrides.style,kpb));
879 WRITE_OVERRIDE(kpb,secondary_style_index,-1,kate_write32v(kpb,kes->overrides.secondary_style_index));
880 WRITE_OVERRIDE(kpb,secondary_style,NULL,ret=kate_encode_style(kes->overrides.secondary_style,kpb));
881 WRITE_OVERRIDE(kpb,font_mapping_index,-1,kate_write32v(kpb,kes->overrides.font_mapping_index));
882 }
883 else kate_pack_write1(kpb,0);
884
885 if (ret==0) {
886 /* bitstream 0.2: add palette, bitmap, markup type */
887 kate_pack_buffer warp;
888 kate_open_warp(&warp);
889 if (kes->overrides.palette_index>=0
890 || kes->overrides.palette
891 || kes->overrides.bitmap_index>=0
892 || kes->overrides.bitmap
893 || kes->overrides.text_markup_type!=k->ki->text_markup_type) {
894 kate_pack_write1(&warp,1);
895 WRITE_OVERRIDE(&warp,palette_index,-1,kate_write32v(&warp,kes->overrides.palette_index));
896 WRITE_OVERRIDE(&warp,palette,NULL,ret=kate_encode_palette(kes->overrides.palette,&warp));
897 WRITE_OVERRIDE(&warp,bitmap_index,-1,kate_write32v(&warp,kes->overrides.bitmap_index));
898 WRITE_OVERRIDE(&warp,bitmap,NULL,ret=kate_encode_bitmap(kes->overrides.bitmap,&warp));
899 WRITE_OVERRIDE(&warp,text_markup_type,k->ki->text_markup_type,kate_pack_write(&warp,kes->overrides.text_markup_type,8));
900 }
901 else kate_pack_write1(&warp,0);
902 kate_close_warp(&warp,kpb);
903 }
904
905 if (ret==0) {
906 /* bitstream 0.4: add bitmaps */
907 kate_pack_buffer warp;
908 kate_open_warp(&warp);
909 kate_write32v(&warp,kes->nbitmaps);
910 for(n=0;n<kes->nbitmaps;++n) {
911 if (kes->bitmaps[n]==NULL) {
912 /* we have an index into the bitmaps header */
913 kate_pack_write1(&warp,1);
914 kate_write32v(&warp,kes->bitmap_indices[n]);
915 }
916 else {
917 /* we have a fully defined bitmap */
918 kate_pack_write1(&warp,0);
919 ret=kate_encode_bitmap(kes->bitmaps[n],&warp);
920 if (ret<0) break;
921 }
922 }
923 kate_close_warp(&warp,kpb);
924 }
925
926 if (ret==0) {
927 /* bitstream 0.6: add metadata */
928 kate_pack_buffer warp;
929 kate_open_warp(&warp);
930 kate_write_metadata(&warp,kes->meta);
931 kate_close_warp(&warp,kpb);
932 }
933
934 kate_warp(kpb);
935
936 return ret;
937 }
938
939 /**
940 \ingroup encoding
941 Encodes a text (which may be NULL) of the given size, starting at t0 and ending at t1.
942 This should always be called when encoding an event, even if the text is NULL.
943 After this is called, the event is fully encoded and cannot be added to anymore.
944 \param k the kate_state to add the text to
945 \param t0 the start time in granule units of the text
946 \param t1 the end time in granule units of the text
947 \param text the text to add (may be NULL)
948 \param sz the length in bytes of the text to add
949 \param kp the packet to encode to
950 \returns 0 success
951 \returns KATE_E_* error
952 */
kate_encode_text_raw_times(kate_state * k,kate_int64_t t0,kate_int64_t t1,const char * text,size_t sz,kate_packet * kp)953 int kate_encode_text_raw_times(kate_state *k,kate_int64_t t0,kate_int64_t t1,const char *text,size_t sz,kate_packet *kp)
954 {
955 kate_pack_buffer *kpb;
956 kate_int64_t start_granulepos;
957 kate_int64_t start;
958 kate_int64_t duration;
959 kate_int64_t backlink;
960 kate_int64_t earliest_t;
961 int ret;
962 size_t n;
963
964 if (!k || !text || !kp) return KATE_E_INVALID_PARAMETER;
965 if (t0<0 || t1<t0) return KATE_E_INVALID_PARAMETER;
966 if (!k->kes) return KATE_E_INIT;
967 if (k->kes->eos) return KATE_E_INIT;
968
969 ret=kate_text_validate(k->kes->overrides.text_encoding,text,sz);
970 if (ret<0) return ret;
971
972 ret=kate_encode_state_trim_events(k->kes,t0);
973 if (ret<0) return ret;
974 ret=kate_encode_state_add_event(k->kes,t0,t1);
975 if (ret<0) return ret;
976 ret=kate_encode_state_get_earliest_event(k->kes,&earliest_t,NULL);
977 if (ret<0) return ret;
978
979 start_granulepos=(earliest_t<<k->ki->granule_shift)|(t0-earliest_t);
980 if (start_granulepos<0) return KATE_E_BAD_GRANULE;
981 if (kate_check_granule(k,&start_granulepos)<0) return KATE_E_BAD_GRANULE;
982
983 start=t0;
984 if (start<0) return KATE_E_BAD_GRANULE;
985 duration=t1-t0;
986 if (duration<0) return KATE_E_BAD_GRANULE;
987 backlink=t0-earliest_t;
988 if (backlink<0) return KATE_E_BAD_GRANULE;
989
990 kpb=&k->kes->kpb;
991 kate_pack_write(kpb,0x00,8);
992
993 kate_write64(kpb,start);
994 kate_write64(kpb,duration);
995 kate_write64(kpb,backlink);
996
997 kate_write32(kpb,sz);
998 kate_writebuf(kpb,text,sz);
999
1000 if (k->kes->id>=0) {
1001 kate_pack_write1(kpb,1);
1002 kate_write32v(kpb,k->kes->id);
1003 }
1004 else {
1005 kate_pack_write1(kpb,0);
1006 }
1007
1008 if (k->kes->nmotions) {
1009 kate_pack_write1(kpb,1);
1010 kate_write32v(kpb,k->kes->nmotions);
1011 for (n=0;n<k->kes->nmotions;++n) {
1012 if (k->kes->motions[n]==NULL) {
1013 /* we have an index into the motions header */
1014 kate_pack_write1(kpb,1);
1015 kate_write32v(kpb,k->kes->motion_indices[n]);
1016 }
1017 else {
1018 /* we have a fully defined motion */
1019 kate_pack_write1(kpb,0);
1020 ret=kate_encode_motion(k->ki,k->kes->motions[n],kpb);
1021 if (ret<0) return ret;
1022 }
1023 }
1024 }
1025 else kate_pack_write1(kpb,0);
1026
1027 kate_encode_overrides(k,kpb);
1028
1029 if (start_granulepos>k->kes->furthest_granule) k->kes->furthest_granule=start_granulepos;
1030
1031 k->kes->granulepos=start_granulepos;
1032 ret=kate_finalize_packet_buffer(kpb,kp,k);
1033 if (ret<0) return ret;
1034
1035 /* save the packet in case we have to repeat it */
1036 ret=kate_encode_state_save_event_buffer(k->kes,kp->nbytes,kp->data);
1037 if (ret<0) return ret;
1038
1039 return 0;
1040 }
1041
1042 /**
1043 \ingroup encoding
1044 Encodes a text (which may be NULL) of the given size, starting at t0 and ending at t1.
1045 This should always be called when encoding an event, even if the text is NULL.
1046 After this is called, the event is fully encoded and cannot be added to anymore.
1047 \param k the kate_state to add the text to
1048 \param t0 the start time in seconds of the text
1049 \param t1 the end time in seconds of the text
1050 \param text the text to add (may be NULL)
1051 \param sz the length in bytes of the text to add
1052 \param kp the packet to encode to
1053 \returns 0 success
1054 \returns KATE_E_* error
1055 */
kate_encode_text(kate_state * k,kate_float t0,kate_float t1,const char * text,size_t sz,kate_packet * kp)1056 int kate_encode_text(kate_state *k,kate_float t0,kate_float t1,const char *text,size_t sz,kate_packet *kp)
1057 {
1058 if (!k) return KATE_E_INVALID_PARAMETER;
1059 return kate_encode_text_raw_times(k,kate_duration_granule(k->ki,t0),kate_duration_granule(k->ki,t1),text,sz,kp);
1060 }
1061
1062 /**
1063 \ingroup encoding
1064 Emits a keepalive packet, to help with seeking.
1065 \param k the kate_state to encode to
1066 \param t the timestamp (in granule rate units) for the keepalive packet
1067 \param kp the packet to encode to
1068 \returns 0 success
1069 \returns KATE_E_* error
1070 */
kate_encode_keepalive_raw_times(kate_state * k,kate_int64_t t,kate_packet * kp)1071 int kate_encode_keepalive_raw_times(kate_state *k,kate_int64_t t,kate_packet *kp)
1072 {
1073 kate_pack_buffer *kpb;
1074 kate_int64_t granulepos;
1075 kate_int64_t earliest_t;
1076 int ret;
1077
1078 if (!k || !kp) return KATE_E_INVALID_PARAMETER;
1079 if (!k->kes) return KATE_E_INIT;
1080 if (k->kes->eos) return KATE_E_INIT;
1081
1082 ret=kate_encode_state_trim_events(k->kes,t);
1083 if (ret<0) return ret;
1084
1085 ret=kate_encode_state_get_earliest_event(k->kes,&earliest_t,NULL);
1086 if (ret==KATE_E_NOT_FOUND) {
1087 /* if there are no live events yet, base from now */
1088 earliest_t=t;
1089 ret=0;
1090 }
1091 if (ret<0) return ret;
1092
1093 granulepos=(earliest_t<<k->ki->granule_shift)|(t-earliest_t);
1094 if (granulepos<0) return KATE_E_BAD_GRANULE;
1095
1096 if (kate_check_granule(k,&granulepos)<0) return KATE_E_BAD_GRANULE;
1097 k->kes->granulepos=granulepos;
1098
1099 ret=kate_encode_state_add_event(k->kes,t,t);
1100 if (ret<0) return ret;
1101
1102 kpb=&k->kes->kpb;
1103 kate_pack_write(kpb,0x01,8);
1104
1105 return kate_finalize_packet_buffer(kpb,kp,k);
1106 }
1107
1108 /**
1109 \ingroup encoding
1110 Emits a keepalive packet, to help with seeking.
1111 \param k the kate_state to encode to
1112 \param t the timestamp for the keepalive packet
1113 \param kp the packet to encode to
1114 \returns 0 success
1115 \returns KATE_E_* error
1116 */
kate_encode_keepalive(kate_state * k,kate_float t,kate_packet * kp)1117 int kate_encode_keepalive(kate_state *k,kate_float t,kate_packet *kp)
1118 {
1119 if (!k) return KATE_E_INVALID_PARAMETER;
1120 return kate_encode_keepalive_raw_times(k,kate_duration_granule(k->ki,t),kp);
1121 }
1122
1123 /**
1124 \ingroup encoding
1125 Emits a repeat packet, to help with seeking.
1126 The first active event at time t, if it or its latest repeat (whichever is
1127 the latest) was emitted more than threshold ago, of if threshold is zero,
1128 will have a repeat packet emitted.
1129 kate_encode_repeat is designed to be called in a loop, as it will return a
1130 repeat packet for only one event even if more than one event needs a repeat.
1131 When the returned value is 0, no more events need repeating at time t.
1132 \param k the kate_state to encode to
1133 \param t the timestamp (in granule rate units) for the the packets, if any
1134 \param threshold the time threshold (in granule rate units) at which to emit
1135 a repeat packets (zero to force a repeat)
1136 \param kp the packet to encode to
1137 \returns 0 success, and no repeat packet was encoded
1138 \returns 1 success, and a repeat packet was encoded
1139 \returns KATE_E_* error
1140 */
kate_encode_repeat_raw_times(kate_state * k,kate_int64_t t,kate_int64_t threshold,kate_packet * kp)1141 int kate_encode_repeat_raw_times(kate_state *k,kate_int64_t t,kate_int64_t threshold,kate_packet *kp)
1142 {
1143 kate_int64_t earliest_t;
1144 kate_int64_t granulepos;
1145 int ret;
1146
1147 if (!k || !kp) return KATE_E_INVALID_PARAMETER;
1148 if (threshold<0) return KATE_E_INVALID_PARAMETER;
1149 if (!k->kes) return KATE_E_INIT;
1150 if (k->kes->eos) return KATE_E_INIT;
1151
1152 ret=kate_encode_state_trim_events(k->kes,t);
1153 if (ret<0) return ret;
1154 ret=kate_encode_state_get_earliest_event(k->kes,&earliest_t,NULL);
1155 if (ret==KATE_E_NOT_FOUND) {
1156 /* if there are no live events yet, base from now */
1157 earliest_t=t;
1158 }
1159 else if (ret<0) {
1160 return ret;
1161 }
1162 granulepos=(earliest_t<<k->ki->granule_shift)|(t-earliest_t);
1163 if (granulepos<0) return KATE_E_BAD_GRANULE;
1164
1165 if (kate_check_granule(k,&granulepos)<0) return KATE_E_BAD_GRANULE;
1166
1167 ret=kate_encode_state_get_repeat(k->kes,t,threshold,kp);
1168 if (ret>0) {
1169 /* if we encoded a repeat, update encoding state granpos */
1170 k->kes->granulepos=granulepos;
1171 }
1172 return ret;
1173 }
1174
1175 /**
1176 \ingroup encoding
1177 Emits a repeat packet, to help with seeking.
1178 The first active event at time t, if it or its latest repeat (whichever is
1179 the latest) was emitted more than threshold ago, of if threshold is zero,
1180 will have a repeat packet emitted.
1181 kate_encode_repeat is designed to be called in a loop, as it will return a
1182 repeat packet for only one event even if more than one event needs a repeat.
1183 When the returned value is 0, no more events need repeating at time t.
1184 \param k the kate_state to encode to
1185 \param t the timestamp for the the packets, if any
1186 \param threshold the time threshold at which to emit a repeat packets (zero to force a repeat)
1187 \param kp the packet to encode to
1188 \returns 0 success, and no repeat packet was encoded
1189 \returns 1 success, and a repeat packet was encoded
1190 \returns KATE_E_* error
1191 */
kate_encode_repeat(kate_state * k,kate_float t,kate_float threshold,kate_packet * kp)1192 int kate_encode_repeat(kate_state *k,kate_float t,kate_float threshold,kate_packet *kp)
1193 {
1194 if (!k) return KATE_E_INVALID_PARAMETER;
1195 return kate_encode_repeat_raw_times(k,kate_duration_granule(k->ki,t),kate_duration_granule(k->ki,threshold),kp);
1196 }
1197
1198 /**
1199 \ingroup encoding
1200 Finalizes the currently encoded stream.
1201 No more events may be added after this is called.
1202 \param k the kate_state to encode to
1203 \param t the timestamp (in granule rate units) for the end (if negative,
1204 the end time of the last event will be used)
1205 \param kp the packet to encode to
1206 \returns 0 success
1207 \returns KATE_E_* error
1208 */
kate_encode_finish_raw_times(kate_state * k,kate_int64_t t,kate_packet * kp)1209 int kate_encode_finish_raw_times(kate_state *k,kate_int64_t t,kate_packet *kp)
1210 {
1211 kate_pack_buffer *kpb;
1212 kate_int64_t granulepos;
1213 int ret;
1214
1215 if (!k || !kp) return KATE_E_INVALID_PARAMETER;
1216 if (!k->kes) return KATE_E_INIT;
1217 if (k->kes->eos) return KATE_E_INIT;
1218
1219 ret=kate_encode_state_trim_events(k->kes,t);
1220 if (ret<0) return ret;
1221
1222 if (t<0) {
1223 ret=kate_encode_state_get_latest_event(k->kes,NULL,&t);
1224 if (ret==KATE_E_NOT_FOUND) {
1225 /* if nothing was encoded, it is still a valid stream */
1226 t=0;
1227 ret=0;
1228 }
1229 if (ret<0) return ret;
1230 }
1231
1232 granulepos=t<<k->ki->granule_shift;
1233 if (granulepos<0) return KATE_E_BAD_GRANULE;
1234
1235 if (kate_check_granule(k,&granulepos)<0) return KATE_E_BAD_GRANULE;
1236 k->kes->granulepos=granulepos;
1237
1238 kpb=&k->kes->kpb;
1239 kate_pack_write(kpb,0x7f,8);
1240
1241 k->kes->eos=1;
1242
1243 return kate_finalize_packet_buffer(kpb,kp,k);
1244 }
1245
1246 /**
1247 \ingroup encoding
1248 Finalizes the currently encoded stream.
1249 No more events may be added after this is called.
1250 \param k the kate_state to encode to
1251 \param t the timestamp for the end (if negative, the end time of the last event will be used)
1252 \param kp the packet to encode to
1253 \returns 0 success
1254 \returns KATE_E_* error
1255 */
kate_encode_finish(kate_state * k,kate_float t,kate_packet * kp)1256 int kate_encode_finish(kate_state *k,kate_float t,kate_packet *kp)
1257 {
1258 if (!k) return KATE_E_INVALID_PARAMETER;
1259 return kate_encode_finish_raw_times(k,kate_duration_granule(k->ki,t),kp);
1260 }
1261
1262 /**
1263 \ingroup encoding
1264 Encodes a header.
1265 This should be repeatedly called at the beginning of encoding, until a
1266 positive value is returned, marking the encoding of the last header.
1267 \param k the kate_state to encode to
1268 \param kc the list of comments to add to the headers
1269 \param kp the packet to encode to
1270 \returns 0 success
1271 \returns 1 success, and all headers have been encoded
1272 \returns KATE_E_* error
1273 */
kate_encode_headers(kate_state * k,kate_comment * kc,kate_packet * kp)1274 int kate_encode_headers(kate_state *k,kate_comment *kc,kate_packet *kp)
1275 {
1276 if (!k || !kc || !kp) return KATE_E_INVALID_PARAMETER;
1277 if (!k->kes) return KATE_E_INIT;
1278 if (k->kes->eos) return KATE_E_INIT;
1279
1280 switch (k->kes->packetno+1) {
1281 case 0: return kate_encode_info(k,kp);
1282 case 1: return kate_encode_comment(k,kc,kp);
1283 case 2: return kate_encode_regions(k,kp);
1284 case 3: return kate_encode_styles(k,kp);
1285 case 4: return kate_encode_curves(k,kp);
1286 case 5: return kate_encode_motions(k,kp);
1287 case 6: return kate_encode_palettes(k,kp);
1288 case 7: return kate_encode_bitmaps(k,kp);
1289 case 8: return kate_encode_font_ranges(k,kp);
1290 case 9: return 1;
1291 default: return KATE_E_INVALID_PARAMETER;
1292 }
1293 }
1294
1295 /**
1296 \ingroup encoding
1297 Adds a motion to the currently encoded event.
1298 If destroy is set, the motion will be automatically destroyed after the current
1299 event has been encoded.
1300 \param k the kate_state to add the motion to
1301 \param km the motion to add
1302 \param destroy if true, the motion will be destroyed when the event is fully encoded
1303 \returns 0 success
1304 \returns KATE_E_* error
1305 */
kate_encode_add_motion(kate_state * k,kate_motion * km,int destroy)1306 int kate_encode_add_motion(kate_state *k,kate_motion *km,int destroy)
1307 {
1308 if (!k || !km) return KATE_E_INVALID_PARAMETER;
1309 if (!k->kes) return KATE_E_INIT;
1310
1311 return kate_encode_state_add_motion(k->kes,km,destroy);
1312 }
1313
1314 /**
1315 \ingroup encoding
1316 Adds a motion to the currently encoded event by its index into the list of predefined motions.
1317 \param k the kate_state to add the motion to
1318 \param motion the index of the motion to add
1319 \returns 0 success
1320 \returns KATE_E_* error
1321 */
kate_encode_add_motion_index(kate_state * k,size_t motion)1322 int kate_encode_add_motion_index(kate_state *k,size_t motion)
1323 {
1324 if (!k) return KATE_E_INVALID_PARAMETER;
1325 if (!k->ki) return KATE_E_INIT;
1326 if (motion>=k->ki->nmotions) return KATE_E_INVALID_PARAMETER;
1327 if (!k->kes) return KATE_E_INIT;
1328
1329 return kate_encode_state_add_motion_index(k->kes,motion);
1330 }
1331
1332 /**
1333 \ingroup encoding
1334 Adds a bitmap to the currently encoded event.
1335 \param k the kate_state to add the bitmap to
1336 \param kb the bitmap to add
1337 \returns 0 success
1338 \returns KATE_E_* error
1339 */
kate_encode_add_bitmap(kate_state * k,const kate_bitmap * kb)1340 int kate_encode_add_bitmap(kate_state *k,const kate_bitmap *kb)
1341 {
1342 if (!k || !kb) return KATE_E_INVALID_PARAMETER;
1343 if (!k->kes) return KATE_E_INIT;
1344
1345 return kate_encode_state_add_bitmap(k->kes,kb);
1346 }
1347
1348 /**
1349 \ingroup encoding
1350 Adds a bitmap to the currently encoded event by its index into the list of predefined bitmaps.
1351 \param k the kate_state to add the bitmap to
1352 \param bitmap the index of the bitmap to add
1353 \returns 0 success
1354 \returns KATE_E_* error
1355 */
kate_encode_add_bitmap_index(kate_state * k,size_t bitmap)1356 int kate_encode_add_bitmap_index(kate_state *k,size_t bitmap)
1357 {
1358 if (!k) return KATE_E_INVALID_PARAMETER;
1359 if (!k->ki) return KATE_E_INIT;
1360 if (bitmap>=k->ki->nbitmaps) return KATE_E_INVALID_PARAMETER;
1361 if (!k->kes) return KATE_E_INIT;
1362
1363 return kate_encode_state_add_bitmap_index(k->kes,bitmap);
1364 }
1365
1366 /**
1367 \ingroup encoding
1368 Sets the region the event should be displayed in, by its index in the predefined regions list.
1369 \param k the kate_state to encode to
1370 \param region the index of the predefined region to use
1371 \returns 0 success
1372 \returns KATE_E_* error
1373 */
kate_encode_set_region_index(kate_state * k,size_t region)1374 int kate_encode_set_region_index(kate_state *k,size_t region)
1375 {
1376 if (!k) return KATE_E_INVALID_PARAMETER;
1377 if (!k->kes) return KATE_E_INIT;
1378 if (!k->ki) return KATE_E_INIT;
1379 if (region>=k->ki->nregions) return KATE_E_INVALID_PARAMETER;
1380 if (k->kes->overrides.region) return KATE_E_INIT;
1381 k->kes->overrides.region_index=region;
1382 return 0;
1383 }
1384
1385 /**
1386 \ingroup encoding
1387 Sets the region the event should be displayed in.
1388 \param k the kate_state to encode to
1389 \param kr the region to use
1390 \returns 0 success
1391 \returns KATE_E_* error
1392 */
kate_encode_set_region(kate_state * k,const kate_region * kr)1393 int kate_encode_set_region(kate_state *k,const kate_region *kr)
1394 {
1395 if (!k || !kr) return KATE_E_INVALID_PARAMETER;
1396 if (!k->kes) return KATE_E_INIT;
1397 if (!k->ki) return KATE_E_INIT;
1398 if (k->kes->overrides.region_index>=0) return KATE_E_INIT;
1399 k->kes->overrides.region=kr;
1400 return 0;
1401 }
1402
1403 /**
1404 \ingroup encoding
1405 Sets the style the event should be displayed with, by its index in the predefined styles list.
1406 \param k the kate_state to encode to
1407 \param style the index of the predefined style to use
1408 \returns 0 success
1409 \returns KATE_E_* error
1410 */
kate_encode_set_style_index(kate_state * k,size_t style)1411 int kate_encode_set_style_index(kate_state *k,size_t style)
1412 {
1413 if (!k) return KATE_E_INVALID_PARAMETER;
1414 if (!k->kes) return KATE_E_INIT;
1415 if (!k->ki) return KATE_E_INIT;
1416 if (style>=k->ki->nstyles) return KATE_E_INVALID_PARAMETER;
1417 if (k->kes->overrides.style) return KATE_E_INIT;
1418 k->kes->overrides.style_index=style;
1419 return 0;
1420 }
1421
1422 /**
1423 \ingroup encoding
1424 Sets the style the event should be displayed with.
1425 \param k the kate_state to encode to
1426 \param ks the style to use
1427 \returns 0 success
1428 \returns KATE_E_* error
1429 */
kate_encode_set_style(kate_state * k,const kate_style * ks)1430 int kate_encode_set_style(kate_state *k,const kate_style *ks)
1431 {
1432 if (!k || !ks) return KATE_E_INVALID_PARAMETER;
1433 if (!k->kes) return KATE_E_INIT;
1434 if (!k->ki) return KATE_E_INIT;
1435 if (k->kes->overrides.style_index>=0) return KATE_E_INIT;
1436 k->kes->overrides.style=ks;
1437 return 0;
1438 }
1439
1440 /**
1441 \ingroup encoding
1442 Sets the secondary style the event should be displayed with, by its index in the predefined styles list.
1443 \param k the kate_state to encode to
1444 \param style the index of the predefined style to use
1445 \returns 0 success
1446 \returns KATE_E_* error
1447 */
kate_encode_set_secondary_style_index(kate_state * k,size_t style)1448 int kate_encode_set_secondary_style_index(kate_state *k,size_t style)
1449 {
1450 if (!k) return KATE_E_INVALID_PARAMETER;
1451 if (!k->kes) return KATE_E_INIT;
1452 if (!k->ki) return KATE_E_INIT;
1453 if (style>=k->ki->nstyles) return KATE_E_INVALID_PARAMETER;
1454 if (k->kes->overrides.secondary_style) return KATE_E_INIT;
1455 k->kes->overrides.secondary_style_index=style;
1456 return 0;
1457 }
1458
1459 /**
1460 \ingroup encoding
1461 Sets the secondary style the event should be displayed with.
1462 \param k the kate_state to encode to
1463 \param ks the style to use
1464 \returns 0 success
1465 \returns KATE_E_* error
1466 */
kate_encode_set_secondary_style(kate_state * k,const kate_style * ks)1467 int kate_encode_set_secondary_style(kate_state *k,const kate_style *ks)
1468 {
1469 if (!k || !ks) return KATE_E_INVALID_PARAMETER;
1470 if (!k->kes) return KATE_E_INIT;
1471 if (!k->ki) return KATE_E_INIT;
1472 if (k->kes->overrides.secondary_style_index>=0) return KATE_E_INIT;
1473 k->kes->overrides.secondary_style=ks;
1474 return 0;
1475 }
1476
1477 /**
1478 \ingroup encoding
1479 Sets the palette the event should use for its bitmap, by its index in the predefined palette list.
1480 \param k the kate_state to encode to
1481 \param palette the index of the predefined palette to use
1482 \returns 0 success
1483 \returns KATE_E_* error
1484 */
kate_encode_set_palette_index(kate_state * k,size_t palette)1485 int kate_encode_set_palette_index(kate_state *k,size_t palette)
1486 {
1487 if (!k) return KATE_E_INVALID_PARAMETER;
1488 if (!k->kes) return KATE_E_INIT;
1489 if (!k->ki) return KATE_E_INIT;
1490 if (palette>=k->ki->npalettes) return KATE_E_INVALID_PARAMETER;
1491 if (k->kes->overrides.palette) return KATE_E_INIT;
1492 k->kes->overrides.palette_index=palette;
1493 return 0;
1494 }
1495
1496 /**
1497 \ingroup encoding
1498 Adds a palette to the currently encoded event.
1499 \param k the kate_state to set the palette for
1500 \param kp the palette to set
1501 \returns 0 success
1502 \returns KATE_E_* error
1503 */
kate_encode_set_palette(kate_state * k,const kate_palette * kp)1504 int kate_encode_set_palette(kate_state *k,const kate_palette *kp)
1505 {
1506 if (!k || !kp) return KATE_E_INVALID_PARAMETER;
1507 if (!k->kes) return KATE_E_INIT;
1508 if (!k->ki) return KATE_E_INIT;
1509 if (k->kes->overrides.palette_index>=0) return KATE_E_INIT;
1510 k->kes->overrides.palette=kp;
1511 return 0;
1512 }
1513
1514 /**
1515 \ingroup encoding
1516 Sets the bitmap the event should use, by its index in the predefined bitmap list.
1517 \param k the kate_state to encode to
1518 \param bitmap the index of the predefined bitmap to use
1519 \returns 0 success
1520 \returns KATE_E_* error
1521 */
kate_encode_set_bitmap_index(kate_state * k,size_t bitmap)1522 int kate_encode_set_bitmap_index(kate_state *k,size_t bitmap)
1523 {
1524 if (!k) return KATE_E_INVALID_PARAMETER;
1525 if (!k->kes) return KATE_E_INIT;
1526 if (!k->ki) return KATE_E_INIT;
1527 if (bitmap>=k->ki->nbitmaps) return KATE_E_INVALID_PARAMETER;
1528 if (k->kes->overrides.bitmap) return KATE_E_INIT;
1529 k->kes->overrides.bitmap_index=bitmap;
1530 return 0;
1531 }
1532
1533 /**
1534 \ingroup encoding
1535 Adds a bitmap to the currently encoded event.
1536 \param k the kate_state to set the bitmap for
1537 \param kb the bitmap to set
1538 \returns 0 success
1539 \returns KATE_E_* error
1540 */
kate_encode_set_bitmap(kate_state * k,const kate_bitmap * kb)1541 int kate_encode_set_bitmap(kate_state *k,const kate_bitmap *kb)
1542 {
1543 if (!k || !kb) return KATE_E_INVALID_PARAMETER;
1544 if (!k->kes) return KATE_E_INIT;
1545 if (!k->ki) return KATE_E_INIT;
1546 if (k->kes->overrides.bitmap_index>=0) return KATE_E_INIT;
1547 k->kes->overrides.bitmap=kb;
1548 return 0;
1549 }
1550
1551 /**
1552 \ingroup encoding
1553 Sets the font mapping this event's text should be displayed with, by its index into the predefined
1554 font mappings list.
1555 \param k the kate_state to encode to
1556 \param font_mapping the font mapping to use
1557 \returns 0 success
1558 \returns KATE_E_* error
1559 */
kate_encode_set_font_mapping_index(kate_state * k,size_t font_mapping)1560 int kate_encode_set_font_mapping_index(kate_state *k,size_t font_mapping)
1561 {
1562 if (!k) return KATE_E_INVALID_PARAMETER;
1563 if (!k->kes) return KATE_E_INIT;
1564 if (!k->ki) return KATE_E_INIT;
1565 if (font_mapping>=k->ki->nfont_mappings) return KATE_E_INVALID_PARAMETER;
1566 k->kes->overrides.font_mapping_index=font_mapping;
1567 return 0;
1568 }
1569
1570 /**
1571 \ingroup encoding
1572 Sets the character encoding used in this event's text, overriding the default character encoding.
1573 \param k the kate_state to encode to
1574 \param text_encoding the text encoding for this event's text
1575 \returns 0 success
1576 \returns KATE_E_* error
1577 */
kate_encode_set_text_encoding(kate_state * k,kate_text_encoding text_encoding)1578 int kate_encode_set_text_encoding(kate_state *k,kate_text_encoding text_encoding)
1579 {
1580 if (!k) return KATE_E_INVALID_PARAMETER;
1581 if (!k->kes) return KATE_E_INIT;
1582 k->kes->overrides.text_encoding=text_encoding;
1583 return 0;
1584 }
1585
1586 /**
1587 \ingroup encoding
1588 Sets the text directionality used in this event's text, overriding the default text directionality.
1589 \param k the kate_state to encode to
1590 \param text_directionality the text directionality for this event's text
1591 \returns 0 success
1592 \returns KATE_E_* error
1593 */
kate_encode_set_text_directionality(kate_state * k,kate_text_directionality text_directionality)1594 int kate_encode_set_text_directionality(kate_state *k,kate_text_directionality text_directionality)
1595 {
1596 if (!k) return KATE_E_INVALID_PARAMETER;
1597 if (!k->kes) return KATE_E_INIT;
1598 k->kes->overrides.text_directionality=text_directionality;
1599 return 0;
1600 }
1601
1602 /**
1603 \ingroup encoding
1604 This function is obsolete and should not be used. An event identifier is automatically
1605 generated for each event.
1606 It used to set a unique identifier for the currently encoded event, so it can be referred
1607 to later, but not does nothing.
1608 \param k the kate_state to encode to
1609 \param id unused
1610 \returns KATE_E_* error
1611 */
kate_encode_set_id(kate_state * k,kate_int32_t id)1612 int kate_encode_set_id(kate_state *k,kate_int32_t id)
1613 {
1614 if (!k) return KATE_E_INVALID_PARAMETER;
1615 if (!k->kes) return KATE_E_INIT;
1616
1617 (void)id;
1618
1619 return KATE_E_IMPL;
1620 }
1621
1622 /**
1623 \ingroup encoding
1624 Sets the language used in this event's text, overriding the default language
1625 \param k the kate_state to encode to
1626 \param language the language for this event's text - may be NULL (use the default language)
1627 \returns 0 success
1628 \returns KATE_E_* error
1629 */
kate_encode_set_language(kate_state * k,const char * language)1630 int kate_encode_set_language(kate_state *k,const char *language) /* language can be NULL */
1631 {
1632 size_t len;
1633 char *l=NULL;
1634
1635 if (!k) return KATE_E_INVALID_PARAMETER;
1636 if (!k->kes) return KATE_E_INIT;
1637
1638 if (language) {
1639 len=strlen(language);
1640 l=(char*)kate_malloc(len+1);
1641 if (!l) return KATE_E_OUT_OF_MEMORY;
1642 memcpy(l,language,len+1);
1643 }
1644
1645 if (k->kes->overrides.language) kate_free(k->kes->overrides.language);
1646 k->kes->overrides.language=l;
1647
1648 return 0;
1649 }
1650
1651 /**
1652 \ingroup encoding
1653 Sets the markup type of this event
1654 \param k the kate_state to encode to
1655 \param text_markup_type the markup type of this event
1656 \returns 0 success
1657 \returns KATE_E_* error
1658 */
kate_encode_set_markup_type(kate_state * k,int text_markup_type)1659 int kate_encode_set_markup_type(kate_state *k,int text_markup_type)
1660 {
1661 if (!k) return KATE_E_INVALID_PARAMETER;
1662 if (!k->kes) return KATE_E_INIT;
1663
1664 k->kes->overrides.text_markup_type=text_markup_type;
1665
1666 return 0;
1667 }
1668
1669 /**
1670 \ingroup encoding
1671 Adds a set of metadata to this event
1672 \param k the kate_state to encode to
1673 \param km the metadata to add (a reference is taken, km must be live until the event is encoded)
1674 \returns 0 success
1675 \returns KATE_E_* error
1676 */
kate_encode_add_meta(kate_state * k,const kate_meta * km)1677 int kate_encode_add_meta(kate_state *k,const kate_meta *km)
1678 {
1679 if (!k || !km) return KATE_E_INVALID_PARAMETER;
1680 if (!k->kes) return KATE_E_INIT;
1681
1682 return kate_encode_state_add_meta(k->kes,km);
1683 }
1684
1685 /**
1686 \ingroup encoding
1687 Adds a set of metadata to this event
1688 \param k the kate_state to encode to
1689 \param km the metadata to add (will become invalid after this call returns successfully)
1690 \returns 0 success
1691 \returns KATE_E_* error
1692 */
kate_encode_merge_meta(kate_state * k,kate_meta * km)1693 int kate_encode_merge_meta(kate_state *k,kate_meta *km)
1694 {
1695 if (!k || !km) return KATE_E_INVALID_PARAMETER;
1696 if (!k->kes) return KATE_E_INIT;
1697
1698 return kate_encode_state_merge_meta(k->kes,km);
1699 }
1700
1701 /**
1702 \ingroup encoding
1703 Retrieves the current granulepos
1704 \param k the kate_state to encode to
1705 \returns >=0 the current granulepos (the one of the last encoded packet)
1706 \returns KATE_E_* error
1707 */
kate_encode_get_granule(const kate_state * k)1708 kate_int64_t kate_encode_get_granule(const kate_state *k)
1709 {
1710 if (!k) return KATE_E_INVALID_PARAMETER;
1711 if (!k->kes) return KATE_E_INIT;
1712
1713 return k->kes->granulepos;
1714 }
1715
1716