On Tue, Nov 29, 2011 at 8:21 PM, Mike Lundin <[email protected]> wrote: > Yegor, > > I've found that using the Tahoma font, the getTextHeight method is much > more accurate. There is still a bit of discrepancy, but not as bad as when > I was using Arial. >
It is just a coincidence that the results with Tahoma are more accurate than with Arial. There always be differences between POI and PowerPoint, mostly because they compute text metrics differently: POI breaks text using the text API's included in JDK and PowerPoint is using its own algorithm. The accuracy may depend on the font family (serif vs sans serif, mono-spaced vs variable spaced, etc.), whether kerning is used, special effects like shadows and other factors. Of course, I assume that both Arial and Tahoma are available in your JVM. > Is it possible to have TextFragment be a public class and the > XSLFTextParagraph.breakText be public? I have cases where I need to split a > large chunk of text when it goes beyond a boundary, and those being public > would make it possible to calculate what text would fit in the boundary, > and what text would need to go somewhere else. > Yes, I can make them public. This change is coming soon. Yegor > Thanks, > Mike > > On Thu, Nov 24, 2011 at 8:24 AM, Yegor Kozlov <[email protected]> wrote: > >> I committed the fix that increases visibility of getTextHeight() in >> r1205902. >> >> > >> > Also, when I use the calculation, it gets the same results as the >> > home-grown algorithm I had been using in the test case I've included >> below >> > (not really a test case, since I can't compare the end result in the >> file). >> > Essentially, I create a text box with a known width (the 82), insert a >> > paragraph, then measure the height of the text, and then reshape the >> anchor >> > of the text box to use that height. However, in this case, it comes up >> with >> > a height of 6. This represents a single line for the text box. However, >> > when I open the file, the text wraps and ends up showing two lines. >> > >> > I'm going to look through the code and see if I can find out why that is, >> > but wanted to run this code past you first and see if I'm doing something >> > wrong. >> > >> >> it looks like a rounding issue. For width=82 the text is wrapped and >> for width=83 it fits on one line. My advise is to always give an extra >> pixel to compensate rounding. >> >> Yegor >> >> > On a positive note, for most every other text sample that I'm trying, it >> > works correctly. >> > >> > Thanks, >> > Mike >> > >> > Code sample: >> > >> > public void testTextBreaking() throws Exception { >> > XMLSlideShow ppt = new XMLSlideShow(); >> > XSLFSlide slide = ppt.createSlide(); >> > >> > XSLFTextBox elementTextBox = slide.createTextBox(); >> > elementTextBox.setAnchor(new Rectangle(20, 20, 82, 1)); >> > elementTextBox.setBottomInset(0); >> > elementTextBox.setTopInset(0); >> > elementTextBox.setLeftInset(0); >> > elementTextBox.setRightInset(0); >> > XSLFTextRun elementTextRun = >> > elementTextBox.addNewTextParagraph().addNewTextRun(); >> > >> > elementTextRun.setFontFamily("Arial"); >> > elementTextRun.setFontSize(6); >> > elementTextRun.setText("25000.000001 - 31680.000000"); >> > >> > elementTextBox.setAnchor(new Rectangle(20, 20, 82, >> > (int)elementTextBox.getTextHeight())); >> > >> > File file = new File("testTextBreaking.pptx"); >> > BufferedOutputStream os = new BufferedOutputStream(new >> > FileOutputStream(file)); >> > try { >> > ppt.write(os); >> > } finally { >> > os.close(); >> > } >> > } >> > >> > On Tue, Nov 22, 2011 at 11:29 PM, Yegor Kozlov <[email protected] >> >wrote: >> > >> >> XSLFTextShape#getTextHeight() returns the cumulative height occupied >> >> by the text, but this method is private for now. The implementation is >> >> pretty complex and takes into account all character and paragraph >> >> properties such as font size and family, line spacing, spacings before >> >> and after paragraphs, margins, indents, etc. >> >> >> >> I'm going to make it public. For now you can call it via reflection or >> >> apply this change to trunk and use your custom build. >> >> >> >> Yegor >> >> >> >> On Wed, Nov 23, 2011 at 2:51 AM, Mike Lundin <[email protected]> wrote: >> >> > I have a text box with a set width (essentially, a entry in a >> column). I >> >> > would like to add text to the text box and determine how much vertical >> >> > space that text will take up with the document. This will allow me to >> >> > position the next element just below the current one. >> >> > >> >> > I'm currently performing an estimation of the height based on a >> >> FontMetrics >> >> > object, but it's not exact and will sometimes result it large gaps or >> >> text >> >> > running over other elements. >> >> > >> >> > Is there a way to do this? >> >> > >> >> > Thanks, >> >> > Mike >> >> > >> >> >> >> --------------------------------------------------------------------- >> >> To unsubscribe, e-mail: [email protected] >> >> For additional commands, e-mail: [email protected] >> >> >> >> >> > >> >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: [email protected] >> For additional commands, e-mail: [email protected] >> >> --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
