Skip to content

Commit

Permalink
Export print area to ppen; clear restrictToPage when manual extent is…
Browse files Browse the repository at this point in the history
… set
  • Loading branch information
perliedman committed Dec 29, 2024
1 parent bf5d555 commit 32c51ff
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 118 deletions.
26 changes: 26 additions & 0 deletions src/models/control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,29 @@ export interface Control {
coordinates: number[];
description: Description;
}

export function toPpen(c: Control) {
return {
type: "control",
id: c.id,
attrs: {
kind: c.kind,
},
children: [
{
type: "location",
attrs: { x: c.coordinates[0], y: c.coordinates[1] },
},
...(c.code ? [{ type: "code", text: c.code.toString() }] : []),
...Object.keys(c.description)
.filter((box) => c.description[box as keyof Description])
.map((box) => ({
type: "description",
attrs: {
box,
"iof-2004-ref": c.description[box as keyof Description],
},
})),
],
};
}
12 changes: 12 additions & 0 deletions src/models/course-appearance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ export function create(options?: CourseAppearance): CourseAppearance {
};
}

export function toPpen(courseAppearance: CourseAppearance) {
return {
type: "course-appearance",
attrs: {
"scale-sizes": courseAppearance.scaleSizes,
"scale-sizes-circle-gaps": courseAppearance.scaleSizesCircleGaps,
"auto-leg-gap-size": courseAppearance.autoLegGapSize,
"blend-purple": courseAppearance.blendPurple,
},
};
}

export interface CourseAppearance {
scaleSizes: "RelativeToMap" | "RelativeTo15000" | "None";
scaleSizesCircleGaps: boolean;
Expand Down
47 changes: 47 additions & 0 deletions src/models/course.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Control, controlDistance } from "./control";
import * as PrintArea from "./print-area";
import { PrintArea as PrintAreaType } from "./print-area";
import { SpecialObject } from "./special-object";
import { ALL_CONTROLS_ID } from "./event";

export interface Course {
id: number;
Expand Down Expand Up @@ -90,3 +91,49 @@ export function getStartRotation({ controls }: Course): number {
}
return 0;
}

export function toPpen(courses: Course[]) {
let id = 1;

return courses
.filter((course) => course.id !== ALL_CONTROLS_ID)
.map((course, i) => {
const ids = course.controls.map(() => ++id);
return [
{
type: "course",
id: course.id,
attrs: {
kind: "normal",
order: i + 1,
},
children: [
{ type: "name", text: course.name },
{ type: "labels", attrs: { "label-kind": "sequence" } },
{
type: "options",
attrs: {
"print-scale": course.printScale,
load: 10, // TODO: what?
"description-kind": "symbols",
},
},
...(course.printArea ? [PrintArea.toPpen(course.printArea)] : []),
...(course.controls.length > 0
? [{ type: "first", attrs: { "course-control": ids[0] } }]
: []),
],
},
...course.controls.map((control, i, cs) => ({
type: "course-control",
id: ids[i],
attrs: { control: control.id },
children:
i < cs.length - 1
? [{ type: "next", attrs: { "course-control": ids[i + 1] } }]
: [],
})),
];
})
.flat();
}
52 changes: 51 additions & 1 deletion src/models/event.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import cloneDeep from "lodash/cloneDeep";
import { cloneDeep } from "lodash";
import { MakeOptional } from "../ts-utils";
import * as Control from "./control";
import { Control as ControlType } from "./control";
Expand Down Expand Up @@ -229,3 +229,53 @@ const sequence = (
function toFinite(x: number, fallback: number): number {
return !isNaN(x) && Math.abs(x) !== Infinity ? x : fallback;
}

type Xml = {
type: string;
id?: string | number;
text?: string;
attrs?: Record<string, string | boolean | number | undefined>;
children?: Xml[];
};

export function toPpen(event: Event): Xml {
const allControls = event.courses.find((c) => c.id === ALL_CONTROLS_ID);
return {
type: "course-scribe-event",
children: [
{
type: "event",
id: 1,
children: [
{ type: "title", text: event.name },
{
type: "map",
attrs: {
kind: "OCAD",
scale: event.mapScale,
"ignore-missing-fonts": false,
"absolute-path": event.mapFilename,
text: event.mapFilename,
},
},
{ type: "standards", attrs: { map: "2017", description: "2018" } },
...(allControls
? [
{
type: "all-controls",
attrs: {
"print-scale": allControls.printScale,
"description-kind": "symbols",
},
},
CourseAppearance.toPpen(event.courseAppearance),
]
: []),
PrintArea.toPpen(event.printArea),
],
},
...Object.values(event.controls).map((c) => Control.toPpen(c)),
...Course.toPpen(event.courses),
],
};
}
25 changes: 24 additions & 1 deletion src/models/print-area.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const {
export function create(options?: PrintArea): PrintArea {
return {
auto: true,
restrictToPage: true,
restrictToPage: false,
pageWidth: defaultWidth,
pageHeight: defaultHeight,
...options,
Expand All @@ -33,3 +33,26 @@ export interface PrintArea {
*/
extent?: Extent;
}

export function toPpen(printArea: PrintArea) {
//<print-area automatic="true" restrict-to-page-size="true" left="0" top="0" right="0" bottom="0" page-width="827" page-height="1169" page-margins="0" page-landscape="false" />
return {
type: "print-area",
attrs: {
automatic: printArea.auto,
"restrict-to-page-size": printArea.restrictToPage,
"page-width": printArea.pageWidth,
"page-height": printArea.pageHeight,
"page-margins": 0,
"page-landscape": false,
...(printArea.extent
? {
left: printArea.extent[0],
bottom: printArea.extent[1],
right: printArea.extent[2],
top: printArea.extent[3],
}
: {}),
},
};
}
117 changes: 1 addition & 116 deletions src/services/ppen.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import flatten from "arr-flatten";
import * as Event from "../models/event";
import * as Control from "../models/control";
import * as Course from "../models/course";
import * as PrintArea from "../models/print-area";
import * as CourseAppearance from "../models/course-appearance";
import Projection from "ol/proj/Projection";
import { createXml } from "./xml-utils";
import { ALL_CONTROLS_ID } from "../models/event";

export function parsePPen(doc) {
const eventTag = doc.getElementsByTagName("event")[0];
Expand Down Expand Up @@ -214,122 +212,9 @@ export function parsePPen(doc) {
}
}

const courses = (courses) => {
let id = 1;

return flatten(
courses.map((course, i) => {
const ids = course.controls.map(() => ++id);
return [
{
type: "course",
id: course.id,
attrs: {
kind: "normal",
order: i + 1,
},
children: [
{ type: "name", text: course.name },
{ type: "labels", attrs: { "label-kind": "sequence" } },
{
type: "options",
attrs: {
"print-scale": course.printScale,
load: 10, // TODO: what?
"description-kind": "symbols",
},
},
].concat(
course.controls.length > 0
? [{ type: "first", attrs: { "course-control": ids[0] } }]
: []
),
},
].concat(
course.controls.map((control, i, cs) => ({
type: "course-control",
id: ids[i],
attrs: { control: control.id },
children:
i < cs.length - 1
? [{ type: "next", attrs: { "course-control": ids[i + 1] } }]
: [],
}))
);
})
);
};

export function writePpen(event) {
const doc = document.implementation.createDocument("", "", null);
const allControls = event.courses.find((c) => c.id === ALL_CONTROLS_ID);
const root = createXml(doc, {
type: "course-scribe-event",
children: [
{
type: "event",
id: 1,
children: [
{ type: "title", text: event.name },
{
type: "map",
attrs: {
kind: "OCAD",
scale: event.mapScale,
"ignore-missing-fonts": false,
"absolute-path": event.mapFilename,
text: event.mapFilename,
},
},
{ type: "standards", attrs: { map: "2017", description: "2018" } },
{
type: "all-controls",
attrs: {
"print-scale": allControls.printScale,
"description-kind": "symbols",
},
},
{
type: "course-appearance",
attrs: {
"scale-sizes": event.courseAppearance.scaleSizes,
"scale-sizes-circle-gaps":
event.courseAppearance.scaleSizesCircleGaps,
"auto-leg-gap-size": event.courseAppearance.autoLegGapSize,
"blend-purple": event.courseAppearance.blendPurple,
},
},
],
},
]
.concat(
Object.values(event.controls).map((c) => ({
type: "control",
id: c.id,
attrs: {
kind: c.kind,
},
children: [
{
type: "location",
attrs: { x: c.coordinates[0], y: c.coordinates[1] },
},
]
.concat(c.code ? [{ type: "code", text: c.code.toString() }] : [])
.concat(
Object.keys(c.description)
.filter((box) => c.description[box])
.map((box) => ({
type: "description",
attrs: { box, "iof-2004-ref": c.description[box] },
}))
),
}))
)
.concat(
courses(event.courses.filter((course) => course.id !== ALL_CONTROLS_ID))
),
});
const root = createXml(doc, Event.toPpen(event));

doc.appendChild(root);
return doc;
Expand Down
1 change: 1 addition & 0 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ const useEvent = create<StateWithActions>(
? draft.printArea
: findCourse(draft, courseId).printArea;
printArea.auto = false;
printArea.restrictToPage = false;
printArea.extent = extent;

if (isAllControls) {
Expand Down

0 comments on commit 32c51ff

Please sign in to comment.