Updates font management for hidpi

Improvements:
- Cleanup of default options files to use OS font by default
- Comments updates
- Simplify font size management as Pango/Gtk directly manage pt/px conversions

Bug fix:
- If Preference panel is closed by "cancel", font size was not reinitialized to previously saved one
- Font size was specified in "RawTherapee.css" theme and so user choice could be ignored
This commit is contained in:
Pandagrapher 2023-08-23 13:43:07 +02:00
parent 58b7d41694
commit bf988ad274
14 changed files with 99 additions and 197 deletions

View File

@ -3,7 +3,6 @@ file(GLOB SOUNDFILES "sounds/*")
file(GLOB INPUTICCFILES "iccprofiles/input/*")
file(GLOB OUTPUTICCFILES "iccprofiles/output/*")
file(GLOB DCPFILES "dcpprofiles/*")
file(GLOB FONTS "fonts/*")
set(PROFILESDIR "profiles")
set(THEMEDIR "themes")
@ -43,9 +42,6 @@ install(FILES ${INPUTICCFILES} DESTINATION "${DATADIR}/iccprofiles/input")
install(FILES ${OUTPUTICCFILES} DESTINATION "${DATADIR}/iccprofiles/output")
install(FILES ${DCPFILES} DESTINATION "${DATADIR}/dcpprofiles")
install(FILES ${OPTIONSFILE} DESTINATION "${DATADIR}" PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ RENAME options)
if(WIN32)
install(FILES ${FONTS} DESTINATION "${DATADIR}/fonts")
endif()
install(DIRECTORY "${PROFILESDIR}" DESTINATION "${DATADIR}" FILES_MATCHING PATTERN "*.pp3")
install(DIRECTORY "${THEMEDIR}" DESTINATION "${DATADIR}")

View File

@ -37,8 +37,3 @@ CustomProfileBuilder=
# Default profile name (without extension) to use for standard (8bits) images
#ImgDefault=Neutral
[GUI]
# Set the included font as default
FontFamily=Droid Sans Mono Slashed
CPFontFamily=Droid Sans Mono Slashed

View File

@ -26,7 +26,6 @@
* {
color: #BBBBBB;
text-shadow: none; /* Keep at none, or suffer serious performance issues */
font-size: 1em;
}
*:disabled {

View File

@ -62,21 +62,12 @@ CropWindow::CropWindow (ImageArea* parent, bool isLowUpdatePriority_, bool isDet
initZoomSteps();
Glib::RefPtr<Pango::Context> context = parent->get_pango_context () ;
Pango::FontDescription fontd = context->get_font_description ();
Pango::FontDescription fontd = parent->get_style_context()->get_font();
fontd.set_weight (Pango::WEIGHT_BOLD);
const int fontSize = 8; // pt
// Converting font size to "px" based on DPI and scale
#ifndef __APPLE__
const double fontScale = RTScalable::getDPI() / RTScalable::pangoDPI; // Refer to notes in rtscalable.h
#else
// On MacOS, font is already scaled by the System library
// Refer to https://gitlab.gnome.org/GNOME/gtk/-/blob/gtk-3-24/gdk/quartz/gdkscreen-quartz.c
const double fontScale = 1.;
#endif
const double absoluteFontSize = static_cast<double>(fontSize) * fontScale; // px
// Absolute size is defined in "Pango units" and shall be multiplied by
// Pango::SCALE from "px":
fontd.set_absolute_size (absoluteFontSize * static_cast<double>(Pango::SCALE));
// Non-absolute size is defined in "Pango units" and shall be multiplied by
// Pango::SCALE from "pt":
fontd.set_size (fontSize * Pango::SCALE);
context->set_font_description (fontd);
Glib::RefPtr<Pango::Layout> cllayout = parent->create_pango_layout("1000%");
@ -2461,21 +2452,12 @@ void CropWindow::drawDecoration (Cairo::RefPtr<Cairo::Context> cr)
int x = xpos, y = ypos;
// prepare label
Glib::RefPtr<Pango::Context> context = iarea->get_pango_context () ;
Pango::FontDescription fontd = context->get_font_description ();
Pango::FontDescription fontd = iarea->get_style_context()->get_font();
fontd.set_weight (Pango::WEIGHT_BOLD);
const int fontSize = 8; // pt
// Converting font size to "px" based on DPI and scale
#ifndef __APPLE__
const double fontScale = RTScalable::getDPI() / RTScalable::pangoDPI; // Refer to notes in rtscalable.h
#else
// On MacOS, font is already scaled by the System library
// Refer to https://gitlab.gnome.org/GNOME/gtk/-/blob/gtk-3-24/gdk/quartz/gdkscreen-quartz.c
const double fontScale = 1.;
#endif
const double absoluteFontSize = static_cast<double>(fontSize) * fontScale; // px
// Absolute size is defined in "Pango units" and shall be multiplied by
// Pango::SCALE from "px":
fontd.set_absolute_size (absoluteFontSize * static_cast<double>(Pango::SCALE));
// Non-absolute size is defined in "Pango units" and shall be multiplied by
// Pango::SCALE from "pt":
fontd.set_size (fontSize * Pango::SCALE);
context->set_font_description (fontd);
Glib::RefPtr<Pango::Layout> cllayout = iarea->create_pango_layout(cropLabel);
int iw, ih;
@ -2537,21 +2519,12 @@ void CropWindow::drawStraightenGuide (Cairo::RefPtr<Cairo::Context> cr)
}
Glib::RefPtr<Pango::Context> context = iarea->get_pango_context () ;
Pango::FontDescription fontd = context->get_font_description ();
Pango::FontDescription fontd = iarea->get_style_context()->get_font();
fontd.set_weight (Pango::WEIGHT_BOLD);
const int fontSize = 8; // pt
// Converting font size to "px" based on DPI and scale
#ifndef __APPLE__
const double fontScale = RTScalable::getDPI() / RTScalable::pangoDPI; // Refer to notes in rtscalable.h
#else
// On MacOS, font is already scaled by the System library
// Refer to https://gitlab.gnome.org/GNOME/gtk/-/blob/gtk-3-24/gdk/quartz/gdkscreen-quartz.c
const double fontScale = 1.;
#endif
const double absoluteFontSize = static_cast<double>(fontSize) * fontScale; // px
// Absolute size is defined in "Pango units" and shall be multiplied by
// Pango::SCALE from "px":
fontd.set_absolute_size (absoluteFontSize * static_cast<double>(Pango::SCALE));
// Non-absolute size is defined in "Pango units" and shall be multiplied by
// Pango::SCALE from "pt":
fontd.set_size (fontSize * Pango::SCALE);
context->set_font_description (fontd);
Glib::RefPtr<Pango::Layout> deglayout = iarea->create_pango_layout(Glib::ustring::compose ("%1 deg", Glib::ustring::format(std::setprecision(2), rot_deg)));

View File

@ -784,21 +784,12 @@ void FileBrowserEntry::drawStraightenGuide (Cairo::RefPtr<Cairo::Context> cr)
}
Glib::RefPtr<Pango::Context> context = parent->getDrawingArea()->get_pango_context () ;
Pango::FontDescription fontd = context->get_font_description ();
Pango::FontDescription fontd = parent->getDrawingArea()->get_style_context()->get_font();
fontd.set_weight (Pango::WEIGHT_BOLD);
const int fontSize = 8; // pt
// Converting font size to "px" based on DPI and scale
#ifndef __APPLE__
const double fontScale = RTScalable::getDPI() / RTScalable::pangoDPI; // Refer to notes in rtscalable.h
#else
// On MacOS, font is already scaled by the System library
// Refer to https://gitlab.gnome.org/GNOME/gtk/-/blob/gtk-3-24/gdk/quartz/gdkscreen-quartz.c
const double fontScale = 1.;
#endif
const double absoluteFontSize = static_cast<double>(fontSize) * fontScale; // px
// Absolute size is defined in "Pango units" and shall be multiplied by
// Pango::SCALE from "px":
fontd.set_absolute_size (absoluteFontSize * static_cast<double>(Pango::SCALE));
// Non-absolute size is defined in "Pango units" and shall be multiplied by
// Pango::SCALE from "pt":
fontd.set_size (fontSize * Pango::SCALE);
context->set_font_description (fontd);
Glib::RefPtr<Pango::Layout> deglayout = parent->getDrawingArea()->create_pango_layout(Glib::ustring::compose ("%1 deg", Glib::ustring::format(std::setprecision(2), rot_deg)));

View File

@ -525,7 +525,7 @@ void FileCatalog::on_realize()
{
Gtk::Box::on_realize();
Pango::FontDescription fontd = get_pango_context()->get_font_description ();
Pango::FontDescription fontd = get_style_context()->get_font();
fileBrowser->get_pango_context()->set_font_description (fontd);
// batchQueue->get_pango_context()->set_font_description (fontd);
}

View File

@ -156,18 +156,9 @@ void ImageArea::setInfoText (Glib::ustring text)
// update font
fontd.set_weight (Pango::WEIGHT_BOLD);
const int fontSize = 10; // pt
// Converting font size to "px" based on DPI and scale
#ifndef __APPLE__
const double fontScale = RTScalable::getDPI() / RTScalable::pangoDPI; // Refer to notes in rtscalable.h
#else
// On MacOS, font is already scaled by the System library
// Refer to https://gitlab.gnome.org/GNOME/gtk/-/blob/gtk-3-24/gdk/quartz/gdkscreen-quartz.c
const double fontScale = 1.;
#endif
const double absoluteFontSize = static_cast<double>(fontSize) * fontScale; // px
// Absolute size is defined in "Pango units" and shall be multiplied by
// Pango::SCALE from "px":
fontd.set_absolute_size (absoluteFontSize * static_cast<double>(Pango::SCALE));
// Non-absolute size is defined in "Pango units" and shall be multiplied by
// Pango::SCALE from "pt":
fontd.set_size (fontSize * Pango::SCALE);
context->set_font_description (fontd);
// create text layout

View File

@ -53,22 +53,13 @@ void LockableColorPicker::updateBackBuffer ()
Gtk::DrawingArea *iArea = cropWindow->getImageArea();
Glib::RefPtr<Pango::Context> pangoContext = iArea->get_pango_context ();
Pango::FontDescription fontd = pangoContext->get_font_description();
Pango::FontDescription fontd = iArea->get_style_context()->get_font();
// set font family and size
fontd.set_family(options.CPFontFamily == "default" ? "sans" : options.CPFontFamily);
const int fontSize = options.CPFontFamily == "default" ? 8 : options.CPFontSize; // pt
// Converting font size to "px" based on DPI and scale
#ifndef __APPLE__
const double fontScale = RTScalable::getDPI() / RTScalable::pangoDPI; // Refer to notes in rtscalable.h
#else
// On MacOS, font is already scaled by the System library
// Refer to https://gitlab.gnome.org/GNOME/gtk/-/blob/gtk-3-24/gdk/quartz/gdkscreen-quartz.c
const double fontScale = 1.;
#endif
const double absoluteFontSize = static_cast<double>(fontSize) * fontScale; // px
// Absolute size is defined in "Pango units" and shall be multiplied by
// Pango::SCALE from "px":
fontd.set_absolute_size (absoluteFontSize * static_cast<double>(Pango::SCALE));
// Non-absolute size is defined in "Pango units" and shall be multiplied by
// Pango::SCALE from "pt":
fontd.set_size (fontSize * Pango::SCALE);
fontd.set_weight(Pango::WEIGHT_NORMAL);
pangoContext->set_font_description (fontd);

View File

@ -82,9 +82,22 @@ Preferences::Preferences(RTWindow *rtwindow)
set_size_request(650, -1);
set_default_size(options.preferencesWidth, options.preferencesHeight);
Pango::FontDescription defaultFont = get_style_context()->get_font();
initialFontFamily = defaultFont.get_family();
initialFontSize = defaultFont.get_size() / Pango::SCALE;
// Request default font and size from Gtk::Settings
const auto defaultSettings = Gtk::Settings::get_default();
Glib::ustring defaultFont;
defaultSettings->get_property("gtk-font-name", defaultFont);
const Pango::FontDescription defaultFontDesc = Pango::FontDescription(defaultFont);
initialFontFamily = defaultFontDesc.get_family();
#if defined(__APPLE__)
// Default MacOS font (i.e. "") is not correctly handled
// in Gtk css. Replacing it by "-apple-system" to avoid this
if (initialFontFamily == ".AppleSystemUIFont") {
initialFontFamily = "-apple-system";
}
#endif
initialFontSize = defaultFontDesc.get_size() / Pango::SCALE; // Font size is managed in ()"pt" * Pango::SCALE) by Pango (also refer to notes in rtscalable.h)
printf("initialFont: %s %d\n", initialFontFamily.c_str(), initialFontSize);
Gtk::Box* mainBox = get_content_area();
//GTK318
@ -2016,15 +2029,15 @@ void Preferences::fillPreferences()
navGuideColorCB->set_alpha ( (unsigned short) (moptions.navGuideBrush[3] * 65535.0));
if (options.fontFamily == "default") {
mainFontFB->set_font_name (Glib::ustring::compose ("%1 %2", initialFontFamily, initialFontSize));
mainFontFB->set_font_name (Glib::ustring::compose ("%1, %2", initialFontFamily, initialFontSize));
} else {
mainFontFB->set_font_name (Glib::ustring::compose ("%1 %2", options.fontFamily, options.fontSize));
mainFontFB->set_font_name (Glib::ustring::compose ("%1, %2", options.fontFamily, options.fontSize));
}
if (options.CPFontFamily == "default") {
colorPickerFontFB->set_font_name (Glib::ustring::compose ("%1 %2", initialFontFamily, initialFontSize));
colorPickerFontFB->set_font_name (Glib::ustring::compose ("%1, %2", initialFontFamily, initialFontSize));
} else {
colorPickerFontFB->set_font_name (Glib::ustring::compose ("%1 %2", options.CPFontFamily, options.CPFontSize));
colorPickerFontFB->set_font_name (Glib::ustring::compose ("%1, %2", options.CPFontFamily, options.CPFontSize));
}
showDateTime->set_active(moptions.fbShowDateTime);
@ -2243,7 +2256,7 @@ void Preferences::cancelPressed()
// set the initial font back
Pango::FontDescription fd (mainFontFB->get_font_name());
if (fd.get_family() != options.fontFamily && (fd.get_size() / Pango::SCALE) != options.fontSize) {
if (fd.get_family() != options.fontFamily || (fd.get_size() / Pango::SCALE) != options.fontSize) {
if (options.fontFamily == "default") {
switchFontTo(initialFontFamily, initialFontSize);
} else {
@ -2446,6 +2459,7 @@ void Preferences::fontChanged()
{
newFont = true;
Pango::FontDescription fd (mainFontFB->get_font_name());
printf("test: %s\n", mainFontFB->get_font_name().c_str());
switchFontTo(fd.get_family(), fd.get_size() / Pango::SCALE);
}
@ -2457,34 +2471,26 @@ void Preferences::cpFontChanged()
void Preferences::switchFontTo(const Glib::ustring &newFontFamily, const int newFontSize)
{
if (newFontFamily != "default") {
if (!fontcss) {
fontcss = Gtk::CssProvider::create();
Glib::RefPtr<Gdk::Screen> screen = Gdk::Screen::get_default();
Gtk::StyleContext::add_provider_for_screen(screen, fontcss, GTK_STYLE_PROVIDER_PRIORITY_USER);
}
try {
//GTK318
//#if GTK_MAJOR_VERSION == 3 && GTK_MINOR_VERSION < 20
// fontcss->load_from_data (Glib::ustring::compose ("* { font-family: %1; font-size: %2px }", newFontFamily, newFontSize));
//#else
fontcss->load_from_data (Glib::ustring::compose ("* { font-family: %1; font-size: %2pt }", newFontFamily, newFontSize));
//#endif
//GTK318
} catch (Glib::Error &err) {
printf("Error: \"%s\"\n", err.what().c_str());
} catch (...) {
printf("Error: Can't find the font named \"%s\"\n", newFontFamily.c_str());
}
} else {
if (fontcss) {
fontcss = Gtk::CssProvider::create();
Glib::RefPtr<Gdk::Screen> screen = Gdk::Screen::get_default();
Gtk::StyleContext::remove_provider_for_screen(screen, fontcss);
}
// Create CssProvider if not existing
if (!fontcss) {
fontcss = Gtk::CssProvider::create();
Glib::RefPtr<Gdk::Screen> screen = Gdk::Screen::get_default();
Gtk::StyleContext::add_provider_for_screen(screen, fontcss, GTK_STYLE_PROVIDER_PRIORITY_USER);
}
// Create css to load based on new font name and size
const auto css = Glib::ustring::compose ("* { font-family: %1; font-size: %2pt }", newFontFamily, newFontSize);
// Load css to update font name and size
try {
fontcss->load_from_data (css);
} catch (Glib::Error &err) {
printf("Error: \"%s\"\n", err.what().c_str());
} catch (...) {
printf("Error: Can't load the desired font correctly\n");
}
printf("switchFontTo: %s\n", css.c_str());
}
void Preferences::workflowUpdate()

View File

@ -34,6 +34,11 @@
* - Non-absolute size (i.e. "pt"): The default resolution is 72 DPI (i.e. pt per inch). To
* convert the size to "px", use the following formula:
* "size in px" = "size in pt" * ("device resolution" / 72)
* Notes:
* - By default, size is expressed in non-absolute size (i.e. "pt"). Conversion between absolute
* and non-absolute size is ensured by Pango.
* - On MacOS, font is already scaled by the System library (i.e. "size in px" = "size in pt" * 1.).
* Refer to https://gitlab.gnome.org/GNOME/gtk/-/blob/gtk-3-24/gdk/quartz/gdkscreen-quartz.c
*
* Hi-DPI implementation according to the OS (source: GDK code):
* - Windows: A default DPI of 96 is considered. Current DPI parameter is provided by the OS.

View File

@ -139,71 +139,35 @@ RTWindow::RTWindow ()
// Set the font face and size
Glib::ustring css;
#ifndef __APPLE__
const double fontScale = RTScalable::getDPI() / RTScalable::pangoDPI; // Refer to notes in rtscalable.h
#else
// On MacOS, font is already scaled by the System library
// Refer to https://gitlab.gnome.org/GNOME/gtk/-/blob/gtk-3-24/gdk/quartz/gdkscreen-quartz.c
const double fontScale = 1.;
#endif
auto style = get_pango_context();
if (options.fontFamily != "default") { // Set font and size according to user choice
// Scale font size based on DPI and Scale
const int scaledFontSize = static_cast<int>(options.fontSize * fontScale + 0.5);
// Set font and size in css from options
css = Glib::ustring::compose ("* { font-family: %1; font-size: %2pt}",
options.fontFamily,
options.fontSize); // Font size is in "pt" in options
} else { // Set font and size according to default values
// Retrieve default style values from Gtk::Settings
const auto defaultSettings = Gtk::Settings::get_default();
Glib::ustring defaultFont;
defaultSettings->get_property("gtk-font-name", defaultFont);
const Pango::FontDescription defaultFontDesc = Pango::FontDescription(defaultFont);
// Set font and size in css
css = Glib::ustring::compose ("* { font-family: %1; font-size: %2px}", options.fontFamily, scaledFontSize);
if (rtengine::settings->verbose) {
printf("\"Non-Default\" font size in pt(%d) * scale(%.3f) = font size in px(%d)\n", options.fontSize, fontScale, scaledFontSize);
auto defaultFontFamily = defaultFontDesc.get_family();
const int defaultFontSize = defaultFontDesc.get_size() / Pango::SCALE; // Font size is managed in ()"pt" * Pango::SCALE) by Pango (also refer to notes in rtscalable.h)
#if defined(__APPLE__)
// Default MacOS font (i.e. "") is not correctly handled
// in Gtk css. Replacing it by "-apple-system" to avoid this
if (defaultFontFamily == ".AppleSystemUIFont") {
defaultFontFamily = "-apple-system";
}
} else { // Set font and size according to default values
// Retrieve default style values
Glib::RefPtr<Gtk::StyleContext> style = Gtk::StyleContext::create();
Pango::FontDescription pfd = style->get_font(Gtk::STATE_FLAG_NORMAL);
if (pfd.get_set_fields() & Pango::FONT_MASK_SIZE) {
int fontSize = pfd.get_size();
const bool isAbsoluteFontSize = pfd.get_size_is_absolute();
int newFontSize;
if (isAbsoluteFontSize) {
// Absolute size is defined in "Pango units" and shall be divided by
// Pango::SCALE to get "px"
fontSize = fontSize / Pango::SCALE;
#ifndef __APPLE__
// Guessing that pixel size is given for a 96 DPI reference:
const double newFontScale = RTScalable::getDPI() / RTScalable::baseDPI; // Refer to notes in rtscalable.h
#else
// On MacOS, font is already scaled by the System library
// Refer to https://gitlab.gnome.org/GNOME/gtk/-/blob/gtk-3-24/gdk/quartz/gdkscreen-quartz.c
const double newFontScale = 1.;
#endif
newFontSize = static_cast<int>(fontSize * newFontScale + 0.5);
if (rtengine::settings->verbose) {
printf("\"Default\" absolute font size(%d)\n", newFontSize);
}
} else {
// Non-absolute size is defined in "Pango units" and shall be divided by
// Pango::SCALE to get "px"
fontSize = fontSize / Pango::SCALE;
// Non-absolute size is defined in "pt" and shall be converted to "px"
newFontSize = static_cast<int>(fontSize * fontScale + 0.5);
if (rtengine::settings->verbose) {
printf("\"Default\" non-absolute font size in pt(%d) * scale(%.3f) = font size in px(%d)\n", fontSize, fontScale, newFontSize);
}
}
// Set font and size in css
css = Glib::ustring::compose ("* { font-size: %1px}", newFontSize);
}
css = Glib::ustring::compose ("* { font-family: %1; font-size: %2pt}",
defaultFontFamily,
defaultFontSize);
}
printf("test : %s\n", css.c_str());
// Load custom CSS for font
if (!css.empty()) {
if (rtengine::settings->verbose) {
@ -218,7 +182,7 @@ RTWindow::RTWindow ()
} catch (Glib::Error &err) {
printf ("Error: \"%s\"\n", err.what().c_str());
} catch (...) {
printf ("Error: Can't find the font named \"%s\"\n", options.fontFamily.c_str());
printf ("Error: Can't load the desired font correctly\n");
}
}
}

View File

@ -41,21 +41,12 @@ bool SplashImage::on_draw(const ::Cairo::RefPtr< Cairo::Context> &cr)
cfo.set_antialias (Cairo::ANTIALIAS_SUBPIXEL);
Glib::RefPtr<Pango::Context> context = get_pango_context ();
context->set_cairo_font_options (cfo);
Pango::FontDescription fontd = context->get_font_description ();
Pango::FontDescription fontd = get_style_context()->get_font();
fontd.set_weight (Pango::WEIGHT_LIGHT);
const int fontSize = 12; // pt
// Converting font size to "px" based on DPI and scale
#ifndef __APPLE__
const double fontScale = RTScalable::getDPI() / RTScalable::pangoDPI; // Refer to notes in rtscalable.h
#else
// On MacOS, font is already scaled by the System library
// Refer to https://gitlab.gnome.org/GNOME/gtk/-/blob/gtk-3-24/gdk/quartz/gdkscreen-quartz.c
const double fontScale = 1.;
#endif
const double absoluteFontSize = static_cast<double>(fontSize) * fontScale; // px
// Absolute size is defined in "Pango units" and shall be multiplied by
// Pango::SCALE from "px":
fontd.set_absolute_size (absoluteFontSize * static_cast<double>(Pango::SCALE));
// Non-absolute size is defined in "Pango units" and shall be multiplied by
// Pango::SCALE from "pt":
fontd.set_size(fontSize * Pango::SCALE);
context->set_font_description (fontd);
int w, h;

View File

@ -376,7 +376,7 @@ void ThumbBrowserEntryBase::updateBackBuffer ()
// draw file name
Glib::RefPtr<Pango::Context> context = w->get_pango_context () ;
Pango::FontDescription fontd = context->get_font_description ();
Pango::FontDescription fontd = w->get_style_context()->get_font();
fontd.set_weight (Pango::WEIGHT_BOLD);
if (italicstyle) {
@ -442,7 +442,7 @@ void ThumbBrowserEntryBase::getTextSizes (int& infow, int& infoh)
// filename:
Pango::FontDescription fontd = context->get_font_description ();
Pango::FontDescription fontd = w->get_style_context()->get_font();
fontd.set_weight (Pango::WEIGHT_BOLD);
context->set_font_description (fontd);
Glib::RefPtr<Pango::Layout> fn = w->create_pango_layout(dispname);