Skip to content

Commit

Permalink
Work on TextField.antiAliasType for windows. Fix TextField border to …
Browse files Browse the repository at this point in the history
…half pixel offst.
  • Loading branch information
hughsando committed Mar 27, 2024
1 parent 42263c7 commit dbd2f18
Show file tree
Hide file tree
Showing 24 changed files with 270 additions and 178 deletions.
11 changes: 6 additions & 5 deletions project/include/Font.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ enum TextFormatFlags

extern bool gNmeNativeFonts;

enum AntiAliasType { aaAdvanced, aaNormal };
enum AntiAliasType { aaNormal, aaAdvanced, aaAdvancedLcd };
enum AutoSizeMode { asCenter, asLeft, asNone, asRight };
enum TextFormatAlign { tfaCenter, tfaJustify, tfaLeft, tfaRight};
enum GridFitType { gftNone, gftPixel, gftSubPixel };
Expand Down Expand Up @@ -127,10 +127,10 @@ class TextFormat : public Object

struct CharGroup
{
CharGroup() : mChar0(0), mFontHeight(0), mFormat(0), mFont(0) { };
CharGroup() : mChar0(0), mFontHeight(0), mFormat(0), mFont(0), mAaType(aaNormal) { };
~CharGroup();
void Clear();
bool UpdateFont(double inScale,bool inNative);
bool UpdateFont(double inScale,bool inNative, AntiAliasType aaType);
void UpdateMetrics(TextLineMetrics &ioMetrics);
double Height(double inFontToLocal);
int Chars() const { return mString.size(); }
Expand All @@ -141,6 +141,7 @@ struct CharGroup
QuickVec<wchar_t,0> mString;
int mFontHeight;
unsigned int mFlags;
AntiAliasType mAaType;
TextFormat *mFormat;
class Font *mFont;
};
Expand Down Expand Up @@ -176,7 +177,7 @@ class FontFace
public:
virtual ~FontFace() { };

static FontFace *CreateNative(const TextFormat &inFormat,double inScale);
static FontFace *CreateNative(const TextFormat &inFormat,double inScale, AntiAliasType aaType);
static FontFace *CreateFreeType(const TextFormat &inFormat,double inScale,FontBuffer inBytes, const std::string &inCombinedName);
static FontFace *CreateCFFIFont(const TextFormat &inFormat,double inScale);

Expand All @@ -203,7 +204,7 @@ class Font : public Object
};

public:
static Font *Create(TextFormat &inFormat,double inScale, bool inNative,bool inInitRef=true);
static Font *Create(TextFormat &inFormat,double inScale, bool inNative, AntiAliasType aaType, bool inInitRef=true);

NmeObjectType getObjectType() { return notFont; }
void encodeStream(class ObjectStreamOut &inStream);
Expand Down
3 changes: 2 additions & 1 deletion project/include/Graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,8 @@ struct RenderTarget

inline int Width() const { return mRect.w; }
inline int Height() const { return mRect.h; }
inline PixelFormat Format() const { return mPixelFormat; }
inline PixelFormat Format() const { return mPixelFormat; }
bool supportsComponentAlpha() const;

Rect mRect;
PixelFormat mPixelFormat;
Expand Down
1 change: 1 addition & 0 deletions project/include/Hardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ class HardwareRenderer : public HardwareContext
virtual void Clear(uint32 inColour,const Rect *inRect=0) = 0;
virtual void Flip() = 0;

virtual bool supportsComponentAlpha() const = 0;


virtual void Render(const RenderState &inState, const HardwareData &inData );
Expand Down
17 changes: 10 additions & 7 deletions project/include/TextField.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ class TextField : public DisplayObject


// Render state
bool screenGrid;
double fontScale;
double fontToLocal;
double fieldWidth;
double fieldHeight;
bool screenGrid;
AntiAliasType fontAaType;
double fontScale;
double fontToLocal;
double fieldWidth;
double fieldHeight;

