Skip to content

Commit 623498a

Browse files
Merge pull request #426 from SixLabors/js/hasflag-allocations
Remove all instances of HasFlag
2 parents 5767254 + fe10182 commit 623498a

File tree

7 files changed

+23
-22
lines changed

7 files changed

+23
-22
lines changed

src/SixLabors.Fonts/Font.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ private string LoadFontName()
290290
return metrics;
291291
}
292292

293-
if (this.RequestedStyle.HasFlag(FontStyle.Italic))
293+
if ((this.RequestedStyle & FontStyle.Italic) == FontStyle.Italic)
294294
{
295295
// Can't find style requested and they want one that's at least partial italic.
296296
// Try the regular italic.
@@ -300,7 +300,7 @@ private string LoadFontName()
300300
}
301301
}
302302

303-
if (this.RequestedStyle.HasFlag(FontStyle.Bold))
303+
if ((this.RequestedStyle & FontStyle.Bold) == FontStyle.Bold)
304304
{
305305
// Can't find style requested and they want one that's at least partial bold.
306306
// Try the regular bold.

src/SixLabors.Fonts/FontDescription.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,24 +172,24 @@ private static FontStyle ConvertStyle(OS2Table? os2, HeadTable? head)
172172

173173
if (os2 != null)
174174
{
175-
if (os2.FontStyle.HasFlag(OS2Table.FontStyleSelection.BOLD))
175+
if ((os2.FontStyle & OS2Table.FontStyleSelection.BOLD) == OS2Table.FontStyleSelection.BOLD)
176176
{
177177
style |= FontStyle.Bold;
178178
}
179179

180-
if (os2.FontStyle.HasFlag(OS2Table.FontStyleSelection.ITALIC))
180+
if ((os2.FontStyle & OS2Table.FontStyleSelection.ITALIC) == OS2Table.FontStyleSelection.ITALIC)
181181
{
182182
style |= FontStyle.Italic;
183183
}
184184
}
185185
else if (head != null)
186186
{
187-
if (head.MacStyle.HasFlag(HeadTable.HeadMacStyle.Bold))
187+
if ((head.MacStyle & HeadTable.HeadMacStyle.Bold) == HeadTable.HeadMacStyle.Bold)
188188
{
189189
style |= FontStyle.Bold;
190190
}
191191

192-
if (head.MacStyle.HasFlag(HeadTable.HeadMacStyle.Italic))
192+
if ((head.MacStyle & HeadTable.HeadMacStyle.Italic) == HeadTable.HeadMacStyle.Italic)
193193
{
194194
style |= FontStyle.Italic;
195195
}

src/SixLabors.Fonts/GlyphMetrics.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ internal GlyphMetrics(
5050

5151
Vector2 offset = Vector2.Zero;
5252
Vector2 scaleFactor = new(unitsPerEM * 72F);
53-
if (textAttributes.HasFlag(TextAttributes.Subscript))
53+
if ((textAttributes & TextAttributes.Subscript) == TextAttributes.Subscript)
5454
{
5555
float units = this.UnitsPerEm;
5656
scaleFactor /= new Vector2(font.SubscriptXSize / units, font.SubscriptYSize / units);
5757
offset = new(font.SubscriptXOffset, font.SubscriptYOffset < 0 ? font.SubscriptYOffset : -font.SubscriptYOffset);
5858
}
59-
else if (textAttributes.HasFlag(TextAttributes.Superscript))
59+
else if ((textAttributes & TextAttributes.Superscript) == TextAttributes.Superscript)
6060
{
6161
float units = this.UnitsPerEm;
6262
scaleFactor /= new Vector2(font.SuperscriptXSize / units, font.SuperscriptYSize / units);

src/SixLabors.Fonts/StreamFontMetrics.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ private static HorizontalMetrics InitializeHorizontalMetrics(HorizontalHeadTable
423423
// 3.If they are zero and the OS/ 2 table exists,
424424
// - Use the OS/ 2 table's sTypo* metrics if they are non-zero.
425425
// - Otherwise, use the OS / 2 table's usWin* metrics.
426-
bool useTypoMetrics = os2.FontStyle.HasFlag(OS2Table.FontStyleSelection.USE_TYPO_METRICS);
426+
bool useTypoMetrics = (os2.FontStyle & OS2Table.FontStyleSelection.USE_TYPO_METRICS) == OS2Table.FontStyleSelection.USE_TYPO_METRICS;
427427
if (useTypoMetrics)
428428
{
429429
ascender = os2.TypoAscender;

src/SixLabors.Fonts/Tables/AdvancedTypographic/GPos/LookupType3SubTable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public override bool TryUpdatePosition(
176176
int parent = nextIndex;
177177
int xOffset = entryXY.XCoordinate - exitXY.XCoordinate;
178178
int yOffset = entryXY.YCoordinate - exitXY.YCoordinate;
179-
if (this.LookupFlags.HasFlag(LookupFlags.RightToLeft))
179+
if ((this.LookupFlags & LookupFlags.RightToLeft) == LookupFlags.RightToLeft)
180180
{
181181
(parent, child) = (child, parent);
182182

src/SixLabors.Fonts/Tables/General/OS2Table.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,11 @@ public OS2Table(OS2Table versionLessThan5Table, ushort lowerOpticalPointSize, us
156156
this.upperOpticalPointSize = upperOpticalPointSize;
157157
}
158158

159+
[Flags]
159160
internal enum FontStyleSelection : ushort
160161
{
162+
NONE = 0,
163+
161164
// 0 bit 1 ITALIC Font contains italic or oblique characters, otherwise they are upright.
162165
ITALIC = 1,
163166

src/SixLabors.Fonts/Tables/TrueType/Glyphs/SimpleGlyphLoader.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static GlyphLoader LoadSimpleGlyph(BigEndianBinaryReader reader, short co
7575
var controlPoints = new ControlPoint[xs.Length];
7676
for (int i = 0; i < flags.Length; i++)
7777
{
78-
controlPoints[i] = new(new Vector2(xs[i], ys[i]), flags[i].HasFlag(Flags.OnCurve));
78+
controlPoints[i] = new(new Vector2(xs[i], ys[i]), (flags[i] & Flags.OnCurve) == Flags.OnCurve);
7979
}
8080

8181
return new SimpleGlyphLoader(controlPoints, endPoints, bounds, instructions);
@@ -96,7 +96,7 @@ private static Flags[] ReadFlags(BigEndianBinaryReader reader, int flagCount)
9696
else
9797
{
9898
flag = (Flags)reader.ReadUInt8();
99-
if (flag.HasFlag(Flags.Repeat))
99+
if ((flag & Flags.Repeat) == Flags.Repeat)
100100
{
101101
repeatCount = reader.ReadByte();
102102
}
@@ -115,21 +115,19 @@ private static short[] ReadCoordinates(BigEndianBinaryReader reader, int pointCo
115115
for (int i = 0; i < pointCount; i++)
116116
{
117117
short dx;
118-
if (flags[i].HasFlag(isByte))
118+
Flags currentFlag = flags[i];
119+
if ((currentFlag & isByte) == isByte)
119120
{
120121
byte b = reader.ReadByte();
121-
dx = (short)(flags[i].HasFlag(signOrSame) ? b : -b);
122+
dx = (short)((currentFlag & signOrSame) == signOrSame ? b : -b);
123+
}
124+
else if ((currentFlag & signOrSame) == signOrSame)
125+
{
126+
dx = 0;
122127
}
123128
else
124129
{
125-
if (flags[i].HasFlag(signOrSame))
126-
{
127-
dx = 0;
128-
}
129-
else
130-
{
131-
dx = reader.ReadInt16();
132-
}
130+
dx = reader.ReadInt16();
133131
}
134132

135133
x += dx;

0 commit comments

Comments
 (0)