262 column = 0 |
262 column = 0 |
263 pos = 0 |
263 pos = 0 |
264 deltaStyle = "" |
264 deltaStyle = "" |
265 styleCurrent = -1 |
265 styleCurrent = -1 |
266 utf8 = self.editor.isUtf8() |
266 utf8 = self.editor.isUtf8() |
267 utf8Ch = "" |
267 utf8Ch = b"" |
268 utf8Len = 0 |
268 utf8Len = 0 |
269 |
269 |
270 while pos < lengthDoc: |
270 while pos < lengthDoc: |
271 ch = self.editor.rawCharAt(pos) |
271 ch = self.editor.byteAt(pos) |
272 style = self.editor.styleAt(pos) |
272 style = self.editor.styleAt(pos) |
273 if style != styleCurrent: |
273 if style != styleCurrent: |
274 deltaStyle = self.__GetRTFStyleChange(lastStyle, styles[style]) |
274 deltaStyle = self.__GetRTFStyleChange(lastStyle, styles[style]) |
275 if deltaStyle: |
275 if deltaStyle: |
276 f.write(deltaStyle) |
276 f.write(deltaStyle) |
277 styleCurrent = style |
277 styleCurrent = style |
278 lastStyle = styles[style] |
278 lastStyle = styles[style] |
279 |
279 |
280 if ch == '{': |
280 if ch == b'{': |
281 f.write('\\{') |
281 f.write('\\{') |
282 elif ch == '}': |
282 elif ch == b'}': |
283 f.write('\\}') |
283 f.write('\\}') |
284 elif ch == '\\': |
284 elif ch == b'\\': |
285 f.write('\\\\') |
285 f.write('\\\\') |
286 elif ch == '\t': |
286 elif ch == b'\t': |
287 if tabs: |
287 if tabs: |
288 f.write(self.RTF_TAB) |
288 f.write(self.RTF_TAB) |
289 else: |
289 else: |
290 ts = tabSize - (column % tabSize) |
290 ts = tabSize - (column % tabSize) |
291 f.write(' ' * ts) |
291 f.write(' ' * ts) |
292 column += ts - 1 |
292 column += ts - 1 |
293 elif ch == '\n': |
293 elif ch == b'\n': |
294 if not prevCR: |
294 if not prevCR: |
295 f.write(self.RTF_EOLN) |
295 f.write(self.RTF_EOLN) |
296 column -= 1 |
296 column -= 1 |
297 elif ch == '\r': |
297 elif ch == b'\r': |
298 f.write(self.RTF_EOLN) |
298 f.write(self.RTF_EOLN) |
299 column -= 1 |
299 column -= 1 |
300 else: |
300 else: |
301 if ord(ch) > 0x7F and utf8: |
301 if ord(ch) > 0x7F and utf8: |
302 utf8Ch += ch |
302 utf8Ch += ch |
303 if utf8Len == 0: |
303 if utf8Len == 0: |
304 if (ord(utf8Ch[0]) & 0xF0) == 0xF0: |
304 if (utf8Ch[0] & 0xF0) == 0xF0: |
305 utf8Len = 4 |
305 utf8Len = 4 |
306 elif (ord(utf8Ch[0]) & 0xE0) == 0xE0: |
306 elif (utf8Ch[0] & 0xE0) == 0xE0: |
307 utf8Len = 3 |
307 utf8Len = 3 |
308 elif (ord(utf8Ch[0]) & 0xC0) == 0xC0: |
308 elif (utf8Ch[0] & 0xC0) == 0xC0: |
309 utf8Len = 2 |
309 utf8Len = 2 |
310 column -= 1 # will be incremented again later |
310 column -= 1 # will be incremented again later |
311 elif len(utf8Ch) == utf8Len: |
311 elif len(utf8Ch) == utf8Len: |
312 ch = utf8Ch.decode('utf8') |
312 ch = utf8Ch.decode('utf8') |
313 if ord(ch) <= 0xff: |
313 if ord(ch) <= 0xff: |
314 f.write("\\'%x" % ord(ch)) |
314 f.write("\\'%x" % ord(ch)) |
315 else: |
315 else: |
316 f.write("\\u%d\\'%x" % (ord(ch), ord(ch) & 0xFF)) |
316 f.write("\\u%d\\'%x" % (ord(ch), ord(ch) & 0xFF)) |
317 utf8Ch = "" |
317 utf8Ch = b"" |
318 utf8Len = 0 |
318 utf8Len = 0 |
319 else: |
319 else: |
320 column -= 1 # will be incremented again later |
320 column -= 1 # will be incremented again later |
321 else: |
321 else: |
322 f.write(ch) |
322 f.write(ch.decode()) |
323 |
323 |
324 column += 1 |
324 column += 1 |
325 prevCR = ch == '\r' |
325 prevCR = ch == '\r' |
326 pos += 1 |
326 pos += 1 |
327 |
327 |