File size: 13,071 Bytes
6bda4a6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
import { useEffect, useRef } from 'react';
import * as d3 from 'd3';
import { calculateMappingDimensions, createGlyphTransform } from '../utils/mappingUtils.js';
import { applyColorsToGlyphGroup } from '../utils/colorUtils.js';
import { getConfig } from '../config/mapConfig.js';
import { useDebugUMAPStore } from '../store';
/**
* Hook pour gérer le rendu des glyphes avec Zustand
*/
export function useGlyphRenderer({ svgRef, enabled = true }) {
const configs = useDebugUMAPStore((state) => state.configs);
const currentConfigIndex = useDebugUMAPStore((state) => state.currentConfigIndex);
const baseGlyphSize = useDebugUMAPStore((state) => state.baseGlyphSize);
const useCategoryColors = useDebugUMAPStore((state) => state.useCategoryColors);
const darkMode = useDebugUMAPStore((state) => state.darkMode);
const showCentroids = useDebugUMAPStore((state) => state.showCentroids);
const setCurrentFonts = useDebugUMAPStore((state) => state.setCurrentFonts);
const setMappingFunctions = useDebugUMAPStore((state) => state.setMappingFunctions);
const setGlyphsLoaded = useDebugUMAPStore((state) => state.setGlyphsLoaded);
// Références pour le cleanup
const abortControllerRef = useRef(null);
const timeoutRefs = useRef([]);
console.log('useGlyphRenderer: Hook appelé avec:', {
configsLength: configs.length,
currentConfigIndex,
baseGlyphSize,
svgRef: !!svgRef.current
});
// Charger et afficher les glyphes - SEULEMENT au premier chargement
useEffect(() => {
console.log('useGlyphRenderer: useEffect déclenché, enabled:', enabled, 'configs.length:', configs.length);
// Cleanup des opérations précédentes
if (abortControllerRef.current) {
abortControllerRef.current.abort();
}
timeoutRefs.current.forEach(timeout => clearTimeout(timeout));
timeoutRefs.current = [];
if (!enabled || configs.length === 0) {
console.log('useGlyphRenderer: Pas activé ou pas de configurations, sortie');
return;
}
const config = configs[currentConfigIndex];
if (!config) {
console.log('useGlyphRenderer: Pas de config pour l\'index', currentConfigIndex);
return;
}
console.log('useGlyphRenderer: Chargement de la config:', config.filename);
// Créer un AbortController pour ce fetch
abortControllerRef.current = new AbortController();
fetch(`/debug-umap/${config.filename}`, {
signal: abortControllerRef.current.signal
})
.then(res => res.json())
.then(data => {
const svg = svgRef.current;
if (!svg) return;
// Créer ou récupérer le groupe viewport (sans nettoyer le SVG)
let viewportGroup = d3.select(svg).select('.viewport-group');
if (viewportGroup.empty()) {
viewportGroup = d3.select(svg).append('g').attr('class', 'viewport-group');
} else {
// Nettoyer seulement les glyphes et centroïdes, pas le groupe viewport
viewportGroup.selectAll('g.glyph-group').remove();
viewportGroup.selectAll('.centroid-label').remove();
}
// Calculer les dimensions de mapping
const { mapX, mapY } = calculateMappingDimensions(data.fonts);
// Stocker les données pour les centroïdes
setCurrentFonts(data.fonts);
setMappingFunctions({ mapX, mapY });
setGlyphsLoaded(false);
// Charger les glyphes par batch pour éviter ERR_INSUFFICIENT_RESOURCES
const batchSize = getConfig('glyph.batchLoading.batchSize', 40);
const fonts = data.fonts;
const loadBatch = (startIndex) => {
const endIndex = Math.min(startIndex + batchSize, fonts.length);
const batch = fonts.slice(startIndex, endIndex);
// Charger le batch actuel
const promises = batch.map(font =>
fetch(`/data/char/${font.id}_a.svg`, {
signal: abortControllerRef.current.signal
})
.then(res => res.text())
.then(svgContent => {
// Vérifier si le composant est toujours monté
if (!svgRef.current || abortControllerRef.current.signal.aborted) {
return;
}
renderGlyph(viewportGroup, svgContent, font, mapX, mapY, baseGlyphSize, useCategoryColors, darkMode);
})
.catch((err) => {
if (err.name !== 'AbortError') {
console.warn('Erreur lors du chargement du glyphe:', font.id, err);
}
})
);
// Attendre que le batch soit terminé avant de continuer
Promise.all(promises).then(() => {
// Vérifier si le composant est toujours monté
if (!svgRef.current || abortControllerRef.current.signal.aborted) {
return;
}
if (endIndex < fonts.length) {
// Charger le batch suivant après un petit délai
const delay = getConfig('glyph.batchLoading.delay', 10);
const timeout = setTimeout(() => loadBatch(endIndex), delay);
timeoutRefs.current.push(timeout);
} else {
// Tous les glyphes sont chargés, créer les centroïdes maintenant
setGlyphsLoaded(true);
if (showCentroids) {
const centroidTimeout = setTimeout(() => {
if (!abortControllerRef.current.signal.aborted) {
createCentroids(data.fonts, mapX, mapY, viewportGroup, darkMode, useCategoryColors);
}
}, 50);
timeoutRefs.current.push(centroidTimeout);
}
}
});
};
// Commencer le chargement par batch
loadBatch(0);
})
.catch(err => {
if (err.name !== 'AbortError') {
console.error('Erreur:', err);
}
});
// Cleanup function
return () => {
if (abortControllerRef.current) {
abortControllerRef.current.abort();
}
timeoutRefs.current.forEach(timeout => clearTimeout(timeout));
timeoutRefs.current = [];
};
}, [enabled, configs, currentConfigIndex]);
// Mettre à jour les couleurs des glyphes existants
useEffect(() => {
if (!svgRef.current) return;
const svg = svgRef.current;
const viewportGroup = svg.querySelector('.viewport-group');
if (!viewportGroup) return;
const glyphGroups = viewportGroup.querySelectorAll('g.glyph-group');
glyphGroups.forEach(group => {
const category = group.getAttribute('data-category');
applyColorsToGlyphGroup(group, category, useCategoryColors, darkMode);
});
// Mettre à jour les couleurs des centroïdes
const centroidLabels = viewportGroup.querySelectorAll('.centroid-label');
const strokeColors = getConfig('color.centroid.stroke', { light: '#ffffff', dark: '#000000' });
const strokeColor = darkMode ? strokeColors.dark : strokeColors.light;
const categoryColors = getConfig('color.categories', {});
const fallbackColor = getConfig('color.centroid.fallback', '#95a5a6');
const defaultColors = getConfig('color.defaults', { light: '#333333', dark: '#ffffff' });
centroidLabels.forEach(label => {
const category = label.textContent;
const fillColor = useCategoryColors
? (categoryColors[category] || fallbackColor)
: (darkMode ? defaultColors.dark : defaultColors.light);
label.setAttribute('fill', fillColor);
label.setAttribute('stroke', strokeColor);
});
}, [useCategoryColors, darkMode]);
// Gérer l'affichage/masquage des centroïdes
useEffect(() => {
if (!svgRef.current) return;
const svg = svgRef.current;
const viewportGroup = svg.querySelector('.viewport-group');
if (!viewportGroup) return;
const centroidLabels = viewportGroup.querySelectorAll('.centroid-label');
centroidLabels.forEach(label => {
label.style.display = showCentroids ? 'block' : 'none';
});
}, [showCentroids]);
// Gérer les changements de taille des glyphes
useEffect(() => {
if (!svgRef.current || !enabled) return;
const svg = svgRef.current;
const viewportGroup = svg.querySelector('.viewport-group');
if (!viewportGroup) return;
const glyphGroups = viewportGroup.querySelectorAll('g.glyph-group');
glyphGroups.forEach(group => {
const originalTransform = group.getAttribute('data-original-transform');
if (originalTransform) {
// Extraire les coordonnées x,y de la transformation originale
const match = originalTransform.match(/translate\(([^,]+),\s*([^)]+)\)/);
if (match) {
const x = parseFloat(match[1]);
const y = parseFloat(match[2]);
// Appliquer la transformation avec scaling centré
// Utiliser la formule : translate(x, y) scale(s) avec transform-origin center
const newTransform = `translate(${x}, ${y}) scale(${baseGlyphSize})`;
group.setAttribute('transform', newTransform);
}
}
});
}, [enabled, baseGlyphSize]);
// Pas besoin de retourner quoi que ce soit, tout est dans le store
return {};
}
/**
* Rend un glyphe individuel
*/
function renderGlyph(viewportGroup, svgContent, font, mapX, mapY, baseGlyphSize, useCategoryColors, darkMode) {
// Créer un groupe pour chaque glyphe
const group = document.createElementNS('http://www.w3.org/2000/svg', 'g');
const originalTransform = createGlyphTransform(
mapX(font.x),
mapY(font.y),
baseGlyphSize
);
group.setAttribute('transform', originalTransform);
group.setAttribute('data-original-transform', originalTransform);
group.setAttribute('data-category', font.family);
group.setAttribute('class', 'glyph-group');
// Parser le SVG et l'ajouter au groupe
const parser = new DOMParser();
const svgDoc = parser.parseFromString(svgContent, 'image/svg+xml');
const svgElement = svgDoc.querySelector('svg');
if (svgElement) {
// Copier le contenu du SVG
while (svgElement.firstChild) {
const child = svgElement.firstChild;
group.appendChild(child);
}
}
// Ajouter au groupe viewport
viewportGroup.node().appendChild(group);
// Appliquer les couleurs immédiatement
applyColorsToGlyphGroup(group, font.family, useCategoryColors, darkMode);
}
/**
* Créer les centroïdes des catégories
*/
function createCentroids(fonts, mapX, mapY, viewportGroup, darkMode, useCategoryColors = true) {
// Calculer les centroïdes par catégorie
const centroids = {};
const categoryCounts = {};
fonts.forEach(font => {
const category = font.family;
if (!centroids[category]) {
centroids[category] = { x: 0, y: 0, count: 0 };
categoryCounts[category] = 0;
}
centroids[category].x += font.x;
centroids[category].y += font.y;
centroids[category].count += 1;
categoryCounts[category] += 1;
});
// Calculer les moyennes
Object.keys(centroids).forEach(category => {
centroids[category].x /= centroids[category].count;
centroids[category].y /= centroids[category].count;
});
// Nettoyer les centroïdes existants
viewportGroup.selectAll('.centroid-label').remove();
// Créer les centroïdes (juste du texte avec bordure blanche)
Object.entries(centroids).forEach(([category, centroid]) => {
const x = mapX(centroid.x);
const y = mapY(centroid.y);
// Utiliser les couleurs de catégorie ou noir/blanc selon useCategoryColors
const categoryColors = getConfig('color.categories', {});
const fallbackColor = getConfig('color.centroid.fallback', '#95a5a6');
const defaultColors = getConfig('color.defaults', { light: '#333333', dark: '#ffffff' });
const color = useCategoryColors
? (categoryColors[category] || fallbackColor)
: (darkMode ? defaultColors.dark : defaultColors.light);
// Texte avec bordure adaptée au mode sombre
const strokeColors = getConfig('color.centroid.stroke', { light: '#ffffff', dark: '#000000' });
const strokeColor = darkMode ? strokeColors.dark : strokeColors.light;
const textConfig = getConfig('centroid.text', {
fontSize: 16,
fontWeight: 'bold',
fontFamily: 'Arial, Helvetica, sans-serif',
strokeWidth: 4
});
viewportGroup.append('text')
.attr('x', x)
.attr('y', y)
.attr('text-anchor', 'middle')
.attr('font-size', `${textConfig.fontSize}px`)
.attr('font-weight', textConfig.fontWeight)
.attr('font-family', textConfig.fontFamily)
.attr('fill', color)
.attr('stroke', strokeColor)
.attr('stroke-width', `${textConfig.strokeWidth}px`)
.attr('stroke-linejoin', 'round')
.attr('stroke-linecap', 'round')
.attr('paint-order', 'stroke fill')
.attr('class', 'centroid-label')
.text(category);
});
}
|