1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <sal/config.h>
21 
22 #include <osl/diagnose.h>
23 #include <vcl/outdev.hxx>
24 #include <editeng/editrids.hrc>
25 #include <unotools/intlwrapper.hxx>
26 #include <unotools/localedatawrapper.hxx>
27 #include <editeng/itemtype.hxx>
28 #include <editeng/eerdll.hxx>
29 #include <rtl/ustrbuf.hxx>
30 
31 
GetMetricText(long nVal,MapUnit eSrcUnit,MapUnit eDestUnit,const IntlWrapper * pIntl)32 OUString GetMetricText( long nVal, MapUnit eSrcUnit, MapUnit eDestUnit, const IntlWrapper* pIntl )
33 {
34     bool bNeg = false;
35     bool bShowAtLeastOneDecimalDigit = true;
36     sal_Int32 nRet = 0;
37 
38     if ( nVal < 0 )
39     {
40         bNeg = true;
41         nVal *= -1;
42     }
43 
44     switch ( eDestUnit )
45     {
46         case MapUnit::Map100thMM:
47         case MapUnit::Map10thMM:
48         case MapUnit::MapMM:
49         case MapUnit::MapCM:
50         {
51             nRet = OutputDevice::LogicToLogic( nVal, eSrcUnit, MapUnit::Map100thMM );
52 
53             switch ( eDestUnit )
54             {
55                 case MapUnit::Map100thMM:  nRet *= 1000; break;
56                 case MapUnit::Map10thMM:   nRet *= 100; break;
57                 case MapUnit::MapMM:        nRet *= 10; break;
58                 default: ;//prevent warning
59             }
60             break;
61         }
62 
63         case MapUnit::Map1000thInch:
64         case MapUnit::Map100thInch:
65         case MapUnit::Map10thInch:
66         case MapUnit::MapInch:
67         {
68             nRet = OutputDevice::LogicToLogic( nVal, eSrcUnit, MapUnit::Map1000thInch );
69 
70             switch ( eDestUnit )
71             {
72                 case MapUnit::Map1000thInch:   nRet *= 1000; break;
73                 case MapUnit::Map100thInch:    nRet *= 100; break;
74                 case MapUnit::Map10thInch:     nRet *= 10; break;
75                 default: ;//prevent warning
76             }
77             break;
78         }
79 
80         case MapUnit::MapPoint:
81             // fractions of a point are used, e.g., for font size
82             nRet = OutputDevice::LogicToLogic(nVal, eSrcUnit, MapUnit::MapTwip) * 50;
83             bShowAtLeastOneDecimalDigit = false;
84             break;
85 
86         case MapUnit::MapTwip:
87         case MapUnit::MapPixel:
88             return OUString::number( OutputDevice::LogicToLogic(
89                         nVal, eSrcUnit, eDestUnit ));
90 
91         default:
92             OSL_FAIL( "not supported mapunit" );
93             return OUString();
94     }
95 
96     if ( MapUnit::MapCM == eDestUnit || MapUnit::MapInch == eDestUnit )
97     {
98         sal_Int32 nMod = nRet % 10;
99 
100         if ( nMod > 4 )
101             nRet += 10 - nMod;
102         else if ( nMod > 0 )
103             nRet -= nMod;
104     }
105 
106     OUStringBuffer sRet;
107 
108     if ( bNeg )
109         sRet.append('-');
110 
111     long nDiff = 1000;
112     for( int nDigits = 4; nDigits; --nDigits, nDiff /= 10 )
113     {
114         if ( nRet < nDiff )
115             sRet.append('0');
116         else
117             sRet.append(nRet / nDiff);
118         nRet %= nDiff;
119         if( 4 == nDigits && (bShowAtLeastOneDecimalDigit || nRet) )
120         {
121             if(pIntl)
122                 sRet.append(pIntl->getLocaleData()->getNumDecimalSep());
123             else
124                 sRet.append(',');
125             if( !nRet )
126             {
127                 sRet.append('0');
128                 break;
129             }
130         }
131         else if( !nRet )
132             break;
133     }
134     return sRet.makeStringAndClear();
135 }
136 
GetColorString(const Color & rCol)137 OUString GetColorString( const Color& rCol )
138 {
139     if (rCol == COL_AUTO)
140         return EditResId(RID_SVXSTR_AUTOMATIC);
141 
142     static const Color aColAry[] = {
143         COL_BLACK, COL_BLUE, COL_GREEN, COL_CYAN,
144         COL_RED, COL_MAGENTA, COL_BROWN, COL_GRAY,
145         COL_LIGHTGRAY, COL_LIGHTBLUE, COL_LIGHTGREEN, COL_LIGHTCYAN,
146         COL_LIGHTRED, COL_LIGHTMAGENTA, COL_YELLOW, COL_WHITE };
147 
148     sal_uInt16 nColor = 0;
149     while ( nColor < SAL_N_ELEMENTS(aColAry) &&
150             aColAry[nColor] != rCol.GetRGBColor() )
151     {
152         nColor += 1;
153     }
154 
155     static const char* RID_SVXITEMS_COLORS[] =
156     {
157         RID_SVXITEMS_COLOR_BLACK,
158         RID_SVXITEMS_COLOR_BLUE,
159         RID_SVXITEMS_COLOR_GREEN,
160         RID_SVXITEMS_COLOR_CYAN,
161         RID_SVXITEMS_COLOR_RED,
162         RID_SVXITEMS_COLOR_MAGENTA,
163         RID_SVXITEMS_COLOR_BROWN,
164         RID_SVXITEMS_COLOR_GRAY,
165         RID_SVXITEMS_COLOR_LIGHTGRAY,
166         RID_SVXITEMS_COLOR_LIGHTBLUE,
167         RID_SVXITEMS_COLOR_LIGHTGREEN,
168         RID_SVXITEMS_COLOR_LIGHTCYAN,
169         RID_SVXITEMS_COLOR_LIGHTRED,
170         RID_SVXITEMS_COLOR_LIGHTMAGENTA,
171         RID_SVXITEMS_COLOR_YELLOW,
172         RID_SVXITEMS_COLOR_WHITE
173     };
174 
175     static_assert(SAL_N_ELEMENTS(aColAry) == SAL_N_ELEMENTS(RID_SVXITEMS_COLORS), "must match");
176 
177     OUString sStr;
178     if ( nColor < SAL_N_ELEMENTS(aColAry) )
179         sStr = EditResId(RID_SVXITEMS_COLORS[nColor]);
180 
181     if ( sStr.isEmpty() )
182     {
183         sStr += "RGB(" +
184                 OUString::number( rCol.GetRed() )   + cpDelim +
185                 OUString::number( rCol.GetGreen() ) + cpDelim +
186                 OUString::number( rCol.GetBlue() )  + ")";
187     }
188     return sStr;
189 }
190 
GetMetricId(MapUnit eUnit)191 const char* GetMetricId( MapUnit eUnit )
192 {
193     const char* pId = RID_SVXITEMS_METRIC_MM;
194 
195     switch ( eUnit )
196     {
197         case MapUnit::Map100thMM:
198         case MapUnit::Map10thMM:
199         case MapUnit::MapMM:
200             pId = RID_SVXITEMS_METRIC_MM;
201             break;
202 
203         case MapUnit::MapCM:
204             pId = RID_SVXITEMS_METRIC_CM;
205             break;
206 
207         case MapUnit::Map1000thInch:
208         case MapUnit::Map100thInch:
209         case MapUnit::Map10thInch:
210         case MapUnit::MapInch:
211             pId = RID_SVXITEMS_METRIC_INCH;
212             break;
213 
214         case MapUnit::MapPoint:
215             pId = RID_SVXITEMS_METRIC_POINT;
216             break;
217 
218         case MapUnit::MapTwip:
219             pId = RID_SVXITEMS_METRIC_TWIP;
220             break;
221 
222         case MapUnit::MapPixel:
223             pId = RID_SVXITEMS_METRIC_PIXEL;
224             break;
225 
226         default:
227             OSL_FAIL( "not supported mapunit" );
228     }
229     return pId;
230 }
231 
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
233