Skip to content

Commit

Permalink
refactoring for endings and its paddings
Browse files Browse the repository at this point in the history
  • Loading branch information
kityan committed Sep 10, 2014
1 parent 6dc92b3 commit 9c9d1dd
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 80 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ Demo HTML document
For more demos visit [official website](http://morfana.ru/)

##Changelog
`2.2.0b` / `10.09.2014`
- Code refactoring: adding paddings for '(zero)-ending' and positioning them
- Changed default of configp['zeroEndingWidthFactor'], now is 0.7
- Added 'paddingFactor' to config, default is 0.2

`2.1.1b` / `07.09.2014`
- Added 'zeroEndingWidthFactor' to config
- SVG for morpheme "ending" and "zero-ending" now contains not PATH but RECT
Expand Down
54 changes: 38 additions & 16 deletions morfana.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
Copyright 2013-2014, Pavel Kityan ([email protected])
Licensed under the MIT license.
Version: 2.1.2b
Build date: 7 September 2014
Version: 2.2.0b
Build date: 10 September 2014
*/

(function (root, factory) {
Expand Down Expand Up @@ -35,7 +35,8 @@ configure({
strokeWidth: 1.5, // px
stroke: 'rgb(150,150,150)',
disablePointerEvents: true, // add pointer-events: none to each svg
zeroEndingWidthFactor: 0.43 // now: width of "zero-ending" = data.height * zeroEndingWidthFactor + 9
zeroEndingWidthFactor: 0.7, // now: width of "zero-ending" = data.height * zeroEndingWidthFactor
paddingFactor: 0.2 // now: width of padding for "ending" = data.height * paddingFactor
});

// Queue - array for processing words with setInterval()
Expand Down Expand Up @@ -63,7 +64,6 @@ $(document).ready(function(){
});



/**
* Wrap letter into spans with paddings. Called by wrapPaddings().
*/
Expand All @@ -72,7 +72,10 @@ function wrapPadding(data, letterIndex, paddingType){
rng.setStart(data.maps.actual[letterIndex].element, data.maps.actual[letterIndex].index);
rng.setEnd(data.maps.actual[letterIndex].element, data.maps.actual[letterIndex].index+1);
var newNode = document.createElement('span');
var val = Math.ceil((paddingType == 'after')?(data.height * config['zeroEndingWidthFactor'] + 14):5); // padding params in px
var val = Math.ceil((paddingType == 'after')
? (data.height * config['zeroEndingWidthFactor'] + data.height * config['paddingFactor'] + config['strokeWidth']*2)
: ((data.height * config['paddingFactor']) + config['strokeWidth'])); // padding params in px

var side = (paddingType != 'start') ? 'right' : 'left';
$(newNode).css('padding-' + side, val + 'px');
$(newNode).addClass('morfana-paddings morfana-paddings-' + side);
Expand All @@ -85,6 +88,7 @@ function wrapPadding(data, letterIndex, paddingType){




/**
* Create SVG for morpheme. Called by createImages().
*/
Expand All @@ -102,24 +106,41 @@ function createImage(data, morphemeType, range)//morphemeType, obj, start, stop,
switch (morphemeType)
{
case 'ok':
var isLastLetter = !data.letters[range[0]+1];
var p = config['strokeWidth']; //config['paddingFactor']*h;

if (range[1] != null){ // morpheme 'ending'
w = w + ((range[0] == range[1])?10:0);
x = x + ((range[0] != range[1])?5:0);
// compensate paddings for "ending"
x-=5;
if (range[0] != range[1]){
x-=5; w+=10;
}
var ofs = h*config['paddingFactor']/2 + config['strokeWidth']*2;
x -= ofs;
w += ofs*2;

} else { // morpheme 'zero-ending'
w = h*config['zeroEndingWidthFactor'] + 9;
x = x + data.metrics[range[0]].w + 2;


if (isLastLetter){
// is last word letter
//x = x + data.metrics[range[0]].w + h*config['paddingFactor'];
x = x + data.metrics[range[0]].w + (data.width - (x + data.metrics[range[0]].w))/2 - h*config['paddingFactor']*2;

} else {
// not last word letter
x = x + data.metrics[range[0]].w + (data.metrics[range[0]+1].x - (x + data.metrics[range[0]].w))/2 - h*config['paddingFactor']*2;
}

w = h*config['zeroEndingWidthFactor'] + config['strokeWidth']*2;
// we have 'ending' stop on this letter and 'zero-ending' after this letter.
// nonsense, but try to show it correctly.
if (data.letters[range[0]].stop && data.letters[range[0]].stop['ok']){
x+=5;
x += h*config['paddingFactor']/2;
if (isLastLetter){
x += h*config['paddingFactor'] - config['strokeWidth']*2.5;
}
}

// but if after 'zero-ending' goes 'edning' again (nonsense too!) clear this:
if (data.letters[range[0]+1] && data.letters[range[0]+1].start && data.letters[range[0]+1].start['ok']){
x -= h*config['paddingFactor']/2;
}

}

h*=1.35;
Expand Down Expand Up @@ -247,6 +268,7 @@ function calculateMetrics(data, justHeightReturnWordHeight){
// setting line-height to normal, calculating word's height
var h_lineHeightAsItWas = tmpDiv.height();
setAllChildren(tmpDiv, 'line-height', 'normal');
data.width = tmpDiv.width(); // width of whole word
data.height = tmpDiv.height();
data.heightDiff = h_lineHeightAsItWas - data.height;

Expand Down
15 changes: 15 additions & 0 deletions tests/examples.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/rangy/1.2.3/rangy-core.js"></script>
<script type="text/javascript" src="./../morfana.js"></script>
</head>
<body style="font-size: 18px; text-transform: uppercase;">

<div class="examples">
<p>Этот пример иллюстрирует возможность разметки слов, находящихся в тексте. <span data-morfana-markup="pr:1-3;ko:4-6;su:7-7;ok:8-9;su:10-11;osL:1-7;osR:10-11;">разлетаются</span> · · · · · · · · <span data-morfana-markup="pr:1-3;ko:4-6;su:7-7;su:8-9;su:10-11;osL:1-7;osR:10-11;">разлетаться</span> · · · · · · · · <span data-morfana-markup="pr:1-4;ko:5-6;su:7-9;ok:10-11;os:1-9;">Разъярённый</span> · · · · · · · · <span data-morfana-markup="pr:1-2;ko:3-5;su:6-7;os:1-5;">Вырасти</span> · · · · · · · · <span data-morfana-markup="ko:1-3;pr:5-7;ko:8-10;ok:0;os:1-10;">Газопровод</span> · · · · · · · · <span data-morfana-markup="ko:1-2;ko:6-7;ok:3-5;ok:8-10;osL:1-2;osR:6-7;">Двумястами</span>. И еще несколько слов: <span data-morfana-markup="ko:1-2;ko:4-9;su:10-11;su:12-13;ok:0;os:1-13;">Инопланетянин</span> · · · · · · · · <span data-morfana-markup="pr:1-2;ko:3-8;ok:0;os:1-8">Изморось</span> · · · · · · · · <span data-morfana-markup="ko:1-6;os:1-6">Ателье</span> · · · · · · · · <span data-morfana-markup="ko:1-1;ok:2-3;os:1-1;">Ешь</span> · · · · · · · · <span data-morfana-markup="ko:1-3;su:4-4;su:5-6;os:1-4;">Ездить</span> · · · · · · · · <span data-morfana-markup="ko:1-5;ok:6-6;ko:7-10;su:11-11;ok:12-13;osL:1-5;osR:7-11">Десятиэтажный</span> · · · · · · · · <span data-morfana-markup="pr:1-2;ko:3-5;su:6-6;ok:6-0;os:1-6;">Заплыв</span> · · · · · · · · <span data-morfana-markup="ko:1-2;ko:5-6;ko:9-14;su:15-15;ok:16-17;os:1-15;">юго-юго-восточный</span>
</div>

</body></html>
31 changes: 0 additions & 31 deletions tests/game.html

This file was deleted.

2 changes: 1 addition & 1 deletion tests/markup_children.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/rangy/1.2.3/rangy-core.js"></script>
<script type="text/javascript" src="./../morfana.js"></script>
<script type="text/x-morfana-config">Morfana.configure({autoStart: false, stroke: 'rgb(255,0,0)', strokeWidth: 3});</script>
<script type="text/x-morfana-config">Morfana.configure({autoStart: false, stroke: 'rgb(255,0,0)', strokeWidth: 1.5});</script>
</head>
<body>
<ul>
Expand Down
50 changes: 21 additions & 29 deletions tests/ok.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,33 @@

</head>
<body style="font-size: 20px; font-family: 'Trebuchet MS';">
<p>проверка окончаний на всё слово, на первую букву, на последнюю, нулевые и проч.</p>

<div style="text-align: center; width: 400px; float: left;">

<p>prev <span data-morfana-markup="ok:0;">перпе́н³дикуляр</span> next</p>
<p>prev <span data-morfana-markup="ok:1-1;">перпендикуляр</span> next</p>
<p>prev <span data-morfana-markup="ok:1-6;">п³ерпендикуляр</span> next</p>
<p>prev <span data-morfana-markup="ok:1-0;">перпендикуляр</span> next</p>
<p>prev <span data-morfana-markup="ok:1-13;">перпендикуляр</span> next</p>
<p>prev <span data-morfana-markup="ok:13-0;">перпендикуляр</span> next</p>
<p>prev <span data-morfana-markup="ok:1-13;">перпендикуляр</span> next</p>
<p>prev <span data-morfana-markup="ok:12-13;">перпендикуляр</span> next</p>
<p>prev <span data-morfana-markup="ok:11-12;">перпендикуляр</span> next</p>
<p>prev <span data-morfana-markup="ok:10-12;">перпендикуляр</span> next</p>
<p>prev <span data-morfana-markup="ok:13-13;">перпендикуляр</span> next</p>

</div>
<div style="">

<p style="font-size: 20px;">prev <span data-morfana-markup="ok:1-1;ok:1-0;ok:3-4;ok:6-6;ok:7-8;ok:8-0;ok:9-10;ok:12-0;ok:0">перпендикуляр</span> next</p>
<p style="font-size: 40px;">prev <span data-morfana-markup="ok:1-1;ok:1-0;ok:3-4;ok:6-6;ok:7-8;ok:8-0;ok:9-10;ok:12-0;ok:0">перпендикуляр</span> next</p>
<p style="font-size: 60px;">prev <span data-morfana-markup="ok:1-1;ok:1-0;ok:3-4;ok:6-6;ok:7-8;ok:8-0;ok:9-10;ok:12-0;ok:0">перпендикуляр</span> next</p>

<hr />

<p style="font-size: 20px;">prev <span data-morfana-markup="ok:1-1;ok:1-0;ok:3-4;ok:6-6;ok:7-8;ok:8-0;ok:9-10;ok:12-13;ok:0">перпендикуляр</span> next</p>
<p style="font-size: 40px;">prev <span data-morfana-markup="ok:1-1;ok:1-0;ok:3-4;ok:6-6;ok:7-8;ok:8-0;ok:9-10;ok:12-13;ok:0">перпендикуляр</span> next</p>

<div style="text-align: center; width: 500px; float: left; font-size: 40px;">

<p>prev <span data-morfana-markup="ok:0;">перпендикуляр</span> next</p>
<p>prev <span data-morfana-markup="ok:1-1;">перпендикуляр</span> next</p>
<p>prev <span data-morfana-markup="ok:1-5;">пе́рпендикуляр</span> next</p>
<p>prev <span data-morfana-markup="ok:1-0;">перпендикуляр</span> next</p>
</div>
<div style="text-align: center; width: 500px; float: left; font-size: 40px;">

<p>prev <span data-morfana-markup="ok:1-13;">перпендикуляр</span> next</p>
<p>prev <span data-morfana-markup="ok:13-0;">перпендикуляр</span> next</p>
<p>prev <span data-morfana-markup="ok:1-13;">перпендикуляр</span> next</p>
<p>prev <span data-morfana-markup="ok:12-13;">перпендикуляр</span> next</p>
<p>prev <span data-morfana-markup="ok:11-12;">перпендикуляр</span> next</p>
<p>prev <span data-morfana-markup="ok:10-12;">перпендикуляр</span> next</p>
<p>prev <span data-morfana-markup="ok:13-13;">перпендикуляр</span> next</p>
<hr />

<div style=" letter-spacing: 10px;">

<p style="font-size: 20px;">prev <span data-morfana-markup="ok:1-1;ok:1-0;ok:3-4;ok:6-6;ok:7-8;ok:8-0;ok:9-10;ok:12-0;ok:0">перпендикуляр</span> next</p>

<p style="font-size: 40px;">prev <span data-morfana-markup="ok:1-1;ok:1-0;ok:3-4;ok:6-6;ok:7-8;ok:8-0;ok:9-10;ok:12-0;ok:0">перпендикуляр</span> next</p>
<p style="font-size: 60px;">prev <span data-morfana-markup="ok:1-1;ok:1-0;ok:3-4;ok:6-6;ok:7-8;ok:8-0;ok:9-10;ok:12-0;ok:0">перпендикуляр</span> next</p>

<hr />
<p style="font-size: 20px;">prev <span data-morfana-markup="ok:1-1;ok:1-0;ok:3-4;ok:6-6;ok:7-8;ok:8-0;ok:9-10;ok:12-13;ok:0">перпендикуляр</span> next</p>
<p style="font-size: 40px;">prev <span data-morfana-markup="ok:1-1;ok:1-0;ok:3-4;ok:6-6;ok:7-8;ok:8-0;ok:9-10;ok:12-13;ok:0">перпендикуляр</span> next</p>

</div>

Expand Down
4 changes: 2 additions & 2 deletions tests/scaled.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
<body>

<div style="padding: 25px; width: 384px; margin: 0 auto; margin-top: 200px; text-align: center; -ms-transform: scale(2,2); -webkit-transform: scale(2,2); transform: scale(2,2);">
<span style="font-size: 25px; letter-spacing: 3px;" class="word" data-morfana-markup="os:1-3;ko:1-2;ok:2-0;ok:3-0;su:7-7;ok:5-6;su:8-9;ko:10-12;po:13-14;ok:0">ДлдрДЙФр3огф12</span> next
<span style="font-size: 25px; letter-spacing: 3px;" class="word" data-morfana-markup="os:1-3;ko:1-2;ok:2-0;ok:3-0;su:7-7;ok:5-6;ok:8-9;ok:9-0;ok:10-11;ok:13-13;po:14-14;ok:0">ДлдрДЙФр3огф12</span> next
</div>

<div style="padding: 25px; width: 384px; margin: 0 auto; margin-top: 200px; text-align: center;">
<span style="font-size: 25px; letter-spacing: 3px;" class="word" data-morfana-markup="os:1-3;ko:1-2;ok:2-0;ok:3-0;su:7-7;ok:5-6;su:8-9;ko:10-12;po:13-14;ok:0">ДлдрДЙФр3огф12</span> next
<span style="font-size: 25px; letter-spacing: 3px;" class="word" data-morfana-markup="os:1-3;ko:1-2;ok:2-0;ok:3-0;su:7-7;ok:5-6;ok:8-9;ok:9-0;ok:10-11;ok:13-13;po:14-14;ok:0">ДлдрДЙФр3огф12</span> next
</div>

</body>
Expand Down
46 changes: 46 additions & 0 deletions tests/simple.long.thick.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/rangy/1.2.3/rangy-core.js"></script>
<script type="text/x-morfana-config">Morfana.configure({autoStart: false, strokeWidth: 3});</script>
<script type="text/javascript" src="./../morfana.js"></script>
<script type="text/javascript">$(document).ready(function(){$('.destination').html($('.source').html()); Morfana.draw();});</script>
<style>
div {border: 1px dashed #FF00FF; float: left; margin: 25px; padding: 25px; width: 500px;}
</style>
</head>
<body>
<div class="source">
<span data-morfana-markup="ko: 1 - 4;
ko:5-9;
ok:10-10;">Авиапо́чта</span>
<span data-morfana-markup="ok:3-5;ok:9-12;ko:1-2;ko:6-8;">двумя́ста́ми</span> / слова без разметки / слова без разметки /
<span data-morfana-markup="ok:4-6;ok:12-15;ko:1-2;ko:9-10;">дв-умя́-ст-а́ми</span>
<span data-morfana-markup="ko:1-5;ok:6-6;ko:7-10;su:11-11;ok:12-13;os:1-11">десятиэтажных</span>
<span data-morfana-markup="ko:1-5;su:6-8;ok:9-10">деревянных</span> / слова без разметки / слова без разметки /
<span data-morfana-markup="pr:1-2;ko:3-4;su:5-5;su:6-6;ok:7-7">затмила</span>
<span data-morfana-markup="ko:1-4;ok:0;os:1-4;">пять</span> / слова без разметки / слова без разметки /
<span data-morfana-markup="ko:1-5;ok:6-8">лебеде́й</span> / слова без разметки / слова без разметки /
<span data-morfana-markup="ko:1-4;ko:6-8;ok:0;">Иван-чай</span><br><br>
<span data-morfana-markup="ko:1-5;ok:6-6;ko:7-10;su:11-11;ok:12-13;os:1-11">дес<span style="letter-spacing: 10px;">яти<b>этажны</b>х</span></span><br>
<span data-morfana-markup="ko:1-5;ok:6-6;ko:7-10;su:11-11;ok:12-13;os:1-11" style="letter-spacing: 10px;">д<span style="letter-spacing: 5px;"><span style="color: red;">еся</span>ти<b>этажны</b>х</span></span><br>
<span data-morfana-markup="ko:1-5;ok:6-6;ko:7-10;su:11-11;ok:12-13;os:1-11">д<span style="font-size: 0.5em;">есятиэта</span>жных</span><br>
<span data-morfana-markup="ko:1-5;ok:6-6;ko:7-10;su:11-11;ok:12-13;os:1-11" style="font-size: 0.3em;">д<span style="font-size: 2.5em;">есятиэта</span>жных</span>
<br><br>
<span data-morfana-markup="ko:1-5;ok:6-6;ko:7-10;su:11-11;ok:12-13;os:1-11"><strong>десят</strong>и<span style="color: #FF00FF;">этаж</span>ный</span>
</div>
<div class="destination" style="font-family: Verdana; font-size: 12pt; line-height: 25px;"></div>
<div class="destination" style="font-family: Verdana; font-size: 12pt;"></div>
<div class="destination" style="font-family: Verdana; font-size: 12pt; text-transform: uppercase;"></div>
<div class="destination" style="font-family: Times New Roman; font-size: 14pt;"></div>
<div class="destination" style="font-family: monospace; font-size: 14pt;"></div>
<div class="destination" style="font-family: Arial; font-size: 14pt;"></div>
<div class="destination" style="font-family: Georgia; font-size: 16pt;"></div>
<div class="destination" style="font-family: Verdana; font-size: 30pt;"></div>
<div class="destination" style="font-family: Verdana; font-size: 8pt;"></div>
<div class="destination" style="font-family: Comic Sans MS; font-size: 12pt;"></div>
</body>
</html>
2 changes: 1 addition & 1 deletion tests/simple.short.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<span data-morfana-markup="ko:1-4;ko:6-8;ok:0;">ABCD-ABC</span><br>
<span data-morfana-markup="ko:1-5;ok:6-6;ko:7-10;su:11-11;ok:12-13;os:1-11">ABCDEFABCDEFG</span><br>
<span data-morfana-markup="ko:1-4;ok:0">FBCD</span><br>
<span style="font-size:68px;" data-morfana-markup="ok:0;ok:5-0">ABCDEFGH</span><br>
<span style="font-size:68px;" data-morfana-markup="ok:0;ok:5-0; ok:2-3;ok:4-4;">ABCDEFGH</span><br>
<span style="font-size:28px;" data-morfana-markup="ok:5-0;su:6-7;">ABCDEFGH</span><br>
<span style="font-size:18px;" data-morfana-markup="ok:5-0;">ABCDEFGH</span><br>
<div>
Expand Down

0 comments on commit 9c9d1dd

Please sign in to comment.