Basic pango.

This commit is contained in:
Jari Vetoniemi 2014-11-23 21:19:40 +02:00
parent 20189fb8d2
commit 9baca87123
5 changed files with 103 additions and 15 deletions

47
CMake/FindPango.cmake Normal file
View File

@ -0,0 +1,47 @@
# - Try to find the pango library
# Once done this will define
#
# PANGO_FOUND - system has pango
# PANGO_INCLUDE_DIRS - the pango include directory
# PANGO_LIBRARIES - Link these to use pango
#
# Define PANGO_MIN_VERSION for which version desired.
#
INCLUDE(FindPkgConfig)
IF(Pango_FIND_REQUIRED)
SET(_pkgconfig_REQUIRED "REQUIRED")
ELSE(Pango_FIND_REQUIRED)
SET(_pkgconfig_REQUIRED "")
ENDIF(Pango_FIND_REQUIRED)
IF(PANGO_MIN_VERSION)
PKG_SEARCH_MODULE(PANGO ${_pkgconfig_REQUIRED} "pango>=${PANGO_MIN_VERSION} pangocairo>=${PANGO_MIN_VERSION}")
ELSE(PANGO_MIN_VERSION)
PKG_SEARCH_MODULE(PANGO ${_pkgconfig_REQUIRED} "pango pangocairo")
ENDIF(PANGO_MIN_VERSION)
IF(NOT PANGO_FOUND AND NOT PKG_CONFIG_FOUND)
FIND_PATH(PANGO_INCLUDE_DIRS pango.h)
FIND_LIBRARY(PANGO_LIBRARIES pango pangocairo)
# Report results
IF(PANGO_LIBRARIES AND PANGO_INCLUDE_DIRS)
SET(PANGO_FOUND 1)
IF(NOT Pango_FIND_QUIETLY)
MESSAGE(STATUS "Found Pango: ${PANGO_LIBRARIES}")
ENDIF(NOT Pango_FIND_QUIETLY)
ELSE(PANGO_LIBRARIES AND PANGO_INCLUDE_DIRS)
IF(Pango_FIND_REQUIRED)
MESSAGE(SEND_ERROR "Could not find Pango")
ELSE(Pango_FIND_REQUIRED)
IF(NOT Pango_FIND_QUIETLY)
MESSAGE(STATUS "Could not find Pango")
ENDIF(NOT Pango_FIND_QUIETLY)
ENDIF(Pango_FIND_REQUIRED)
ENDIF(PANGO_LIBRARIES AND PANGO_INCLUDE_DIRS)
ENDIF(NOT PANGO_FOUND AND NOT PKG_CONFIG_FOUND)
# Hide advanced variables from CMake GUIs
MARK_AS_ADVANCED(PANGO_LIBRARIES PANGO_INCLUDE_DIRS)

View File

@ -3,6 +3,8 @@ SET(RENDERERS
"wayland"
)
ADD_DEFINITIONS(-DPANGO_DISABLE_DEPRECATED)
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/renderers)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/..)

View File

