public TextPieceTable(byte[] documentStream, byte[] tableStream, int offset, int size, int fcMin) { // get our plex of PieceDescriptors PlexOfCps pieceTable = new PlexOfCps(tableStream, offset, size, PieceDescriptor .SizeInBytes); int length = pieceTable.Length; PieceDescriptor[] pieces = new PieceDescriptor[length]; // iterate through piece descriptors raw bytes and create // PieceDescriptor objects for (int x = 0; x < length; x++) { GenericPropertyNode node = pieceTable.GetProperty(x); pieces[x] = new PieceDescriptor(node.Bytes, 0); } // Figure out the cp of the earliest text piece // Note that text pieces don't have to be stored in order! _cpMin = pieces[0].FilePosition - fcMin; for (int x = 0; x < pieces.Length; x++) { int start = pieces[x].FilePosition - fcMin; if (start < _cpMin) { _cpMin = start; } } // using the PieceDescriptors, build our list of TextPieces. for (int x = 0; x < pieces.Length; x++) { int start = pieces[x].FilePosition; PropertyNode node = pieceTable.GetProperty(x); // Grab the start and end, which are in characters int nodeStartChars = node.Start; int nodeEndChars = node.End; // What's the relationship between bytes and characters? bool unicode = pieces[x].IsUnicode; int multiple = 1; if (unicode) { multiple = 2; } // Figure out the Length, in bytes and chars int textSizeChars = (nodeEndChars - nodeStartChars); int textSizeBytes = textSizeChars * multiple; // Grab the data that Makes up the piece byte[] buf = new byte[textSizeBytes]; Array.Copy(documentStream, start, buf, 0, textSizeBytes); // And now build the piece _textPieces.Add(new TextPiece(nodeStartChars, nodeEndChars, buf, pieces[x], node .Start)); } // In the interest of our sanity, now sort the text pieces // into order, if they're not already _textPieces.Sort(); _textPiecesFCOrder = new List<TextPiece>(_textPieces); _textPiecesFCOrder.Sort(new FCComparator()); }