2003-01-01 01:19:48 +03:00
|
|
|
//------------------------------------------------------------------------------
|
2004-09-21 02:50:02 +04:00
|
|
|
// Copyright (c) 2001-2002, Haiku, Inc.
|
2003-01-01 01:19:48 +03:00
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
|
|
// to deal in the Software without restriction, including without limitation
|
|
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
//
|
|
|
|
// File Name: ServerFont.cpp
|
|
|
|
// Author: DarkWyrm <bpmagic@columbus.rr.com>
|
|
|
|
// Description: Shadow BFont class
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------------
|
2005-05-12 19:09:30 +04:00
|
|
|
#include <ByteOrder.h>
|
2005-04-01 11:00:32 +04:00
|
|
|
#include <Shape.h>
|
2005-06-03 23:50:30 +04:00
|
|
|
#include <String.h>
|
2005-05-12 19:09:30 +04:00
|
|
|
#include <UTF8.h>
|
|
|
|
|
2005-04-01 11:00:32 +04:00
|
|
|
#include "Angle.h"
|
2005-05-12 19:09:30 +04:00
|
|
|
#include "FontServer.h"
|
|
|
|
#include "moreUTF8.h"
|
2005-06-03 23:50:30 +04:00
|
|
|
#include "truncate_string.h"
|
2005-05-12 19:09:30 +04:00
|
|
|
|
2005-04-01 11:00:32 +04:00
|
|
|
#include FT_FREETYPE_H
|
|
|
|
#include FT_OUTLINE_H
|
2003-01-01 01:19:48 +03:00
|
|
|
|
2005-05-12 19:09:30 +04:00
|
|
|
#include "ServerFont.h"
|
|
|
|
|
|
|
|
|
2003-01-01 01:19:48 +03:00
|
|
|
/*!
|
|
|
|
\brief Constructor
|
2003-01-20 02:04:58 +03:00
|
|
|
\param style Style object to which the ServerFont belongs
|
|
|
|
\param size Character size in points
|
|
|
|
\param rotation Rotation in degrees
|
|
|
|
\param shear Shear (slant) in degrees. 45 <= shear <= 135
|
|
|
|
\param flags Style flags as defined in <Font.h>
|
|
|
|
\param spacing String spacing flag as defined in <Font.h>
|
2003-01-01 01:19:48 +03:00
|
|
|
*/
|
2005-06-03 23:50:30 +04:00
|
|
|
ServerFont::ServerFont(FontStyle *style, float size,
|
|
|
|
float rotation, float shear,
|
|
|
|
uint16 flags, uint8 spacing)
|
|
|
|
: fStyle(style),
|
|
|
|
fSize(size),
|
|
|
|
fRotation(rotation),
|
|
|
|
fShear(shear),
|
|
|
|
fBounds(0, 0, 0, 0),
|
|
|
|
fFlags(flags),
|
|
|
|
fSpacing(spacing),
|
|
|
|
fDirection(B_FONT_LEFT_TO_RIGHT),
|
|
|
|
fFace(B_REGULAR_FACE),
|
|
|
|
fEncoding(B_UNICODE_UTF8)
|
|
|
|
|
2003-01-01 01:19:48 +03:00
|
|
|
{
|
2005-06-03 23:50:30 +04:00
|
|
|
if (fStyle)
|
2005-01-21 16:13:08 +03:00
|
|
|
fStyle->AddDependent();
|
2003-01-01 01:19:48 +03:00
|
|
|
}
|
|
|
|
|
2005-04-14 04:22:01 +04:00
|
|
|
// TODO: fStyle should not be NULL. There should be another FontStyle
|
|
|
|
// constructor, that initializes without actually interfacing with
|
|
|
|
// freetype, so that a ServerFont can be guaranteed to be "valid".
|
|
|
|
|
2005-06-03 23:50:30 +04:00
|
|
|
ServerFont::ServerFont()
|
|
|
|
: fStyle(NULL),
|
|
|
|
fSize(0.0),
|
|
|
|
fRotation(0.0),
|
|
|
|
fShear(90.0),
|
|
|
|
fBounds(0, 0, 0, 0),
|
|
|
|
fFlags(0),
|
|
|
|
fSpacing(B_STRING_SPACING),
|
|
|
|
fDirection(B_FONT_LEFT_TO_RIGHT),
|
|
|
|
fFace(B_REGULAR_FACE),
|
|
|
|
fEncoding(B_UNICODE_UTF8)
|
2003-10-05 21:51:13 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2003-01-01 01:19:48 +03:00
|
|
|
/*!
|
|
|
|
\brief Copy Constructor
|
2003-01-20 02:04:58 +03:00
|
|
|
\param font ServerFont to copy
|
2003-01-01 01:19:48 +03:00
|
|
|
*/
|
|
|
|
ServerFont::ServerFont(const ServerFont &font)
|
2005-06-03 23:50:30 +04:00
|
|
|
: fStyle(font.fStyle),
|
|
|
|
fSize(font.fSize),
|
|
|
|
fRotation(font.fRotation),
|
|
|
|
fShear(font.fShear),
|
|
|
|
fBounds(0, 0, 0, 0),
|
|
|
|
fFlags(font.fFlags),
|
|
|
|
fSpacing(font.fSpacing),
|
|
|
|
fDirection(font.fDirection),
|
|
|
|
fFace(font.fFace),
|
|
|
|
fEncoding(font.fEncoding)
|
2003-01-01 01:19:48 +03:00
|
|
|
{
|
2005-06-03 23:50:30 +04:00
|
|
|
if (fStyle)
|
2005-01-21 16:13:08 +03:00
|
|
|
fStyle->AddDependent();
|
2003-01-01 01:19:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2003-01-18 23:32:45 +03:00
|
|
|
\brief Removes itself as a dependency of its owning style.
|
2003-01-01 01:19:48 +03:00
|
|
|
*/
|
2005-06-03 23:50:30 +04:00
|
|
|
ServerFont::~ServerFont()
|
2003-01-01 01:19:48 +03:00
|
|
|
{
|
2005-06-03 23:50:30 +04:00
|
|
|
if (fStyle)
|
2005-01-21 16:13:08 +03:00
|
|
|
fStyle->RemoveDependent();
|
2003-01-01 01:19:48 +03:00
|
|
|
}
|
|
|
|
|
2003-09-15 23:09:13 +04:00
|
|
|
/*!
|
|
|
|
\brief Returns a copy of the specified font
|
|
|
|
\param The font to copy from.
|
|
|
|
\return A copy of the specified font
|
|
|
|
*/
|
2005-06-03 23:50:30 +04:00
|
|
|
ServerFont&
|
|
|
|
ServerFont::operator=(const ServerFont& font)
|
|
|
|
{
|
2005-01-21 16:13:08 +03:00
|
|
|
fSize = font.fSize;
|
2005-05-12 19:09:30 +04:00
|
|
|
fRotation = font.fRotation;
|
|
|
|
fShear = font.fShear;
|
2005-01-21 16:13:08 +03:00
|
|
|
fFlags = font.fFlags;
|
|
|
|
fSpacing = font.fSpacing;
|
2005-06-03 23:50:30 +04:00
|
|
|
fDirection = font.fDirection;
|
|
|
|
fFace = font.fFace;
|
|
|
|
fEncoding = font.fEncoding;
|
|
|
|
fBounds = font.fBounds;
|
|
|
|
|
|
|
|
_SetStyle(font.fStyle);
|
|
|
|
|
2003-09-15 23:09:13 +04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2003-01-01 01:19:48 +03:00
|
|
|
/*!
|
|
|
|
\brief Returns the number of strikes in the font
|
|
|
|
\return The number of strikes in the font
|
|
|
|
*/
|
2005-06-03 23:50:30 +04:00
|
|
|
int32
|
|
|
|
ServerFont::CountTuned()
|
2003-01-01 01:19:48 +03:00
|
|
|
{
|
2005-06-03 23:50:30 +04:00
|
|
|
if (fStyle)
|
|
|
|
return fStyle->TunedCount();
|
2003-01-01 01:19:48 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\brief Returns the file format of the font. Currently unimplemented.
|
|
|
|
\return B_TRUETYPE_WINDOWS
|
|
|
|
*/
|
2005-06-03 23:50:30 +04:00
|
|
|
font_file_format
|
|
|
|
ServerFont::FileFormat()
|
2003-01-01 01:19:48 +03:00
|
|
|
{
|
2005-01-21 16:13:08 +03:00
|
|
|
// TODO: implement ServerFont::FileFormat
|
2005-01-17 05:05:50 +03:00
|
|
|
return B_TRUETYPE_WINDOWS;
|
2003-01-01 01:19:48 +03:00
|
|
|
}
|
|
|
|
|
2005-06-03 23:50:30 +04:00
|
|
|
const char*
|
|
|
|
ServerFont::GetStyle() const
|
2003-01-01 01:19:48 +03:00
|
|
|
{
|
2005-06-03 23:50:30 +04:00
|
|
|
if (fStyle)
|
|
|
|
return fStyle->Name();
|
|
|
|
|
|
|
|
return NULL;
|
2003-01-01 01:19:48 +03:00
|
|
|
}
|
|
|
|
|
2005-06-03 23:50:30 +04:00
|
|
|
const char*
|
|
|
|
ServerFont::GetFamily() const
|
2005-01-17 05:05:50 +03:00
|
|
|
{
|
2005-06-03 23:50:30 +04:00
|
|
|
if (fStyle)
|
|
|
|
return fStyle->Family()->Name();
|
|
|
|
|
|
|
|
return NULL;
|
2005-01-17 05:05:50 +03:00
|
|
|
}
|
|
|
|
|
2005-06-03 23:50:30 +04:00
|
|
|
/*!
|
|
|
|
\brief Sets the ServerFont instance to whatever font is specified
|
|
|
|
\param familyID ID number of the family to set
|
|
|
|
\param styleID ID number of the style to set
|
|
|
|
\return B_OK if successful, B_ERROR if not
|
|
|
|
*/
|
|
|
|
status_t
|
|
|
|
ServerFont::SetFamilyAndStyle(uint16 familyID, uint16 styleID)
|
2005-01-17 05:05:50 +03:00
|
|
|
{
|
2005-06-03 23:50:30 +04:00
|
|
|
FontStyle* style = NULL;
|
|
|
|
|
|
|
|
if (fontserver->Lock()) {
|
|
|
|
style = fontserver->GetStyle(familyID, styleID);
|
|
|
|
fontserver->Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!style)
|
|
|
|
return B_ERROR;
|
|
|
|
|
|
|
|
_SetStyle(style);
|
|
|
|
|
|
|
|
return B_OK;
|
2005-01-17 05:05:50 +03:00
|
|
|
}
|
|
|
|
|
2005-06-03 23:50:30 +04:00
|
|
|
/*!
|
|
|
|
\brief Sets the ServerFont instance to whatever font is specified
|
|
|
|
\param fontID the combination of family and style ID numbers
|
|
|
|
\return B_OK if successful, B_ERROR if not
|
2003-01-01 01:19:48 +03:00
|
|
|
*/
|
2005-06-03 23:50:30 +04:00
|
|
|
status_t
|
|
|
|
ServerFont::SetFamilyAndStyle(uint32 fontID)
|
2003-01-01 01:19:48 +03:00
|
|
|
{
|
2005-06-03 23:50:30 +04:00
|
|
|
uint16 style = fontID & 0xFFFF;
|
|
|
|
uint16 family = (fontID & 0xFFFF0000) >> 16;
|
2005-01-21 16:13:08 +03:00
|
|
|
|
2005-06-03 23:50:30 +04:00
|
|
|
return SetFamilyAndStyle(family, style);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\brief Gets the ID values for the ServerFont instance in one shot
|
|
|
|
\return the combination of family and style ID numbers
|
|
|
|
*/
|
|
|
|
uint32
|
|
|
|
ServerFont::GetFamilyAndStyle(void) const
|
|
|
|
{
|
|
|
|
return (FamilyID() << 16) | StyleID();
|
2005-01-21 16:13:08 +03:00
|
|
|
}
|
|
|
|
|
2005-04-01 11:00:32 +04:00
|
|
|
// functions needed to convert a freetype vector graphics to a BShape
|
|
|
|
inline BPoint
|
|
|
|
VectorToPoint(FT_Vector *vector)
|
|
|
|
{
|
|
|
|
BPoint result;
|
|
|
|
result.x = float(int32(vector->x)) / 2097152;
|
|
|
|
result.y = -float(int32(vector->y)) / 2097152;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
MoveToFunc(FT_Vector *to, void *user)
|
|
|
|
{
|
|
|
|
((BShape *)user)->MoveTo(VectorToPoint(to));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
LineToFunc(FT_Vector *to, void *user)
|
|
|
|
{
|
|
|
|
((BShape *)user)->LineTo(VectorToPoint(to));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ConicToFunc(FT_Vector *control, FT_Vector *to, void *user)
|
|
|
|
{
|
|
|
|
BPoint controls[3];
|
|
|
|
|
|
|
|
controls[0] = VectorToPoint(control);
|
|
|
|
controls[1] = VectorToPoint(to);
|
|
|
|
controls[2] = controls[1];
|
|
|
|
|
|
|
|
((BShape *)user)->BezierTo(controls);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
CubicToFunc(FT_Vector *control1, FT_Vector *control2, FT_Vector *to, void *user)
|
|
|
|
{
|
|
|
|
BPoint controls[3];
|
|
|
|
|
|
|
|
controls[0] = VectorToPoint(control1);
|
|
|
|
controls[1] = VectorToPoint(control2);
|
|
|
|
controls[2] = VectorToPoint(to);
|
|
|
|
|
|
|
|
((BShape *)user)->BezierTo(controls);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
BShape **
|
|
|
|
ServerFont::GetGlyphShapes(const char charArray[], int32 numChars) const
|
|
|
|
{
|
|
|
|
if (!charArray || numChars <= 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
FT_Outline_Funcs funcs;
|
|
|
|
funcs.move_to = MoveToFunc;
|
|
|
|
funcs.line_to = LineToFunc;
|
|
|
|
funcs.conic_to = ConicToFunc;
|
|
|
|
funcs.cubic_to = CubicToFunc;
|
|
|
|
|
|
|
|
FT_Face face = fStyle->GetFTFace();
|
|
|
|
if (!face)
|
|
|
|
return NULL;
|
|
|
|
|
2005-06-15 21:09:00 +04:00
|
|
|
FT_Set_Char_Size(face, 0, int32(fSize * 64), 72, 72);
|
2005-04-01 11:00:32 +04:00
|
|
|
|
2005-05-12 19:09:30 +04:00
|
|
|
Angle rotation(fRotation);
|
|
|
|
Angle shear(fShear);
|
2005-04-01 11:00:32 +04:00
|
|
|
|
|
|
|
// First, rotate
|
|
|
|
FT_Matrix rmatrix;
|
|
|
|
rmatrix.xx = (FT_Fixed)( rotation.Cosine()*0x10000);
|
|
|
|
rmatrix.xy = (FT_Fixed)(-rotation.Sine()*0x10000);
|
|
|
|
rmatrix.yx = (FT_Fixed)( rotation.Sine()*0x10000);
|
|
|
|
rmatrix.yy = (FT_Fixed)( rotation.Cosine()*0x10000);
|
|
|
|
|
|
|
|
// Next, shear
|
|
|
|
FT_Matrix smatrix;
|
|
|
|
smatrix.xx = (FT_Fixed)(0x10000);
|
|
|
|
smatrix.xy = (FT_Fixed)(-shear.Cosine()*0x10000);
|
|
|
|
smatrix.yx = (FT_Fixed)(0);
|
|
|
|
smatrix.yy = (FT_Fixed)(0x10000);
|
|
|
|
|
|
|
|
// Multiply togheter
|
|
|
|
FT_Matrix_Multiply(&rmatrix, &smatrix);
|
|
|
|
|
2005-04-01 13:18:25 +04:00
|
|
|
//FT_Vector pen;
|
|
|
|
//FT_Set_Transform(face, &smatrix, &pen);
|
2005-04-01 11:00:32 +04:00
|
|
|
|
|
|
|
BShape **shapes = (BShape **)malloc(sizeof(BShape *) * numChars);
|
|
|
|
for (int i = 0; i < numChars; i++) {
|
|
|
|
shapes[i] = new BShape();
|
|
|
|
shapes[i]->Clear();
|
|
|
|
FT_Load_Char(face, charArray[i], FT_LOAD_NO_BITMAP);
|
|
|
|
FT_Outline outline = face->glyph->outline;
|
|
|
|
FT_Outline_Decompose(&outline, &funcs, shapes[i]);
|
|
|
|
shapes[i]->Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return shapes;
|
|
|
|
}
|
|
|
|
|
2005-05-20 21:36:55 +04:00
|
|
|
// GetEscapements
|
2005-05-12 19:09:30 +04:00
|
|
|
BPoint*
|
2005-04-01 13:18:25 +04:00
|
|
|
ServerFont::GetEscapements(const char charArray[], int32 numChars,
|
2005-05-12 19:09:30 +04:00
|
|
|
BPoint offsetArray[]) const
|
2005-04-01 13:18:25 +04:00
|
|
|
{
|
2005-06-03 23:50:30 +04:00
|
|
|
if (!fStyle || !charArray || numChars <= 0 || !offsetArray)
|
2005-04-01 13:18:25 +04:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
FT_Face face = fStyle->GetFTFace();
|
|
|
|
if (!face)
|
|
|
|
return NULL;
|
|
|
|
|
2005-06-15 21:09:00 +04:00
|
|
|
FT_Set_Char_Size(face, 0, int32(fSize * 64), 72, 72);
|
2005-04-01 13:18:25 +04:00
|
|
|
|
2005-05-12 19:09:30 +04:00
|
|
|
Angle rotation(fRotation);
|
|
|
|
Angle shear(fShear);
|
2005-04-01 13:18:25 +04:00
|
|
|
|
|
|
|
// First, rotate
|
|
|
|
FT_Matrix rmatrix;
|
|
|
|
rmatrix.xx = (FT_Fixed)( rotation.Cosine()*0x10000);
|
|
|
|
rmatrix.xy = (FT_Fixed)(-rotation.Sine()*0x10000);
|
|
|
|
rmatrix.yx = (FT_Fixed)( rotation.Sine()*0x10000);
|
|
|
|
rmatrix.yy = (FT_Fixed)( rotation.Cosine()*0x10000);
|
|
|
|
|
|
|
|
// Next, shear
|
|
|
|
FT_Matrix smatrix;
|
|
|
|
smatrix.xx = (FT_Fixed)(0x10000);
|
|
|
|
smatrix.xy = (FT_Fixed)(-shear.Cosine()*0x10000);
|
|
|
|
smatrix.yx = (FT_Fixed)(0);
|
|
|
|
smatrix.yy = (FT_Fixed)(0x10000);
|
|
|
|
|
|
|
|
// Multiply togheter
|
|
|
|
FT_Matrix_Multiply(&rmatrix, &smatrix);
|
|
|
|
|
|
|
|
//FT_Vector pen;
|
|
|
|
//FT_Set_Transform(face, &smatrix, &pen);
|
|
|
|
|
2005-05-12 19:09:30 +04:00
|
|
|
// TODO: I'm not sure if this the correct interpretation
|
|
|
|
// of the BeBook. Have actual tests been done here?
|
|
|
|
|
|
|
|
// TODO: handle UTF8... see below!!
|
2005-04-01 13:18:25 +04:00
|
|
|
BPoint *escapements = (BPoint *)malloc(sizeof(BPoint) * numChars);
|
|
|
|
for (int i = 0; i < numChars; i++) {
|
|
|
|
FT_Load_Char(face, charArray[i], FT_LOAD_NO_BITMAP);
|
2005-05-16 15:03:28 +04:00
|
|
|
// escapements[i].x = float(face->glyph->metrics.width / 64) / fSize;
|
|
|
|
// escapements[i].y = 0;
|
|
|
|
escapements[i].x = float(face->glyph->metrics.horiAdvance / 64) / fSize;
|
|
|
|
escapements[i].y = float(face->glyph->metrics.vertAdvance / 64) / fSize;
|
2005-04-01 13:18:25 +04:00
|
|
|
escapements[i] += offsetArray[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return escapements;
|
|
|
|
}
|
|
|
|
|
2005-05-16 15:03:28 +04:00
|
|
|
// is_white_space
|
|
|
|
inline bool
|
|
|
|
is_white_space(uint16 glyph)
|
|
|
|
{
|
|
|
|
// TODO: handle them all!
|
2005-06-03 23:50:30 +04:00
|
|
|
if (glyph == ' ' || glyph == B_TAB)
|
2005-05-16 15:03:28 +04:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-05-20 21:36:55 +04:00
|
|
|
// GetEscapements
|
2005-05-12 19:09:30 +04:00
|
|
|
bool
|
|
|
|
ServerFont::GetEscapements(const char charArray[], int32 numChars,
|
|
|
|
float widthArray[], escapement_delta delta) const
|
|
|
|
{
|
2005-06-03 23:50:30 +04:00
|
|
|
if (!fStyle || !charArray || numChars <= 0)
|
2005-05-12 19:09:30 +04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
FT_Face face = fStyle->GetFTFace();
|
|
|
|
if (!face)
|
|
|
|
return false;
|
|
|
|
|
2005-06-15 21:09:00 +04:00
|
|
|
FT_Set_Char_Size(face, 0, int32(fSize * 64), 72, 72);
|
2005-05-12 19:09:30 +04:00
|
|
|
|
|
|
|
// UTF8 handling...this can probably be smarter
|
|
|
|
// Here is what I do in the AGGTextRenderer to handle UTF8...
|
|
|
|
// It is probably highly inefficient, so it should be reviewed.
|
|
|
|
int32 numBytes = UTF8CountBytes(charArray, numChars);
|
|
|
|
int32 convertedLength = numBytes * 2;
|
|
|
|
char* convertedBuffer = new char[convertedLength];
|
|
|
|
|
|
|
|
int32 state = 0;
|
|
|
|
status_t ret;
|
|
|
|
if ((ret = convert_from_utf8(B_UNICODE_CONVERSION,
|
|
|
|
charArray, &numBytes,
|
|
|
|
convertedBuffer, &convertedLength,
|
|
|
|
&state, B_SUBSTITUTE)) >= B_OK
|
|
|
|
&& (ret = swap_data(B_INT16_TYPE, convertedBuffer, convertedLength,
|
|
|
|
B_SWAP_BENDIAN_TO_HOST)) >= B_OK) {
|
|
|
|
|
|
|
|
uint16* glyphIndex = (uint16*)convertedBuffer;
|
|
|
|
// just to be sure
|
2005-06-15 21:09:00 +04:00
|
|
|
numChars = min_c((uint32)numChars, convertedLength / sizeof(uint16));
|
2005-05-12 19:09:30 +04:00
|
|
|
|
|
|
|
for (int i = 0; i < numChars; i++) {
|
|
|
|
FT_Load_Char(face, glyphIndex[i], FT_LOAD_NO_BITMAP);
|
2005-06-15 21:09:00 +04:00
|
|
|
if (face->glyph) {
|
|
|
|
widthArray[i] = ((float)face->glyph->metrics.horiAdvance / 64.0) / fSize;
|
|
|
|
widthArray[i] += is_white_space(glyphIndex[i]) ? delta.space : delta.nonspace;
|
|
|
|
}
|
2005-05-12 19:09:30 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
delete[] convertedBuffer;
|
|
|
|
|
|
|
|
return ret >= B_OK;
|
|
|
|
}
|
|
|
|
|
2005-05-20 21:36:55 +04:00
|
|
|
// StringWidth
|
|
|
|
float
|
2005-05-21 03:51:33 +04:00
|
|
|
ServerFont::StringWidth(const char* string, int32 numBytes) const
|
2005-05-20 21:36:55 +04:00
|
|
|
{
|
2005-06-03 23:50:30 +04:00
|
|
|
if (!fStyle || !string || numBytes <= 0)
|
2005-05-21 03:51:33 +04:00
|
|
|
return 0.0;
|
2005-05-20 21:36:55 +04:00
|
|
|
|
2005-05-21 03:51:33 +04:00
|
|
|
FT_Face face = fStyle->GetFTFace();
|
|
|
|
if (!face)
|
|
|
|
return 0.0;
|
2005-05-20 21:36:55 +04:00
|
|
|
|
|
|
|
float width = 0.0;
|
|
|
|
|
2005-05-21 03:51:33 +04:00
|
|
|
int32 convertedLength = numBytes * 2;
|
|
|
|
char* convertedBuffer = new char[convertedLength];
|
|
|
|
|
|
|
|
int32 state = 0;
|
|
|
|
status_t ret;
|
|
|
|
if ((ret = convert_from_utf8(B_UNICODE_CONVERSION,
|
|
|
|
string, &numBytes,
|
|
|
|
convertedBuffer, &convertedLength,
|
|
|
|
&state, B_SUBSTITUTE)) >= B_OK
|
|
|
|
&& (ret = swap_data(B_INT16_TYPE, convertedBuffer, convertedLength,
|
|
|
|
B_SWAP_BENDIAN_TO_HOST)) >= B_OK) {
|
|
|
|
|
|
|
|
uint16* glyphIndex = (uint16*)convertedBuffer;
|
|
|
|
// just to be sure
|
|
|
|
int numChars = convertedLength / sizeof(uint16);
|
|
|
|
|
|
|
|
for (int i = 0; i < numChars; i++) {
|
|
|
|
FT_Load_Char(face, glyphIndex[i], FT_LOAD_NO_BITMAP);
|
2005-05-21 03:54:28 +04:00
|
|
|
width += (float)face->glyph->metrics.horiAdvance / 64.0;
|
2005-05-21 03:51:33 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
delete[] convertedBuffer;
|
2005-05-20 21:36:55 +04:00
|
|
|
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
2005-06-03 23:50:30 +04:00
|
|
|
/*!
|
|
|
|
\brief Returns a BRect which encloses the entire font
|
|
|
|
\return A BRect which encloses the entire font
|
2005-02-05 23:12:05 +03:00
|
|
|
*/
|
2005-06-03 23:50:30 +04:00
|
|
|
BRect
|
|
|
|
ServerFont::BoundingBox()
|
2005-02-05 23:12:05 +03:00
|
|
|
{
|
2005-06-03 23:50:30 +04:00
|
|
|
// TODO: fBounds is nowhere calculated!
|
|
|
|
return fBounds;
|
2005-02-05 23:12:05 +03:00
|
|
|
}
|
|
|
|
|
2005-06-03 23:50:30 +04:00
|
|
|
/*!
|
|
|
|
\brief Obtains the height values for characters in the font in its current state
|
|
|
|
\param fh pointer to a font_height object to receive the values for the font
|
2005-01-21 16:13:08 +03:00
|
|
|
*/
|
2005-06-03 23:50:30 +04:00
|
|
|
void
|
|
|
|
ServerFont::Height(font_height *fh) const
|
2005-01-21 16:13:08 +03:00
|
|
|
{
|
2005-06-03 23:50:30 +04:00
|
|
|
if (fh && fStyle)
|
|
|
|
*fh = fStyle->GetHeight(fSize);
|
2003-01-01 01:19:48 +03:00
|
|
|
}
|
2005-01-23 16:40:11 +03:00
|
|
|
|
2005-06-03 23:50:30 +04:00
|
|
|
// TruncateString
|
|
|
|
void
|
|
|
|
ServerFont::TruncateString(BString* inOut, uint32 mode, float width) const
|
2005-01-23 16:40:11 +03:00
|
|
|
{
|
2005-06-03 23:50:30 +04:00
|
|
|
if (inOut) {
|
|
|
|
// the width of the "…" glyph
|
|
|
|
float ellipsisWidth = StringWidth(B_UTF8_ELLIPSIS, 3);
|
|
|
|
const char* string = inOut->String();
|
|
|
|
int32 length = strlen(string);
|
|
|
|
// temporary array to hold result
|
|
|
|
char* result = new char[length + 3];
|
|
|
|
// count the individual glyphs
|
|
|
|
int32 numChars = UTF8CountChars(string, length);
|
|
|
|
// get the escapement of each glyph in font units
|
|
|
|
float* escapementArray = new float[numChars];
|
|
|
|
static escapement_delta delta = (escapement_delta){ 0.0, 0.0 };
|
|
|
|
GetEscapements(string, numChars, escapementArray, delta);
|
|
|
|
|
|
|
|
truncate_string(string, mode, width, result,
|
|
|
|
escapementArray, fSize, ellipsisWidth, length, numChars);
|
|
|
|
|
|
|
|
inOut->SetTo(result);
|
|
|
|
|
|
|
|
delete[] escapementArray;
|
|
|
|
delete[] result;
|
|
|
|
}
|
2005-01-23 16:40:11 +03:00
|
|
|
}
|
|
|
|
|
2005-06-03 23:50:30 +04:00
|
|
|
// _SetStyle
|
|
|
|
void
|
|
|
|
ServerFont::_SetStyle(FontStyle* style)
|
|
|
|
{
|
|
|
|
if (style != fStyle) {
|
|
|
|
// detach from old style
|
|
|
|
if (fStyle)
|
|
|
|
fStyle->RemoveDependent();
|
|
|
|
|
|
|
|
fStyle = style;
|
|
|
|
|
|
|
|
// attach to new style
|
|
|
|
if (fStyle)
|
|
|
|
fStyle->AddDependent();
|
|
|
|
}
|
|
|
|
}
|