bool mLinesDirty;
bool mGfxDirty;
Expand Down Expand Up @@ -119,6 +120,8 @@ class TextField : public DisplayObject
AutoSizeMode getAutoSize() { return autoSize; }
void setAutoSize(int inAutoSize);
void modifyLocalMatrix(Matrix &ioMatrix);
void setAntiAliasType(int inVal);
int getAntiAliasType() const { return (int)antiAliasType; }


int getCaretIndex() { return caretIndex; }
Expand Down Expand Up @@ -218,8 +221,8 @@ class TextField : public DisplayObject
private:
TextField(const TextField &);
void operator=(const TextField &);
void Layout(const Matrix &inMatrix);
void Layout() { Layout(GetFullMatrix(true)); }
void Layout(const Matrix &inMatrix, const RenderTarget *inTarget);
void Layout() { Layout(GetFullMatrix(true), nullptr); }

void Clear();
void AddNode(const TiXmlNode *inNode, TextFormat *inFormat, int &ioCharCount);
Expand Down
2 changes: 1 addition & 1 deletion project/src/android/AndroidFont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace nme
{

FontFace *FontFace::CreateNative(const TextFormat &inFormat,double inScale)
FontFace *FontFace::CreateNative(const TextFormat &inFormat,double inScale, AntiAliasType aaType)
{
return 0;
}
Expand Down
1 change: 1 addition & 0 deletions project/src/common/ExternalInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4123,6 +4123,7 @@ TEXT_PROP_PRIME(border_color,BorderColor,int);
TEXT_PROP_PRIME(embed_fonts,EmbedFonts,bool);
TEXT_PROP_PRIME(auto_size,AutoSize,int);
TEXT_PROP_PRIME(caret_index,CaretIndex,int);
TEXT_PROP_PRIME(anti_alias_type,AntiAliasType,int);
TEXT_PROP_GET_PRIME(text_width,TextWidth,double);
TEXT_PROP_GET_PRIME(text_height,TextHeight,double);
TEXT_PROP_GET_PRIME(max_scroll_h,MaxScrollH,int);
Expand Down
18 changes: 11 additions & 7 deletions project/src/common/Font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ double CharGroup::Height(double inFontToLocal)

struct FontInfo
{
FontInfo(const TextFormat &inFormat,double inScale, bool inAllowNative)
FontInfo(const TextFormat &inFormat,double inScale, bool inAllowNative, AntiAliasType inAaType)
{
allowNative = inAllowNative;
name = inFormat.font;
Expand All @@ -323,6 +323,7 @@ struct FontInfo
flags ^= (miter<<16);
}

aaType = inAaType;
}

bool operator<(const FontInfo &inRHS) const
Expand All @@ -332,11 +333,14 @@ struct FontInfo
if (name > inRHS.name) return false;
if (height < inRHS.height) return true;
if (height > inRHS.height) return false;
if (aaType < inRHS.aaType) return true;
if (aaType > inRHS.aaType) return false;
return flags < inRHS.flags;
}
WString name;
bool allowNative;
int height;
AntiAliasType aaType;
unsigned int flags;
};

Expand Down Expand Up @@ -416,15 +420,15 @@ static std::string registerNorm(const std::string &inName)
return result;
}

