|
|
777717 |
/*
|
|
|
777717 |
......... 2015 Ivan Mahonin
|
|
|
777717 |
|
|
|
777717 |
This program is free software: you can redistribute it and/or modify
|
|
|
777717 |
it under the terms of the GNU General Public License as published by
|
|
|
777717 |
the Free Software Foundation, either version 3 of the License, or
|
|
|
777717 |
(at your option) any later version.
|
|
|
777717 |
|
|
|
777717 |
This program is distributed in the hope that it will be useful,
|
|
|
777717 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
777717 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
777717 |
GNU General Public License for more details.
|
|
|
777717 |
|
|
|
777717 |
You should have received a copy of the GNU General Public License
|
|
|
777717 |
along with this program. If not, see <http: licenses="" www.gnu.org="">.</http:>
|
|
|
777717 |
*/
|
|
|
777717 |
|
|
|
777717 |
using System;
|
|
|
8cb222 |
using System.Collections.Generic;
|
|
|
8cb222 |
using System.Data.Linq;
|
|
|
8cb222 |
using System.Drawing;
|
|
|
8cb222 |
|
|
|
8cb222 |
namespace Diagram {
|
|
|
8cb222 |
public class TextUtils {
|
|
|
8cb222 |
static Bitmap tmpBitmap = new Bitmap(10, 10);
|
|
|
8cb222 |
static Graphics tmpGraphics = Graphics.FromImage(tmpBitmap);
|
|
|
8cb222 |
|
|
|
8cb222 |
public static string wrap(string text, double width, Font font) {
|
|
|
8cb222 |
string wrapped = "";
|
|
|
8cb222 |
string line = "";
|
|
|
8cb222 |
int wordStart = 0;
|
|
|
8cb222 |
for(int i = 0; i <= text.Length; ++i) {
|
|
|
8cb222 |
char c = i < text.Length ? text[i] : ' ';
|
|
|
8cb222 |
if (char.IsWhiteSpace(c)) {
|
|
|
8cb222 |
bool newLine = c == '\n';
|
|
|
8cb222 |
string word = text.Substring(wordStart, i - wordStart);
|
|
|
8cb222 |
if (word != "") {
|
|
|
8cb222 |
if (line == "") {
|
|
|
8cb222 |
line = word;
|
|
|
8cb222 |
word = "";
|
|
|
8cb222 |
} else
|
|
|
8cb222 |
if (tmpGraphics.MeasureString(line, font).Width <= width) {
|
|
|
8cb222 |
line += " " + word;
|
|
|
8cb222 |
word = "";
|
|
|
8cb222 |
} else {
|
|
|
8cb222 |
newLine = true;
|
|
|
8cb222 |
}
|
|
|
8cb222 |
}
|
|
|
8cb222 |
if (newLine) {
|
|
|
8cb222 |
if (wrapped != "") wrapped += "\r\n";
|
|
|
8cb222 |
wrapped += line;
|
|
|
8cb222 |
line = word;
|
|
|
8cb222 |
}
|
|
|
8cb222 |
wordStart = i + 1;
|
|
|
8cb222 |
}
|
|
|
8cb222 |
}
|
|
|
8cb222 |
|
|
|
8cb222 |
if (line != "") {
|
|
|
8cb222 |
if (wrapped != "") wrapped += "\r\n";
|
|
|
8cb222 |
wrapped += line;
|
|
|
8cb222 |
}
|
|
|
8cb222 |
return wrapped;
|
|
|
8cb222 |
}
|
|
|
8cb222 |
|
|
|
8cb222 |
public static SizeF measure(string text, Font font) {
|
|
|
8cb222 |
return tmpGraphics.MeasureString(text, font);
|
|
|
8cb222 |
}
|
|
|
8cb222 |
}
|
|
|
8cb222 |
}
|
|
|
8cb222 |
|