示例#1
0
		// Calculate and draw the string by drawing each line.
		public void Draw
					(Graphics graphics, String text, Font font,
					 Rectangle drawLayout, StringFormat format, Brush brush)
				{
					// set the current graphics
					this.graphics = graphics;

					// set the current toolkit graphics
					this.toolkitGraphics = graphics.ToolkitGraphics;

					// set the current text
					this.text = text;

					// set the current font
					this.font = font;

					// set the current layout rectangle
					this.layout = drawLayout;

					// set the current brush
					this.brush = brush;

					// ensure we have a string format
					if(format == null) { format = SF_DEFAULT; }

					// set the current string format
					this.format = format;

					// set the default hotkey index
					this.hotkeyIndex = -1;

					// set the current line height
					lineHeight = font.Height;

					// set the only whole lines flag
					onlyWholeLines = (((format.FormatFlags &
					                    StringFormatFlags.LineLimit) != 0) ||
					                  ((format.Trimming &
					                    StringTrimming.None) != 0));

					// set the index of the next character
					nextIndex = 0;

					// set the current line space usage
					lineSpaceUsedUp = 0;

					// set the current line number
					lineNumber = 0;

					// set the previous span ended in new line flag
					prevIsNewLine = false;

					// select the current font into the graphics context
					graphics.SelectFont(font);

					// select the current brush into the graphics context
					graphics.SelectBrush(brush);

					// set the current text start
					int textStart = 0;

					// set the current text length
					int textLength = 0;

					// set the current text width
					int textWidth = 0;

					// get the actual hotkey index, if needed
					if(format.HotkeyPrefix != HotkeyPrefix.None)
					{
						// get the hotkey index
						hotkeyIndex = text.IndexOf('&');

						// handle the hotkey as needed
						if(hotkeyIndex != -1)
						{
							if(hotkeyIndex >= (text.Length - 1) ||
							   Char.IsControl(text[hotkeyIndex + 1]))
							{
								// no need for this anymore
								hotkeyIndex = -1;
							}
							else
							{
								// remove the hotkey character
								text = text.Substring(0, hotkeyIndex) +
								       text.Substring(hotkeyIndex + 1);

								// set the current text
								this.text = text;

								// prepare to show or hide the underline
								if(format.HotkeyPrefix == HotkeyPrefix.Show)
								{
									// get the underline font
									underlineFont = new Font
										(font, font.Style | FontStyle.Underline);
								}
								else
								{
									// no need for this anymore
									hotkeyIndex = -1;
								}
							}
						}
					}

					// draw the text
					try
					{
						// handle drawing based on line alignment
						if(format.LineAlignment == StringAlignment.Near)
						{
							// set the current y position
							int y = layout.Top;

							// get the maximum y position
							int maxY = layout.Bottom;

							// adjust for whole lines, if needed
							if(onlyWholeLines)
							{
								maxY -= ((maxY - y) % lineHeight);
							}

							// get the last line y position
							int lastLineY = maxY - lineHeight;

							// create character spans
							CharSpan span = new CharSpan();
							CharSpan prev = new CharSpan();

							// set the first span flag
							bool firstSpan = true;

							// process the text
							while(nextIndex < text.Length)
							{
								// get the next span of characters
								GetNextSpan(span);

								// draw the pending line, as needed
								if(span.newline && !firstSpan)
								{
									// draw the line, if needed
									if(textWidth > 0)
									{
										// remove trailing spaces, if needed
										if(!firstSpan && text[prev.start] == ' ')
										{
											// update text width
											textWidth -= GetSpanWidth(prev);

											// update text length
											textLength -= prev.length;
										}

										// draw the line
										DrawLine
											(textStart, textLength, textWidth,
											 y, (y > lastLineY));
									}

									// update the y position
									y += lineHeight;

									// update the line number
									++lineNumber;

									// update the text start
									textStart = span.start;

									// reset the text length
									textLength = 0;

									// reset the text width
									textWidth = 0;
								}

								// update the text length
								textLength += span.length;

								// update the text width
								textWidth += GetSpanWidth(span);

								// copy span values to previous span
								span.CopyTo(prev);

								// set the first span flag
								firstSpan = false;

								// break if the y position is out of bounds
								if(y > maxY) { break; }
							}

							// draw the last line, if needed
							if(textWidth > 0 && y <= maxY)
							{
								// draw the last line
								DrawLine
									(textStart, textLength, textWidth, y,
									 (y > lastLineY));
							}
						}
						else
						{
							// set default lines to draw
							int linesToDraw = 0;

							// calculate lines to draw
							if(onlyWholeLines)
							{
								linesToDraw = layout.Height / lineHeight;
							} 
							else
							{
								linesToDraw = (int)Math.Ceiling((double)layout.Height / lineHeight);
							}

							// create line span list
							LineSpan[] lines = new LineSpan[linesToDraw];

							// create character spans
							CharSpan span = new CharSpan();
							CharSpan prev = new CharSpan();

							// set the first span flag
							bool firstSpan = true;

							// set the current line position
							int linePos = 0;

							// populate line span list
							while(linePos < lines.Length &&
							      nextIndex < text.Length)
							{
								// get the next span of characters
								GetNextSpan(span);

								// handle span on new line
								if(span.newline && !firstSpan)
								{
									// remove trailing spaces, if needed
									if(!firstSpan && text[prev.start] == ' ')
									{
										// update text width
										textWidth -= GetSpanWidth(prev);

										// update text length
										textLength -= prev.length;
									}

									// create line span for current line
									LineSpan lineSpan = new LineSpan
										(textStart, textLength, textWidth);

									// add current line span to line span list
									lines[linePos++] = lineSpan;

									// update text start
									textStart = span.start;

									// update text length
									textLength = 0;

									// update text width
									textWidth = 0;
								}

								// update text length
								textLength += span.length;

								// update text width
								textWidth += GetSpanWidth(span);

								// copy span values to previous span
								span.CopyTo(prev);

								// set the first span flag
								firstSpan = false;
							}

							// add the last line to the line span list
							if(linePos < lines.Length)
							{
								// create line span for last line
								LineSpan lineSpan = new LineSpan
									(textStart, textLength, textWidth);

								// add last line span to the line span list
								lines[linePos++] = lineSpan;
							}

							// calculate the top line y
							int y = (layout.Height - (linePos * lineHeight));

							// adjust y for center alignment, if needed
							if(format.LineAlignment == StringAlignment.Center)
							{
								y /= 2;
							}

							// translate y to layout rectangle
							y += layout.Top;

							// adjust line position to last line
							--linePos;

							// draw the lines
							for(int i = 0; i < linePos; ++i)
							{
								// get the current line
								LineSpan line = lines[i];

								// draw the current line
								DrawLine
									(line.start, line.length, line.pixelWidth,
									 y, false);

								// update the y position
								y += lineHeight;
							}

							// draw the last line
							DrawLine
								(lines[linePos].start, lines[linePos].length,
								 lines[linePos].pixelWidth, y, true);
						}
					}
					finally
					{
						// dispose the underline font, if we have one
						if(underlineFont != null)
						{
							// dispose the underline font
							underlineFont.Dispose();

							// reset the underline font
							underlineFont = null;
						}
					}
				}
