Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -14,22 +14,45 @@ from reportlab.lib import colors
|
|
14 |
from reportlab.pdfbase import pdfmetrics
|
15 |
from reportlab.pdfbase.ttfonts import TTFont
|
16 |
|
17 |
-
# --- Step 1:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
# URL
|
20 |
-
|
21 |
-
font_path = "NotoEmoji-Regular.ttf"
|
22 |
|
23 |
-
if not
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
# Register the
|
32 |
-
|
|
|
|
|
33 |
|
34 |
# --- Streamlit App Setup ---
|
35 |
st.set_page_config(layout="wide", initial_sidebar_state="collapsed")
|
@@ -102,7 +125,7 @@ default_markdown = """# Cutting-Edge ML Outline
|
|
102 |
- Documentation synthesis
|
103 |
"""
|
104 |
|
105 |
-
# --- Markdown to PDF
|
106 |
def markdown_to_pdf_content(markdown_text):
|
107 |
lines = markdown_text.strip().split('\n')
|
108 |
pdf_content = []
|
@@ -116,7 +139,7 @@ def markdown_to_pdf_content(markdown_text):
|
|
116 |
continue
|
117 |
|
118 |
if line.startswith('# '):
|
119 |
-
#
|
120 |
pass
|
121 |
elif line.startswith('## '):
|
122 |
if current_item and sub_items:
|
@@ -182,11 +205,11 @@ def create_main_pdf(markdown_text, base_font_size=10, auto_size=False):
|
|
182 |
section_font_size = base_font_size * 1.2
|
183 |
title_font_size = min(16, base_font_size * 1.5)
|
184 |
|
185 |
-
# Define styles using the emoji-supporting font
|
186 |
title_style = ParagraphStyle(
|
187 |
'Heading1',
|
188 |
parent=styles['Heading1'],
|
189 |
-
fontName=
|
190 |
textColor=colors.darkblue,
|
191 |
alignment=1,
|
192 |
fontSize=title_font_size
|
@@ -195,7 +218,7 @@ def create_main_pdf(markdown_text, base_font_size=10, auto_size=False):
|
|
195 |
section_style = ParagraphStyle(
|
196 |
'SectionStyle',
|
197 |
parent=styles['Heading2'],
|
198 |
-
fontName=
|
199 |
textColor=colors.darkblue,
|
200 |
fontSize=section_font_size,
|
201 |
leading=section_font_size * 1.2,
|
@@ -205,7 +228,7 @@ def create_main_pdf(markdown_text, base_font_size=10, auto_size=False):
|
|
205 |
item_style = ParagraphStyle(
|
206 |
'ItemStyle',
|
207 |
parent=styles['Normal'],
|
208 |
-
fontName=
|
209 |
fontSize=item_font_size,
|
210 |
leading=item_font_size * 1.2,
|
211 |
spaceAfter=1
|
@@ -214,7 +237,7 @@ def create_main_pdf(markdown_text, base_font_size=10, auto_size=False):
|
|
214 |
subitem_style = ParagraphStyle(
|
215 |
'SubItemStyle',
|
216 |
parent=styles['Normal'],
|
217 |
-
fontName=
|
218 |
fontSize=subitem_font_size,
|
219 |
leading=subitem_font_size * 1.2,
|
220 |
leftIndent=10,
|
@@ -280,7 +303,7 @@ def pdf_to_image(pdf_bytes):
|
|
280 |
try:
|
281 |
doc = fitz.open(stream=pdf_bytes, filetype="pdf")
|
282 |
page = doc[0]
|
283 |
-
pix = page.get_pixmap(matrix=fitz.Matrix(2.0, 2.0))
|
284 |
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
285 |
doc.close()
|
286 |
return img
|
@@ -288,7 +311,7 @@ def pdf_to_image(pdf_bytes):
|
|
288 |
st.error(f"Failed to render PDF preview: {e}")
|
289 |
return None
|
290 |
|
291 |
-
# --- Sidebar UI for Settings ---
|
292 |
with st.sidebar:
|
293 |
auto_size = st.checkbox("Auto-size text", value=True)
|
294 |
if not auto_size:
|
|
|
14 |
from reportlab.pdfbase import pdfmetrics
|
15 |
from reportlab.pdfbase.ttfonts import TTFont
|
16 |
|
17 |
+
# --- Step 1: Define and Download Available Emoji Fonts ---
|
18 |
+
# List of available TTF filenames from the repository.
|
19 |
+
font_files = [
|
20 |
+
"Noto-COLRv1-emojicompat.ttf",
|
21 |
+
"Noto-COLRv1-noflags.ttf",
|
22 |
+
"Noto-COLRv1.ttf",
|
23 |
+
"NotoColorEmoji-emojicompat.ttf",
|
24 |
+
"NotoColorEmoji-flagsonly.ttf",
|
25 |
+
"NotoColorEmoji-noflags.ttf",
|
26 |
+
"NotoColorEmoji.ttf",
|
27 |
+
"NotoColorEmoji_WindowsCompatible.ttf"
|
28 |
+
]
|
29 |
|
30 |
+
# Base URL to download the fonts from GitHub
|
31 |
+
base_font_url = "https://github.com/googlefonts/noto-emoji/raw/main/fonts/"
|
|
|
32 |
|
33 |
+
# Download each font if not already present.
|
34 |
+
for font_file in font_files:
|
35 |
+
if not os.path.exists(font_file):
|
36 |
+
st.info(f"Downloading {font_file}...")
|
37 |
+
try:
|
38 |
+
urllib.request.urlretrieve(base_font_url + font_file, font_file)
|
39 |
+
st.success(f"Downloaded {font_file}")
|
40 |
+
except Exception as e:
|
41 |
+
st.error(f"Failed to download {font_file}: {e}")
|
42 |
+
|
43 |
+
# --- Step 2: Allow User to Select the Emoji Font ---
|
44 |
+
# Create a mapping for display (remove the .ttf extension for clarity)
|
45 |
+
font_display_names = {f: f.replace(".ttf", "") for f in font_files}
|
46 |
+
selected_font_file = st.sidebar.selectbox(
|
47 |
+
"Select Emoji Font",
|
48 |
+
options=font_files,
|
49 |
+
format_func=lambda f: font_display_names[f]
|
50 |
+
)
|
51 |
|
52 |
+
# Register the selected font with ReportLab.
|
53 |
+
# We use the display name (without .ttf) as the registered font name.
|
54 |
+
registered_font_name = font_display_names[selected_font_file]
|
55 |
+
pdfmetrics.registerFont(TTFont(registered_font_name, selected_font_file))
|
56 |
|
57 |
# --- Streamlit App Setup ---
|
58 |
st.set_page_config(layout="wide", initial_sidebar_state="collapsed")
|
|
|
125 |
- Documentation synthesis
|
126 |
"""
|
127 |
|
128 |
+
# --- Markdown to PDF Content Processing ---
|
129 |
def markdown_to_pdf_content(markdown_text):
|
130 |
lines = markdown_text.strip().split('\n')
|
131 |
pdf_content = []
|
|
|
139 |
continue
|
140 |
|
141 |
if line.startswith('# '):
|
142 |
+
# Optionally, skip main title
|
143 |
pass
|
144 |
elif line.startswith('## '):
|
145 |
if current_item and sub_items:
|
|
|
205 |
section_font_size = base_font_size * 1.2
|
206 |
title_font_size = min(16, base_font_size * 1.5)
|
207 |
|
208 |
+
# Define styles using the selected emoji-supporting font.
|
209 |
title_style = ParagraphStyle(
|
210 |
'Heading1',
|
211 |
parent=styles['Heading1'],
|
212 |
+
fontName=registered_font_name,
|
213 |
textColor=colors.darkblue,
|
214 |
alignment=1,
|
215 |
fontSize=title_font_size
|
|
|
218 |
section_style = ParagraphStyle(
|
219 |
'SectionStyle',
|
220 |
parent=styles['Heading2'],
|
221 |
+
fontName=registered_font_name,
|
222 |
textColor=colors.darkblue,
|
223 |
fontSize=section_font_size,
|
224 |
leading=section_font_size * 1.2,
|
|
|
228 |
item_style = ParagraphStyle(
|
229 |
'ItemStyle',
|
230 |
parent=styles['Normal'],
|
231 |
+
fontName=registered_font_name,
|
232 |
fontSize=item_font_size,
|
233 |
leading=item_font_size * 1.2,
|
234 |
spaceAfter=1
|
|
|
237 |
subitem_style = ParagraphStyle(
|
238 |
'SubItemStyle',
|
239 |
parent=styles['Normal'],
|
240 |
+
fontName=registered_font_name,
|
241 |
fontSize=subitem_font_size,
|
242 |
leading=subitem_font_size * 1.2,
|
243 |
leftIndent=10,
|
|
|
303 |
try:
|
304 |
doc = fitz.open(stream=pdf_bytes, filetype="pdf")
|
305 |
page = doc[0]
|
306 |
+
pix = page.get_pixmap(matrix=fitz.Matrix(2.0, 2.0))
|
307 |
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
308 |
doc.close()
|
309 |
return img
|
|
|
311 |
st.error(f"Failed to render PDF preview: {e}")
|
312 |
return None
|
313 |
|
314 |
+
# --- Sidebar UI for Additional Settings ---
|
315 |
with st.sidebar:
|
316 |
auto_size = st.checkbox("Auto-size text", value=True)
|
317 |
if not auto_size:
|