yes! ich habs kaputt gemacht! (doesn't compile anymore, doesn't work anymore,
[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
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 eString &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 eString fontRenderClass::AddFont(const eString &filename, const eString &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/2.0)/64;
209         delete fnt;
210         return height;
211 }
212
213
214 fontRenderClass::~fontRenderClass()
215 {
216         singleLock s(ftlock);
217         while(font)
218         {
219                 fontListEntry *f=font;
220                 font=font->next;
221                 delete f;
222         }
223 //      auskommentiert weil freetype und enigma die kritische masse des suckens ueberschreiten. 
224 //      FTC_Manager_Done(cacheManager);
225 //      FT_Done_FreeType(library);
226 }
227
228 int fontRenderClass::getFont(ePtr<Font> &font, const eString &face, int size, int tabwidth)
229 {
230         FTC_FaceID id=getFaceID(face);
231         if (!id)
232         {
233                 font = 0;
234                 return -1;
235         }
236         font = new Font(this, id, size * ((fontListEntry*)id)->scale / 100, tabwidth);
237         return 0;
238 }
239
240 DEFINE_REF(Font);
241
242 Font::Font(fontRenderClass *render, FTC_FaceID faceid, int isize, int tw): ref(0), tabwidth(tw)
243 {
244         renderer=render;
245         font.font.face_id=faceid;
246         font.font.pix_width     = isize;
247         font.font.pix_height = isize;
248         font.image_type = ftc_image_grays;
249         height=isize;
250         if (tabwidth==-1)
251                 tabwidth=8*isize;
252         ref=0;
253 //      font.image_type |= ftc_image_flag_autohinted;
254 }
255
256 FT_Error Font::getGlyphBitmap(FT_ULong glyph_index, FTC_SBit *sbit)
257 {
258         return renderer->getGlyphBitmap(&font, glyph_index, sbit);
259 }
260
261 Font::~Font()
262 {
263 }
264
265 int eTextPara::appendGlyph(Font *current_font, FT_Face current_face, FT_UInt glyphIndex, int flags, int rflags)
266 {
267         FTC_SBit glyph;
268         if (current_font->getGlyphBitmap(glyphIndex, &glyph))
269                 return 1;
270
271         int nx=cursor.x();
272
273         nx+=glyph->xadvance;
274         
275         if (
276                         (rflags&RS_WRAP) && 
277                         (nx >= area.right())
278                 )
279         {
280                 int cnt = 0;
281                 glyphString::reverse_iterator i(glyphs.rbegin());
282                 while (i != glyphs.rend())
283                 {
284                         if (i->flags&(GS_ISSPACE|GS_ISFIRST))
285                                 break;
286                         cnt++;
287                         ++i;
288                 } 
289                 if (i != glyphs.rend()
290                         && ((i->flags&(GS_ISSPACE|GS_ISFIRST))==GS_ISSPACE)
291                         && cnt )
292                 {
293                         --i;
294                         int linelength=cursor.x()-i->x;
295                         i->flags|=GS_ISFIRST;
296                         ePoint offset=ePoint(i->x, i->y);
297                         newLine(rflags);
298                         offset-=cursor;
299                         do
300                         {
301                                 i->x-=offset.x();
302                                 i->y-=offset.y();
303                                 i->bbox.moveBy(-offset.x(), -offset.y());
304                         }
305                         while (i-- != glyphs.rbegin()); // rearrange them into the next line
306                         cursor+=ePoint(linelength, 0);  // put the cursor after that line
307                 }
308                 else
309                 {
310                         if (cnt)
311                         {
312                                 newLine(rflags);
313                                 flags|=GS_ISFIRST;
314                         }
315                 }
316         }
317
318         int xadvance=glyph->xadvance, kern=0;
319         
320         if (previous && use_kerning)
321         {
322                 FT_Vector delta;
323                 FT_Get_Kerning(current_face, previous, glyphIndex, ft_kerning_default, &delta);
324                 kern=delta.x>>6;
325         }
326
327         pGlyph ng;
328         ng.bbox.setLeft( (flags&GS_ISFIRST|cursor.x()-1)+glyph->left );
329         ng.bbox.setTop( cursor.y() - glyph->top );
330         ng.bbox.setWidth( glyph->width );
331         ng.bbox.setHeight( glyph->height );
332
333         xadvance+=kern;
334
335         ng.x=cursor.x()+kern;
336         ng.y=cursor.y();
337         ng.w=xadvance;
338         ng.font=current_font;
339         ng.glyph_index=glyphIndex;
340         ng.flags=flags;
341         glyphs.push_back(ng);
342
343         cursor+=ePoint(xadvance, 0);
344         previous=glyphIndex;
345         return 0;
346 }
347
348 void eTextPara::calc_bbox()
349 {
350         boundBox.setLeft( 32000 );
351         boundBox.setTop( 32000 );
352         boundBox.setRight( -32000 );         // for each glyph image, compute its bounding box, translate it,
353         boundBox.setBottom( -32000 );
354         // and grow the string bbox
355
356         for ( glyphString::iterator i(glyphs.begin()); i != glyphs.end(); ++i)
357         {
358                 if ( i->flags & GS_ISSPACE )
359                         continue;
360                 if ( i->bbox.left() < boundBox.left() )
361                         boundBox.setLeft( i->bbox.left() );
362                 if ( i->bbox.top() < boundBox.top() )
363                         boundBox.setTop( i->bbox.top() );
364                 if ( i->bbox.right() > boundBox.right() )
365                         boundBox.setRight( i->bbox.right() );
366                 if ( i->bbox.bottom() > boundBox.bottom() )
367                         boundBox.setBottom( i->bbox.bottom() );
368         }
369 //      eDebug("boundBox left = %i, top = %i, right = %i, bottom = %i", boundBox.left(), boundBox.top(), boundBox.right(), boundBox.bottom() );
370         if ( glyphs.size() )
371                 bboxValid=1;
372 }
373
374 void eTextPara::newLine(int flags)
375 {
376         if (maximum.width()<cursor.x())
377                 maximum.setWidth(cursor.x());
378         cursor.setX(left);
379         previous=0;
380         int linegap=current_face->size->metrics.height-(current_face->size->metrics.ascender+current_face->size->metrics.descender);
381         cursor+=ePoint(0, (current_face->size->metrics.ascender+current_face->size->metrics.descender+linegap*1/2)>>6);
382         if (maximum.height()<cursor.y())
383                 maximum.setHeight(cursor.y());
384         previous=0;
385 }
386
387 eTextPara::~eTextPara()
388 {
389         clear();
390         if (refcnt>=0)
391                 eFatal("verdammt man der war noch gelockt :/\n");
392 }
393
394 void eTextPara::destroy()
395 {
396         singleLock s(refcntlck);
397
398         if (!refcnt--)
399                 delete this;
400 }
401
402 eTextPara *eTextPara::grab()
403 {
404         singleLock s(refcntlck);
405
406         refcnt++;
407         return this;
408 }
409
410 void eTextPara::setFont(const gFont *font)
411 {
412         if (refcnt)
413                 eFatal("mod. after lock");
414         ePtr<Font> fnt, replacement;
415         fontRenderClass::getInstance()->getFont(fnt, font->family.c_str(), font->pointSize);
416         if (!fnt)
417                 eWarning("FONT '%s' MISSING!", font->family.c_str());
418         fontRenderClass::getInstance()->getFont(replacement, replacement_facename.c_str(), font->pointSize);
419         setFont(fnt, replacement);
420 }
421
422 eString eTextPara::replacement_facename;
423
424 void eTextPara::setFont(Font *fnt, Font *replacement)
425 {
426         if (refcnt)
427                 eFatal("mod. after lock");
428         if (!fnt)
429                 return;
430         current_font=fnt;
431         replacement_font=replacement;
432         singleLock s(ftlock);
433
434                         // we ask for replacment_font first becauseof the cache
435         if (replacement_font)
436         {
437                 if (FTC_Manager_Lookup_Size(fontRenderClass::instance->cacheManager, 
438                                 &replacement_font->font.font, &replacement_face, 
439                                 &replacement_font->size)<0)
440                 {
441                         eDebug("FTC_Manager_Lookup_Size failed!");
442                         return;
443                 }
444         }
445         if (current_font)
446         {
447                 if (FTC_Manager_Lookup_Size(fontRenderClass::instance->cacheManager, &current_font->font.font, &current_face, &current_font->size)<0)
448                 {
449                         eDebug("FTC_Manager_Lookup_Size failed!");
450                         return;
451                 }
452         }
453         cache_current_font=&current_font->font.font;
454         previous=0;
455         use_kerning=FT_HAS_KERNING(current_face);
456 }
457
458 void
459 shape (std::vector<unsigned long> &string, const std::vector<unsigned long> &text);
460
461 int eTextPara::renderString(const eString &string, int rflags)
462 {
463         singleLock s(ftlock);
464         
465         if (refcnt)
466                 eFatal("mod. after lock");
467
468         if (!current_font)
469                 return -1;
470                 
471         if (cursor.y()==-1)
472         {
473                 cursor=ePoint(area.x(), area.y()+(current_face->size->metrics.ascender>>6));
474                 left=cursor.x();
475         }
476                 
477         if (&current_font->font.font != cache_current_font)
478         {
479                 if (FTC_Manager_Lookup_Size(fontRenderClass::instance->cacheManager, &current_font->font.font, &current_face, &current_font->size)<0)
480                 {
481                         eDebug("FTC_Manager_Lookup_Size failed!");
482                         return -1;
483                 }
484                 cache_current_font=&current_font->font.font;
485         }
486         
487         std::vector<unsigned long> uc_string, uc_visual;
488         uc_string.reserve(string.length());
489         
490         eString::const_iterator p(string.begin());
491
492         while(p != string.end())
493         {
494                 unsigned int unicode=*p++;
495
496                 if (unicode & 0x80) // we have (hopefully) UTF8 here, and we assume that the encoding is VALID
497                 {
498                         if ((unicode & 0xE0)==0xC0) // two bytes
499                         {
500                                 unicode&=0x1F;
501                                 unicode<<=6;
502                                 if (p != string.end())
503                                         unicode|=(*p++)&0x3F;
504                         } else if ((unicode & 0xF0)==0xE0) // three bytes
505                         {
506                                 unicode&=0x0F;
507                                 unicode<<=6;
508                                 if (p != string.end())
509                                         unicode|=(*p++)&0x3F;
510                                 unicode<<=6;
511                                 if (p != string.end())
512                                         unicode|=(*p++)&0x3F;
513                         } else if ((unicode & 0xF8)==0xF0) // four bytes
514                         {
515                                 unicode&=0x07;
516                                 unicode<<=6;
517                                 if (p != string.end())
518                                         unicode|=(*p++)&0x3F;
519                                 unicode<<=6;
520                                 if (p != string.end())
521                                         unicode|=(*p++)&0x3F;
522                                 unicode<<=6;
523                                 if (p != string.end())
524                                         unicode|=(*p++)&0x3F;
525                         }
526                 }
527                 uc_string.push_back(unicode);
528         }
529
530         std::vector<unsigned long> uc_shape;
531
532                 // character -> glyph conversion
533         shape(uc_shape, uc_string);
534         
535                 // now do the usual logical->visual reordering
536 #ifdef HAVE_FRIBIDI     
537         FriBidiCharType dir=FRIBIDI_TYPE_ON;
538         {
539                 int size=uc_shape.size();
540                 uc_visual.resize(size);
541                 // gaaanz lahm, aber anders geht das leider nicht, sorry.
542                 FriBidiChar array[size], target[size];
543                 std::copy(uc_shape.begin(), uc_shape.end(), array);
544                 fribidi_log2vis(array, size, &dir, target, 0, 0, 0);
545                 uc_visual.assign(target, target+size);
546         }
547 #else
548         uc_visual=uc_shape;
549 #endif
550
551         glyphs.reserve(uc_visual.size());
552         
553         int nextflags = 0;
554         
555         for (std::vector<unsigned long>::const_iterator i(uc_visual.begin());
556                 i != uc_visual.end(); ++i)
557         {
558                 int isprintable=1;
559                 int flags = nextflags;
560                 nextflags = 0;
561                 if (!(rflags&RS_DIRECT))
562                 {
563                         switch (*i)
564                         {
565                         case '\\':
566                         {
567                                 unsigned long c = *(i+1);
568                                 switch (c)
569                                 {
570                                         case 'n':
571                                                 i++;
572                                                 goto newline;
573                                         case 't':
574                                                 i++;
575                                                 goto tab;
576                                         case 'r':
577                                                 i++;
578                                                 goto nprint;
579                                         default:
580                                         ;
581                                 }
582                                 break;
583                         }
584                         case '\t':
585 tab:            isprintable=0;
586                                 cursor+=ePoint(current_font->tabwidth, 0);
587                                 cursor-=ePoint(cursor.x()%current_font->tabwidth, 0);
588                                 break;
589                         case 0x8A:
590                         case 0xE08A:
591                         case '\n':
592 newline:isprintable=0;
593                                 newLine(rflags);
594                                 nextflags|=GS_ISFIRST;
595                                 break;
596                         case '\r':
597                         case 0x86: case 0xE086:
598                         case 0x87: case 0xE087:
599 nprint: isprintable=0;
600                                 break;
601                         case ' ':
602                                 flags|=GS_ISSPACE;
603                         default:
604                                 break;
605                         }
606                 }
607                 if (isprintable)
608                 {
609                         FT_UInt index;
610
611                         index=(rflags&RS_DIRECT)? *i : FT_Get_Char_Index(current_face, *i);
612
613                         if (!index)
614                         {
615                                 if (replacement_face)
616                                         index=(rflags&RS_DIRECT)? *i : FT_Get_Char_Index(replacement_face, *i);
617
618                                 if (!index)
619                                         eDebug("unicode %d ('%c') not present", *i, *i);
620                                 else
621                                         appendGlyph(replacement_font, replacement_face, index, flags, rflags);
622                         } else
623                                 appendGlyph(current_font, current_face, index, flags, rflags);
624                 }
625         }
626         bboxValid=false;
627         calc_bbox();
628 #ifdef HAVE_FRIBIDI
629         if (dir & FRIBIDI_MASK_RTL)
630                 realign(dirRight);
631 #endif
632         return 0;
633 }
634
635 void eTextPara::blit(gDC &dc, const ePoint &offset, const gRGB &background, const gRGB &foreground)
636 {
637         singleLock s(ftlock);
638         
639         if (!current_font)
640                 return;
641
642         if (&current_font->font.font != cache_current_font)
643         {
644                 if (FTC_Manager_Lookup_Size(fontRenderClass::instance->cacheManager, &current_font->font.font, &current_face, &current_font->size)<0)
645                 {
646                         eDebug("FTC_Manager_Lookup_Size failed!");
647                         return;
648                 }
649                 cache_current_font=&current_font->font.font;
650         }
651
652         ePtr<gPixmap> target;
653         dc.getPixmap(target);
654         gSurface *surface = target->surface;
655
656         register int opcode;
657         gColor *lookup8=0;
658         __u32 lookup32[16];
659                 
660         if (surface->bpp == 8)
661         {
662                 if (surface->clut.data)
663                 {
664                         lookup8=getColor(surface->clut, background, foreground).lookup;
665                         opcode=0;
666                 } else
667                         opcode=1;
668         } else if (surface->bpp == 32)
669         {
670                 opcode=3;
671                 if (surface->clut.data)
672                 {
673                         lookup8=getColor(surface->clut, background, foreground).lookup;
674                         for (int i=0; i<16; ++i)
675                                 lookup32[i]=((surface->clut.data[lookup8[i]].a<<24)|
676                                         (surface->clut.data[lookup8[i]].r<<16)|
677                                         (surface->clut.data[lookup8[i]].g<<8)|
678                                         (surface->clut.data[lookup8[i]].b))^0xFF000000;
679                 } else
680                 {
681                         for (int i=0; i<16; ++i)
682                                 lookup32[i]=(0x010101*i)|0xFF000000;
683                 }
684         } else
685         {
686                 eWarning("can't render to %dbpp", surface->bpp);
687                 return;
688         }
689         
690         gRegion area(eRect(0, 0, surface->x, surface->y));
691         gRegion clip;
692         clip.intersect(area, dc.getClip());
693
694         int buffer_stride=surface->stride;
695
696         for (glyphString::iterator i(glyphs.begin()); i != glyphs.end(); ++i)
697         {
698                 static FTC_SBit glyph_bitmap;
699                 if (fontRenderClass::instance->getGlyphBitmap(&i->font->font, i->glyph_index, &glyph_bitmap))
700                         continue;
701                 int rx=i->x+glyph_bitmap->left + offset.x();
702                 int ry=i->y-glyph_bitmap->top  + offset.y();
703                 __u8 *d=(__u8*)(surface->data)+buffer_stride*ry+rx*surface->bypp;
704                 __u8 *s=glyph_bitmap->buffer;
705                 register int sx=glyph_bitmap->width;
706                 int sy=glyph_bitmap->height;
707                 if ((sy+ry) >= clip.extends.bottom())
708                         sy=clip.extends.bottom()-ry;
709                 if ((sx+rx) >= clip.extends.right())
710                         sx=clip.extends.right()-rx;
711                 if (rx < clip.extends.left())
712                 {
713                         int diff=clip.extends.left()-rx;
714                         s+=diff;
715                         sx-=diff;
716                         rx+=diff;
717                         d+=diff*surface->bypp;
718                 }
719                 if (ry < clip.extends.top())
720                 {
721                         int diff=clip.extends.top()-ry;
722                         s+=diff*glyph_bitmap->pitch;
723                         sy-=diff;
724                         ry+=diff;
725                         d+=diff*buffer_stride;
726                 }
727                 if (sx>0)
728                         for (int ay=0; ay<sy; ay++)
729                         {
730                                 if (!opcode)            // 4bit lookup to 8bit
731                                 {
732                                         register __u8 *td=d;
733                                         register int ax;
734                                         for (ax=0; ax<sx; ax++)
735                                         {       
736                                                 register int b=(*s++)>>4;
737                                                 if(b)
738                                                         *td++=lookup8[b];
739                                                 else
740                                                         td++;
741                                         }
742                                 } else if (opcode == 1) // 8bit direct
743                                 {
744                                         register __u8 *td=d;
745                                         register int ax;
746                                         for (ax=0; ax<sx; ax++)
747                                         {       
748                                                 register int b=*s++;
749                                                 *td++^=b;
750                                         }
751                                 } else
752                                 {
753                                         register __u32 *td=(__u32*)d;
754                                         register int ax;
755                                         for (ax=0; ax<sx; ax++)
756                                         {       
757                                                 register int b=(*s++)>>4;
758                                                 if(b)
759                                                         *td++=lookup32[b];
760                                                 else
761                                                         td++;
762                                         }
763                                 }
764                                 s+=glyph_bitmap->pitch-sx;
765                                 d+=buffer_stride;
766                         }
767         }
768 }
769
770 void eTextPara::realign(int dir)        // der code hier ist ein wenig merkwuerdig.
771 {
772         glyphString::iterator begin(glyphs.begin()), c(glyphs.begin()), end(glyphs.begin()), last;
773         if (dir==dirLeft)
774                 return;
775         while (c != glyphs.end())
776         {
777                 int linelength=0;
778                 int numspaces=0, num=0;
779                 begin=end;
780                 
781                 ASSERT( end != glyphs.end());
782                 
783                         // zeilenende suchen
784                 do {
785                         last=end;
786                         ++end;
787                 } while ((end != glyphs.end()) && (!(end->flags&GS_ISFIRST)));
788                         // end zeigt jetzt auf begin der naechsten zeile
789                 
790                 for (c=begin; c!=end; ++c)
791                 {
792                                 // space am zeilenende skippen
793                         if ((c==last) && (c->flags&GS_ISSPACE))
794                                 continue;
795
796                         if (c->flags&GS_ISSPACE)
797                                 numspaces++;
798                         linelength+=c->w;
799                         num++;
800                 }
801
802                 if (!num)               // line mit nur einem space
803                         continue;
804
805                 switch (dir)
806                 {
807                 case dirRight:
808                 case dirCenter:
809                 {
810                         int offset=area.width()-linelength;
811                         if (dir==dirCenter)
812                                 offset/=2;
813                         offset+=area.left();
814                         while (begin != end)
815                         {
816                                 begin->bbox.moveBy(offset-begin->x,0);
817                                 begin->x=offset;
818                                 offset+=begin->w;
819                                 ++begin;
820                         }
821                         break;
822                 }
823                 case dirBlock:
824                 {
825                         if (end == glyphs.end())                // letzte zeile linksbuendig lassen
826                                 continue;
827                         int spacemode;
828                         if (numspaces)
829                                 spacemode=1;
830                         else
831                                 spacemode=0;
832                         if ((!spacemode) && (num<2))
833                                 break;
834                         int off=(area.width()-linelength)*256/(spacemode?numspaces:(num-1));
835                         int curoff=0;
836                         while (begin != end)
837                         {
838                                 int doadd=0;
839                                 if ((!spacemode) || (begin->flags&GS_ISSPACE))
840                                         doadd=1;
841                                 begin->x+=curoff>>8;
842                                 begin->bbox.moveBy(curoff>>8,0);
843                                 if (doadd)
844                                         curoff+=off;
845                                 ++begin;
846                         }
847                         break;
848                 }
849                 }
850         }
851         bboxValid=false;
852         calc_bbox();
853 }
854
855 void eTextPara::clear()
856 {
857         singleLock s(ftlock);
858
859         current_font = 0;
860         replacement_font = 0;
861
862         glyphs.clear();
863 }
864
865 eAutoInitP0<fontRenderClass> init_fontRenderClass(eAutoInitNumbers::graphic-1, "Font Render Class");