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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
|
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hslf.usermodel;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.ddf.EscherContainerRecord;
import org.apache.poi.ddf.EscherDgRecord;
import org.apache.poi.ddf.EscherDggRecord;
import org.apache.poi.ddf.EscherRecordTypes;
import org.apache.poi.ddf.EscherSpRecord;
import org.apache.poi.hslf.exceptions.HSLFException;
import org.apache.poi.hslf.model.HeadersFooters;
import org.apache.poi.hslf.record.CString;
import org.apache.poi.hslf.record.ColorSchemeAtom;
import org.apache.poi.hslf.record.Comment2000;
import org.apache.poi.hslf.record.EscherTextboxWrapper;
import org.apache.poi.hslf.record.HeadersFootersContainer;
import org.apache.poi.hslf.record.RecordContainer;
import org.apache.poi.hslf.record.RecordTypes;
import org.apache.poi.hslf.record.SSSlideInfoAtom;
import org.apache.poi.hslf.record.SlideAtom;
import org.apache.poi.hslf.record.SlideAtomLayout.SlideLayoutType;
import org.apache.poi.hslf.record.SlideListWithText.SlideAtomsSet;
import org.apache.poi.hslf.record.StyleTextProp9Atom;
import org.apache.poi.hslf.record.TextHeaderAtom;
import org.apache.poi.sl.draw.DrawFactory;
import org.apache.poi.sl.draw.Drawable;
import org.apache.poi.sl.usermodel.Notes;
import org.apache.poi.sl.usermodel.Placeholder;
import org.apache.poi.sl.usermodel.ShapeType;
import org.apache.poi.sl.usermodel.SimpleShape;
import org.apache.poi.sl.usermodel.Slide;
import org.apache.poi.sl.usermodel.TextShape.TextPlaceholder;
/**
* This class represents a slide in a PowerPoint Document. It allows
* access to the text within, and the layout. For now, it only does
* the text side of things though
*/
public final class HSLFSlide extends HSLFSheet implements Slide<HSLFShape,HSLFTextParagraph> {
private int _slideNo;
private SlideAtomsSet _atomSet;
private final List<List<HSLFTextParagraph>> _paragraphs = new ArrayList<>();
private HSLFNotes _notes; // usermodel needs to set this
/**
* Constructs a Slide from the Slide record, and the SlideAtomsSet
* containing the text.
* Initializes TextRuns, to provide easier access to the text
*
* @param slide the Slide record we're based on
* @param notes the Notes sheet attached to us
* @param atomSet the SlideAtomsSet to get the text from
*/
public HSLFSlide(org.apache.poi.hslf.record.Slide slide, HSLFNotes notes, SlideAtomsSet atomSet, int slideIdentifier, int slideNumber) {
super(slide, slideIdentifier);
_notes = notes;
_atomSet = atomSet;
_slideNo = slideNumber;
// For the text coming in from the SlideAtomsSet:
// Build up TextRuns from pairs of TextHeaderAtom and
// one of TextBytesAtom or TextCharsAtom
if (_atomSet != null && _atomSet.getSlideRecords().length > 0) {
// Grab text from SlideListWithTexts entries
_paragraphs.addAll(HSLFTextParagraph.findTextParagraphs(_atomSet.getSlideRecords()));
if (_paragraphs.isEmpty()) {
throw new HSLFException("No text records found for slide");
}
}
// Grab text from slide's PPDrawing
for (List<HSLFTextParagraph> l : HSLFTextParagraph.findTextParagraphs(getPPDrawing(), this)) {
if (!_paragraphs.contains(l)) {
_paragraphs.add(l);
}
}
}
/**
* Create a new Slide instance
* @param sheetNumber The internal number of the sheet, as used by PersistPtrHolder
* @param slideNumber The user facing number of the sheet
*/
public HSLFSlide(int sheetNumber, int sheetRefId, int slideNumber){
super(new org.apache.poi.hslf.record.Slide(), sheetNumber);
_slideNo = slideNumber;
getSheetContainer().setSheetId(sheetRefId);
}
/**
* Returns the Notes Sheet for this slide, or null if there isn't one
*/
@Override
public HSLFNotes getNotes() {
return _notes;
}
/**
* Sets the Notes that are associated with this. Updates the
* references in the records to point to the new ID
*/
@Override
public void setNotes(Notes<HSLFShape,HSLFTextParagraph> notes) {
if (notes != null && !(notes instanceof HSLFNotes)) {
throw new IllegalArgumentException("notes needs to be of type HSLFNotes");
}
_notes = (HSLFNotes)notes;
// Update the Slide Atom's ID of where to point to
SlideAtom sa = getSlideRecord().getSlideAtom();
if(_notes == null) {
// Set to 0
sa.setNotesID(0);
} else {
// Set to the value from the notes' sheet id
sa.setNotesID(_notes._getSheetNumber());
}
}
/**
* Changes the Slide's (external facing) page number.
* @see org.apache.poi.hslf.usermodel.HSLFSlideShow#reorderSlide(int, int)
*/
public void setSlideNumber(int newSlideNumber) {
_slideNo = newSlideNumber;
}
/**
* Called by SlideShow after a new slide is created.
* <p>
* For Slide we need to do the following:
* <ul>
* <li> set id of the drawing group.</li>
* <li> set shapeId for the container descriptor and background</li>
* </ul>
*/
@Override
public void onCreate(){
//initialize drawing group id
EscherDggRecord dgg = getSlideShow().getDocumentRecord().getPPDrawingGroup().getEscherDggRecord();
EscherContainerRecord dgContainer = getSheetContainer().getPPDrawing().getDgContainer();
EscherDgRecord dg = HSLFShape.getEscherChild(dgContainer, EscherDgRecord.RECORD_ID);
int dgId = dgg.getMaxDrawingGroupId() + 1;
dg.setOptions((short)(dgId << 4));
dgg.setDrawingsSaved(dgg.getDrawingsSaved() + 1);
for (EscherContainerRecord c : dgContainer.getChildContainers()) {
EscherSpRecord spr = null;
switch(EscherRecordTypes.forTypeID(c.getRecordId())){
case SPGR_CONTAINER:
EscherContainerRecord dc = (EscherContainerRecord)c.getChild(0);
spr = dc.getChildById(EscherSpRecord.RECORD_ID);
break;
case SP_CONTAINER:
spr = c.getChildById(EscherSpRecord.RECORD_ID);
break;
default:
break;
}
if(spr != null) {
spr.setShapeId(allocateShapeId());
}
}
//PPT doesn't increment the number of saved shapes for group descriptor and background
dg.setNumShapes(1);
}
/**
* Create a {@code TextBox} object that represents the slide's title.
*
* @return {@code TextBox} object that represents the slide's title.
*/
public HSLFTextBox addTitle() {
HSLFPlaceholder pl = new HSLFPlaceholder();
pl.setShapeType(ShapeType.RECT);
pl.setPlaceholder(Placeholder.TITLE);
pl.setRunType(TextPlaceholder.TITLE.nativeId);
pl.setText("Click to edit title");
pl.setAnchor(new java.awt.Rectangle(54, 48, 612, 90));
addShape(pl);
return pl;
}
// Complex Accessor methods follow
/**
* <p>
* The title is a run of text of type {@code TextHeaderAtom.CENTER_TITLE_TYPE} or
* {@code TextHeaderAtom.TITLE_TYPE}
* </p>
*
* @see TextHeaderAtom
*/
@Override
public String getTitle(){
for (List<HSLFTextParagraph> tp : getTextParagraphs()) {
if (tp.isEmpty()) {
continue;
}
int type = tp.get(0).getRunType();
if (TextPlaceholder.isTitle(type)) {
String str = HSLFTextParagraph.getRawText(tp);
return HSLFTextParagraph.toExternalString(str, type);
}
}
return null;
}
@Override
public String getSlideName() {
final CString name = (CString)getSlideRecord().findFirstOfType(RecordTypes.CString.typeID);
return name != null ? name.getText() : "Slide"+getSlideNumber();
}
/**
* Returns an array of all the TextRuns found
*/
@Override
public List<List<HSLFTextParagraph>> getTextParagraphs() { return _paragraphs; }
/**
* Returns the (public facing) page number of this slide
*/
@Override
public int getSlideNumber() { return _slideNo; }
/**
* Returns the underlying slide record
*/
public org.apache.poi.hslf.record.Slide getSlideRecord() {
return (org.apache.poi.hslf.record.Slide)getSheetContainer();
}
/**
* @return set of records inside {@code SlideListWithtext} container
* which hold text data for this slide (typically for placeholders).
*/
public SlideAtomsSet getSlideAtomsSet() { return _atomSet; }
/**
* Returns master sheet associated with this slide.
* It can be either SlideMaster or TitleMaster objects.
*
* @return the master sheet associated with this slide.
*/
@Override
public HSLFMasterSheet getMasterSheet(){
int masterId = getSlideRecord().getSlideAtom().getMasterID();
for (HSLFSlideMaster sm : getSlideShow().getSlideMasters()) {
if (masterId == sm._getSheetNumber()) {
return sm;
}
}
for (HSLFTitleMaster tm : getSlideShow().getTitleMasters()) {
if (masterId == tm._getSheetNumber()) {
return tm;
}
}
return null;
}
/**
* Change Master of this slide.
*/
public void setMasterSheet(HSLFMasterSheet master){
SlideAtom sa = getSlideRecord().getSlideAtom();
int sheetNo = master._getSheetNumber();
sa.setMasterID(sheetNo);
}
/**
* Sets whether this slide follows master background
*
* @param flag {@code true} if the slide follows master,
* {@code false} otherwise
*/
@Override
public void setFollowMasterBackground(boolean flag){
SlideAtom sa = getSlideRecord().getSlideAtom();
sa.setFollowMasterBackground(flag);
}
/**
* Whether this slide follows master sheet background
*
* @return {@code true} if the slide follows master background,
* {@code false} otherwise
*/
@Override
public boolean getFollowMasterBackground(){
SlideAtom sa = getSlideRecord().getSlideAtom();
return sa.getFollowMasterBackground();
}
/**
* Sets whether this slide draws master sheet objects
*
* @param flag {@code true} if the slide draws master sheet objects,
* {@code false} otherwise
*/
@Override
public void setFollowMasterObjects(boolean flag){
SlideAtom sa = getSlideRecord().getSlideAtom();
sa.setFollowMasterObjects(flag);
}
/**
* Whether this slide follows master color scheme
*
* @return {@code true} if the slide follows master color scheme,
* {@code false} otherwise
*/
public boolean getFollowMasterScheme(){
SlideAtom sa = getSlideRecord().getSlideAtom();
return sa.getFollowMasterScheme();
}
/**
* Sets whether this slide draws master color scheme
*
* @param flag {@code true} if the slide draws master color scheme,
* {@code false} otherwise
*/
public void setFollowMasterScheme(boolean flag){
SlideAtom sa = getSlideRecord().getSlideAtom();
sa.setFollowMasterScheme(flag);
}
/**
* Whether this slide draws master sheet objects
*
* @return {@code true} if the slide draws master sheet objects,
* {@code false} otherwise
*/
@Override
public boolean getFollowMasterObjects(){
SlideAtom sa = getSlideRecord().getSlideAtom();
return sa.getFollowMasterObjects();
}
/**
* Background for this slide.
*/
@Override
public HSLFBackground getBackground() {
if (getFollowMasterBackground()) {
final HSLFMasterSheet ms = getMasterSheet();
return (ms == null) ? null : ms.getBackground();
}
return super.getBackground();
}
/**
* Color scheme for this slide.
*/
@Override
public ColorSchemeAtom getColorScheme() {
if (getFollowMasterScheme()) {
final HSLFMasterSheet ms = getMasterSheet();
return (ms == null) ? null : ms.getColorScheme();
}
return super.getColorScheme();
}
private static RecordContainer selectContainer(final RecordContainer root, final int index, final RecordTypes... path) {
if (root == null || index >= path.length) {
return root;
}
final RecordContainer newRoot = (RecordContainer) root.findFirstOfType(path[index].typeID);
return selectContainer(newRoot, index+1, path);
}
/**
* Get the comment(s) for this slide.
* Note - for now, only works on PPT 2000 and
* PPT 2003 files. Doesn't work for PPT 97
* ones, as they do their comments oddly.
*/
@Override
public List<HSLFComment> getComments() {
final List<HSLFComment> comments = new ArrayList<>();
// If there are any, they're in
// ProgTags -> ProgBinaryTag -> BinaryTagData
final RecordContainer binaryTags =
selectContainer(getSheetContainer(), 0,
RecordTypes.ProgTags, RecordTypes.ProgBinaryTag, RecordTypes.BinaryTagData);
if (binaryTags != null) {
for (final org.apache.poi.hslf.record.Record record : binaryTags.getChildRecords()) {
if (record instanceof Comment2000) {
comments.add(new HSLFComment((Comment2000)record));
}
}
}
return comments;
}
/**
* Header / Footer settings for this slide.
*
* @return Header / Footer settings for this slide
*/
@Override
public HeadersFooters getHeadersFooters(){
return new HeadersFooters(this, HeadersFootersContainer.SlideHeadersFootersContainer);
}
@Override
protected void onAddTextShape(HSLFTextShape shape) {
List<HSLFTextParagraph> newParas = shape.getTextParagraphs();
_paragraphs.add(newParas);
}
/** This will return an atom per TextBox, so if the page has two text boxes the method should return two atoms. */
public StyleTextProp9Atom[] getNumberedListInfo() {
return this.getPPDrawing().getNumberedListInfo();
}
public EscherTextboxWrapper[] getTextboxWrappers() {
return this.getPPDrawing().getTextboxWrappers();
}
@Override
public void setHidden(boolean hidden) {
org.apache.poi.hslf.record.Slide cont = getSlideRecord();
SSSlideInfoAtom slideInfo =
(SSSlideInfoAtom)cont.findFirstOfType(RecordTypes.SSSlideInfoAtom.typeID);
if (slideInfo == null) {
slideInfo = new SSSlideInfoAtom();
cont.addChildAfter(slideInfo, cont.findFirstOfType(RecordTypes.SlideAtom.typeID));
}
slideInfo.setEffectTransitionFlagByBit(SSSlideInfoAtom.HIDDEN_BIT, hidden);
}
@Override
public boolean isHidden() {
SSSlideInfoAtom slideInfo =
(SSSlideInfoAtom)getSlideRecord().findFirstOfType(RecordTypes.SSSlideInfoAtom.typeID);
return (slideInfo != null) && slideInfo.getEffectTransitionFlagByBit(SSSlideInfoAtom.HIDDEN_BIT);
}
@Override
public void draw(Graphics2D graphics) {
DrawFactory drawFact = DrawFactory.getInstance(graphics);
Drawable draw = drawFact.getDrawable(this);
draw.draw(graphics);
}
@Override
public boolean getFollowMasterColourScheme() {
return false;
}
@Override
public void setFollowMasterColourScheme(boolean follow) {
}
@Override
public boolean getFollowMasterGraphics() {
return getFollowMasterObjects();
}
@Override
public boolean getDisplayPlaceholder(final Placeholder placeholder) {
final HeadersFooters hf = getHeadersFooters();
final SlideLayoutType slt = getSlideRecord().getSlideAtom().getSSlideLayoutAtom().getGeometryType();
final boolean isTitle =
(slt == SlideLayoutType.TITLE_SLIDE || slt == SlideLayoutType.TITLE_ONLY || slt == SlideLayoutType.MASTER_TITLE);
switch (placeholder) {
case DATETIME:
return (hf.isDateTimeVisible() && (hf.isTodayDateVisible() || (hf.isUserDateVisible() && hf.getUserDateAtom() != null))) && !isTitle;
case SLIDE_NUMBER:
return hf.isSlideNumberVisible() && !isTitle;
case HEADER:
return hf.isHeaderVisible() && hf.getHeaderAtom() != null && !isTitle;
case FOOTER:
return hf.isFooterVisible() && hf.getFooterAtom() != null && !isTitle;
default:
return false;
}
}
@Override
public boolean getDisplayPlaceholder(final SimpleShape<?,?> placeholderRef) {
Placeholder placeholder = placeholderRef.getPlaceholder();
if (placeholder == null) {
return false;
}
final HeadersFooters hf = getHeadersFooters();
final SlideLayoutType slt = getSlideRecord().getSlideAtom().getSSlideLayoutAtom().getGeometryType();
final boolean isTitle =
(slt == SlideLayoutType.TITLE_SLIDE || slt == SlideLayoutType.TITLE_ONLY || slt == SlideLayoutType.MASTER_TITLE);
switch (placeholder) {
case HEADER:
return hf.isHeaderVisible() && hf.getHeaderAtom() != null && !isTitle;
case FOOTER:
return hf.isFooterVisible() && hf.getFooterAtom() != null && !isTitle;
case DATETIME:
case SLIDE_NUMBER:
default:
return false;
}
}
@Override
public HSLFMasterSheet getSlideLayout(){
// TODO: find out how we can find the mastersheet base on the slide layout type, i.e.
// getSlideRecord().getSlideAtom().getSSlideLayoutAtom().getGeometryType()
return getMasterSheet();
}
}
|