summaryrefslogtreecommitdiff
path: root/libs/litehtml/src/css_length.cpp
blob: d1c69d2612a2627c1fe70813b9116e40d7f20b85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "html.h"
#include "css_length.h"

void litehtml::css_length::fromString( const string& str, const string& predefs, int defValue )
{
	// TODO: Make support for calc
	if(str.substr(0, 4) == "calc")
	{
		m_is_predefined = true;
		m_predef		= defValue;
		return;
	}

	int predef = value_index(str, predefs, -1);
	if(predef >= 0)
	{
		m_is_predefined = true;
		m_predef		= predef;
	} else
	{
		m_is_predefined = false;

		string num;
		string un;
		bool is_unit = false;
		for(char chr : str)
		{
			if(!is_unit)
			{
				if(t_isdigit(chr) || chr == '.' || chr == '+' || chr == '-')
				{
					num += chr;
				} else
				{
					is_unit = true;
				}
			}
			if(is_unit)
			{
				un += chr;
			}
		}
		if(!num.empty())
		{
			m_value = t_strtof(num);
			m_units	= (css_units) value_index(un, css_units_strings, css_units_none);
		} else
		{
			// not a number so it is predefined
			m_is_predefined = true;
			m_predef = defValue;
		}
	}
}

litehtml::css_length litehtml::css_length::from_string(const string& str, const string& predefs, int defValue)
{
	css_length len;
	len.fromString(str, predefs, defValue);
	return len;
}

litehtml::string litehtml::css_length::to_string() const
{
    if(m_is_predefined)
    {
        return "def(" + std::to_string(m_predef) + ")";
    }
    return std::to_string(m_value) + "{" + index_value(m_units, css_units_strings) + "}";
}

litehtml::css_length litehtml::css_length::predef_value(int val)
{
	css_length len;
	len.predef(val);
	return len;
}