Skip to content

Commit

Permalink
Fixed|libgui: Font sizes (points vs. pixels)
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Sep 1, 2019
1 parent 5cc4995 commit c672b05
Show file tree
Hide file tree
Showing 15 changed files with 314 additions and 203 deletions.
Expand Up @@ -53,14 +53,14 @@ group {

font default {
family: Source Sans Pro
size $: gui.scale("12pt", DisplayMode.PIXEL_RATIO)
size: 12pt
weight: normal
style: normal
}

font monospace inherits default {
family: Source Code Pro
size $: gui.scale("9pt", DisplayMode.PIXEL_RATIO)
size: 9pt
}
}

Expand All @@ -70,28 +70,23 @@ group {
script {
# Define mappings for native font styles and weights.
import App
if Version.OS == 'macx'
prefix = '.SFNS'
else
prefix = '.SFUI'
end
App.addFontMapping("SF UI Text", {
['regular', 25]: prefix + 'Text-Light',
['regular', 50]: prefix + 'Text-Regular',
['regular', 75]: prefix + 'Text-Bold',
['italic', 25]: prefix + 'Text-LightItalic',
['italic', 50]: prefix + 'Text-Italic',
['italic', 75]: prefix + 'Text-BoldItalic'
})
App.addFontMapping("SF UI Display", {
['regular', 25]: prefix + 'Display-Thin',
['regular', 50]: prefix + 'Display-Regular',
['regular', 75]: prefix + 'Display-Bold',
['italic', 25]: prefix + 'Text-LightItalic', # Only non-italic available.
['italic', 50]: prefix + 'Text-Italic',
['italic', 75]: prefix + 'Text-BoldItalic'
App.addFontMapping("Apple UI", {
['regular', 25]: '#system-light',
['regular', 50]: '#system',
['regular', 75]: '#system-bold',
['italic', 25]: '#system-italic-light',
['italic', 50]: '#system-italic',
['italic', 75]: '#system-italic-bold'
})
App.addFontMapping("Menlo", {
#App.addFontMapping("SF UI Display", {
# ['regular', 25]: prefix + 'Display-Thin',
# ['regular', 50]: prefix + 'Display-Regular',
# ['regular', 75]: prefix + 'Display-Bold',
# ['italic', 25]: prefix + 'Text-LightItalic', # Only non-italic available.
# ['italic', 50]: prefix + 'Text-Italic',
# ['italic', 75]: prefix + 'Text-BoldItalic'
#})
App.addFontMapping("Apple Mono", {
['regular', 25]: 'Menlo-Regular',
['regular', 50]: 'Menlo-Regular',
['regular', 75]: 'Menlo-Bold',
Expand All @@ -102,15 +97,15 @@ group {
}

font default {
family: SF UI Text
size $: gui.scale('16pt', DisplayMode.PIXEL_RATIO)
family: Apple UI
size: 16pt
weight: normal
style: normal
}

font monospace inherits default {
family: Menlo
size $: gui.scale('12pt', DisplayMode.PIXEL_RATIO)
family: Apple Mono
size: 12pt
}
}

Expand All @@ -123,13 +118,13 @@ font heading inherits title {
size $: gui.scale(default.size, 1.2)
}

script {
if Version.OS in ['macx', 'ios']
# Use the larger variant.
title.family = "SF UI Display"
heading.family = "SF UI Display"
end
}
# script {
# if Version.OS in ['macx', 'ios']
# # Use the larger variant.
# title.family = "SF UI Display"
# heading.family = "SF UI Display"
# end
# }

font small inherits default {
size $: gui.scale(self.size, 0.75)
Expand Down
2 changes: 1 addition & 1 deletion doomsday/libs/gui/CMakeLists.txt
Expand Up @@ -28,7 +28,7 @@ add_definitions (-D__LIBGUI__=1)

# Source and header files.
file (GLOB_RECURSE HEADERS include/de/*)
file (GLOB SOURCES src/stb_impl.c src/*.cpp src/*.h src/input/*.c)
file (GLOB SOURCES src/stb_impl.c src/*.cpp src/*.h src/input/*.c src/text/*.m)

deng_merge_sources (audio src/audio/*.cpp)
deng_merge_sources (dialogs src/dialogs/*.cpp)
Expand Down
10 changes: 5 additions & 5 deletions doomsday/libs/gui/include/de/text/font.h
Expand Up @@ -36,15 +36,15 @@ namespace de {
struct FontParams
{
String family;
float size; // points
float pointSize;
NativeFont::Spec spec;

FontParams();
FontParams(const NativeFont &font);

bool operator==(FontParams const &other) const
bool operator==(const FontParams &other) const
{
return fequal(size, other.size) && spec == other.spec && family == other.family;
return fequal(pointSize, other.pointSize) && spec == other.spec && family == other.family;
}
};
/**
Expand Down Expand Up @@ -292,7 +292,7 @@ class LIBGUI_PUBLIC Font
*
* @return Rectangle covered by the text.
*/
Rectanglei measure(String const &textLine) const;
Rectanglei measure(const String &textLine) const;

/**
* Determines the size of the given line of rich text, i.e., how large an
Expand All @@ -303,7 +303,7 @@ class LIBGUI_PUBLIC Font
*
* @return Rectangle covered by the text.
*/
Rectanglei measure(RichFormatRef const &format) const;
Rectanglei measure(const RichFormatRef &format) const;

/**
* Returns the advance width of a line of text. This may not be the same as
Expand Down
69 changes: 43 additions & 26 deletions doomsday/libs/gui/include/de/text/nativefont.h
Expand Up @@ -57,16 +57,16 @@ class LIBGUI_PUBLIC NativeFont : public Asset
struct Spec
{
Style style;
dint weight;
int weight;
Transform transform;

Spec(Style s = Regular, dint w = Normal, Transform xform = NoTransform)
Spec(Style s = Regular, int w = Normal, Transform xform = NoTransform)
: style(s), weight(w), transform(xform) {}

bool operator == (Spec const &other) const {
bool operator == (const Spec &other) const {
return style == other.style && weight == other.weight && transform == other.transform;
}
bool operator < (Spec const &other) const { // Map key order
bool operator < (const Spec &other) const { // Map key order
if (weight < other.weight) return true;
if (weight > other.weight) return false;
if (style == other.style) {
Expand All @@ -85,60 +85,77 @@ class LIBGUI_PUBLIC NativeFont : public Asset
* @param family Native font family name.
* @param mapping Mapping of styles to native font names.
*/
static void defineMapping(String const &family, StyleMapping const &mapping);
static void defineMapping(const String &family, const StyleMapping &mapping);

public:
NativeFont(String const &family = "");
NativeFont(NativeFont const &other);
NativeFont(const String &family = "");
NativeFont(const NativeFont &other);

void setFamily(String const &family);
void setSize(dfloat size);
void setFamily(const String &family);
void setPointSize(float pointSize);
void setStyle(Style style);
void setWeight(dint weight);
void setWeight(int weight);
void setTransform(Transform transform);

String family() const;
dfloat size() const;
float pointSize() const;
Style style() const;
dint weight() const;
int weight() const;
Transform transform() const;

/**
* Determines the native font name based on style mappings.
*/
String nativeFontName() const;

// Metrics are returned as pixels:
int ascent() const;
int descent() const;
int height() const;
int lineSpacing() const;

/**
* Measures the extents of a line of text.
* Measures the extents of a line of text as pixels.
*
* @param text Text line.
*
* @return Boundaries.
* @return Boundaries as pixels. The coordinat eorigin (0,0) is on the baseline.
*/
Rectanglei measure(String const &text) const;
Rectanglei measure(const String &text) const;

int width(String const &text) const;
/**
* Width of a text string as pixels.
*
* @param text Text string.
*
* @return Pixel width.
*/
int width(const String &text) const;

/**
* Draws a line of text using the font into an image.
*
* @param text Text line.
* @param text Text line.
* @param foreground Foreground/text color.
* @param background Background color.
*
* @return Image of the text, with the same dimensions as returned by measure().
*/
Image rasterize(String const & text,
Image::Color const &foreground,
Image::Color const &background) const;
Image rasterize(const String & text,
const Image::Color &foreground,
const Image::Color &background) const;

/**
* Sets the pixels-per-point ratio for measuring and rasterizing text.
*
* @param pixelRatio Number of pixels per point.
*/
static void setPixelRatio(float pixelRatio);

static float pixelRatio();

protected:
NativeFont &operator = (NativeFont const &other);
NativeFont &operator=(const NativeFont &other);

/**
* Called when the font is needed to be used but it isn't marked Ready.
Expand All @@ -150,11 +167,11 @@ class LIBGUI_PUBLIC NativeFont : public Asset
virtual int nativeFontHeight() const = 0;
virtual int nativeFontLineSpacing() const = 0;

virtual int nativeFontWidth(String const &text) const = 0;
virtual Rectanglei nativeFontMeasure(String const &text) const = 0;
virtual Image nativeFontRasterize(String const & text,
Image::Color const &foreground,
Image::Color const &background) const = 0;
virtual int nativeFontWidth(const String &text) const = 0;
virtual Rectanglei nativeFontMeasure(const String &text) const = 0;
virtual Image nativeFontRasterize(const String & text,
const Image::Color &foreground,
const Image::Color &background) const = 0;

private:
DE_PRIVATE(d)
Expand Down
8 changes: 4 additions & 4 deletions doomsday/libs/gui/src/graphics/glwindow.cpp
Expand Up @@ -105,10 +105,10 @@ DE_PIMPL(GLWindow)

void updatePixelRatio()
{
int dw, dh, w, h;
SDL_GetWindowSize(window, &w, &h);
SDL_GL_GetDrawableSize(window, &dw, &dh);
const double ratio = double(dw) / double(w);
int points, pixels;
SDL_GetWindowSize(window, &points, nullptr);
SDL_GL_GetDrawableSize(window, &pixels, nullptr);
const double ratio = double(pixels) / double(points);
if (!fequal(ratio, pixelRatio))
{
pixelRatio = ratio;
Expand Down
6 changes: 5 additions & 1 deletion doomsday/libs/gui/src/guiapp.cpp
Expand Up @@ -28,6 +28,7 @@
#include <de/EventLoop>
#include <de/FileSystem>
#include <de/Log>
#include <de/NativeFont>
#include <de/NativePath>
#include <de/NumberValue>
#include <de/ScriptSystem>
Expand Down Expand Up @@ -169,7 +170,8 @@ GuiApp::GuiApp(const StringList &args)
}
}

d->determineDevicePixelRatio();
d->determineDevicePixelRatio();
NativeFont::setPixelRatio(d->windowPixelRatio);

static ImageFile::Interpreter intrpImageFile;
fileSystem().addInterpreter(intrpImageFile);
Expand Down Expand Up @@ -216,6 +218,8 @@ void GuiApp::setPixelRatio(float pixelRatio)
d->pixelRatio->set(pixelRatio);
scriptSystem()["DisplayMode"].set("PIXEL_RATIO", Value::Number(pixelRatio));
}

NativeFont::setPixelRatio(pixelRatio);
}

void GuiApp::setMetadata(const String &orgName,
Expand Down
4 changes: 3 additions & 1 deletion doomsday/libs/gui/src/style.cpp
Expand Up @@ -70,7 +70,7 @@ DE_PIMPL(Style)

void updateFontSizeFactor()
{
float fontSize = 1.f;
float fontSize = 1.0f;
if (CommandLine::ArgWithParams arg = App::commandLine().check("-fontsize", 1))
{
fontSize = arg.params.at(0).toFloat();
Expand Down Expand Up @@ -102,6 +102,7 @@ DE_PIMPL(Style)
{
LOG_MSG("UI style being updated due to pixel ratio change");

#if 0
#if defined (WIN32)
/*
* KLUDGE: The operating system provides fonts scaled according to the desktop
Expand All @@ -110,6 +111,7 @@ DE_PIMPL(Style)
* DisplayMode.PIXEL_RATIO directly. (Should do that on Windows, too?)
*/
updateFontSizeFactor();
#endif
#endif
self().performUpdate();
}
Expand Down

0 comments on commit c672b05

Please sign in to comment.