Last active
September 25, 2018 18:51
-
-
Save changkun/8564a2f08e56d1f7d92ee8304731a95c to your computer and use it in GitHub Desktop.
Test pango
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| /* | |
| #cgo CFLAGS: -I/usr/local/include/pango-1.0 | |
| #cgo CFLAGS: -I/usr/local/include/glib-2.0 | |
| #cgo CFLAGS: -I/usr/local/include/cairo | |
| #cgo LDFLAGS: -L/usr/local/lib -lpangocairo-1.0 -lpango-1.0 -lcairo -lgobject-2.0 -lglib-2.0 | |
| #include <pango/pangocairo.h> | |
| #include <stdio.h> | |
| #include <pthread.h> | |
| void hello() { | |
| printf("threa id: %li\n", (unsigned long int)pthread_self()); | |
| PangoFontMap* font_map = pango_cairo_font_map_get_default(); | |
| PangoFontDescription* font_desc = pango_font_description_new(); | |
| pango_font_description_set_family(font_desc, "monospace"); | |
| pango_font_description_set_weight(font_desc, PANGO_WEIGHT_NORMAL); | |
| pango_font_description_set_size(font_desc, 20 * PANGO_SCALE * 700 / 96); | |
| PangoContext* context = pango_font_map_create_context(font_map); | |
| PangoFont* font = pango_font_map_load_font(font_map, context, font_desc); | |
| PangoFontMetrics* metrics = pango_font_get_metrics(font, NULL); | |
| int width = pango_font_metrics_get_approximate_digit_width(metrics) / PANGO_SCALE; | |
| int height = (pango_font_metrics_get_descent(metrics) | |
| + pango_font_metrics_get_ascent(metrics)) / PANGO_SCALE; | |
| printf("%d, %d\n", width, height); | |
| cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, width, height); | |
| cairo_t* cairo = cairo_create(surface); | |
| cairo_set_source_rgb(cairo, 155 / 255.0, 155 / 255.0, 155 / 255.0); | |
| cairo_rectangle(cairo, 0, 0, width, height); | |
| PangoLayout* layout = pango_cairo_create_layout(cairo); | |
| pango_layout_set_font_description(layout, font_desc); | |
| g_object_unref(layout); | |
| cairo_destroy(cairo); | |
| cairo_surface_destroy(surface); | |
| } | |
| void* font() { | |
| hello(); | |
| hello(); | |
| return NULL; | |
| } | |
| void two_threads() { | |
| pthread_t thread1; | |
| pthread_create(&thread1, NULL, font, NULL); | |
| pthread_join(thread1, NULL); | |
| pthread_t thread2; | |
| pthread_create(&thread2, NULL, font, NULL); | |
| pthread_join(thread2, NULL); | |
| } | |
| */ | |
| import "C" | |
| import "sync" | |
| func main() { | |
| wg := sync.WaitGroup{} | |
| for i := 0; i < 1; i++ { | |
| wg.Add(1) | |
| go func() { | |
| C.two_threads() | |
| wg.Done() | |
| }() | |
| } | |
| wg.Wait() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment