Merge lp:~zsombi/ubuntu-ui-toolkit/native-fontutils into lp:ubuntu-ui-toolkit

Proposed by Zsombor Egri
Status: Merged
Approved by: Florian Boucault
Approved revision: 500
Merged at revision: 502
Proposed branch: lp:~zsombi/ubuntu-ui-toolkit/native-fontutils
Merge into: lp:ubuntu-ui-toolkit
Diff against target: 333 lines (+203/-37)
9 files modified
modules/Ubuntu/Components/Label.qml (+0/-1)
modules/Ubuntu/Components/TextField.qml (+0/-1)
modules/Ubuntu/Components/fontUtils.js (+0/-31)
modules/Ubuntu/Components/plugin/plugin.cpp (+8/-1)
modules/Ubuntu/Components/plugin/plugin.pro (+4/-2)
modules/Ubuntu/Components/plugin/ucfontutils.cpp (+99/-0)
modules/Ubuntu/Components/plugin/ucfontutils.h (+40/-0)
modules/Ubuntu/Components/qmldir (+0/-1)
tests/unit/tst_components/tst_fontutils.qml (+52/-0)
To merge this branch: bzr merge lp:~zsombi/ubuntu-ui-toolkit/native-fontutils
Reviewer Review Type Date Requested Status
PS Jenkins bot continuous-integration Approve
Florian Boucault (community) Needs Fixing
Review via email: mp+164682@code.launchpad.net

Commit message

[theming] FontUtils converted from stateful JS library to native module due to its dependency to "units" context property.

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Florian Boucault (fboucault) wrote :

In ucfontutils.h:

* unnecessary includes:
#include <QtQml/QQmlEngine>
#include <QtQml/QJSEngine>

* copyright year should be 2013

review: Needs Fixing
Revision history for this message
Florian Boucault (fboucault) wrote :

In ucfontutils.cpp:

* unnecessary includes:
#include <QtCore/QMetaEnum>
#include <QtQml/QQmlInfo>

* copyright year should be 2013

* documentation generation fails; also I wonder how to document a context property

review: Needs Fixing
499. By Zsombor Egri

comments applied, more documentation added

500. By Zsombor Egri

