/// /// This file is part of ILNumerics Community Edition. /// /// ILNumerics Community Edition - high performance computing for applications. /// Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net /// /// ILNumerics Community Edition is free software: you can redistribute it and/or modify /// it under the terms of the GNU General Public License version 3 as published by /// the Free Software Foundation. /// /// ILNumerics Community Edition is distributed in the hope that it will be useful, /// but WITHOUT ANY WARRANTY; without even the implied warranty of /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /// GNU General Public License for more details. /// /// You should have received a copy of the GNU General Public License /// along with ILNumerics Community Edition. See the file License.txt in the root /// of your distribution package. If not, see . /// /// In addition this software uses the following components and/or licenses: /// /// ================================================================================= /// The Open Toolkit Library License /// /// Copyright (c) 2006 - 2009 the Open Toolkit library. /// /// 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. /// /// ================================================================================= /// using System; using System.Drawing; using System.Collections.Generic; using System.Text; using ILNumerics.Drawing.Interfaces; using ILNumerics.Drawing; namespace ILNumerics.Drawing.Labeling { /// /// Base class for text elements /// public abstract class ILLabelingElement { #region event handler /// /// fires when an element's property has changed /// public event EventHandler Changed; protected virtual void OnChanged() { if (Changed != null) Changed(this,null); } #endregion #region attributes protected String m_expression; protected String m_cachedExpression; protected Color m_color; protected Font m_font; protected Size m_size; internal PointF m_anchor; protected TextOrientation m_orientation; protected IILTextInterpreter m_interpreter; protected IILTextRenderer m_renderer; protected ILRenderQueue m_renderQueue; #endregion #region properties /// /// Size of the label for rendering, regardless of orientation /// public virtual Size Size { get { if (m_cachedExpression != m_expression) { interprete(m_expression); } return m_size; } } /// /// color used to display the element /// public virtual Color Color { get { return m_color; } set { m_color = value; OnChanged(); } } /// /// Get/set system font used to draw the text /// public virtual Font Font { get { return m_font; } set { if (m_font != null && (m_font.Name != value.Name || m_font.Style != value.Style || m_font.Size != value.Size)) { m_font = value; interprete(m_expression); OnChanged(); } } } /// /// Get the orientation for the labeling element or sets it /// public virtual TextOrientation Orientation { get { return m_orientation; } set { m_orientation = value; OnChanged(); } } /// /// Get/set the relative offset of the 'Position' point rel. to the overall label size, range 0..1 for X and Y /// public PointF Anchor { get { return m_anchor; } set { m_anchor = value; OnChanged(); } } /// /// Text interpreter used to transform labeling source into bitmap /// public IILTextInterpreter Interpreter { get { return m_interpreter; } set { if (value != null) { m_interpreter = value; OnChanged(); } } } /// /// device dependent renderer, used to draw the labeling bitmap onto panel /// public IILTextRenderer Renderer { get { return m_renderer; } set { if (value != null) { m_renderer = value; OnChanged(); } } } /// /// Get / set the text expression defining the content to be displayed /// /// The expression may contain markups according to /// the specific interpreter instance. public string Text { get { return m_expression; } set { if (value != m_expression) { m_expression = value; // (clean up, interpret & cache: when drawing) OnChanged(); } } } #endregion #region constructor /// /// [abstract] Create a new labeling element /// /// panel hosting the element /// default font for the element /// default color for the element public ILLabelingElement(ILPanel panel, Font font, Color color) : this(panel,font,color,CoordSystem.Screen) { } /// /// [abstract] Create a new labeling element /// /// panel hosting the element /// default font for the element /// default color for the element /// world / screen rendering method public ILLabelingElement (ILPanel panel, Font font, Color color, CoordSystem coordSystem) { if (font != null) { m_font = font; } else { m_font = new System.Drawing.Font( System.Drawing.FontFamily.GenericSansSerif,10.0f); } m_color = color; m_orientation = TextOrientation.Horizontal; m_renderQueue = null; m_size = Size.Empty; m_expression = string.Empty; // init interpreter & renderer m_renderer = panel.TextRendererManager.GetDefault(coordSystem); m_renderer.CacheCleared += new EventHandler(m_renderer_CacheCleared); m_interpreter = new ILSimpleTexInterpreter(); } #endregion #region public methods /// /// Dispose off this element's ressources /// internal void Dispose() { if (m_renderer != null) { m_renderer.CacheCleared -= m_renderer_CacheCleared; if (m_renderer is IDisposable) { (m_renderer as IDisposable).Dispose(); } m_renderer = null; } } /// /// draws the whole rendering queue /// public virtual void Draw(ILRenderProperties p) { if (m_expression != m_cachedExpression) interprete(m_expression); m_renderer.Begin(p); m_renderer.Draw(m_renderQueue,offsetAlignment(m_renderQueue.Size),m_orientation,m_color); m_renderer.End(p); } #endregion #region helper methods /// /// if the renderer cleares its cache, the expression needs to be re-interpreted /// /// /// protected void m_renderer_CacheCleared(object sender, EventArgs e) { m_cachedExpression = ""; } /// /// add offset to an existing point according to internal alignment /// /// size of content to be aligned /// original point protected void offsetAlignment(Size size, ref Point point) { if (m_orientation == TextOrientation.Vertical) { point.X = point.X + (int)(m_anchor.Y * size.Height); point.Y = point.Y - (int)(m_anchor.X * size.Width); //if (0 != (TickLabelAlign.right & m_alignment)) { // // point.X = point.X; //} else if (0 != (TickLabelAlign.center & m_alignment)) { // point.X = point.X - size.Height / 2; //} else // point.X = point.X + size.Height; //if (0 != (TickLabelAlign.bottom & m_alignment)) { // point.Y = point.Y - size.Width; //} else if (0 != (TickLabelAlign.vertCenter & m_alignment)) { // point.Y = point.Y - size.Width / 2; //} //else point.Y = point.Y; } else { point.X = point.X - (int)(m_anchor.X * size.Width); point.Y = point.Y - (int)(m_anchor.Y * size.Height); //if (0 != (TickLabelAlign.right & m_alignment)) { // point.X = point.X - size.Width; //} else if (0 != (TickLabelAlign.center & m_alignment)) { // point.X = point.X - size.Width / 2; //} // else point.X = point.X; //if (0 != (TickLabelAlign.bottom & m_alignment)) { // point.Y = point.Y - size.Height; //} else if (0 != (TickLabelAlign.vertCenter & m_alignment)) { // point.Y = point.Y - size.Height / 2; //} // else point.Y = point.Y; } } /// /// return offset according to current setting of m_alignment /// /// size of an element to align /// offset according to Alignment property protected Point offsetAlignment(Size size) { Point ret = new Point(); offsetAlignment(size, ref ret); return ret; } /// /// return offset according to current setting of m_alignment /// /// size of an element to align /// point inside rectangle of size 'size' /// offset according to Alignment property protected Point offsetAlignment(Size size, Point p) { offsetAlignment(size, ref p); return p; } /// /// interprete the expression and cache render queue /// /// protected void interprete(string expression) { if (m_renderQueue != null) m_renderQueue.Clear(); m_renderQueue = m_interpreter.Transform(expression,m_font,m_color,m_renderer); m_size = m_renderQueue.Size; m_cachedExpression = expression; } #endregion } }