Nux

Merge lp:~macslow/nux/nux.metrics into lp:nux/2.0

Proposed by Mirco Müller
Status: Merged
Approved by: Mirco Müller
Approved revision: 511
Merged at revision: 575
Proposed branch: lp:~macslow/nux/nux.metrics
Merge into: lp:nux/2.0
Diff against target: 236 lines (+193/-0)
5 files modified
Nux/Makefile.am (+2/-0)
Nux/Metrics.cpp (+57/-0)
Nux/Metrics.h (+39/-0)
tests/Makefile.am (+1/-0)
tests/test-nux-metrics.cpp (+94/-0)
To merge this branch: bzr merge lp:~macslow/nux/nux.metrics
Reviewer Review Type Date Requested Status
Marco Trevisan (Treviño) Needs Information
Andrea Azzarone (community) Approve
Review via email: mp+92800@code.launchpad.net

Description of the change

Class Metrics providing pixel->EM and EM->pixel conversion functions, thus making nux ready for resolution-independence glory... complete with unit-test. A bling-bling example program is missing currently as the typical rush-rush spree of approaching release-day and FF is dialing up other feature/bug-priorities through the roof.

To post a comment you must log in.
Revision history for this message
Andrea Azzarone (azzar1) wrote :

 Copyright (C) 2011 Canonical Ltd

2012...

---

+#include "config.h"

Is this really needed?

---

57 + xres = ((((double) DisplayWidth(dpy,scr)) * 25.4) /
58 + ((double) DisplayWidthMM(dpy,scr)));
59 + yres = ((((double) DisplayHeight(dpy,scr)) * 25.4) /
60 + ((double) DisplayHeightMM(dpy,scr)));

C style cast... :(

---

+ Metrics::~Metrics ()
73 + {
74 + }

It doesn nothing, so remove it :)

---

110 +#ifndef METRICS_H
111 +#define METRICS_H

Should be NUX_MET..

---

+#include <X11/Xlib.h>

I'd prefer a fwd declaration for Display if it is possible.

---

Ah, no spaces between the function name and the bracket. And you've not pushed the unit-test :)

P.S. I'm not marking it as Need Fixing because these "issues" are not blocking.

Revision history for this message
Andrea Azzarone (azzar1) wrote :

Oh sorry

«And you've not pushed the unit-test»

This need to be fixed so...

review: Needs Fixing
Revision history for this message
Mirco Müller (macslow) wrote :

All issues, but the forward-declaration of Display, fixed. I didn't find an easy way to get around using Xlib.h there.

lp:~macslow/nux/nux.metrics updated
511. By Mirco Müller

Corrected some minor style issues, forgot to add missing unit-test earlier

Revision history for this message
Andrea Azzarone (azzar1) wrote :

> All issues, but the forward-declaration of Display, fixed. I didn't find an
> easy way to get around using Xlib.h there.

Looks good...

review: Approve
Revision history for this message
Marco Trevisan (Treviño) (3v1n0) wrote :

+ if (dpy)
54 + {
55 + // determine current system DPI, remember that: 1 inch == 2.54 cm == 25.4 mm
56 + xres = ((static_cast<double> DisplayWidth(dpy,scr) * 25.4) /
57 + (static_cast<double> DisplayWidthMM(dpy,scr)));
58 + yres = ((static_cast<double> DisplayHeight(dpy,scr) * 25.4) /
59 + (static_cast<double> DisplayHeightMM(dpy,scr)));
60 + dpi = (xres + yres) / 2.0;
61 + }

Aren't here no risks of dividing per 0? I mean, are we sure these functions won't return 0?

review: Needs Information
Revision history for this message
Mirco Müller (macslow) wrote :

