Commit 22cae6ee by ramdayalmunda

margin and padding added on the text written

parent aeefff86
...@@ -9,12 +9,12 @@ var ADocEditor = function (customConfig) { ...@@ -9,12 +9,12 @@ var ADocEditor = function (customConfig) {
pageSetup: { size: "A4" }, zoom: 1, pageSetup: { size: "A4" }, zoom: 1,
format: { format: {
background: "#fff", background: "#fff",
margin: 20, margin: 20, // mm
border: "", border: "",
tabWidth: 20, tabWidth: 20, // mm
}, },
style: { style: {
fontSize: 100, fontSize: 16, // this is in mm
fontFamily: 'Calibri', fontFamily: 'Calibri',
bold: false, bold: false,
italic: false, italic: false,
...@@ -199,7 +199,7 @@ var ADocEditor = function (customConfig) { ...@@ -199,7 +199,7 @@ var ADocEditor = function (customConfig) {
function calculateTextSizeAndPosition() { function calculateTextSizeAndPosition() {
let d = 0, c=0; let d = 0, c = 0;
function getLineObj() { function getLineObj() {
let newLineObj = { let newLineObj = {
...@@ -224,25 +224,25 @@ var ADocEditor = function (customConfig) { ...@@ -224,25 +224,25 @@ var ADocEditor = function (customConfig) {
// to calculate the lines // to calculate the lines
let tempLineWidth = 0; let tempLineWidth = 0;
let maxLineWidth = config.pageSetup.pxWidth - (config.format.margin * config.pageSetup.pxWidth*2)/100 let maxLineWidth = config.pageSetup.pxWidth - (config.format.margin * pxMmRatio*2)
let wordEndIndex = 0; // this stores the index of the word which can fit in the line; let wordEndIndex = 0; // this stores the index of the word which can fit in the line;
for (c = 0; c < dataBlock.plainContent.length; c++) { for (c = 0; c < dataBlock.plainContent.length; c++) {
let style = dataBlock?.formatedText?.[d] let style = dataBlock?.formatedText?.[c]
if (/\s/.test(dataBlock.plainContent[c])) { if (/\s/.test(dataBlock.plainContent[c])) {
wordEndIndex = c wordEndIndex = c
lineObj.charEndIndex = c lineObj.charEndIndex = c
} }
let charWidth = getCharacterWidth(d, dataBlock.plainContent[c], style ) let charWidth = getCharacterWidth(dataBlock.plainContent[c], style)
dataBlock.formatedText[c] = { dataBlock.formatedText[c] = {
...config.style, ...config.style,
...style, ...style,
width: charWidth width: charWidth
} }
tempLineWidth += charWidth tempLineWidth += charWidth
if (tempLineWidth > maxLineWidth){ if (tempLineWidth > maxLineWidth) {
// cannot add this// new line should be added// // cannot add this// new line should be added//
i = wordEndIndex; i = wordEndIndex;
lineObj.plainContent = dataBlock.plainContent.slice(lineObj.charStartIndex, lineObj.charEndIndex + 1) lineObj.plainContent = dataBlock.plainContent.slice(lineObj.charStartIndex, lineObj.charEndIndex + 1)
lines.push(lineObj) lines.push(lineObj)
lineObj = new getLineObj() lineObj = new getLineObj()
...@@ -275,21 +275,21 @@ var ADocEditor = function (customConfig) { ...@@ -275,21 +275,21 @@ var ADocEditor = function (customConfig) {
function renderLines() { function renderLines() {
console.log('render lines', lines) console.log('render lines', lines)
let x = 0, y=0; let x = 0, y = config.format.margin*pxMmRatio;
for ( let l=0; l<lines.length; l++){ for (let l = 0; l < lines.length; l++) {
let ctx = pageList[0].el.getContext('2d', { willReadFrequently: true }) let ctx = pageList[0].el.getContext('2d', { willReadFrequently: true })
console.log(lines[l]) console.log(lines[l])
x = 0 x = config.format.margin*pxMmRatio
y = y+lines[l].maxFontSize y = y + (lines[l].maxFontSize * pxMmRatio)
ctx.save() ctx.save()
let setData = dataList[lines[l].dataIndex] let setData = dataList[lines[l].dataIndex]
for (let c = lines[l].charStartIndex; c <= lines[l].charEndIndex; c++) { for (let c = lines[l].charStartIndex; c <= lines[l].charEndIndex; c++) {
let char = setData?.plainContent[c] let char = setData?.plainContent[c]
if (char) { if (char) {
let style = setData.formatedText[c] let style = setData.formatedText[c]
ctx.save() ctx.save()
ctx.font = `${style?.bold ? 'bold ' : ''}${style?.italic ? 'italic ' : ''} ${style.fontSize}px ${style.fontFamily}` ctx.font = `${style?.bold ? 'bold ' : ''}${style?.italic ? 'italic ' : ''} ${style.fontSize * pxMmRatio}px ${style.fontFamily}`
ctx.fillStyle = `${style?.fontColor}` ctx.fillStyle = `${style?.fontColor}`
ctx.fillText(char, x, y) ctx.fillText(char, x, y)
setData.formatedText[c].x = x setData.formatedText[c].x = x
...@@ -352,11 +352,12 @@ var ADocEditor = function (customConfig) { ...@@ -352,11 +352,12 @@ var ADocEditor = function (customConfig) {
renderCaret() renderCaret()
}, caretData.timeoutDuration) }, caretData.timeoutDuration)
} }
function getCharacterWidth(canvasIndex, char, style) { function getCharacterWidth(char, style) {
let canvas = pageList[canvasIndex].el console.log('style', style)
let canvas = pageList[0].el
let ctx = canvas.getContext('2d', { willReadFrequently: true }) let ctx = canvas.getContext('2d', { willReadFrequently: true })
ctx.save() ctx.save()
ctx.font = `${style?.bold ? 'bold ' : ''}${style?.italic ? 'italic ' : ''} ${style.fontSize * config.pageSetup.multipler}px ${style.fontFamily}` ctx.font = `${style?.bold ? 'bold ' : ''}${style?.italic ? 'italic ' : ''} ${style.fontSize * pxMmRatio}px ${style.fontFamily}`
ctx.fillStyle = `${style?.fontColor}` ctx.fillStyle = `${style?.fontColor}`
let width = ctx.measureText(char).width let width = ctx.measureText(char).width
ctx.restore() ctx.restore()
......
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
</head> </head>
<body> <body>
<div class="body"></div> <div class="body"></div>
<p>Click on the <span style="font-family: 'customfont';">canvas</span> and start typing <p style="font-family: 'Calibri';">Click on the <span>canvas</span> and start typing
<button onclick="extractData()">Extract</button> <button onclick="extractData()">Extract</button>
<button onclick="setData()">Set Data</button> <button onclick="setData()">Set Data</button>
<button onclick="generatePDF()">Generate PDF</button> <button onclick="generatePDF()">Generate PDF</button>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment