Skip to content

Commit

Permalink
Fix text renderer bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kagamia committed Jan 3, 2017
1 parent 459b75e commit 2e3af70
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 8 deletions.
58 changes: 54 additions & 4 deletions WzComparerR2/CharaSimControl/GearGraphics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ public static void DrawString(Graphics g, string s, Font font, int x, int x1, re

using (var r = new FormattedTextRenderer())
{
r.WordWrapEnabled = false;
r.WordWrapEnabled = true;
r.UseGDIRenderer = false;
r.DrawString(g, s, font, x, x1, ref y, height);
}
}
Expand Down Expand Up @@ -419,6 +420,7 @@ public FormattedTextRenderer()
}

public bool WordWrapEnabled { get; set; }
public bool UseGDIRenderer { get; set; }

const int MAX_RANGES = 32;
StringFormat fmt;
Expand Down Expand Up @@ -638,6 +640,25 @@ private void MeasureRuns(List<Run> runs)
}

MeasureBatch(tempRuns);

//failed
if (runs.Where(run => !run.IsBreakLine && run.Length > 0)
.All(run => run.Width == 0))
{
float x = 0;
foreach (var run in runs.Where(r => !r.IsBreakLine))
{
run.X = (int)Math.Round(x);
float width = 0;
for (int i = 0; i < run.Length; i++)
{
var chr = this.sb[run.StartIndex + i];
width += chr > 0xff ? this.font.Size : (this.font.Size / 2);
}
run.Width = (int)Math.Round(x);
x += width;
}
}
}

private void MeasureBatch(List<Run> runs)
Expand Down Expand Up @@ -682,6 +703,23 @@ private Rectangle[] MeasureChars(int startIndex, int length)
}
}

//failed
if (rects.All(rect => rect.Width == 0))
{
float x = 0;
for (int i = 0; i < rects.Length; i++)
{
var chr = this.sb[startIndex + i];
var width = chr > 0xff ? this.font.Size : (this.font.Size / 2);
rects[i] = new Rectangle(
(int)Math.Round(x),
0,
(int)Math.Round(width),
font.Height
);
}
}

return rects;
}

Expand All @@ -703,7 +741,18 @@ private void DrawRuns(List<Run> runs, int x, int x1, ref int y, int lineHeight)
if (hasContent())
{
string content = sb.ToString(start, end - start);
TR.DrawText(g, content, font, new Point(drawX, drawY), color);
if (this.UseGDIRenderer)
{
TR.DrawText(g, content, font, new Point(drawX, drawY), color);
}
else
{
using (var brush = new SolidBrush(color))
{
g.DrawString(content, font, brush, drawX, drawY, fmt);
}
}
}
if (isNewLine)
{
Expand All @@ -730,7 +779,7 @@ private void DrawRuns(List<Run> runs, int x, int x1, ref int y, int lineHeight)
}
else
{
if (run.ForeColor != color)
if (!run.IsWhiteSpace && run.ForeColor != color)
{
end = run.StartIndex;
curX = x + run.X - xOffset;
Expand All @@ -748,7 +797,7 @@ private void DrawRuns(List<Run> runs, int x, int x1, ref int y, int lineHeight)
curX = x + run.X - xOffset;
if (this.WordWrapEnabled ? (x1 - curX < run.Width) : (curX >= x1)) //奇怪的算法 暂定
{ //宽度不够
if (hasContent())
if (curX > x) //(hasContent())
{ //有内容
flush(true);
start = run.StartIndex;
Expand All @@ -774,6 +823,7 @@ private void DrawRuns(List<Run> runs, int x, int x1, ref int y, int lineHeight)
{ //限定至少输出一个字符
end = start + 1;
flush(true);
xOffset = rects[i].Right;
continue;
}
else
Expand Down
6 changes: 2 additions & 4 deletions WzComparerR2/CharaSimControl/GearTooltipRender2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ private Bitmap RenderBase(out int picH)
{
Bitmap bitmap = new Bitmap(261, DefaultPicHeight);
Graphics g = Graphics.FromImage(bitmap);
StringFormat format = (StringFormat)StringFormat.GenericDefault.Clone();
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
StringFormat format = (StringFormat)StringFormat.GenericTypographic.Clone();
int value;

picH = 13;
Expand Down Expand Up @@ -185,9 +186,6 @@ private Bitmap RenderBase(out int picH)
g.DrawString(gearName, GearGraphics.ItemNameFont2,
GearGraphics.GetGearNameBrush(Gear.diff, Gear.ScrollUp > 0), 130, picH, format);
picH += 23;
format.Dispose();
format = (StringFormat)StringFormat.GenericTypographic.Clone();
format.Alignment = StringAlignment.Center;

//装备rank
string rankStr = null;
Expand Down

1 comment on commit 2e3af70

@Sunaries
Copy link

@Sunaries Sunaries commented on 2e3af70 Jan 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Kagamia Do you think I can add f0845fc and 2e3af70 on top of the code that KENNYSOFT provided with the GDI change? Will there be no conflicts?

I used a backup version where I added your code along with KENNYSOFT's code.
This is the result:

Current version without your code:

Your code fixes the medal style, however the font is incorrect.
Because look at this:

In-game version

Version with your code:

Version without your code:

Font is the same, Arial. Also your code nicely wraps the orange description.
Is there a way to fix this so it can keep the font that I currently have in my version without the new code? Because the medal style fix is great too.

Please sign in to comment.