-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElementHelper.cs
More file actions
221 lines (190 loc) · 9.95 KB
/
Copy pathElementHelper.cs
File metadata and controls
221 lines (190 loc) · 9.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
using System;
using Microsoft.Office.Interop.PowerPoint;
using Office = Microsoft.Office.Core;
namespace decksterity
{
// Centralized utility class for inserting Unicode symbols into PowerPoint slides
// Handles context-aware insertion with font preservation and color support
public static class ElementHelper
{
// Main insertion method that handles all PowerPoint selection contexts
// Supports optional RGB color for colored symbols (stoplights)
public static void InsertElement(string element, int? colorRgb = null)
{
var app = (Application)System.Runtime.InteropServices.Marshal.GetActiveObject("PowerPoint.Application");
var selection = app.ActiveWindow.Selection;
// Context 1: Table cell selection - insert into table with formatting preservation
if (selection.Type == PpSelectionType.ppSelectionShapes)
{
Shape tableShape = null;
foreach (Shape shape in selection.ShapeRange)
{
if (shape.HasTable == Office.MsoTriState.msoTrue)
{
tableShape = shape;
break;
}
}
if (tableShape != null)
{
InsertElementIntoTable(element, tableShape, selection, colorRgb);
return;
}
}
// Context 2: Text selection - insert at cursor position with font preservation
if (selection.Type == PpSelectionType.ppSelectionText)
{
InsertElementIntoText(element, selection, colorRgb);
return;
}
else if (selection.Type == PpSelectionType.ppSelectionShapes)
{
// Context 3: Shape selection - insert into shape text frame
foreach (Shape shape in selection.ShapeRange)
{
if (shape.HasTextFrame == Office.MsoTriState.msoTrue)
{
InsertElementIntoShapeText(element, shape, colorRgb);
return;
}
}
}
// Context 4: Fallback - create new centered textbox on slide
InsertElementIntoSlide(element, app, colorRgb);
}
#region Context-Specific Insertion Methods
// Inserts element into table cell with dual font formatting system
// Preserves original font for continued typing via zero-width spacer
private static void InsertElementIntoTable(string element, Shape tableShape, Selection selection, int? colorRgb = null)
{
// Active text cursor in table cell - use advanced font preservation
if (selection.Type == PpSelectionType.ppSelectionText && selection.TextRange != null)
{
// Step 1: Record original formatting before making changes
var originalFontName = selection.TextRange.Font.Name;
var originalFontColor = selection.TextRange.Font.Color.RGB;
// Step 2: Insert symbol + zero-width space for cursor positioning
var textRange = selection.TextRange;
textRange.Text = element + "\u200B"; // Zero-width space
// Step 3: Calculate the length of the element (handles surrogate pairs)
int elementLength = element.Length;
// Step 4: Apply symbol formatting to the element part only
var symbolRange = textRange.Characters(1, elementLength);
symbolRange.Font.Name = "Segoe UI Symbol";
if (colorRgb.HasValue)
{
int bgrColor = ConvertRgbToBgr(colorRgb.Value);
symbolRange.Font.Color.RGB = bgrColor;
}
// Step 5: Apply original formatting to the zero-width space for future typing
var spacerRange = textRange.Characters(elementLength + 1, 1);
spacerRange.Font.Name = originalFontName;
spacerRange.Font.Color.RGB = originalFontColor;
// Step 6: Position cursor after the symbol but in the formatted spacer
spacerRange.Select();
}
else
{
// No active cursor - fallback to first cell replacement
var cell = tableShape.Table.Cell(1, 1);
var textRange = cell.Shape.TextFrame.TextRange;
// Simple replacement without cursor positioning
textRange.Text = element;
textRange.Font.Name = "Segoe UI Symbol";
if (colorRgb.HasValue)
{
int bgrColor = ConvertRgbToBgr(colorRgb.Value);
textRange.Font.Color.RGB = bgrColor;
}
}
}
// Inserts element into active text selection with font preservation
// Uses zero-width spacer technique for seamless cursor positioning
private static void InsertElementIntoText(string element, Selection selection, int? colorRgb = null)
{
// Record original formatting before making changes
var originalFontName = selection.TextRange.Font.Name;
var originalFontColor = selection.TextRange.Font.Color.RGB;
// Insert symbol + zero-width space for cursor positioning
var textRange = selection.TextRange;
textRange.Text = element + "\u200B"; // Zero-width space
// Calculate the length of the element (handles surrogate pairs)
int elementLength = element.Length;
// Apply symbol formatting to the element part only
var symbolRange = textRange.Characters(1, elementLength);
symbolRange.Font.Name = "Segoe UI Symbol";
if (colorRgb.HasValue)
{
int bgrColor = ConvertRgbToBgr(colorRgb.Value);
symbolRange.Font.Color.RGB = bgrColor;
}
// Apply original formatting to the zero-width space for future typing
var spacerRange = textRange.Characters(elementLength + 1, 1);
spacerRange.Font.Name = originalFontName;
spacerRange.Font.Color.RGB = originalFontColor;
// Position cursor after the symbol but in the formatted spacer
spacerRange.Select();
}
// Inserts element into shape text frame replacing existing content
// Uses dual formatting but without cursor positioning (shape context)
private static void InsertElementIntoShapeText(string element, Shape shape, int? colorRgb = null)
{
// Record original formatting before making changes
var textRange = shape.TextFrame.TextRange;
var originalFontName = textRange.Font.Name;
var originalFontColor = textRange.Font.Color.RGB;
// Insert symbol + zero-width space for cursor positioning
textRange.Text = element + "\u200B"; // Zero-width space
// Calculate the length of the element (handles surrogate pairs)
int elementLength = element.Length;
// Apply symbol formatting to the element part only
var symbolRange = textRange.Characters(1, elementLength);
symbolRange.Font.Name = "Segoe UI Symbol";
if (colorRgb.HasValue)
{
int bgrColor = ConvertRgbToBgr(colorRgb.Value);
symbolRange.Font.Color.RGB = bgrColor;
}
// Apply original formatting to the zero-width space for future typing
var spacerRange = textRange.Characters(elementLength + 1, 1);
spacerRange.Font.Name = originalFontName;
spacerRange.Font.Color.RGB = originalFontColor;
}
// Creates new centered textbox on slide with symbol
// Fallback method when no suitable selection context is found
private static void InsertElementIntoSlide(string element, Application app, int? colorRgb = null)
{
// Insert as new shape in the center of the active slide
var slide = app.ActiveWindow.View.Slide;
float left = app.ActivePresentation.PageSetup.SlideWidth / 2 - 20;
float top = app.ActivePresentation.PageSetup.SlideHeight / 2 - 20;
var shape = slide.Shapes.AddTextbox(Office.MsoTextOrientation.msoTextOrientationHorizontal, left, top, 40, 40);
// Configure text properties
shape.TextFrame.TextRange.Text = element;
shape.TextFrame.TextRange.Font.Name = "Segoe UI Symbol";
shape.TextFrame.TextRange.Font.Size = 16;
// Apply color if specified
if (colorRgb.HasValue)
{
// Convert RGB to BGR format for PowerPoint
int bgrColor = ConvertRgbToBgr(colorRgb.Value);
shape.TextFrame.TextRange.Font.Color.RGB = bgrColor;
}
// Center and middle align the text
shape.TextFrame.TextRange.ParagraphFormat.Alignment = PpParagraphAlignment.ppAlignCenter;
shape.TextFrame.VerticalAnchor = Office.MsoVerticalAnchor.msoAnchorMiddle;
}
// Converts RGB color format to BGR format required by PowerPoint COM API
// PowerPoint internally uses BGR instead of standard RGB format
private static int ConvertRgbToBgr(int rgbColor)
{
// Step 1: Extract RGB components from input color
int r = (rgbColor >> 16) & 0xFF;
int g = (rgbColor >> 8) & 0xFF;
int b = rgbColor & 0xFF;
// Step 2: Recombine as BGR for PowerPoint compatibility
return (b << 16) | (g << 8) | r;
}
#endregion
}
}