PCL
UIScaling.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.1.19
6 // ----------------------------------------------------------------------------
7 // pcl/UIScaling.h - Released 2019-11-07T10:59:34Z
8 // ----------------------------------------------------------------------------
9 // This file is part of the PixInsight Class Library (PCL).
10 // PCL is a multiplatform C++ framework for development of PixInsight modules.
11 //
12 // Copyright (c) 2003-2019 Pleiades Astrophoto S.L. All Rights Reserved.
13 //
14 // Redistribution and use in both source and binary forms, with or without
15 // modification, is permitted provided that the following conditions are met:
16 //
17 // 1. All redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer.
19 //
20 // 2. All redistributions in binary form must reproduce the above copyright
21 // notice, this list of conditions and the following disclaimer in the
22 // documentation and/or other materials provided with the distribution.
23 //
24 // 3. Neither the names "PixInsight" and "Pleiades Astrophoto", nor the names
25 // of their contributors, may be used to endorse or promote products derived
26 // from this software without specific prior written permission. For written
27 // permission, please contact info@pixinsight.com.
28 //
29 // 4. All products derived from this software, in any form whatsoever, must
30 // reproduce the following acknowledgment in the end-user documentation
31 // and/or other materials provided with the product:
32 //
33 // "This product is based on software from the PixInsight project, developed
34 // by Pleiades Astrophoto and its contributors (http://pixinsight.com/)."
35 //
36 // Alternatively, if that is where third-party acknowledgments normally
37 // appear, this acknowledgment must be reproduced in the product itself.
38 //
39 // THIS SOFTWARE IS PROVIDED BY PLEIADES ASTROPHOTO AND ITS CONTRIBUTORS
40 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PLEIADES ASTROPHOTO OR ITS
43 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44 // EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, BUSINESS
45 // INTERRUPTION; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; AND LOSS OF USE,
46 // DATA OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 // POSSIBILITY OF SUCH DAMAGE.
50 // ----------------------------------------------------------------------------
51 
52 #ifndef __PCL_UIScaling_h
53 #define __PCL_UIScaling_h
54 
56 
57 #include <pcl/Defs.h>
58 #include <pcl/Diagnostics.h>
59 
60 #include <pcl/GlobalSettings.h>
61 #include <pcl/Math.h>
62 #include <pcl/String.h>
63 
64 namespace pcl
65 {
66 
67 // ----------------------------------------------------------------------------
68 
73 #define PCL_UIScaling_LUT_Length 7
74 
75 extern PCL_DATA const double PCL_UIScalingFactor_LUT[ PCL_UIScaling_LUT_Length ];
76 extern PCL_DATA const char* PCL_UIScalingSubdir_LUT[ PCL_UIScaling_LUT_Length ];
77 
87 inline int UIResourceScalingIndex( double scalingFactor )
88 {
89  if ( scalingFactor > 1 )
90  {
91  int index = Min( RoundInt( 2*scalingFactor ), PCL_UIScaling_LUT_Length+1 );
92  if ( index > 2 )
93  return index-2;
94  }
95  return 0;
96 }
97 
114 inline double UIResourceScalingFactor( double scalingFactor )
115 {
116  return PCL_UIScalingFactor_LUT[UIResourceScalingIndex( scalingFactor )];
117 }
118 
133 inline double UIResourceScalingFactorForIndex( int index )
134 {
135  return PCL_UIScalingFactor_LUT[Range( index, 0, PCL_UIScaling_LUT_Length-1 )];
136 }
137 
145 inline int UIScaled( double scalingFactor, int size )
146 {
147  return (scalingFactor <= 1) ? size : RoundInt( scalingFactor*size );
148 }
149 
157 inline int UIUnscaled( double scalingFactor, int size )
158 {
159  return (scalingFactor <= 1) ? size : RoundInt( size/scalingFactor );
160 }
161 
175 template <class R>
176 inline String UIScaledResourceForIndex( int index, R resource )
177 {
178  String path( resource );
179  index = Range( index, 0, PCL_UIScaling_LUT_Length-1 );
180  if ( index > 0 )
181  {
182  size_type p = path.FindLast( '/' );
183  if ( p != String::notFound && p > 0 ) // assume resource starts with ":/"
184  return path.Left( p+1 ) + PCL_UIScalingSubdir_LUT[index] + path.Substring( p );
185  }
186  return path;
187 }
188 
202 template <class R>
203 inline String UIScaledResource( double scalingFactor, R resource )
204 {
205  return UIScaledResourceForIndex( UIResourceScalingIndex( scalingFactor ), resource );
206 }
207 
208 /*
209  * Auxiliary structure used by UIScaledStyleSheet().
210  */
211 struct PCL_UIStringSection
212 {
213  size_type pos = 0;
214  size_type len = 0;
215 
216  PCL_UIStringSection( size_type i, size_type j ) :
217  pos( i ),
218  len( j - i )
219  {
220  }
221 
222  PCL_UIStringSection() = default;
223  PCL_UIStringSection( const PCL_UIStringSection& ) = default;
224 };
225 
258 template <class S>
259 String UIScaledStyleSheet( double displayScalingFactor, double resourceScalingFactor, S styleSheet, int fontDPI = 0 )
260 {
261 #define PCL_UI_IS_NUMBER_DIGIT( c ) (CharTraits::IsDigit( c ) || CharTraits::IsDecimalSeparator( c ))
262 
263  String cssCode( styleSheet );
264 
265  /*
266  * Scale resource file paths.
267  */
268  if ( resourceScalingFactor > 0 )
269  {
270  Array<PCL_UIStringSection> urlSections;
271  for ( size_type p = 0; ; )
272  {
273  p = cssCode.Find( ":/", p );
274  if ( p == String::notFound )
275  break;
276  size_type q = cssCode.Find( ')', p+2 );
277  if ( q != String::notFound )
278  {
279  urlSections.Add( PCL_UIStringSection( p, q ) );
280  p = q+1;
281  }
282  else
283  p += 2;
284  }
285  if ( !urlSections.IsEmpty() )
286  {
287  int index = UIResourceScalingIndex( resourceScalingFactor );
288  for ( const PCL_UIStringSection& s : ReverseIterable( urlSections ) )
289  cssCode.Replace( s.pos, s.len, String( UIScaledResourceForIndex( index, cssCode.Substring( s.pos, s.len ) ) ) );
290  }
291  }
292 
293  /*
294  * Scale pixel dimensions and convert points to scaled pixels.
295  */
296  if ( displayScalingFactor > 0 )
297  {
298  Array<PCL_UIStringSection> pxSections;
299  for ( size_type q = 0; ; q += 2 )
300  {
301  q = cssCode.Find( "px", q );
302  if ( q == String::notFound )
303  break;
304  size_type p = q;
305  while ( p > 0 && PCL_UI_IS_NUMBER_DIGIT( cssCode[p-1] ) )
306  --p;
307  if ( p < q )
308  pxSections.Add( PCL_UIStringSection( p, q ) );
309  }
310  for ( const PCL_UIStringSection& s : ReverseIterable( pxSections ) )
311  {
312  double px;
313  if ( cssCode.Substring( s.pos, s.len ).TryToDouble( px ) )
314  cssCode.Replace( s.pos, s.len, String().Format( "%d", RoundInt( px * displayScalingFactor ) ) );
315  }
316 
317  /*
318  * If fontDPI=0 (the default value), use core font resolution settings for
319  * point-to-pixel conversions.
320  */
321  if ( fontDPI == 0 )
322  fontDPI = PixInsightSettings::GlobalInteger( "Application/FontResolution" );
323 
324  /*
325  * If a valid font resolution is available, convert points to scaled pixels.
326  */
327  if ( fontDPI > 0 )
328  {
329  Array<PCL_UIStringSection> ptSections;
330  for ( size_type q = 0; ; q += 2 )
331  {
332  q = cssCode.Find( "pt", q );
333  if ( q == String::notFound )
334  break;
335  size_type p = q;
336  while ( p > 0 && PCL_UI_IS_NUMBER_DIGIT( cssCode[p-1] ) )
337  --p;
338  if ( p < q )
339  ptSections.Add( PCL_UIStringSection( p, q ) );
340  }
341  for ( const PCL_UIStringSection& s : ReverseIterable( ptSections ) )
342  {
343  double pt;
344  if ( cssCode.Substring( s.pos, s.len ).TryToDouble( pt ) )
345  cssCode.Replace( s.pos, s.len+2, String().Format( "%dpx", RoundInt( displayScalingFactor*pt*fontDPI/72 ) ) );
346  }
347  }
348  }
349 
350  return cssCode;
351 
352 #undef PCL_UI_IS_NUMBER_DIGIT
353 }
354 
355 // ----------------------------------------------------------------------------
356 
357 } // pcl
358 
359 #endif // __PCL_UIScaling_h
360 
361 // ----------------------------------------------------------------------------
362 // EOF pcl/UIScaling.h - Released 2019-11-07T10:59:34Z
bool IsEmpty() const
Definition: Array.h:311
int RoundInt(T x)
Definition: Math.h:1397
PCL root namespace.
Definition: AbstractImage.h:76
int UIScaled(double scalingFactor, int size)
Definition: UIScaling.h:145
constexpr const T & Range(const T &x, const T &a, const T &b)
Definition: Utility.h:190
size_t size_type
Definition: Defs.h:543
bool TryToDouble(double &value) const
Unicode (UTF-16) string.
Definition: String.h:7911
constexpr const T & Min(const T &a, const T &b)
Definition: Utility.h:90
double UIResourceScalingFactor(double scalingFactor)
Definition: UIScaling.h:114
int UIResourceScalingIndex(double scalingFactor)
Definition: UIScaling.h:87
int UIUnscaled(double scalingFactor, int size)
Definition: UIScaling.h:157
String UIScaledResource(double scalingFactor, R resource)
Definition: UIScaling.h:203
void Add(const Array &x)
Definition: Array.h:1006
Generic dynamic array.
Definition: Array.h:99
double UIResourceScalingFactorForIndex(int index)
Definition: UIScaling.h:133
String UIScaledStyleSheet(double displayScalingFactor, double resourceScalingFactor, S styleSheet, int fontDPI=0)
Definition: UIScaling.h:259
Reverse container adaptor.
Definition: Iterator.h:546
static int GlobalInteger(const IsoString &globalId)
String UIScaledResourceForIndex(int index, R resource)
Definition: UIScaling.h:176