-
Notifications
You must be signed in to change notification settings - Fork 0
/
linemultiparas.tsx
341 lines (253 loc) · 10.3 KB
/
linemultiparas.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import React, { useState } from 'react';
import { Button, View, StyleSheet, ScrollView } from 'react-native';
import {
Paragraph, Skia, useFonts, TextAlign, Canvas, Circle, Group, useFont, Text, TextBlob, Rect, TextHeightBehavior,
SkTextFontFeatures,
SkParagraphStyle,
TextDirection,
SkTextStyle,
SkRect
} from "@shopify/react-native-skia";
import { quranTextService, PAGE_WIDTH, FONTSIZE, MARGIN, INTERLINE, TOP, SPACEWIDTH, SpaceType, LineTextInfo } from './qurantext.service'
import { JustInfo, JustService } from './just.service'
type LineProps = {
pageWidth: number;
pageIndex: number;
lineIndex: number;
};
const LineMultiPars = (props: LineProps) => {
const fontMgr = useFonts({
oldmadina: [require("./oldmadina.otf")]
});
if (!fontMgr) {
return null;
}
const pageIndex = props.pageIndex;
const lineIndex = props.lineIndex;
const pageText = quranTextService.quranText[pageIndex]
const pageWidth = props.pageWidth
const scale = pageWidth / PAGE_WIDTH;
let fontSize = FONTSIZE * scale;
const defaultMargin = MARGIN * scale
let margin = defaultMargin;
let lineWidth = pageWidth - 2 * margin;
const interLine = INTERLINE * scale
const pageHeight = 15 * interLine + (400 * scale);
const maxWidth = 2 * lineWidth
const defaultSpaceWidth = SPACEWIDTH * scale
const spaceWidthInFont = 100 * scale
const lineText = pageText[lineIndex]
const lineInfo = quranTextService.getLineInfo(pageIndex, lineIndex)
const tajweedInfo = quranTextService.getTajweed(pageIndex, lineIndex)
const lineTextInfo = quranTextService.analyzeText(pageIndex, lineIndex)
const lineParStyle: SkParagraphStyle = {
textAlign: TextAlign.Left,
textHeightBehavior: TextHeightBehavior.DisableAll
};
if (lineInfo.lineType === 1) {
//sura name : center
const lineTextStyle = {
color: Skia.Color("black"),
fontFamilies: ["oldmadina"],
fontSize: fontSize,
};
let paragraphBuilder = Skia.ParagraphBuilder.Make(lineParStyle, fontMgr).pushStyle(lineTextStyle);
paragraphBuilder.addText(lineText)
let paragraph = paragraphBuilder.pop().build();
paragraph.layout(maxWidth);
const parHeight = paragraph.getHeight();
const parWidth = paragraph.getLongestLine();
return (
<View style={{ width: parWidth, height: interLine, position: 'relative', left: (pageWidth - parWidth) / 2 }}>
<Canvas style={{ width: pageWidth, height: interLine, position: 'relative', top: -250 * scale }}>
<Paragraph style="fill" paragraph={paragraph} x={0} y={0} width={maxWidth} />
</Canvas>
</View>
);
} else if (lineInfo.lineType === 2 && pageIndex != 0 && pageIndex != 1) {
//basmala : center
const lineTextStyle = {
color: Skia.Color("black"),
fontFamilies: ["oldmadina"],
fontSize: fontSize,
fontFeatures: [{ name: "basm", value: 1 }]
};
let paragraphBuilder = Skia.ParagraphBuilder.Make(lineParStyle, fontMgr).pushStyle(lineTextStyle);
paragraphBuilder.addText(lineText)
let paragraph = paragraphBuilder.pop().build();
paragraph.layout(maxWidth);
const parHeight = paragraph.getHeight();
const parWidth = paragraph.getLongestLine();
return (
<View style={{ width: parWidth, height: interLine, position: 'relative', left: (pageWidth - parWidth) / 2 }}>
<Canvas style={{ width: pageWidth, height: interLine, position: 'relative', top: -250 * scale }}>
<Paragraph paragraph={paragraph} x={0} y={0} width={maxWidth} />
</Canvas>
</View>
);
} else {
// simple line
if (lineInfo.lineWidthRatio !== 1) {
const newlineWidth = lineWidth * lineInfo.lineWidthRatio
margin += (lineWidth - newlineWidth) / 2
lineWidth = newlineWidth
}
let marginTop = 0
if ((pageIndex === 0 || pageIndex == 1) && lineIndex == 1) {
marginTop = 2 * interLine;
}
const lineTextStyle = {
color: Skia.Color("black"),
fontFamilies: ["oldmadina"],
fontSize: fontSize
};
let layOutResult = []
let justResults: JustInfo | undefined
let simpleSpaceWidth;
let ayaSpaceWidth;
const totalSpaces = lineTextInfo.ayaSpaceIndexes.length + lineTextInfo.simpleSpaceIndexes.length;
let currentLineWidth = defaultSpaceWidth * totalSpaces
for (let wordIndex = 0; wordIndex < lineTextInfo.wordInfos.length; wordIndex++) {
const wordInfo = lineTextInfo.wordInfos[wordIndex]
let paragraphBuilder = Skia.ParagraphBuilder.Make(lineParStyle, fontMgr).pushStyle(lineTextStyle);
paragraphBuilder.addText(wordInfo.text)
let paragraph = paragraphBuilder.pop().build();
paragraph.layout(maxWidth);
const parHeight = paragraph.getHeight();
const parWidth = paragraph.getLongestLine();
currentLineWidth += parWidth
layOutResult.push({
paragraph,
parHeight,
parWidth,
})
}
let diff = lineWidth - currentLineWidth
if (diff > 0) {
// stretch
let maxStretchBySpace = defaultSpaceWidth * 0.5;
let maxStretchByAyaSpace = defaultSpaceWidth * 2;
let maxStretch = maxStretchBySpace * lineTextInfo.simpleSpaceIndexes.length + maxStretchByAyaSpace * lineTextInfo.ayaSpaceIndexes.length;
let stretch = Math.min(lineWidth - currentLineWidth, maxStretch);
let spaceRatio = maxStretch != 0 ? stretch / maxStretch : 0;
let stretchBySpace = spaceRatio * maxStretchBySpace;
let stretchByByAyaSpace = spaceRatio * maxStretchByAyaSpace;
simpleSpaceWidth = defaultSpaceWidth + stretchBySpace
ayaSpaceWidth = defaultSpaceWidth + stretchByByAyaSpace
currentLineWidth += stretch
// stretching
if (lineWidth > currentLineWidth) {
const lineTextStyle: SkTextStyle = {
fontFamilies: ["oldmadina"],
fontSize: fontSize,
}
let justService: JustService = new JustService(pageIndex, lineIndex, lineTextInfo, fontMgr, currentLineWidth, lineWidth, lineTextStyle, layOutResult)
justResults = justService.justifyLine()
currentLineWidth = justResults.textLineWidth
}
if (lineWidth > currentLineWidth) {
// full justify with space
let addToSpace = (lineWidth - currentLineWidth) / lineTextInfo.spaces.size
simpleSpaceWidth += addToSpace
ayaSpaceWidth += addToSpace
}
} else {
//shrink
const ratio = lineWidth / currentLineWidth;
// TODO : add shrinking features to the font
//shrink by changing font size for now
fontSize = fontSize * ratio
simpleSpaceWidth = defaultSpaceWidth * ratio
ayaSpaceWidth = defaultSpaceWidth * ratio
}
return wordParagraphs(pageWidth, defaultMargin, fontSize, layOutResult, lineTextInfo, fontMgr, lineText, tajweedInfo, justResults,
maxWidth, ayaSpaceWidth, simpleSpaceWidth, interLine, marginTop, scale, margin)
}
};
function wordParagraphs(pageWidth: number, defaultMargin: number, fontSize: number, layOutResult: any[], lineTextInfo: any, fontMgr: any,
lineText: string, tajweedInfo: any, justResults: any, maxWidth: number, ayaSpaceWidth: number, simpleSpaceWidth: number,
interLine: number, marginTop: number, scale: number, margin: number) {
var wordList = [];
const x_start = pageWidth - defaultMargin
var currentxPos = x_start
const paragraphStyle = {
textHeightBehavior: TextHeightBehavior.DisableAll,
textAlign: TextAlign.Left,
//textDirection: TextDirection.RTL
};
const textStyle = {
color: Skia.Color("black"),
fontFamilies: ["oldmadina"],
fontSize: fontSize
};
for (let wordIndex = 0; wordIndex < layOutResult.length; wordIndex++) {
const layOut = layOutResult[wordIndex];
const wordInfo = lineTextInfo.wordInfos[wordIndex]
let paragraphBuilder = Skia.ParagraphBuilder.Make(paragraphStyle, fontMgr).pushStyle(textStyle);
for (let i = wordInfo.startIndex; i <= wordInfo.endIndex; i++) {
const char = lineText.charAt(i)
const tajweed = tajweedInfo.get(i)
const justInfo = justResults?.fontFeatures?.get(i)
if (tajweed || justInfo) {
const newtextStyle: SkTextStyle = {
color: textStyle.color,
fontFamilies: textStyle.fontFamilies,
fontSize: textStyle.fontSize,
};
if (tajweed) {
newtextStyle.color = Skia.Color(Float32Array.from(getColor(tajweed).map(a => a / 255)))
}
if (justInfo) {
newtextStyle.fontFeatures = justInfo
}
paragraphBuilder.pushStyle(newtextStyle)
paragraphBuilder.addText(char)
paragraphBuilder.pop()
} else {
paragraphBuilder.addText(char)
}
}
let paragraph = paragraphBuilder.pop().build();
paragraph.layout(maxWidth)
currentxPos -= paragraph.getLongestLine()
wordList.push(
<Paragraph key={wordIndex} paragraph={paragraph} x={currentxPos} y={0} width={maxWidth} />
);
if (lineTextInfo.spaces.get(wordInfo.endIndex + 1) === SpaceType.Aya) {
currentxPos -= ayaSpaceWidth
} else {
currentxPos -= simpleSpaceWidth
}
}
return (
<View style={{ width: pageWidth, height: interLine, marginTop: marginTop }}>
<Canvas style={{ width: pageWidth, height: interLine, top: -250 * scale, position: 'relative', left: defaultMargin - margin }}>
{wordList}
</Canvas>
</View>
);
}
const styles = StyleSheet.create({
line: {
flexDirection: 'column',
flex: 1,
justifyContent: "flex-start",
alignItems: "center",
backgroundColor: "rgb(153, 153, 153)",
alignContent: 'flex-start'
}
});
function getColor(className: string) {
switch (className) {
case "green": return [0, 166, 80];
case "tafkim": return [0, 102, 148];
case "lgray": return [156, 154, 155];
case "lkalkala": return [0, 173, 239];
case "red1": return [195, 138, 8];
case "red2": return [244, 114, 22]
case "red3": return [236, 0, 140]
case "red4": return [140, 0, 0]
default: return [0, 0, 0]
}
}
export default LineMultiPars;