Use FTFace() function to get the face instead of always creating a new one.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@12007 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Michael Lotz
2005-03-25 21:11:46 +00:00
parent 6d495dfc26
commit d31e3b8e27
+144 -190
View File
@@ -1946,156 +1946,137 @@ void DisplayDriverImpl::DrawString(const char *string, const int32 &length, cons
// TODO: properly calculate intersecting rectangle with cursor in DisplayDriverImpl::DrawString // TODO: properly calculate intersecting rectangle with cursor in DisplayDriverImpl::DrawString
// Rough guesstimate for size // Rough guesstimate for size
BRect intersection(pt.x,pt.x,pt.y,pt.y); BRect intersection(pt.x, pt.x, pt.y, pt.y);
intersection.top-=d->font.Size()*1.5; intersection.top -= d->font.Size() * 1.5;
intersection.right+=d->font.Size()*1.5*length; intersection.right += d->font.Size() * 1.5 * length;
if(fCursorHandler.IntersectsCursor(intersection))
if (fCursorHandler.IntersectsCursor(intersection))
fCursorHandler.DriverHide(); fCursorHandler.DriverHide();
BPoint point(pt); BPoint point(pt);
const ServerFont *font = &(d->font);
const ServerFont *font=&(d->font); bool antialias = true;
if (font->Size() < 18 && font->Flags() & B_DISABLE_ANTIALIASING)
antialias = false;
FT_Face face;
FT_GlyphSlot slot;
FT_Matrix rmatrix,smatrix;
FT_UInt glyph_index=0, previous=0;
FT_Vector pen,delta,space,nonspace;
int16 error=0;
int32 strlength,i;
Angle rotation(font->Rotation()), shear(font->Shear());
bool antialias=true;
if(font->Size()<18 && (font->Flags()& B_DISABLE_ANTIALIASING==1))
antialias=false;
// Originally, I thought to do this shear checking here, but it really should be // Originally, I thought to do this shear checking here, but it really should be
// done in BFont::SetShear() // done in BFont::SetShear()
float shearangle=shear.Value(); Angle shear(font->Shear());
if(shearangle>135) if (font->Shear() != 90) {
shearangle=135; float shearangle = shear.Value();
if(shearangle<45) if (shearangle > 135)
shearangle=45; shearangle = 135;
if (shearangle < 45)
if(shearangle>90) shearangle = 45;
shear=90+((180-shearangle)*2);
else if(shearangle > 90)
shear=90-(90-shearangle)*2; shear = 90 + ((180 - shearangle) * 2);
else
shear = 90 - (90 - shearangle) * 2;
}
error=FT_New_Face(ftlib, font->GetPath(), 0, &face); FT_Face face = font->GetFTFace();
if(error) if (!face) {
{
Unlock(); Unlock();
return; return;
} }
slot=face->glyph;
bool use_kerning=FT_HAS_KERNING(face) && font->Spacing()==B_STRING_SPACING;
error=FT_Set_Char_Size(face, 0,int32(font->Size())*64,72,72); uint16 error = FT_Set_Char_Size(face, 0, int32(font->Size()) * 64, 72, 72);
if(error) if (error) {
{
Unlock(); Unlock();
return; return;
} }
// if we do any transformation, we do a call to FT_Set_Transform() here bool use_kerning = font->HasKerning() && font->Spacing() == B_STRING_SPACING;
FT_Vector space, nonspace;
Angle rotation(font->Rotation());
// First, rotate // First, rotate
rmatrix.xx = (FT_Fixed)( rotation.Cosine()*0x10000); FT_Matrix rmatrix;
rmatrix.xy = (FT_Fixed)(-rotation.Sine()*0x10000); rmatrix.xx = (FT_Fixed)( rotation.Cosine()*0x10000);
rmatrix.yx = (FT_Fixed)( rotation.Sine()*0x10000); rmatrix.xy = (FT_Fixed)(-rotation.Sine()*0x10000);
rmatrix.yy = (FT_Fixed)( rotation.Cosine()*0x10000); rmatrix.yx = (FT_Fixed)( rotation.Sine()*0x10000);
rmatrix.yy = (FT_Fixed)( rotation.Cosine()*0x10000);
// Next, shear // Next, shear
FT_Matrix smatrix;
smatrix.xx = (FT_Fixed)(0x10000); smatrix.xx = (FT_Fixed)(0x10000);
smatrix.xy = (FT_Fixed)(-shear.Cosine()*0x10000); smatrix.xy = (FT_Fixed)(-shear.Cosine()*0x10000);
smatrix.yx = (FT_Fixed)(0); smatrix.yx = (FT_Fixed)(0);
smatrix.yy = (FT_Fixed)(0x10000); smatrix.yy = (FT_Fixed)(0x10000);
FT_Matrix_Multiply(&rmatrix,&smatrix); // Multiply togheter
FT_Matrix_Multiply(&rmatrix, &smatrix);
// Set up the increment value for escapement padding // Set up the increment value for escapement padding
space.x=int32(d->edelta.space * rotation.Cosine()*64); space.x = int32(d->edelta.space * rotation.Cosine() * 64);
space.y=int32(d->edelta.space * rotation.Sine()*64); space.y = int32(d->edelta.space * rotation.Sine() * 64);
nonspace.x=int32(d->edelta.nonspace * rotation.Cosine()*64); nonspace.x = int32(d->edelta.nonspace * rotation.Cosine() * 64);
nonspace.y=int32(d->edelta.nonspace * rotation.Sine()*64); nonspace.y = int32(d->edelta.nonspace * rotation.Sine() * 64);
// set the pen position in 26.6 cartesian space coordinates // set the pen position in 26.6 cartesian space coordinates
pen.x=(int32)point.x * 64; FT_Vector pen;
pen.y=(int32)point.y * 64; pen.x = (int32)point.x * 64;
pen.y = (int32)point.y * 64;
slot=face->glyph; int32 strlength = strlen(string);
if(length < strlength)
strlength = length;
strlength=strlen(string); FT_Vector delta;
if(length<strlength) FT_GlyphSlot slot = face->glyph;
strlength=length; FT_UInt glyph_index = 0, previous = 0;
for (int32 i = 0; i < strlength; i++) {
for(i=0;i<strlength;i++) FT_Set_Transform(face, &smatrix, &pen);
{
FT_Set_Transform(face,&smatrix,&pen);
// Handle escapement padding option // Handle escapement padding option
if((uint8)string[i]<=0x20) if((uint8)string[i] <= 0x20) {
{ pen.x += space.x;
pen.x+=space.x; pen.y += space.y;
pen.y+=space.y; } else {
pen.x += nonspace.x;
pen.y += nonspace.y;
} }
else
{
pen.x+=nonspace.x;
pen.y+=nonspace.y;
}
// get kerning and move pen // get kerning and move pen
if(use_kerning && previous && glyph_index) if(use_kerning && previous && glyph_index) {
{ FT_Get_Kerning(face, previous, glyph_index, ft_kerning_default, &delta);
FT_Get_Kerning(face, previous, glyph_index,ft_kerning_default, &delta); pen.x += delta.x;
pen.x+=delta.x; pen.y += delta.y;
pen.y+=delta.y;
} }
error=FT_Load_Char(face,string[i], error = FT_Load_Char(face, string[i], (antialias ? FT_LOAD_RENDER : FT_LOAD_RENDER | FT_LOAD_MONOCHROME));
((antialias)?FT_LOAD_RENDER:FT_LOAD_RENDER | FT_LOAD_MONOCHROME) ); if (error)
continue;
if(!error)
{ if (antialias)
if(antialias) BlitGray2RGB32(&slot->bitmap, BPoint(slot->bitmap_left, point.y - (slot->bitmap_top - point.y)), d);
BlitGray2RGB32(&slot->bitmap, else
BPoint(slot->bitmap_left,point.y-(slot->bitmap_top-point.y)), d); BlitMono2RGB32(&slot->bitmap, BPoint(slot->bitmap_left, point.y - (slot->bitmap_top - point.y)), d);
else
BlitMono2RGB32(&slot->bitmap,
BPoint(slot->bitmap_left,point.y-(slot->bitmap_top-point.y)), d);
}
// increment pen position // increment pen position
pen.x+=slot->advance.x; pen.x += slot->advance.x;
pen.y+=slot->advance.y; pen.y += slot->advance.y;
previous=glyph_index; previous = glyph_index;
} }
// TODO: implement calculation of invalid rectangle in DisplayDriverImpl::DrawString properly // TODO: implement calculation of invalid rectangle in DisplayDriverImpl::DrawString properly
BRect r; BRect r;
r.left=MIN(point.x,pen.x>>6); r.left = MIN(point.x, pen.x >> 6);
r.right=MAX(point.x,pen.x>>6); r.right = MAX(point.x, pen.x >> 6);
r.top=point.y-face->height; r.top = point.y - face->height;
r.bottom=point.y+face->height; r.bottom = point.y + face->height;
fCursorHandler.DriverShow(); fCursorHandler.DriverShow();
Invalidate(r); Invalidate(r);
// Update the caller's pen position // Update the caller's pen position
d->penlocation.x=pen.x / 64; d->penlocation.x = pen.x / 64;
d->penlocation.y=pen.y / 64; d->penlocation.y = pen.y / 64;
FT_Done_Face(face);
Unlock(); Unlock();
} }
/*! /*!
@@ -2111,66 +2092,53 @@ float DisplayDriverImpl::StringWidth(const char *string, int32 length, const Dra
{ {
if(!string || !d) if(!string || !d)
return 0.0; return 0.0;
Lock(); Lock();
const ServerFont *font=&(d->font);
FT_Face face; const ServerFont *font = &(d->font);
FT_GlyphSlot slot;
FT_UInt glyph_index=0, previous=0; FT_Face face = font->GetFTFace();
FT_Vector pen,delta; if(!face) {
int16 error=0;
int32 strlength,i;
float returnval;
error=FT_New_Face(ftlib, font->GetPath(), 0, &face);
if(error)
{
Unlock(); Unlock();
return 0.0; return 0.0;
} }
slot=face->glyph;
bool use_kerning=FT_HAS_KERNING(face) && font->Spacing()==B_STRING_SPACING;
error=FT_Set_Char_Size(face, 0,int32(font->Size())*64,72,72); uint16 error = FT_Set_Char_Size(face, 0, int32(font->Size()) * 64, 72, 72);
if(error) if (error) {
{
Unlock(); Unlock();
return 0.0; return 0.0;
} }
bool use_kerning = font->HasKerning() && font->Spacing() == B_STRING_SPACING;
// set the pen position in 26.6 cartesian space coordinates // set the pen position in 26.6 cartesian space coordinates
pen.x=0; FT_Vector pen, delta;
pen.x = 0;
slot=face->glyph; int32 strlength = strlen(string);
if (length < strlength)
strlength = length;
strlength=strlen(string); FT_GlyphSlot slot = face->glyph;
if(length<strlength) FT_UInt glyph_index = 0, previous = 0;
strlength=length; for (int32 i = 0; i < strlength; i++) {
for(i=0;i<strlength;i++)
{
// get kerning and move pen // get kerning and move pen
if(use_kerning && previous && glyph_index) if (use_kerning && previous && glyph_index) {
{ FT_Get_Kerning(face, previous, glyph_index, ft_kerning_default, &delta);
FT_Get_Kerning(face, previous, glyph_index,ft_kerning_default, &delta); pen.x += delta.x;
pen.x+=delta.x;
} }
error=FT_Load_Char(face,string[i],FT_LOAD_MONOCHROME); error = FT_Load_Char(face, string[i], FT_LOAD_MONOCHROME);
if (error)
continue;
// increment pen position // increment pen position
pen.x+=slot->advance.x; pen.x += slot->advance.x;
previous=glyph_index; previous = glyph_index;
} }
FT_Done_Face(face);
Unlock(); Unlock();
return pen.x >> 6;
returnval=pen.x>>6;
return returnval;
} }
/*! /*!
@@ -2187,54 +2155,40 @@ float DisplayDriverImpl::StringWidth(const char *string, int32 length, const Dra
*/ */
float DisplayDriverImpl::StringHeight(const char *string, int32 length, const DrawData *d) float DisplayDriverImpl::StringHeight(const char *string, int32 length, const DrawData *d)
{ {
if(!string || !d) if (!string || !d)
return 0.0; return 0.0;
Lock(); Lock();
const ServerFont *font=&(d->font); const ServerFont *font = &(d->font);
FT_Face face; FT_Face face = font->GetFTFace();
FT_GlyphSlot slot; if (!face) {
int16 error=0;
int32 strlength,i;
float returnval=0.0,ascent=0.0,descent=0.0;
error=FT_New_Face(ftlib, font->GetPath(), 0, &face);
if(error)
{
Unlock(); Unlock();
return 0.0; return 0.0;
} }
slot=face->glyph;
error=FT_Set_Char_Size(face, 0,int32(font->Size())*64,72,72); uint16 error = FT_Set_Char_Size(face, 0, int32(font->Size()) * 64, 72, 72);
if(error)
{
Unlock();
return 0.0;
}
slot=face->glyph;
strlength=strlen(string); int32 strlength = strlen(string);
if(length<strlength) if (length < strlength)
strlength=length; strlength = length;
for(i=0;i<strlength;i++) FT_GlyphSlot slot = face->glyph;
{ float ascent = 0.0, descent = 0.0;
FT_Load_Char(face,string[i],FT_LOAD_RENDER); for (int32 i = 0; i < strlength; i++) {
if(slot->metrics.horiBearingY<slot->metrics.height) error = FT_Load_Char(face, string[i], FT_LOAD_MONOCHROME);
descent=MAX((slot->metrics.height-slot->metrics.horiBearingY)>>6,descent); if (error)
continue;
if (slot->metrics.horiBearingY < slot->metrics.height)
descent = MAX((slot->metrics.height - slot->metrics.horiBearingY) >> 6, descent);
else else
ascent=MAX(slot->bitmap.rows,ascent); ascent = MAX(slot->bitmap.rows, ascent);
} }
FT_Done_Face(face);
Unlock(); Unlock();
returnval=ascent+descent; return ascent + descent;
return returnval;
} }
/*! /*!