Skip to content

Commit a33659a

Browse files
committed
WIP3
1 parent a731639 commit a33659a

File tree

2 files changed

+45
-26
lines changed

2 files changed

+45
-26
lines changed

src/FontStashSharp/FontAtlas.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using FontStashSharp.Rasterizers.FreeType;
33
using Microsoft.Xna.Framework;
44
using Microsoft.Xna.Framework.Graphics;
5-
5+
using FreeTypeSharp;
66

77
namespace FontStashSharp
88
{
@@ -168,6 +168,16 @@ public void RenderGlyph(GraphicsDevice graphicsDevice, DynamicFontGlyph glyph, F
168168
return;
169169
}
170170

171+
fontSource.RasterizeGlyphBitmap(glyph.Id, glyph.FontSize, fontStyle);
172+
173+
if (fontStyle.HasFlag(FontStyle.Italic) && !fontSource.HasStyleFlag(FT_STYLE_FLAG.FT_STYLE_FLAG_ITALIC))
174+
{
175+
fontSource.GetCurrentGlyph(out var glyphRec);
176+
// Set new advance and size for italic glyph
177+
glyph.XAdvance = (int)glyphRec.advance.x >> 6;
178+
glyph.Size = new Point((int)glyphRec.bitmap.width, (int)glyphRec.bitmap.rows);
179+
}
180+
171181
const int cBytesPerPixel = 4;
172182

173183
// Render glyph to pixel buffer(rgba32)
@@ -210,7 +220,7 @@ public void RenderGlyph(GraphicsDevice graphicsDevice, DynamicFontGlyph glyph, F
210220

211221
Texture2DManager.SetTextureData(Texture, eraseArea, _eraseBuffer);
212222

213-
fontSource.RasterizeGlyphBitmap(glyph.Id,
223+
fontSource.FetchGlyphBitmapBuffer(glyph.Id,
214224
glyph.FontSize,
215225
pixelBuffer,
216226
0,

src/FontStashSharp/Rasterizers/FreeTypeSource.cs

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private void LoadGlyph(int glyphId)
102102
throw new FreeTypeException(err);
103103
}
104104

105-
private void GetCurrentGlyph(out FT_GlyphSlotRec_ glyph)
105+
public void GetCurrentGlyph(out FT_GlyphSlotRec_ glyph)
106106
{
107107
glyph = Marshal.PtrToStructure<FT_GlyphSlotRec_>((IntPtr)_faceRec->glyph);
108108
}
@@ -137,30 +137,8 @@ static byte LuminanceFromLinearRGB(byte r, byte g, byte b)
137137
// Y = 0.2126*R + 0.7152*G + 0.0722*B. Computed on the integer pipe.
138138
return (byte)((4732UL * r + 46871UL * g + 13933UL * b) >> 16);
139139
}
140-
public unsafe void RasterizeGlyphBitmap(int glyphId, float fontSize, byte[] buffer, int startIndex, int outWidth, int outHeight, int outStride, FontStyle fontStyle)
140+
public void FetchGlyphBitmapBuffer(int glyphId, float fontSize, byte[] buffer, int startIndex, int outWidth, int outHeight, int outStride, FontStyle fontStyle)
141141
{
142-
SetPixelSizes(0, fontSize);
143-
LoadGlyph(glyphId);
144-
145-
FT_Render_Glyph(_faceRec->glyph, _renderMode);
146-
147-
if ((fontStyle & FontStyle.Bold) != 0 && (_faceRec->style_flags & (int)FT_STYLE_FLAG.FT_STYLE_FLAG_BOLD) == 0)
148-
FT_Bitmap_Embolden(_libraryHandle, &_faceRec->glyph->bitmap, 64, 0);
149-
150-
if ((fontStyle & FontStyle.Italic) != 0 && (_faceRec->style_flags & (int)FT_STYLE_FLAG.FT_STYLE_FLAG_ITALIC) == 0)
151-
{
152-
FT_Matrix_ italicMatrix = new FT_Matrix_
153-
{
154-
xx = 0x10000,
155-
xy = (nint)(0.3f * 0x10000),
156-
yx = 0,
157-
yy = 0x10000
158-
};
159-
FT_Set_Transform(_faceRec, &italicMatrix, default);
160-
}
161-
else
162-
FT_Set_Transform(_faceRec, default, default);
163-
164142
FT_GlyphSlotRec_ glyph;
165143
GetCurrentGlyph(out glyph);
166144
var ftbmp = glyph.bitmap;
@@ -229,5 +207,36 @@ public unsafe void RasterizeGlyphBitmap(int glyphId, float fontSize, byte[] buff
229207
}
230208
}
231209
}
210+
211+
public void RasterizeGlyphBitmap(int glyphId, float fontSize, FontStyle fontStyle)
212+
{
213+
SetPixelSizes(0, fontSize);
214+
215+
if ((fontStyle & FontStyle.Italic) != 0 && (_faceRec->style_flags & (int)FT_STYLE_FLAG.FT_STYLE_FLAG_ITALIC) == 0)
216+
{
217+
FT_Matrix_ italicMatrix = new FT_Matrix_
218+
{
219+
xx = 0x10000,
220+
xy = (nint)(0.3f * 0x10000),
221+
yx = 0,
222+
yy = 0x10000
223+
};
224+
FT_Set_Transform(_faceRec, &italicMatrix, default);
225+
}
226+
else
227+
FT_Set_Transform(_faceRec, default, default);
228+
229+
LoadGlyph(glyphId);
230+
231+
FT_Render_Glyph(_faceRec->glyph, _renderMode);
232+
233+
if ((fontStyle & FontStyle.Bold) != 0 && !HasStyleFlag(FT_STYLE_FLAG.FT_STYLE_FLAG_BOLD))
234+
FT_Bitmap_Embolden(_libraryHandle, &_faceRec->glyph->bitmap, 64, 0); // 1Pixel embolden
235+
}
236+
237+
public bool HasStyleFlag(FT_STYLE_FLAG flag)
238+
{
239+
return (_faceRec->style_flags & (int)flag) != 0;
240+
}
232241
}
233242
}

0 commit comments

Comments
 (0)