int CL_GlyphCache::get_character_index(CL_FontEngine *font_engine, CL_GraphicContext &gc, const CL_String &text, const CL_Point &point) { int dest_x = 0; int dest_y = 0; int character_counter = 0; CL_FontMetrics fm = get_font_metrics(); int font_height = fm.get_height(); int font_ascent = fm.get_ascent(); int font_external_leading = fm.get_external_leading(); std::vector<CL_String> lines = CL_StringHelp::split_text(text, "\n", false); for (std::vector<CL_String>::size_type i=0; i<lines.size(); i++) { int xpos = dest_x; int ypos = dest_y; CL_String &textline = lines[i]; CL_String::size_type string_length = textline.length(); // Scan the string CL_UTF8_Reader reader(textline); while(!reader.is_end()) { unsigned int glyph = reader.get_char(); CL_String::size_type glyph_pos = reader.get_position(); reader.next(); CL_Font_TextureGlyph *gptr = get_glyph(font_engine, gc, glyph); if (gptr == NULL) continue; CL_Rect position(xpos, ypos - font_ascent, CL_Size(gptr->increment.x, gptr->increment.y + font_height + font_external_leading)); if (position.contains(point)) { return glyph_pos + character_counter; } xpos += gptr->increment.x; ypos += gptr->increment.y; } dest_y += font_height + font_external_leading; character_counter += string_length + 1; // (Including the '\n') } return -1; // Not found }
void CL_Font::draw_text(CL_GraphicContext &gc, float dest_x, float dest_y, const CL_StringRef &text, const CL_Colorf &color) { if (!impl.is_null()) { CL_FontMetrics fm = get_font_metrics(gc); int line_spacing = fm.get_height() + fm.get_external_leading(); std::vector<CL_String> lines = CL_StringHelp::split_text(text, "\n", false); for (std::vector<CL_String>::size_type i=0; i<lines.size(); i++) { get_provider()->draw_text(gc, dest_x, dest_y, lines[i], color); dest_y += line_spacing; } } }