subfontname.c 762 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. /*
  5. * Default version: convert to file name
  6. */
  7. char*
  8. subfontname(char *cfname, char *fname, int maxdepth)
  9. {
  10. char *t, *u, tmp1[64], tmp2[64];
  11. int i;
  12. if(strcmp(cfname, "*default*") == 0)
  13. return strdup(cfname);
  14. t = cfname;
  15. if(t[0] != '/'){
  16. snprint(tmp2, sizeof tmp2, "%s", fname);
  17. u = utfrrune(tmp2, '/');
  18. if(u)
  19. u[0] = 0;
  20. else
  21. strcpy(tmp2, ".");
  22. snprint(tmp1, sizeof tmp1, "%s/%s", tmp2, t);
  23. t = tmp1;
  24. }
  25. if(maxdepth > 8)
  26. maxdepth = 8;
  27. for(i=log2[maxdepth]; i>=0; i--){
  28. /* try i-bit grey */
  29. snprint(tmp2, sizeof tmp2, "%s.%d", t, i);
  30. if(access(tmp2, AREAD) == 0)
  31. return strdup(tmp2);
  32. }
  33. /* try default */
  34. if(access(t, AREAD) == 0)
  35. return strdup(t);
  36. return nil;
  37. }