getting the length of a text string

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
mattaspen
Posts: 5
Joined: Thu Apr 17, 2008 5:53 pm

getting the length of a text string

Post by mattaspen »

Hello,

I need to get the length (dimension in millimeters not the number of characters!) of a string (text) to calculate the size of rectangle it needs to go into.

Here is my code snippet:

Code: Select all

// create a TextShape
			XShape headingTexShape = ShapeHelper.createShape(xDrawDoc, new Point( 1410, 1197 ), new Size( 25256, 2037 ), "com.sun.star.drawing.TextShape");
			// add it to the page's shapes
			xShapes.add(headingTexShape);
			
			// create a new XText object
			XText headingTextShapeText = (XText)UnoRuntime.queryInterface(XText.class, headingTexShape);
			
			// create text cursor for selecting and formatting
			XTextCursor headingTextShapeTextCursor = headingTextShapeText.createTextCursor();
			XPropertySet headingTextShapeTextCursorPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, headingTextShapeTextCursor);
			
			headingTextShapeTextCursorPropertySet.setPropertyValue("CharWeight", new Float ( com.sun.star.awt.FontWeight.BOLD));
			headingTextShapeTextCursorPropertySet.setPropertyValue("CharHeight", new Float ( 20 ));
			headingTextShapeTextCursorPropertySet.setPropertyValue("CharFontName", "Arial");
			
			headingTextShapeText.setString("My very long string goes here ... blah blah blah");
When I create the rectangles, they have to align in a grid on a presentation page.
I'm reading in data from a spreadsheet and have to "align" these strings to fit into the rectangles. The rectangles are of fixed width, but, obviously, if the string is long, I will have to 'wrap' it and increase the rectangle's height accordingly. I need the string's length to (a) calculate when I need to split it up, and as a result, by how much to increase the rectangle's height.

My question, again, is: "once I set my string, how can I get its length (in millimeters; since I'm composing the page this way)".

Thanks,
--MA
User avatar
acknak
Moderator
Posts: 22756
Joined: Mon Oct 08, 2007 1:25 am
Location: USA:NJ:E3

Re: getting the length of a text string

Post by acknak »

[Moved to Macros and UNO API]
AOO4/LO5 • Linux • Fedora 23
pitonyak
Volunteer
Posts: 186
Joined: Sun Oct 07, 2007 9:13 pm
Location: Columbus, Ohio, USA

Re: getting the length of a text string

Post by pitonyak »

I am not aware of any method to calculate the dimensions of a text string. More specifically, I do not believe that the API will help you much. I can think of a few methods to try and hack together an approximation, but nothing terribly useful. You can not even trust a character width, because you have kerning and sorts of other things that may be done.

Can you accept approximations?

If so, you may be able to grab the font metrics and create a "largest" bounding box...
Andrew Pitonyak
http://www.pitonyak.org/oo.php
LO and AOO on Fedora
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: getting the length of a text string

Post by Villeroy »

You may try to prepare a preformatted c.s.s.awt.EditControlModel (font properties, multi-line, no scrollbars), put your (multiline) text into it and get a Size struct from method XLayoutConstrains.getMinimumSize(). There is another method XTextLayoutConstrains.getMinimumSize(nCols, nLines) which accepts the numbers of cols and lines as argument. I wonder how nCols works with proportional fonts.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Post Reply