Пример #1
0
void Parser::parseFont(const string& s) {
	// this contains multiple params separated by spaces.
	StringTokenizer<string> tok(s, ' ');
	auto& l = tok.getTokens();

	// remove empty strings.
	l.erase(std::remove_if(l.begin(), l.end(), [](const string& s) { return s.empty(); }), l.end());

	if(l.size() < 2) // the last 2 params (font size & font family) are compulsory.
		return;

	// the last param (font family) may contain spaces; merge if that is the case.
	while(*(l.back().end() - 1) == '\'' && (l.back().size() <= 1 || *l.back().begin() != '\'')) {
		*(l.end() - 2) += ' ' + std::move(l.back());
		l.erase(l.end() - 1);
		if(l.size() < 2)
			return;
	}

	// parse the last param (font family).
	/// @todo handle multiple fonts separated by commas...
	auto& family = l.back();
	family.erase(std::remove(family.begin(), family.end(), '\''), family.end());
	if(family.empty())
		return;
	contexts.back().font = addFont("\\fnil " + std::move(family));

	// parse the second to last param (font size).
	/// @todo handle more than px sizes
	auto& size = *(l.end() - 2);
	parseFontSize(size);

	// parse the optional third to last param (font weight).
	if(l.size() > 2 && Util::toInt(*(l.end() - 3)) >= FW_BOLD) {
		contexts.back().setFlag(Context::Bold);
	}

	// parse the optional first param (font style).
	if(l.size() > 2 && l[0] == "italic") {
		contexts.back().setFlag(Context::Italic);
	}
}
Пример #2
0
StyleParam::Value StyleParam::parseString(StyleParamKey key, const std::string& _value) {
    switch (key) {
    case StyleParamKey::extrude: {
        return parseExtrudeString(_value);
    }
    case StyleParamKey::text_wrap: {
        int textWrap;
        if (_value == "false") {
            return std::numeric_limits<uint32_t>::max();
        }
        if (_value == "true") {
            return 15; // DEFAULT
        }
        if (parseInt(_value, textWrap) > 0 && textWrap > 0) {
             return static_cast<uint32_t>(textWrap);
        }
        return std::numeric_limits<uint32_t>::max();
    }
    case StyleParamKey::text_offset:
    case StyleParamKey::offset: {
        UnitVec<glm::vec2> vec;
        if (!parseVec2(_value, { Unit::pixel }, vec) || std::isnan(vec.value.y)) {
            LOGW("Invalid offset parameter '%s'.", _value.c_str());
        }
        return vec.value;
    }
    case StyleParamKey::size: {
        UnitVec<glm::vec2> vec;
        if (!parseVec2(_value, { Unit::pixel }, vec)) {
            LOGW("Invalid size parameter '%s'.", _value.c_str());
        }
        return vec.value;
    }
    case StyleParamKey::transition_hide_time:
    case StyleParamKey::transition_show_time:
    case StyleParamKey::transition_selected_time:
    case StyleParamKey::text_transition_hide_time:
    case StyleParamKey::text_transition_show_time:
    case StyleParamKey::text_transition_selected_time: {
        float time = 0.0f;
        if (!parseTime(_value, time)) {
            LOGW("Invalid time param '%s'", _value.c_str());
        }
        return time;
    }
    case StyleParamKey::text_font_family:
    case StyleParamKey::text_font_weight:
    case StyleParamKey::text_font_style: {
        std::string normalized = _value;
        std::transform(normalized.begin(), normalized.end(), normalized.begin(), ::tolower);
        return normalized;
    }
    case StyleParamKey::text_align:
    case StyleParamKey::anchor:
    case StyleParamKey::text_anchor:
    case StyleParamKey::text_source:
    case StyleParamKey::text_transform:
    case StyleParamKey::sprite:
    case StyleParamKey::sprite_default:
    case StyleParamKey::style:
    case StyleParamKey::outline_style:
    case StyleParamKey::text_repeat_group:
        return _value;
    case StyleParamKey::text_font_size: {
        float fontSize = 0.f;
        if (!parseFontSize(_value, fontSize)) {
            LOGW("Invalid font-size '%s'.", _value.c_str());
        }
        return fontSize;
    }
    case StyleParamKey::centroid:
    case StyleParamKey::interactive:
    case StyleParamKey::text_interactive:
    case StyleParamKey::tile_edges:
    case StyleParamKey::visible:
    case StyleParamKey::collide:
    case StyleParamKey::text_collide:
        if (_value == "true") { return true; }
        if (_value == "false") { return false; }
        LOGW("Bool value required for capitalized/visible. Using Default.");
        break;
    case StyleParamKey::order:
    case StyleParamKey::outline_order:
    case StyleParamKey::priority:
    case StyleParamKey::text_priority: {
        int num;
        if (parseInt(_value, num) > 0) {
            if (num >= 0) {
                return static_cast<uint32_t>(num);
            }
        }
        LOGW("Invalid '%s' value '%s'", keyName(key).c_str(), _value.c_str());
        break;
    }
    case StyleParamKey::text_repeat_distance: {
        ValueUnitPair repeatDistance;
        repeatDistance.unit = Unit::pixel;

        int pos = parseValueUnitPair(_value, 0, repeatDistance);
        if (pos < 0) {
            LOGW("Invalid repeat distance value '%s'", _value.c_str());
            repeatDistance.value =  256.0f;
            repeatDistance.unit = Unit::pixel;
        } else {
            if (repeatDistance.unit != Unit::pixel) {
                LOGW("Invalid unit provided for repeat distance");
            }
        }

        return Width(repeatDistance);
    }
    case StyleParamKey::width:
    case StyleParamKey::outline_width: {
        ValueUnitPair width;
        width.unit = Unit::meter;

        int pos = parseValueUnitPair(_value, 0, width);
        if (pos < 0) {
            LOGW("Invalid width value '%s'", _value.c_str());
            width.value =  2.0f;
            width.unit = Unit::pixel;
        }

        return Width(width);
    }
    case StyleParamKey::miter_limit:
    case StyleParamKey::outline_miter_limit:
    case StyleParamKey::text_font_stroke_width: {
        double num;
        if (parseFloat(_value, num) > 0) {
             return static_cast<float>(num);
        }
        break;
    }

    case StyleParamKey::color:
    case StyleParamKey::outline_color:
    case StyleParamKey::text_font_fill:
    case StyleParamKey::text_font_stroke_color:
        return parseColor(_value);

    case StyleParamKey::cap:
    case StyleParamKey::outline_cap:
        return static_cast<uint32_t>(CapTypeFromString(_value));

    case StyleParamKey::join:
    case StyleParamKey::outline_join:
        return static_cast<uint32_t>(JoinTypeFromString(_value));

    default:
        break;
    }

    return none_type{};
}
Пример #3
0
StyleParam::Value StyleParam::parseString(StyleParamKey key, const std::string& _value) {
    switch (key) {
    case StyleParamKey::extrude: {
        return parseExtrudeString(_value);
    }
    case StyleParamKey::text_wrap: {
        int textWrap;
        if (_value == "false") {
            return std::numeric_limits<uint32_t>::max();
        }
        if (_value == "true") {
            return uint32_t(15); // DEFAULT
        }
        if (parseInt(_value, textWrap) > 0 && textWrap > 0) {
             return static_cast<uint32_t>(textWrap);
        }
        return std::numeric_limits<uint32_t>::max();
    }
    case StyleParamKey::text_offset:
    case StyleParamKey::offset: {
        UnitVec<glm::vec2> vec;
        if (!parseVec2(_value, { Unit::pixel }, vec) || std::isnan(vec.value.y)) {
            LOGW("Invalid offset parameter '%s'.", _value.c_str());
        }
        return vec.value;
    }
    case StyleParamKey::size: {
        UnitVec<glm::vec2> vec;
        if (!parseVec2(_value, { Unit::pixel }, vec)) {
            LOGW("Invalid size parameter '%s'.", _value.c_str());
        }
        return vec.value;
    }
    case StyleParamKey::transition_hide_time:
    case StyleParamKey::transition_show_time:
    case StyleParamKey::transition_selected_time:
    case StyleParamKey::text_transition_hide_time:
    case StyleParamKey::text_transition_show_time:
    case StyleParamKey::text_transition_selected_time: {
        float time = 0.0f;
        if (!parseTime(_value, time)) {
            LOGW("Invalid time param '%s'", _value.c_str());
        }
        return time;
    }
    case StyleParamKey::text_font_family:
    case StyleParamKey::text_font_weight:
    case StyleParamKey::text_font_style: {
        std::string normalized = _value;
        std::transform(normalized.begin(), normalized.end(), normalized.begin(), ::tolower);
        return normalized;
    }
    case StyleParamKey::anchor:
    case StyleParamKey::text_anchor: {
        LabelProperty::Anchors anchors;
        for (auto& anchor : splitString(_value, ',')) {
            if (anchors.count == LabelProperty::max_anchors) { break; }

            LabelProperty::Anchor labelAnchor;
            if (LabelProperty::anchor(anchor, labelAnchor)) {
                anchors.anchor[anchors.count++] = labelAnchor;
            } else {
                LOG("Invalid anchor %s", anchor.c_str());
            }
        }
        return anchors;
    }
    case StyleParamKey::placement: {
        LabelProperty::Placement placement = LabelProperty::Placement::vertex;
        if (!LabelProperty::placement(_value, placement)) {
            LOG("Invalid placement parameter, Setting vertex as default.");
        }
        return placement;
    }
    case StyleParamKey::text_align:
    case StyleParamKey::text_source:
    case StyleParamKey::text_transform:
    case StyleParamKey::sprite:
    case StyleParamKey::sprite_default:
    case StyleParamKey::style:
    case StyleParamKey::outline_style:
    case StyleParamKey::repeat_group:
    case StyleParamKey::text_repeat_group:
        return _value;
    case StyleParamKey::text_font_size: {
        float fontSize = 0.f;
        if (!parseFontSize(_value, fontSize)) {
            LOGW("Invalid font-size '%s'.", _value.c_str());
        }
        return fontSize;
    }
    case StyleParamKey::flat:
    case StyleParamKey::interactive:
    case StyleParamKey::text_interactive:
    case StyleParamKey::tile_edges:
    case StyleParamKey::visible:
    case StyleParamKey::text_visible:
    case StyleParamKey::outline_visible:
    case StyleParamKey::collide:
    case StyleParamKey::text_optional:
    case StyleParamKey::text_collide:
        if (_value == "true") { return true; }
        if (_value == "false") { return false; }
        LOGW("Invalid boolean value %s for key %s", _value.c_str(), StyleParam::keyName(key).c_str());
        break;
    case StyleParamKey::text_order:
        LOGW("text:order parameter is ignored.");
        break;
    case StyleParamKey::order:
    case StyleParamKey::outline_order:
    case StyleParamKey::priority:
    case StyleParamKey::text_priority: {
        int num;
        if (parseInt(_value, num) > 0) {
            if (num >= 0) {
                return static_cast<uint32_t>(num);
            }
        }
        LOGW("Invalid '%s' value '%s'", keyName(key).c_str(), _value.c_str());
        break;
    }
    case StyleParamKey::placement_spacing: {
        ValueUnitPair placementSpacing;
        placementSpacing.unit = Unit::pixel;

        int pos = parseValueUnitPair(_value, 0, placementSpacing);
        if (pos < 0) {
            LOGW("Invalid placement spacing value '%s'", _value.c_str());
            placementSpacing.value =  80.0f;
            placementSpacing.unit = Unit::pixel;
        } else {
            if (placementSpacing.unit != Unit::pixel) {
                LOGW("Invalid unit provided for placement spacing");
            }
        }

        return Width(placementSpacing);
    }
    case StyleParamKey::repeat_distance:
    case StyleParamKey::text_repeat_distance: {
        ValueUnitPair repeatDistance;
        repeatDistance.unit = Unit::pixel;

        int pos = parseValueUnitPair(_value, 0, repeatDistance);
        if (pos < 0) {
            LOGW("Invalid repeat distance value '%s'", _value.c_str());
            repeatDistance.value =  256.0f;
            repeatDistance.unit = Unit::pixel;
        } else {
            if (repeatDistance.unit != Unit::pixel) {
                LOGW("Invalid unit provided for repeat distance");
            }
        }

        return Width(repeatDistance);
    }
    case StyleParamKey::width:
    case StyleParamKey::outline_width: {
        ValueUnitPair width;
        width.unit = Unit::meter;

        int pos = parseValueUnitPair(_value, 0, width);
        if (pos < 0) {
            LOGW("Invalid width value '%s'", _value.c_str());
            width.value =  2.0f;
            width.unit = Unit::pixel;
        }

        return Width(width);
    }
    case StyleParamKey::angle: {
        double num;
        if (_value == "auto") {
            return std::nanf("1");
        } else if (parseFloat(_value, num) > 0) {
            return static_cast<float>(num);
        }
        break;
    }
    case StyleParamKey::miter_limit:
    case StyleParamKey::outline_miter_limit:
    case StyleParamKey::placement_min_length_ratio:
    case StyleParamKey::text_font_stroke_width: {
        double num;
        if (parseFloat(_value, num) > 0) {
             return static_cast<float>(num);
        }
        break;
    }

    case StyleParamKey::color:
    case StyleParamKey::outline_color:
    case StyleParamKey::text_font_fill:
    case StyleParamKey::text_font_stroke_color:
        return parseColor(_value);

    case StyleParamKey::cap:
    case StyleParamKey::outline_cap:
        return static_cast<uint32_t>(CapTypeFromString(_value));

    case StyleParamKey::join:
    case StyleParamKey::outline_join:
        return static_cast<uint32_t>(JoinTypeFromString(_value));

    default:
        break;
    }

    return none_type{};
}
Пример #4
0
void Parser::startTag(const string& name_, StringPairList& attribs, bool simple) {
	auto name = boost::algorithm::trim_copy(name_);

	if(name == "br") {
		ret += _T("\\line\n");
	}

	if(simple) {
		return;
	}

	contexts.push_back(contexts.back());
	ScopedFunctor([this] { ret += contexts.back().getBegin(); });

	if(name == "b") {
		contexts.back().setFlag(Context::Bold);
	} else if(name == "i") {
		contexts.back().setFlag(Context::Italic);
	} else if(name == "u") {
		contexts.back().setFlag(Context::Underlined);
	}

	if(attribs.empty()) {
		return;
	}

	if(name == "a") {
		const auto& link = getAttrib(attribs, "href", 0);
		if(!link.empty()) {
			auto& context = contexts.back();
			context.link = link;
			if ((WinUtil::m_TextStyleURL.dwMask & CFE_UNDERLINE) == CFE_UNDERLINE)
				context.setFlag(Context::Underlined);
			context.textColor = addColor(WinUtil::m_TextStyleURL.crTextColor); /// @todo move to styles
		}
	}

	const auto& style = getAttrib(attribs, "style", 0);

	enum { Declaration, Font, Decoration, TextColor, BgColor, FontSize, Unknown } state = Declaration;

	string tmp;
	size_t i = 0, j;
	while((j = style.find_first_of(":;", i)) != string::npos) {
		tmp = style.substr(i, j - i);
		i = j + 1;

		boost::algorithm::trim(tmp);

		switch(state) {
		case Declaration:
			{
				if(tmp == "font") { state = Font; }
				else if(tmp == "color") { state = TextColor; }
				else if(tmp == "text-decoration") { state = Decoration; }
				else if(tmp == "background-color") { state = BgColor; }
				else if(tmp == "font-size") { state = FontSize; }
				else { state = Unknown; }
				break;
			}

		case Font:
			{
				parseFont(tmp);
				state = Declaration;
				break;
			}
		case FontSize:
			{
				parseFontSize(tmp);
				state = Declaration;
				break;
			}

		case Decoration:
			{
				parseDecoration(tmp);
				state = Declaration;
				break;
			}

		case TextColor:
			{
				parseColor(contexts.back().textColor, tmp);
				state = Declaration;
				break;
			}

		case BgColor:
			{
				parseColor(contexts.back().bgColor, tmp);
				state = Declaration;
				break;
			}

		case Unknown:
			{
				state = Declaration;
				break;
			}
		}
	}
}
Пример #5
0
StyleParam::Value StyleParam::parseString(StyleParamKey key, const std::string& _value) {

    switch (key) {
    case StyleParamKey::extrude: {
        if (_value == "true") { return glm::vec2(NAN, NAN); }
        if (_value == "false") { return glm::vec2(0, 0) ; }
        auto vec2 = glm::vec2(NAN, NAN);
        if (!parseVec2(_value, { Unit::meter, Unit::pixel }, vec2)) {
            LOGW("Invalid extrude parameter '%s'.", _value.c_str());
        }
        return vec2;
    }
    case StyleParamKey::text_wrap: {
        int textWrap;
        if (_value == "true") return textWrap;
        if (_value == "false") return std::numeric_limits<uint32_t>::max();
        if (parseInt(_value, textWrap) > 0) {
             return static_cast<uint32_t>(textWrap);
        }
    }
    case StyleParamKey::offset: {
        auto vec2 = glm::vec2(0.f, 0.f);
        if (!parseVec2(_value, { Unit::pixel }, vec2) || isnan(vec2.y)) {
            LOGW("Invalid offset parameter '%s'.", _value.c_str());
        }
        return vec2;
    }
    case StyleParamKey::size: {
        auto vec2 = glm::vec2(0.f, 0.f);
        if (!parseVec2(_value, { Unit::pixel }, vec2)) {
            LOGW("Invalid size parameter '%s'.", _value.c_str());
        }
        return vec2;
    }
    case StyleParamKey::transition_hide_time:
    case StyleParamKey::transition_show_time:
    case StyleParamKey::transition_selected_time: {
        float time = 0.0f;
        if (!parseTime(_value, time)) {
            LOGW("Invalid time param '%s'", _value.c_str());
        }
        return time;
    }
    case StyleParamKey::align:
    case StyleParamKey::anchor:
    case StyleParamKey::font_family:
    case StyleParamKey::font_weight:
    case StyleParamKey::font_style:
    case StyleParamKey::text_source:
    case StyleParamKey::transform:
    case StyleParamKey::sprite:
    case StyleParamKey::sprite_default:
    case StyleParamKey::style:
        return _value;
    case StyleParamKey::font_size: {
        float fontSize = 0.f;
        if (!parseFontSize(_value, fontSize)) {
            LOGW("Invalid font-size '%s'.", _value.c_str());
        }
        return fontSize;
    }
    case StyleParamKey::centroid:
    case StyleParamKey::interactive:
    case StyleParamKey::visible:
    case StyleParamKey::collide:
        if (_value == "true") { return true; }
        if (_value == "false") { return false; }
        LOGW("Bool value required for capitalized/visible. Using Default.");
        break;
    case StyleParamKey::order:
    case StyleParamKey::outline_order:
    case StyleParamKey::priority: {
        int num;
        if (parseInt(_value, num) > 0) {
             return static_cast<uint32_t>(num);
        }
        break;
    }
    case StyleParamKey::width:
    case StyleParamKey::outline_width: {
        ValueUnitPair width;
        width.unit = Unit::meter;

        int pos = parseValueUnitPair(_value, 0, width);
        if (pos < 0) {
            logMsg("Warning: Invalid width value '%s'\n", _value.c_str());
            width.value =  2.0f;
            width.unit = Unit::pixel;
        }

        return Width(width);
    }
    case StyleParamKey::font_stroke_width: {
        double num;
        if (parseFloat(_value, num) > 0) {
             return static_cast<float>(num);
        }
        break;
    }

    case StyleParamKey::color:
    case StyleParamKey::outline_color:
    case StyleParamKey::font_fill:
    case StyleParamKey::font_stroke_color:
        return parseColor(_value);

    case StyleParamKey::cap:
    case StyleParamKey::outline_cap:
        return static_cast<uint32_t>(CapTypeFromString(_value));

    case StyleParamKey::join:
    case StyleParamKey::outline_join:
        return static_cast<uint32_t>(JoinTypeFromString(_value));

    case StyleParamKey::none:
        break;
    }

    return none_type{};
}
Пример #6
0
StyleParam::Value StyleParam::parseString(StyleParamKey key, const std::string& _value) {

    switch (key) {
    case StyleParamKey::extrude: {
        if (_value == "true") { return glm::vec2(NAN, NAN); }
        if (_value == "false") { return glm::vec2(0, 0) ; }
        auto vec2 = glm::vec2(NAN, NAN);
        if (!parseVec2(_value, {"m", "px"}, vec2)) {
            logMsg("Warning: Badly formed extrude parameter %s.\n", _value.c_str());
        }
        return vec2;
    }
    case StyleParamKey::offset:
    case StyleParamKey::size: {
        auto vec2 = glm::vec2(0.f, 0.f);
        if (!parseVec2(_value, {"px"}, vec2)) {
            logMsg("Warning: Badly formed offset parameter %s.\n", _value.c_str());
        }
        return vec2;
    }
    case StyleParamKey::font_family:
    case StyleParamKey::font_weight:
    case StyleParamKey::font_style:
    case StyleParamKey::text_source:
    case StyleParamKey::transform:
    case StyleParamKey::sprite:
    case StyleParamKey::sprite_default:
        return _value;
    case StyleParamKey::font_size: {
        float fontSize = 16;
        if (!parseFontSize(_value, fontSize)) {
            logMsg("Warning: Invalid font-size '%s'.\n", _value.c_str());
        }
        return fontSize;
    }
    case StyleParamKey::visible:
        if (_value == "true") { return true; }
        if (_value == "false") { return false; }
        logMsg("Warning: Bool value required for capitalized/visible. Using Default.");
        break;
    case StyleParamKey::order:
    case StyleParamKey::priority: {
        try {
            return static_cast<uint32_t>(std::stoi(_value));
        } catch (std::invalid_argument) {
        } catch (std::out_of_range) {}
        logMsg("Warning: Not an Integer '%s', key: '%s'",
               _value.c_str(), keyName(key));
        break;
    }
    case StyleParamKey::width:
    case StyleParamKey::outline_width:
    case StyleParamKey::font_stroke_width: {
        try {
            return static_cast<float>(std::stof(_value));
        } catch (std::invalid_argument) {
        } catch (std::out_of_range) {}
        logMsg("Warning: Not a Float '%s', key: '%s'",
               _value.c_str(), keyName(key));
        break;
    }
    case StyleParamKey::color:
    case StyleParamKey::outline_color:
    case StyleParamKey::font_fill:
    case StyleParamKey::font_stroke:
    case StyleParamKey::font_stroke_color:
        return parseColor(_value);

    case StyleParamKey::cap:
    case StyleParamKey::outline_cap:
        return static_cast<uint32_t>(CapTypeFromString(_value));

    case StyleParamKey::join:
    case StyleParamKey::outline_join:
        return static_cast<uint32_t>(JoinTypeFromString(_value));

    case StyleParamKey::none:
        break;
    }

    return none_type{};
}