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