fix issues #76, #72 - High DPI (Retina) displays support

This commit is contained in:
Vadim Lopatin 2015-03-23 10:25:26 +03:00
parent 568b87cc5f
commit 9ccbbb307c
4 changed files with 35 additions and 10 deletions

View File

@ -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) /// convert custom size to pixels (sz can be either pixels, or points if SIZE_IN_POINTS_FLAG bit set)
int toPixels(int sz) { 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) /// convert custom size Point to pixels (sz can be either pixels, or points if SIZE_IN_POINTS_FLAG bit set)

View File

@ -273,21 +273,34 @@ class FrameDrawable : Drawable {
enum DimensionUnits { enum DimensionUnits {
pixels, pixels,
points points,
percents
} }
/// decode size string, e.g. 1px or 2 or 3pt /// decode size string, e.g. 1px or 2 or 3pt
static uint decodeDimension(string s) { static uint decodeDimension(string s) {
uint value = 0; uint value = 0;
DimensionUnits units = DimensionUnits.pixels; DimensionUnits units = DimensionUnits.pixels;
bool dotFound = false;
uint afterPointValue = 0;
uint afterPointDivider = 1;
foreach(c; s) { foreach(c; s) {
int digit = -1; int digit = -1;
if (c >='0' && c <= '9') if (c >='0' && c <= '9')
digit = c - '0'; digit = c - '0';
if (digit >= 0) if (digit >= 0) {
if (dotFound) {
afterPointValue = afterPointValue * 10 + digit;
afterPointDivider *= 10;
} else {
value = value * 10 + digit; value = value * 10 + digit;
else if (c == 't') // just test by containing 't' - for NNNpt }
} else if (c == 't') // just test by containing 't' - for NNNpt
units = DimensionUnits.points; // "pt" units = DimensionUnits.points; // "pt"
else if (c == '%')
units = DimensionUnits.percents;
else if (c == '.')
dotFound = true;
} }
// TODO: convert points to pixels // TODO: convert points to pixels
switch(units) { switch(units) {
@ -295,6 +308,10 @@ static uint decodeDimension(string s) {
// need to convert points to pixels // need to convert points to pixels
value |= SIZE_IN_POINTS_FLAG; value |= SIZE_IN_POINTS_FLAG;
break; break;
case DimensionUnits.percents:
// need to convert percents
value = ((value * 100) + (afterPointValue * 100 / afterPointDivider)) | SIZE_IN_PERCENTS_FLAG;
break;
default: default:
break; break;
} }

View File

@ -15,6 +15,7 @@ import dlangui.widgets.styles;
Recent changes: 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" 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 Copyright: Vadim Lopatin, 2014
License: Boost License 1.0 License: Boost License 1.0
@ -443,9 +444,11 @@ class Style {
/// font size /// font size
@property int fontSize() const { @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); return toPixels(_fontSize);
else } else
return parentStyle.fontSize; return parentStyle.fontSize;
} }
@ -1237,7 +1240,7 @@ bool loadStyleAttributes(Style style, Element elem, bool allowStates) {
if ("fontFamily" in elem.tag.attr) if ("fontFamily" in elem.tag.attr)
style.fontFamily = decodeFontFamily(elem.tag.attr["fontFamily"]); style.fontFamily = decodeFontFamily(elem.tag.attr["fontFamily"]);
if ("fontSize" in elem.tag.attr) 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) if ("layoutWidth" in elem.tag.attr)
style.layoutWidth = decodeLayoutDimension(elem.tag.attr["layoutWidth"]); style.layoutWidth = decodeLayoutDimension(elem.tag.attr["layoutWidth"]);
if ("layoutHeight" in elem.tag.attr) if ("layoutHeight" in elem.tag.attr)

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<theme id="theme_default" <theme id="theme_default"
fontSize="11pt" fontSize="9pt"
fontFace="Verdana,Arial,DejaVu Sans" fontFace="Verdana,Arial,DejaVu Sans"
fontFamily="SansSerif" fontFamily="SansSerif"
> >
@ -421,7 +421,7 @@
<style id="SETTINGS_PAGE_TITLE" <style id="SETTINGS_PAGE_TITLE"
margins="4,4,4,14" margins="4,4,4,14"
fontSize="11pt" fontSize="120%"
layoutWeight="0" layoutWeight="0"
layoutWidth="FILL_PARENT" layoutWidth="FILL_PARENT"
/> />