From 9ccbbb307c54e09bcf9a14f574f944a0a71b7b76 Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Mon, 23 Mar 2015 10:25:26 +0300 Subject: [PATCH] fix issues #76, #72 - High DPI (Retina) displays support --- src/dlangui/core/types.d | 7 ++++++- src/dlangui/graphics/resources.d | 25 +++++++++++++++++++++---- src/dlangui/widgets/styles.d | 9 ++++++--- views/res/theme_default.xml | 4 ++-- 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/src/dlangui/core/types.d b/src/dlangui/core/types.d index b49dd066..425607c5 100644 --- a/src/dlangui/core/types.d +++ b/src/dlangui/core/types.d @@ -189,7 +189,12 @@ immutable int SIZE_IN_PERCENTS_FLAG = 0x0800_0000; /// convert custom size to pixels (sz can be either pixels, or points if SIZE_IN_POINTS_FLAG bit set) int toPixels(int sz) { - return sz > 0 ? ((sz & SIZE_IN_POINTS_FLAG) ? pointsToPixels(sz ^ SIZE_IN_POINTS_FLAG) : sz) : sz; + if (sz > 0 && (sz & SIZE_IN_POINTS_FLAG) != 0) { + import dlangui.core.logger; + Log.d("size in points"); + return pointsToPixels(sz ^ SIZE_IN_POINTS_FLAG); + } + return sz; } /// convert custom size Point to pixels (sz can be either pixels, or points if SIZE_IN_POINTS_FLAG bit set) diff --git a/src/dlangui/graphics/resources.d b/src/dlangui/graphics/resources.d index e461b08d..d2f099d9 100644 --- a/src/dlangui/graphics/resources.d +++ b/src/dlangui/graphics/resources.d @@ -273,21 +273,34 @@ class FrameDrawable : Drawable { enum DimensionUnits { pixels, - points + points, + percents } /// decode size string, e.g. 1px or 2 or 3pt static uint decodeDimension(string s) { uint value = 0; DimensionUnits units = DimensionUnits.pixels; + bool dotFound = false; + uint afterPointValue = 0; + uint afterPointDivider = 1; foreach(c; s) { int digit = -1; if (c >='0' && c <= '9') digit = c - '0'; - if (digit >= 0) - value = value * 10 + digit; - else if (c == 't') // just test by containing 't' - for NNNpt + if (digit >= 0) { + if (dotFound) { + afterPointValue = afterPointValue * 10 + digit; + afterPointDivider *= 10; + } else { + value = value * 10 + digit; + } + } else if (c == 't') // just test by containing 't' - for NNNpt units = DimensionUnits.points; // "pt" + else if (c == '%') + units = DimensionUnits.percents; + else if (c == '.') + dotFound = true; } // TODO: convert points to pixels switch(units) { @@ -295,6 +308,10 @@ static uint decodeDimension(string s) { // need to convert points to pixels value |= SIZE_IN_POINTS_FLAG; break; + case DimensionUnits.percents: + // need to convert percents + value = ((value * 100) + (afterPointValue * 100 / afterPointDivider)) | SIZE_IN_PERCENTS_FLAG; + break; default: break; } diff --git a/src/dlangui/widgets/styles.d b/src/dlangui/widgets/styles.d index c1670b70..dbd38a7e 100644 --- a/src/dlangui/widgets/styles.d +++ b/src/dlangui/widgets/styles.d @@ -15,6 +15,7 @@ import dlangui.widgets.styles; Recent changes: Dimensions like fontSize, padding, margins, min/max width and height can be specified in points, e.g. minWidth = "3pt" margins="1pt,2pt,1pt,2pt" + % for font size, based on parent font size, e.g. fontSize="120.5%" means parentStyle.fontSize * 120.5 / 100.0; Copyright: Vadim Lopatin, 2014 License: Boost License 1.0 @@ -443,9 +444,11 @@ class Style { /// font size @property int fontSize() const { - if (_fontSize != FONT_SIZE_UNSPECIFIED) + if (_fontSize != FONT_SIZE_UNSPECIFIED) { + if (_fontSize & SIZE_IN_PERCENTS_FLAG) + return parentStyle.fontSize * (_fontSize ^ SIZE_IN_PERCENTS_FLAG) / 10000; return toPixels(_fontSize); - else + } else return parentStyle.fontSize; } @@ -1237,7 +1240,7 @@ bool loadStyleAttributes(Style style, Element elem, bool allowStates) { if ("fontFamily" in elem.tag.attr) style.fontFamily = decodeFontFamily(elem.tag.attr["fontFamily"]); if ("fontSize" in elem.tag.attr) - style.fontSize = cast(ushort)decodeDimension(elem.tag.attr["fontSize"]); + style.fontSize = cast(int)decodeDimension(elem.tag.attr["fontSize"]); if ("layoutWidth" in elem.tag.attr) style.layoutWidth = decodeLayoutDimension(elem.tag.attr["layoutWidth"]); if ("layoutHeight" in elem.tag.attr) diff --git a/views/res/theme_default.xml b/views/res/theme_default.xml index dc3aa024..86b9560c 100644 --- a/views/res/theme_default.xml +++ b/views/res/theme_default.xml @@ -1,6 +1,6 @@ @@ -421,7 +421,7 @@