Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,21 +11,21 @@ from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
|
| 11 |
from reportlab.lib import colors
|
| 12 |
from reportlab.pdfbase import pdfmetrics
|
| 13 |
from reportlab.pdfbase.ttfonts import TTFont
|
|
|
|
| 14 |
|
| 15 |
st.set_page_config(layout="wide", initial_sidebar_state="collapsed")
|
| 16 |
|
| 17 |
def create_pdf_tab(default_markdown):
|
| 18 |
# Dynamically load all .ttf fonts from the current directory
|
| 19 |
-
# Fonts are sourced from: https://fonts.google.com/download/next-steps
|
| 20 |
font_files = glob.glob("*.ttf")
|
| 21 |
if not font_files:
|
| 22 |
-
st.error("No .ttf font files found in the current directory. Please add some, e.g.,
|
| 23 |
return
|
| 24 |
available_fonts = {os.path.splitext(os.path.basename(f))[0]: f for f in font_files}
|
| 25 |
|
| 26 |
# Sidebar configuration
|
| 27 |
with st.sidebar:
|
| 28 |
-
selected_font_name = st.selectbox("Select Font", options=list(available_fonts.keys()), index=0 if "
|
| 29 |
selected_font_path = available_fonts[selected_font_name]
|
| 30 |
base_font_size = st.slider("Font Size (points)", min_value=6, max_value=16, value=9, step=1)
|
| 31 |
plain_text_mode = st.checkbox("Render as Plain Text (Preserve Bold Only)", value=False)
|
|
@@ -42,18 +42,16 @@ def create_pdf_tab(default_markdown):
|
|
| 42 |
|
| 43 |
st.download_button(label="Save Markdown", data=st.session_state.markdown_content, file_name="deities_guide.md", mime="text/markdown")
|
| 44 |
|
| 45 |
-
# Register the selected font
|
| 46 |
-
# Noto Color Emoji is a color font, which ReportLab may not render correctly (monochrome only).
|
| 47 |
-
# If emojis don’t show, try a monochrome font like NotoEmoji-Regular.ttf as a fallback.
|
| 48 |
try:
|
| 49 |
pdfmetrics.registerFont(TTFont(selected_font_name, selected_font_path))
|
|
|
|
|
|
|
| 50 |
except Exception as e:
|
| 51 |
st.error(f"Failed to register font {selected_font_name}: {e}")
|
| 52 |
return
|
| 53 |
|
| 54 |
-
# Emoji font application
|
| 55 |
-
# We apply the selected font to emojis, falling back to plain text if the font doesn’t support them.
|
| 56 |
-
# ReportLab needs explicit font tagging for emojis; Noto Color Emoji may need special handling.
|
| 57 |
def apply_emoji_font(text, emoji_font):
|
| 58 |
emoji_pattern = re.compile(
|
| 59 |
r"([\U0001F300-\U0001F5FF" # Miscellaneous Symbols and Pictographs
|
|
@@ -69,13 +67,29 @@ def create_pdf_tab(default_markdown):
|
|
| 69 |
r"\u2700-\u27BF]+" # Dingbats (e.g., ✝ U+271D)
|
| 70 |
r")"
|
| 71 |
)
|
|
|
|
| 72 |
def replace_emoji(match):
|
| 73 |
emoji = match.group(1)
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
# Wrap emoji in font tag; if it doesn’t render, it’ll
|
| 77 |
return f'<font face="{emoji_font}">{emoji}</font>'
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
# Markdown to PDF content
|
| 81 |
def markdown_to_pdf_content(markdown_text, plain_text_mode, auto_bold_numbers):
|
|
@@ -122,7 +136,7 @@ def create_pdf_tab(default_markdown):
|
|
| 122 |
item_font_size = base_font_size
|
| 123 |
section_font_size = base_font_size * 1.1
|
| 124 |
|
| 125 |
-
#
|
| 126 |
section_style = ParagraphStyle(
|
| 127 |
'SectionStyle', parent=styles['Heading2'], fontName="Helvetica-Bold",
|
| 128 |
textColor=colors.darkblue, fontSize=section_font_size, leading=section_font_size * 1.2, spaceAfter=2
|
|
@@ -210,6 +224,7 @@ def create_pdf_tab(default_markdown):
|
|
| 210 |
with st.sidebar:
|
| 211 |
st.download_button(label="Download PDF", data=pdf_bytes, file_name="deities_guide.pdf", mime="application/pdf")
|
| 212 |
|
|
|
|
| 213 |
default_markdown = """# Deities Guide: Mythology and Moral Lessons 🌟
|
| 214 |
|
| 215 |
1. 📜 Introduction
|
|
|
|
| 11 |
from reportlab.lib import colors
|
| 12 |
from reportlab.pdfbase import pdfmetrics
|
| 13 |
from reportlab.pdfbase.ttfonts import TTFont
|
| 14 |
+
import unicodedata
|
| 15 |
|
| 16 |
st.set_page_config(layout="wide", initial_sidebar_state="collapsed")
|
| 17 |
|
| 18 |
def create_pdf_tab(default_markdown):
|
| 19 |
# Dynamically load all .ttf fonts from the current directory
|
|
|
|
| 20 |
font_files = glob.glob("*.ttf")
|
| 21 |
if not font_files:
|
| 22 |
+
st.error("No .ttf font files found in the current directory. Please add some, e.g., NotoEmoji-Regular.ttf.")
|
| 23 |
return
|
| 24 |
available_fonts = {os.path.splitext(os.path.basename(f))[0]: f for f in font_files}
|
| 25 |
|
| 26 |
# Sidebar configuration
|
| 27 |
with st.sidebar:
|
| 28 |
+
selected_font_name = st.selectbox("Select Font", options=list(available_fonts.keys()), index=0 if "NotoEmoji-Regular" in available_fonts else 0)
|
| 29 |
selected_font_path = available_fonts[selected_font_name]
|
| 30 |
base_font_size = st.slider("Font Size (points)", min_value=6, max_value=16, value=9, step=1)
|
| 31 |
plain_text_mode = st.checkbox("Render as Plain Text (Preserve Bold Only)", value=False)
|
|
|
|
| 42 |
|
| 43 |
st.download_button(label="Save Markdown", data=st.session_state.markdown_content, file_name="deities_guide.md", mime="text/markdown")
|
| 44 |
|
| 45 |
+
# Register the selected font and a fallback font
|
|
|
|
|
|
|
| 46 |
try:
|
| 47 |
pdfmetrics.registerFont(TTFont(selected_font_name, selected_font_path))
|
| 48 |
+
# Register a fallback font (e.g., Helvetica) for non-emoji text
|
| 49 |
+
pdfmetrics.registerFont(TTFont("Helvetica", "Helvetica.ttf")) # Ensure Helvetica is available
|
| 50 |
except Exception as e:
|
| 51 |
st.error(f"Failed to register font {selected_font_name}: {e}")
|
| 52 |
return
|
| 53 |
|
| 54 |
+
# Emoji font application with fallback
|
|
|
|
|
|
|
| 55 |
def apply_emoji_font(text, emoji_font):
|
| 56 |
emoji_pattern = re.compile(
|
| 57 |
r"([\U0001F300-\U0001F5FF" # Miscellaneous Symbols and Pictographs
|
|
|
|
| 67 |
r"\u2700-\u27BF]+" # Dingbats (e.g., ✝ U+271D)
|
| 68 |
r")"
|
| 69 |
)
|
| 70 |
+
|
| 71 |
def replace_emoji(match):
|
| 72 |
emoji = match.group(1)
|
| 73 |
+
# Normalize emoji to avoid multi-codepoint issues (e.g., combining characters)
|
| 74 |
+
emoji = unicodedata.normalize('NFC', emoji)
|
| 75 |
+
# Wrap emoji in font tag; if it doesn’t render, it’ll fall back to text
|
| 76 |
return f'<font face="{emoji_font}">{emoji}</font>'
|
| 77 |
+
|
| 78 |
+
# Split text into segments: emoji and non-emoji
|
| 79 |
+
segments = []
|
| 80 |
+
last_pos = 0
|
| 81 |
+
for match in emoji_pattern.finditer(text):
|
| 82 |
+
start, end = match.span()
|
| 83 |
+
# Add non-emoji text with Helvetica
|
| 84 |
+
if last_pos < start:
|
| 85 |
+
segments.append(f'<font face="Helvetica">{text[last_pos:start]}</font>')
|
| 86 |
+
# Add emoji with the selected font
|
| 87 |
+
segments.append(replace_emoji(match))
|
| 88 |
+
last_pos = end
|
| 89 |
+
# Add remaining non-emoji text
|
| 90 |
+
if last_pos < len(text):
|
| 91 |
+
segments.append(f'<font face="Helvetica">{text[last_pos:]}</font>')
|
| 92 |
+
return ''.join(segments)
|
| 93 |
|
| 94 |
# Markdown to PDF content
|
| 95 |
def markdown_to_pdf_content(markdown_text, plain_text_mode, auto_bold_numbers):
|
|
|
|
| 136 |
item_font_size = base_font_size
|
| 137 |
section_font_size = base_font_size * 1.1
|
| 138 |
|
| 139 |
+
# Define styles with explicit font names
|
| 140 |
section_style = ParagraphStyle(
|
| 141 |
'SectionStyle', parent=styles['Heading2'], fontName="Helvetica-Bold",
|
| 142 |
textColor=colors.darkblue, fontSize=section_font_size, leading=section_font_size * 1.2, spaceAfter=2
|
|
|
|
| 224 |
with st.sidebar:
|
| 225 |
st.download_button(label="Download PDF", data=pdf_bytes, file_name="deities_guide.pdf", mime="application/pdf")
|
| 226 |
|
| 227 |
+
# Your default markdown content remains unchanged
|
| 228 |
default_markdown = """# Deities Guide: Mythology and Moral Lessons 🌟
|
| 229 |
|
| 230 |
1. 📜 Introduction
|