• R/O
  • HTTP
  • SSH
  • HTTPS

コミット

タグ
未設定

よく使われているワード(クリックで追加)

javaandroidc++linuxc#objective-ccocoa誰得qtrubybathyscaphegamephpguicwindows翻訳pythonomegattwitterframeworkbtronarduinovb.net計画中(planning stage)directxpreviewertestゲームエンジンdom

Small footprint UI library for hardware accelerated games & applications


コミットメタ情報

リビジョン389baa6946986d6d5afead50c90dc6b55e23b976 (tree)
日時2016-12-22 05:02:51
作者Emil Segerås <emilsegers@gmai...>
コミッターEmil Segerås

ログメッセージ

Added float dimension conversions and slightly changed calculations.

Round integer conversions and removed nonzero guarantee for values
that where not zero before conversion.

Unknown/mistyped unit is treated like dp instead of px.

変更サマリ

差分

--- a/src/tb/tb_dimension.cpp
+++ b/src/tb/tb_dimension.cpp
@@ -10,6 +10,7 @@
1010 #include "tb_value.h"
1111 #include <stdlib.h>
1212 #include <ctype.h>
13+#include <math.h>
1314
1415 namespace tb {
1516
@@ -38,42 +39,54 @@ void TBDimensionConverter::GetDstDPIFilename(const char *filename, TBTempBuffer
3839
3940 int TBDimensionConverter::DpToPx(int dp) const
4041 {
42+ return (int) roundf(DpToPxF((float) dp));
43+}
44+
45+float TBDimensionConverter::DpToPxF(float dp) const
46+{
4147 if (dp <= TB_INVALID_DIMENSION || dp == 0 || !NeedConversion())
4248 return dp;
43- if (dp > 0)
44- {
45- dp = dp * m_dst_dpi / m_src_dpi;
46- return MAX(dp, 1);
47- }
48- else
49- {
50- dp = dp * m_dst_dpi / m_src_dpi;
51- return MIN(dp, -1);
52- }
49+ return dp * m_dst_dpi / m_src_dpi;
5350 }
5451
5552 int TBDimensionConverter::MmToPx(int mm) const
5653 {
54+ return (int) roundf(MmToPxF((float) mm));
55+}
56+
57+float TBDimensionConverter::MmToPxF(float mm) const
58+{
5759 if (mm <= TB_INVALID_DIMENSION || mm == 0)
5860 return mm;
59-
60- return (int) (mm * TBSystem::GetDPI() / 25.4f + 0.5f);
61+ return mm * TBSystem::GetDPI() / 25.4f;
6162 }
6263
6364 int TBDimensionConverter::GetPxFromString(const char *str, int def_value) const
6465 {
6566 if (!str || !is_start_of_number(str))
6667 return def_value;
67- int len = strlen(str);
68- int val = atoi(str);
69- // "dp" and unspecified unit is dp.
70- if ((len > 0 && isdigit(str[len - 1])) ||
71- (len > 2 && strcmp(str + len - 2, "dp") == 0))
72- return DpToPx(val);
73- else if (len > 2 && strcmp(str + len - 2, "mm") == 0)
68+ const int len = strlen(str);
69+ const int val = atoi(str);
70+ if (len > 2 && strcmp(str + len - 2, "px") == 0)
71+ return val;
72+ if (len > 2 && strcmp(str + len - 2, "mm") == 0)
7473 return MmToPx(val);
75- else
74+ // "dp", unspecified or unknown unit is treated as dp.
75+ return DpToPx(val);
76+}
77+
78+float TBDimensionConverter::GetPxFromStringF(const char *str, float def_value) const
79+{
80+ if (!str || !is_start_of_number(str))
81+ return def_value;
82+ const int len = strlen(str);
83+ const float val = (float) atof(str);
84+ if (len > 2 && strcmp(str + len - 2, "px") == 0)
7685 return val;
86+ if (len > 2 && strcmp(str + len - 2, "mm") == 0)
87+ return MmToPxF(val);
88+ // "dp", unspecified or unknown unit is treated as dp.
89+ return DpToPxF(val);
7790 }
7891
7992 int TBDimensionConverter::GetPxFromValue(TBValue *value, int def_value) const
@@ -83,9 +96,19 @@ int TBDimensionConverter::GetPxFromValue(TBValue *value, int def_value) const
8396 if (value->GetType() == TBValue::TYPE_INT)
8497 return DpToPx(value->GetInt());
8598 else if (value->GetType() == TBValue::TYPE_FLOAT)
86- // FIX: We might want float versions of all dimension functions.
87- return DpToPx((int)value->GetFloat());
99+ return (int) roundf(DpToPxF(value->GetFloat()));
88100 return GetPxFromString(value->GetString(), def_value);
89101 }
90102
103+float TBDimensionConverter::GetPxFromValueF(TBValue *value, float def_value) const
104+{
105+ if (!value)
106+ return def_value;
107+ if (value->GetType() == TBValue::TYPE_INT)
108+ return DpToPxF((float) value->GetInt());
109+ else if (value->GetType() == TBValue::TYPE_FLOAT)
110+ return DpToPxF(value->GetFloat());
111+ return GetPxFromStringF(value->GetString(), def_value);
112+}
113+
91114 } // namespace tb
--- a/src/tb/tb_dimension.h
+++ b/src/tb/tb_dimension.h
@@ -53,9 +53,11 @@ public:
5353
5454 /** Convert device independant point to pixel. */
5555 int DpToPx(int dp) const;
56+ float DpToPxF(float dp) const;
5657
5758 /** Convert millimeter to pixel. */
5859 int MmToPx(int mm) const;
60+ float MmToPxF(float mm) const;
5961
6062 /** Get a pixel value from string in any of the following formats:
6163 str may be nullptr. def_value is returned on fail.
@@ -64,6 +66,7 @@ public:
6466 Pixel value: "1px"
6567 */
6668 int GetPxFromString(const char *str, int def_value) const;
69+ float GetPxFromStringF(const char *str, float def_value) const;
6770
6871 /** Get a pixel value from TBValue.
6972 value may be nullptr. def_value is returned on fail.
@@ -72,6 +75,7 @@ public:
7275 String format is treated like for GetPxFromString.
7376 */
7477 int GetPxFromValue(TBValue *value, int def_value) const;
78+ float GetPxFromValueF(TBValue *value, float def_value) const;
7579 };
7680
7781 } // namespace tb