forked from genstein/trizbort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextBlock.cs
258 lines (225 loc) · 10.2 KB
/
TextBlock.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
Copyright (c) 2010 by Genstein
This file is (or was originally) part of Trizbort, the Interactive Fiction Mapper.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using PdfSharp.Drawing;
namespace Trizbort
{
class TextBlock
{
public string Text
{
get { return m_text; }
set
{
m_text = value;
m_invalidLayout = true;
}
}
/// <summary>
/// Draw a multi-line string as it would be drawn by GDI+.
/// Compensates for issues and draw-vs-PDF differences in PDFsharp.
/// </summary>
/// <param name="graphics">The graphics with which to draw.</param>
/// <param name="text">The text to draw, which may contain line breaks.</param>
/// <param name="font">The font with which to draw.</param>
/// <param name="brush">The brush with which to draw.</param>
/// <param name="pos">The position at which to draw.</param>
/// <param name="size">The size to which to limit the drawn text; or Vector.Zero for no limit.</param>
/// <param name="format">The string format to use.</param>
/// <remarks>
/// PDFsharp cannot currently render multi-line text to PDF files; it comes out as single line.
/// This method simulates standard Graphics.DrawString() over PDFsharp.
/// It always has the effect of StringFormatFlags.LineLimit (which PDFsharp does not support).
/// </remarks>
public void Draw(XGraphics graphics, Font font, Brush brush, Vector pos, Vector size, XStringFormat format)
{
// do a quick test to see if text is going to get drawn at the same size as last time;
// if so, assume we don't need to recompute our layout for that reason.
var sizeChecker = graphics.MeasureString("M q", font);
if (sizeChecker != m_sizeChecker || pos != m_pos || m_size != size || m_requestedFormat.Alignment != format.Alignment || m_requestedFormat.LineAlignment != format.LineAlignment || m_requestedFormat.FormatFlags != format.FormatFlags)
{
m_invalidLayout = true;
}
m_sizeChecker = sizeChecker;
if (m_invalidLayout)
{
// something vital has changed; rebuild our cached layout data
RebuildCachedLayout(graphics, font, ref pos, ref size, format);
m_invalidLayout = false;
}
var state = graphics.Save();
if (size != Vector.Zero)
{
graphics.IntersectClip(new RectangleF(pos.X, pos.Y, size.X, size.Y));
}
// disable smoothing whilst rendering text;
// visually this is no different, but is faster
var smoothingMode = graphics.SmoothingMode;
graphics.SmoothingMode = XSmoothingMode.HighSpeed;
var origin = m_origin;
for (var index=0; index<m_lines.Count; ++index)
{
if (size.Y > 0 && size.Y < m_lineHeight)
break; // not enough remaining vertical space for a whole line
var line = m_lines[index];
graphics.DrawString(line, font, brush, origin.X, origin.Y, m_actualFormat);
origin += m_delta;
size.Y -= m_lineHeight;
}
graphics.SmoothingMode = smoothingMode;
graphics.Restore(state);
}
private void RebuildCachedLayout(XGraphics graphics, Font font, ref Vector pos, ref Vector size, XStringFormat baseFormat)
{
// for diagnostic purposes
++s_rebuildCount;
// store current settings to help us tell if we need a rebuild next time around
m_requestedFormat = new XStringFormat();
m_requestedFormat.Alignment = baseFormat.Alignment;
m_requestedFormat.FormatFlags = baseFormat.FormatFlags;
m_requestedFormat.LineAlignment = baseFormat.LineAlignment;
m_actualFormat = new XStringFormat();
m_actualFormat.Alignment = baseFormat.Alignment;
m_actualFormat.FormatFlags = baseFormat.FormatFlags;
m_actualFormat.LineAlignment = baseFormat.LineAlignment;
m_pos = pos;
m_size = size;
var text = m_text;
if (text.IndexOf('\n') == -1 && size.X > 0 && size.Y > 0 && graphics.MeasureString(text, font).Width > size.X)
{
// wrap single-line text to fit in rectangle
// measure a space, countering the APIs unwillingness to measure spaces
var spaceLength = (float)(graphics.MeasureString("M M", font).Width - graphics.MeasureString("M", font).Width * 2);
var words = new List<Word>();
foreach (var word in text.Split(' '))
{
if (words.Count != 0)
{
words.Add(new Word(" ", spaceLength));
}
words.Add(new Word(word, (float)graphics.MeasureString(word, font).Width));
}
var lineLength = 0.0f;
var total = string.Empty;
var line = string.Empty;
foreach (var word in words)
{
if (word.Text != " " && word.Length > Math.Max(0, size.X - lineLength) && lineLength > 0)
{
if (line.Length > 0)
{
if (total.Length > 0)
{
total += "\n";
}
total += line;
lineLength = word.Length + spaceLength;
line = word.Text;
}
}
else
{
line += word.Text;
lineLength += word.Length + spaceLength;
}
}
if (line.Length > 0)
{
if (total.Length > 0)
{
total += "\n";
}
total += line;
}
text = total;
}
m_lineHeight = font.GetHeight();
m_lines.Clear();
m_lines.AddRange(text.Split('\n'));
switch (m_actualFormat.LineAlignment)
{
case XLineAlignment.Near:
default:
m_origin = pos;
m_delta = new Vector(0, m_lineHeight);
break;
case XLineAlignment.Far:
m_origin = new Vector(pos.X, pos.Y + size.Y - m_lineHeight);
if (size.Y > 0)
{
var count = m_lines.Count;
while (m_origin.Y - m_lineHeight >= pos.Y && --count > 0)
{
m_origin.Y -= m_lineHeight;
}
}
else
{
m_origin.Y -= (m_lines.Count - 1) * m_lineHeight;
}
m_delta = new Vector(0, m_lineHeight);
break;
case XLineAlignment.Center:
m_origin = new Vector(pos.X, pos.Y + size.Y / 2 - (m_lines.Count - 1) * m_lineHeight / 2 - m_lineHeight / 2);
m_delta = new Vector(0, m_lineHeight);
break;
}
m_actualFormat.LineAlignment = XLineAlignment.Near;
switch (m_actualFormat.Alignment)
{
case XStringAlignment.Far:
m_origin.X = pos.X + size.X;
break;
case XStringAlignment.Center:
m_origin.X = pos.X + size.X / 2;
break;
}
}
public static int RebuildCount
{
get { return s_rebuildCount; }
}
private struct Word
{
public Word(string text, float length)
{
Text = text;
Length = length;
}
public string Text;
public float Length;
}
private string m_text = string.Empty;
// cached layout data to speed drawing
private bool m_invalidLayout = true;
private XSize m_sizeChecker;
private Vector m_pos;
private Vector m_size;
private Vector m_origin;
private Vector m_delta;
private float m_lineHeight;
private XStringFormat m_requestedFormat;
private XStringFormat m_actualFormat;
private List<string> m_lines = new List<string>();
public static int s_rebuildCount = 0;
}
}