| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 |
- <!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: 1400px;
- 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;
- }
- .input-section {
- background: #f8f9fa;
- padding: 20px;
- border-radius: 8px;
- margin-bottom: 30px;
- }
- .form-row {
- display: grid;
- grid-template-columns: 1fr auto;
- gap: 15px;
- align-items: end;
- }
- .form-group {
- flex: 1;
- }
- label {
- display: block;
- margin-bottom: 8px;
- color: #555;
- font-weight: 500;
- font-size: 14px;
- }
- input[type="text"],
- input[type="number"],
- input[type="color"] {
- width: 100%;
- padding: 12px;
- border: 2px solid #e0e0e0;
- border-radius: 6px;
- font-size: 14px;
- transition: border-color 0.3s;
- }
- input[type="text"]:focus,
- input[type="number"]:focus {
- outline: none;
- border-color: #667eea;
- }
- .form-controls {
- display: grid;
- grid-template-columns: auto auto auto;
- gap: 10px;
- align-items: end;
- }
- .color-input-group {
- display: flex;
- gap: 10px;
- align-items: center;
- }
- .color-input-group input[type="color"] {
- width: 50px;
- height: 46px;
- cursor: pointer;
- padding: 2px;
- }
- .color-input-group input[type="text"] {
- flex: 1;
- }
- 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;
- white-space: nowrap;
- }
- 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;
- }
- .checkbox-group {
- display: flex;
- align-items: center;
- gap: 8px;
- }
- .checkbox-group input[type="checkbox"] {
- width: 18px;
- height: 18px;
- cursor: pointer;
- }
- .preview-grid {
- display: grid;
- grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
- gap: 20px;
- margin-top: 20px;
- }
- .font-preview-card {
- background: white;
- border: 2px solid #e0e0e0;
- border-radius: 8px;
- padding: 15px;
- transition: all 0.3s;
- }
- .font-preview-card:hover {
- border-color: #667eea;
- box-shadow: 0 4px 12px rgba(102, 126, 234, 0.2);
- }
- .font-name {
- font-size: 14px;
- font-weight: 600;
- color: #333;
- margin-bottom: 10px;
- padding-bottom: 8px;
- border-bottom: 1px solid #e0e0e0;
- }
- .font-preview-image {
- width: 100%;
- height: auto;
- border-radius: 4px;
- background:
- repeating-linear-gradient(45deg, #f0f0f0, #f0f0f0 10px, #ffffff 10px, #ffffff 20px);
- padding: 10px;
- }
- .loading {
- text-align: center;
- padding: 60px 20px;
- color: #667eea;
- font-size: 16px;
- }
- .loading-spinner {
- border: 4px solid #f3f3f3;
- border-top: 4px solid #667eea;
- border-radius: 50%;
- width: 40px;
- height: 40px;
- animation: spin 1s linear infinite;
- margin: 0 auto 20px;
- }
- @keyframes spin {
- 0% { transform: rotate(0deg); }
- 100% { transform: rotate(360deg); }
- }
- .error {
- background: #fee;
- color: #c33;
- padding: 15px;
- border-radius: 6px;
- margin-top: 15px;
- border-left: 4px solid #c33;
- }
- .stats {
- text-align: center;
- color: #666;
- margin-bottom: 20px;
- font-size: 14px;
- }
- .empty-state {
- text-align: center;
- padding: 60px 20px;
- color: #999;
- }
- .empty-state-icon {
- font-size: 64px;
- margin-bottom: 20px;
- }
- @media (max-width: 768px) {
- .form-row {
- grid-template-columns: 1fr;
- }
-
- .form-controls {
- grid-template-columns: 1fr;
- }
- .preview-grid {
- grid-template-columns: 1fr;
- }
- .container {
- padding: 20px;
- }
- }
- </style>
- </head>
- <body>
- <div class="container">
- <h1>🎨 字体对比预览</h1>
-
- <div class="input-section">
- <div class="form-row">
- <div class="form-group">
- <label for="textInput">输入文字</label>
- <input type="text" id="textInput" placeholder="请输入要预览的文字" value="字体预览">
- </div>
- <div class="form-controls">
- <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="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>
- <button id="generateBtn" onclick="generateAllPreviews()">生成所有预览</button>
- </div>
- </div>
- <div style="margin-top: 15px;">
- <div class="checkbox-group">
- <input type="checkbox" id="handwritingEffect" checked>
- <label for="handwritingEffect" style="margin: 0;">启用手写效果(轻微位置扰动)</label>
- </div>
- </div>
- </div>
- <div id="stats" class="stats" style="display: none;"></div>
- <div id="previewContainer">
- <div class="empty-state">
- <div class="empty-state-icon">📝</div>
- <div>请输入文字并点击"生成所有预览"查看所有字体效果</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');
- // 颜色输入同步
- 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 generateAllPreviews() {
- const textInput = document.getElementById('textInput');
- const fontSize = parseInt(document.getElementById('fontSize').value);
- const textColor = hexToRgb(document.getElementById('textColor').value);
- const handwritingEffect = document.getElementById('handwritingEffect').checked;
-
- if (!textInput.value.trim()) {
- alert('请输入要预览的文字');
- return;
- }
- const generateBtn = document.getElementById('generateBtn');
- const previewContainer = document.getElementById('previewContainer');
- const statsDiv = document.getElementById('stats');
-
- generateBtn.disabled = true;
- generateBtn.textContent = '生成中...';
- previewContainer.innerHTML = `
- <div class="loading">
- <div class="loading-spinner"></div>
- 正在生成所有字体预览,请稍候...
- </div>
- `;
- statsDiv.style.display = 'none';
- try {
- const response = await fetch(`${API_BASE}/batch_generate_preview`, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- },
- body: JSON.stringify({
- text: textInput.value,
- font_size: fontSize,
- padding: 20,
- background_color: [255, 255, 255, 0], // 透明背景
- text_color: textColor,
- use_handwriting_effect: handwritingEffect
- })
- });
- const data = await response.json();
- if (!data.success) {
- throw new Error(data.error || '生成失败');
- }
- if (!data.previews || data.previews.length === 0) {
- previewContainer.innerHTML = `
- <div class="empty-state">
- <div class="empty-state-icon">😕</div>
- <div>没有生成任何预览,请检查字体文件是否存在</div>
- </div>
- `;
- return;
- }
- // 显示统计信息
- statsDiv.textContent = `共生成 ${data.total} 个字体预览`;
- statsDiv.style.display = 'block';
- // 生成预览卡片
- const grid = document.createElement('div');
- grid.className = 'preview-grid';
- data.previews.forEach(preview => {
- const card = document.createElement('div');
- card.className = 'font-preview-card';
-
- const fontName = document.createElement('div');
- fontName.className = 'font-name';
- fontName.textContent = preview.font_name;
-
- const previewImg = document.createElement('img');
- previewImg.className = 'font-preview-image';
- // 确保image_base64格式正确(支持带前缀或不带前缀)
- let imageSrc = preview.image_base64;
- if (imageSrc && !imageSrc.startsWith('data:image/') && !imageSrc.startsWith('http')) {
- // 如果是纯base64字符串,添加前缀
- imageSrc = 'data:image/png;base64,' + imageSrc;
- }
- previewImg.src = imageSrc;
- previewImg.alt = preview.font_name;
- previewImg.title = '点击右键保存图片';
- // 添加错误处理
- previewImg.onerror = function() {
- console.error('图片加载失败:', preview.font_name, imageSrc.substring(0, 50) + '...');
- this.alt = '图片加载失败';
- this.style.border = '2px solid red';
- };
-
- card.appendChild(fontName);
- card.appendChild(previewImg);
- grid.appendChild(card);
- });
- previewContainer.innerHTML = '';
- previewContainer.appendChild(grid);
- } catch (error) {
- console.error('生成预览失败:', error);
- previewContainer.innerHTML = `
- <div class="error">
- 生成失败: ${error.message}
- </div>
- `;
- } finally {
- generateBtn.disabled = false;
- generateBtn.textContent = '生成所有预览';
- }
- }
- // 支持回车键生成
- document.getElementById('textInput').addEventListener('keypress', function(e) {
- if (e.key === 'Enter') {
- generateAllPreviews();
- }
- });
- </script>
- </body>
- </html>
|