Skip to content

Commit

Permalink
groupbar: unify title rendering
Browse files Browse the repository at this point in the history
moves the text renderer to the unified opengl impl
  • Loading branch information
vaxerski committed Jan 2, 2025
1 parent 365caa4 commit 81721b8
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 73 deletions.
4 changes: 2 additions & 2 deletions src/render/OpenGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2555,12 +2555,12 @@ SP<CTexture> CHyprOpenGLImpl::loadAsset(const std::string& filename) {
return tex;
}

SP<CTexture> CHyprOpenGLImpl::renderText(const std::string& text, CHyprColor col, int pt, bool italic) {
SP<CTexture> CHyprOpenGLImpl::renderText(const std::string& text, CHyprColor col, int pt, bool italic, const std::string& fontFamily) {
SP<CTexture> tex = makeShared<CTexture>();

static auto FONT = CConfigValue<std::string>("misc:font_family");

const auto FONTFAMILY = *FONT;
const auto FONTFAMILY = fontFamily.empty() ? *FONT : fontFamily;
const auto FONTSIZE = pt;
const auto COLOR = col;

Expand Down
2 changes: 1 addition & 1 deletion src/render/OpenGL.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class CHyprOpenGLImpl {
void bindBackOnMain();

SP<CTexture> loadAsset(const std::string& file);
SP<CTexture> renderText(const std::string& text, CHyprColor col, int pt, bool italic = false);
SP<CTexture> renderText(const std::string& text, CHyprColor col, int pt, bool italic = false, const std::string& fontFamily = "");

void setDamage(const CRegion& damage, std::optional<CRegion> finalDamage = {});

Expand Down
75 changes: 7 additions & 68 deletions src/render/decorations/CHyprGroupBarDecoration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ void CHyprGroupBarDecoration::draw(PHLMONITOR pMonitor, float const& a) {
Vector2D{m_fBarWidth * pMonitor->scale, (*PTITLEFONTSIZE + 2L * BAR_TEXT_PAD) * pMonitor->scale},
pMonitor->scale))
.get();
rect.y += (rect.height - pTitleTex->textHeight) / 2.0;
rect.height = pTitleTex->textHeight;
rect.width = pTitleTex->textWidth;
rect.x += (m_fBarWidth * pMonitor->scale) / 2.0 - (pTitleTex->textWidth / 2.0);
rect.y += std::ceil((rect.height - pTitleTex->texSize.y) / 2.0);
rect.height = pTitleTex->texSize.y;
rect.width = pTitleTex->texSize.x;
rect.x += std::round((m_fBarWidth * pMonitor->scale) / 2.0 - (pTitleTex->texSize.x / 2.0));
rect.round();

CTexPassElement::SRenderData data;
Expand Down Expand Up @@ -215,10 +215,6 @@ void CHyprGroupBarDecoration::invalidateTextures() {
}

CTitleTex::CTitleTex(PHLWINDOW pWindow, const Vector2D& bufferSize, const float monitorScale) : szContent(pWindow->m_szTitle), pWindowOwner(pWindow) {
tex = makeShared<CTexture>();
const auto LAYOUTSURFACE = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
const auto LAYOUTCAIRO = cairo_create(LAYOUTSURFACE);

static auto FALLBACKFONT = CConfigValue<std::string>("misc:font_family");
static auto PTITLEFONTFAMILY = CConfigValue<std::string>("group:groupbar:font_family");
static auto PTITLEFONTSIZE = CConfigValue<Hyprlang::INT>("group:groupbar:font_size");
Expand All @@ -227,67 +223,10 @@ CTitleTex::CTitleTex(PHLWINDOW pWindow, const Vector2D& bufferSize, const float
const CHyprColor COLOR = CHyprColor(*PTEXTCOLOR);
const auto FONTFAMILY = *PTITLEFONTFAMILY != STRVAL_EMPTY ? *PTITLEFONTFAMILY : *FALLBACKFONT;

cairo_surface_destroy(LAYOUTSURFACE);

// draw title using Pango
PangoLayout* layout = pango_cairo_create_layout(LAYOUTCAIRO);
pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT);
pango_layout_set_text(layout, szContent.c_str(), -1);

PangoFontDescription* fontDesc = pango_font_description_new();
pango_font_description_set_family_static(fontDesc, FONTFAMILY.c_str());
pango_font_description_set_size(fontDesc, *PTITLEFONTSIZE * PANGO_SCALE * monitorScale);
pango_layout_set_font_description(layout, fontDesc);
pango_font_description_free(fontDesc);

const int maxWidth = bufferSize.x;

pango_layout_set_width(layout, maxWidth * PANGO_SCALE);
pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);

int layoutWidth, layoutHeight;
PangoRectangle inkRect;
PangoRectangle logicalRect;
pango_layout_get_pixel_extents(layout, &inkRect, &logicalRect);
layoutWidth = inkRect.width;
layoutHeight = inkRect.height;

const auto CAIROSURFACE = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, layoutWidth, layoutHeight);
const auto CAIRO = cairo_create(CAIROSURFACE);

// clear the pixmap
cairo_save(CAIRO);
cairo_set_operator(CAIRO, CAIRO_OPERATOR_CLEAR);
cairo_paint(CAIRO);
cairo_restore(CAIRO);
cairo_move_to(CAIRO, -inkRect.x, -inkRect.y);
cairo_set_source_rgba(CAIRO, COLOR.r, COLOR.g, COLOR.b, COLOR.a);
pango_cairo_show_layout(CAIRO, layout);

g_object_unref(layout);

cairo_surface_flush(CAIROSURFACE);

// copy the data to an OpenGL texture we have
const auto DATA = cairo_image_surface_get_data(CAIROSURFACE);
tex->allocate();
glBindTexture(GL_TEXTURE_2D, tex->m_iTexID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

#ifndef GLES2
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_BLUE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED);
#endif
tex = g_pHyprOpenGL->renderText(pWindow->m_szTitle, COLOR, *PTITLEFONTSIZE, false, FONTFAMILY);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layoutWidth, layoutHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, DATA);

// delete cairo
textWidth = layoutWidth;
textHeight = layoutHeight;
cairo_destroy(LAYOUTCAIRO);
cairo_destroy(CAIRO);
cairo_surface_destroy(CAIROSURFACE);
if (tex)
texSize = tex->m_vSize;
}

CTitleTex::~CTitleTex() = default;
Expand Down
3 changes: 1 addition & 2 deletions src/render/decorations/CHyprGroupBarDecoration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ class CTitleTex {

SP<CTexture> tex;
std::string szContent;
int textWidth;
int textHeight;
Vector2D texSize;

PHLWINDOWREF pWindowOwner;
};
Expand Down

0 comments on commit 81721b8

Please sign in to comment.