File size: 1,322 Bytes
eebc40f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useMemo, useCallback } from 'react';
import { calculatePositions } from '../utils/fontUtils';

/**
 * Hook spécialisé pour la gestion du positionnement des polices
 * Séparation claire de la logique de calcul des positions
 */
export const usePositioning = (fonts, width, height, dilationFactor) => {
  // Mémoriser le calcul des positions pour éviter les recalculs inutiles
  const positions = useMemo(() => {
    if (!fonts.length || width <= 0 || height <= 0) {
      return [];
    }
    
    console.log('usePositioning: Calculating positions for', fonts.length, 'fonts');
    return calculatePositions(fonts, width, height, dilationFactor);
  }, [fonts, width, height, dilationFactor]);

  // Fonction pour recalculer les positions si nécessaire
  const recalculatePositions = useCallback(() => {
    if (!fonts.length || width <= 0 || height <= 0) {
      return [];
    }
    return calculatePositions(fonts, width, height, dilationFactor);
  }, [fonts, width, height, dilationFactor]);

  // Fonction pour obtenir la position d'une police spécifique
  const getFontPosition = useCallback((fontName) => {
    return positions.find(p => p.name === fontName);
  }, [positions]);

  return {
    positions,
    recalculatePositions,
    getFontPosition,
    hasPositions: positions.length > 0
  };
};