Commit a1013b41 authored by Alexander.Trofimov's avatar Alexander.Trofimov

add TextMeasurer.js

parent af7db800
......@@ -18,6 +18,7 @@
"../common/FontsFreeType/FontManager.js",
"../word/Drawing/Metafile.js",
"../common/FontsFreeType/TextMeasurer.js",
"../common/Drawings/Hit.js",
"../common/Drawings/ArcTo.js",
......
......@@ -18,6 +18,7 @@
"../common/FontsFreeType/FontManager.js",
"../word/Drawing/Metafile.js",
"../common/FontsFreeType/TextMeasurer.js",
"../common/Drawings/Hit.js",
"../common/Drawings/ArcTo.js",
......
......@@ -21,6 +21,7 @@
"../common/FontsFreeType/FontManager.js",
"../word/Drawing/Metafile.js",
"../common/FontsFreeType/TextMeasurer.js",
"../common/Drawings/Hit.js",
"../common/Drawings/ArcTo.js",
......
/*
*
* (c) Copyright Ascensio System Limited 2010-2016
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
*
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7  3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7  3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
"use strict";
(function(window, undefined){
function CTextMeasurer()
{
this.m_oManager = new AscFonts.CFontManager();
this.m_oFont = null;
// RFonts
this.m_oTextPr = null;
this.m_oGrFonts = new CGrRFonts();
this.m_oLastFont = new CFontSetup();
this.LastFontOriginInfo = {Name : "", Replace : null};
}
CTextMeasurer.prototype =
{
Init : function()
{
this.m_oManager.Initialize();
},
SetStringGid : function(bGID)
{
this.m_oManager.SetStringGID(bGID);
},
SetFont : function(font)
{
if (!font)
return;
this.m_oFont = font;
var bItalic = true === font.Italic;
var bBold = true === font.Bold;
var oFontStyle = FontStyle.FontStyleRegular;
if ( !bItalic && bBold )
oFontStyle = FontStyle.FontStyleBold;
else if ( bItalic && !bBold )
oFontStyle = FontStyle.FontStyleItalic;
else if ( bItalic && bBold )
oFontStyle = FontStyle.FontStyleBoldItalic;
var _lastSetUp = this.m_oLastFont;
if (_lastSetUp.SetUpName != font.FontFamily.Name || _lastSetUp.SetUpSize != font.FontSize || _lastSetUp.SetUpStyle != oFontStyle)
{
_lastSetUp.SetUpName = font.FontFamily.Name;
_lastSetUp.SetUpSize = font.FontSize;
_lastSetUp.SetUpStyle = oFontStyle;
g_fontApplication.LoadFont(_lastSetUp.SetUpName, window.g_font_loader, this.m_oManager, _lastSetUp.SetUpSize, _lastSetUp.SetUpStyle, 72, 72, undefined, this.LastFontOriginInfo);
}
},
SetFontInternal : function(_name, _size, _style)
{
var _lastSetUp = this.m_oLastFont;
if (_lastSetUp.SetUpName != _name || _lastSetUp.SetUpSize != _size || _lastSetUp.SetUpStyle != _style)
{
_lastSetUp.SetUpName = _name;
_lastSetUp.SetUpSize = _size;
_lastSetUp.SetUpStyle = _style;
g_fontApplication.LoadFont(_lastSetUp.SetUpName, window.g_font_loader, this.m_oManager, _lastSetUp.SetUpSize, _lastSetUp.SetUpStyle, 72, 72, undefined, this.LastFontOriginInfo);
}
},
SetTextPr : function(textPr, theme)
{
this.m_oTextPr = textPr;
if (theme)
this.m_oGrFonts.checkFromTheme(theme.themeElements.fontScheme, this.m_oTextPr.RFonts);
else
this.m_oGrFonts = this.m_oTextPr.RFonts;
},
SetFontSlot : function(slot, fontSizeKoef)
{
var _rfonts = this.m_oGrFonts;
var _lastFont = this.m_oLastFont;
switch (slot)
{
case fontslot_ASCII:
{
_lastFont.Name = _rfonts.Ascii.Name;
_lastFont.Index = _rfonts.Ascii.Index;
_lastFont.Size = this.m_oTextPr.FontSize;
_lastFont.Bold = this.m_oTextPr.Bold;
_lastFont.Italic = this.m_oTextPr.Italic;
break;
}
case fontslot_CS:
{
_lastFont.Name = _rfonts.CS.Name;
_lastFont.Index = _rfonts.CS.Index;
_lastFont.Size = this.m_oTextPr.FontSizeCS;
_lastFont.Bold = this.m_oTextPr.BoldCS;
_lastFont.Italic = this.m_oTextPr.ItalicCS;
break;
}
case fontslot_EastAsia:
{
_lastFont.Name = _rfonts.EastAsia.Name;
_lastFont.Index = _rfonts.EastAsia.Index;
_lastFont.Size = this.m_oTextPr.FontSize;
_lastFont.Bold = this.m_oTextPr.Bold;
_lastFont.Italic = this.m_oTextPr.Italic;
break;
}
case fontslot_HAnsi:
default:
{
_lastFont.Name = _rfonts.HAnsi.Name;
_lastFont.Index = _rfonts.HAnsi.Index;
_lastFont.Size = this.m_oTextPr.FontSize;
_lastFont.Bold = this.m_oTextPr.Bold;
_lastFont.Italic = this.m_oTextPr.Italic;
break;
}
}
if (undefined !== fontSizeKoef)
_lastFont.Size *= fontSizeKoef;
var _style = 0;
if (_lastFont.Italic)
_style += 2;
if (_lastFont.Bold)
_style += 1;
if (_lastFont.Name != _lastFont.SetUpName || _lastFont.Size != _lastFont.SetUpSize || _style != _lastFont.SetUpStyle)
{
_lastFont.SetUpName = _lastFont.Name;
_lastFont.SetUpSize = _lastFont.Size;
_lastFont.SetUpStyle = _style;
g_fontApplication.LoadFont(_lastFont.SetUpName, window.g_font_loader, this.m_oManager, _lastFont.SetUpSize, _lastFont.SetUpStyle, 72, 72, undefined, this.LastFontOriginInfo);
}
},
GetTextPr : function()
{
return this.m_oTextPr;
},
GetFont : function()
{
return this.m_oFont;
},
Measure : function(text)
{
var Width = 0;
var Height = 0;
var _code = text.charCodeAt(0);
if (null != this.LastFontOriginInfo.Replace)
_code = g_fontApplication.GetReplaceGlyph(_code, this.LastFontOriginInfo.Replace);
var Temp = this.m_oManager.MeasureChar( _code );
Width = Temp.fAdvanceX * 25.4 / 72;
Height = 0;//Temp.fHeight;
return { Width : Width, Height : Height };
},
Measure2 : function(text)
{
var Width = 0;
var _code = text.charCodeAt(0);
if (null != this.LastFontOriginInfo.Replace)
_code = g_fontApplication.GetReplaceGlyph(_code, this.LastFontOriginInfo.Replace);
var Temp = this.m_oManager.MeasureChar( _code, true );
Width = Temp.fAdvanceX * 25.4 / 72;
if (Temp.oBBox.rasterDistances == null)
{
return {
Width : Width,
Ascent : (Temp.oBBox.fMaxY * 25.4 / 72),
Height : ((Temp.oBBox.fMaxY - Temp.oBBox.fMinY) * 25.4 / 72),
WidthG : ((Temp.oBBox.fMaxX - Temp.oBBox.fMinX) * 25.4 / 72),
rasterOffsetX: 0,
rasterOffsetY: 0
};
}
return {
Width : Width,
Ascent : (Temp.oBBox.fMaxY * 25.4 / 72),
Height : ((Temp.oBBox.fMaxY - Temp.oBBox.fMinY) * 25.4 / 72),
WidthG : ((Temp.oBBox.fMaxX - Temp.oBBox.fMinX) * 25.4 / 72),
rasterOffsetX: Temp.oBBox.rasterDistances.dist_l * 25.4 / 72,
rasterOffsetY: Temp.oBBox.rasterDistances.dist_t * 25.4 / 72
};
},
MeasureCode : function(lUnicode)
{
var Width = 0;
var Height = 0;
if (null != this.LastFontOriginInfo.Replace)
lUnicode = g_fontApplication.GetReplaceGlyph(lUnicode, this.LastFontOriginInfo.Replace);
var Temp = this.m_oManager.MeasureChar( lUnicode );
Width = Temp.fAdvanceX * 25.4 / 72;
Height = ((Temp.oBBox.fMaxY - Temp.oBBox.fMinY) * 25.4 / 72);
return { Width : Width, Height : Height, Ascent : (Temp.oBBox.fMaxY * 25.4 / 72) };
},
Measure2Code : function(lUnicode)
{
var Width = 0;
if (null != this.LastFontOriginInfo.Replace)
lUnicode = g_fontApplication.GetReplaceGlyph(lUnicode, this.LastFontOriginInfo.Replace);
var Temp = this.m_oManager.MeasureChar( lUnicode, true );
Width = Temp.fAdvanceX * 25.4 / 72;
if (Temp.oBBox.rasterDistances == null)
{
return {
Width : Width,
Ascent : (Temp.oBBox.fMaxY * 25.4 / 72),
Height : ((Temp.oBBox.fMaxY - Temp.oBBox.fMinY) * 25.4 / 72),
WidthG : ((Temp.oBBox.fMaxX - Temp.oBBox.fMinX) * 25.4 / 72),
rasterOffsetX: 0,
rasterOffsetY: 0
};
}
return {
Width : Width,
Ascent : (Temp.oBBox.fMaxY * 25.4 / 72),
Height : ((Temp.oBBox.fMaxY - Temp.oBBox.fMinY) * 25.4 / 72),
WidthG : ((Temp.oBBox.fMaxX - Temp.oBBox.fMinX) * 25.4 / 72),
rasterOffsetX: (Temp.oBBox.rasterDistances.dist_l + Temp.oBBox.fMinX) * 25.4 / 72,
rasterOffsetY: Temp.oBBox.rasterDistances.dist_t * 25.4 / 72
};
},
GetAscender : function()
{
var UnitsPerEm = this.m_oManager.m_lUnits_Per_Em;
var Ascender = this.m_oManager.m_lAscender;
return Ascender * this.m_oLastFont.SetUpSize / UnitsPerEm * g_dKoef_pt_to_mm;
},
GetDescender : function()
{
var UnitsPerEm = this.m_oManager.m_lUnits_Per_Em;
var Descender = this.m_oManager.m_lDescender;
return Descender * this.m_oLastFont.SetUpSize / UnitsPerEm * g_dKoef_pt_to_mm;
},
GetHeight : function()
{
var UnitsPerEm = this.m_oManager.m_lUnits_Per_Em;
var Height = this.m_oManager.m_lLineHeight;
return Height * this.m_oLastFont.SetUpSize / UnitsPerEm * g_dKoef_pt_to_mm;
}
};
var g_oTextMeasurer = new CTextMeasurer();
g_oTextMeasurer.Init();
//--------------------------------------------------------export----------------------------------------------------
window['AscCommon'] = window['AscCommon'] || {};
window['AscCommon'].g_oTextMeasurer = g_oTextMeasurer;
})(window);
......@@ -1317,14 +1317,14 @@ CMetafile.prototype =
var _old_pos = this.Memory.pos;
g_fontApplication.LoadFont(_font_info.Name, window.g_font_loader, g_oTextMeasurer.m_oManager, this.m_oFont.FontSize, Math.max(this.m_oFont.Style, 0), 72, 72);
g_fontApplication.LoadFont(_font_info.Name, window.g_font_loader, AscCommon.g_oTextMeasurer.m_oManager, this.m_oFont.FontSize, Math.max(this.m_oFont.Style, 0), 72, 72);
if (null != this.LastFontOriginInfo.Replace)
{
code = g_fontApplication.GetReplaceGlyph(code, this.LastFontOriginInfo.Replace);
}
g_oTextMeasurer.m_oManager.LoadStringPathCode(code, false, x, y, this);
AscCommon.g_oTextMeasurer.m_oManager.LoadStringPathCode(code, false, x, y, this);
// start (1) + draw(1) + typedraw(4) + end(1) = 7!
if ((this.Memory.pos - _old_pos) < 8)
......@@ -1347,8 +1347,8 @@ CMetafile.prototype =
var _old_pos = this.Memory.pos;
g_fontApplication.LoadFont(this.m_oFont.Name, window.g_font_loader, g_oTextMeasurer.m_oManager, this.m_oFont.FontSize, Math.max(this.m_oFont.Style, 0), 72, 72);
g_oTextMeasurer.m_oManager.LoadStringPathCode(gid, true, x, y, this);
g_fontApplication.LoadFont(this.m_oFont.Name, window.g_font_loader, AscCommon.g_oTextMeasurer.m_oManager, this.m_oFont.FontSize, Math.max(this.m_oFont.Style, 0), 72, 72);
AscCommon.g_oTextMeasurer.m_oManager.LoadStringPathCode(gid, true, x, y, this);
// start (1) + draw(1) + typedraw(4) + end(1) = 7!
if ((this.Memory.pos - _old_pos) < 8)
......@@ -2428,291 +2428,4 @@ CMatrixL.prototype =
{
return x * this.shy + y * this.sy + this.ty;
}
};
function CTextMeasurer()
{
this.m_oManager = new AscFonts.CFontManager();
this.m_oFont = null;
// RFonts
this.m_oTextPr = null;
this.m_oGrFonts = new CGrRFonts();
this.m_oLastFont = new CFontSetup();
this.LastFontOriginInfo = {Name : "", Replace : null};
}
CTextMeasurer.prototype =
{
Init : function()
{
this.m_oManager.Initialize();
},
SetStringGid : function(bGID)
{
this.m_oManager.SetStringGID(bGID);
},
SetFont : function(font)
{
if (!font)
return;
this.m_oFont = font;
var bItalic = true === font.Italic;
var bBold = true === font.Bold;
var oFontStyle = FontStyle.FontStyleRegular;
if ( !bItalic && bBold )
oFontStyle = FontStyle.FontStyleBold;
else if ( bItalic && !bBold )
oFontStyle = FontStyle.FontStyleItalic;
else if ( bItalic && bBold )
oFontStyle = FontStyle.FontStyleBoldItalic;
var _lastSetUp = this.m_oLastFont;
if (_lastSetUp.SetUpName != font.FontFamily.Name || _lastSetUp.SetUpSize != font.FontSize || _lastSetUp.SetUpStyle != oFontStyle)
{
_lastSetUp.SetUpName = font.FontFamily.Name;
_lastSetUp.SetUpSize = font.FontSize;
_lastSetUp.SetUpStyle = oFontStyle;
g_fontApplication.LoadFont(_lastSetUp.SetUpName, window.g_font_loader, this.m_oManager, _lastSetUp.SetUpSize, _lastSetUp.SetUpStyle, 72, 72, undefined, this.LastFontOriginInfo);
}
},
SetFontInternal : function(_name, _size, _style)
{
var _lastSetUp = this.m_oLastFont;
if (_lastSetUp.SetUpName != _name || _lastSetUp.SetUpSize != _size || _lastSetUp.SetUpStyle != _style)
{
_lastSetUp.SetUpName = _name;
_lastSetUp.SetUpSize = _size;
_lastSetUp.SetUpStyle = _style;
g_fontApplication.LoadFont(_lastSetUp.SetUpName, window.g_font_loader, this.m_oManager, _lastSetUp.SetUpSize, _lastSetUp.SetUpStyle, 72, 72, undefined, this.LastFontOriginInfo);
}
},
SetTextPr : function(textPr, theme)
{
this.m_oTextPr = textPr;
if (theme)
this.m_oGrFonts.checkFromTheme(theme.themeElements.fontScheme, this.m_oTextPr.RFonts);
else
this.m_oGrFonts = this.m_oTextPr.RFonts;
},
SetFontSlot : function(slot, fontSizeKoef)
{
var _rfonts = this.m_oGrFonts;
var _lastFont = this.m_oLastFont;
switch (slot)
{
case fontslot_ASCII:
{
_lastFont.Name = _rfonts.Ascii.Name;
_lastFont.Index = _rfonts.Ascii.Index;
_lastFont.Size = this.m_oTextPr.FontSize;
_lastFont.Bold = this.m_oTextPr.Bold;
_lastFont.Italic = this.m_oTextPr.Italic;
break;
}
case fontslot_CS:
{
_lastFont.Name = _rfonts.CS.Name;
_lastFont.Index = _rfonts.CS.Index;
_lastFont.Size = this.m_oTextPr.FontSizeCS;
_lastFont.Bold = this.m_oTextPr.BoldCS;
_lastFont.Italic = this.m_oTextPr.ItalicCS;
break;
}
case fontslot_EastAsia:
{
_lastFont.Name = _rfonts.EastAsia.Name;
_lastFont.Index = _rfonts.EastAsia.Index;
_lastFont.Size = this.m_oTextPr.FontSize;
_lastFont.Bold = this.m_oTextPr.Bold;
_lastFont.Italic = this.m_oTextPr.Italic;
break;
}
case fontslot_HAnsi:
default:
{
_lastFont.Name = _rfonts.HAnsi.Name;
_lastFont.Index = _rfonts.HAnsi.Index;
_lastFont.Size = this.m_oTextPr.FontSize;
_lastFont.Bold = this.m_oTextPr.Bold;
_lastFont.Italic = this.m_oTextPr.Italic;
break;
}
}
if (undefined !== fontSizeKoef)
_lastFont.Size *= fontSizeKoef;
var _style = 0;
if (_lastFont.Italic)
_style += 2;
if (_lastFont.Bold)
_style += 1;
if (_lastFont.Name != _lastFont.SetUpName || _lastFont.Size != _lastFont.SetUpSize || _style != _lastFont.SetUpStyle)
{
_lastFont.SetUpName = _lastFont.Name;
_lastFont.SetUpSize = _lastFont.Size;
_lastFont.SetUpStyle = _style;
g_fontApplication.LoadFont(_lastFont.SetUpName, window.g_font_loader, this.m_oManager, _lastFont.SetUpSize, _lastFont.SetUpStyle, 72, 72, undefined, this.LastFontOriginInfo);
}
},
GetTextPr : function()
{
return this.m_oTextPr;
},
GetFont : function()
{
return this.m_oFont;
},
Measure : function(text)
{
var Width = 0;
var Height = 0;
var _code = text.charCodeAt(0);
if (null != this.LastFontOriginInfo.Replace)
_code = g_fontApplication.GetReplaceGlyph(_code, this.LastFontOriginInfo.Replace);
var Temp = this.m_oManager.MeasureChar( _code );
Width = Temp.fAdvanceX * 25.4 / 72;
Height = 0;//Temp.fHeight;
return { Width : Width, Height : Height };
},
Measure2 : function(text)
{
var Width = 0;
var _code = text.charCodeAt(0);
if (null != this.LastFontOriginInfo.Replace)
_code = g_fontApplication.GetReplaceGlyph(_code, this.LastFontOriginInfo.Replace);
var Temp = this.m_oManager.MeasureChar( _code, true );
Width = Temp.fAdvanceX * 25.4 / 72;
if (Temp.oBBox.rasterDistances == null)
{
return {
Width : Width,
Ascent : (Temp.oBBox.fMaxY * 25.4 / 72),
Height : ((Temp.oBBox.fMaxY - Temp.oBBox.fMinY) * 25.4 / 72),
WidthG : ((Temp.oBBox.fMaxX - Temp.oBBox.fMinX) * 25.4 / 72),
rasterOffsetX: 0,
rasterOffsetY: 0
};
}
return {
Width : Width,
Ascent : (Temp.oBBox.fMaxY * 25.4 / 72),
Height : ((Temp.oBBox.fMaxY - Temp.oBBox.fMinY) * 25.4 / 72),
WidthG : ((Temp.oBBox.fMaxX - Temp.oBBox.fMinX) * 25.4 / 72),
rasterOffsetX: Temp.oBBox.rasterDistances.dist_l * 25.4 / 72,
rasterOffsetY: Temp.oBBox.rasterDistances.dist_t * 25.4 / 72
};
},
MeasureCode : function(lUnicode)
{
var Width = 0;
var Height = 0;
if (null != this.LastFontOriginInfo.Replace)
lUnicode = g_fontApplication.GetReplaceGlyph(lUnicode, this.LastFontOriginInfo.Replace);
var Temp = this.m_oManager.MeasureChar( lUnicode );
Width = Temp.fAdvanceX * 25.4 / 72;
Height = ((Temp.oBBox.fMaxY - Temp.oBBox.fMinY) * 25.4 / 72);
return { Width : Width, Height : Height, Ascent : (Temp.oBBox.fMaxY * 25.4 / 72) };
},
Measure2Code : function(lUnicode)
{
var Width = 0;
if (null != this.LastFontOriginInfo.Replace)
lUnicode = g_fontApplication.GetReplaceGlyph(lUnicode, this.LastFontOriginInfo.Replace);
var Temp = this.m_oManager.MeasureChar( lUnicode, true );
Width = Temp.fAdvanceX * 25.4 / 72;
if (Temp.oBBox.rasterDistances == null)
{
return {
Width : Width,
Ascent : (Temp.oBBox.fMaxY * 25.4 / 72),
Height : ((Temp.oBBox.fMaxY - Temp.oBBox.fMinY) * 25.4 / 72),
WidthG : ((Temp.oBBox.fMaxX - Temp.oBBox.fMinX) * 25.4 / 72),
rasterOffsetX: 0,
rasterOffsetY: 0
};
}
return {
Width : Width,
Ascent : (Temp.oBBox.fMaxY * 25.4 / 72),
Height : ((Temp.oBBox.fMaxY - Temp.oBBox.fMinY) * 25.4 / 72),
WidthG : ((Temp.oBBox.fMaxX - Temp.oBBox.fMinX) * 25.4 / 72),
rasterOffsetX: (Temp.oBBox.rasterDistances.dist_l + Temp.oBBox.fMinX) * 25.4 / 72,
rasterOffsetY: Temp.oBBox.rasterDistances.dist_t * 25.4 / 72
};
},
GetAscender : function()
{
var UnitsPerEm = this.m_oManager.m_lUnits_Per_Em;
var Ascender = this.m_oManager.m_lAscender;
return Ascender * this.m_oLastFont.SetUpSize / UnitsPerEm * g_dKoef_pt_to_mm;
},
GetDescender : function()
{
var UnitsPerEm = this.m_oManager.m_lUnits_Per_Em;
var Descender = this.m_oManager.m_lDescender;
return Descender * this.m_oLastFont.SetUpSize / UnitsPerEm * g_dKoef_pt_to_mm;
},
GetHeight : function()
{
var UnitsPerEm = this.m_oManager.m_lUnits_Per_Em;
var Height = this.m_oManager.m_lLineHeight;
return Height * this.m_oLastFont.SetUpSize / UnitsPerEm * g_dKoef_pt_to_mm;
}
};
var g_oTextMeasurer = new CTextMeasurer();
g_oTextMeasurer.Init();
//------------------------------------------------------------export----------------------------------------------------
window['AscCommon'] = window['AscCommon'] || {};
window['AscCommon'].g_oTextMeasurer = g_oTextMeasurer;
\ No newline at end of file
};
\ No newline at end of file
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