Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/fileformats/ocd_file_export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,23 +423,23 @@ Ocd::OcdPoint32 convertPoint(const MapCoord& coord)


/**
* Convert a size to the OCD format.
* Convert a size or offset to the OCD format.
*
* This function converts from 1/100 mm to 1/10 mm, rounding half up for positive values.
* This function converts from 1/100 mm to 1/10 mm, rounding halfway cases away from zero.
*/
constexpr qint16 convertSize(qint32 size)
{
return qint16((size+5) / 10);
return qint16(((size < 0) ? (size-5) : (size+5)) / 10);
}

/**
* Convert a size to the OCD format.
*
* This function converts from 1/100 mm to 1/10 mm, rounding half up for positive values.
* This function converts from 1/100 mm to 1/10 mm, rounding halfway cases away from zero.
*/
constexpr qint32 convertSize(qint64 size)
{
return qint32((size+5) / 10);
return qint32(((size < 0) ? (size-5) : (size+5)) / 10);
}


Expand Down Expand Up @@ -1877,6 +1877,7 @@ QByteArray OcdFileExport::exportTextSymbol(const TextSymbol* text_symbol, quint3
setupTextSymbolExtra(text_symbol, ocd_symbol);
setupTextSymbolBasic(text_symbol, alignment, ocd_symbol.basic);
setupTextSymbolSpecial(text_symbol, ocd_symbol.special);
setupTextSymbolFraming(text_symbol, ocd_symbol.framing);

auto header_size = int(sizeof(OcdTextSymbol));
ocd_symbol.base.size = decltype(ocd_symbol.base.size)(header_size);
Expand Down Expand Up @@ -1963,6 +1964,7 @@ void OcdFileExport::setupTextSymbolFraming(const TextSymbol* text_symbol, OcdTex
if (text_symbol->getFramingColor())
{
ocd_text_framing.color = convertColor(text_symbol->getFramingColor());
ocd_text_framing.line_style_V9 = (ocd_version >= 9) ? /* miter join */ 4 : 0;
switch (text_symbol->getFramingMode())
{
case TextSymbol::NoFraming:
Expand Down
6 changes: 6 additions & 0 deletions src/fileformats/ocd_file_import.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2355,7 +2355,13 @@ void OcdFileImport::setFraming(OcdFileImport::OcdImportedTextSymbol* symbol, con
case Ocd::FramingLine: // since V7
symbol->framing = true;
symbol->framing_mode = TextSymbol::LineFraming;
symbol->framing_color = convertColor(framing.color);
symbol->framing_line_half_width = convertLength(framing.line_width);
if (ocd_version >= 9)
{
if (framing.line_style_V9 != 0 && framing.line_style_V9 != 4)
addSymbolWarning(symbol, tr("Ignoring text framing line style."));
}
break;
case Ocd::FramingRectangle:
default:
Expand Down
4 changes: 2 additions & 2 deletions src/fileformats/ocd_types_v8.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,8 @@ namespace Ocd
quint16 line_width;
quint16 font_weight; /// TextSymbol only
quint16 italic; /// TextSymbol only
quint16 offset_x;
quint16 offset_y;
qint16 offset_x;
qint16 offset_y;
};

struct TextSymbolV8
Expand Down
29 changes: 29 additions & 0 deletions test/data/text-symbol.omap
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<map xmlns="http://openorienteering.org/apps/mapper/xml/v2" version="9">
<notes></notes>
<georeferencing scale="10000"><projected_crs id="Local"/></georeferencing>
<colors count="2">
<color priority="0" name="Black 100%" c="0" m="0" y="0" k="1" opacity="1"><spotcolors knockout="true"><namedcolor>BLACK</namedcolor></spotcolors><cmyk method="custom"/><rgb method="cmyk" r="0" g="0" b="0"/></color>
<color priority="1" name="Purple" c="0.35" m="0.85" y="0" k="0" opacity="1"><spotcolors knockout="true"><namedcolor>PURPLE</namedcolor></spotcolors><cmyk method="custom"/><rgb method="cmyk" r="0.65" g="0.15" b="1"/></color>
</colors>
<barrier version="6" required="0.6.0">
<symbols count="5">
<symbol type="8" id="0" code="1.1" name="Text symbol with negative shadow framing offset"><text_symbol icon_text="" rotatable="true"><font family="Arial" size="6350"/><text color="0" line_spacing="1" paragraph_spacing="0" character_spacing="0" kerning="true"/><framing color="1" mode="2" line_half_width="200" shadow_x_offset="-400" shadow_y_offset="-500"/></text_symbol></symbol>
<symbol type="8" id="1" code="1.2" name="Text symbol with line framing"><text_symbol icon_text="" rotatable="true"><font family="Arial" size="6350"/><text color="1" line_spacing="1" paragraph_spacing="0" character_spacing="0" kerning="true"/><framing color="0" mode="1" line_half_width="200" shadow_x_offset="-200" shadow_y_offset="200"/></text_symbol></symbol>
<symbol type="8" id="2" code="1.3" name="Text symbol without framing"><text_symbol icon_text="" rotatable="true"><font family="Arial" size="6350"/><text color="1" line_spacing="1" paragraph_spacing="0" character_spacing="0" kerning="true"/></text_symbol></symbol>
<symbol type="8" id="3" code="1.4" name="Text symbol with line below"><text_symbol icon_text="" rotatable="true"><font family="Arial" size="6350"/><text color="1" line_spacing="1" paragraph_spacing="0" character_spacing="0" kerning="true"/><line_below color="0" width="250" distance="150"/></text_symbol></symbol>
<symbol type="8" id="4" code="1.5" name="Text symbol with tabs"><text_symbol icon_text="" rotatable="true"><font family="Arial" size="6350"/><text color="1" line_spacing="1" paragraph_spacing="0" character_spacing="0" kerning="true"/><tabs count="4"><tab>0</tab><tab>1010</tab><tab>20200</tab><tab>300000</tab></tabs></text_symbol></symbol>
</symbols>
<parts count="1" current="0">
<part name="default part"><objects count="0">
</objects></part>
</parts>
<templates count="0" first_front_template="0">
<defaults use_meters_per_pixel="true" meters_per_pixel="0" dpi="0" scale="0"/>
</templates>
<view>
<grid color="#80646464" display="0" alignment="0" additional_rotation="0" unit="1" h_spacing="500" v_spacing="500" h_offset="0" v_offset="0" snapping_enabled="true"/>
<map_view zoom="1" position_x="0" position_y="0"><map opacity="1" visible="true"/><templates count="0"/></map_view>
</view>
</barrier>
</map>
103 changes: 100 additions & 3 deletions test/file_format_t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,17 @@
#include "core/objects/object.h"
#include "core/objects/text_object.h"
#include "core/symbols/area_symbol.h"
#include "core/symbols/symbol.h"
#include "core/symbols/line_symbol.h"
#include "core/symbols/symbol.h"
#include "core/symbols/text_symbol.h"
#include "fileformats/file_format.h"
#include "fileformats/file_format_registry.h"
#include "fileformats/file_import_export.h"
#include "fileformats/iof_course_export.h"
#include "fileformats/kml_course_export.h"
#include "fileformats/ocd_file_export.h"
#include "fileformats/ocd_file_import.h"
#include "fileformats/ocd_file_format.h"
#include "fileformats/ocd_file_import.h"
#include "fileformats/ocd_types.h"
#include "fileformats/ocd_types_v8.h"
#include "fileformats/ocd_types_v12.h"
Expand Down Expand Up @@ -143,7 +144,7 @@ namespace QTest
ba += ", overprinting";
return qstrdup(ba.data());
}
}
} // namespace QTest

#endif

Expand Down Expand Up @@ -1787,6 +1788,102 @@ void FileFormatTest::ocdAreaSymbolTest()
}
}

void FileFormatTest::ocdTextSymbolTest_data()
{
QTest::addColumn<int>("format_version");

#ifndef MAPPER_BIG_ENDIAN
static struct { int version; const char* id; } const tests[] = {
{ 8, "OCD8" },
{ 9, "OCD9" },
{ 10, "OCD10" },
{ 11, "OCD11" },
{ 12, "OCD12" },
};
for (auto& t : tests)
{
QTest::newRow(t.id) << t.version;
}
#endif
}

void FileFormatTest::ocdTextSymbolTest()
{
QFETCH(int, format_version);

auto* format_id = QTest::currentDataTag();
const auto* format = FileFormats.findFormat(format_id);
QVERIFY(format);

static const auto filepath = QString::fromLatin1("data:text-symbol.omap");
QVERIFY(QFileInfo::exists(filepath));

// Load the test map
auto expected = std::make_unique<Map>();
QVERIFY(expected->loadFrom(filepath));

// Save and load the map
auto actual = saveAndLoadMap(*expected, format);
QVERIFY2(actual, "Exception while importing / exporting.");
QCOMPARE(actual->property(OcdFileFormat::versionProperty()), format_version);

// Symbols
QCOMPARE(actual->getNumSymbols(), expected->getNumSymbols());
for (int i = 0; i < actual->getNumSymbols(); ++i)
{
auto* expected_symbol = expected->getSymbol(i);
if (expected_symbol->getType() != Symbol::Text) // actually redundant for the given test data
continue;

auto* actual_symbol = actual->getSymbol(i);
QCOMPARE(actual_symbol->getType(), Symbol::Text);

COMPARE_SYMBOL_PROPERTY(actual_symbol->getNumberComponent(0), expected_symbol->getNumberComponent(0), *expected_symbol);
// OCD limitation: always two number components
if (expected_symbol->getNumberComponent(1) != -1)
COMPARE_SYMBOL_PROPERTY(actual_symbol->getNumberComponent(1), expected_symbol->getNumberComponent(1), *expected_symbol);

QVERIFY(expected_symbol->stateEquals(actual_symbol));

auto* expected_text_symbol = expected_symbol->asText();
auto* actual_text_symbol = actual_symbol->asText();

COMPARE_SYMBOL_PROPERTY(bool(actual_text_symbol->getColor()), bool(expected_text_symbol->getColor()), *expected_text_symbol);
if (expected_text_symbol->getColor())
COMPARE_SYMBOL_PROPERTY(actual_text_symbol->getColor()->operator QRgb(), expected_text_symbol->getColor()->operator QRgb(), *expected_text_symbol);
COMPARE_SYMBOL_PROPERTY(bool(actual_text_symbol->getFramingColor()), bool(expected_text_symbol->getFramingColor()), *expected_text_symbol);
if (expected_text_symbol->getFramingColor())
COMPARE_SYMBOL_PROPERTY(actual_text_symbol->getFramingColor()->operator QRgb(), expected_text_symbol->getFramingColor()->operator QRgb(), *expected_text_symbol);
COMPARE_SYMBOL_PROPERTY(bool(actual_text_symbol->getLineBelowColor()), bool(expected_text_symbol->getLineBelowColor()), *expected_text_symbol);
if (expected_text_symbol->getLineBelowColor())
COMPARE_SYMBOL_PROPERTY(actual_text_symbol->getLineBelowColor()->operator QRgb(), expected_text_symbol->getLineBelowColor()->operator QRgb(), *expected_text_symbol);

COMPARE_SYMBOL_PROPERTY(actual_text_symbol->getFramingMode(), expected_text_symbol->getFramingMode(), *expected_text_symbol);
if (expected_text_symbol->getFramingMode() == TextSymbol::FramingMode::ShadowFraming)
{
COMPARE_SYMBOL_PROPERTY(actual_text_symbol->getFramingShadowYOffset(), expected_text_symbol->getFramingShadowYOffset(), *expected_text_symbol);
COMPARE_SYMBOL_PROPERTY(actual_text_symbol->getFramingShadowXOffset(), expected_text_symbol->getFramingShadowXOffset(), *expected_text_symbol);
}
if (expected_text_symbol->getFramingMode() == TextSymbol::FramingMode::LineFraming)
{
COMPARE_SYMBOL_PROPERTY(actual_text_symbol->getFramingLineHalfWidth(), expected_text_symbol->getFramingLineHalfWidth(), *expected_text_symbol);
}

if (expected_text_symbol->hasLineBelow())
{
COMPARE_SYMBOL_PROPERTY(actual_text_symbol->getLineBelowWidth(), expected_text_symbol->getLineBelowWidth(), *expected_text_symbol);
COMPARE_SYMBOL_PROPERTY(actual_text_symbol->getLineBelowDistance(), expected_text_symbol->getLineBelowDistance(), *expected_text_symbol);
}

COMPARE_SYMBOL_PROPERTY(actual_text_symbol->getNumCustomTabs(), expected_text_symbol->getNumCustomTabs(), *expected_text_symbol);
if (expected_text_symbol->getNumCustomTabs())
{
for (int i = 0; i < expected_text_symbol->getNumCustomTabs(); ++i)
COMPARE_SYMBOL_PROPERTY(actual_text_symbol->getCustomTab(i), expected_text_symbol->getCustomTab(i), *expected_text_symbol);
}
}
}


/*
* We don't need a real GUI window.
Expand Down
8 changes: 7 additions & 1 deletion test/file_format_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,13 @@ private slots:
*/
void ocdAreaSymbolTest_data();
void ocdAreaSymbolTest();


/**
* Test OCD text symbol export and import.
*/
void ocdTextSymbolTest_data();
void ocdTextSymbolTest();

};

#endif // OPENORIENTEERING_FILE_FORMAT_T_H