| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 |
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>字体预览工具</title>
- <style>
- * {
- margin: 0;
- padding: 0;
- box-sizing: border-box;
- }
- body {
- font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
- min-height: 100vh;
- padding: 20px;
- }
- .container {
- max-width: 1200px;
- margin: 0 auto;
- background: white;
- border-radius: 12px;
- box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
- padding: 30px;
- }
- h1 {
- color: #333;
- margin-bottom: 30px;
- text-align: center;
- font-size: 28px;
- font-weight: 600;
- }
- .form-section {
- margin-bottom: 25px;
- }
- label {
- display: block;
- margin-bottom: 8px;
- color: #555;
- font-weight: 500;
- font-size: 14px;
- }
- input[type="text"],
- select {
- width: 100%;
- padding: 12px;
- border: 2px solid #e0e0e0;
- border-radius: 6px;
- font-size: 14px;
- transition: border-color 0.3s;
- }
- input[type="text"]:focus,
- select:focus {
- outline: none;
- border-color: #667eea;
- }
- input[type="number"],
- input[type="color"] {
- padding: 8px;
- border: 2px solid #e0e0e0;
- border-radius: 6px;
- font-size: 14px;
- transition: border-color 0.3s;
- }
- input[type="number"]:focus {
- outline: none;
- border-color: #667eea;
- }
- .form-row {
- display: grid;
- grid-template-columns: 1fr 1fr;
- gap: 15px;
- margin-bottom: 15px;
- }
- .form-row.full-width {
- grid-template-columns: 1fr;
- }
- .form-group {
- display: flex;
- flex-direction: column;
- }
- .form-group label {
- margin-bottom: 5px;
- }
- button {
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
- color: white;
- border: none;
- padding: 12px 30px;
- border-radius: 6px;
- font-size: 16px;
- font-weight: 500;
- cursor: pointer;
- transition: transform 0.2s, box-shadow 0.2s;
- width: 100%;
- }
- button:hover {
- transform: translateY(-2px);
- box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
- }
- button:active {
- transform: translateY(0);
- }
- button:disabled {
- background: #ccc;
- cursor: not-allowed;
- transform: none;
- }
- .preview-section {
- margin-top: 30px;
- padding: 25px;
- background: #f8f9fa;
- border-radius: 8px;
- min-height: 200px;
- }
- .preview-title {
- font-size: 16px;
- font-weight: 600;
- color: #333;
- margin-bottom: 15px;
- }
- .preview-image {
- max-width: 100%;
- height: auto;
- border-radius: 6px;
- box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
- background:
- repeating-linear-gradient(45deg, #f0f0f0, #f0f0f0 10px, #ffffff 10px, #ffffff 20px);
- padding: 20px;
- display: block;
- margin: 0 auto;
- }
- .loading {
- text-align: center;
- padding: 40px;
- color: #667eea;
- font-size: 16px;
- }
- .error {
- background: #fee;
- color: #c33;
- padding: 15px;
- border-radius: 6px;
- margin-top: 15px;
- border-left: 4px solid #c33;
- }
- .success {
- background: #efe;
- color: #3c3;
- padding: 15px;
- border-radius: 6px;
- margin-top: 15px;
- border-left: 4px solid #3c3;
- }
- .checkbox-group {
- display: flex;
- align-items: center;
- gap: 8px;
- }
- .checkbox-group input[type="checkbox"] {
- width: 18px;
- height: 18px;
- cursor: pointer;
- }
- .color-input-group {
- display: flex;
- gap: 10px;
- align-items: center;
- }
- .color-input-group input[type="color"] {
- width: 50px;
- height: 40px;
- cursor: pointer;
- }
- .color-input-group input[type="text"] {
- flex: 1;
- }
- select {
- cursor: pointer;
- }
- @media (max-width: 768px) {
- .form-row {
- grid-template-columns: 1fr;
- }
-
- .container {
- padding: 20px;
- }
- }
- </style>
- </head>
- <body>
- <div class="container">
- <h1>🎨 字体预览工具</h1>
-
- <div class="form-section">
- <label for="fontSelect">选择字体</label>
- <select id="fontSelect">
- <option value="">加载中...</option>
- </select>
- </div>
- <div class="form-section">
- <label for="textInput">输入文字</label>
- <input type="text" id="textInput" placeholder="请输入要预览的文字" value="字体预览">
- </div>
- <div class="form-row">
- <div class="form-group">
- <label for="fontSize">字体大小</label>
- <input type="number" id="fontSize" value="60" min="10" max="200">
- </div>
- <div class="form-group">
- <label for="padding">内边距</label>
- <input type="number" id="padding" value="20" min="0" max="100">
- </div>
- </div>
- <div class="form-row">
- <div class="form-group">
- <label for="textColor">文字颜色</label>
- <div class="color-input-group">
- <input type="color" id="textColor" value="#000000">
- <input type="text" id="textColorInput" value="#000000" placeholder="#000000">
- </div>
- </div>
- <div class="form-group">
- <label for="backgroundColor">背景颜色(透明)</label>
- <div class="color-input-group">
- <input type="color" id="backgroundColor" value="#ffffff" disabled>
- <input type="text" id="backgroundColorInput" value="透明" placeholder="透明背景" disabled>
- </div>
- </div>
- </div>
- <div class="form-section">
- <div class="checkbox-group">
- <input type="checkbox" id="handwritingEffect" checked>
- <label for="handwritingEffect" style="margin: 0;">启用手写效果(轻微位置扰动)</label>
- </div>
- </div>
- <button id="generateBtn" onclick="generatePreview()">生成预览</button>
- <div class="preview-section">
- <div class="preview-title">预览效果</div>
- <div id="previewContainer">
- <div class="loading">请选择字体并输入文字,然后点击"生成预览"</div>
- </div>
- </div>
- </div>
- <script>
- // 自动检测环境,本地开发时不添加前缀,生产环境添加 /PdfProcessing 前缀
- const isLocal = window.location.hostname === 'localhost' ||
- window.location.hostname === '127.0.0.1' ||
- window.location.hostname === '';
- const API_BASE = window.location.origin + (isLocal ? '' : '/PdfProcessing');
- let fonts = [];
- // 加载字体列表
- async function loadFonts() {
- try {
- const response = await fetch(`${API_BASE}/get_font_list`);
- const data = await response.json();
-
- if (data.success && data.fonts) {
- fonts = data.fonts;
- const fontSelect = document.getElementById('fontSelect');
- fontSelect.innerHTML = '<option value="">请选择字体</option>';
-
- fonts.forEach(font => {
- const option = document.createElement('option');
- option.value = font.path;
- option.textContent = font.name;
- fontSelect.appendChild(option);
- });
- } else {
- throw new Error(data.error || '加载字体列表失败');
- }
- } catch (error) {
- console.error('加载字体列表失败:', error);
- document.getElementById('fontSelect').innerHTML =
- '<option value="">加载失败,请刷新页面重试</option>';
- }
- }
- // 颜色输入同步
- document.getElementById('textColor').addEventListener('input', function(e) {
- document.getElementById('textColorInput').value = e.target.value;
- });
- document.getElementById('textColorInput').addEventListener('input', function(e) {
- const color = e.target.value;
- if (/^#[0-9A-F]{6}$/i.test(color)) {
- document.getElementById('textColor').value = color;
- }
- });
- // 将十六进制颜色转换为RGB数组
- function hexToRgb(hex) {
- const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
- return result ? [
- parseInt(result[1], 16),
- parseInt(result[2], 16),
- parseInt(result[3], 16)
- ] : [0, 0, 0];
- }
- // 生成预览
- async function generatePreview() {
- const fontSelect = document.getElementById('fontSelect');
- const textInput = document.getElementById('textInput');
- const fontSize = parseInt(document.getElementById('fontSize').value);
- const padding = parseInt(document.getElementById('padding').value);
- const textColor = hexToRgb(document.getElementById('textColor').value);
- const handwritingEffect = document.getElementById('handwritingEffect').checked;
-
- if (!fontSelect.value) {
- alert('请先选择字体');
- return;
- }
-
- if (!textInput.value.trim()) {
- alert('请输入要预览的文字');
- return;
- }
- const generateBtn = document.getElementById('generateBtn');
- const previewContainer = document.getElementById('previewContainer');
-
- generateBtn.disabled = true;
- generateBtn.textContent = '生成中...';
- previewContainer.innerHTML = '<div class="loading">正在生成预览...</div>';
- try {
- const response = await fetch(`${API_BASE}/generate_handwriting`, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- },
- body: JSON.stringify({
- text: textInput.value,
- font_size: fontSize,
- padding: padding,
- background_color: [255, 255, 255, 0], // 透明背景
- text_color: textColor,
- font_path: fontSelect.value,
- use_handwriting_effect: handwritingEffect
- })
- });
- if (!response.ok) {
- // 如果响应不是图片,尝试解析为JSON错误信息
- const errorData = await response.json().catch(() => null);
- throw new Error(errorData?.error || `HTTP错误: ${response.status}`);
- }
- // 检查响应类型是否为图片
- const contentType = response.headers.get('content-type');
- if (contentType && contentType.startsWith('image/')) {
- // 创建图片URL(使用Blob)
- const blob = await response.blob();
- const imageUrl = URL.createObjectURL(blob);
-
- previewContainer.innerHTML = `
- <img src="${imageUrl}" alt="字体预览" class="preview-image">
- <div class="success" style="margin-top: 15px;">
- 生成成功!点击图片可以右键保存。
- </div>
- `;
- } else {
- // 如果不是图片,尝试解析为JSON
- const data = await response.json();
- throw new Error(data.error || '生成失败');
- }
- } catch (error) {
- console.error('生成预览失败:', error);
- previewContainer.innerHTML = `
- <div class="error">
- 生成失败: ${error.message}
- </div>
- `;
- } finally {
- generateBtn.disabled = false;
- generateBtn.textContent = '生成预览';
- }
- }
- // 页面加载时获取字体列表
- window.addEventListener('DOMContentLoaded', loadFonts);
- // 支持回车键生成
- document.getElementById('textInput').addEventListener('keypress', function(e) {
- if (e.key === 'Enter') {
- generatePreview();
- }
- });
- </script>
- </body>
- </html>
|