From 97d0beab68c4b1aff70c784caf8219ab4bb7a433 Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Wed, 25 Jan 2012 07:53:07 +0000 Subject: [PATCH] support kerning in XSLF text runs git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1235668 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/xslf/usermodel/XSLFTextParagraph.java | 3 +- .../poi/xslf/usermodel/XSLFTextRun.java | 22 +++++++ .../xslf/usermodel/TestXSLFTextParagraph.java | 6 +- .../poi/xslf/usermodel/TestXSLFTextRun.java | 66 +++++++++++++++++++ 4 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFTextRun.java diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTextParagraph.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTextParagraph.java index e78acefb8e..b2d5953743 100644 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTextParagraph.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTextParagraph.java @@ -117,7 +117,8 @@ public class XSLFTextParagraph implements Iterable{ public XSLFTextRun addNewTextRun(){ CTRegularTextRun r = _p.addNewR(); - r.addNewRPr(); + CTTextCharacterProperties rPr = r.addNewRPr(); + rPr.setLang("en-US"); XSLFTextRun run = new XSLFTextRun(r, this); _runs.add(run); return run; diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTextRun.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTextRun.java index cd94cdd0b3..ff54d5ceba 100644 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTextRun.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTextRun.java @@ -167,6 +167,10 @@ public class XSLFTextRun { 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)); } } @@ -212,6 +216,24 @@ public class XSLFTextRun { return fetcher.getValue() == null ? 0 : fetcher.getValue(); } + /** + * Set the spacing between characters within a text run. + *

+ * The spacing is specified in points. Positive values will cause the text to expand, + * negative values to condense. + *

+ * + * @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. * diff --git a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFTextParagraph.java b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFTextParagraph.java index 50d4c812b5..19eee771f4 100644 --- a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFTextParagraph.java +++ b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFTextParagraph.java @@ -27,11 +27,7 @@ import java.awt.image.BufferedImage; import java.util.List; /** - * Created by IntelliJ IDEA. - * User: yegor - * Date: Nov 10, 2011 - * Time: 1:43:25 PM - * To change this template use File | Settings | File Templates. + * @author Yegor Kozlov */ public class TestXSLFTextParagraph extends TestCase { private static POILogger _logger = POILogFactory.getLogger(XSLFTextParagraph.class); diff --git a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFTextRun.java b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFTextRun.java new file mode 100644 index 0000000000..3ee2616b0b --- /dev/null +++ b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFTextRun.java @@ -0,0 +1,66 @@ +/* + * ==================================================================== + * 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.xslf.usermodel; + +import junit.framework.TestCase; +import org.apache.poi.util.POILogFactory; +import org.apache.poi.util.POILogger; +import org.apache.poi.xslf.XSLFTestDataSamples; + +import java.awt.*; +import java.awt.geom.Rectangle2D; +import java.awt.image.BufferedImage; +import java.util.List; + +/** + * @author Yegor Kozlov + */ +public class TestXSLFTextRun extends TestCase { + + public void testRunProperties(){ + XMLSlideShow ppt = new XMLSlideShow(); + XSLFSlide slide = ppt.createSlide(); + XSLFTextShape sh = slide.createAutoShape(); + + XSLFTextRun r = sh.addNewTextParagraph().addNewTextRun(); + assertEquals("en-US", r.getRpR().getLang()); + + assertEquals(0., r.getCharacterSpacing()); + r.setCharacterSpacing(3); + assertEquals(3., r.getCharacterSpacing()); + r.setCharacterSpacing(-3); + assertEquals(-3., r.getCharacterSpacing()); + r.setCharacterSpacing(0); + assertEquals(0., r.getCharacterSpacing()); + assertFalse(r.getRpR().isSetSpc()); + + assertEquals(Color.black, r.getFontColor()); + r.setFontColor(Color.red); + assertEquals(Color.red, r.getFontColor()); + + assertEquals("Calibri", r.getFontFamily()); + r.setFontFamily("Arial"); + assertEquals("Arial", r.getFontFamily()); + + assertEquals(18.0, r.getFontSize()); + r.setFontSize(13.0); + assertEquals(13.0, r.getFontSize()); + + } +} -- 2.39.5