示例#2
0
		// Calculate text metrics information.
		//
		// Note that this is currently broken. Turn this on at your own risk.
		public Size GetBounds
					(Graphics graphics, String text, Font font,
					 SizeF layoutSize, StringFormat format,
					 out int charactersFitted, out int linesFilled)
				{
					// set the current graphics
					this.graphics = graphics;

					// set the current toolkit graphics
					this.toolkitGraphics = graphics.ToolkitGraphics;

					// set the current text
					this.text = text;

					// set the current font
					this.font = font;

					// ensure we have a string format
					if(format == null) { format = SF_DEFAULT; }

					// set the current string format
					this.format = format;

					// set the current layout rectangle
					this.layout = new Rectangle
						(0, 0, (int)layoutSize.Width, (int)layoutSize.Height);

					// set the current line height
					lineHeight = font.Height;

					// set the only whole lines flag
					onlyWholeLines = (((format.FormatFlags &
					                    StringFormatFlags.LineLimit) != 0) ||
					                  ((format.Trimming &
					                    StringTrimming.None) != 0));

					// set the index of the next character
					nextIndex = 0;

					// set the current line space usage
					lineSpaceUsedUp = 0;

					// set the previous span ended in new line flag
					prevIsNewLine = false;

					// select the current font into the graphics context
					graphics.SelectFont(font);

					// set the text width
					int textWidth = 0;

					// set the maximum width
					int maxWidth = 0;

					// set the default characters fitted
					charactersFitted = 0;

					// set the default lines filled
					linesFilled = 0;

					// remove the hotkey prefix, if needed
					if(format.HotkeyPrefix != HotkeyPrefix.None)
					{
						// get the hotkey index
						hotkeyIndex = text.IndexOf('&');

						// handle the hotkey as needed
						if(hotkeyIndex != -1)
						{
							if(hotkeyIndex < (text.Length - 1) &&
							   !Char.IsControl(text[hotkeyIndex + 1]))
							{
								// remove the hotkey character
								text = text.Substring(0, hotkeyIndex) +
								       text.Substring(hotkeyIndex + 1);

								// set the current text
								this.text = text;

								// update characters fitted
								++charactersFitted;
							}

							// no need for this anymore
							hotkeyIndex = -1;
						}
					}

					// create character spans
					CharSpan span = new CharSpan();
					CharSpan prev = new CharSpan();

					// set the first span flag
					bool firstSpan = true;

					// set the measure trailing spaces flag
					bool mts = ((format.FormatFlags &
					             StringFormatFlags.MeasureTrailingSpaces) != 0);

					// process the text
					while(nextIndex < text.Length)
					{
						// get the next span of characters
						GetNextSpan(span);

						// handle span on new line
						if(span.newline)
						{
							// remove trailing spaces, if needed
							if(!firstSpan && !mts && text[prev.start] == ' ')
							{
								// update the text width
								textWidth -= GetSpanWidth(prev);
							}

							// update the maximum width, if needed
							if(textWidth > maxWidth) { maxWidth = textWidth; }

							// update the text width
							textWidth = 0;

							// update the lines filled
							++linesFilled;
						}

						// update the text width
						textWidth += GetSpanWidth(span);

						// update the characters fitted
						charactersFitted += span.length;

						// copy span values to previous span
						span.CopyTo(prev);
					}

					// update the maximum width, if needed
					if(textWidth > maxWidth) { maxWidth = textWidth; }

					// update the lines filled to account for the first line
					++linesFilled;

					// update the maximum width, if needed
					if(maxWidth > layout.Width) { maxWidth = layout.Width; }

					// calculate the height
					int height = (lineHeight * linesFilled);

					// update the height, if needed
					if(height > layout.Height) { height = layout.Height; }

					// return the size of the text
					return new Size(maxWidth, height);
				}