Font *Font::Create(TextFormat &inFormat,double inScale,bool inNative,bool inInitRef)
Font *Font::Create(TextFormat &inFormat,double inScale,bool inNative,AntiAliasType aaType, bool inInitRef)
{
bool native = inNative && gNmeNativeFonts;
bool outline = inFormat.outline.Get()>0;
if (outline)
native = false;


FontInfo info(inFormat,inScale,native);
FontInfo info(inFormat,inScale,native,aaType);

Font *font = 0;
FontMap::iterator fit = sgFontMap.find(info);
Expand Down Expand Up @@ -522,23 +526,23 @@ Font *Font::Create(TextFormat &inFormat,double inScale,bool inNative,bool inInit

#ifndef HX_WINRT
if (native && !face)
face = FontFace::CreateNative(inFormat,inScale);
face = FontFace::CreateNative(inFormat,inScale, aaType);
#endif

if (!face)
face = FontFace::CreateFreeType(inFormat,inScale,NULL,"");

#ifndef HX_WINRT
if (!native && !face)
face = FontFace::CreateNative(inFormat,inScale);
face = FontFace::CreateNative(inFormat,inScale, aaType);
#endif
if (!face)
{
//printf("Missing face : %s\n", fontName.c_str() );
TextFormat defaultFormat = inFormat;
defaultFormat.font = UTF8ToWide("_sans");
return Create(defaultFormat, inScale, inNative, inInitRef);
return 0;
return Create(defaultFormat, inScale, inNative, aaType, inInitRef);
return 0;
}

font = new Font(face,info.height,inInitRef);
Expand Down
6 changes: 6 additions & 0 deletions project/src/common/Graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,12 @@ RenderTarget::RenderTarget() : mRect(0,0)
mHardware = 0;
}

bool RenderTarget::supportsComponentAlpha() const
{
return !mHardware || mHardware->supportsComponentAlpha();
}



RenderTarget RenderTarget::ClipRect(const Rect &inRect) const
{
Expand Down
86 changes: 59 additions & 27 deletions project/src/common/TextField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ static const double GAP = 2.0;
TextField::TextField(bool inInitRef) : DisplayObject(inInitRef),
alwaysShowSelection(false),
antiAliasType(aaNormal),
fontAaType(aaNormal),
autoSize(asNone),
background(false),
backgroundColor(0xffffffff),
Expand Down Expand Up @@ -290,8 +291,14 @@ void TextField::setTextFormat(TextFormat *inFmt,int inStart,int inEnd)
mCaretDirty = true;
}



void TextField::setAntiAliasType(int inVal)
{
if (antiAliasType!=inVal)
{
antiAliasType = (AntiAliasType)inVal;
mFontsDirty = true;
}
}


void TextField::setTextColor(int inCol)
Expand Down Expand Up @@ -1605,20 +1612,29 @@ void TextField::BuildBackground()
gfx.clear();
if (mHighlightGfx)
mHighlightGfx->clear();
if (background || border)
if (background)
{
if (background)
gfx.beginFill( backgroundColor, 1 );
if (border)
gfx.lineStyle(0, borderColor,1.0, false, ssmOpenGL );

gfx.moveTo(0.001,0.001);
gfx.lineTo(fieldWidth+0.001,0.001);
gfx.lineTo(fieldWidth+0.001,fieldHeight+0.001);
gfx.lineTo(0.001,fieldHeight+0.001);
gfx.lineTo(0.001,0.001);
gfx.beginFill( backgroundColor, 1 );

gfx.moveTo(0,0);
gfx.lineTo(fieldWidth,0);
gfx.lineTo(fieldWidth,fieldHeight);
gfx.lineTo(0,fieldHeight);
gfx.lineTo(0,0);
gfx.endFill();
}

if (border)
{
gfx.lineStyle(0, borderColor,1.0, false, ssmOpenGL );
gfx.moveTo(0.5,0.5);
gfx.lineTo(fieldWidth-0.5,0.5);
gfx.lineTo(fieldWidth-0.5,fieldHeight-0.5);
gfx.lineTo(0.5,fieldHeight-0.5);
gfx.lineTo(0.5,0.5);
}


//printf("%d,%d\n", mSelectMin , mSelectMax);
if (mSelectMin < mSelectMax)
{
Expand Down Expand Up @@ -1706,7 +1722,7 @@ void TextField::Render( const RenderTarget &inTarget, const RenderState &inState
if (inState.mPhase==rpBitmap && inState.mWasDirtyPtr && !*inState.mWasDirtyPtr && IsCacheDirty())
{
const Matrix &matrix = *inState.mTransform.mMatrix;
Layout(matrix);
Layout(matrix, nullptr);

RenderState state(inState);

Expand All @@ -1725,7 +1741,7 @@ void TextField::Render( const RenderTarget &inTarget, const RenderState &inState
return;

const Matrix &matrix = *inState.mTransform.mMatrix;
Layout(matrix);
Layout(matrix, &inTarget);


RenderState state(inState);
Expand Down Expand Up @@ -1926,7 +1942,7 @@ void TextField::Render( const RenderTarget &inTarget, const RenderState &inState

void TextField::GetExtent(const Transform &inTrans, Extent2DF &outExt,bool inForBitmap,bool inIncludeStroke)
{
Layout(*inTrans.mMatrix);
Layout(*inTrans.mMatrix, nullptr);

if (inForBitmap && border)
{
Expand Down Expand Up @@ -1975,7 +1991,7 @@ void TextField::DeleteChars(int inFirst,int inEnd)

mLinesDirty = true;
mGfxDirty = true;
Layout(GetFullMatrix(true));
Layout();
}
}

Expand Down Expand Up @@ -2078,7 +2094,7 @@ void TextField::InsertString(const WString &inString)
caretIndex += inString.length();
mLinesDirty = true;
mGfxDirty = true;
Layout(GetFullMatrix(true));
Layout();
}

#ifdef EPPC
Expand All @@ -2095,25 +2111,40 @@ static bool IsWord(int inCh)

// Combine x,y scaling with rotation to calculate pixel coordinates for
// each character.
void TextField::Layout(const Matrix &inMatrix)
void TextField::Layout(const Matrix &inMatrix, const RenderTarget *inTarget)
{
//double scale = scaleY<=0 ? 0.0 : sqrt( inMatrix.m10*inMatrix.m10 + inMatrix.m11*inMatrix.m11 )/scaleY;
double scale = sqrt( inMatrix.m10*inMatrix.m10 + inMatrix.m11*inMatrix.m11 );
bool grid = ( fabs(fabs(inMatrix.m10)-fabs(inMatrix.m10)))<0.0001 &&
fabs(fabs(inMatrix.m00)-fabs(inMatrix.m11))<0.0001 &&
( fabs(inMatrix.m10)<0.0001 || fabs(inMatrix.m11)<0.0001);

if (mFontsDirty || fabs(scale-fontScale)>0.0001 || grid!=screenGrid)
// Reuse old type
AntiAliasType aaType = fontAaType;

if (inTarget)
{
aaType = antiAliasType;
if (aaType==aaAdvancedLcd && ( !grid || fabs(inMatrix.m10)>1e-5 || !inTarget->supportsComponentAlpha() ) )
aaType = aaAdvanced;
}

bool charPositionDirty = mFontsDirty || fabs(scale-fontScale)>0.0001 || grid!=screenGrid;
if (charPositionDirty || aaType!=fontAaType )
{
// fontScale is local-to-pixel scale
fontScale = scale;
screenGrid = grid;
fontAaType = aaType;
for(int i=0;i<mCharGroups.size();i++)
mCharGroups[i]->UpdateFont(fontScale,!embedFonts);
mCharGroups[i]->UpdateFont(fontScale,!embedFonts,fontAaType);

mLinesDirty = true;
mFontsDirty = false;
fontToLocal = scale>0 ? 1.0/scale : 0.0;
if (charPositionDirty)
{
mLinesDirty = true;
mFontsDirty = false;
fontToLocal = scale>0 ? 1.0/scale : 0.0;
}
}

if (!mLinesDirty)
Expand Down Expand Up @@ -2678,7 +2709,7 @@ CharGroup::~CharGroup()
mFont->DecRef();
}

bool CharGroup::UpdateFont(double inScale,bool inNative)
bool CharGroup::UpdateFont(double inScale,bool inNative, AntiAliasType aaType)
{
int h = 0.5 + inScale*mFormat->size;
int flags = (mFormat->bold.Get() ? 1 : 0 ) |
Expand All @@ -2694,14 +2725,15 @@ bool CharGroup::UpdateFont(double inScale,bool inNative)
flags ^= miter<<16;
}

if (!mFont || h!=mFontHeight || mFlags!=flags )
if (!mFont || h!=mFontHeight || mFlags!=flags || mAaType!=aaType)
{
Font *oldFont = mFont;
mFont = Font::Create(*mFormat,inScale,inNative,true);
mFont = Font::Create(*mFormat,inScale,inNative,aaType,true);
if (oldFont)
oldFont->DecRef();
mFontHeight = h;
mFlags=flags;
mAaType = aaType;
return true;
}
return false;
Expand Down
Loading

0 comments on commit dbd2f18

Please sign in to comment.