measuredigits.cpp 3.61 KB
Newer Older
1
#include "measuredigits.h"
Elen.Subbotina's avatar
Elen.Subbotina committed
2

Alexey.Golubev's avatar
Alexey.Golubev committed
3 4
#include <boost/lexical_cast.hpp>
#include <logging.h>
5 6 7 8

#include <float.h>

#include "../../DesktopEditor/fontengine/FontManager.h"
Elen.Subbotina's avatar
Elen.Subbotina committed
9
#include "../../DesktopEditor/fontengine/ApplicationFonts.h"
Alexey.Golubev's avatar
Alexey.Golubev committed
10

11 12
double getSystemDPI()
{
13 14 15 16 17
	//При запросе системных настроек-проблема в linux без графического интерфейса.
    //Используется в GetMaxDigitSizePixels для измерения символов, можно указывать любой dpi,
    //потому что после измерения pix переводятся обратно в метрические величины.
    //Используется для конвертации картинок с процентными размерами oox->odf. Из редактора никогда не приходят относительные размеры,
    //думаю тут несущественнен dpi.
18 19 20 21
	return 96.;
}


Alexey.Golubev's avatar
Alexey.Golubev committed
22 23 24 25 26 27 28 29 30 31
namespace cpdoccore {
namespace utils {

namespace 
{
    class Error : public std::exception {};    
    class LoadFontError : public Error {};
    class MeasureError : public Error {};
}

32
std::pair<float, float> GetMaxDigitSizePixelsImpl(const std::wstring & fontName, double fontSize, double dpi, long fontStyle, CFontManager *pFontManager)
Alexey.Golubev's avatar
Alexey.Golubev committed
33
{
Elen.Subbotina's avatar
Elen.Subbotina committed
34
    if (pFontManager == NULL) return std::pair<float, float>(7,8);
Alexey.Golubev's avatar
Alexey.Golubev committed
35

Elen.Subbotina's avatar
Elen.Subbotina committed
36
   int hr = FALSE;
Alexey.Golubev's avatar
Alexey.Golubev committed
37

Elen.Subbotina's avatar
Elen.Subbotina committed
38
    if (FALSE == (hr = pFontManager->LoadFontByName(fontName, fontSize, fontStyle, dpi, dpi )))
Alexey.Golubev's avatar
Alexey.Golubev committed
39
	{
Elen.Subbotina's avatar
Elen.Subbotina committed
40
        if (FALSE == (hr = pFontManager->LoadFontByName(L"Arial", fontSize, fontStyle, dpi, dpi )))
Alexey.Golubev's avatar
Alexey.Golubev committed
41
		{
42
            return std::pair<float, float>(7, 8);
Alexey.Golubev's avatar
Alexey.Golubev committed
43 44 45 46
		}
	}

    float maxWidth = 0;
47
    float maxHeight = 0;
Elen.Subbotina's avatar
Elen.Subbotina committed
48 49 50 51

	float minWidth = 0xffff;
	float minHeight = 0xffff;

52
   // for (int i = 0; i <= 9; ++i)
Alexey.Golubev's avatar
Alexey.Golubev committed
53
    {
54 55 56 57 58
		//if (FALSE == (hr = pFontManager->LoadString2( boost::lexical_cast<std::wstring>(i), 0, 0)))
		//	return std::pair<float, float>(7,8);

		if (FALSE == (hr = pFontManager->LoadString2( L"xxxxx" , 0, 0)))
			return std::pair<float, float>(7,8);
Alexey.Golubev's avatar
Alexey.Golubev committed
59

60
		TBBox box;
61 62
		try
		{
63
           box = pFontManager->MeasureString();
64 65
		}catch(...)
		{
66
            return std::pair<float, float>(7,8);
67
		}
Elen.Subbotina's avatar
Elen.Subbotina committed
68

Elen.Subbotina's avatar
Elen.Subbotina committed
69 70
		if (box.fMaxX < -0xffff+1 || box.fMaxY < -0xffff+1 ||
			box.fMinX > 0xffff-1 || box.fMinY > 0xffff-1)		
Elen.Subbotina's avatar
Elen.Subbotina committed
71
				return std::pair<float, float>(7,8);
72
          
73 74
        if (box.fMaxX - box.fMinX > maxWidth)   maxWidth = box.fMaxX - box.fMinX;
        if (box.fMaxY - box.fMinY > maxHeight)  maxHeight = box.fMaxY - box.fMinY;
Elen.Subbotina's avatar
Elen.Subbotina committed
75 76 77
       
		if (box.fMaxX - box.fMinX < minWidth)   minWidth = box.fMaxX - box.fMinX;
        if (box.fMaxY - box.fMinY < minHeight)  minHeight = box.fMaxY - box.fMinY;
Alexey.Golubev's avatar
Alexey.Golubev committed
78 79
    }

80
    return std::pair<float, float>(maxWidth / 5.f, maxHeight);
Alexey.Golubev's avatar
Alexey.Golubev committed
81 82 83
}


Elen.Subbotina's avatar
Elen.Subbotina committed
84
std::pair<float, float> GetMaxDigitSizePixels(const std::wstring & fontName, double fontSize, double dpi, long fontStyle, CApplicationFonts *appFonts)
Alexey.Golubev's avatar
Alexey.Golubev committed
85 86 87
{
    try 
    {
Elen.Subbotina's avatar
Elen.Subbotina committed
88 89
        _CP_LOG << "[info] : GetMaxDigitSizePixels...";

Elen.Subbotina's avatar
Elen.Subbotina committed
90 91 92
        if (appFonts)
        {
            CFontManager *pFontManager = appFonts->GenerateFontManager();
Elen.Subbotina's avatar
Elen.Subbotina committed
93

Elen.Subbotina's avatar
Elen.Subbotina committed
94 95 96 97 98 99 100 101 102 103 104
            std::pair<float, float> val = GetMaxDigitSizePixelsImpl(fontName, fontSize, dpi, fontStyle, pFontManager);

            if (pFontManager)
            {
                pFontManager->m_pApplication = NULL;
                delete pFontManager;
            }

            _CP_LOG << "ok" << std::endl;
            return val;
        }
Alexey.Golubev's avatar
Alexey.Golubev committed
105 106 107 108 109
    }
    catch(...)
    {
        // TODO: default value!
    }    
110
	return std::pair<float, float>(7, 8);
Alexey.Golubev's avatar
Alexey.Golubev committed
111 112 113 114 115
}

}
}