Exemplo n.º 1
0
		/// <summary>
		/// Initializes a new SDUFormattedString object with given string and
		/// initial CharacterFormat.<br/>
		/// Format string definition is at documentation for 
		/// <see cref="CharacterFormat.Formatted"/> member.
		/// </summary>
		/// <param name="str"></param>
		/// <param name="initFmt"></param>
		public SDUFormattedString(string str, CharacterFormat initFmt)
		{
			if(sdu==null)
				sdu=StringDrawUtils.GetInstance();

			m_strings=new ArrayList();
			m_formats=new ArrayList();
			m_softbreaks=new Hashtable();
			m_initialFormat=initFmt;

			Debug.Assert(initFmt.Angle==0);

			CharacterFormat curfmt=initFmt.ShallowCopy();
			curfmt.Angle=0;
			curfmt.Formatted=false;

			if(!initFmt.Formatted)
			{
				AddFormattedPiece(str,curfmt);
			} 
			else
			{
				Stack brushes=new Stack();

				string curstr="";
				for (int i = 0; i < str.Length; i++)
				{
					if(str[i]!='#') 
						curstr+=str[i];
					else if(str[i+1]=='#') 
					{
						curstr+='#';
						i++;
					} 
					else 
					{ 
						if(curstr.Length>0)
						{
							AddFormattedPiece(curstr,curfmt);
							curstr="";
							curfmt=curfmt.ShallowCopy();
						}
						switch(str[i+1])
						{
							case 'C':
								if(str.Length > i + 2 && str[i+2]=='-')
								{
									if(!initFmt.IgnoreColorFormatting)
									{
										if(brushes.Count==0)
											throw new ArgumentException("Invalid format string","str");

										curfmt.Brush=(Brush) brushes.Pop();
									}
									i+=2;
								} 
								else if(str.Length > i + 7)
								{
									string colstr=str.Substring(i+2,6);
									if(colstr.Length<6)
										throw new ArgumentException("Invalid format string","str");
									int colint;
									try
									{
										colint=Int32.Parse(colstr,NumberStyles.HexNumber);
									} 
									catch(FormatException)
									{
										throw new ArgumentException("Invalid format string","str");
									}
									Color clr=Color.FromArgb( (255<<24) + colint );

									if(!initFmt.IgnoreColorFormatting) 
									{
										brushes.Push(curfmt.Brush);
										curfmt.Brush=new SolidBrush(clr);
									}
									i+=7;
								}
								break;
							case 'B':
							switch(str[i+2])
							{
								case '+':
									curfmt.Font=new Font(curfmt.Font,curfmt.Font.Style|FontStyle.Bold);
									break;
								case '-':
									curfmt.Font=new Font(curfmt.Font,curfmt.Font.Style & ~FontStyle.Bold);
									break;
								default:
									throw new ArgumentException("Invalid format string","str");
							}
								i+=2;
								break;
							case 'I':
							switch(str[i+2])
							{
								case '+':
									curfmt.Font=new Font(curfmt.Font,curfmt.Font.Style|FontStyle.Italic);
									break;
								case '-':
									curfmt.Font=new Font(curfmt.Font,curfmt.Font.Style & ~FontStyle.Italic);
									break;
								default:
									throw new ArgumentException("Invalid format string","str");
							}
								i+=2;
								break;
							case 'U':
							switch(str[i+2])
							{
								case '+':
									curfmt.Font=new Font(curfmt.Font,curfmt.Font.Style|FontStyle.Underline);
									break;
								case '-':
									curfmt.Font=new Font(curfmt.Font,curfmt.Font.Style & ~FontStyle.Underline);
									break;
								default:
									throw new ArgumentException("Invalid format string","str");
							}
								i+=2;
								break;
							default:
								throw new ArgumentException("Invalid format string","str");
						}
					}
				
				}
				if(curstr.Length>0)
					AddFormattedPiece(curstr,curfmt);
			}

		}
Exemplo n.º 2
0
		/// <summary>
		/// Returns instance of StringDrawUtils object.
		/// </summary>
		/// <returns></returns>
		/// <remarks>As StringDrawUtils is a singleton, always the same instance is returned.</remarks>
		public static StringDrawUtils GetInstance()
		{
			if (instance == null)
				instance = new StringDrawUtils();
			return instance;
		}