Skip to content

Commit 3f0eafe

Browse files
committed
ENH: Adding Text Rendering Example
- Adding Text Rendering to DisplayArea. - Using pangomm Library that was included with gtkmm (brew) install.
1 parent 0f789b2 commit 3f0eafe

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

calculator_simple/build/macos/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ COMPILER_WITH_FLAGS := $(COMPILER) $(FLAGS)
3737
LIB_GTKMM = gtkmm-3.0
3838
LIB_GLIB = glib-2.0
3939
LIB_CAIRO = cairomm-1.0
40-
PKG_CONFIG_LIBS := $(LIB_GTKMM) $(LIB_GLIB) $(LIB_CAIRO)
40+
LIB_PANGO = pangomm-1.4
41+
PKG_CONFIG_LIBS := $(LIB_GTKMM) $(LIB_GLIB) $(LIB_CAIRO) $(LIB_PANGO)
4142
OTHER_LIBS := -lstdc++
4243
LIBS := $(OTHER_LIBS) `pkg-config --cflags --libs $(PKG_CONFIG_LIBS)`
4344

calculator_simple/src/app_window.cpp

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,25 +174,46 @@ bool AppWindow::handle_display_update(
174174
int const area_x_full = display_area_allocation.get_width();
175175
int const area_y_full = display_area_allocation.get_height();
176176

177-
int const top_left_x = (0);
178-
int const top_left_y = (0);
177+
int const lft_x = (0);
178+
int const top_y = (0);
179179

180180
int const center_x = (area_x_full / 2);
181181
int const center_y = (area_y_full / 2);
182182

183-
int const bot_rght_x = (area_x_full);
184-
int const bot_rght_y = (area_y_full);
185-
183+
// int const rght_x = (area_x_full);
184+
int const bot_y = (area_y_full);
186185

187186
draw_context_ptr->set_line_width(10.0);
188-
draw_context_ptr->set_source_rgb(0.8, 0.0, 0.0);
189-
draw_context_ptr->move_to(top_left_x, top_left_y);
187+
draw_context_ptr->set_source_rgb(0.8, 0.0, 0.5);
188+
draw_context_ptr->move_to(lft_x, top_y);
190189
draw_context_ptr->line_to(center_x, center_y);
191-
draw_context_ptr->line_to(top_left_x, bot_rght_y);
192-
draw_context_ptr->move_to(center_x, center_y);
193-
draw_context_ptr->line_to(bot_rght_x, center_y);
190+
draw_context_ptr->line_to(lft_x, bot_y);
194191
draw_context_ptr->stroke();
195192

193+
Pango::FontDescription font;
194+
195+
font.set_family("Monospace");
196+
font.set_weight(Pango::WEIGHT_BOLD);
197+
198+
// http://developer.gnome.org/pangomm/unstable/classPango_1_1Layout.html
199+
auto layout = create_pango_layout("Calculator Test");
200+
201+
layout->set_font_description(font);
202+
203+
int text_width;
204+
int text_height;
205+
206+
//get the text dimensions (it updates the variables -- by reference)
207+
layout->get_pixel_size(text_width, text_height);
208+
209+
// Position the text in the middle
210+
draw_context_ptr->move_to(
211+
(area_x_full - text_width)
212+
, (1.5 * text_height)
213+
);
214+
215+
layout->show_in_cairo_context(draw_context_ptr);
216+
196217
return PROPAGATE_SIG; // Should propagate to make sure save/restore are processed correctly.
197218
}
198219

calculator_simple/src/app_window.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525

2626
#include <cairomm/context.h>
2727

28+
#include <pangomm/fontdescription.h>
29+
#include <pangomm/fontface.h>
30+
#include <pangomm/layout.h>
31+
2832
#include <array>
2933

3034
#include "logic_calculator.hpp"

0 commit comments

Comments
 (0)