From 092797fb4a8ab00d4acacc6fa6baa69b214760de Mon Sep 17 00:00:00 2001 From: Nikita Khromov Date: Wed, 25 Oct 2023 12:46:23 +0800 Subject: [PATCH] [pdf] Fixed vert align text in text/combobox form --- pdf/src/forms/base/base.js | 41 +++++++++++++++++++++++++++--- pdf/src/forms/base/basecheckbox.js | 3 +++ pdf/src/forms/combobox.js | 14 +++++++--- pdf/src/forms/text.js | 15 ++++++++--- pdf/src/viewer.js | 2 +- 5 files changed, 65 insertions(+), 10 deletions(-) diff --git a/pdf/src/forms/base/base.js b/pdf/src/forms/base/base.js index 5686fce9b3..502c50bc00 100644 --- a/pdf/src/forms/base/base.js +++ b/pdf/src/forms/base/base.js @@ -1822,14 +1822,14 @@ let oPara = this.content.GetElement(0); let oApiPara = editor.private_CreateApiParagraph(oPara); - oApiPara.SetFontSize(nSize); + oApiPara.SetFontSize(nSize * 2); oPara.RecalcCompiledPr(true); } if (this.contentFormat) { let oPara = this.contentFormat.GetElement(0); let oApiPara = editor.private_CreateApiParagraph(oPara); - oApiPara.SetFontSize(nSize); + oApiPara.SetFontSize(nSize * 2); oPara.RecalcCompiledPr(true); } } @@ -1997,8 +1997,42 @@ memory.WriteLong(annotFlags); memory.Seek(nEndPos); }; + CBaseField.prototype.GetFontSizeAP = function() { + let oPara = this.content.GetElement(0); + let oRun = oPara.GetElement(0); + let oTextPr = oRun.Get_CompiledPr(true); + return oTextPr.FontSize; + }; CBaseField.prototype.WriteToBinaryBase2 = function(memory) { + // font name + let sFontName = this.GetTextFont(); + if (sFontName != null) { + memory.WriteString(sFontName); + } + + // text size + let nFontSize = this.GetTextSize(); + if (nFontSize != null) { + memory.WriteDouble(nFontSize); + } + + // text size for ap + memory.WriteDouble(this.GetFontSizeAP()); + + // text style + let nStyle = 0; + let isBold = true; + let isItalic = true; + if (isBold) { + nStyle |= (1 << 0); + } + if (isItalic) { + nStyle |= (1 << 1); + } + memory.WriteLong(nStyle); + + // text color let aTextColor = this.GetTextColor(); if (aTextColor && aTextColor.length != 0) { memory.WriteLong(aTextColor.length); @@ -2007,7 +2041,8 @@ memory.WriteDouble(aTextColor[i]); } } - + + // align if (this.GetType() == AscPDF.FIELD_TYPES.text) { let nAlignType = this.GetAlign(); memory.WriteByte(nAlignType); diff --git a/pdf/src/forms/base/basecheckbox.js b/pdf/src/forms/base/basecheckbox.js index 9e120c25ba..603b5735af 100644 --- a/pdf/src/forms/base/basecheckbox.js +++ b/pdf/src/forms/base/basecheckbox.js @@ -382,6 +382,9 @@ this.AddActionsToQueue(AscPDF.FORMS_TRIGGERS_TYPES.OnFocus); oDoc.activeForm = this; }; + CBaseCheckBoxField.prototype.GetFontSizeAP = function() { + return 12; + }; CBaseCheckBoxField.prototype.onMouseEnter = function() { this.AddActionsToQueue(AscPDF.FORMS_TRIGGERS_TYPES.MouseEnter); diff --git a/pdf/src/forms/combobox.js b/pdf/src/forms/combobox.js index 0754864efb..87e330eac2 100644 --- a/pdf/src/forms/combobox.js +++ b/pdf/src/forms/combobox.js @@ -114,8 +114,7 @@ if (this.GetTextSize() == 0) this.ProcessAutoFitContent(); - let oContentBounds = this.content.GetContentBounds(0); - let nContentH = oContentBounds.Bottom - oContentBounds.Top; + let nContentH = this.GetTextHeight(); contentY = (Y + nHeight / 2) * g_dKoef_pix_to_mm - nContentH / 2; this._formRect.X = X * g_dKoef_pix_to_mm; @@ -630,7 +629,7 @@ var nTextHeight = g_oTextMeasurer.GetHeight(); var nMaxWidth = oPara.RecalculateMinMaxContentWidth(false).Max; - var nFontSize = Math.max(oTextPr.FontSize); + var nFontSize = oTextPr.FontSize; if (nMaxWidth < 0.001 || nTextHeight < 0.001 || oBounds.W < 0.001 || oBounds.H < 0.001) return nTextHeight; @@ -644,7 +643,16 @@ this.AddToRedraw(); }; + CComboBoxField.prototype.GetTextHeight = function() { + let oPara = this.content.GetElement(0); + let oRun = oPara.GetElement(0); + let oTextPr = oRun.Get_CompiledPr(true); + g_oTextMeasurer.SetTextPr(oTextPr, null); + g_oTextMeasurer.SetFontSlot(AscWord.fontslot_ASCII); + + return g_oTextMeasurer.GetHeight(); + }; CComboBoxField.prototype.WriteToBinary = function(memory) { // TODO /* diff --git a/pdf/src/forms/text.js b/pdf/src/forms/text.js index 66120969fd..d0eb16d248 100644 --- a/pdf/src/forms/text.js +++ b/pdf/src/forms/text.js @@ -318,7 +318,7 @@ var nTextHeight = g_oTextMeasurer.GetHeight(); var nMaxWidth = oPara.RecalculateMinMaxContentWidth(false).Max; - var nFontSize = Math.max(oTextPr.FontSize); + var nFontSize = oTextPr.FontSize; if (nMaxWidth < 0.001 || nTextHeight < 0.001 || oBounds.W < 0.001 || oBounds.H < 0.001) return nTextHeight; @@ -380,6 +380,16 @@ this.AddToRedraw(); }; + CTextField.prototype.GetTextHeight = function() { + let oPara = this.content.GetElement(0); + let oRun = oPara.GetElement(0); + let oTextPr = oRun.Get_CompiledPr(true); + + g_oTextMeasurer.SetTextPr(oTextPr, null); + g_oTextMeasurer.SetFontSlot(AscWord.fontslot_ASCII); + + return g_oTextMeasurer.GetHeight(); + }; CTextField.prototype.Recalculate = function() { if (this.IsNeedRecalc() == false) return; @@ -421,8 +431,7 @@ if (this.GetTextSize() == 0) this.ProcessAutoFitContent(); - let oContentBounds = this.content.GetContentBounds(0); - let nContentH = oContentBounds.Bottom - oContentBounds.Top; + let nContentH = this.GetTextHeight(); contentY = (Y + nHeight / 2) * g_dKoef_pix_to_mm - nContentH / 2; } diff --git a/pdf/src/viewer.js b/pdf/src/viewer.js index d0c2bce1af..72ddaa0cea 100644 --- a/pdf/src/viewer.js +++ b/pdf/src/viewer.js @@ -893,7 +893,7 @@ // if (oFormInfo["font"]["name"] != null) // oForm.SetTextFont(oFormInfo["font"]["name"]); if (oFormInfo["font"]["size"] != null) - oForm.SetTextSize(oFormInfo["font"]["size"] * 2); + oForm.SetTextSize(oFormInfo["font"]["size"]); } if (oFormInfo["AP"] != null) {