public Region [] MeasureCharacterRanges (string text, Font font, RectangleF layoutRect, StringFormat stringFormat) {
			if (stringFormat == null)
				throw new ArgumentException("stringFormat");

			CharacterRange[] ranges = stringFormat.CharRanges;
			if (ranges == null || ranges.Length == 0)
				return new Region[0];

			GraphicsPath[] pathes = new GraphicsPath[ranges.Length];
			for (int i = 0; i < pathes.Length; i++)
				pathes[i] = new GraphicsPath();

			TextLineIterator iter = new TextLineIterator(text, font, NativeObject.getFontRenderContext(),
				stringFormat, layoutRect.Width, layoutRect.Height);
			
			for (LineLayout layout = iter.NextLine(); layout != null; layout = iter.NextLine()) {

				for (int i = 0; i < ranges.Length; i++) {
					int start = ranges[i].First;
					int length = ranges[i].Length;
					start -= iter.CharsConsumed;
					int limit = start + length;
					int layoutStart = iter.CurrentPosition - layout.CharacterCount;
					if (start < iter.CurrentPosition && limit > layoutStart) {

						float layoutOffset;
						if (start > layoutStart)
							layoutOffset = iter.GetAdvanceBetween(layoutStart, start);
						else {
							layoutOffset = 0;
							start = layoutStart;
						}

						float width = (limit < iter.CurrentPosition) ?
							iter.GetAdvanceBetween(start, limit) :
							layout.Width - layoutOffset;

						float height = layout.Ascent + layout.Descent;

						float x = layout.NativeX;
						float y = layout.NativeY;

						if (stringFormat.IsVertical) {
							y += layoutOffset;
							x -= layout.Descent;
						}
						else {
							x += layoutOffset;
							y -= layout.Ascent;
						}

						if (layout.AccumulatedHeight + height > iter.WrapHeight) {
							float diff = iter.WrapHeight - layout.AccumulatedHeight;
							if (stringFormat.IsVertical && stringFormat.IsRightToLeft) {
								x += diff;
								height -= diff;
							}
							else
								height = diff;
						}

						if (stringFormat.IsVertical)
							pathes[i].AddRectangle(x + layoutRect.X, y + layoutRect.Y, height, width);
						else
							pathes[i].AddRectangle(x + layoutRect.X, y + layoutRect.Y, width, height);
					}
				}
			}

			geom.AffineTransform lineAlignT = iter.CalcLineAlignmentTransform();
			if (lineAlignT != null) {
				for (int i = 0; i < pathes.Length; i++)
					pathes[i].NativeObject.transform(lineAlignT);
			}

			Region[] regions = new Region[ranges.Length];
			for (int i = 0; i < regions.Length; i++)
				regions[i] = new Region(pathes[i]);

			return regions;
		}
示例#2
0
		void AddString (string s, Font font,
			float x, float y, float width, float height, 
			StringFormat format) {

			TextLineIterator iter = new TextLineIterator(s, font,
				new java.awt.font.FontRenderContext(null, false, false),
				format, width, height);

			int coordsCount = NativeObject.CoordsCount;

			for (LineLayout layout = iter.NextLine(); layout != null; layout = iter.NextLine()) {
				NativeObject.append(layout.GetOutline(x, y), false);
			}

			AffineTransform lineAlignT = iter.CalcLineAlignmentTransform();
			if (lineAlignT != null)
				NativeObject.transform(lineAlignT, coordsCount, NativeObject.CoordsCount - coordsCount);
		}