diff --git a/darwin/drawtext.m b/darwin/drawtext.m index c04b402b..dbcbccd0 100644 --- a/darwin/drawtext.m +++ b/darwin/drawtext.m @@ -212,3 +212,8 @@ void uiDrawTextLayoutExtents(uiDrawTextLayout *tl, double *width, double *height [tl->frame returnWidth:width height:NULL]; [tl->forLines returnWidth:NULL height:height]; } + +uiDrawTextFont *uiDrawLoadDefaultFont() +{ + return mkTextFontFromNSFont([NSFont systemFontOfSize:0]); +} diff --git a/ui.h b/ui.h index b5fb9a27..228230b1 100644 --- a/ui.h +++ b/ui.h @@ -919,6 +919,8 @@ struct uiFontDescriptor { uiTextStretch Stretch; }; +_UI_EXTERN uiDrawTextFont *uiDrawLoadDefaultFont(); + // uiDrawTextLayout is a concrete representation of a // uiAttributedString that can be displayed in a uiDrawContext. // It includes information important for the drawing of a block of diff --git a/unix/drawtext.c b/unix/drawtext.c index 477e9ca3..80ed4194 100644 --- a/unix/drawtext.c +++ b/unix/drawtext.c @@ -79,3 +79,19 @@ void uiDrawTextLayoutExtents(uiDrawTextLayout *tl, double *width, double *height *width = pangoToCairo(logical.width); *height = pangoToCairo(logical.height); } + +uiDrawTextFont *uiDrawLoadDefaultFont() +{ + GtkWidget *widget; + GtkStyleContext *style; + PangoFontDescription *fontdesc; + PangoFont *font; + + widget = g_object_ref_sink(gtk_drawing_area_new()); + style = gtk_widget_get_style_context(widget); + gtk_style_context_get(style, GTK_STATE_FLAG_NORMAL, + "font", &fontdesc, NULL); + font = pangoDescToPangoFont(fontdesc); + g_object_unref(widget); + return mkTextFont(font, FALSE); +} diff --git a/windows/drawtext.cpp b/windows/drawtext.cpp index ec2ae152..09d4c151 100644 --- a/windows/drawtext.cpp +++ b/windows/drawtext.cpp @@ -534,3 +534,47 @@ void uiDrawTextLayoutExtents(uiDrawTextLayout *tl, double *width, double *height // TODO make sure the behavior of this on empty strings is the same on all platforms (ideally should be 0-width, line height-height; TODO note this in the docs too) *height = metrics.height; } + +uiDrawTextFont *uiDrawLoadDefaultFont() +{ + uiDrawTextFont *font; + fontCollection *collection; + IDWriteGdiInterop *gdi; + IDWriteFont *dwfont; + IDWriteFontFamily *dwfamily; + NONCLIENTMETRICS metrics; + WCHAR *family; + double size; + int pixels; + HRESULT hr; + + metrics.cbSize = sizeof(metrics); + if (!SystemParametersInfo(SPI_GETNONCLIENTMETRICS, metrics.cbSize, &metrics, 0)) + logLastError(L"error getting non-client metrics"); + hr = dwfactory->GetGdiInterop(&gdi); + if (hr != S_OK) + logHRESULT(L"error getting GDI interop", hr); + + hr = gdi->CreateFontFromLOGFONT(&metrics.lfMessageFont, &dwfont); + if (hr != S_OK) + logHRESULT(L"error loading font", hr); + + hr = dwfont->GetFontFamily(&dwfamily); + if (hr != S_OK) + logHRESULT(L"error loading font family", hr); + collection = loadFontCollection(); + family = fontCollectionFamilyName(collection, dwfamily); + + pixels = GetDeviceCaps(GetDC(NULL), LOGPIXELSY); + if (pixels == 0) + logLastError(L"error getting device caps"); + size = abs(metrics.lfMessageFont.lfHeight) * 72 / pixels; + + font = mkTextFont(dwfont, FALSE, family, FALSE, size); + + fontCollectionFree(collection); + dwfamily->Release(); + gdi->Release(); + + return font; +}