younes21000 commited on
Commit
4acf977
1 Parent(s): 97eba94

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -14
app.py CHANGED
@@ -110,26 +110,32 @@ def reverse_text_for_rtl(text):
110
  return ' '.join([word[::-1] for word in text.split()])
111
 
112
  # Helper function to write PDF documents
113
- def write_pdf(transcription, output_file, tokenizer=None, translation_model=None):
114
  # Create PDF with A4 page size
115
  c = canvas.Canvas(output_file, pagesize=A4)
 
116
  # Get the directory where app.py is located
117
  app_dir = os.path.dirname(os.path.abspath(__file__))
118
 
119
- # Construct the full path to the font file
120
- font_path = os.path.join(app_dir, 'B-NAZANIN.TTF')
 
 
 
 
121
 
 
 
 
122
  if os.path.exists(font_path):
123
  try:
124
- pdfmetrics.registerFont(TTFont('B-Nazanin', font_path))
125
- except Exception as e: # Changed to general exception to avoid missing imports
 
126
  raise RuntimeError(f"Error registering font: {e}.")
127
  else:
128
  raise FileNotFoundError(f"Font file not found at {font_path}. Please ensure it is available.")
129
 
130
- # Set font and size
131
- c.setFont('B-Nazanin', 12)
132
-
133
  # Initialize y position from top of page
134
  y_position = A4[1] - 50 # Start 50 points from top
135
  line_height = 20
@@ -145,18 +151,24 @@ def write_pdf(transcription, output_file, tokenizer=None, translation_model=None
145
  # Format the line with segment number
146
  line = f"{i + 1}. {text.strip()}"
147
 
148
- # Reshape and reorder the text for correct RTL display
149
- reshaped_text = arabic_reshaper.reshape(line)
150
- bidi_text = get_display(reshaped_text)
 
 
 
151
 
152
  # Add new page if needed
153
  if y_position < 50: # Leave 50 points margin at bottom
154
  c.showPage()
155
- c.setFont('B-Nazanin', 12)
156
  y_position = A4[1] - 50
157
 
158
- # Draw the text right-aligned
159
- c.drawRightString(A4[0] - 50, y_position, bidi_text) # 50 points margin from right
 
 
 
160
 
161
  # Update y position for next line
162
  y_position -= line_height
@@ -166,6 +178,7 @@ def write_pdf(transcription, output_file, tokenizer=None, translation_model=None
166
  return output_file
167
 
168
 
 
169
  # Helper function to write PowerPoint slides
170
  def write_ppt(transcription, output_file, tokenizer=None, translation_model=None):
171
  ppt = Presentation()
 
110
  return ' '.join([word[::-1] for word in text.split()])
111
 
112
  # Helper function to write PDF documents
113
+ def write_pdf(transcription, output_file, tokenizer=None, translation_model=None, target_language=None):
114
  # Create PDF with A4 page size
115
  c = canvas.Canvas(output_file, pagesize=A4)
116
+
117
  # Get the directory where app.py is located
118
  app_dir = os.path.dirname(os.path.abspath(__file__))
119
 
120
+ # Define font paths for different languages
121
+ fonts = {
122
+ 'fa': os.path.join(app_dir, 'B-NAZANIN.TTF'), # Persian Font
123
+ 'ar': os.path.join(app_dir, 'Amiri-Regular.ttf'), # Arabic Font
124
+ 'default': 'Arial' # Default font for other languages
125
+ }
126
 
127
+ # Register and set the appropriate font
128
+ font_path = fonts.get(target_language, fonts['default'])
129
+
130
  if os.path.exists(font_path):
131
  try:
132
+ pdfmetrics.registerFont(TTFont('custom_font', font_path))
133
+ c.setFont('custom_font', 12)
134
+ except Exception as e:
135
  raise RuntimeError(f"Error registering font: {e}.")
136
  else:
137
  raise FileNotFoundError(f"Font file not found at {font_path}. Please ensure it is available.")
138
 
 
 
 
139
  # Initialize y position from top of page
140
  y_position = A4[1] - 50 # Start 50 points from top
141
  line_height = 20
 
151
  # Format the line with segment number
152
  line = f"{i + 1}. {text.strip()}"
153
 
154
+ # For RTL languages like Persian and Arabic, reshape and reorder text
155
+ if target_language in ['fa', 'ar']:
156
+ reshaped_text = arabic_reshaper.reshape(line)
157
+ bidi_text = get_display(reshaped_text)
158
+ else:
159
+ bidi_text = line # For LTR languages, no reshaping needed
160
 
161
  # Add new page if needed
162
  if y_position < 50: # Leave 50 points margin at bottom
163
  c.showPage()
164
+ c.setFont('custom_font', 12)
165
  y_position = A4[1] - 50
166
 
167
+ # Draw the text right-aligned for RTL languages, otherwise left-aligned
168
+ if target_language in ['fa', 'ar']:
169
+ c.drawRightString(A4[0] - 50, y_position, bidi_text) # Right align for RTL
170
+ else:
171
+ c.drawString(50, y_position, bidi_text) # Left align for LTR
172
 
173
  # Update y position for next line
174
  y_position -= line_height
 
178
  return output_file
179
 
180
 
181
+
182
  # Helper function to write PowerPoint slides
183
  def write_ppt(transcription, output_file, tokenizer=None, translation_model=None):
184
  ppt = Presentation()