//-----------------------------------------------------------
vector<ofTTFCharacter> ofTrueTypeFont::getStringAsPoints(const string &  str, bool vflip, bool filled) const{
	vector<ofTTFCharacter> shapes;

	if (!bLoadedOk){
		ofLogError("ofxTrueTypeFont") << "getStringAsPoints(): font not allocated: line " << __LINE__ << " in " << __FILE__;
		return shapes;
	};
	iterateString(str,0,0,vflip,[&](uint32_t c, ofVec2f pos){
		shapes.push_back(getCharacterAsPoints(c,vflip,filled));
		shapes.back().translate(pos);
	});
	return shapes;

}
//-----------------------------------------------------------
void ofTrueTypeFont::drawStringAsShapes(const std::string& str, float x, float y) const{
    if (!bLoadedOk){
    	ofLogError("ofTrueTypeFont") << "drawStringAsShapes(): font not allocated: line " << __LINE__ << " in " << __FILE__;
    	return;
    };

	//----------------------- error checking
	if (!settings.contours){
		ofLogError("ofTrueTypeFont") << "drawStringAsShapes(): contours not created for this font, call loadFont() with makeContours set to true";
		return;
	}

	iterateString(str,x,y,true,[&](uint32_t c, ofVec2f pos){
		drawCharAsShape(c, pos.x, pos.y, ofIsVFlipped(), ofGetStyle().bFill);
	});
}
//-----------------------------------------------------------
ofTexture ofTrueTypeFont::getStringTexture(const std::string& str, bool vflip) const {
    vector<glyph> glyphs;
    vector<ofVec2f> glyphPositions;
    long height = 0;
    int width = 0;
    int lineWidth = 0;
    uint32_t prevC = 0;
    iterateString(str, 0, 0, vflip, [&](uint32_t c, ofVec2f pos) {
        try {
            if (c != '\n') {
                auto g = loadGlyph(c);
                glyphs.push_back(g);
                int x = pos.x + g.props.xmin;
                int y = g.props.ymax + pos.y;
                glyphPositions.emplace_back(x, y);
                lineWidth += glyphs.back().props.advance * letterSpacing;
                width = max(width, lineWidth);
                height = max(height, y + long(getLineHeight()));
            } else {
                lineWidth = 0;
            }
        } catch(...) {
        }
    });

    ofTexture tex;
    ofPixels totalPixels;
    totalPixels.allocate(width, height, OF_PIXELS_GRAY_ALPHA);
    //-------------------------------- clear data:
    totalPixels.set(0,255); // every luminance pixel = 255
    totalPixels.set(1,0);
    size_t i = 0;
    for(auto & g: glyphs) {
        if(settings.direction == ofTtfSettings::LeftToRight) {
            g.pixels.blendInto(totalPixels, glyphPositions[i].x, glyphPositions[i].y + getLineHeight() + g.props.ymin + getDescenderHeight() );
        } else {
            g.pixels.blendInto(totalPixels, width-glyphPositions[i].x, glyphPositions[i].y + getLineHeight() + g.props.ymin + getDescenderHeight() );
        }
        i++;
        if(i==glyphPositions.size()) {
            break;
        }
    }
    tex.allocate(totalPixels);
    return tex;
}
//-----------------------------------------------------------
void ofTrueTypeFont::createStringMesh(const std::string& str, float x, float y, bool vflip) const{
	iterateString(str,x,y,vflip,[&](uint32_t c, ofVec2f pos){
		drawChar(c, pos.x, pos.y, vflip);
	});
}