Merge branch 'bug_518_add_demux_reserve_from_python'
[vuplus_dvbapp] / lib / gdi / font.cpp
1 #include <lib/gdi/font.h>
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <ctype.h>
6 #include <pthread.h>
7 #include <sys/types.h>
8 #include <unistd.h>
9
10 // use this for init Freetype...
11 #include <ft2build.h>
12 #include FT_FREETYPE_H
13 #ifdef HAVE_FREETYPE2
14 #define FTC_Image_Cache_New(a,b)        FTC_ImageCache_New(a,b)
15 #define FTC_Image_Cache_Lookup(a,b,c,d) FTC_ImageCache_Lookup(a,b,c,d,NULL)
16 #define FTC_SBit_Cache_New(a,b)         FTC_SBitCache_New(a,b)
17 #define FTC_SBit_Cache_Lookup(a,b,c,d)  FTC_SBitCache_Lookup(a,b,c,d,NULL)
18 #endif
19
20 #include <lib/base/eerror.h>
21 #include <lib/gdi/lcd.h>
22 #include <lib/gdi/grc.h>
23 #include <lib/base/elock.h>
24 #include <lib/base/init.h>
25 #include <lib/base/init_num.h>
26
27 #define HAVE_FRIBIDI
28 // until we have it in the cdk
29
30 #ifdef HAVE_FRIBIDI
31 #include <fribidi/fribidi.h>
32 #endif
33
34 #include <map>
35
36 fontRenderClass *fontRenderClass::instance;
37
38 static pthread_mutex_t ftlock=PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP;
39
40 #ifndef HAVE_FREETYPE2
41 static FTC_Font cache_current_font=0;
42 #endif
43
44 struct fntColorCacheKey
45 {
46         gRGB start, end;
47         fntColorCacheKey(const gRGB &start, const gRGB &end)
48                 : start(start), end(end)
49         {
50         }
51         bool operator <(const fntColorCacheKey &c) const
52         {
53                 if (start < c.start)
54                         return 1;
55                 else if (start == c.start)
56                         return end < c.end;
57                 return 0;
58         }
59 };
60
61 std::map<fntColorCacheKey,gLookup> colorcache;
62
63 static gLookup &getColor(const gPalette &pal, const gRGB &start, const gRGB &end)
64 {
65         fntColorCacheKey key(start, end);
66         std::map<fntColorCacheKey,gLookup>::iterator i=colorcache.find(key);
67         if (i != colorcache.end())
68                 return i->second;
69         gLookup &n=colorcache.insert(std::pair<fntColorCacheKey,gLookup>(key,gLookup())).first->second;
70 //      eDebug("[FONT] creating new font color cache entry %02x%02x%02x%02x .. %02x%02x%02x%02x", start.a, start.r, start.g, start.b,
71 //              end.a, end.r, end.g, end.b);
72         n.build(16, pal, start, end);
73 //      for (int i=0; i<16; i++)
74 //              eDebugNoNewLine("%02x|%02x%02x%02x%02x ", (int)n.lookup[i], pal.data[n.lookup[i]].a, pal.data[n.lookup[i]].r, pal.data[n.lookup[i]].g, pal.data[n.lookup[i]].b);
75 //      eDebug("");
76         return n;
77 }
78
79 fontRenderClass *fontRenderClass::getInstance()
80 {
81         return instance;
82 }
83
84 FT_Error myFTC_Face_Requester(FTC_FaceID        face_id,
85                                                                                                                         FT_Library      library,
86                                                                                                                         FT_Pointer      request_data,
87                                                                                                                         FT_Face*                aface)
88 {
89         return ((fontRenderClass*)request_data)->FTC_Face_Requester(face_id, aface);
90 }
91
92
93 FT_Error fontRenderClass::FTC_Face_Requester(FTC_FaceID face_id, FT_Face* aface)
94 {
95         fontListEntry *font=(fontListEntry *)face_id;
96         if (!font)
97                 return -1;
98         
99 //      eDebug("[FONT] FTC_Face_Requester (%s)", font->face.c_str());
100
101         int error;
102         if ((error=FT_New_Face(library, font->filename.c_str(), 0, aface)))
103         {
104                 eDebug(" failed: %s", strerror(error));
105                 return error;
106         }
107         FT_Select_Charmap(*aface, ft_encoding_unicode);
108         return 0;
109 }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
110
111 FTC_FaceID fontRenderClass::getFaceID(const std::string &face)
112 {
113         for (fontListEntry *f=font; f; f=f->next)
114         {
115                 if (f->face == face)
116                         return (FTC_FaceID)f;
117         }
118         return 0;
119 }
120
121 FT_Error fontRenderClass::getGlyphBitmap(FTC_Image_Desc *font, FT_ULong glyph_index, FTC_SBit *sbit)
122 {
123         FT_Error res=FTC_SBit_Cache_Lookup(sbitsCache, font, glyph_index, sbit);
124         return res;
125 }
126
127 std::string fontRenderClass::AddFont(const std::string &filename, const std::string &name, int scale)
128 {
129         eDebugNoNewLine("[FONT] adding font %s...", filename.c_str());
130         fflush(stdout);
131         int error;
132         fontListEntry *n=new fontListEntry;
133
134         n->scale=scale;
135         FT_Face face;
136         singleLock s(ftlock);
137
138         if ((error=FT_New_Face(library, filename.c_str(), 0, &face)))
139                 eFatal(" failed: %s", strerror(error));
140
141         n->filename=filename;
142         n->face=name;
143         FT_Done_Face(face);
144
145         n->next=font;
146         eDebug("OK (%s)", n->face.c_str());
147         font=n;
148
149         return n->face;
150 }
151
152 fontRenderClass::fontListEntry::~fontListEntry()
153 {
154 }
155
156 fontRenderClass::fontRenderClass(): fb(fbClass::getInstance())
157 {
158         instance=this;
159         eDebug("[FONT] initializing lib...");
160         {
161                 if (FT_Init_FreeType(&library))
162                 {
163                         eDebug("[FONT] initializing failed.");
164                         return;
165                 }
166         }
167         eDebug("[FONT] loading fonts...");
168         fflush(stdout);
169         font=0;
170         
171         int maxbytes=4*1024*1024;
172         eDebug("[FONT] Intializing font cache, using max. %dMB...", maxbytes/1024/1024);
173         fflush(stdout);
174         {
175                 if (FTC_Manager_New(library, 8, 8, maxbytes, myFTC_Face_Requester, this, &cacheManager))
176                 {
177                         eDebug("[FONT] initializing font cache failed!");
178                         return;
179                 }
180                 if (!cacheManager)
181                 {
182                         eDebug("[FONT] initializing font cache manager error.");
183                         return;
184                 }
185                 if (FTC_SBit_Cache_New(cacheManager, &sbitsCache))
186                 {
187                         eDebug("[FONT] initializing font cache sbit failed!");
188                         return;
189                 }
190                 if (FTC_Image_Cache_New(cacheManager, &imageCache))
191                 {
192                         eDebug("[FONT] initializing font cache imagecache failed!");
193                 }
194         }
195         return;
196 }
197
198 float fontRenderClass::getLineHeight(const gFont& font)
199 {
200         if (!instance)
201                 return 0;
202         ePtr<Font> fnt;
203         getFont(fnt, font.family.c_str(), font.pointSize);
204         if (!fnt)
205                 return 0;
206         singleLock s(ftlock);
207         FT_Face current_face;
208 #ifdef HAVE_FREETYPE2
209         if ((FTC_Manager_LookupFace(cacheManager, fnt->scaler.face_id, &current_face) < 0) ||
210             (FTC_Manager_LookupSize(cacheManager, &fnt->scaler, &fnt->size) < 0))
211 #else
212         if (FTC_Manager_Lookup_Size(cacheManager, &fnt->font.font, &current_face, &fnt->size)<0)
213 #endif
214         {
215                 eDebug("FTC_Manager_Lookup_Size failed!");
216                 return 0;
217         }
218         int linegap=current_face->size->metrics.height-(current_face->size->metrics.ascender+current_face->size->metrics.descender);
219         float height=(current_face->size->metrics.ascender+current_face->size->metrics.descender+linegap)/64.0;
220         return height;
221 }
222
223 fontRenderClass::~fontRenderClass()
224 {
225         singleLock s(ftlock);
226         while(font)
227         {
228                 fontListEntry *f=font;
229                 font=font->next;
230                 delete f;
231         }
232 //      auskommentiert weil freetype und enigma die kritische masse des suckens ueberschreiten. 
233 //      FTC_Manager_Done(cacheManager);
234 //      FT_Done_FreeType(library);
235 }
236
237 int fontRenderClass::getFont(ePtr<Font> &font, const std::string &face, int size, int tabwidth)
238 {
239         FTC_FaceID id=getFaceID(face);
240         if (!id)
241         {
242                 font = 0;
243                 return -1;
244         }
245         font = new Font(this, id, size * ((fontListEntry*)id)->scale / 100, tabwidth);
246         return 0;
247 }
248
249 void addFont(const char *filename, const char *alias, int scale_factor, int is_replacement)
250 {
251         fontRenderClass::getInstance()->AddFont(filename, alias, scale_factor);
252         if (is_replacement)
253                 eTextPara::setReplacementFont(alias);
254 }
255
256 DEFINE_REF(Font);
257
258 Font::Font(fontRenderClass *render, FTC_FaceID faceid, int isize, int tw): tabwidth(tw)
259 {
260         renderer=render;
261 #ifdef HAVE_FREETYPE2
262         font.face_id = faceid;
263         font.width = isize;
264         font.height = isize;
265         font.flags = FT_LOAD_DEFAULT;
266         scaler.face_id = faceid;
267         scaler.width = isize;
268         scaler.height = isize;
269         scaler.pixel = 1;
270 #else
271         font.font.face_id=faceid;
272         font.font.pix_width     = isize;
273         font.font.pix_height = isize;
274         font.image_type = ftc_image_grays;
275 #endif
276         height=isize;
277         if (tabwidth==-1)
278                 tabwidth=8*isize;
279 //      font.image_type |= ftc_image_flag_autohinted;
280 }
281
282 FT_Error Font::getGlyphBitmap(FT_ULong glyph_index, FTC_SBit *sbit)
283 {
284         return renderer->getGlyphBitmap(&font, glyph_index, sbit);
285 }
286
287 Font::~Font()
288 {
289 }
290
291 DEFINE_REF(eTextPara);
292
293 int eTextPara::appendGlyph(Font *current_font, FT_Face current_face, FT_UInt glyphIndex, int flags, int rflags)
294 {
295         FTC_SBit glyph;
296         if (current_font->getGlyphBitmap(glyphIndex, &glyph))
297                 return 1;
298
299         int nx=cursor.x();
300
301         nx+=glyph->xadvance;
302
303         if (
304                         (rflags&RS_WRAP) && 
305                         (nx >= area.right())
306                 )
307         {
308                 int cnt = 0;
309                 glyphString::reverse_iterator i(glyphs.rbegin());
310                         /* find first possibility (from end to begin) to break */
311                 while (i != glyphs.rend())
312                 {
313                         if (i->flags&(GS_CANBREAK|GS_ISFIRST)) /* stop on either space/hyphen/shy or start of line (giving up) */
314                                 break;
315                         cnt++;
316                         ++i;
317                 }
318
319                         /* if ... */
320                 if (i != glyphs.rend()  /* ... we found anything */
321                         && (i->flags&GS_CANBREAK) /* ...and this is a space/hyphen/soft-hyphen */
322                         && (!(i->flags & GS_ISFIRST)) /* ...and this is not an start of line (line with just a single space/hyphen) */
323                         && cnt ) /* ... and there are actual non-space characters after this */
324                 {
325                                 /* if we have a soft-hyphen, and used that for breaking, turn it into a real hyphen */
326                         if (i->flags & GS_SOFTHYPHEN)
327                         {
328                                 i->flags &= ~GS_SOFTHYPHEN;
329                                 i->flags |= GS_HYPHEN;
330                         }
331                         --i; /* skip the space/hypen/softhyphen */
332                         int linelength=cursor.x()-i->x;
333                         i->flags|=GS_ISFIRST; /* make this a line start */
334                         ePoint offset=ePoint(i->x, i->y);
335                         newLine(rflags);
336                         offset-=cursor;
337
338                                 /* and move everything to the end into the next line. */
339                         do
340                         {
341                                 i->x-=offset.x();
342                                 i->y-=offset.y();
343                                 i->bbox.moveBy(-offset.x(), -offset.y());
344                                 --lineChars.back();
345                                 ++charCount;
346                         } while (i-- != glyphs.rbegin()); // rearrange them into the next line
347                         cursor+=ePoint(linelength, 0);  // put the cursor after that line
348                 } else
349                 {
350                         if (cnt)
351                         {
352                                 newLine(rflags);
353                                 flags|=GS_ISFIRST;
354                         }
355                 }
356         }
357
358         int xadvance=glyph->xadvance, kern=0;
359
360         if (previous && use_kerning)
361         {
362                 FT_Vector delta;
363                 FT_Get_Kerning(current_face, previous, glyphIndex, ft_kerning_default, &delta);
364                 kern=delta.x>>6;
365         }
366
367         pGlyph ng;
368         ng.bbox.setLeft( ((flags&GS_ISFIRST)|(cursor.x()-1))+glyph->left );
369         ng.bbox.setTop( cursor.y() - glyph->top );
370         ng.bbox.setWidth( glyph->width );
371         ng.bbox.setHeight( glyph->height );
372
373         xadvance += kern;
374         ng.bbox.setWidth(xadvance);
375
376         ng.x = cursor.x()+kern;
377         ng.y = cursor.y();
378         ng.w = xadvance;
379         ng.font = current_font;
380         ng.glyph_index = glyphIndex;
381         ng.flags = flags;
382         glyphs.push_back(ng);
383         ++charCount;
384
385                 /* when we have a SHY, don't xadvance. It will either be the last in the line (when used for breaking), or not displayed. */
386         if (!(flags & GS_SOFTHYPHEN))
387                 cursor += ePoint(xadvance, 0);
388         previous = glyphIndex;
389         return 0;
390 }
391
392 void eTextPara::calc_bbox()
393 {
394         if (!glyphs.size())
395         {
396                 bboxValid = 0;
397                 boundBox = eRect();
398                 return;
399         }
400         
401         bboxValid = 1;
402
403         glyphString::iterator i(glyphs.begin());
404         
405         boundBox = i->bbox; 
406         ++i;
407
408         for (; i != glyphs.end(); ++i)
409         {
410                 if (i->flags & (GS_ISSPACE|GS_SOFTHYPHEN))
411                         continue;
412                 if ( i->bbox.left() < boundBox.left() )
413                         boundBox.setLeft( i->bbox.left() );
414                 if ( i->bbox.top() < boundBox.top() )
415                         boundBox.setTop( i->bbox.top() );
416                 if ( i->bbox.right() > boundBox.right() )
417                         boundBox.setRight( i->bbox.right() );
418                 if ( i->bbox.bottom() > boundBox.bottom() )
419                         boundBox.setBottom( i->bbox.bottom() );
420         }
421 //      eDebug("boundBox left = %i, top = %i, right = %i, bottom = %i", boundBox.left(), boundBox.top(), boundBox.right(), boundBox.bottom() );
422 }
423
424 void eTextPara::newLine(int flags)
425 {
426         if (maximum.width()<cursor.x())
427                 maximum.setWidth(cursor.x());
428         cursor.setX(left);
429         previous=0;
430         int linegap=current_face->size->metrics.height-(current_face->size->metrics.ascender+current_face->size->metrics.descender);
431
432         lineOffsets.push_back(cursor.y());
433         lineChars.push_back(charCount);
434         charCount=0;
435
436         cursor+=ePoint(0, (current_face->size->metrics.ascender+current_face->size->metrics.descender+linegap)>>6);
437
438         if (maximum.height()<cursor.y())
439                 maximum.setHeight(cursor.y());
440         previous=0;
441 }
442
443 eTextPara::~eTextPara()
444 {
445         clear();
446 }
447
448 void eTextPara::setFont(const gFont *font)
449 {
450         ePtr<Font> fnt, replacement;
451         fontRenderClass::getInstance()->getFont(fnt, font->family.c_str(), font->pointSize);
452         if (!fnt)
453                 eWarning("FONT '%s' MISSING!", font->family.c_str());
454         fontRenderClass::getInstance()->getFont(replacement, replacement_facename.c_str(), font->pointSize);
455         setFont(fnt, replacement);
456 }
457
458 std::string eTextPara::replacement_facename;
459 std::set<int> eTextPara::forced_replaces;
460
461 void eTextPara::setFont(Font *fnt, Font *replacement)
462 {
463         if (!fnt)
464                 return;
465         current_font=fnt;
466         replacement_font=replacement;
467         singleLock s(ftlock);
468
469                         // we ask for replacment_font first becauseof the cache
470         if (replacement_font)
471         {
472 #ifdef HAVE_FREETYPE2
473                 if ((FTC_Manager_LookupFace(fontRenderClass::instance->cacheManager,
474                                             replacement_font->scaler.face_id,
475                                             &replacement_face) < 0) ||
476                     (FTC_Manager_LookupSize(fontRenderClass::instance->cacheManager,
477                                             &replacement_font->scaler,
478                                             &replacement_font->size) < 0))
479 #else
480                 if (FTC_Manager_Lookup_Size(fontRenderClass::instance->cacheManager, 
481                                 &replacement_font->font.font, &replacement_face, 
482                                 &replacement_font->size)<0)
483 #endif
484                 {
485                         eDebug("FTC_Manager_Lookup_Size failed!");
486                         return;
487                 }
488         }
489         if (current_font)
490         {
491 #ifdef HAVE_FREETYPE2
492                 if ((FTC_Manager_LookupFace(fontRenderClass::instance->cacheManager,
493                                             current_font->scaler.face_id,
494                                             &current_face) < 0) ||
495                     (FTC_Manager_LookupSize(fontRenderClass::instance->cacheManager,
496                                             &current_font->scaler,
497                                             &current_font->size) < 0))
498 #else
499                 if (FTC_Manager_Lookup_Size(fontRenderClass::instance->cacheManager, &current_font->font.font, &current_face, &current_font->size)<0)
500 #endif
501                 {
502                         eDebug("FTC_Manager_Lookup_Size failed!");
503                         return;
504                 }
505         }
506 #ifndef HAVE_FREETYPE2
507         cache_current_font=&current_font->font.font;
508 #endif
509         previous=0;
510         use_kerning=FT_HAS_KERNING(current_face);
511 }
512
513 void
514 shape (std::vector<unsigned long> &string, const std::vector<unsigned long> &text);
515
516 int eTextPara::renderString(const char *string, int rflags)
517 {
518         singleLock s(ftlock);
519         
520         if (!current_font)
521                 return -1;
522
523 #ifdef HAVE_FREETYPE2
524         if ((FTC_Manager_LookupFace(fontRenderClass::instance->cacheManager,
525                                     current_font->scaler.face_id,
526                                     &current_face) < 0) ||
527             (FTC_Manager_LookupSize(fontRenderClass::instance->cacheManager,
528                                     &current_font->scaler,
529                                     &current_font->size) < 0))
530         {
531                 eDebug("FTC_Manager_Lookup_Size failed!");
532                 return -1;
533         }
534 #else
535         if (&current_font->font.font != cache_current_font)
536         {
537                 if (FTC_Manager_Lookup_Size(fontRenderClass::instance->cacheManager, &current_font->font.font, &current_face, &current_font->size)<0)
538                 {
539                         eDebug("FTC_Manager_Lookup_Size failed!");
540                         return -1;
541                 }
542                 cache_current_font=&current_font->font.font;
543         }
544 #endif
545
546         if (!current_face)
547                 eFatal("eTextPara::renderString: no current_face");
548         if (!current_face->size)
549                 eFatal("eTextPara::renderString: no current_face->size");
550
551         if (cursor.y()==-1)
552         {
553                 cursor=ePoint(area.x(), area.y()+(current_face->size->metrics.ascender>>6));
554                 left=cursor.x();
555         }
556
557         std::vector<unsigned long> uc_string, uc_visual;
558         if (string)
559                 uc_string.reserve(strlen(string));
560         
561         const char *p = string ? string : "";
562
563         while (*p)
564         {
565                 unsigned int unicode=(unsigned char)*p++;
566
567                 if (unicode & 0x80) // we have (hopefully) UTF8 here, and we assume that the encoding is VALID
568                 {
569                         if ((unicode & 0xE0)==0xC0) // two bytes
570                         {
571                                 unicode&=0x1F;
572                                 unicode<<=6;
573                                 if (*p)
574                                         unicode|=(*p++)&0x3F;
575                         } else if ((unicode & 0xF0)==0xE0) // three bytes
576                         {
577                                 unicode&=0x0F;
578                                 unicode<<=6;
579                                 if (*p)
580                                         unicode|=(*p++)&0x3F;
581                                 unicode<<=6;
582                                 if (*p)
583                                         unicode|=(*p++)&0x3F;
584                         } else if ((unicode & 0xF8)==0xF0) // four bytes
585                         {
586                                 unicode&=0x07;
587                                 unicode<<=6;
588                                 if (*p)
589                                         unicode|=(*p++)&0x3F;
590                                 unicode<<=6;
591                                 if (*p)
592                                         unicode|=(*p++)&0x3F;
593                                 unicode<<=6;
594                                 if (*p)
595                                         unicode|=(*p++)&0x3F;
596                         }
597                 }
598                 uc_string.push_back(unicode);
599         }
600
601         std::vector<unsigned long> uc_shape;
602
603                 // character -> glyph conversion
604         shape(uc_shape, uc_string);
605         
606                 // now do the usual logical->visual reordering
607         int size=uc_shape.size();
608 #ifdef HAVE_FRIBIDI
609         FriBidiCharType dir=FRIBIDI_TYPE_ON;
610         uc_visual.resize(size);
611         // gaaanz lahm, aber anders geht das leider nicht, sorry.
612         FriBidiChar array[size], target[size];
613         std::copy(uc_shape.begin(), uc_shape.end(), array);
614         fribidi_log2vis(array, size, &dir, target, 0, 0, 0);
615         uc_visual.assign(target, target+size);
616 #else
617         uc_visual=uc_shape;
618 #endif
619
620         glyphs.reserve(size);
621         
622         int nextflags = 0;
623         
624         for (std::vector<unsigned long>::const_iterator i(uc_visual.begin());
625                 i != uc_visual.end(); ++i)
626         {
627                 int isprintable=1;
628                 int flags = nextflags;
629                 nextflags = 0;
630                 unsigned long chr = *i;
631
632                 if (!(rflags&RS_DIRECT))
633                 {
634                         switch (chr)
635                         {
636                         case '\\':
637                         {
638                                 unsigned long c = *(i+1);
639                                 switch (c)
640                                 {
641                                         case 'n':
642                                                 i++;
643                                                 goto newline;
644                                         case 't':
645                                                 i++;
646                                                 goto tab;
647                                         case 'r':
648                                                 i++;
649                                                 goto nprint;
650                                         default:
651                                         ;
652                                 }
653                                 break;
654                         }
655                         case '\t':
656 tab:            isprintable=0;
657                                 cursor+=ePoint(current_font->tabwidth, 0);
658                                 cursor-=ePoint(cursor.x()%current_font->tabwidth, 0);
659                                 break;
660                         case 0x8A:
661                         case 0xE08A:
662                         case '\n':
663 newline:isprintable=0;
664                                 newLine(rflags);
665                                 nextflags|=GS_ISFIRST;
666                                 break;
667                         case '\r':
668                         case 0x86: case 0xE086:
669                         case 0x87: case 0xE087:
670 nprint: isprintable=0;
671                                 break;
672                         case 0xAD: // soft-hyphen
673                                 flags |= GS_SOFTHYPHEN;
674                                 chr = 0x2010; /* hyphen */
675                                 break;
676                         case 0x2010:
677                         case '-':
678                                 flags |= GS_HYPHEN;
679                                 break;
680                         case ' ':
681                                 flags|=GS_ISSPACE;
682                         default:
683                                 break;
684                         }
685                 }
686                 if (isprintable)
687                 {
688                         FT_UInt index = 0;
689
690                                 /* FIXME: our font doesn't seem to have a hyphen, so use hyphen-minus for it. */
691                         if (chr == 0x2010)
692                                 chr = '-';
693
694                         if (forced_replaces.find(chr) == forced_replaces.end())
695                                 index=(rflags&RS_DIRECT)? chr : FT_Get_Char_Index(current_face, chr);
696
697                         if (!index)
698                         {
699                                 if (replacement_face)
700                                         index=(rflags&RS_DIRECT)? chr : FT_Get_Char_Index(replacement_face, chr);
701
702                                 if (!index)
703                                         eDebug("unicode U+%4lx not present", chr);
704                                 else
705                                         appendGlyph(replacement_font, replacement_face, index, flags, rflags);
706                         } else
707                                 appendGlyph(current_font, current_face, index, flags, rflags);
708                 }
709         }
710         bboxValid=false;
711         calc_bbox();
712 #ifdef HAVE_FRIBIDI
713         if (dir & FRIBIDI_MASK_RTL)
714         {
715                 realign(dirRight);
716                 doTopBottomReordering=true;
717         }
718 #endif
719
720         if (charCount)
721         {
722                 lineOffsets.push_back(cursor.y());
723                 lineChars.push_back(charCount);
724                 charCount=0;
725         }
726
727         return 0;
728 }
729
730 void eTextPara::blit(gDC &dc, const ePoint &offset, const gRGB &background, const gRGB &foreground)
731 {
732         singleLock s(ftlock);
733         
734         if (!current_font)
735                 return;
736
737 #ifdef HAVE_FREETYPE2
738         if ((FTC_Manager_LookupFace(fontRenderClass::instance->cacheManager,
739                                     current_font->scaler.face_id,
740                                     &current_face) < 0) ||
741             (FTC_Manager_LookupSize(fontRenderClass::instance->cacheManager,
742                                     &current_font->scaler,
743                                     &current_font->size) < 0))
744         {
745                 eDebug("FTC_Manager_Lookup_Size failed!");
746                 return;
747         }
748 #else
749         if (&current_font->font.font != cache_current_font)
750         {
751                 if (FTC_Manager_Lookup_Size(fontRenderClass::instance->cacheManager, &current_font->font.font, &current_face, &current_font->size)<0)
752                 {
753                         eDebug("FTC_Manager_Lookup_Size failed!");
754                         return;
755                 }
756                 cache_current_font=&current_font->font.font;
757         }
758 #endif
759
760         ePtr<gPixmap> target;
761         dc.getPixmap(target);
762         gSurface *surface = target->surface;
763
764         register int opcode;
765
766         gColor *lookup8, lookup8_invert[16];
767         gColor *lookup8_normal=0;
768
769         __u32 lookup32_normal[16], lookup32_invert[16], *lookup32;
770         
771         if (surface->bpp == 8)
772         {
773                 if (surface->clut.data)
774                 {
775                         lookup8_normal=getColor(surface->clut, background, foreground).lookup;
776                         
777                         int i;
778                         for (i=0; i<16; ++i)
779                                 lookup8_invert[i] = lookup8_normal[i^0xF];
780                         
781                         opcode=0;
782                 } else
783                         opcode=1;
784         } else if (surface->bpp == 32)
785         {
786                 opcode=3;
787
788                 for (int i=0; i<16; ++i)
789                 {
790 #define BLEND(y, x, a) (y + (((x-y) * a)>>8))
791
792                         unsigned char da = background.a, dr = background.r, dg = background.g, db = background.b;
793                         int sa = i * 16;
794                         if (sa < 256)
795                         {
796                                 da = BLEND(background.a, foreground.a, sa) & 0xFF;
797                                 dr = BLEND(background.r, foreground.r, sa) & 0xFF;
798                                 dg = BLEND(background.g, foreground.g, sa) & 0xFF;
799                                 db = BLEND(background.b, foreground.b, sa) & 0xFF;
800                         }
801 #undef BLEND
802                         da ^= 0xFF;
803                         lookup32_normal[i]=db | (dg << 8) | (dr << 16) | (da << 24);;
804                 }
805                 for (int i=0; i<16; ++i)
806                         lookup32_invert[i]=lookup32_normal[i^0xF];
807         } else
808         {
809                 eWarning("can't render to %dbpp", surface->bpp);
810                 return;
811         }
812         
813         gRegion area(eRect(0, 0, surface->x, surface->y));
814         gRegion clip = dc.getClip() & area;
815
816         int buffer_stride=surface->stride;
817
818         for (unsigned int c = 0; c < clip.rects.size(); ++c)
819         {
820                 std::list<int>::reverse_iterator line_offs_it(lineOffsets.rbegin());
821                 std::list<int>::iterator line_chars_it(lineChars.begin());
822                 int line_offs=0;
823                 int line_chars=0;
824                 for (glyphString::iterator i(glyphs.begin()); i != glyphs.end(); ++i, --line_chars)
825                 {
826                         while(!line_chars)
827                         {
828                                 line_offs = *(line_offs_it++);
829                                 line_chars = *(line_chars_it++);
830                         }
831
832                         if (i->flags & GS_SOFTHYPHEN)
833                                 continue;
834
835                         if (!(i->flags & GS_INVERT))
836                         {
837                                 lookup8 = lookup8_normal;
838                                 lookup32 = lookup32_normal;
839                         } else
840                         {
841                                 lookup8 = lookup8_invert;
842                                 lookup32 = lookup32_invert;
843                         }
844
845                         static FTC_SBit glyph_bitmap;
846                         if (fontRenderClass::instance->getGlyphBitmap(&i->font->font, i->glyph_index, &glyph_bitmap))
847                                 continue;
848                         int rx=i->x+glyph_bitmap->left + offset.x();
849                         int ry=(doTopBottomReordering ? line_offs : i->y) - glyph_bitmap->top + offset.y();
850
851                         __u8 *d=(__u8*)(surface->data)+buffer_stride*ry+rx*surface->bypp;
852                         __u8 *s=glyph_bitmap->buffer;
853                         register int sx=glyph_bitmap->width;
854                         int sy=glyph_bitmap->height;
855                         if ((sy+ry) >= clip.rects[c].bottom())
856                                 sy=clip.rects[c].bottom()-ry;
857                         if ((sx+rx) >= clip.rects[c].right())
858                                 sx=clip.rects[c].right()-rx;
859                         if (rx < clip.rects[c].left())
860                         {
861                                 int diff=clip.rects[c].left()-rx;
862                                 s+=diff;
863                                 sx-=diff;
864                                 rx+=diff;
865                                 d+=diff*surface->bypp;
866                         }
867                         if (ry < clip.rects[c].top())
868                         {
869                                 int diff=clip.rects[c].top()-ry;
870                                 s+=diff*glyph_bitmap->pitch;
871                                 sy-=diff;
872                                 ry+=diff;
873                                 d+=diff*buffer_stride;
874                         }
875                         if (sx>0)
876                                 for (int ay=0; ay<sy; ay++)
877                                 {
878                                         if (!opcode)            // 4bit lookup to 8bit
879                                         {
880                                                 register __u8 *td=d;
881                                                 register int ax;
882                                                 
883                                                 for (ax=0; ax<sx; ax++)
884                                                 {       
885                                                         register int b=(*s++)>>4;
886                                                         if(b)
887                                                                 *td++=lookup8[b];
888                                                         else
889                                                                 td++;
890                                                 }
891                                         } else if (opcode == 1) // 8bit direct
892                                         {
893                                                 register __u8 *td=d;
894                                                 register int ax;
895                                                 for (ax=0; ax<sx; ax++)
896                                                 {       
897                                                         register int b=*s++;
898                                                         *td++^=b;
899                                                 }
900                                         } else
901                                         {
902                                                 register __u32 *td=(__u32*)d;
903                                                 register int ax;
904                                                 for (ax=0; ax<sx; ax++)
905                                                 {       
906                                                         register int b=(*s++)>>4;
907                                                         if(b)
908                                                                 *td++=lookup32[b];
909                                                         else
910                                                                 td++;
911                                                 }
912                                         }
913                                         s+=glyph_bitmap->pitch-sx;
914                                         d+=buffer_stride;
915                                 }
916                 }
917         }
918 }
919
920 void eTextPara::realign(int dir)        // der code hier ist ein wenig merkwuerdig.
921 {
922         glyphString::iterator begin(glyphs.begin()), c(glyphs.begin()), end(glyphs.begin()), last;
923         if (dir==dirLeft)
924                 return;
925         while (c != glyphs.end())
926         {
927                 int linelength=0;
928                 int numspaces=0, num=0;
929                 begin=end;
930                 
931                 ASSERT( end != glyphs.end());
932                 
933                         // zeilenende suchen
934                 do {
935                         last=end;
936                         ++end;
937                 } while ((end != glyphs.end()) && (!(end->flags&GS_ISFIRST)));
938                         // end zeigt jetzt auf begin der naechsten zeile
939                 
940                 for (c=begin; c!=end; ++c)
941                 {
942                                 // space am zeilenende skippen
943                         if ((c==last) && (c->flags&GS_ISSPACE))
944                                 continue;
945
946                         if (c->flags&GS_ISSPACE)
947                                 numspaces++;
948                         linelength+=c->w;
949                         num++;
950                 }
951
952                 switch (dir)
953                 {
954                 case dirRight:
955                 case dirCenter:
956                 {
957                         int offset=area.width()-linelength;
958                         if (dir==dirCenter)
959                                 offset/=2;
960                         offset+=area.left();
961                         while (begin != end)
962                         {
963                                 begin->bbox.moveBy(offset-begin->x,0);
964                                 begin->x=offset;
965                                 offset+=begin->w;
966                                 ++begin;
967                         }
968                         break;
969                 }
970                 case dirBlock:
971                 {
972                         if (end == glyphs.end())                // letzte zeile linksbuendig lassen
973                                 continue;
974                         int spacemode;
975                         if (numspaces)
976                                 spacemode=1;
977                         else
978                                 spacemode=0;
979                         if ((!spacemode) && (num<2))
980                                 break;
981                         int off=(area.width()-linelength)*256/(spacemode?numspaces:(num-1));
982                         int curoff=0;
983                         while (begin != end)
984                         {
985                                 int doadd=0;
986                                 if ((!spacemode) || (begin->flags&GS_ISSPACE))
987                                         doadd=1;
988                                 begin->x+=curoff>>8;
989                                 begin->bbox.moveBy(curoff>>8,0);
990                                 if (doadd)
991                                         curoff+=off;
992                                 ++begin;
993                         }
994                         break;
995                 }
996                 }
997         }
998         bboxValid=false;
999         calc_bbox();
1000 }
1001
1002 void eTextPara::clear()
1003 {
1004         singleLock s(ftlock);
1005
1006         current_font = 0;
1007         replacement_font = 0;
1008
1009         glyphs.clear();
1010 }
1011
1012 eAutoInitP0<fontRenderClass> init_fontRenderClass(eAutoInitNumbers::graphic-1, "Font Render Class");