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
|
/* ====================================================================
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.xssf.usermodel;
import org.apache.poi.ooxml.util.NumberHelper;
import org.apache.poi.ooxml.util.POIXMLUnits;
import org.apache.poi.util.Units;
import org.openxmlformats.schemas.drawingml.x2006.main.CTRegularTextRun;
import org.openxmlformats.schemas.drawingml.x2006.main.CTSRgbColor;
import org.openxmlformats.schemas.drawingml.x2006.main.CTSolidColorFillProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextFont;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextNormalAutofit;
import org.openxmlformats.schemas.drawingml.x2006.main.STTextStrikeType;
import org.openxmlformats.schemas.drawingml.x2006.main.STTextUnderlineType;
/**
* Represents a run of text within the containing text body. The run element is the
* lowest level text separation mechanism within a text body.
*/
public class XSSFTextRun {
private final CTRegularTextRun _r;
private final XSSFTextParagraph _p;
XSSFTextRun(CTRegularTextRun r, XSSFTextParagraph p){
_r = r;
_p = p;
}
XSSFTextParagraph getParentParagraph(){
return _p;
}
public String getText(){
return _r.getT();
}
public void setText(String text){
_r.setT(text);
}
public CTRegularTextRun getXmlObject(){
return _r;
}
public void setFontColor(java.awt.Color color) {
setFontColor(new byte[]{(byte)color.getRed(), (byte)color.getGreen(), (byte)color.getBlue()});
}
/**
* @param rgb
* @since POI 5.4.2
*/
public void setFontColor(byte[] rgb) {
CTTextCharacterProperties rPr = getRPr();
CTSolidColorFillProperties fill = rPr.isSetSolidFill() ? rPr.getSolidFill() : rPr.addNewSolidFill();
CTSRgbColor clr = fill.isSetSrgbClr() ? fill.getSrgbClr() : fill.addNewSrgbClr();
clr.setVal(rgb);
if(fill.isSetHslClr()) fill.unsetHslClr();
if(fill.isSetPrstClr()) fill.unsetPrstClr();
if(fill.isSetSchemeClr()) fill.unsetSchemeClr();
if(fill.isSetScrgbClr()) fill.unsetScrgbClr();
if(fill.isSetSysClr()) fill.unsetSysClr();
}
public java.awt.Color getFontColor() {
final byte[] rgb = getFontColorAsBytes();
if (rgb.length == 3) {
return new java.awt.Color(0xFF & rgb[0], 0xFF & rgb[1], 0xFF & rgb[2]);
} else {
return new java.awt.Color(0xFF & rgb[1], 0xFF & rgb[2], 0xFF & rgb[3], 0xFF & rgb[0]);
}
}
/**
* @return the font color as a byte array.
* @since POI 5.4.2
*/
public byte[] getFontColorAsBytes() {
CTTextCharacterProperties rPr = getRPrOrNull();
if (rPr != null && rPr.isSetSolidFill()) {
CTSolidColorFillProperties fill = rPr.getSolidFill();
if(fill.isSetSrgbClr()){
CTSRgbColor clr = fill.getSrgbClr();
return clr.getVal();
}
}
return new byte[]{0, 0, 0};
}
/**
*
* @param fontSize font size in points.
* The value of {@code -1} unsets the Sz attribute from the underlying xml bean
*/
public void setFontSize(double fontSize) {
CTTextCharacterProperties rPr = getRPr();
if(fontSize == -1.0) {
if(rPr.isSetSz()) rPr.unsetSz();
} else {
if(fontSize < 1.0) {
throw new IllegalArgumentException("Minimum font size is 1pt but was " + fontSize);
}
rPr.setSz((int)(100*fontSize));
}
}
/**
* @return font size in points or -1 if font size is not set.
*/
public double getFontSize() {
double scale = 1;
double size = XSSFFont.DEFAULT_FONT_SIZE; // default font size
CTTextNormalAutofit afit = getParentParagraph().getParentShape().getTxBody().getBodyPr().getNormAutofit();
if (afit != null && afit.isSetFontScale()) {
scale = NumberHelper.toDouble(afit.getFontScale()) / 100000;
}
CTTextCharacterProperties rPr = getRPrOrNull();
if(rPr != null && rPr.isSetSz()){
size = rPr.getSz()*0.01;
}
return size * scale;
}
/**
*
* @return the spacing between characters within a text run,
* If this attribute is omitted then a value of 0 or no adjustment is assumed.
*/
public double getCharacterSpacing(){
CTTextCharacterProperties rPr = getRPrOrNull();
if(rPr != null && rPr.isSetSpc()){
return Units.toPoints(POIXMLUnits.parseLength(rPr.xgetSpc()));
}
return 0;
}
/**
* Set the spacing between characters within a text run.
* <p>
* The spacing is specified in points. Positive values will cause the text to expand,
* negative values to condense.
* </p>
*
* @param spc character spacing in points.
*/
public void setCharacterSpacing(double spc){
CTTextCharacterProperties rPr = getRPr();
if(spc == 0.0) {
if(rPr.isSetSpc()) rPr.unsetSpc();
} else {
rPr.setSpc((int)(100*spc));
}
}
/**
* Specifies the typeface, or name of the font that is to be used for this text run.
*
* @param typeface the font to apply to this text run.
* The value of {@code null} unsets the Typeface attribute from the underlying xml.
*/
public void setFont(String typeface){
setFontFamily(typeface, (byte)-1, (byte)-1, false);
}
public void setFontFamily(String typeface, byte charset, byte pictAndFamily, boolean isSymbol){
CTTextCharacterProperties rPr = getRPr();
if(typeface == null){
if(rPr.isSetLatin()) rPr.unsetLatin();
if(rPr.isSetCs()) rPr.unsetCs();
if(rPr.isSetSym()) rPr.unsetSym();
} else {
if(isSymbol){
CTTextFont font = rPr.isSetSym() ? rPr.getSym() : rPr.addNewSym();
font.setTypeface(typeface);
} else {
CTTextFont latin = rPr.isSetLatin() ? rPr.getLatin() : rPr.addNewLatin();
latin.setTypeface(typeface);
if(charset != -1) latin.setCharset(charset);
if(pictAndFamily != -1) latin.setPitchFamily(pictAndFamily);
}
}
}
/**
* @return font family or null if not set
*/
public String getFontFamily(){
CTTextCharacterProperties rPr = getRPrOrNull();
if (rPr != null) {
CTTextFont font = rPr.getLatin();
if(font != null) {
return font.getTypeface();
}
}
return XSSFFont.DEFAULT_FONT_NAME;
}
public byte getPitchAndFamily() {
CTTextCharacterProperties rPr = getRPrOrNull();
if (rPr != null) {
CTTextFont font = rPr.getLatin();
if(font != null) {
return font.getPitchFamily();
}
}
return 0;
}
/**
* Specifies whether a run of text will be formatted as strikethrough text.
*
* @param strike whether a run of text will be formatted as strikethrough text.
*/
public void setStrikethrough(boolean strike) {
getRPr().setStrike(strike ? STTextStrikeType.SNG_STRIKE : STTextStrikeType.NO_STRIKE);
}
/**
* @return whether a run of text will be formatted as strikethrough text. Default is false.
*/
public boolean isStrikethrough() {
CTTextCharacterProperties rPr = getRPrOrNull();
if (rPr != null && rPr.isSetStrike()){
return rPr.getStrike() != STTextStrikeType.NO_STRIKE;
}
return false;
}
/**
* @return whether a run of text will be formatted as a superscript text. Default is false.
*/
public boolean isSuperscript() {
CTTextCharacterProperties rPr = getRPrOrNull();
if (rPr != null && rPr.isSetBaseline()){
return POIXMLUnits.parsePercent(rPr.xgetBaseline()) > 0;
}
return false;
}
/**
* Set the baseline for both the superscript and subscript fonts.
* <p>
* The size is specified using a percentage.
* Positive values indicate superscript, negative values indicate subscript.
*/
public void setBaselineOffset(double baselineOffset){
getRPr().setBaseline((int) baselineOffset * 1000);
}
/**
* Set whether the text in this run is formatted as superscript.
* Default base line offset is 30%
*
* @see #setBaselineOffset(double)
*/
public void setSuperscript(boolean flag){
setBaselineOffset(flag ? 30. : 0.);
}
/**
* Set whether the text in this run is formatted as subscript.
* Default base line offset is -25%.
*
* @see #setBaselineOffset(double)
*/
public void setSubscript(boolean flag){
setBaselineOffset(flag ? -25.0 : 0.);
}
/**
* @return whether a run of text will be formatted as a superscript text. Default is false.
*/
public boolean isSubscript() {
CTTextCharacterProperties rPr = getRPrOrNull();
if (rPr != null && rPr.isSetBaseline()){
return POIXMLUnits.parsePercent(rPr.xgetBaseline()) < 0;
}
return false;
}
/**
* @return whether a run of text will be formatted as a superscript text. Default is false.
*/
public TextCap getTextCap() {
CTTextCharacterProperties rPr = getRPrOrNull();
if (rPr != null && rPr.isSetCap()) {
return TextCap.values()[rPr.getCap().intValue() - 1];
}
return TextCap.NONE;
}
/**
* Specifies whether this run of text will be formatted as bold text
*
* @param bold whether this run of text will be formatted as bold text
*/
public void setBold(boolean bold){
getRPr().setB(bold);
}
/**
* @return whether this run of text is formatted as bold text
*/
public boolean isBold(){
CTTextCharacterProperties rPr = getRPrOrNull();
if (rPr != null && rPr.isSetB()){
return rPr.getB();
}
return false;
}
/**
* @param italic whether this run of text is formatted as italic text
*/
public void setItalic(boolean italic){
getRPr().setI(italic);
}
/**
* @return whether this run of text is formatted as italic text
*/
public boolean isItalic(){
CTTextCharacterProperties rPr = getRPrOrNull();
if (rPr != null && rPr.isSetI()){
return rPr.getI();
}
return false;
}
/**
* @param underline whether this run of text is formatted as underlined text
*/
public void setUnderline(boolean underline) {
getRPr().setU(underline ? STTextUnderlineType.SNG : STTextUnderlineType.NONE);
}
/**
* @return whether this run of text is formatted as underlined text
*/
public boolean isUnderline(){
CTTextCharacterProperties rPr = getRPrOrNull();
if (rPr != null && rPr.isSetU()){
return rPr.getU() != STTextUnderlineType.NONE;
}
return false;
}
protected CTTextCharacterProperties getRPr() {
return _r.isSetRPr() ? _r.getRPr() : _r.addNewRPr();
}
private CTTextCharacterProperties getRPrOrNull() {
return _r.isSetRPr() ? _r.getRPr() : null;
}
@Override
public String toString(){
return "[" + getClass() + "]" + getText();
}
}
|