# Textbox leading - measureGlyphs sometimes inaccurate

**URL:** <https://discourse.libcinder.org/t/textbox-leading-measureglyphs-sometimes-inaccurate/247>\
**Category:** Using Cinder\
**Created:** [September 5, 2016, 12:48am UTC](https://discourse.libcinder.org/t/textbox-leading-measureglyphs-sometimes-inaccurate/247 "2016-09-05T00:48:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nay](https://avatars.discourse-cdn.com/v4/letter/n/8e7dd6/32.png) [@nay](https://discourse.libcinder.org/u/nay)\
**Post date:** [September 5, 2016, 12:48am UTC](https://discourse.libcinder.org/t/textbox-leading-measureglyphs-sometimes-inaccurate/247/1 "2016-09-05T00:48:17Z")

</div>

Hi All,

I need a solution for TextBox leading (vertical line-spacing). My previous solutions are failing me at the moment. In [the past](https://forum.libcinder.org/topic/textbox-leading) I have hacked the OSX TextBox implementation to accomodate this but this is trickier on Windows. At the moment I am working on a cross platform solution using `TextBox::measureGlyphs()`.

I first used this to calculate the number of lines and crop the generated texture into separate lines and reposition when drawing. However, this has some artefacts in some fonts/sizes when characters from some lines creep too close to others (the bottom of a lower case _y_ for example).

So I then tried the approach of creating a TextBox texture just so that I can use `TextBox::measureGlyphs()` to calculate where the lines breaks are and then create a new TextBox for each line. This works perfectly some of the time however with some fonts, such as the OTF fonts I need to work with at the moment `TextBox::measureGlyphs()` returns an array size 2 less than the number of characters in the text.

This seems to happen if word wraps are used. I see the comment in the source code that word wraps are not supported by this function on windows but as I said this approach works with some fonts and the problem also occurs on both Windows and OSX. I tried converting the OTF font to TTF as I have other TTF fonts that work but this did not fix the problem.

Does anyone have any advice or a leading solution they use on windows?

I have my class used in a sample app [here](https://github.com/codeoncanvas/coc-utils/tree/master/ci-samples/TextBoxExtendedSample) although the relevant logic for breaking up the lines is below:

```auto
	std::vector<std::pair<uint16_t,ci::vec2> >pairs = measureGlyphs();
	float y = pairs[0].second.y;

	string text = getText();
	vector<string> lines;

	string line = "";
	int i = 0;
	for (auto p : pairs ) {

		if ( p.second.y > y) {
			counter++;
			y = p.second.y;
			boost::trim(line);
			if (i>0) lines.push_back(line);
			line="";
		}
		line += text[i];
		i++;
	}
        line += text[text.length()-1];
	boost::trim(line);
	lines.push_back(line);

```

EDIT - If someone is keen to take a look at a problem font please PM me and I can get one to you.

---

<div class="post-metadata">

**Author:** ![nay](https://avatars.discourse-cdn.com/v4/letter/n/8e7dd6/32.png) [@nay](https://discourse.libcinder.org/u/nay)\
**Post date:** [September 5, 2016, 1:28am UTC](https://discourse.libcinder.org/t/textbox-leading-measureglyphs-sometimes-inaccurate/247/2 "2016-09-05T01:28:24Z")

</div>

sigh, so this seems to be sorted by calling `TextBox::ligate(false)`  
sorry for the noise but if anyone has a use for the now working class feel free!

EDIT - actually, this works on OSX but on windows `measureGlyphs()` returns 2 less glyphs than it should regardless of the ligate setting… help! I assume this is a bug in cinder’s text handling? strange that TextBox always renders correctly even if `measureGlyphs()` returns the wrong data.

---

<div class="post-metadata">

**Author:** ![nay](https://avatars.discourse-cdn.com/v4/letter/n/8e7dd6/32.png) [@nay](https://discourse.libcinder.org/u/nay)\
**Post date:** [September 5, 2016, 5:57am UTC](https://discourse.libcinder.org/t/textbox-leading-measureglyphs-sometimes-inaccurate/247/3 "2016-09-05T05:57:04Z")

</div>

Okay. so to get this working on windows I had to override the `measureGlyphs()` function and remove some flags in `GetCharacterPlacementW()`. Removing `GCP_LIGATE` did not suffice so I removed all flags. This seems to work fine for my purposes although I imagine there might be some issues for some languages other than english.

```auto
//this different to TextBox default:
if (!::GetCharacterPlacementW(ci::Font::getGlobalDc(), (wchar_t*)&wideText[0], wideText.length(), 0,
	&gcpResults, NULL )) { //removed these flags for accurate glyphs array size: GCP_DIACRITIC | GCP_LIGATE | GCP_GLYPHSHAPE | GCP_REORDER
	return vector<pair<uint16_t, vec2> >(); // failure
}

```
