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