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