示例#3
0
		// Measures one full line. Updates the positions of the characters in that line relative to 0,0
		private void MeasureLine
					(ref Rectangle[] bounds, ref int currentPos,
					 ref string text, float maxX, Graphics g, Font f,
					 bool vertical, bool noWrap)
				{
					// set the initial position
					int initialPos = currentPos;

					// set the default x position
					int x = (int)(f.SizeInPoints*g.DpiX/369.7184);

					// set the default y position
					int y = 0;

					// ??
					do
					{
						// get the current character
						char c = text[currentPos];

						// check for the end of the line
						if(c == '\n')
						{
							// move past the end of the line
							currentPos++;

							// we're done
							return;
						}

						// Ignore returns
						if(c != '\r')
						{
							//TODO use Platform specific measure function & take into account kerning

							// get the size of the current character
							g.SelectFont(f);
							int charactersFitted, linesFilled;
							Size s = g.ToolkitGraphics.MeasureString(c.ToString(), null, null, out charactersFitted, out linesFilled, false);
							s.Height = f.Height;

							// ??
							int newX = x;

							// ??
							int newY = y;

							// ??
							if(vertical)
							{
								newX += s.Height;
							}
							else
							{
								newX += s.Width;
							}

							// ??
							if(newX > maxX)
							{
								// ??
								if(noWrap) { return; }

								// Backtrack to wrap the word

								// ??
								for(int i = currentPos; i > initialPos; i--)
								{
									// ??
									if(text[i] == ' ')
									{
										// Swallow the space

										// ??
										bounds[i++] = Rectangle.Empty;

										// ??
										currentPos = i;

										// ??
										return;
									}
								}

								// ??
								return;
							}
							else
							{
								// ??
								if(vertical)
								{
									bounds[currentPos] = new Rectangle
										(y, x, s.Height, s.Width - 1);
								}
								else
								{
									bounds[currentPos] = new Rectangle
										(x, y, s.Width, s.Height - 1);
								}
							}

							// ??
							x = newX;
						}

						// ??
						currentPos++;
					}
					while (currentPos < text.Length);
				}