1 | ///
|
---|
2 | /// This file is part of ILNumerics Community Edition.
|
---|
3 | ///
|
---|
4 | /// ILNumerics Community Edition - high performance computing for applications.
|
---|
5 | /// Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net
|
---|
6 | ///
|
---|
7 | /// ILNumerics Community Edition is free software: you can redistribute it and/or modify
|
---|
8 | /// it under the terms of the GNU General Public License version 3 as published by
|
---|
9 | /// the Free Software Foundation.
|
---|
10 | ///
|
---|
11 | /// ILNumerics Community Edition is distributed in the hope that it will be useful,
|
---|
12 | /// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | /// GNU General Public License for more details.
|
---|
15 | ///
|
---|
16 | /// You should have received a copy of the GNU General Public License
|
---|
17 | /// along with ILNumerics Community Edition. See the file License.txt in the root
|
---|
18 | /// of your distribution package. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | ///
|
---|
20 | /// In addition this software uses the following components and/or licenses:
|
---|
21 | ///
|
---|
22 | /// =================================================================================
|
---|
23 | /// The Open Toolkit Library License
|
---|
24 | ///
|
---|
25 | /// Copyright (c) 2006 - 2009 the Open Toolkit library.
|
---|
26 | ///
|
---|
27 | /// Permission is hereby granted, free of charge, to any person obtaining a copy
|
---|
28 | /// of this software and associated documentation files (the "Software"), to deal
|
---|
29 | /// in the Software without restriction, including without limitation the rights to
|
---|
30 | /// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
---|
31 | /// the Software, and to permit persons to whom the Software is furnished to do
|
---|
32 | /// so, subject to the following conditions:
|
---|
33 | ///
|
---|
34 | /// The above copyright notice and this permission notice shall be included in all
|
---|
35 | /// copies or substantial portions of the Software.
|
---|
36 | ///
|
---|
37 | /// =================================================================================
|
---|
38 | ///
|
---|
39 |
|
---|
40 | using System;
|
---|
41 | using System.Text;
|
---|
42 | using System.Drawing;
|
---|
43 | using System.Collections.Generic;
|
---|
44 | using ILNumerics.Drawing;
|
---|
45 | using ILNumerics.Drawing.Interfaces;
|
---|
46 |
|
---|
47 | namespace ILNumerics.Drawing.Labeling {
|
---|
48 | /// <summary>
|
---|
49 | /// Transforms characters into bitmaps (1:1)
|
---|
50 | /// </summary>
|
---|
51 | /// <remarks>this is the base class for most IILTextInterpreter implementations</remarks>
|
---|
52 | public class ILSimpleInterpreter : IILTextInterpreter {
|
---|
53 |
|
---|
54 | #region attributes
|
---|
55 | static Graphics m_measGraphics;
|
---|
56 | static Bitmap m_measBitmap;
|
---|
57 | protected Font m_normalFont;
|
---|
58 | protected Size m_size;
|
---|
59 | #endregion
|
---|
60 |
|
---|
61 | #region constructor
|
---|
62 |
|
---|
63 | /// <summary>
|
---|
64 | /// create a new instance of this text interpreter
|
---|
65 | /// </summary>
|
---|
66 | public ILSimpleInterpreter () : base () {
|
---|
67 | resizeMeasureBMP(20,20);
|
---|
68 | }
|
---|
69 |
|
---|
70 | #endregion
|
---|
71 |
|
---|
72 | #region helper functions
|
---|
73 |
|
---|
74 | protected void resizeMeasureBMP(int width, int height) {
|
---|
75 | if (m_measBitmap != null)
|
---|
76 | m_measBitmap.Dispose();
|
---|
77 | if (m_measGraphics != null)
|
---|
78 | m_measGraphics.Dispose();
|
---|
79 | m_measBitmap = new Bitmap(width+1,height+1);
|
---|
80 | m_measGraphics = Graphics.FromImage(m_measBitmap);
|
---|
81 | }
|
---|
82 |
|
---|
83 | protected virtual void parseString (string expression, Font font, Point offset, Color color,
|
---|
84 | IILTextRenderer renderer, ref Size size,
|
---|
85 | ref List<ILRenderQueueItem> queue) {
|
---|
86 | int pos = 0;
|
---|
87 | string key, itemText;
|
---|
88 | RectangleF bmpSize = new RectangleF();
|
---|
89 | int curHeigth = 0, curWidth = 0;
|
---|
90 | Bitmap itemBMP = null;
|
---|
91 | int lineHeight = 0, lineWidth = 0;
|
---|
92 | Size itemSize = Size.Empty;
|
---|
93 | while (pos < expression.Length) {
|
---|
94 | itemText = expression.Substring(pos++,1);
|
---|
95 | key = ILHashCreator.Hash(font,itemText);
|
---|
96 |
|
---|
97 | if (renderer.TryGetSize(key, ref itemSize)) {
|
---|
98 | queue.Add(new ILRenderQueueItem(key,offset,color));
|
---|
99 | if (itemSize.Height > lineHeight) lineHeight = itemSize.Height;
|
---|
100 | lineWidth += (int)itemSize.Width;
|
---|
101 | } else {
|
---|
102 | lock (this) {
|
---|
103 | itemBMP = transformItem(itemText,font,out bmpSize);
|
---|
104 | renderer.Cache(key,itemBMP,bmpSize);
|
---|
105 | queue.Add(new ILRenderQueueItem(key,offset,color));
|
---|
106 | // update size
|
---|
107 | if (bmpSize.Height > lineHeight)
|
---|
108 | lineHeight = (int)bmpSize.Height;
|
---|
109 | lineWidth += (int)bmpSize.Width;
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|
113 | size.Width += ((curWidth>lineWidth)?curWidth:lineWidth);
|
---|
114 | size.Height = curHeigth + lineHeight;
|
---|
115 | }
|
---|
116 |
|
---|
117 | /// <summary>
|
---|
118 | /// Render a string onto a bitmap and measure exact size
|
---|
119 | /// </summary>
|
---|
120 | /// <param name="item">item to be rendered</param>
|
---|
121 | /// <param name="font">font used for rendering</param>
|
---|
122 | /// <param name="size">[output] size of the rendered item</param>
|
---|
123 | /// <returns>bitmap containing the item</returns>
|
---|
124 | public Bitmap TransformItem (string item, Font font, out RectangleF size) {
|
---|
125 | Bitmap ret = transformItem(item,font,out size);
|
---|
126 | return ret.Clone(Rectangle.Round(size),ret.PixelFormat);
|
---|
127 | }
|
---|
128 |
|
---|
129 | /// <summary>
|
---|
130 | /// Render a string onto a bitmap and measure exact size
|
---|
131 | /// </summary>
|
---|
132 | /// <param name="item">item to be rendered</param>
|
---|
133 | /// <param name="font">font used for rendering</param>
|
---|
134 | /// <param name="size">[output] size of the rendered item</param>
|
---|
135 | /// <returns>bitmap containing the item</returns>
|
---|
136 | protected Bitmap transformItem(string item, Font font, out RectangleF size) {
|
---|
137 | if (String.IsNullOrEmpty(item)) {
|
---|
138 | size = new RectangleF(0,0,1,1);
|
---|
139 | return m_measBitmap;
|
---|
140 | }
|
---|
141 | if (item == " ") {
|
---|
142 | size = new RectangleF (new PointF(0,0),m_measGraphics.MeasureString(item,font));
|
---|
143 | if (size.Right >= m_measBitmap.Width || size.Bottom >= m_measBitmap.Height) {
|
---|
144 | resizeMeasureBMP((int)size.Right+15,(int)size.Bottom+15);
|
---|
145 | return transformItem(item,font,out size);
|
---|
146 | }
|
---|
147 | m_measGraphics.Clear(Color.Transparent);
|
---|
148 | return m_measBitmap;
|
---|
149 | }
|
---|
150 | StringFormat sformat = StringFormat.GenericDefault;
|
---|
151 | sformat.FormatFlags = StringFormatFlags.NoWrap
|
---|
152 | | StringFormatFlags.NoClip
|
---|
153 | | StringFormatFlags.MeasureTrailingSpaces;
|
---|
154 | sformat.SetMeasurableCharacterRanges(new CharacterRange[]{new CharacterRange(0,item.Length)});
|
---|
155 |
|
---|
156 | // draw the text
|
---|
157 | if (font.SizeInPoints < 16) {
|
---|
158 | m_measGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
|
---|
159 | } else {
|
---|
160 | m_measGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
|
---|
161 | }
|
---|
162 | RectangleF layout = new RectangleF(0,0,m_measBitmap.Width,m_measBitmap.Height);
|
---|
163 | m_measGraphics.Clear(Color.Transparent);
|
---|
164 | m_measGraphics.DrawString(item,font,Brushes.White,layout,sformat);
|
---|
165 | // measure bounds
|
---|
166 | Region[] reg = m_measGraphics.MeasureCharacterRanges(item,font,layout,sformat);
|
---|
167 | size = reg[0].GetBounds(m_measGraphics);
|
---|
168 | size.Width += 1;
|
---|
169 | size.Height += 1;
|
---|
170 | // compensate
|
---|
171 | //size = new RectangleF((size.Left>0)?size.Left-1:size.Left,size.Top,size.Width,size.Height);
|
---|
172 | #if EXPORTBMP // debug support only
|
---|
173 | if (true) {
|
---|
174 | Bitmap debBitmap = (Bitmap)m_measBitmap.Clone();
|
---|
175 | Graphics debGrap = Graphics.FromImage(debBitmap);
|
---|
176 | //debGrap.Clear(Color.White);
|
---|
177 | //debGrap.DrawString(item,font,Brushes.Black,layout,sformat);
|
---|
178 | debGrap.DrawRectangle(new Pen(Brushes.Red),Rectangle.Round(size));
|
---|
179 | debBitmap.Save("EXPORTBMP_transformItemResult.bmp",System.Drawing.Imaging.ImageFormat.Bmp);
|
---|
180 | }
|
---|
181 | #endif
|
---|
182 | if (size.Right >= m_measBitmap.Width || size.Bottom >= m_measBitmap.Height) {
|
---|
183 | resizeMeasureBMP((int)size.Right+15,(int)size.Bottom+15);
|
---|
184 | return transformItem(item,font,out size);
|
---|
185 | }
|
---|
186 | return m_measBitmap;
|
---|
187 | }
|
---|
188 |
|
---|
189 | #endregion
|
---|
190 |
|
---|
191 | #region IILTextInterpreter Member
|
---|
192 |
|
---|
193 | /// <summary>
|
---|
194 | /// Transforms an expression into render queue definition
|
---|
195 | /// </summary>
|
---|
196 | /// <param name="expression">expression to be transformed</param>
|
---|
197 | /// <param name="font">font may used for transformation</param>
|
---|
198 | /// <param name="color">standard color used for transformation</param>
|
---|
199 | /// <param name="renderer">IILTextRenderer instance used for caching (and later rendering)</param>
|
---|
200 | /// <returns>render queue, later used to render the visual representation of the expression</returns>
|
---|
201 | /// <remarks>the expression may contain markups. See the online help at http://ilnumerics.net
|
---|
202 | /// for a detailed descriptioin of known symbols and their syntax.</remarks>
|
---|
203 | public ILRenderQueue Transform(string expression, Font font, Color color,
|
---|
204 | IILTextRenderer renderer) {
|
---|
205 | List<ILRenderQueueItem> ret = new List<ILRenderQueueItem>();
|
---|
206 | Size size = Size.Empty;
|
---|
207 | m_normalFont = font;
|
---|
208 | parseString(expression,font,new Point(),color,renderer,ref size,ref ret);
|
---|
209 | return new ILRenderQueue(expression, ret, size);
|
---|
210 | }
|
---|
211 | /// <summary>
|
---|
212 | /// Render text expression into bitmap
|
---|
213 | /// </summary>
|
---|
214 | /// <param name="expression">text expression</param>
|
---|
215 | /// <param name="font">font used for rendering</param>
|
---|
216 | /// <returns>Bitmap with rendered expression</returns>
|
---|
217 | /// <remarks>The size of the bitmap returned will tightly fit around the rendered content.</remarks>
|
---|
218 | public virtual Bitmap Transform(string expression, Font font) {
|
---|
219 | RectangleF size;
|
---|
220 | Bitmap ret = transformItem(expression, font, out size);
|
---|
221 | return ret.Clone(size,ret.PixelFormat);
|
---|
222 | }
|
---|
223 | /// <summary>
|
---|
224 | /// Render text expression into bitmap
|
---|
225 | /// </summary>
|
---|
226 | /// <param name="expression">text expression</param>
|
---|
227 | /// <returns>Bitmap with rendered expression</returns>
|
---|
228 | /// <remarks>The size of the bitmap returned will tightly fit around the rendered content.
|
---|
229 | /// <para>A generic sans serif font of size 10em will be used. </para></remarks>
|
---|
230 | public virtual Bitmap Transform(string expression) {
|
---|
231 | Font font = new Font(FontFamily.GenericSansSerif,10f,FontStyle.Regular);
|
---|
232 | RectangleF size;
|
---|
233 | Bitmap ret = transformItem(expression, font, out size);
|
---|
234 | return ret.Clone(size,ret.PixelFormat);
|
---|
235 | }
|
---|
236 | #endregion
|
---|
237 | }
|
---|
238 | }
|
---|