documentation update

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'modules/Ubuntu/Components/Label.qml'
2--- modules/Ubuntu/Components/Label.qml 2013-04-11 18:00:34 +0000
3+++ modules/Ubuntu/Components/Label.qml 2013-05-20 12:15:41 +0000
4@@ -15,7 +15,6 @@
5 */
6
7 import QtQuick 2.0
8-import "fontUtils.js" as FontUtils
9 // FIXME: When a module contains QML, C++ and JavaScript elements exported,
10 // we need to use named imports otherwise namespace collision is reported
11 // by the QML engine. As workaround, we use Theming named import.
12
13=== modified file 'modules/Ubuntu/Components/TextField.qml'
14--- modules/Ubuntu/Components/TextField.qml 2013-05-19 19:23:40 +0000
15+++ modules/Ubuntu/Components/TextField.qml 2013-05-20 12:15:41 +0000
16@@ -15,7 +15,6 @@
17 */
18
19 import QtQuick 2.0
20-import "fontUtils.js" as FontUtils
21 // FIXME: When a module contains QML, C++ and JavaScript elements exported,
22 // we need to use named imports otherwise namespace collision is reported
23 // by the QML engine. As workaround, we use Theming named import.
24
25=== removed file 'modules/Ubuntu/Components/fontUtils.js'
26--- modules/Ubuntu/Components/fontUtils.js 2012-11-01 11:01:28 +0000
27+++ modules/Ubuntu/Components/fontUtils.js 1970-01-01 00:00:00 +0000
28@@ -1,31 +0,0 @@
29-/*
30- * Copyright 2012 Canonical Ltd.
31- *
32- * This program is free software; you can redistribute it and/or modify
33- * it under the terms of the GNU Lesser General Public License as published by
34- * the Free Software Foundation; version 3.
35- *
36- * This program is distributed in the hope that it will be useful,
37- * but WITHOUT ANY WARRANTY; without even the implied warranty of
38- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39- * GNU Lesser General Public License for more details.
40- *
41- * You should have received a copy of the GNU Lesser General Public License
42- * along with this program. If not, see <http://www.gnu.org/licenses/>.
43- */
44-
45-function sizeToPixels(size) {
46- var baseSize = units.dp(14)
47- return Math.round(modularScale(size) * baseSize)
48-}
49-
50-function modularScale(size) {
51- switch (size) {
52- case "xx-small": return 0.606
53- case "x-small": return 0.707
54- case "small": return 0.857
55- case "medium": return 1.0
56- case "large": return 1.414
57- case "x-large": return 2.423
58- }
59-}
60
61=== modified file 'modules/Ubuntu/Components/plugin/plugin.cpp'
62--- modules/Ubuntu/Components/plugin/plugin.cpp 2013-05-09 19:04:28 +0000
63+++ modules/Ubuntu/Components/plugin/plugin.cpp 2013-05-20 12:15:41 +0000
64@@ -41,6 +41,7 @@
65 #include "bottombarvisibilitycommunicator.h"
66 #include "ucstyle.h"
67 #include "ucubuntuanimation.h"
68+#include "ucfontutils.h"
69
70 #include <sys/types.h>
71 #include <unistd.h>
72@@ -96,12 +97,18 @@
73 QObject::connect(&UCUnits::instance(), SIGNAL(gridUnitChanged()),
74 &unitsChangeListener, SLOT(updateContextProperty()));
75
76+ // register FontUtils
77+ context->setContextProperty("FontUtils", &UCFontUtils::instance());
78+ static ContextPropertyChangeListener fontUtilsListener(context, "FontUtils");
79+ QObject::connect(&UCUnits::instance(), SIGNAL(gridUnitChanged()),
80+ &fontUtilsListener, SLOT(updateContextProperty()));
81+
82 context->setContextProperty("bottomBarVisibilityCommunicator", &BottomBarVisibilityCommunicator::instance());
83
84 engine->addImageProvider(QLatin1String("scaling"), new UCScalingImageProvider);
85
86 // register gicon provider
87- engine->addImageProvider(QLatin1String("gicon"), new GIconProvider);
88+ engine->addImageProvider(QLatin1String("gicon"), new GIconProvider);
89
90 // Necessary for Screen.orientation (from import QtQuick.Window 2.0) to work
91 QGuiApplication::primaryScreen()->setOrientationUpdateMask(
92
93=== modified file 'modules/Ubuntu/Components/plugin/plugin.pro'
94--- modules/Ubuntu/Components/plugin/plugin.pro 2013-05-09 19:04:28 +0000
95+++ modules/Ubuntu/Components/plugin/plugin.pro 2013-05-20 12:15:41 +0000
96@@ -44,7 +44,8 @@
97 qquickclipboard_p.h \
98 selector_p.h \
99 ucstyle.h \
100- ucubuntuanimation.h
101+ ucubuntuanimation.h \
102+ ucfontutils.h
103
104 SOURCES += plugin.cpp \
105 themeengine.cpp \
106@@ -66,7 +67,8 @@
107 qquickmimedata.cpp \
108 selector.cpp \
109 ucstyle.cpp \
110- ucubuntuanimation.cpp
111+ ucubuntuanimation.cpp \
112+ ucfontutils.cpp
113
114 # deployment rules for the plugin
115 installPath = $$[QT_INSTALL_QML]/$$replace(uri, \\., /)
116
117=== added file 'modules/Ubuntu/Components/plugin/ucfontutils.cpp'
118--- modules/Ubuntu/Components/plugin/ucfontutils.cpp 1970-01-01 00:00:00 +0000
119+++ modules/Ubuntu/Components/plugin/ucfontutils.cpp 2013-05-20 12:15:41 +0000
120@@ -0,0 +1,99 @@
121+/*
122+ * Copyright 2013 Canonical Ltd.
123+ *
124+ * This program is free software; you can redistribute it and/or modify
125+ * it under the terms of the GNU Lesser General Public License as published by
126+ * the Free Software Foundation; version 3.
127+ *
128+ * This program is distributed in the hope that it will be useful,
129+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
130+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
131+ * GNU Lesser General Public License for more details.
132+ *
133+ * You should have received a copy of the GNU Lesser General Public License
134+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
135+ *
136+ */
137+
138+#include "ucfontutils.h"
139+#include "ucunits.h"
140+
141+/*!
142+ * \qmltype FontUtils
143+ * \instantiates UCFontUtils
144+ * \inqmlmodule Ubuntu.Components 0.1
145+ * \ingroup ubuntu
146+ * \brief FontUtils is a \l {http://qt-project.org/doc/qt-5.0/qtqml/qqmlcontext.html#details} {context property},
147+ * which provides utility functions for font manipulations.
148+ *
149+ * FontUtils cannot be instantiated and it is already available as a context
150+ * property, i.e. 'FontUtils' is an instance that can be used from anywhere in
151+ * the code.
152+ *
153+ * Example of a Label implementation which uses the default base scale for the font
154+ * size:
155+ * \qml
156+ * import QtQuick 2.0
157+ * import Ubuntu.Components 0.1
158+ *
159+ * Text {
160+ * property string fontSize: "medium"
161+ * font.pixelSize: FontUtils.sizeToPixels(fontSize)
162+ * }
163+ * \endqml
164+ *
165+ * Another example of a custom text input component exposing a fontSize property and a base
166+ * font unit size to scale its font:
167+ * \qml
168+ * import QtQuick 2.0
169+ * import Ubuntu.Components 0.1
170+ *
171+ * TextInput {
172+ * property string fontSize: "small"
173+ * property int baseFontUnits: units.dp(20)
174+ * font.pixelSize: FontUtils.modularScale(fontSize) * units.dp(baseFontUnits)
175+ * }
176+ * \endqml
177+ *
178+ */
179+
180+/*!
181+ * \qmlmethod real FontUtils::sizeToPixels(string size)
182+ * The function calculates the pixel size of a given scale. The size scale can be
183+ * one of the strings specified at modularScale function. On failure returns 0.
184+ */
185+qreal UCFontUtils::sizeToPixels(const QString &size)
186+{
187+ return modularScale(size) * UCUnits::instance().dp(14);
188+}
189+
190+/*!
191+ * \qmlmethod real FontUtils::modularScale(string size)
192+ * The function returns the number interpretation of a given font scale. The scale
193+ * can have one of the following values:
194+ * \list
195+ * \li \b{xx-small}
196+ * \li \b{x-small}
197+ * \li \b{small}
198+ * \li \b{medium}
199+ * \li \b{large}
200+ * \li \b{x-large}
201+ * \endlist
202+ */
203+qreal UCFontUtils::modularScale(const QString &size)
204+{
205+ if (size == "xx-small") {
206+ return 0.606;
207+ } else if (size == "x-small") {
208+ return 0.707;
209+ } else if (size == "small") {
210+ return 0.857;
211+ } else if (size == "medium") {
212+ return 1.0;
213+ } else if (size == "large") {
214+ return 1.414;
215+ } else if (size == "x-large") {
216+ return 2.423;
217+ }
218+ return 0.0;
219+}
220
221=== added file 'modules/Ubuntu/Components/plugin/ucfontutils.h'
222--- modules/Ubuntu/Components/plugin/ucfontutils.h 1970-01-01 00:00:00 +0000
223+++ modules/Ubuntu/Components/plugin/ucfontutils.h 2013-05-20 12:15:41 +0000
224@@ -0,0 +1,40 @@
225+/*
226+ * Copyright 2013 Canonical Ltd.
227+ *
228+ * This program is free software; you can redistribute it and/or modify
229+ * it under the terms of the GNU Lesser General Public License as published by
230+ * the Free Software Foundation; version 3.
231+ *
232+ * This program is distributed in the hope that it will be useful,
233+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
234+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
235+ * GNU Lesser General Public License for more details.
236+ *
237+ * You should have received a copy of the GNU Lesser General Public License
238+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
239+ *
240+ */
241+
242+#ifndef UCFONTUTILS_H
243+#define UCFONTUTILS_H
244+
245+#include <QtCore/QObject>
246+
247+class UCFontUtils : public QObject
248+{
249+ Q_OBJECT
250+public:
251+
252+ static UCFontUtils& instance()
253+ {
254+ static UCFontUtils instance;
255+ return instance;
256+ }
257+
258+ explicit UCFontUtils(QObject *parent = 0) : QObject(parent) {}
259+
260+ Q_INVOKABLE qreal sizeToPixels(const QString &size);
261+ Q_INVOKABLE qreal modularScale(const QString &size);
262+};
263+
264+#endif // UCFONTUTILS_H
265
266=== modified file 'modules/Ubuntu/Components/qmldir'
267--- modules/Ubuntu/Components/qmldir 2013-05-20 09:06:47 +0000
268+++ modules/Ubuntu/Components/qmldir 2013-05-20 12:15:41 +0000
269@@ -33,7 +33,6 @@
270 UbuntuShape 0.1 UbuntuShape.qml
271 Icon 0.1 Icon.qml
272 OrientationHelper 0.1 OrientationHelper.qml
273-FontUtils 0.1 fontUtils.js
274 MathUtils 0.1 mathUtils.js
275 SliderUtils 0.1 sliderUtils.js
276 ScrollbarUtils 0.1 scrollbarUtils.js
277
278=== added file 'tests/unit/tst_components/tst_fontutils.qml'
279--- tests/unit/tst_components/tst_fontutils.qml 1970-01-01 00:00:00 +0000
280+++ tests/unit/tst_components/tst_fontutils.qml 2013-05-20 12:15:41 +0000
281@@ -0,0 +1,52 @@
282+/*
283+ * Copyright 2012 Canonical Ltd.
284+ *
285+ * This program is free software; you can redistribute it and/or modify
286+ * it under the terms of the GNU Lesser General Public License as published by
287+ * the Free Software Foundation; version 3.
288+ *
289+ * This program is distributed in the hope that it will be useful,
290+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
291+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
292+ * GNU Lesser General Public License for more details.
293+ *
294+ * You should have received a copy of the GNU Lesser General Public License
295+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
296+ */
297+
298+import QtQuick 2.0
299+import QtTest 1.0
300+import Ubuntu.Components 0.1
301+
302+TestCase {
303+ name: "FontUtilsAPI"
304+
305+ function test_modularScale() {
306+ compare(FontUtils.modularScale("xx-small"), 0.606, "xx-small scale");
307+ compare(FontUtils.modularScale("x-small"), 0.707, "x-small scale");
308+ compare(FontUtils.modularScale("small"), 0.857, "small scale");
309+ compare(FontUtils.modularScale("medium"), 1.0, "medium scale");
310+ compare(FontUtils.modularScale("large"), 1.414, "large scale");
311+ compare(FontUtils.modularScale("x-large"), 2.423, "x-large scale");
312+ }
313+
314+ function test_modularScale_failures() {
315+ compare(FontUtils.modularScale("xxsmall"), 0.0, "xxsmall not defined");
316+ compare(FontUtils.modularScale("undefined"), 0.0, "undefined scale");
317+ }
318+
319+ function test_sizeToPixels() {
320+ compare(FontUtils.sizeToPixels("xx-small"), 0.606 * units.dp(14), "xx-small scale");
321+ compare(FontUtils.sizeToPixels("x-small"), 0.707 * units.dp(14), "x-small scale");
322+ compare(FontUtils.sizeToPixels("small"), 0.857 * units.dp(14), "small scale");
323+ compare(FontUtils.sizeToPixels("medium"), 1.0 * units.dp(14), "medium scale");
324+ compare(FontUtils.sizeToPixels("large"), 1.414 * units.dp(14), "large scale");
325+ compare(FontUtils.sizeToPixels("x-large"), 2.423 * units.dp(14), "x-large scale");
326+ }
327+
328+ function test_sizeToPixels_failures() {
329+ compare(FontUtils.sizeToPixels("xxsmall"), 0.0, "xxsmall not defined");
330+ compare(FontUtils.sizeToPixels("undefined"), 0.0, "undefined scale");
331+ }
332+
333+}

Subscribers

People subscribed via source and target branches

to status/vote changes: