Skip to content

Commit

Permalink
Add base for pango cairo
Browse files Browse the repository at this point in the history
  • Loading branch information
DevNils committed Feb 16, 2018
1 parent 4435fdd commit 5a211c4
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 52 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pkg_search_module(FFMPEG_AVRESAMPLE libavresample)
set(Python_ADDITIONAL_VERSIONS 2.7)
find_package(PythonLibs REQUIRED)
pkg_search_module(PANGOFT2 REQUIRED pangoft2)
pkg_search_module(PANGOCAIRO REQUIRED pangocairo)
pkg_search_module(RSVG REQUIRED librsvg-2.0)
pkg_search_module(FONTCONFIG REQUIRED fontconfig)
find_package(RT)
Expand Down Expand Up @@ -92,6 +93,7 @@ endif()
if(${PLATFORM_LINUX})
if(NOT ${AVG_ENABLE_EGL})
set(AVG_ENABLE_GLX_X11 TRUE)
set(RSVG_LDFLAGS "${RSVG_LDFLAGS} -lpangocairo-1.0")
endif()
endif()

Expand Down
48 changes: 19 additions & 29 deletions src/player/TextEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,13 @@
#include "../base/StringHelper.h"

#include <algorithm>
#include <cairo.h>
#include <pango/pangocairo.h>