If DisplayHeightMM() or DisplayWidthMM() are 0 you've bigger problems, than a division by zero. But for that very unlikely edge-case, I can add an additional if-statement test.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'Nux/Makefile.am'
2--- Nux/Makefile.am 2012-02-12 22:25:19 +0000
3+++ Nux/Makefile.am 2012-02-14 09:35:28 +0000
4@@ -70,6 +70,7 @@
5 $(srcdir)/MainLoopGLib.cpp \
6 $(srcdir)/MenuPage.cpp \
7 $(srcdir)/MenuBar.cpp \
8+ $(srcdir)/Metrics.cpp \
9 $(srcdir)/MouseAreaCtrl.cpp \
10 $(srcdir)/NumericValuator.cpp \
11 $(srcdir)/Nux.cpp \
12@@ -153,6 +154,7 @@
13 $(srcdir)/LinearLayout.h \
14 $(srcdir)/MenuPage.h \
15 $(srcdir)/MenuBar.h \
16+ $(srcdir)/Metrics.h \
17 $(srcdir)/MouseAreaCtrl.h \
18 $(srcdir)/NumericValuator.h \
19 $(srcdir)/Nux.h \
20
21=== added file 'Nux/Metrics.cpp'
22--- Nux/Metrics.cpp 1970-01-01 00:00:00 +0000
23+++ Nux/Metrics.cpp 2012-02-14 09:35:28 +0000
24@@ -0,0 +1,57 @@
25+/*
26+ * Copyright (C) 2012 Canonical Ltd
27+ *
28+ * This program is free software: you can redistribute it and/or modify
29+ * it under the terms of the GNU General Public License version 3 as
30+ * published by the Free Software Foundation.
31+ *
32+ * This program is distributed in the hope that it will be useful,
33+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
34+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35+ * GNU General Public License for more details.
36+ *
37+ * You should have received a copy of the GNU General Public License
38+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
39+ *
40+ * Authored by: Mirco Müller <mirco.mueller@canonical.com
41+ */
42+
43+#include "Nux/Metrics.h"
44+
45+namespace nux
46+{
47+ Metrics::Metrics(Display* dpy, int scr, double points)
48+ {
49+ double xres = 0.0;
50+ double yres = 0.0;
51+ double dpi = 96.0; // default DPI if no Display provided (e.g. unit-test)
52+
53+ if (dpy)
54+ {
55+ // determine current system DPI, remember that: 1 inch == 2.54 cm == 25.4 mm
56+ xres = ((static_cast<double> DisplayWidth(dpy,scr) * 25.4) /
57+ (static_cast<double> DisplayWidthMM(dpy,scr)));
58+ yres = ((static_cast<double> DisplayHeight(dpy,scr) * 25.4) /
59+ (static_cast<double> DisplayHeightMM(dpy,scr)));
60+ dpi = (xres + yres) / 2.0;
61+ }
62+
63+ // update stored ppe
64+ pixels_per_em_ = points * dpi / 72.0;
65+
66+ // sanity-check
67+ if (pixels_per_em_ == 0.0)
68+ pixels_per_em_ = 10.0; // assume points == 10.0, dpi == 96.0
69+ }
70+
71+ double Metrics::Pixel2EM(int value)
72+ {
73+ return static_cast<double> (value) / pixels_per_em_;
74+ }
75+
76+ int Metrics::EM2Pixel(double value)
77+ {
78+ return static_cast<int> (value * pixels_per_em_);
79+ }
80+} // nux namespace
81+
82
83=== added file 'Nux/Metrics.h'
84--- Nux/Metrics.h 1970-01-01 00:00:00 +0000
85+++ Nux/Metrics.h 2012-02-14 09:35:28 +0000
86@@ -0,0 +1,39 @@
87+/*
88+ * Copyright (C) 2012 Canonical Ltd
89+ *
90+ * This program is free software: you can redistribute it and/or modify
91+ * it under the terms of the GNU General Public License version 3 as
92+ * published by the Free Software Foundation.
93+ *
94+ * This program is distributed in the hope that it will be useful,
95+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
96+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
97+ * GNU General Public License for more details.
98+ *
99+ * You should have received a copy of the GNU General Public License
100+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
101+ *
102+ * Authored by: Mirco Müller <mirco.mueller@canonical.com
103+ */
104+
105+#ifndef NUX_METRICS_H
106+#define NUX_METRICS_H
107+
108+#include <X11/Xlib.h>
109+
110+namespace nux
111+{
112+ class Metrics
113+ {
114+ public:
115+ Metrics(Display* dpy, int scr, double points);
116+
117+ double Pixel2EM(int value);
118+ int EM2Pixel(double value);
119+
120+ private:
121+ double pixels_per_em_;
122+ };
123+}
124+
125+#endif // NUX_METRICS_H
126
127=== modified file 'tests/Makefile.am'
128--- tests/Makefile.am 2012-02-12 02:24:29 +0000
129+++ tests/Makefile.am 2012-02-14 09:35:28 +0000
130@@ -81,6 +81,7 @@
131
132 gtest_nux_SOURCES = \
133 test-nux-main.cpp \
134+ test-nux-metrics.cpp \
135 test-nux-statictext.cpp \
136 test-nux-windowthread.cpp
137
138
139=== added file 'tests/test-nux-metrics.cpp'
140--- tests/test-nux-metrics.cpp 1970-01-01 00:00:00 +0000
141+++ tests/test-nux-metrics.cpp 2012-02-14 09:35:28 +0000
142@@ -0,0 +1,94 @@
143+#include <gtest/gtest.h>
144+#include <gmock/gmock.h>
145+
146+#include "Nux/Nux.h"
147+#include "Nux/Metrics.h"
148+
149+namespace {
150+ TEST(TestMetrics, TestCreate)
151+ {
152+ // Test fallback (no display) of constructor, DPI of 96.0 will be used
153+ nux::Metrics* metrics5 = new nux::Metrics(NULL, 0, 5.0);
154+ nux::Metrics* metrics10 = new nux::Metrics(NULL, 0, 10.0);
155+ nux::Metrics* metrics15 = new nux::Metrics(NULL, 0, 15.0);
156+ nux::Metrics* metrics20 = new nux::Metrics(NULL, 0, 20.0);
157+
158+ // Test conversion from pixel to EM (testing fallback-case) 5-point font
159+ EXPECT_EQ(metrics5->Pixel2EM(30), 4.5);
160+ EXPECT_EQ(metrics5->Pixel2EM(25), 3.75);
161+ EXPECT_EQ(metrics5->Pixel2EM(20), 3.0);
162+ EXPECT_EQ(metrics5->Pixel2EM(15), 2.25);
163+ EXPECT_EQ(metrics5->Pixel2EM(10), 1.5);
164+ EXPECT_EQ(metrics5->Pixel2EM(5), 0.75);
165+ EXPECT_EQ(metrics5->Pixel2EM(0), 0.0);
166+
167+ // Test conversion from EM to pixel (testing fallback-case) 5-point font
168+ EXPECT_EQ(metrics5->EM2Pixel(4.5), 30);
169+ EXPECT_EQ(metrics5->EM2Pixel(3.75), 25);
170+ EXPECT_EQ(metrics5->EM2Pixel(3.0), 20);
171+ EXPECT_EQ(metrics5->EM2Pixel(2.25), 15);
172+ EXPECT_EQ(metrics5->EM2Pixel(1.5), 10);
173+ EXPECT_EQ(metrics5->EM2Pixel(0.75), 5);
174+ EXPECT_EQ(metrics5->EM2Pixel(0.0), 0);
175+
176+ // Test conversion from pixel to EM (testing fallback-case) 10-point font
177+ EXPECT_EQ(metrics10->Pixel2EM(30), 2.25);
178+ EXPECT_EQ(metrics10->Pixel2EM(25), 1.875);
179+ EXPECT_EQ(metrics10->Pixel2EM(20), 1.5);
180+ EXPECT_EQ(metrics10->Pixel2EM(15), 1.125);
181+ EXPECT_EQ(metrics10->Pixel2EM(10), 0.75);
182+ EXPECT_EQ(metrics10->Pixel2EM(5), 0.375);
183+ EXPECT_EQ(metrics10->Pixel2EM(0), 0.0);
184+
185+ // Test conversion from EM to pixel (testing fallback-case) 10-point font
186+ EXPECT_EQ(metrics10->EM2Pixel(2.25), 30);
187+ EXPECT_EQ(metrics10->EM2Pixel(1.875), 25);
188+ EXPECT_EQ(metrics10->EM2Pixel(1.5), 20);
189+ EXPECT_EQ(metrics10->EM2Pixel(1.125), 15);
190+ EXPECT_EQ(metrics10->EM2Pixel(0.75), 10);
191+ EXPECT_EQ(metrics10->EM2Pixel(0.375), 5);
192+ EXPECT_EQ(metrics10->EM2Pixel(0.0), 0);
193+
194+ // Test conversion from pixel to EM (testing fallback-case) 15-point font
195+ EXPECT_EQ(metrics15->Pixel2EM(30), 1.5);
196+ EXPECT_EQ(metrics15->Pixel2EM(25), 1.25);
197+ EXPECT_EQ(metrics15->Pixel2EM(20), 1.0);
198+ EXPECT_EQ(metrics15->Pixel2EM(15), 0.75);
199+ EXPECT_EQ(metrics15->Pixel2EM(10), 0.5);
200+ EXPECT_EQ(metrics15->Pixel2EM(5), 0.25);
201+ EXPECT_EQ(metrics15->Pixel2EM(0), 0.0);
202+
203+ // Test conversion from EM to pixel (testing fallback-case) 15-point font
204+ EXPECT_EQ(metrics15->EM2Pixel(1.5), 30);
205+ EXPECT_EQ(metrics15->EM2Pixel(1.25), 25);
206+ EXPECT_EQ(metrics15->EM2Pixel(1.0), 20);
207+ EXPECT_EQ(metrics15->EM2Pixel(0.75), 15);
208+ EXPECT_EQ(metrics15->EM2Pixel(0.5), 10);
209+ EXPECT_EQ(metrics15->EM2Pixel(0.25), 5);
210+ EXPECT_EQ(metrics15->EM2Pixel(0.0), 0);
211+
212+ // Test conversion from pixel to EM (testing fallback-case) 20-point font
213+ EXPECT_EQ(metrics20->Pixel2EM(30), 1.125);
214+ EXPECT_EQ(metrics20->Pixel2EM(25), 0.9375);
215+ EXPECT_EQ(metrics20->Pixel2EM(20), 0.75);
216+ EXPECT_EQ(metrics20->Pixel2EM(15), 0.5625);
217+ EXPECT_EQ(metrics20->Pixel2EM(10), 0.375);
218+ EXPECT_EQ(metrics20->Pixel2EM(5), 0.1875);
219+ EXPECT_EQ(metrics20->Pixel2EM(0), 0.0);
220+
221+ // Test conversion from EM to pixel (testing fallback-case) 20-point font
222+ EXPECT_EQ(metrics20->EM2Pixel(1.125), 30);
223+ EXPECT_EQ(metrics20->EM2Pixel(0.9375), 25);
224+ EXPECT_EQ(metrics20->EM2Pixel(0.75), 20);
225+ EXPECT_EQ(metrics20->EM2Pixel(0.5625), 15);
226+ EXPECT_EQ(metrics20->EM2Pixel(0.375), 10);
227+ EXPECT_EQ(metrics20->EM2Pixel(0.1875), 5);
228+ EXPECT_EQ(metrics20->EM2Pixel(0.0), 0);
229+
230+ delete metrics5;
231+ delete metrics10;
232+ delete metrics15;
233+ delete metrics20;
234+ }
235+} // unnamed namespace
236+

Subscribers

People subscribed via source and target branches

to all changes: