Skip to content

Commit

Permalink
Support font resizing and improve print preview accuracy
Browse files Browse the repository at this point in the history
  • Loading branch information
woshibiantai committed Jul 21, 2024
1 parent 72a4941 commit a543e96
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/components/ChordChartTag.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const isKeychange = props.tag.name === 'keychange';
.chordchart-keychange {
display: block;
margin-left: auto;
flex: 1;
text-align: right;
}
</style>
18 changes: 18 additions & 0 deletions src/components/PrintFormatToolbar.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
<template>
<menu>
<div class="field">
<label for="fontSizeInput">Font size:</label>
<input
id="fontSizeInput"
min="1"
type="number"
:value="fontSize"
@input="onFontSizeInput"
/>
</div>
<div class="field">
<label for="columnSelect">Number of columns:</label>
<select
Expand Down Expand Up @@ -31,6 +41,7 @@

<script setup>
const columns = defineModel('columns', { type: Number, default: 1 });
const fontSize = defineModel('fontSize', { type: Number, default: 16 });
function onPrint() {
window.print();
Expand All @@ -39,6 +50,13 @@ function onPrint() {
function updateColumns(event) {
columns.value = Number(event.target.value);
}
function onFontSizeInput(event) {
const value = Number(event.target.value);
if (value > 0) {
fontSize.value = value;
}
}
</script>

<style scoped>
Expand Down
22 changes: 14 additions & 8 deletions src/views/PrintView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
<section class="no-print">
<PrintFormatToolbar
v-model:columns="columns"
v-model:fontSize="fontSize"
/>
</section>
<div
class="print-view"
:style="{
'--font-size': `${fontSize}px`,
'--transform-scale': transformScale,
'--width': `${MAIN_MAX_WIDTH}px`
}"
>
<ChordChart
Expand All @@ -24,10 +25,11 @@ import { computed, defineAsyncComponent, onMounted, onUnmounted, ref } from 'vue
const ChordChart = defineAsyncComponent(() => import('../components/ChordChart.vue'));
const PrintFormatToolbar = defineAsyncComponent(() => import('../components/PrintFormatToolbar.vue'));
const MAIN_MAX_WIDTH = 1216;
const main = ref(null);
const A4_WIDTH = 595; // Width of A4 paper in points
const columns = ref(1);
const mainWidth = ref(MAIN_MAX_WIDTH);
const fontSize = ref(16);
const main = ref(null);
const mainWidth = ref(A4_WIDTH);
const transformScale = computed(computeTransformScale);
onMounted(() => {
Expand All @@ -40,7 +42,8 @@ onUnmounted(() => {
});
function computeTransformScale() {
return mainWidth.value < MAIN_MAX_WIDTH ? mainWidth.value / MAIN_MAX_WIDTH : 1;
const widthInPt = mainWidth.value * 3 / 4;
return widthInPt < A4_WIDTH ? widthInPt / A4_WIDTH : 1;
}
function updateMainWidth() {
Expand All @@ -50,16 +53,19 @@ function updateMainWidth() {

<style scoped>
.print-view {
aspect-ratio: 1 / 1.414;
font-size: var(--font-size, 16pt);
height: 894pt;
margin: 0 auto;
outline: 1px solid #ccc;
padding: 2em;
transform: scale(var(--transform-scale));
transform-origin: top left;
width: var(--width);
width: 595pt;
@media print {
transform: none;
outline: none;
padding: 0;
transform: none;
}
}
</style>
Expand Down

0 comments on commit a543e96

Please sign in to comment.