namespace avg {

using namespace std;

static void
text_subst_func_hint(FcPattern *pattern, gpointer data)
{
FcPatternAddBool(pattern, FC_HINTING, true);
FcPatternAddInteger(pattern, FC_HINT_STYLE, FC_HINT_MEDIUM);
FcPatternAddInteger(pattern, FC_RGBA, FC_RGBA_NONE);
FcPatternAddBool(pattern, FC_ANTIALIAS, true);
}

static void
text_subst_func_nohint(FcPattern *pattern, gpointer data)
{
FcPatternAddBool(pattern, FC_HINTING, false);
FcPatternAddBool(pattern, FC_AUTOHINT, false);
FcPatternAddInteger(pattern, FC_HINT_STYLE, FC_HINT_NONE);
FcPatternAddInteger(pattern, FC_RGBA, FC_RGBA_NONE);
FcPatternAddBool(pattern, FC_ANTIALIAS, true);
}

TextEngine& TextEngine::get(bool bHint)
{
if (bHint) {
Expand All @@ -78,21 +61,28 @@ TextEngine::~TextEngine()

void TextEngine::init()
{
m_pFontMap = PANGO_FT2_FONT_MAP(pango_ft2_font_map_new());
pango_ft2_font_map_set_resolution(m_pFontMap, 72, 72);
if (m_bHint) {
pango_ft2_font_map_set_default_substitute(m_pFontMap, text_subst_func_hint,
0, 0);
} else {
pango_ft2_font_map_set_default_substitute(m_pFontMap, text_subst_func_nohint,
0, 0);
}
m_pFontMap = pango_cairo_font_map_new();
pango_cairo_font_map_set_resolution((PangoCairoFontMap*)m_pFontMap, 72.0);

#if PANGO_VERSION > PANGO_VERSION_ENCODE(1,22,0)
m_pPangoContext = pango_font_map_create_context(PANGO_FONT_MAP(m_pFontMap));
#else
m_pPangoContext = pango_ft2_font_map_create_context(m_pFontMap);
m_pPangoContext = pango_cairo_font_map_create_context((PangoCairoFontMap*)m_pFontMap);
#endif

cairo_font_options_t *pFontOptions;
pFontOptions = cairo_font_options_create();
cairo_font_options_set_antialias(pFontOptions, CAIRO_ANTIALIAS_DEFAULT);
if (m_bHint) {
cairo_font_options_set_hint_style(pFontOptions,
CAIRO_HINT_STYLE_MEDIUM);
} else {
cairo_font_options_set_hint_style(pFontOptions,
CAIRO_HINT_STYLE_NONE);
}
pango_cairo_context_set_font_options(m_pPangoContext, pFontOptions);
cairo_font_options_destroy(pFontOptions);

pango_context_set_language(m_pPangoContext,
pango_language_from_string ("en_US"));
pango_context_set_base_dir(m_pPangoContext, PANGO_DIRECTION_LTR);
Expand Down
4 changes: 1 addition & 3 deletions src/player/TextEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#define _TextEngine_H_

#include <pango/pango.h>
#include <pango/pangoft2.h>
#include <fontconfig/fontconfig.h>

#include <vector>
Expand All @@ -46,7 +45,6 @@ class TextEngine {

PangoFontDescription * getFontDescription(const std::string& sFamily,
const std::string& sVariant);
void FT2SubstituteFunc(FcPattern *pattern, gpointer data);

private:
TextEngine(bool bHint);
Expand All @@ -59,7 +57,7 @@ class TextEngine {

bool m_bHint;
PangoContext * m_pPangoContext;
PangoFT2FontMap * m_pFontMap;
PangoFontMap * m_pFontMap;
std::set<std::string> m_sFontsNotFound;
std::set<std::pair<std::string, std::string> > m_VariantsNotFound;
int m_NumFontFamilies;
Expand Down
45 changes: 31 additions & 14 deletions src/player/WordsNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@
#include "../base/ObjectCounter.h"

#include "../graphics/Filterfill.h"
#include "../graphics/Filterfliprgb.h"
#include "../graphics/FilterUnmultiplyAlpha.h"
#include "../graphics/BitmapLoader.h"
#include "../graphics/GLContext.h"
#include "../graphics/GLContextManager.h"
#include "../graphics/GLTexture.h"
#include "../graphics/TextureMover.h"

#include <pango/pangoft2.h>
#include <pango/pangocairo.h>

#include <iostream>
#include <algorithm>
Expand Down Expand Up @@ -544,6 +547,8 @@ void WordsNode::updateLayout()
}
m_pLayout = pango_layout_new(pContext);

pango_layout_set_font_description(m_pLayout, m_pFontDescription);

PangoAttrList * pAttrList = 0;
#if PANGO_VERSION > PANGO_VERSION_ENCODE(1,18,2)
PangoAttribute * pLetterSpacing = pango_attr_letter_spacing_new
Expand Down Expand Up @@ -639,21 +644,26 @@ void WordsNode::renderText()
+ toString(m_InkSize) + ", max=" + toString(maxTexSize) + ")");
}

BitmapPtr pBmp(new Bitmap(m_InkSize, A8));
FilterFill<unsigned char>(0).applyInPlace(pBmp);
FT_Bitmap bitmap;
bitmap.rows = m_InkSize.y;
bitmap.width = m_InkSize.x;
unsigned char * pLines = pBmp->getPixels();
bitmap.pitch = pBmp->getStride();
bitmap.buffer = pLines;
bitmap.num_grays = 256;
bitmap.pixel_mode = ft_pixel_mode_grays;
BitmapPtr pBmp(new Bitmap(m_InkSize, B8G8R8A8));
FilterFill<Pixel32>(Pixel32(0,0,0,0)).applyInPlace(pBmp);

cairo_surface_t* pSurface;
pSurface = cairo_image_surface_create_for_data(pBmp->getPixels(),
CAIRO_FORMAT_ARGB32, m_InkSize.x, m_InkSize.y,
pBmp->getStride());
cairo_t* pCairo = cairo_create(pSurface);

PangoRectangle logical_rect;
PangoRectangle ink_rect;
pango_layout_get_pixel_extents(m_pLayout, &ink_rect, &logical_rect);
pango_ft2_render_layout(&bitmap, m_pLayout, -ink_rect.x, -ink_rect.y);
Color color = m_FontStyle.getColor();
cairo_set_source_rgb(pCairo,
color.getR() / 255.0,
color.getG() / 255.0,
color.getB() / 255.0);
cairo_translate(pCairo, -m_InkOffset.x, -m_InkOffset.y);
pango_cairo_show_layout (pCairo, m_pLayout);

switch (m_FontStyle.getAlignmentVal()) {
case PANGO_ALIGN_LEFT:
m_AlignOffset = 0;
Expand All @@ -667,12 +677,19 @@ void WordsNode::renderText()
default:
AVG_ASSERT(false);
}
setRenderColor(m_FontStyle.getColor());

FilterUnmultiplyAlpha().applyInPlace(pBmp);
if (!BitmapLoader::get()->isBlueFirst()) {
FilterFlipRGB().applyInPlace(pBmp);
}

GLContextManager* pCM = GLContextManager::get();
MCTexturePtr pTex = pCM->createTextureFromBmp(pBmp);
getSurface()->create(A8, pTex);
getSurface()->create(B8G8R8A8, pTex);
newSurface();

cairo_destroy(pCairo);
cairo_surface_destroy(pSurface);
}
m_bRenderNeeded = false;
}
Expand Down
12 changes: 6 additions & 6 deletions src/test/WordsTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,13 @@ def checkPositions():
# print node0.getGlyphPos(i)
noHint = root.getChild(0)
hint = root.getChild(1)
posNoHint = noHint.getGlyphPos(6)
posHint = hint.getGlyphPos(6)
posNoHint = noHint.getGlyphPos(16)
posHint = hint.getGlyphPos(16)
self.assertNotEqual(posNoHint, posHint)
noHint.hint = True
hint.hint = False
self.assertEqual(posNoHint, hint.getGlyphPos(6))
self.assertEqual(posHint, noHint.getGlyphPos(6))
self.assertEqual(posNoHint, hint.getGlyphPos(16))
self.assertEqual(posHint, noHint.getGlyphPos(16))

if platform.system() == "Linux":
self.skip("Linux support requires modified font config")
Expand Down Expand Up @@ -562,9 +562,9 @@ def onMouse(event):

lambda: click((81-centerWidth/2,80)),
lambda: self.assert_(testInside(True)),
lambda: click((80-centerWidth/2,80)),
lambda: click((79-centerWidth/2,80)),
lambda: self.assert_(testInside(False)),
lambda: click((80+centerWidth/2,80)),
lambda: click((79+centerWidth/2,80)),
lambda: self.assert_(testInside(True)),
lambda: click((81+centerWidth/2,80)),
lambda: self.assert_(testInside(False)),
Expand Down

0 comments on commit 5a211c4

Please sign in to comment.