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