Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions Game.Shared/GarrysMod/GarrysMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,29 @@ void AddLanguageFiles(ReadOnlySpan<char> language) {
}
filesystem.FindClose(handle);
}

void AddCustomFontsFromFolder(string folder) {
HashSet<string> workshopFonts = [];
ReadOnlySpan<char> fileName = filesystem.FindFirstEx($"{folder}/*.ttf", "workshop", out ulong handle);
while (!fileName.IsEmpty) {
workshopFonts.Add(new(fileName));
fileName = filesystem.FindNext(handle);
}
filesystem.FindClose(handle);

fileName = filesystem.FindFirstEx($"{folder}/*.ttf", "GAME", out handle);
while (!fileName.IsEmpty) {
if (!workshopFonts.Contains(new(fileName)))
Surface.AddCustomFontFile(fileName, $"{folder}/{fileName}");
fileName = filesystem.FindNext(handle);
}
filesystem.FindClose(handle);
}

void AddCustomFonts() {
AddCustomFontsFromFolder("resource/fonts");
AddCustomFontsFromFolder("cache/workshop/resource/fonts");
}
#endif

public void InitializeMod(IServiceProvider services) {
Expand All @@ -98,8 +121,9 @@ public void InitializeMod(IServiceProvider services) {
#endif
#if !SWDS
get.IntroScreen()?.Update("Adding Custom Fonts", true);
// todo: AddCustomFonts
#if CLIENT_DLL
AddCustomFonts();

if (filesystem == null)
Error("g_pFullFileSystem is NULL!?");
if (filesystem!.Language() == null)
Expand Down Expand Up @@ -157,7 +181,7 @@ public void LevelInit(ReadOnlySpan<char> mapName) {
get.Audio()?.StopAllPlayback();

engine.ClientCmd("net_maxroutable 1260");
// todo: AddCustomFonts();
AddCustomFonts();

if (gpGlobals.MaxClients > 1) {
// todo: ParseParticleEffects(true, false);
Expand Down
20 changes: 18 additions & 2 deletions Source.MaterialSystem/Surface/Clip2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ public unsafe static int ClipPolygon(in ScissorRectState state, Span<SurfaceVert
return clipPolygon.ClipCount;
}

static bool StretchTexture = false;

static float InterpTCoord(float val, float mins, float maxs, float tMin, float tMax) {
float percent = (mins != maxs) ? (val - mins) / (maxs - mins) : 0.5f;
return tMin + (tMax - tMin) * percent;
}

public static bool ClipRect(in ScissorRectState scissorRect, in SurfaceVertex inUL, in SurfaceVertex inLR, out SurfaceVertex outUL, out SurfaceVertex outLR) {
if (scissorRect.Scissor) {
outUL = default;
Expand All @@ -178,8 +185,17 @@ public static bool ClipRect(in ScissorRectState scissorRect, in SurfaceVertex in
if (outUL.Position.X > outLR.Position.X || outUL.Position.Y > outLR.Position.Y)
return false;

outUL.TexCoord = inUL.TexCoord;
outLR.TexCoord = inLR.TexCoord;
if (!StretchTexture) {
outUL.TexCoord.X = InterpTCoord(outUL.Position.X, inUL.Position.X, inLR.Position.X, inUL.TexCoord.X, inLR.TexCoord.X);
outUL.TexCoord.Y = InterpTCoord(outUL.Position.Y, inUL.Position.Y, inLR.Position.Y, inUL.TexCoord.Y, inLR.TexCoord.Y);

outLR.TexCoord.X = InterpTCoord(outLR.Position.X, inUL.Position.X, inLR.Position.X, inUL.TexCoord.X, inLR.TexCoord.X);
outLR.TexCoord.Y = InterpTCoord(outLR.Position.Y, inUL.Position.Y, inLR.Position.Y, inUL.TexCoord.Y, inLR.TexCoord.Y);
}
else {
outUL.TexCoord = inUL.TexCoord;
outLR.TexCoord = inLR.TexCoord;
}
}
else {
outUL = inUL;
Expand Down
163 changes: 163 additions & 0 deletions Source.MaterialSystem/Surface/FontEffects.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
namespace Source.MaterialSystem.Surface;

internal static class FontEffects
{
internal static void ApplyDropShadowToTexture(int rgbaWide, int rgbaTall, Span<byte> rgba, uint dropShadowOffset) {
if (dropShadowOffset == 0)
return;

for (int y = rgbaTall - 1; y >= dropShadowOffset; y--) {
for (int x = rgbaWide - 1; x >= dropShadowOffset; x--) {
Span<byte> dest = rgba[((x + (y * rgbaWide)) * 4)..];
if (dest[3] == 0) {
Span<byte> src = rgba[(int)((x - dropShadowOffset + ((y - dropShadowOffset) * rgbaWide)) * 4)..];
dest[0] = 0;
dest[1] = 0;
dest[2] = 0;
dest[3] = src[3];
}
}
}
}
internal static void ApplyGaussianBlurToTexture(int rgbaWide, int rgbaTall, Span<byte> rgba, uint nBlur) {
if (nBlur == 0)
return;

int filterWidth = (int)(nBlur * 2 + 1);
Span<float> pGaussianDistribution = stackalloc float[filterWidth];
float sigma = 0.683f * nBlur;
for (int x = 0; x <= nBlur * 2; x++) {
int val = x - (int)nBlur;
pGaussianDistribution[x] = (1.0f / MathF.Sqrt(2 * 3.14f * sigma * sigma)) * MathF.Pow(2.7f, -1.0f * (val * val) / (2 * sigma * sigma));
}

int totalBytes = rgbaWide * rgbaTall * 4;
Span<byte> src = new byte[totalBytes];
rgba[..totalBytes].CopyTo(src);

for (int x = 0; x < rgbaWide; x++) {
int distOffset = 0;
int nValues = filterWidth;
int nOffset = 0;
if (x < nBlur) {
nOffset += (int)nBlur - x;
distOffset += nOffset;
nValues -= nOffset;
}
if (x >= rgbaWide - nBlur) {
nValues = rgbaWide - (x - nOffset);
}

for (int y = 0; y < rgbaTall; y++) {
int readIndex = (y * rgbaWide + x + nOffset - (int)nBlur) * 4 + 3;
float accum = 0.0f;
for (int n = 0; n < nValues; n++) {
accum += pGaussianDistribution[distOffset + n] * src[readIndex + n * 4];
}
byte alpha = (byte)accum;

int dstIndex = (y * rgbaWide + x) * 4;
rgba[dstIndex + 0] = rgba[dstIndex + 1] = rgba[dstIndex + 2] = alpha > 0 ? (byte)255 : (byte)0;
rgba[dstIndex + 3] = alpha;
}
}

rgba[..totalBytes].CopyTo(src);

for (int y = 0; y < rgbaTall; y++) {
int distOffset = 0;
int nValues = filterWidth;
int nOffset = 0;
if (y < nBlur) {
nOffset += (int)nBlur - y;
distOffset += nOffset;
nValues -= nOffset;
}
if (y >= rgbaTall - nBlur) {
nValues = rgbaTall - (y - nOffset);
}

for (int x = 0; x < rgbaWide; x++) {
int readIndex = ((y + nOffset - (int)nBlur) * rgbaWide + x) * 4 + 3;
float accum = 0.0f;
for (int n = 0; n < nValues; n++) {
accum += pGaussianDistribution[distOffset + n] * src[readIndex + n * 4 * rgbaWide];
}
byte alpha = (byte)accum;

int dstIndex = (y * rgbaWide + x) * 4;
rgba[dstIndex + 0] = rgba[dstIndex + 1] = rgba[dstIndex + 2] = alpha > 0 ? (byte)255 : (byte)0;
rgba[dstIndex + 3] = alpha;
}
}
}
internal static void ApplyScanlineEffectToTexture(int rgbaWide, int rgbaTall, Span<byte> rgba, uint scanLines) {
if (scanLines < 2)
return;

const float scale = 0.7f;

for (int y = 0; y < rgbaTall; y++) {
if (y % scanLines == 0)
continue;

Span<byte> pBits = rgba[((y * rgbaWide) * 4)..];

for (int x = 0; x < rgbaWide; x++, pBits = pBits[4..]) {
pBits[0] = (byte)(pBits[0] * scale);
pBits[1] = (byte)(pBits[1] * scale);
pBits[2] = (byte)(pBits[2] * scale);
}
}
}
internal static void ApplyRotaryEffectToTexture(int rgbaWide, int rgbaTall, Span<byte> rgba, bool rotary) {
if (!rotary)
return;

int y = rgbaTall / 2;

Span<byte> line = rgba[((y * rgbaWide) * 4)..];

for (int x = 0; x < rgbaWide; x++, line = line[4..]) {
line[0] = 127;
line[1] = 127;
line[2] = 127;
line[3] = 255;
}
}
internal static void ApplyOutlineToTexture(int rgbaWide, int rgbaTall, Span<byte> rgba, uint outlineSize) {
if (outlineSize == 0)
return;

int x, y;
for (y = 0; y < rgbaTall; y++) {
for (x = 0; x < rgbaWide; x++) {
Span<byte> src = rgba[((x + (y * rgbaWide)) * 4)..];
if (src[3] == 0) {
int shadowX, shadowY;
for (shadowX = -(int)outlineSize; shadowX <= (int)outlineSize; shadowX++) {
for (shadowY = -(int)outlineSize; shadowY <= (int)outlineSize; shadowY++) {
if (shadowX == 0 && shadowY == 0) {
continue;
}
int testX, testY;
testX = shadowX + x;
testY = shadowY + y;
if (testX < 0 || testX >= rgbaWide ||
testY < 0 || testY >= rgbaTall) {
continue;
}
Span<byte> test = rgba[((testX + (testY * rgbaWide)) * 4)..];
if (test[0] != 0 && test[1] != 0 && test[2] != 0 && test[3] != 0) {
src[0] = 0;
src[1] = 0;
src[2] = 0;
src[3] = 255;
}
}
}
}
}
}
}
}
78 changes: 76 additions & 2 deletions Source.MaterialSystem/Surface/FontManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,12 @@ public FontManager(IMaterialSystem materialSystem, IFileSystem fileSystem, ISyst
}

List<FontAmalgam> FontAmalgams = [];
#if WIN32
List<Win32Font> Win32Fonts = [];
List<string> CustomFontFileNames = [];
#else
List<FreeTypeFont> FreeTypeFonts = [];
#endif

static ulong FontHash(ReadOnlySpan<char> name, int weight) => sprintf(stackalloc char[name.Length + 16], "%s-%i").S(name).I(weight).ToSpan().Hash();

Expand All @@ -171,7 +176,13 @@ public FontManager(IMaterialSystem materialSystem, IFileSystem fileSystem, ISyst
Dictionary<ulong, string> CustomFontFiles = [];

public void ClearAllFonts() {
#if WIN32
for (int i = 0; i < Win32Fonts.Count; i++)
Win32Fonts[i].Dispose();
Win32Fonts.Clear();
#else
FreeTypeFonts.Clear();
#endif

for (int i = 0; i < FontAmalgams.Count; i++)
FontAmalgams[i].RemoveAll();
Expand All @@ -192,7 +203,7 @@ internal bool SetFontGlyphSet(IFont font, ReadOnlySpan<char> fontName, int tall,
if (fontAmalgam.GetCount() > 0)
fontAmalgam.RemoveAll();

FreeTypeFont? baseFont = CreateOrFindFreeTypeFont(fontName, tall, weight, blur, scanlines, flags);
BaseFont? baseFont = CreateOrFindFont(fontName, tall, weight, blur, scanlines, flags);

do {
if (IsFontForeignLanguageCapable(fontName)) {
Expand All @@ -208,7 +219,7 @@ internal bool SetFontGlyphSet(IFont font, ReadOnlySpan<char> fontName, int tall,
return true;
}

FreeTypeFont? extendedFont = CreateOrFindFreeTypeFont(localizedFontName, tall, weight, blur, scanlines, flags);
BaseFont? extendedFont = CreateOrFindFont(localizedFontName, tall, weight, blur, scanlines, flags);
if (baseFont != null && extendedFont != null) {
int min = 0x0000, max = 0x00FF;

Expand Down Expand Up @@ -300,6 +311,40 @@ private bool IsFontForeignLanguageCapable(ReadOnlySpan<char> fontName) {
return (byte*)binary;
}

private BaseFont? CreateOrFindFont(ReadOnlySpan<char> fontName, int tall, int weight, int blur, int scanlines, SurfaceFontFlags flags) {
#if WIN32
return CreateOrFindWin32Font(fontName, tall, weight, blur, scanlines, flags);
#else
return CreateOrFindFreeTypeFont(fontName, tall, weight, blur, scanlines, flags);
#endif
}

#if WIN32
private Win32Font? CreateOrFindWin32Font(ReadOnlySpan<char> windowsFontName, int tall, int weight, int blur, int scanlines, SurfaceFontFlags flags) {
Win32Font? winFont = null;
int i;
for (i = 0; i < Win32Fonts.Count; i++) {
if (Win32Fonts[i].IsEqualTo(windowsFontName, tall, weight, blur, scanlines, flags)) {
winFont = Win32Fonts[i];
break;
}
}

if (winFont == null) {
Win32Font font = new();
if (font.Create(windowsFontName, tall, weight, blur, scanlines, flags)) {
Win32Fonts.Add(font);
winFont = font;
}
else {
font.Dispose();
return null;
}
}

return winFont;
}
#else
private FreeTypeFont? CreateOrFindFreeTypeFont(ReadOnlySpan<char> fontName, int tall, int weight, int blur, int scanlines, SurfaceFontFlags flags) {
if (!FontsReady) InitFontSettings();

Expand Down Expand Up @@ -342,6 +387,7 @@ private bool IsFontForeignLanguageCapable(ReadOnlySpan<char> fontName) {
}
return foundFont;
}
#endif

public struct FT_SfntName
{
Expand All @@ -354,6 +400,33 @@ public struct FT_SfntName
}

internal bool AddCustomFontFile(ReadOnlySpan<char> fontName, ReadOnlySpan<char> fontFileName) {
#if WIN32
ReadOnlySpan<char> fullPath = fileSystem.RelativePathToFullPath(fontFileName, null, stackalloc char[260]);
if (fullPath.IsEmpty) {
Warning($"Couldn't find custom font file '{fontFileName}'\n");
return false;
}

string path = new(fullPath);
int i;
for (i = 0; i < CustomFontFileNames.Count; i++) {
if (CustomFontFileNames[i] == path)
break;
}
if (i >= CustomFontFileNames.Count) {
CustomFontFileNames.Add(path);
fileSystem.GetLocalCopy(path);
}

if (Win32Font.AddFontResourceExA(path, Win32Font.FR_PRIVATE, 0) > 0)
return true;

if (Win32Font.AddFontResourceA(path) < 1) {
Warning($"Failed to load custom font file '{path}'\n");
return false;
}
return true;
#else
ReadOnlySpan<char> fontFilepath = fileSystem.RelativePathToFullPath(fontFileName, null, stackalloc char[512]);

if (fontFilepath.IsEmpty) {
Expand Down Expand Up @@ -403,6 +476,7 @@ internal bool AddCustomFontFile(ReadOnlySpan<char> fontName, ReadOnlySpan<char>
CustomFontFiles[fontName.Hash()] = new(fontFilepath);

return true;
#endif
}

internal int GetFontTall(IFont? font) => ((FontAmalgam?)font)?.GetFontHeight() ?? 0;
Expand Down
Loading
Loading