CL_Size CL_Font::get_text_size(CL_GraphicContext &gc, const CL_StringRef &text) { CL_Size total_size; if (!impl.is_null()) { CL_FontMetrics fm = get_font_metrics(gc); int line_spacing = 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++) { CL_Size line_size = get_provider()->get_text_size(gc, lines[i]); if ((line_size.width == 0) && (line_size.height == 0) && (lines.size() > 1)) // blank line line_size.height = fm.get_descent() + fm.get_ascent(); if ((i+1) != lines.size()) // Do not add the line spacing on the last line line_size.height += line_spacing; if (total_size.width < line_size.width) // Find the widest line total_size.width = line_size.width; total_size.height += line_size.height; } } return total_size; }
void LabelImpl::draw(CL_GraphicContext &p_gc) { G_ASSERT(m_parent->isLoaded()); float ax, ay; const CL_Size s = m_parent->size(p_gc); calculateAttachPoint(s.width, s.height, ax, ay); CL_Pointf position; position.x = m_pos.x - ax; position.y = m_pos.y - ay - m_fontMetrics.get_descent(); if (m_shadowVisible) { drawShadow(p_gc, position); } m_clFont->draw_text(p_gc, position.x, position.y, m_text, m_color); #if !defined(NDEBUG) && defined(DRAW_LABEL_BOUNDS) // draw label frame debug code CL_Pen newPen; newPen.set_line_width(1.0f); const CL_Pen oldPen = p_gc.get_pen(); p_gc.set_pen(newPen); const float y2 = y + m_fontMetrics.get_descent(); CL_Draw::box(p_gc, x, y2 - s.height, x + s.width, y2, CL_Colorf::red); p_gc.set_pen(oldPen); #endif // !NDEBUG && DRAW_LABEL_BOUNDS }
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; } } }
CL_GUIThemePart::VerticalTextPosition CL_GUIThemePart::get_vertical_text_align(CL_GraphicContext &gc, CL_Font &font, const CL_Rect &content_rect) { // See diagram in: Documentation\Overview\fonts.html (Font Metrics) CL_FontMetrics metrics = font.get_font_metrics(); float align_height = metrics.get_ascent() - metrics.get_internal_leading(); float content_height = content_rect.get_height(); float baseline = (content_height + align_height) / 2.0f; VerticalTextPosition result; result.baseline = baseline + content_rect.top; result.top = result.baseline - metrics.get_ascent(); result.bottom = result.baseline + metrics.get_descent(); return result; }
void Info::draw(CL_GraphicContext &gc) { CL_String text_to_draw = name; CL_Rect draw_rect = get_geometry(); int draw_xpos = 0; int draw_ypos = 0; //CL_Draw::fill(gc, CL_Rect(draw_xpos, draw_ypos, CL_Size(get_width(), get_height())), CL_Colorf(CL_Colorf::red)); CL_Font font = gui->get_font(); CL_FontMetrics metrics = font.get_font_metrics(gc); draw_ypos += (int) metrics.get_ascent(); CL_Colorf color; int time_diff = CL_System::get_time() - activated_time; float color_value = 1.0f - ( ( (float) time_diff ) / 1000.0f); if ( (color_value <= 1.0f) && (color_value > 0.0f) ) { color = CL_Colorf(color_value, color_value/2.0f, color_value/2.0f, color_value); font.draw_text(gc, draw_xpos, draw_ypos, "#", color); color = CL_Colorf(color_value, color_value/2.0f, color_value/2.0f, 1.0f); text_to_draw = name + comment; set_constant_repaint(true); } else { color = CL_Colorf(0, 0, 0, 1.0f); set_constant_repaint(false); } //font.draw_text(gc, draw_xpos + 16, draw_ypos, text_to_draw, CL_Colorf::white); font.draw_text(gc, draw_xpos + 16-1, draw_ypos-1, text_to_draw, color); }
void run() { quit = false; CL_DisplayWindowDescription window_desc; window_desc.set_size(CL_Size(1920, 1080), true); window_desc.set_title("Luna"); CL_DisplayWindow window(window_desc); CL_Slot slot_quit = window.sig_window_close().connect(this, &RootWindow::on_window_close); CL_GraphicContext gc = window.get_gc(); CL_InputDevice keyboard = window.get_ic().get_keyboard(); CL_FontDescription font_desc; font_desc.set_typeface_name("Monospace"); font_desc.set_height(16); CL_Font_System font(gc, font_desc); CL_FontMetrics fmetrics = font.get_font_metrics(); int fntWidth = fmetrics.get_max_character_width(); CL_SpriteDescription spr_desc; spr_desc.add_frame(CL_ImageProviderFactory::load("spaceship.png")); CL_Sprite node(gc, spr_desc); node.set_play_loop(true); node.set_play_pingpong(false); node.set_frame(0); CL_Image pict(gc,"stars.jpg"); x = gc.get_width() / 2 ; y = gc.get_height() / 2 ; U = 0; W = 0; V = 0; H = 0; step = 0.2; while (!quit) { if(keyboard.get_keycode(CL_KEY_ESCAPE) == true) quit = true; if(keyboard.get_keycode(CL_KEY_LEFT) == true) U -= step ; if(keyboard.get_keycode(CL_KEY_RIGHT) == true) U += step ; if(keyboard.get_keycode(CL_KEY_UP) == true) V -= step ; if(keyboard.get_keycode(CL_KEY_DOWN) == true) V += step ; if(x < -node.get_width()) { x = gc.get_width(); } else if(x > gc.get_width()) { x = -node.get_width(); } else x += U ; if(y < step) { y = step; V = 0; } else if(y > gc.get_height()-node.get_height()) { y = gc.get_height()-node.get_height(); V = 0; } else y += V; gc.clear(); pict.draw(gc,W,H); pict.draw(gc,W,H-gc.get_height() ); pict.draw(gc,W-gc.get_width(),H ); H++ ; if( H > gc.get_height() ) H = 0; node.draw(gc, x, y); CL_String velocity = cl_format("Velocity U[%1] V[%2] --- Coordinate X[%3] Y[%4] --- Origin W[%5] H[%6]",U,V,x,y,W,H); font.draw_text(gc, gc.get_width()/2 - (velocity.length() * fntWidth)/2, 16, velocity); window.flip(); CL_KeepAlive::process(); CL_System::sleep(10); } }