From 9ab1010e02ee38077ef66f6d0cb11c368ee60ce0 Mon Sep 17 00:00:00 2001
From: wuke65536 <2446214230@qq.com>
Date: Thu, 11 Jun 2026 23:33:22 +0800
Subject: [PATCH] disable cache for (string, Rectangle, float, Vector2,
Vector2, SpriteEffects, float) tuple
---
Velentr.Font/FontCollection.cs | 38 +---------------------------------
1 file changed, 1 insertion(+), 37 deletions(-)
diff --git a/Velentr.Font/FontCollection.cs b/Velentr.Font/FontCollection.cs
index 303c9b9..b4676e0 100644
--- a/Velentr.Font/FontCollection.cs
+++ b/Velentr.Font/FontCollection.cs
@@ -1,4 +1,4 @@
-using System;
+using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -33,11 +33,6 @@ public class FontCollection : IDisposable
public Font MainFont;
public Font AltFont;
- ///
- /// The calculation cache.
- ///
- private Dictionary<(string, Rectangle, float, Vector2, Vector2, SpriteEffects, float), List<(Vector2, Glyph)>> _calculationCache = new();
-
///
/// Initializes a new instance of the class.
///
@@ -117,18 +112,6 @@ public Glyph GenerateGlyph(char character)
/// The color.
/// The line spacing.
public void Draw(SpriteBatch spriteBatch, string text, Rectangle boundaries, Color color, int lineSpacing = 0) {
- var key = (text, boundaries, 0f, Vector2.Zero, Vector2.One, SpriteEffects.None, lineSpacing);
- if (_calculationCache.TryGetValue(key, out var items)) {
- foreach (var (position, character) in items) {
- float rotation = key.Item3;
- var origin = key.Item4;
- var scale = key.Item5;
- spriteBatch.Draw(character.GlyphCache.Texture, position, character.Boundary, color, rotation, origin, scale, SpriteEffects.None, 0f);
- }
-
- return;
- }
-
var warpLine = boundaries.Width > 0;
var offsetX = 0;
var offsetY = 0;
@@ -141,7 +124,6 @@ public void Draw(SpriteBatch spriteBatch, string text, Rectangle boundaries, Col
var finalCharacterIndex = text.Length - 1;
var currentColor = color;
- var infos = new List<(Vector2, Glyph)>();
for (var i = 0; i < text.Length; i++) {
TryGetGlyph(text[i], out var cachedCharacter, out var font);
lineSpacing = lineSpacing is 0 ? cachedCharacter.AdvanceY : lineSpacing;
@@ -172,8 +154,6 @@ public void Draw(SpriteBatch spriteBatch, string text, Rectangle boundaries, Col
var position = new Vector2(boundaries.X + offsetX, boundaries.Y + offsetY);
- infos.Add((position, cachedCharacter));
-
spriteBatch.Draw(cachedCharacter.GlyphCache.Texture, position, cachedCharacter.Boundary, currentColor, 0f, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
offsetX += cachedCharacter.Boundary.Width;
@@ -189,8 +169,6 @@ public void Draw(SpriteBatch spriteBatch, string text, Rectangle boundaries, Col
}
}
}
-
- _calculationCache.Add(key, infos);
}
///
@@ -219,14 +197,6 @@ public void Draw(SpriteBatch spriteBatch, string text, Vector2 position, Color c
/// The line spacing.
public void Draw(SpriteBatch spriteBatch, string text, Rectangle boundaries, Color color, float rotation,
Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth, int lineSpacing = 0) {
- var key = (text, boundaries, rotation, origin, scale, effects, lineSpacing);
- if (_calculationCache.TryGetValue(key, out var items)) {
- foreach (var (position, character) in items) {
- spriteBatch.Draw(character.GlyphCache.Texture, position, character.Boundary, color, rotation, origin, scale, effects, 0f);
- }
-
- return;
- }
// calculate the rest of the text position
var warpLine = boundaries.Width > 0;
@@ -241,7 +211,6 @@ public void Draw(SpriteBatch spriteBatch, string text, Rectangle boundaries, Col
var finalCharacterIndex = text.Length - 1;
var currentColor = color;
- var infos = new List<(Vector2, Glyph)>();
for (var i = 0; i < text.Length; i++) {
TryGetGlyph(text[i], out var cachedCharacter, out var font);
lineSpacing = lineSpacing is 0 ? cachedCharacter.AdvanceY : lineSpacing;
@@ -317,8 +286,6 @@ public void Draw(SpriteBatch spriteBatch, string text, Rectangle boundaries, Col
var characterPosition = new Vector2(offsetX, offsetY);
Vector2.Transform(ref characterPosition, ref transformation, out characterPosition);
- infos.Add((characterPosition, cachedCharacter));
-
spriteBatch.Draw(cachedCharacter.GlyphCache.Texture, characterPosition, cachedCharacter.Boundary,
currentColor, rotation, origin, scale, effects, layerDepth);
offsetX += cachedCharacter.Boundary.Width;
@@ -335,8 +302,6 @@ public void Draw(SpriteBatch spriteBatch, string text, Rectangle boundaries, Col
}
}
}
-
- _calculationCache.Add(key, infos);
}
///
@@ -442,7 +407,6 @@ public Vector2 MeasureText(string text, int lineSpacing = 0) {
public void Dispose() {
TextCache.Clear();
- _calculationCache.Clear();
_glyphCaches.Clear();
CharacterGlyphs.Clear();
}