[Drawkit] DXF reader and DrawKit text objects

Graham Cox graham.cox at bigpond.com
Thu Feb 5 06:55:27 PST 2009


On 6 Feb 2009, at 1:07 am, Allan wrote:

> Turns out the existing DXF reader codes are not going to work for  
> me. In the case of dxf_lib it's too simple in that it doesn't deal  
> with "blocks" correctly and the underlying code is not particularly  
> well structured (not object oriented). Dime is a much better  
> library, but it's just too expensive for me -- they only want to  
> license it as part of the larger Coin_3D package and they want over  
> $3000 for it. So I've decided to create a stand-alone DXF reader  
> framework. I'm most of the way through it and have implemented  
> lines, polylines, arcs, and circles so far. These are the basic  
> building blocks I need for what I'm doing and I have the import to  
> the DKSimpleDXF application working well.

Sounds great!

> The three final things I need to deal with are TEXT entities (single  
> line of text), MTEXT objects (multiple lines), and blocks. I'm a bit  
> stuck on the TEXT import right now and I'm hoping you can help with  
> some advice. The DXF TEXT entity does not use the concept of a  
> bounding box or bounding rectangle like DrawKit does. In a TEXT  
> entity, it anchors to an alignment point and then drawings the text  
> springing from that point.
>
> Is there any equivalent to this in DrawKit? I have played around  
> with the DKTextShape and the associated DKTextAdornment quite a bit  
> and cannot find anything there. I've also played with the DKTextPath  
> and it also needs to be made "big enough" to show all the text. Am I  
> going to have to get the TEXT entity data from the DXF file and then  
> calculate the size of a bounding box or long-enough text-path to  
> contain the text I create so it all will show?
>
> Along these lines, the DKTextAdornment property  
> "allowsTextToExtendHorizontally" seems like it might be intended to  
> accomplish what I'm trying to do here (let the text extend beyond  
> the bounding box of the associated shape), but even when I set this  
> property to YES the text disappears as I make the bounding box  
> smaller and doesn't actually seem to "ExtendHorizontally."
>
> Any thoughts? Thanks for any help you can provide.
>

Everything in DK has a bounding box. I suspect that that's also true  
in Autocad and other apps too, it's just that the DXF file isn't  
making that explicit for text.

Basically yes, you'll have to work out the bounds you need given the  
text itself. In turn that requires that you know how big the text is.  
I assume that the DXF format has some way to indicate differing text  
sizes and you can then associate these with particular fonts and point  
sizes.

For standalone text labels, I'd suggest DKTextShape. This object has  
gone through some evolution within DK and it hasn't quite stopped yet  
(I've got some pending changes for the next update) but it should do  
what you want. If you can end up with an NSAttributedString containing  
your text in the desired font, figuring out the bounding box you need  
is as easy as calling its -size method. I have found that  
[NSAttributedString size] is not always completely accurate, though it  
depends on the font. Most "workhorse" fonts such as Helvetica are OK,  
but fancy ones like Zapfino come out fairly inaccurately. To make sure  
that the text really will fit, I'd be generous with the horizontal  
size - maybe add an extra 10%. With left alignment the extra won't be  
seen. You can also set the vertical size to fit automatically using  
[DKTextShape sizeVerticallyToFitText] once you've set the shape's  
text. You can pass an attributed string directly to its setText: method.

The text adornment property you mention doesn't really work at the  
moment - sorry. I was intending to make that do something like what  
you're doing here, and I should at least make it functional or drop  
it, but my use for it was a little different, and I ran into other  
difficulties that I solved in a totally different manner for my app. I  
need to go back and fix that up, but in this case I'm sure you'll find  
it easier to use DKTextShape (it uses a DKTextAdornment internally  
anyway, it just does so independently of the style).

I'll also look into providing a convenience method that makes a text  
shape of the right size to fit an attributed string on a single line.

I hope that's some help - let me know if you need more.

--Graham




More information about the Drawkit mailing list