99 lines
6.4 KiB
C#
99 lines
6.4 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Runtime.InteropServices;
|
|
using PlotDirector.Companion.Protocol.Manuscript;
|
|
using Word = Microsoft.Office.Interop.Word;
|
|
|
|
namespace PlotDirector.WordAddIn
|
|
{
|
|
public sealed partial class WordAdapter
|
|
{
|
|
// All COM access stays on the Word STA. One shape or text segment is
|
|
// measured per scheduler turn; no document, layout or selection is changed.
|
|
private void ReadChapterPictureAnchor(LocalDocument doc, ChapterRead read)
|
|
{
|
|
Word.Range paragraph = null, anchor = null, unit = null;
|
|
Word.ShapeRange shapes = null; Word.Shape shape = null; Word.TextRetrievalMode mode = null;
|
|
try
|
|
{
|
|
paragraph = Com(() => doc.Document.Range(read.PendingStart, read.PendingEnd));
|
|
shapes = Com(() => paragraph.ShapeRange, "chapter-picture-anchors");
|
|
var count = Com(() => shapes.Count);
|
|
if (count > 256) throw new InvalidOperationException("More than 256 floating shapes in one paragraph; review this structural range before retrying.");
|
|
if (read.ShapeIndex > count)
|
|
{
|
|
if (read.PendingAnchors.Count != read.PendingPosition.WordLength - read.PendingPosition.Length)
|
|
throw new InvalidOperationException("Unaccounted Word positions in paragraph at " + read.PendingStart + ": span " + read.PendingPosition.WordLength + ", text units " + read.PendingPosition.Length + ", verified picture anchors " + read.PendingAnchors.Count + ". Other structural content requires review.");
|
|
read.PendingPosition.Anchors = read.PendingAnchors.OrderBy(p => p.Key).Select(p => p.Value).ToArray();
|
|
read.SegmentPosition = read.PendingStart; read.SegmentAnchor = 0; read.SegmentText.Clear(); read.Phase = 10;
|
|
return;
|
|
}
|
|
object index = read.ShapeIndex++;
|
|
shape = Com(() => shapes.get_Item(ref index));
|
|
anchor = Com(() => shape.Anchor);
|
|
if (Com(() => anchor.StoryType) != Word.WdStoryType.wdMainTextStory) return;
|
|
var at = Com(() => anchor.Start);
|
|
if (at < read.PendingStart || at >= read.PendingEnd) return;
|
|
unit = Com(() => doc.Document.Range(at, at + 1));
|
|
if ((Com(() => unit.Text) ?? "").Length != 0) return;
|
|
// Empty must not mean hidden manuscript text or hidden field codes.
|
|
mode = Com(() => unit.TextRetrievalMode);
|
|
Com(() => { mode.IncludeHiddenText = true; mode.IncludeFieldCodes = true; return true; });
|
|
if ((Com(() => unit.Text) ?? "").Length != 0)
|
|
throw new InvalidOperationException("Hidden text/field content at Word position " + at + " cannot certify an empty picture anchor.");
|
|
var type = (int)Com(() => shape.Type);
|
|
if (type != 11 && type != 13)
|
|
throw new InvalidOperationException("Unsupported floating shape type " + type + " at Word position " + at + "; only measured picture anchors can account for empty positions.");
|
|
if (!read.PendingAnchors.ContainsKey(at - read.PendingStart))
|
|
read.PendingAnchors.Add(at - read.PendingStart, new ChapterPictureAnchor { Offset = at - read.PendingStart, ShapeId = Com(() => shape.ID), ShapeType = type });
|
|
}
|
|
catch (COMException error)
|
|
{
|
|
throw new InvalidOperationException("Cannot measure native floating picture anchors in paragraph at " + read.PendingStart + " (COM 0x" + error.HResult.ToString("X8") + "). Existing mappings are retained.", error);
|
|
}
|
|
finally { ReleaseChapter(mode); ReleaseChapter(unit); ReleaseChapter(anchor); ReleaseChapter(shape); ReleaseChapter(shapes); ReleaseChapter(paragraph); }
|
|
}
|
|
|
|
private void ReadChapterPositionSegment(LocalDocument doc, ChapterRead read)
|
|
{
|
|
var anchors = read.PendingPosition.Anchors;
|
|
var end = read.SegmentAnchor < anchors.Length ? read.PendingStart + anchors[read.SegmentAnchor].Offset : read.PendingEnd;
|
|
Word.Range segment = null;
|
|
try
|
|
{
|
|
if (end > read.SegmentPosition)
|
|
{
|
|
segment = Com(() => doc.Document.Range(read.SegmentPosition, end));
|
|
var text = Com(() => segment.Text, "chapter-position-segment") ?? "";
|
|
if (text.Length != end - read.SegmentPosition)
|
|
throw new InvalidOperationException("Unaccounted structural position between " + read.SegmentPosition + " and " + end + "; native picture evidence does not explain this text span.");
|
|
read.SegmentText.Append(text); read.SegmentPosition = end;
|
|
}
|
|
if (read.SegmentAnchor < anchors.Length)
|
|
{
|
|
// Skip only the exact one-position range independently proven
|
|
// empty and anchored to a genuine native floating picture.
|
|
read.SegmentPosition++; read.SegmentAnchor++; return;
|
|
}
|
|
if (read.SegmentText.ToString() != read.PendingText)
|
|
throw new InvalidOperationException("Measured native text segments do not equal the paragraph text; no position was inferred.");
|
|
CompleteChapterNativeParagraph(read, read.PendingPosition, read.PendingText, read.PendingEnd);
|
|
}
|
|
finally { ReleaseChapter(segment); }
|
|
}
|
|
|
|
private void CompleteChapterNativeParagraph(ChapterRead read, ChapterParagraph evidence, string text, int end)
|
|
{
|
|
read.NativePositions.Add(evidence); read.NativeText.Append(text);
|
|
read.Identities.Append(evidence.NativeId).Append(evidence.Length.ToString("X8", System.Globalization.CultureInfo.InvariantCulture));
|
|
read.IdentityPosition = end; read.Phase = 7;
|
|
if (end != read.End) return;
|
|
if (read.NativeText.ToString() != read.Text)
|
|
throw new InvalidOperationException("Native paragraph text does not equal the complete chapter range; retry after saving Word.");
|
|
var rows = read.NativePositions.ToArray();
|
|
ChapterWordPositions.Validate(rows, read.Start, read.End, ChapterWordPositions.Hash(rows));
|
|
read.IdentityFrames = ChapterWordPositions.Encode(rows); read.Phase = 4;
|
|
}
|
|
}
|
|
}
|