@ -5,6 +5,7 @@
#include <string.h>
#include <assert.h>
#include <cairo/cairo.h>
#include <pango/pangocairo.h>
#ifndef MAX
# define MAX(a,b) (((a)>(b))?(a):(b))
@ -17,6 +18,7 @@
struct cairo {
cairo_t *cr;
cairo_surface_t *surface;
PangoContext *pango;
};
struct cairo_color {
@ -46,6 +48,34 @@ struct cairo_result {
static size_t blen = 0;
static char *buffer = NULL;
__attribute__((unused)) static bool
bm_cairo_create_for_surface(struct cairo *cairo, cairo_surface_t *surface)
{
assert(cairo && surface);
if (!(cairo->cr = cairo_create(surface)))
goto fail;
if (!(cairo->pango = pango_cairo_create_context(cairo->cr)))
goto fail;
cairo->surface = surface;
return true;
fail:
if (cairo->cr)
cairo_destroy(cairo->cr);
return false;
}
__attribute__((unused)) static void
bm_cairo_destroy(struct cairo *cairo)
{
if (cairo->cr)
cairo_destroy(cairo->cr);
if (cairo->surface)
cairo_surface_destroy(cairo->surface);
}
__attribute__((unused)) static void
bm_cairo_get_font_extents(struct cairo *cairo, const struct bm_font *font, cairo_font_extents_t *fe)
{
@ -87,21 +117,32 @@ bm_cairo_draw_line(struct cairo *cairo, struct cairo_paint *paint, struct cairo_
if (!ret)
return false;
cairo_text_extents_t te;
cairo_text_extents(cairo->cr, buffer, &te);
PangoLayout *layout = pango_cairo_create_layout(cairo->cr);
pango_layout_set_text(layout, buffer, -1);
PangoFontDescription *desc = pango_font_description_from_string("Terminus 12");
pango_layout_set_font_description(layout, desc);
pango_font_description_free(desc);
pango_cairo_update_layout(cairo->cr, layout);
int width, height;
pango_layout_get_size(layout, &width, &height);
cairo_set_source_rgba(cairo->cr, paint->bg.r, paint->bg.b, paint->bg.g, paint->bg.a);
cairo_rectangle(cairo->cr,
paint->pos.x - paint->box.lx, paint->pos.y - paint->box.ty,
(paint->box.w > 0 ? paint->box.w : te.width) + paint->box.rx + paint->box.lx,
(paint->box.w > 0 ? paint->box.w : width / PANGO_SCALE) + paint->box.rx + paint->box.lx,
(paint->box.h > 0 ? paint->box.h : paint->fe.height) + paint->box.by + paint->box.ty);
cairo_fill(cairo->cr);
cairo_set_source_rgba(cairo->cr, paint->fg.r, paint->fg.b, paint->fg.g, paint->fg.a);
cairo_move_to(cairo->cr, paint->box.lx + paint->pos.x, paint->pos.y + paint->fe.descent + paint->fe.height * 0.5 + paint->box.ty);
cairo_show_text(cairo->cr, buffer);
cairo_move_to(cairo->cr, paint->box.lx + paint->pos.x, paint->pos.y - ((height * 0.25) / PANGO_SCALE) + paint->box.ty);
pango_cairo_show_layout(cairo->cr, layout);
g_object_unref(layout);
te.x_advance += paint->box.rx;
cairo_text_extents_t te;
cairo_text_extents(cairo->cr, buffer, &te);
te.x_advance = width / PANGO_SCALE + paint->box.rx;
memcpy(&result->te, &te, sizeof(te));
return true;
}

View File

@ -2,12 +2,13 @@ FIND_PACKAGE(Wayland)
if (WAYLAND_FOUND)
FIND_PACKAGE(Cairo REQUIRED)
FIND_PACKAGE(Pango REQUIRED)
FIND_PACKAGE(XKBCommon REQUIRED)
INCLUDE(Wayland)
WAYLAND_ADD_PROTOCOL_CLIENT(proto-xdg-shell "xdg-shell.xml" xdg-shell)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR} ${WAYLAND_CLIENT_INCLUDE_DIR} ${XKBCOMMON_INCLUDE_DIR} ${CAIRO_INCLUDE_DIRECTORIES})
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR} ${WAYLAND_CLIENT_INCLUDE_DIR} ${XKBCOMMON_INCLUDE_DIR} ${CAIRO_INCLUDE_DIRS} ${PANGO_INCLUDE_DIRS})
ADD_LIBRARY(bemenu-renderer-wayland SHARED wayland.c registry.c window.c ${proto-xdg-shell})
SET_TARGET_PROPERTIES(bemenu-renderer-wayland PROPERTIES PREFIX "")
TARGET_LINK_LIBRARIES(bemenu-renderer-wayland ${WAYLAND_CLIENT_LIBRARIES} ${XKBCOMMON_LIBRARIES} ${CAIRO_LIBRARIES})
TARGET_LINK_LIBRARIES(bemenu-renderer-wayland ${WAYLAND_CLIENT_LIBRARIES} ${XKBCOMMON_LIBRARIES} ${CAIRO_LIBRARIES} ${PANGO_LIBRARIES})
INSTALL(TARGETS bemenu-renderer-wayland DESTINATION lib/bemenu)
endif ()

View File

@ -105,11 +105,7 @@ destroy_buffer(struct buffer *buffer)
{
if (buffer->buffer)
wl_buffer_destroy(buffer->buffer);
if (buffer->cairo.cr)
cairo_destroy(buffer->cairo.cr);
if (buffer->cairo.surface)
cairo_surface_destroy(buffer->cairo.surface);
bm_cairo_destroy(&buffer->cairo);
memset(buffer, 0, sizeof(struct buffer));
}
@ -149,10 +145,11 @@ create_buffer(struct wl_shm *shm, struct buffer *buffer, int32_t width, int32_t
wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer);
if (!(buffer->cairo.surface = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, width, height, stride)))
cairo_surface_t *surf;
if (!(surf = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, width, height, stride)))
goto fail;
if (!(buffer->cairo.cr = cairo_create(buffer->cairo.surface)))
if (!bm_cairo_create_for_surface(&buffer->cairo, surf))
goto fail;
buffer->width = width;