- Main Page
- Related Pages
- Data Structures
- Files
- Examples

- File List

# /home/v/Kate/WORK_TREES/kate-sorceress-backup/v/src/kate/src/kate_encode.c

    00001 /* Copyright (C) 2008 Vincent Penquerc'h.
    00002    This file is part of the Kate codec library.
    00003    Written by Vincent Penquerc'h.
    00004
    00005    Use, distribution and reproduction of this library is governed
    00006    by a BSD style source license included with this source in the
    00007    file 'COPYING'. Please read these terms before distributing. */
    00008
    00009
    00010 #define KATE_INTERNAL
    00011 #include "kate_internal.h"
    00012
    00013 #ifdef HAVE_STDLIB_H
    00014 #include <stdlib.h>
    00015 #endif
    00016 #ifdef HAVE_STRING_H
    00017 #include <string.h>
    00018 #endif
    00019 #include "kate/kate.h"
    00020 #include "kate_encode_state.h"
    00021 #include "kate_fp.h"
    00022 #include "kate_rle.h"
    00023
    00024 #define NUM_HEADERS 9
    00025
    00035 int kate_encode_init(kate_state *k,kate_info *ki)
    00036 {
    00037   if (!k || !ki) return KATE_E_INVALID_PARAMETER;
    00038
    00039   k->ki=ki;
    00040   k->kds=NULL;
    00041   k->ki->num_headers=NUM_HEADERS;
    00042   k->kes=kate_encode_state_create(ki);
    00043   if (!k->kes) return KATE_E_OUT_OF_MEMORY;
    00044
    00045   return 0;
    00046 }
    00047
    00048 static inline void kate_pack_write1(kate_pack_buffer *kpb,long value)
    00049 {
    00050   kate_pack_write(kpb,value,1);
    00051 }
    00052
    00053 static void kate_writebuf(kate_pack_buffer *kpb,const char *s,int len)
    00054 {
    00055   while (len--) kate_pack_write(kpb,*s++,8);
    00056 }
    00057
    00058 static void kate_write32(kate_pack_buffer *kpb,kate_int32_t v)
    00059 {
    00060   kate_pack_write(kpb,v&0xff,8);
    00061   v>>=8;
    00062   kate_pack_write(kpb,v&0xff,8);
    00063   v>>=8;
    00064   kate_pack_write(kpb,v&0xff,8);
    00065   v>>=8;
    00066   kate_pack_write(kpb,v&0xff,8);
    00067 }
    00068
    00069 static void kate_write32v(kate_pack_buffer *kpb,kate_int32_t v)
    00070 {
    00071   if (v>=0 && v<=14) {
    00072     kate_pack_write(kpb,v,4);
    00073   }
    00074   else {
    00075     int bits=0;
    00076     kate_int32_t tmp;
    00077     kate_pack_write(kpb,15,4);
    00078     if (v&0x80000000) {
    00079       kate_pack_write1(kpb,1);
    00080       v=-v;
    00081     }
    00082     else {
    00083       kate_pack_write1(kpb,0);
    00084     }
    00085     tmp=v;
    00086     while (tmp) {
    00087       ++bits;
    00088       tmp>>=1;
    00089     }
    00090     if (bits==0) bits=1;
    00091     kate_pack_write(kpb,bits-1,5);
    00092     kate_pack_write(kpb,v,bits);
    00093   }
    00094 }
    00095
    00096 static void kate_write64(kate_pack_buffer *kpb,kate_int64_t v)
    00097 {
    00098   kate_write32(kpb,(kate_int32_t)v);
    00099   v>>=32;
    00100   kate_write32(kpb,(kate_int32_t)v);
    00101 }
    00102
    00103 static void kate_open_warp(kate_pack_buffer *warp)
    00104 {
    00105   kate_pack_writeinit(warp);
    00106 }
    00107
    00108 static void kate_close_warp(kate_pack_buffer *warp,kate_pack_buffer *kpb)
    00109 {
    00110   int bits=kate_pack_bits(warp);
    00111   unsigned char *buffer=kate_pack_get_buffer(warp);
    00112   kate_write32v(kpb,bits);
    00113   while (bits>0) {
    00114     kate_pack_writecopy(kpb,buffer,bits>32?32:bits);
    00115     buffer+=32/8;
    00116     bits-=32;
    00117   }
    00118   kate_pack_writeclear(warp);
    00119 }
    00120
    00121 static void kate_warp(kate_pack_buffer *kpb)
    00122 {
    00123   kate_pack_buffer warp;
    00124   kate_open_warp(&warp);
    00125   kate_close_warp(&warp,kpb);
    00126 }
    00127
    00128 static int kate_finalize_packet_buffer(kate_pack_buffer *kpb,kate_packet *kp,kate_state *k)
    00129 {
    00130   if (!kpb || !kp || !k) return KATE_E_INVALID_PARAMETER;
    00131   if (!k->kes) return KATE_E_INIT;
    00132
    00133   /* fill up any remaining bits in the last byte with zeros */
    00134   kate_pack_writealign(kpb);
    00135
    00136   kp->nbytes=kate_pack_bytes(kpb);
    00137   kp->data=kate_malloc(kp->nbytes);
    00138   if (!kp->data) return KATE_E_OUT_OF_MEMORY;
    00139
    00140   memcpy(kp->data,kate_pack_get_buffer(kpb),kp->nbytes);
    00141
    00142   /* reset the buffer so we're ready for next packet */
    00143   kate_pack_writeclear(kpb);
    00144   kate_pack_writeinit(kpb);
    00145
    00146   ++k->kes->packetno;
    00147
    00148   /* clear any overrides */
    00149   return kate_encode_state_clear_overrides(k->kes);
    00150 }
    00151
    00152 static int kate_encode_start_header(kate_pack_buffer *kpb,int headerid)
    00153 {
    00154   if (!kpb || !(headerid&0x80)) return KATE_E_INVALID_PARAMETER;
    00155
    00156   kate_pack_write(kpb,headerid,8);
    00157   kate_writebuf(kpb,"kate\0\0\0",7);
    00158   kate_pack_write(kpb,0,8); /* reserved - 0 */
    00159
    00160   return 0;
    00161 }
    00162
    00163 static int kate_encode_write_canvas_size(kate_pack_buffer *kpb,size_t size)
    00164 {
    00165   size_t base=size;
    00166   size_t shift=0;
    00167
    00168   if (!kpb) return KATE_E_INVALID_PARAMETER;
    00169
    00170   while (base&~((1<<12)-1)) {
    00171     /* we have a high bit we can't fit, increase shift if we wouldn't lose low bits */
    00172     if ((size>>shift)&1) return KATE_E_LIMIT;
    00173     ++shift;
    00174     base>>=1;
    00175   }
    00176   if (shift>=16) return KATE_E_LIMIT;
    00177
    00178   /* the size can be represented in our encoding */
    00179   kate_pack_write(kpb,shift,4);
    00180   kate_pack_write(kpb,base&0x0f,4);
    00181   kate_pack_write(kpb,base>>4,8);
    00182
    00183   return 0;
    00184 }
    00185
    00186 static int kate_encode_info(kate_state *k,kate_packet *kp)
    00187 {
    00188   kate_pack_buffer *kpb;
    00189   kate_info *ki;
    00190   size_t len;
    00191   int ret;
    00192
    00193   if (!k || !kp) return KATE_E_INVALID_PARAMETER;
    00194   if (!k->kes) return KATE_E_INIT;
    00195
    00196   kpb=&k->kes->kpb;
    00197
    00198   ret=kate_encode_start_header(kpb,0x80);
    00199   if (ret<0) return ret;
    00200
    00201   ki=k->ki;
    00202   kate_pack_write(kpb,KATE_BITSTREAM_VERSION_MAJOR,8);
    00203   kate_pack_write(kpb,KATE_BITSTREAM_VERSION_MINOR,8);
    00204   kate_pack_write(kpb,ki->num_headers,8);
    00205   kate_pack_write(kpb,ki->text_encoding,8);
    00206   kate_pack_write(kpb,ki->text_directionality,8);
    00207   kate_pack_write(kpb,0,8); /* reserved - 0 */
    00208   kate_pack_write(kpb,kate_granule_shift(k->ki),8);
    00209   ret=kate_encode_write_canvas_size(kpb,ki->original_canvas_width);
    00210   if (ret<0) return ret;
    00211   ret=kate_encode_write_canvas_size(kpb,ki->original_canvas_height);
    00212   if (ret<0) return ret;
    00213   kate_write32(kpb,0); /* reserved - 0 */
    00214   kate_write32(kpb,ki->gps_numerator);
    00215   kate_write32(kpb,ki->gps_denominator);
    00216
    00217   /* language is a 15+1 character max null terminated string */
    00218   if (ki->language) {
    00219     len=strlen(ki->language);
    00220     if (len>15) return KATE_E_LIMIT;
    00221     kate_writebuf(kpb,ki->language,len);
    00222   }
    00223   else len=0;
    00224   while (len++<16) kate_pack_write(kpb,0,8);
    00225
    00226   /* category is a 15+1 character max null terminated string */
    00227   if (ki->category) {
    00228     len=strlen(ki->category);
    00229     if (len>15) return KATE_E_LIMIT;
    00230     kate_writebuf(kpb,ki->category,len);
    00231   }
    00232   else len=0;
    00233   while (len++<16) kate_pack_write(kpb,0,8);
    00234
    00235   return kate_finalize_packet_buffer(kpb,kp,k);
    00236 }
    00237
    00238 static int kate_encode_comment(kate_state *k,kate_comment *kc,kate_packet *kp)
    00239 {
    00240   kate_pack_buffer *kpb;
    00241   const char *vendor;
    00242   int vendor_len;
    00243   int ret;
    00244
    00245   if (!k || !kc || !kp) return KATE_E_INVALID_PARAMETER;
    00246   if (!k->kes) return KATE_E_INIT;
    00247
    00248   kpb=&k->kes->kpb;
    00249
    00250   ret=kate_encode_start_header(kpb,0x81);
    00251   if (ret<0) return ret;
    00252
    00253   vendor=kate_get_version_string();
    00254   if (!vendor) return KATE_E_INIT; /* wtf ??? */
    00255   vendor_len=strlen(vendor);
    00256
    00257   /* mostly copied from theora encoder_toplevel.c */
    00258   kate_write32(kpb,vendor_len);
    00259   kate_writebuf(kpb,vendor,vendor_len);
    00260
    00261   if (kc->comments<0) return KATE_E_INIT;
    00262   kate_write32(kpb,kc->comments);
    00263   if (kc->comments) {
    00264     int i;
    00265     for(i=0;i<kc->comments;++i) {
    00266       if (kc->user_comments[i]) {
    00267         if (kc->comment_lengths[i]<0) return KATE_E_INIT;
    00268         kate_write32(kpb,kc->comment_lengths[i]);
    00269         ret=kate_text_validate(kate_utf8,kc->user_comments[i],kc->comment_lengths[i]);
    00270         if (ret<0) return ret;
    00271         kate_writebuf(kpb,kc->user_comments[i],kc->comment_lengths[i]);
    00272       }
    00273       else {
    00274         kate_write32(kpb,0);
    00275       }
    00276     }
    00277   }
    00278
    00279   return kate_finalize_packet_buffer(kpb,kp,k);
    00280 }
    00281
    00282 static int kate_encode_region(const kate_region *kr,kate_pack_buffer *kpb)
    00283 {
    00284   if (!kr || !kpb) return KATE_E_INVALID_PARAMETER;
    00285
    00286   kate_pack_write(kpb,kr->metric,8);
    00287   kate_write32v(kpb,kr->x);
    00288   kate_write32v(kpb,kr->y);
    00289   kate_write32v(kpb,kr->w);
    00290   kate_write32v(kpb,kr->h);
    00291   kate_write32v(kpb,kr->style);
    00292
    00293   {
    00294     /* bitstream 0.2: add clip */
    00295     kate_pack_buffer warp;
    00296     kate_open_warp(&warp);
    00297     kate_pack_write1(&warp,kr->clip);
    00298     kate_close_warp(&warp,kpb);
    00299   }
    00300
    00301   kate_warp(kpb);
    00302
    00303   return 0;
    00304 }
    00305
    00306 static int kate_encode_regions(kate_state *k,kate_packet *kp)
    00307 {
    00308   kate_pack_buffer *kpb;
    00309   kate_info *ki;
    00310   size_t n;
    00311   int ret;
    00312
    00313   if (!k || !kp) return KATE_E_INVALID_PARAMETER;
    00314   if (!k->kes) return KATE_E_INIT;
    00315
    00316   kpb=&k->kes->kpb;
    00317
    00318   ret=kate_encode_start_header(kpb,0x82);
    00319   if (ret<0) return ret;
    00320
    00321   ki=k->ki;
    00322   if (!ki) return KATE_E_INIT;
    00323
    00324   kate_write32v(kpb,ki->nregions);
    00325
    00326   for(n=0;n<ki->nregions;++n) {
    00327     ret=kate_encode_region(ki->regions[n],kpb);
    00328     if (ret<0) return ret;
    00329   }
    00330
    00331   kate_warp(kpb);
    00332
    00333   return kate_finalize_packet_buffer(kpb,kp,k);
    00334 }
    00335
    00336 static int kate_encode_color(const kate_color *kc,kate_pack_buffer *kpb)
    00337 {
    00338   if (!kc || !kpb) return KATE_E_INVALID_PARAMETER;
    00339   kate_pack_write(kpb,kc->r,8);
    00340   kate_pack_write(kpb,kc->g,8);
    00341   kate_pack_write(kpb,kc->b,8);
    00342   kate_pack_write(kpb,kc->a,8);
    00343   return 0;
    00344 }
    00345
    00346 static int kate_encode_style(const kate_style *ks,kate_pack_buffer *kpb)
    00347 {
    00348   kate_float d[8];
    00349   size_t idx;
    00350
    00351   if (!ks || !kpb) return KATE_E_INVALID_PARAMETER;
    00352
    00353   idx=0;
    00354   d[idx++]=ks->halign;
    00355   d[idx++]=ks->valign;
    00356   d[idx++]=ks->font_width;
    00357   d[idx++]=ks->font_height;
    00358   d[idx++]=ks->left_margin;
    00359   d[idx++]=ks->top_margin;
    00360   d[idx++]=ks->right_margin;
    00361   d[idx++]=ks->bottom_margin;
    00362   kate_fp_encode_kate_float(sizeof(d)/sizeof(d[0]),d,1,kpb);
    00363   kate_encode_color(&ks->text_color,kpb);
    00364   kate_encode_color(&ks->background_color,kpb);
    00365   kate_encode_color(&ks->draw_color,kpb);
    00366   kate_pack_write(kpb,ks->font_metric,8);
    00367   kate_pack_write(kpb,ks->margin_metric,8);
    00368   kate_pack_write1(kpb,ks->bold);
    00369   kate_pack_write1(kpb,ks->italics);
    00370   kate_pack_write1(kpb,ks->underline);
    00371   kate_pack_write1(kpb,ks->strike);
    00372
    00373   {
    00374     /* bitstream 0.2: add justify and font */
    00375     kate_pack_buffer warp;
    00376     kate_open_warp(&warp);
    00377     kate_pack_write1(&warp,ks->justify);
    00378     if (ks->font) {
    00379       size_t len=strlen(ks->font);
    00380       kate_write32v(&warp,len);
    00381       kate_writebuf(&warp,ks->font,len);
    00382     }
    00383     else {
    00384       kate_write32v(&warp,0);
    00385     }
    00386     kate_close_warp(&warp,kpb);
    00387   }
    00388
    00389   {
    00390     /* bitstream 0.4: add nowrap */
    00391     kate_pack_buffer warp;
    00392     kate_open_warp(&warp);
    00393     kate_write32v(&warp,ks->wrap_mode);
    00394     kate_close_warp(&warp,kpb);
    00395   }
    00396
    00397   kate_warp(kpb);
    00398
    00399   return 0;
    00400 }
    00401
    00402 static int kate_encode_styles(kate_state *k,kate_packet *kp)
    00403 {
    00404   kate_pack_buffer *kpb;
    00405   kate_info *ki;
    00406   size_t n;
    00407   int ret;
    00408
    00409   if (!k || !kp) return KATE_E_INVALID_PARAMETER;
    00410   if (!k->kes) return KATE_E_INIT;
    00411
    00412   kpb=&k->kes->kpb;
    00413
    00414   ret=kate_encode_start_header(kpb,0x83);
    00415   if (ret<0) return ret;
    00416
    00417   ki=k->ki;
    00418   if (!ki) return KATE_E_INIT;
    00419
    00420   kate_write32v(kpb,ki->nstyles);
    00421
    00422   for(n=0;n<ki->nstyles;++n) {
    00423     ret=kate_encode_style(ki->styles[n],kpb);
    00424     if (ret<0) return ret;
    00425   }
    00426
    00427   kate_warp(kpb);
    00428
    00429   return kate_finalize_packet_buffer(kpb,kp,k);
    00430 }
    00431
    00432 static int kate_encode_curve(const kate_curve *kc,kate_pack_buffer *kpb)
    00433 {
    00434   if (!kc || !kpb) return KATE_E_INVALID_PARAMETER;
    00435
    00436   kate_pack_write(kpb,kc->type,8);
    00437   kate_write32v(kpb,kc->npts);
    00438   kate_warp(kpb);
    00439   if (kc->npts) kate_fp_encode_kate_float(kc->npts,kc->pts,2,kpb);
    00440
    00441   return 0;
    00442 }
    00443
    00444 static int kate_encode_curves(kate_state *k,kate_packet *kp)
    00445 {
    00446   kate_pack_buffer *kpb;
    00447   kate_info *ki;
    00448   size_t n;
    00449   int ret;
    00450
    00451   if (!k || !kp) return KATE_E_INVALID_PARAMETER;
    00452   if (!k->kes) return KATE_E_INIT;
    00453
    00454   kpb=&k->kes->kpb;
    00455
    00456   ret=kate_encode_start_header(kpb,0x84);
    00457   if (ret<0) return ret;
    00458
    00459   ki=k->ki;
    00460   if (!ki) return KATE_E_INIT;
    00461
    00462   kate_write32v(kpb,ki->ncurves);
    00463
    00464   for(n=0;n<ki->ncurves;++n) {
    00465     ret=kate_encode_curve(ki->curves[n],kpb);
    00466     if (ret<0) return ret;
    00467   }
    00468
    00469   kate_warp(kpb);
    00470
    00471   return kate_finalize_packet_buffer(kpb,kp,k);
    00472 }
    00473
    00474 static int kate_encode_motion(const kate_info *ki,const kate_motion *km,kate_pack_buffer *kpb)
    00475 {
    00476   size_t n;
    00477   int ret;
    00478
    00479   if (!ki || !km || !kpb) return KATE_E_INVALID_PARAMETER;
    00480
    00481   kate_write32v(kpb,km->ncurves);
    00482   for (n=0;n<km->ncurves;++n) {
    00483     int idx=kate_find_curve(ki,km->curves[n]);
    00484     if (idx<0) {
    00485       kate_pack_write1(kpb,0);
    00486       ret=kate_encode_curve(km->curves[n],kpb);
    00487       if (ret<0) return ret;
    00488     }
    00489     else {
    00490       kate_pack_write1(kpb,1);
    00491       kate_write32v(kpb,idx);
    00492     }
    00493   }
    00494   kate_fp_encode_kate_float(km->ncurves,km->durations,1,kpb);
    00495   kate_pack_write(kpb,km->x_mapping,8);
    00496   kate_pack_write(kpb,km->y_mapping,8);
    00497   kate_pack_write(kpb,km->semantics,8);
    00498   kate_pack_write1(kpb,km->periodic);
    00499   kate_warp(kpb);
    00500
    00501   return 0;
    00502 }
    00503
    00504 static int kate_encode_motions(kate_state *k,kate_packet *kp)
    00505 {
    00506   kate_pack_buffer *kpb;
    00507   kate_info *ki;
    00508   size_t n;
    00509   int ret;
    00510
    00511   if (!k || !kp) return KATE_E_INVALID_PARAMETER;
    00512   if (!k->kes) return KATE_E_INIT;
    00513
    00514   kpb=&k->kes->kpb;
    00515
    00516   ret=kate_encode_start_header(kpb,0x85);
    00517   if (ret<0) return ret;
    00518
    00519   ki=k->ki;
    00520   if (!ki) return KATE_E_INIT;
    00521
    00522   kate_write32v(kpb,ki->nmotions);
    00523
    00524   for(n=0;n<ki->nmotions;++n) {
    00525     ret=kate_encode_motion(ki,ki->motions[n],kpb);
    00526     if (ret<0) return ret;
    00527   }
    00528
    00529   kate_warp(kpb);
    00530
    00531   return kate_finalize_packet_buffer(kpb,kp,k);
    00532 }
    00533
    00534 static int kate_encode_palette(const kate_palette *kp,kate_pack_buffer *kpb)
    00535 {
    00536   size_t n;
    00537
    00538   if (!kp || !kpb) return KATE_E_INVALID_PARAMETER;
    00539   if (kp->ncolors<=0 || kp->ncolors>256) return KATE_E_LIMIT;
    00540
    00541   kate_pack_write(kpb,kp->ncolors-1,8);
    00542   for (n=0;n<kp->ncolors;++n) {
    00543     int ret=kate_encode_color(kp->colors+n,kpb);
    00544     if (ret<0) return ret;
    00545   }
    00546   kate_warp(kpb);
    00547
    00548   return 0;
    00549 }
    00550
    00551 static int kate_encode_palettes(kate_state *k,kate_packet *kp)
    00552 {
    00553   kate_pack_buffer *kpb;
    00554   kate_info *ki;
    00555   size_t n;
    00556   int ret;
    00557
    00558   if (!k || !kp) return KATE_E_INVALID_PARAMETER;
    00559   if (!k->kes) return KATE_E_INIT;
    00560
    00561   kpb=&k->kes->kpb;
    00562
    00563   ret=kate_encode_start_header(kpb,0x86);
    00564   if (ret<0) return ret;
    00565
    00566   ki=k->ki;
    00567   if (!ki) return KATE_E_INIT;
    00568
    00569   kate_write32v(kpb,ki->npalettes);
    00570
    00571   for(n=0;n<ki->npalettes;++n) {
    00572     ret=kate_encode_palette(ki->palettes[n],kpb);
    00573     if (ret<0) return ret;
    00574   }
    00575
    00576   kate_warp(kpb);
    00577
    00578   return kate_finalize_packet_buffer(kpb,kp,k);
    00579 }
    00580
    00581 #if 0
    00582 static int kate_encode_paletted_bitmap(const kate_bitmap *kb,kate_pack_buffer *kpb)
    00583 {
    00584   size_t w,h,n;
    00585   unsigned int maxpixel;
    00586
    00587   if (kb->bpp>8) return KATE_E_LIMIT;
    00588
    00589   kate_write32v(kpb,kb->palette);
    00590   n=0;
    00591   maxpixel=(1<<kb->bpp)-1;
    00592   for (h=0;h<kb->height;++h) {
    00593     for (w=0;w<kb->width;++w) {
    00594       unsigned int pixel=kb->pixels[n++];
    00595       if (pixel>maxpixel) return KATE_E_LIMIT;
    00596       kate_pack_write(kpb,pixel,kb->bpp);
    00597     }
    00598   }
    00599
    00600   return 0;
    00601 }
    00602 #endif
    00603
    00604 static int kate_encode_rle_bitmap(const kate_bitmap *kb,kate_pack_buffer *kpb)
    00605 {
    00606   if (kb->bpp>8) return KATE_E_LIMIT;
    00607
    00608   kate_pack_write(kpb,kate_bitmap_type_paletted,8);
    00609   kate_pack_write(kpb,1,8); /* RLE encoding */
    00610   kate_write32v(kpb,kb->bpp);
    00611   kate_write32v(kpb,kb->palette);
    00612
    00613   return kate_rle_encode(kb->width,kb->height,kb->pixels,kb->bpp,kpb);
    00614 }
    00615
    00616 static int kate_encode_png_bitmap(const kate_bitmap *kb,kate_pack_buffer *kpb)
    00617 {
    00618   kate_pack_write(kpb,kate_bitmap_type_png,8);
    00619   kate_write32(kpb,kb->size);
    00620   kate_writebuf(kpb,(const char*)kb->pixels,kb->size);
    00621
    00622   return 0;
    00623 }
    00624
    00625 static int kate_encode_bitmap(const kate_bitmap *kb,kate_pack_buffer *kpb)
    00626 {
    00627   int ret;
    00628
    00629   if (!kb || !kpb) return KATE_E_INVALID_PARAMETER;
    00630
    00631   kate_write32v(kpb,kb->width);
    00632   kate_write32v(kpb,kb->height);
    00633 #if 0
    00634   /* paletted bitmaps are now written compressed */
    00635   kate_pack_write(kpb,kb->bpp,8); /* 0 marks a raw bitmap */
    00636 #else
    00637   kate_pack_write(kpb,0,8); /* 0 marks a raw bitmap */
    00638 #endif
    00639
    00640   switch (kb->type) {
    00641     case kate_bitmap_type_paletted:
    00642       ret=kate_encode_rle_bitmap(kb,kpb);
    00643       break;
    00644     case kate_bitmap_type_png:
    00645       ret=kate_encode_png_bitmap(kb,kpb);
    00646       break;
    00647     default:
    00648       ret=KATE_E_INVALID_PARAMETER;
    00649       break;
    00650   }
    00651
    00652   if (ret<0) return ret;
    00653
    00654   {
    00655     /* bitstream 0.4: x/y offsets */
    00656     kate_pack_buffer warp;
    00657     kate_open_warp(&warp);
    00658     kate_write32v(&warp,kb->x_offset);
    00659     kate_write32v(&warp,kb->y_offset);
    00660     kate_close_warp(&warp,kpb);
    00661   }
    00662
    00663   kate_warp(kpb);
    00664
    00665   return 0;
    00666 }
    00667
    00668 static int kate_encode_bitmaps(kate_state *k,kate_packet *kp)
    00669 {
    00670   kate_pack_buffer *kpb;
    00671   kate_info *ki;
    00672   size_t n;
    00673   int ret;
    00674
    00675   if (!k || !kp) return KATE_E_INVALID_PARAMETER;
    00676   if (!k->kes) return KATE_E_INIT;
    00677
    00678   kpb=&k->kes->kpb;
    00679
    00680   ret=kate_encode_start_header(kpb,0x87);
    00681   if (ret<0) return ret;
    00682
    00683   ki=k->ki;
    00684   if (!ki) return KATE_E_INIT;
    00685
    00686   kate_write32v(kpb,ki->nbitmaps);
    00687
    00688   for(n=0;n<ki->nbitmaps;++n) {
    00689     ret=kate_encode_bitmap(ki->bitmaps[n],kpb);
    00690     if (ret<0) return ret;
    00691   }
    00692
    00693   kate_warp(kpb);
    00694
    00695   return kate_finalize_packet_buffer(kpb,kp,k);
    00696 }
    00697
    00698 static int kate_encode_font_range(const kate_info *ki,const kate_font_range *kfr,kate_pack_buffer *kpb)
    00699 {
    00700   if (!ki || !kfr || !kpb) return KATE_E_INVALID_PARAMETER;
    00701
    00702   if (!kate_is_valid_code_point(kfr->first_code_point)) return KATE_E_TEXT;
    00703   if (!kate_is_valid_code_point(kfr->last_code_point)) return KATE_E_TEXT;
    00704   if (kfr->first_bitmap<0) return KATE_E_LIMIT;
    00705   if ((size_t)(kfr->first_bitmap+(kfr->last_code_point-kfr->first_code_point))>=ki->nbitmaps) return KATE_E_LIMIT;
    00706
    00707   kate_write32v(kpb,kfr->first_code_point);
    00708   kate_write32v(kpb,kfr->last_code_point);
    00709   kate_write32v(kpb,kfr->first_bitmap);
    00710   kate_warp(kpb);
    00711
    00712   return 0;
    00713 }
    00714
    00715 static int kate_encode_font_ranges(kate_state *k,kate_packet *kp)
    00716 {
    00717   kate_pack_buffer *kpb;
    00718   kate_info *ki;
    00719   size_t n,l;
    00720   int ret;
    00721
    00722   if (!k || !kp) return KATE_E_INVALID_PARAMETER;
    00723   if (!k->kes) return KATE_E_INIT;
    00724
    00725   kpb=&k->kes->kpb;
    00726
    00727   ret=kate_encode_start_header(kpb,0x88);
    00728   if (ret<0) return ret;
    00729
    00730   ki=k->ki;
    00731   if (!ki) return KATE_E_INIT;
    00732
    00733   kate_write32v(kpb,ki->nfont_ranges);
    00734   for(n=0;n<ki->nfont_ranges;++n) {
    00735     ret=kate_encode_font_range(ki,ki->font_ranges[n],kpb);
    00736     if (ret<0) return ret;
    00737   }
    00738
    00739   kate_write32v(kpb,ki->nfont_mappings);
    00740   for(n=0;n<ki->nfont_mappings;++n) {
    00741     const kate_font_mapping *kfm=ki->font_mappings[n];
    00742     kate_write32v(kpb,kfm->nranges);
    00743     for (l=0;l<kfm->nranges;++l) {
    00744       const kate_font_range *kfr=kfm->ranges[l];
    00745       int idx=kate_find_font_range(ki,kfr);
    00746       if (idx>=0) {
    00747         kate_pack_write1(kpb,1);
    00748         kate_write32v(kpb,idx);
    00749       }
    00750       else {
    00751         kate_pack_write1(kpb,0);
    00752         ret=kate_encode_font_range(ki,kfr,kpb);
    00753         if (ret<0) return ret;
    00754       }
    00755     }
    00756   }
    00757
    00758   kate_warp(kpb);
    00759
    00760   return kate_finalize_packet_buffer(kpb,kp,k);
    00761 }
    00762
    00763 static inline int kate_check_granule(kate_state *k,kate_int64_t *granulepos)
    00764 {
    00765   if (*granulepos<k->kes->granulepos) return -1;
    00766   return 0;
    00767 }
    00768
    00769 #define WRITE_OVERRIDE(kpb,var,def,write) \
    00770   do \
    00771     if (ret==0 && (kes->overrides.var!=(def))) { \
    00772       kate_pack_write1(kpb,1); \
    00773       write; \
    00774     } \
    00775     else kate_pack_write1(kpb,0); \
    00776   while(0)
    00777
    00778 static int kate_encode_overrides(kate_state *k,kate_pack_buffer *kpb)
    00779 {
    00780   kate_encode_state *kes;
    00781   size_t n;
    00782   int ret=0;
    00783
    00784   if (!k || !kpb) return KATE_E_INVALID_PARAMETER;
    00785   kes=k->kes;
    00786   if (!kes) return KATE_E_INIT;
    00787
    00788   if (kes->overrides.language
    00789    || kes->overrides.text_encoding!=k->ki->text_encoding
    00790    || kes->overrides.text_directionality!=k->ki->text_directionality
    00791    || kes->overrides.region_index>=0
    00792    || kes->overrides.region
    00793    || kes->overrides.style_index>=0
    00794    || kes->overrides.style
    00795    || kes->overrides.secondary_style_index>=0
    00796    || kes->overrides.secondary_style
    00797    || kes->overrides.font_mapping_index>=0
    00798   ) {
    00799     kate_pack_write1(kpb,1);
    00800     WRITE_OVERRIDE(kpb,text_encoding,k->ki->text_encoding,kate_pack_write(kpb,kes->overrides.text_encoding,8));
    00801     WRITE_OVERRIDE(kpb,text_directionality,k->ki->text_directionality,kate_pack_write(kpb,kes->overrides.text_directionality,8));
    00802     WRITE_OVERRIDE(kpb,language,NULL,
    00803       do {
    00804         size_t len=strlen(kes->overrides.language);
    00805         kate_write32v(kpb,len);
    00806         kate_writebuf(kpb,kes->overrides.language,len);
    00807       } while(0));
    00808     WRITE_OVERRIDE(kpb,region_index,-1,kate_write32v(kpb,kes->overrides.region_index));
    00809     WRITE_OVERRIDE(kpb,region,NULL,ret=kate_encode_region(kes->overrides.region,kpb));
    00810     WRITE_OVERRIDE(kpb,style_index,-1,kate_write32v(kpb,kes->overrides.style_index));
    00811     WRITE_OVERRIDE(kpb,style,NULL,ret=kate_encode_style(kes->overrides.style,kpb));
    00812     WRITE_OVERRIDE(kpb,secondary_style_index,-1,kate_write32v(kpb,kes->overrides.secondary_style_index));
    00813     WRITE_OVERRIDE(kpb,secondary_style,NULL,ret=kate_encode_style(kes->overrides.secondary_style,kpb));
    00814     WRITE_OVERRIDE(kpb,font_mapping_index,-1,kate_write32v(kpb,kes->overrides.font_mapping_index));
    00815   }
    00816   else kate_pack_write1(kpb,0);
    00817
    00818   if (ret==0) {
    00819     /* bitstream 0.2: add palette, bitmap, markup type */
    00820     kate_pack_buffer warp;
    00821     kate_open_warp(&warp);
    00822     if (kes->overrides.palette_index>=0
    00823      || kes->overrides.palette
    00824      || kes->overrides.bitmap_index>=0
    00825      || kes->overrides.bitmap
    00826      || kes->overrides.text_markup_type!=k->ki->text_markup_type) {
    00827       kate_pack_write1(&warp,1);
    00828       WRITE_OVERRIDE(&warp,palette_index,-1,kate_write32v(&warp,kes->overrides.palette_index));
    00829       WRITE_OVERRIDE(&warp,palette,NULL,ret=kate_encode_palette(kes->overrides.palette,&warp));
    00830       WRITE_OVERRIDE(&warp,bitmap_index,-1,kate_write32v(&warp,kes->overrides.bitmap_index));
    00831       WRITE_OVERRIDE(&warp,bitmap,NULL,ret=kate_encode_bitmap(kes->overrides.bitmap,&warp));
    00832       WRITE_OVERRIDE(&warp,text_markup_type,k->ki->text_markup_type,kate_pack_write(&warp,kes->overrides.text_markup_type,8));
    00833     }
    00834     else kate_pack_write1(&warp,0);
    00835     kate_close_warp(&warp,kpb);
    00836   }
    00837
    00838   if (ret==0) {
    00839     /* bitstream 0.4: add bitmaps */
    00840     kate_pack_buffer warp;
    00841     kate_open_warp(&warp);
    00842     kate_write32v(&warp,kes->nbitmaps);
    00843     for(n=0;n<kes->nbitmaps;++n) {
    00844       if (kes->bitmaps[n]==NULL) {
    00845         /* we have an index into the bitmaps header */
    00846         kate_pack_write1(&warp,1);
    00847         kate_write32v(&warp,kes->bitmap_indices[n]);
    00848       }
    00849       else {
    00850         /* we have a fully defined bitmap */
    00851         kate_pack_write1(&warp,0);
    00852         ret=kate_encode_bitmap(kes->bitmaps[n],&warp);
    00853         if (ret<0) break;
    00854       }
    00855     }
    00856     kate_close_warp(&warp,kpb);
    00857   }
    00858
    00859   kate_warp(kpb);
    00860
    00861   return ret;
    00862 }
    00863
    00878 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)
    00879 {
    00880   kate_pack_buffer *kpb;
    00881   kate_int64_t start_granulepos;
    00882   kate_int64_t start;
    00883   kate_int64_t duration;
    00884   kate_int64_t backlink;
    00885   kate_int64_t earliest_t;
    00886   int ret;
    00887   size_t n;
    00888
    00889   if (!k || !text || !kp) return KATE_E_INVALID_PARAMETER;
    00890   if (t0<0 || t1<t0) return KATE_E_INVALID_PARAMETER;
    00891   if (!k->kes) return KATE_E_INIT;
    00892   if (k->kes->eos) return KATE_E_INIT;
    00893
    00894   ret=kate_text_validate(k->kes->overrides.text_encoding,text,sz);
    00895   if (ret<0) return ret;
    00896
    00897   ret=kate_encode_state_trim_events(k->kes,t0);
    00898   if (ret<0) return ret;
    00899   ret=kate_encode_state_add_event(k->kes,t0,t1);
    00900   if (ret<0) return ret;
    00901   ret=kate_encode_state_get_earliest_event(k->kes,&earliest_t,NULL);
    00902   if (ret<0) return ret;
    00903
    00904   start_granulepos=(earliest_t<<k->ki->granule_shift)|(t0-earliest_t);
    00905   if (start_granulepos<0) return KATE_E_BAD_GRANULE;
    00906   if (kate_check_granule(k,&start_granulepos)<0) return KATE_E_BAD_GRANULE;
    00907
    00908   start=t0;
    00909   if (start<0) return KATE_E_BAD_GRANULE;
    00910   duration=t1-t0;
    00911   if (duration<0) return KATE_E_BAD_GRANULE;
    00912   backlink=t0-earliest_t;
    00913   if (backlink<0) return KATE_E_BAD_GRANULE;
    00914
    00915   kpb=&k->kes->kpb;
    00916   kate_pack_write(kpb,0x00,8);
    00917
    00918   kate_write64(kpb,start);
    00919   kate_write64(kpb,duration);
    00920   kate_write64(kpb,backlink);
    00921
    00922   kate_write32(kpb,sz);
    00923   kate_writebuf(kpb,text,sz);
    00924
    00925   if (k->kes->id>=0) {
    00926     kate_pack_write1(kpb,1);
    00927     kate_write32v(kpb,k->kes->id);
    00928   }
    00929   else {
    00930     kate_pack_write1(kpb,0);
    00931   }
    00932
    00933   if (k->kes->nmotions) {
    00934     kate_pack_write1(kpb,1);
    00935     kate_write32v(kpb,k->kes->nmotions);
    00936     for (n=0;n<k->kes->nmotions;++n) {
    00937       if (k->kes->motions[n]==NULL) {
    00938         /* we have an index into the motions header */
    00939         kate_pack_write1(kpb,1);
    00940         kate_write32v(kpb,k->kes->motion_indices[n]);
    00941       }
    00942       else {
    00943         /* we have a fully defined motion */
    00944         kate_pack_write1(kpb,0);
    00945         ret=kate_encode_motion(k->ki,k->kes->motions[n],kpb);
    00946         if (ret<0) return ret;
    00947       }
    00948     }
    00949   }
    00950   else kate_pack_write1(kpb,0);
    00951
    00952   kate_encode_overrides(k,kpb);
    00953
    00954   if (start_granulepos>k->kes->furthest_granule) k->kes->furthest_granule=start_granulepos;
    00955
    00956   k->kes->granulepos=start_granulepos;
    00957   ret=kate_finalize_packet_buffer(kpb,kp,k);
    00958   if (ret<0) return ret;
    00959
    00960   /* save the packet in case we have to repeat it */
    00961   ret=kate_encode_state_save_event_buffer(k->kes,kp->nbytes,kp->data);
    00962   if (ret<0) return ret;
    00963
    00964   return 0;
    00965 }
    00966
    00981 int kate_encode_text(kate_state *k,kate_float t0,kate_float t1,const char *text,size_t sz,kate_packet *kp)
    00982 {
    00983   if (!k) return KATE_E_INVALID_PARAMETER;
    00984   return kate_encode_text_raw_times(k,kate_duration_granule(k->ki,t0),kate_duration_granule(k->ki,t1),text,sz,kp);
    00985 }
    00986
    00996 int kate_encode_keepalive_raw_times(kate_state *k,kate_int64_t t,kate_packet *kp)
    00997 {
    00998   kate_pack_buffer *kpb;
    00999   kate_int64_t granulepos;
    01000   kate_int64_t earliest_t;
    01001   int ret;
    01002
    01003   if (!k || !kp) return KATE_E_INVALID_PARAMETER;
    01004   if (!k->kes) return KATE_E_INIT;
    01005   if (k->kes->eos) return KATE_E_INIT;
    01006
    01007   ret=kate_encode_state_trim_events(k->kes,t);
    01008   if (ret<0) return ret;
    01009
    01010   ret=kate_encode_state_get_earliest_event(k->kes,&earliest_t,NULL);
    01011   if (ret==KATE_E_NOT_FOUND) {
    01012     /* if there are no live events yet, base from now */
    01013     earliest_t=t;
    01014     ret=0;
    01015   }
    01016   if (ret<0) return ret;
    01017
    01018   granulepos=(earliest_t<<k->ki->granule_shift)|(t-earliest_t);
    01019   if (granulepos<0) return KATE_E_BAD_GRANULE;
    01020
    01021   if (kate_check_granule(k,&granulepos)<0) return KATE_E_BAD_GRANULE;
    01022   k->kes->granulepos=granulepos;
    01023
    01024   ret=kate_encode_state_add_event(k->kes,t,t);
    01025   if (ret<0) return ret;
    01026
    01027   kpb=&k->kes->kpb;
    01028   kate_pack_write(kpb,0x01,8);
    01029
    01030   return kate_finalize_packet_buffer(kpb,kp,k);
    01031 }
    01032
    01042 int kate_encode_keepalive(kate_state *k,kate_float t,kate_packet *kp)
    01043 {
    01044   if (!k) return KATE_E_INVALID_PARAMETER;
    01045   return kate_encode_keepalive_raw_times(k,kate_duration_granule(k->ki,t),kp);
    01046 }
    01047
    01066 int kate_encode_repeat_raw_times(kate_state *k,kate_int64_t t,kate_int64_t threshold,kate_packet *kp)
    01067 {
    01068   kate_int64_t earliest_t;
    01069   kate_int64_t granulepos;
    01070   int ret;
    01071
    01072   if (!k || !kp) return KATE_E_INVALID_PARAMETER;
    01073   if (threshold<0) return KATE_E_INVALID_PARAMETER;
    01074   if (!k->kes) return KATE_E_INIT;
    01075   if (k->kes->eos) return KATE_E_INIT;
    01076
    01077   ret=kate_encode_state_trim_events(k->kes,t);
    01078   if (ret<0) return ret;
    01079   ret=kate_encode_state_get_earliest_event(k->kes,&earliest_t,NULL);
    01080   if (ret==KATE_E_NOT_FOUND) {
    01081     /* if there are no live events yet, base from now */
    01082     earliest_t=t;
    01083   }
    01084   else if (ret<0) {
    01085     return ret;
    01086   }
    01087   granulepos=(earliest_t<<k->ki->granule_shift)|(t-earliest_t);
    01088   if (granulepos<0) return KATE_E_BAD_GRANULE;
    01089
    01090   if (kate_check_granule(k,&granulepos)<0) return KATE_E_BAD_GRANULE;
    01091
    01092   ret=kate_encode_state_get_repeat(k->kes,t,threshold,kp);
    01093   if (ret>0) {
    01094     /* if we encoded a repeat, update encoding state granpos */
    01095     k->kes->granulepos=granulepos;
    01096   }
    01097   return ret;
    01098 }
    01099
    01117 int kate_encode_repeat(kate_state *k,kate_float t,kate_float threshold,kate_packet *kp)
    01118 {
    01119   if (!k) return KATE_E_INVALID_PARAMETER;
    01120   return kate_encode_repeat_raw_times(k,kate_duration_granule(k->ki,t),kate_duration_granule(k->ki,threshold),kp);
    01121 }
    01122
    01134 int kate_encode_finish_raw_times(kate_state *k,kate_int64_t t,kate_packet *kp)
    01135 {
    01136   kate_pack_buffer *kpb;
    01137   kate_int64_t granulepos;
    01138   int ret;
    01139
    01140   if (!k || !kp) return KATE_E_INVALID_PARAMETER;
    01141   if (!k->kes) return KATE_E_INIT;
    01142   if (k->kes->eos) return KATE_E_INIT;
    01143
    01144   ret=kate_encode_state_trim_events(k->kes,t);
    01145   if (ret<0) return ret;
    01146
    01147   if (t<0) {
    01148     ret=kate_encode_state_get_latest_event(k->kes,NULL,&t);
    01149     if (ret==KATE_E_NOT_FOUND) {
    01150       /* if nothing was encoded, it is still a valid stream */
    01151       t=0;
    01152       ret=0;
    01153     }
    01154     if (ret<0) return ret;
    01155   }
    01156
    01157   granulepos=t<<k->ki->granule_shift;
    01158   if (granulepos<0) return KATE_E_BAD_GRANULE;
    01159
    01160   if (kate_check_granule(k,&granulepos)<0) return KATE_E_BAD_GRANULE;
    01161   k->kes->granulepos=granulepos;
    01162
    01163   kpb=&k->kes->kpb;
    01164   kate_pack_write(kpb,0x7f,8);
    01165
    01166   k->kes->eos=1;
    01167
    01168   return kate_finalize_packet_buffer(kpb,kp,k);
    01169 }
    01170
    01181 int kate_encode_finish(kate_state *k,kate_float t,kate_packet *kp)
    01182 {
    01183   if (!k) return KATE_E_INVALID_PARAMETER;
    01184   return kate_encode_finish_raw_times(k,kate_duration_granule(k->ki,t),kp);
    01185 }
    01186
    01199 int kate_encode_headers(kate_state *k,kate_comment *kc,kate_packet *kp)
    01200 {
    01201   if (!k || !kc || !kp) return KATE_E_INVALID_PARAMETER;
    01202   if (!k->kes) return KATE_E_INIT;
    01203   if (k->kes->eos) return KATE_E_INIT;
    01204
    01205   switch (k->kes->packetno+1) {
    01206     case 0: return kate_encode_info(k,kp);
    01207     case 1: return kate_encode_comment(k,kc,kp);
    01208     case 2: return kate_encode_regions(k,kp);
    01209     case 3: return kate_encode_styles(k,kp);
    01210     case 4: return kate_encode_curves(k,kp);
    01211     case 5: return kate_encode_motions(k,kp);
    01212     case 6: return kate_encode_palettes(k,kp);
    01213     case 7: return kate_encode_bitmaps(k,kp);
    01214     case 8: return kate_encode_font_ranges(k,kp);
    01215     case 9: return 1;
    01216     default: return KATE_E_INVALID_PARAMETER;
    01217   }
    01218 }
    01219
    01231 int kate_encode_add_motion(kate_state *k,kate_motion *km,int destroy)
    01232 {
    01233   if (!k || !km) return KATE_E_INVALID_PARAMETER;
    01234   if (!k->kes) return KATE_E_INIT;
    01235
    01236   return kate_encode_state_add_motion(k->kes,km,destroy);
    01237 }
    01238
    01247 int kate_encode_add_motion_index(kate_state *k,size_t motion)
    01248 {
    01249   if (!k) return KATE_E_INVALID_PARAMETER;
    01250   if (!k->ki) return KATE_E_INIT;
    01251   if (motion>=k->ki->nmotions) return KATE_E_INVALID_PARAMETER;
    01252   if (!k->kes) return KATE_E_INIT;
    01253
    01254   return kate_encode_state_add_motion_index(k->kes,motion);
    01255 }
    01256
    01265 int kate_encode_add_bitmap(kate_state *k,const kate_bitmap *kb)
    01266 {
    01267   if (!k || !kb) return KATE_E_INVALID_PARAMETER;
    01268   if (!k->kes) return KATE_E_INIT;
    01269
    01270   return kate_encode_state_add_bitmap(k->kes,kb);
    01271 }
    01272
    01281 int kate_encode_add_bitmap_index(kate_state *k,size_t bitmap)
    01282 {
    01283   if (!k) return KATE_E_INVALID_PARAMETER;
    01284   if (!k->ki) return KATE_E_INIT;
    01285   if (bitmap>=k->ki->nbitmaps) return KATE_E_INVALID_PARAMETER;
    01286   if (!k->kes) return KATE_E_INIT;
    01287
    01288   return kate_encode_state_add_bitmap_index(k->kes,bitmap);
    01289 }
    01290
    01299 int kate_encode_set_region_index(kate_state *k,size_t region)
    01300 {
    01301   if (!k) return KATE_E_INVALID_PARAMETER;
    01302   if (!k->kes) return KATE_E_INIT;
    01303   if (!k->ki) return KATE_E_INIT;
    01304   if (region>=k->ki->nregions) return KATE_E_INVALID_PARAMETER;
    01305   if (k->kes->overrides.region) return KATE_E_INIT;
    01306   k->kes->overrides.region_index=region;
    01307   return 0;
    01308 }
    01309
    01318 int kate_encode_set_region(kate_state *k,const kate_region *kr)
    01319 {
    01320   if (!k || !kr) return KATE_E_INVALID_PARAMETER;
    01321   if (!k->kes) return KATE_E_INIT;
    01322   if (!k->ki) return KATE_E_INIT;
    01323   if (k->kes->overrides.region_index>=0) return KATE_E_INIT;
    01324   k->kes->overrides.region=kr;
    01325   return 0;
    01326 }
    01327
    01336 int kate_encode_set_style_index(kate_state *k,size_t style)
    01337 {
    01338   if (!k) return KATE_E_INVALID_PARAMETER;
    01339   if (!k->kes) return KATE_E_INIT;
    01340   if (!k->ki) return KATE_E_INIT;
    01341   if (style>=k->ki->nstyles) return KATE_E_INVALID_PARAMETER;
    01342   if (k->kes->overrides.style) return KATE_E_INIT;
    01343   k->kes->overrides.style_index=style;
    01344   return 0;
    01345 }
    01346
    01355 int kate_encode_set_style(kate_state *k,const kate_style *ks)
    01356 {
    01357   if (!k || !ks) return KATE_E_INVALID_PARAMETER;
    01358   if (!k->kes) return KATE_E_INIT;
    01359   if (!k->ki) return KATE_E_INIT;
    01360   if (k->kes->overrides.style_index>=0) return KATE_E_INIT;
    01361   k->kes->overrides.style=ks;
    01362   return 0;
    01363 }
    01364
    01373 int kate_encode_set_secondary_style_index(kate_state *k,size_t style)
    01374 {
    01375   if (!k) return KATE_E_INVALID_PARAMETER;
    01376   if (!k->kes) return KATE_E_INIT;
    01377   if (!k->ki) return KATE_E_INIT;
    01378   if (style>=k->ki->nstyles) return KATE_E_INVALID_PARAMETER;
    01379   if (k->kes->overrides.secondary_style) return KATE_E_INIT;
    01380   k->kes->overrides.secondary_style_index=style;
    01381   return 0;
    01382 }
    01383
    01392 int kate_encode_set_secondary_style(kate_state *k,const kate_style *ks)
    01393 {
    01394   if (!k || !ks) return KATE_E_INVALID_PARAMETER;
    01395   if (!k->kes) return KATE_E_INIT;
    01396   if (!k->ki) return KATE_E_INIT;
    01397   if (k->kes->overrides.secondary_style_index>=0) return KATE_E_INIT;
    01398   k->kes->overrides.secondary_style=ks;
    01399   return 0;
    01400 }
    01401
    01410 int kate_encode_set_palette_index(kate_state *k,size_t palette)
    01411 {
    01412   if (!k) return KATE_E_INVALID_PARAMETER;
    01413   if (!k->kes) return KATE_E_INIT;
    01414   if (!k->ki) return KATE_E_INIT;
    01415   if (palette>=k->ki->npalettes) return KATE_E_INVALID_PARAMETER;
    01416   if (k->kes->overrides.palette) return KATE_E_INIT;
    01417   k->kes->overrides.palette_index=palette;
    01418   return 0;
    01419 }
    01420
    01429 int kate_encode_set_palette(kate_state *k,const kate_palette *kp)
    01430 {
    01431   if (!k || !kp) return KATE_E_INVALID_PARAMETER;
    01432   if (!k->kes) return KATE_E_INIT;
    01433   if (!k->ki) return KATE_E_INIT;
    01434   if (k->kes->overrides.palette_index>=0) return KATE_E_INIT;
    01435   k->kes->overrides.palette=kp;
    01436   return 0;
    01437 }
    01438
    01447 int kate_encode_set_bitmap_index(kate_state *k,size_t bitmap)
    01448 {
    01449   if (!k) return KATE_E_INVALID_PARAMETER;
    01450   if (!k->kes) return KATE_E_INIT;
    01451   if (!k->ki) return KATE_E_INIT;
    01452   if (bitmap>=k->ki->nbitmaps) return KATE_E_INVALID_PARAMETER;
    01453   if (k->kes->overrides.bitmap) return KATE_E_INIT;
    01454   k->kes->overrides.bitmap_index=bitmap;
    01455   return 0;
    01456 }
    01457
    01466 int kate_encode_set_bitmap(kate_state *k,const kate_bitmap *kb)
    01467 {
    01468   if (!k || !kb) return KATE_E_INVALID_PARAMETER;
    01469   if (!k->kes) return KATE_E_INIT;
    01470   if (!k->ki) return KATE_E_INIT;
    01471   if (k->kes->overrides.bitmap_index>=0) return KATE_E_INIT;
    01472   k->kes->overrides.bitmap=kb;
    01473   return 0;
    01474 }
    01475
    01485 int kate_encode_set_font_mapping_index(kate_state *k,size_t font_mapping)
    01486 {
    01487   if (!k) return KATE_E_INVALID_PARAMETER;
    01488   if (!k->kes) return KATE_E_INIT;
    01489   if (!k->ki) return KATE_E_INIT;
    01490   if (font_mapping>=k->ki->nfont_mappings) return KATE_E_INVALID_PARAMETER;
    01491   k->kes->overrides.font_mapping_index=font_mapping;
    01492   return 0;
    01493 }
    01494
    01503 int kate_encode_set_text_encoding(kate_state *k,kate_text_encoding text_encoding)
    01504 {
    01505   if (!k) return KATE_E_INVALID_PARAMETER;
    01506   if (!k->kes) return KATE_E_INIT;
    01507   k->kes->overrides.text_encoding=text_encoding;
    01508   return 0;
    01509 }
    01510
    01519 int kate_encode_set_text_directionality(kate_state *k,kate_text_directionality text_directionality)
    01520 {
    01521   if (!k) return KATE_E_INVALID_PARAMETER;
    01522   if (!k->kes) return KATE_E_INIT;
    01523   k->kes->overrides.text_directionality=text_directionality;
    01524   return 0;
    01525 }
    01526
    01537 int kate_encode_set_id(kate_state *k,kate_int32_t id)
    01538 {
    01539   if (!k) return KATE_E_INVALID_PARAMETER;
    01540   if (!k->kes) return KATE_E_INIT;
    01541
    01542   (void)id;
    01543
    01544   return KATE_E_IMPL;
    01545 }
    01546
    01555 int kate_encode_set_language(kate_state *k,const char *language) /* language can be NULL */
    01556 {
    01557   size_t len;
    01558   char *l=NULL;
    01559
    01560   if (!k) return KATE_E_INVALID_PARAMETER;
    01561   if (!k->kes) return KATE_E_INIT;
    01562
    01563   if (language) {
    01564     len=strlen(language);
    01565     l=(char*)kate_malloc(len+1);
    01566     if (!l) return KATE_E_OUT_OF_MEMORY;
    01567     memcpy(l,language,len+1);
    01568   }
    01569
    01570   if (k->kes->overrides.language) kate_free(k->kes->overrides.language);
    01571   k->kes->overrides.language=l;
    01572
    01573   return 0;
    01574 }
    01575
    01584 int kate_encode_set_markup_type(kate_state *k,int text_markup_type)
    01585 {
    01586   if (!k) return KATE_E_INVALID_PARAMETER;
    01587   if (!k->kes) return KATE_E_INIT;
    01588
    01589   k->kes->overrides.text_markup_type=text_markup_type;
    01590
    01591   return 0;
    01592 }
    01593
    01601 kate_int64_t kate_encode_get_granule(const kate_state *k)
    01602 {
    01603   if (!k) return KATE_E_INVALID_PARAMETER;
    01604   if (!k->kes) return KATE_E_INIT;
    01605
    01606   return k->kes->granulepos;
    01607 }
    01608