/// <summary>Finds the index of the nearest character to x pixels.</summary> /// <param name="x">The number of pixels from the left edge of this text element.</param> /// <returns>The index of the nearest letter.</returns> public int LetterIndex(int x) { if (ChildNodes == null) { return(0); } int widthSoFar = 0; int lettersSoFar = 0; for (int i = 0; i < ChildNodes.Count; i++) { WordElement word = (WordElement)ChildNodes[i]; // Grab the words computed style: Css.ComputedStyle wordStyle = word.Style.Computed; // Bump up the current width: widthSoFar += wordStyle.PixelWidth; if (x <= widthSoFar) { int localWidthOffset = x - (widthSoFar - wordStyle.PixelWidth); lettersSoFar += wordStyle.Text.LetterIndex(localWidthOffset); break; } else { lettersSoFar += word.LetterCount(); } } return(lettersSoFar); }
/// <summary>Gets the word containing the letter at the given index.</summary> /// <param name="index">The index of the letter to find in this text element.</param> /// <param name="localOffset">The index of the letter in the word.</param> /// <returns>The nearest word element found.</returns> public WordElement GetWordWithLetter(int index, out int localOffset) { localOffset = 0; if (ChildNodes == null) { return(null); } int lettersSoFar = 0; for (int i = 0; i < ChildNodes.Count; i++) { WordElement word = (WordElement)ChildNodes[i]; int letterCount = word.LetterCount(); lettersSoFar += letterCount; if (index <= lettersSoFar) { localOffset = index - (lettersSoFar - letterCount); return(word); } } localOffset = lettersSoFar; return(null); }
/// <summary>Finds the index of the nearest character to x pixels.</summary> /// <param name="x">The number of pixels from the left edge of this text element.</param> /// <param name="y">The number of pixels from the top edge of this text element.</param> /// <returns>The index of the nearest letter.</returns> public int LetterIndex(int x, int y) { if (ChildNodes == null) { return(0); } int widthSoFar = 0; int lettersSoFar = 0; for (int i = 0; i < ChildNodes.Count; i++) { WordElement word = (WordElement)ChildNodes[i]; // Grab the words computed style: Css.ComputedStyle wordStyle = word.Style.Computed; // Find where the bottom of the line is at: int lineBottom = wordStyle.PixelHeight + wordStyle.ParentOffsetTop; if (lineBottom < y) { // Not at the right line yet. lettersSoFar += word.LetterCount(); continue; } // Crank over the width: widthSoFar += wordStyle.PixelWidth; if (x <= widthSoFar) { int localWidthOffset = x - (widthSoFar - wordStyle.PixelWidth); lettersSoFar += wordStyle.Text.LetterIndex(localWidthOffset); break; } else { lettersSoFar += word.LetterCount(); } } return(lettersSoFar); }