File size: 12,202 Bytes
93c4f75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd58911
93c4f75
 
 
 
fd58911
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93c4f75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fb5ace
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93c4f75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import os
import tempfile
from reportlab.lib.pagesizes import letter
from reportlab.lib import colors
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
import io

# Try to register a font that supports Braille Unicode characters
try:
    # Check for common Braille fonts
    font_paths = [
        "DejaVuSans.ttf",  # Common on Linux
        "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
        "/System/Library/Fonts/Arial Unicode.ttf",  # Mac
        "C:\\Windows\\Fonts\\arial.ttf"  # Windows
    ]
    
    font_registered = False
    for font_path in font_paths:
        if os.path.exists(font_path):
            pdfmetrics.registerFont(TTFont('BrailleFont', font_path))
            font_registered = True
            break
    
    if not font_registered:
        # Use default font if none of the above are found
        print("No suitable font found for Braille. Using default font.")
except Exception as e:
    print(f"Error registering font: {str(e)}")


def create_braille_pdf(original_text, braille_text, title="Menu in Braille"):
    """
    Create a PDF file with original text and its Braille translation.
    
    Args:
        original_text: Original text content
        braille_text: Braille translation
        title: PDF title
        
    Returns:
        BytesIO object containing the PDF
    """
    try:
        # Create a BytesIO object to store the PDF
        buffer = io.BytesIO()
        
        # Create the PDF document
        doc = SimpleDocTemplate(
            buffer,
            pagesize=letter,
            rightMargin=72,
            leftMargin=72,
            topMargin=72,
            bottomMargin=72
        )
        
        # Define styles
        styles = getSampleStyleSheet()
        title_style = styles['Title']
        heading_style = styles['Heading2']
        normal_style = styles['Normal']
        
        # Create a custom style for Braille text
        braille_style = ParagraphStyle(
            'Braille',
            parent=normal_style,
            fontName='Helvetica',  # Use standard font to avoid issues
            fontSize=14,
            leading=18,
            spaceAfter=12
        )
        
        # Create the content
        content = []
        
        # Add title
        content.append(Paragraph(title, title_style))
        content.append(Spacer(1, 12))
        
        # Add original text section
        content.append(Paragraph("Original Text", heading_style))
        content.append(Spacer(1, 6))
        
        # Split original text by lines and add each as a paragraph
        for line in original_text.split('\n'):
            if line.strip():
                content.append(Paragraph(line.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;'), normal_style))
            else:
                content.append(Spacer(1, 12))
        
        content.append(Spacer(1, 24))
        
        # Add Braille section
        content.append(Paragraph("Braille Translation", heading_style))
        content.append(Spacer(1, 6))
        
        # Split Braille text by lines and add each as a paragraph
        for line in braille_text.split('\n'):
            if line.strip():
                content.append(Paragraph(line.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;'), braille_style))
            else:
                content.append(Spacer(1, 12))
        
        # Build the PDF
        doc.build(content)
        
        # Reset buffer position to the beginning
        buffer.seek(0)
        return buffer
    except Exception as e:
        print(f"Error in create_braille_pdf: {str(e)}")
        raise


def create_braille_pdf1(original_text, braille_text, title="Menu in Braille"):
    """
    Create a PDF file with original text and its Braille translation.
    
    Args:
        original_text: Original text content
        braille_text: Braille translation
        title: PDF title
        
    Returns:
        BytesIO object containing the PDF
    """
    # Create a BytesIO object to store the PDF
    buffer = io.BytesIO()
    
    # Create the PDF document
    doc = SimpleDocTemplate(
        buffer,
        pagesize=letter,
        rightMargin=72,
        leftMargin=72,
        topMargin=72,
        bottomMargin=72
    )
    
    # Define styles
    styles = getSampleStyleSheet()
    title_style = styles['Title']
    heading_style = styles['Heading2']
    normal_style = styles['Normal']
    
    # Create a custom style for Braille text
    braille_style = ParagraphStyle(
        'Braille',
        parent=normal_style,
        fontName='BrailleFont' if font_registered else 'Helvetica',
        fontSize=14,
        leading=18,
        spaceAfter=12
    )
    
    # Create the content
    content = []
    
    # Add title
    content.append(Paragraph(title, title_style))
    content.append(Spacer(1, 12))
    
    # Add original text section
    content.append(Paragraph("Original Text", heading_style))
    content.append(Spacer(1, 6))
    
    # Split original text by lines and add each as a paragraph
    for line in original_text.split('\n'):
        if line.strip():
            content.append(Paragraph(line, normal_style))
        else:
            content.append(Spacer(1, 12))
    
    content.append(Spacer(1, 24))
    
    # Add Braille section
    content.append(Paragraph("Braille Translation", heading_style))
    content.append(Spacer(1, 6))
    
    # Split Braille text by lines and add each as a paragraph
    for line in braille_text.split('\n'):
        if line.strip():
            content.append(Paragraph(line, braille_style))
        else:
            content.append(Spacer(1, 12))
    
    # Build the PDF
    doc.build(content)
    
    # Reset buffer position to the beginning
    buffer.seek(0)
    return buffer

def create_braille_pdf_with_comparison(original_text, braille_text, title="Menu in Braille"):
    """
    Create a PDF file with side-by-side comparison of original text and Braille.
    
    Args:
        original_text: Original text content
        braille_text: Braille translation
        title: PDF title
        
    Returns:
        BytesIO object containing the PDF
    """
    try:
        # Create a BytesIO object to store the PDF
        buffer = io.BytesIO()
        
        # Create the PDF document
        doc = SimpleDocTemplate(
            buffer,
            pagesize=letter,
            rightMargin=72,
            leftMargin=72,
            topMargin=72,
            bottomMargin=72
        )
        
        # Define styles
        styles = getSampleStyleSheet()
        title_style = styles['Title']
        heading_style = styles['Heading2']
        normal_style = styles['Normal']
        
        # Create a custom style for Braille text - use standard font
        braille_style = ParagraphStyle(
            'Braille',
            parent=normal_style,
            fontName='Helvetica',
            fontSize=14,
            leading=18
        )
        
        # Create the content
        content = []
        
        # Add title
        content.append(Paragraph(title, title_style))
        content.append(Spacer(1, 12))
        
        # Create a simpler table structure
        data = [["Original Text", "Braille Translation"]]
        
        # Process text line by line
        orig_lines = original_text.split('\n')
        braille_lines = braille_text.split('\n')
        
        # Make sure both lists have the same length
        max_len = max(len(orig_lines), len(braille_lines))
        orig_lines = orig_lines + [''] * (max_len - len(orig_lines))
        braille_lines = braille_lines + [''] * (max_len - len(braille_lines))
        
        # Add each line pair to the table
        for i in range(max_len):
            orig = orig_lines[i].replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')
            braille = braille_lines[i].replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')
            data.append([orig, braille])
        
        # Create the table
        table = Table(data, colWidths=[doc.width/2-12, doc.width/2-12])
        table.setStyle(TableStyle([
            ('FONT', (0, 0), (1, 0), 'Helvetica-Bold'),
            ('BACKGROUND', (0, 0), (1, 0), colors.lightgrey),
            ('ALIGN', (0, 0), (1, 0), 'CENTER'),
            ('VALIGN', (0, 0), (-1, -1), 'TOP'),
            ('GRID', (0, 0), (1, 0), 1, colors.black),
            ('BOX', (0, 0), (-1, -1), 1, colors.black),
            ('LINEABOVE', (0, 1), (-1, -1), 1, colors.black),
            ('FONT', (0, 1), (0, -1), 'Helvetica'),
            ('FONT', (1, 1), (1, -1), 'Helvetica')
        ]))
        
        content.append(table)
        
        # Build the PDF
        doc.build(content)
        
        # Reset buffer position to the beginning
        buffer.seek(0)
        return buffer
    except Exception as e:
        print(f"Error in create_braille_pdf_with_comparison: {str(e)}")
        # Return the error message in a simple PDF
        simple_buffer = io.BytesIO()
        doc = SimpleDocTemplate(simple_buffer, pagesize=letter)
        styles = getSampleStyleSheet()
        content = [Paragraph(f"Error creating PDF: {str(e)}", styles['Normal'])]
        doc.build(content)
        simple_buffer.seek(0)
        return simple_buffer


def create_braille_pdf_with_comparison1(original_text, braille_text, title="Menu in Braille"):
    """
    Create a PDF file with side-by-side comparison of original text and Braille.
    
    Args:
        original_text: Original text content
        braille_text: Braille translation
        title: PDF title
        
    Returns:
        BytesIO object containing the PDF
    """
    # Create a BytesIO object to store the PDF
    buffer = io.BytesIO()
    
    # Create the PDF document
    doc = SimpleDocTemplate(
        buffer,
        pagesize=letter,
        rightMargin=72,
        leftMargin=72,
        topMargin=72,
        bottomMargin=72
    )
    
    # Define styles
    styles = getSampleStyleSheet()
    title_style = styles['Title']
    heading_style = styles['Heading2']
    normal_style = styles['Normal']
    
    # Create a custom style for Braille text
    braille_style = ParagraphStyle(
        'Braille',
        parent=normal_style,
        fontName='BrailleFont' if font_registered else 'Helvetica',
        fontSize=14,
        leading=18
    )
    
    # Create the content
    content = []
    
    # Add title
    content.append(Paragraph(title, title_style))
    content.append(Spacer(1, 12))
    
    # Split text into lines
    original_lines = original_text.split('\n')
    braille_lines = braille_text.split('\n')
    
    # Ensure both lists have the same length
    max_lines = max(len(original_lines), len(braille_lines))
    original_lines = original_lines + [''] * (max_lines - len(original_lines))
    braille_lines = braille_lines + [''] * (max_lines - len(braille_lines))
    
    # Create a table for side-by-side comparison
    table_data = [
        [Paragraph("Original Text", heading_style), Paragraph("Braille Translation", heading_style)]
    ]
    
    # Add each line as a row in the table
    for i in range(max_lines):
        original_para = Paragraph(original_lines[i], normal_style) if original_lines[i].strip() else Spacer(1, 12)
        braille_para = Paragraph(braille_lines[i], braille_style) if braille_lines[i].strip() else Spacer(1, 12)
        table_data.append([original_para, braille_para])
    
    # Create the table
    table = Table(table_data, colWidths=[doc.width/2.0-12, doc.width/2.0-12])
    
    # Style the table
    table.setStyle(TableStyle([
        ('VALIGN', (0, 0), (-1, -1), 'TOP'),
        ('GRID', (0, 0), (-1, 0), 1, colors.black),
        ('BOX', (0, 0), (-1, -1), 1, colors.black),
        ('BACKGROUND', (0, 0), (1, 0), colors.lightgrey)
    ]))
    
    content.append(table)
    
    # Build the PDF
    doc.build(content)
    
    # Reset buffer position to the beginning
    buffer.seek(0)
    return buffer