<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Getting the width of a justified string?</title>
		<link>http://www.allegro.cc/forums/view/606465</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Mon, 21 Feb 2011 23:49:53 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I would like to add the ability for my textbox to deal with justified. But to do this I would need the width of a string of text when it is justified in order to get the correct caret positioning. Does Allegro 5 provide functions for this? I know I can draw a justified string but I do not know how to get its width.</p><p>Thanks
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (jmasterx)</author>
		<pubDate>Sun, 20 Feb 2011 20:56:50 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Very good question IMO.<br />I think this feature might be of interest to me some time in the future since I&#39;m a GUI developer.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trezker)</author>
		<pubDate>Sun, 20 Feb 2011 21:16:38 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><a href="http://www.allegro.cc/manual/5/al_get_text_width">http://www.allegro.cc/manual/5/al_get_text_width</a><br /><a href="http://www.allegro.cc/manual/5/al_get_text_dimensions">http://www.allegro.cc/manual/5/al_get_text_dimensions</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LennyLen)</author>
		<pubDate>Sun, 20 Feb 2011 21:17:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/606465/904376#target">LennyLen</a> said:</div><div class="quote"><p><a href="http://www.allegro.cc/manual/5/al_get_text_width">http://www.allegro.cc/manual/5/al_get_text_width</a><br /><a href="http://www.allegro.cc/manual/5/al_get_text_dimensions">http://www.allegro.cc/manual/5/al_get_text_dimensions</a></p></div></div><p>


I&#39;m aware of these functions and use them a lot. However I do not see how they can apply to the width of part of a line which has had an arbitrary number of spaces added to it.</p><p>What I meant is something maybe like:</p><p>al_get_justfied_text_width(const char* textLine, ALLEGRO_FONT *f, int w, int subStrLen);</p><p>Which would return the width that that substring of characters would occupy if that line were justified.</p><p>This would allow me to accurately figure out where to position my caret
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (jmasterx)</author>
		<pubDate>Sun, 20 Feb 2011 21:25:57 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>A string should should have exactly the same width regardless of justification. The only difference is the offset, i.e. if it&#39;s drawn at x, x - width/2 or x - width.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gnolam)</author>
		<pubDate>Sun, 20 Feb 2011 22:02:42 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/606465/904379#target">gnolam</a> said:</div><div class="quote"><p>A string should should have exactly the same width regardless of justification. The only difference is the offset, i.e. if it&#39;s drawn at x, x - width/2 or x - width.</p></div></div><p>
I know correcting gnolam is dangerous, but that sounds more like alignment rather than justfication. <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /></p><p>All I can think for getting the width of a justified string, is separating the words and getting the width of each of &#39;em. If the necessary offset between them to cover the whole width is bigger than the offset you set in <span class="source-code"><a href="http://www.allegro.cc/manual/al_draw_justified_text"><span class="a">al_draw_justified_text</span></a></span>, you get the normal text width. If it&#39;s smaller, then it&#39;s as big as x2-x in the first method. At least that&#39;s how I think it should work, I haven&#39;t worked with those functions too much.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Dario ff)</author>
		<pubDate>Sun, 20 Feb 2011 22:08:40 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If the string is being fully justified to fit into an area, then it&#39;s width should be the width of the area.</p><p>edit: I&#39;d set it to whichever is larger out of the width you&#39;re justifying to, or the result of <span class="source-code"><a href="http://www.allegro.cc/manual/al_get_text_width"><span class="a">al_get_text_width</span></a><span class="k2">(</span><span class="k2">)</span></span></p><p>edit 2:</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/606465/904380#target">Dario ff</a> said:</div><div class="quote"><p> I know correcting gnolam is dangerous, but that sounds more like alignment rather than justfication</p></div></div><p>The terms left- and right-justified used to be used extensively to describe what is generally called left- and right-aligned these days.  I made the same assumption that gnolam did.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LennyLen)</author>
		<pubDate>Sun, 20 Feb 2011 23:11:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Text:<br /><span class="source-code">This is a <span class="k1">short</span> sentence.</span></p><p>Text when justified to 30 characters:<br /><span class="source-code">This  is  a  <span class="k1">short</span>   sentence.</span></p><p>What the OP wants:
</p><div class="source-code snippet"><div class="inner"><pre>This  is  a  <span class="k1">short</span>   sentence.
<span class="c">//           ^... the distance from the beginning of the line to here</span>
</pre></div></div><p>

See?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (X-G)</author>
		<pubDate>Mon, 21 Feb 2011 00:08:04 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><s>What I thought this was about was getting the position of a character somewhere in the middle of a justified string.</s><br />Beaten by X-G.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trezker)</author>
		<pubDate>Mon, 21 Feb 2011 00:08:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It&#39;s not particularly pretty, but I filled out your function prototype from above:</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="k1">int</span> al_get_justfied_text_width<span class="k2">(</span><span class="k1">const</span> <span class="k1">char</span><span class="k3">*</span> textLine, <a href="http://www.allegro.cc/manual/ALLEGRO_FONT"><span class="a">ALLEGRO_FONT</span></a> <span class="k3">*</span>f, <span class="k1">int</span> w, <span class="k1">int</span> subStrLen<span class="k2">)</span>
<span class="number">  2</span><span class="k2">{</span>
<span class="number">  3</span>  string fullStr, subStr<span class="k2">;</span>
<span class="number">  4</span>  <span class="k1">int</span> strLength, subLength<span class="k2">;</span>
<span class="number">  5</span>  <span class="k1">int</span> fullSpaces, subSpaces<span class="k2">;</span> 
<span class="number">  6</span>  <span class="k1">int</span> justifiedPos<span class="k2">;</span>
<span class="number">  7</span>
<span class="number">  8</span>  fullStr <span class="k3">=</span> textLine<span class="k2">;</span>
<span class="number">  9</span>  subStr <span class="k3">=</span> fullStr.substr<span class="k2">(</span><span class="n">0</span>,subStrLen<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 10</span>
<span class="number"> 11</span>  fullSpaces <span class="k3">=</span> count<span class="k2">(</span>fullStr.begin<span class="k2">(</span><span class="k2">)</span>, fullStr.end<span class="k2">(</span><span class="k2">)</span>, <span class="s">' '</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 12</span>  subSpaces <span class="k3">=</span> count<span class="k2">(</span>subStr.begin<span class="k2">(</span><span class="k2">)</span>, subStr.end<span class="k2">(</span><span class="k2">)</span>, <span class="s">' '</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 13</span>
<span class="number"> 14</span>  strLength <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_get_text_width"><span class="a">al_get_text_width</span></a><span class="k2">(</span>f, fullStr.c_str<span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 15</span>  subLength <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_get_text_width"><span class="a">al_get_text_width</span></a><span class="k2">(</span>f, subStr.c_str<span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 16</span>
<span class="number"> 17</span>  justifiedPos <span class="k3">=</span> subLength <span class="k3">+</span> <span class="k2">(</span><span class="k2">(</span><span class="k2">(</span>w-strLength<span class="k2">)</span> <span class="k3">/</span> fullSpaces<span class="k2">)</span> <span class="k3">*</span> subSpaces<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 18</span>
<span class="number"> 19</span>  <span class="k1">return</span> <span class="k2">(</span>justifiedPos<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 20</span><span class="k2">}</span>
</div></div><p>

That will give you the position of something like a cursor at a certain position. It assumes all spaces are set to equal width in order to justify the string to the width passed as &#39;w&#39;.</p><p>Is that what you were looking for? I&#39;m not sure if I totally misunderstood the question here.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Nazerith)</author>
		<pubDate>Mon, 21 Feb 2011 04:22:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Getting the width of a text render can be an expensive operation, enough that if you fit it in to a box of, say, 500 pixels and it has more than 30 lines, you may see a drop in framerates.</p><p>You can curb that by creating a cache representing each line and its text width.  Then you would only have to take a speed hit when you need to update the data.</p><p>The most flexible method I&#39;ve come up with so far is to spit the text into words, and have a structure that contains the word and it&#39;s width.  That way you can freely change the width of your box without a speed cost.  Also, changing a single word would only require a change to a word-width pair of a single element.  You would need to create your own justify function with this method, but it&#39;s not difficult.</p><p>However, this method has a few holes, but none that I&#39;ve had to actually deal with in real applications.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Mark Oates)</author>
		<pubDate>Mon, 21 Feb 2011 05:11:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/606465/904432#target">Mark Oates</a> said:</div><div class="quote"><p> Getting the width of a text render can be an expensive operation, enough that if you fit it in to a box of, say, 500 pixels and it has more than 30 lines, you may see a drop in framerates.
</p></div></div><p>You&#39;re doing something wrong. My GUI drops <i>way</i> before that. <img src="http://www.allegro.cc/forums/smileys/cool.gif" alt="8-)" /></p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/606465/904374#target">jmasterx</a> said:</div><div class="quote"><p> Does Allegro 5 provide functions for this? 
</p></div></div><p>No. Its text drawing is quite limited, unfortunately. You&#39;re probably better off creating your own text justifying routine and keep track of lengths of individual words.</p><p>Are you really creating a text box that is justified while typing? That sounds like a weird experience. But I could understand the need for selecting or highlighting justified text.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Matthew Leverton)</author>
		<pubDate>Mon, 21 Feb 2011 09:20:47 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/606465/904452#target">Matthew Leverton</a> said:</div><div class="quote"><p>You&#39;re doing something wrong. My GUI drops way before that. <img src="http://www.allegro.cc/forums/smileys/cool.gif" alt="8-)" /></p></div></div><p><img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /></p><p>Let&#39;s see... I did some tests a while back.</p><p>Here&#39;s an example where the spacing is calculated on each render.  The text box is 500px wide, it ends up having about 14 lines.<br /><span class="remote-thumbnail"><span class="json">{"name":"603446","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/8\/0\/802b3d4e31829a226f2a7b35f4f46211.png","w":1075,"h":834,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/8\/0\/802b3d4e31829a226f2a7b35f4f46211"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/8/0/802b3d4e31829a226f2a7b35f4f46211-240.jpg" alt="603446" width="240" height="186" /></span><br />It looks like the whole operation (User Render) takes about a frame and a half.  So you&#39;ll get a frame hit at about 10 lines (!!!). <img src="http://www.allegro.cc/forums/smileys/lipsrsealed.gif" alt=":-X" /></p><p>This example already has a pre-calculated word-width set, and words are spaced and rendered each frame. same 500px width, and ends up with about 24 lines:<br /><span class="remote-thumbnail"><span class="json">{"name":"603447","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/3\/2\/32f53e0a2d3e2f06a1e2d71f8b69bf5e.png","w":1075,"h":834,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/3\/2\/32f53e0a2d3e2f06a1e2d71f8b69bf5e"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/3/2/32f53e0a2d3e2f06a1e2d71f8b69bf5e-240.jpg" alt="603447" width="240" height="186" /></span><br />This one takes only about 1/8 of a frame.</p><p>That&#39;s a big difference. <img src="http://www.allegro.cc/forums/smileys/undecided.gif" alt=":-/" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Mark Oates)</author>
		<pubDate>Mon, 21 Feb 2011 09:43:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Those texts aren&#39;t even justified, they&#39;re just word wrapped.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trezker)</author>
		<pubDate>Mon, 21 Feb 2011 11:03:50 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/606465/904459#target">Trezker</a> said:</div><div class="quote"><p>Those texts aren&#39;t even justified, they&#39;re just word wrapped.</p></div></div><p>Justification is just a basic calculation on top of that.  This is an example of how expensive <span class="source-code"><a href="http://www.allegro.cc/manual/al_get_text_width"><span class="a">al_get_text_width</span></a><span class="k2">(</span><span class="k2">)</span></span> is - an important thing to consider if you&#39;re finding points in any text.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Mark Oates)</author>
		<pubDate>Mon, 21 Feb 2011 11:16:20 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Justifying the text would be simple. Just insert <span class="source-code">extra_space <span class="k3">/</span> <span class="k2">(</span>word_count <span class="k3">-</span> <span class="n">1</span><span class="k2">)</span></span> pixels of spacing in between each word.</p><p>I haven&#39;t written a multiline text widget or display widget yet, but I would basically take a double linked list of pointers to lines. Each line would contain a double linked list of words. Each word would contain its width and spacing information.</p><p>From that you&#39;d be able to determine which word the mouse was over. When clicking, you could easily compute the exact character.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Matthew Leverton)</author>
		<pubDate>Mon, 21 Feb 2011 11:22:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yeah, I feel that&#39;s probably the proper approach to this problem.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trezker)</author>
		<pubDate>Mon, 21 Feb 2011 23:49:53 +0000</pubDate>
	</item>
</rss>
