androidfonts.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright (C) 2004-2025 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. /* Android font functions */
  23. static fz_font *load_noto(fz_context *ctx, const char *a, const char *b, const char *c, int idx)
  24. {
  25. char buf[500];
  26. fz_font *font = NULL;
  27. fz_try(ctx)
  28. {
  29. fz_snprintf(buf, sizeof buf, "/system/fonts/%s%s%s.ttf", a, b, c);
  30. if (!fz_file_exists(ctx, buf))
  31. fz_snprintf(buf, sizeof buf, "/system/fonts/%s%s%s.otf", a, b, c);
  32. if (!fz_file_exists(ctx, buf))
  33. fz_snprintf(buf, sizeof buf, "/system/fonts/%s%s%s.ttc", a, b, c);
  34. if (fz_file_exists(ctx, buf))
  35. font = fz_new_font_from_file(ctx, NULL, buf, idx, 0);
  36. }
  37. fz_catch(ctx)
  38. return NULL;
  39. return font;
  40. }
  41. static fz_font *load_noto_cjk(fz_context *ctx, int lang)
  42. {
  43. fz_font *font = load_noto(ctx, "NotoSerif", "CJK", "-Regular", lang);
  44. if (!font) font = load_noto(ctx, "NotoSans", "CJK", "-Regular", lang);
  45. if (!font) font = load_noto(ctx, "DroidSans", "Fallback", "", 0);
  46. return font;
  47. }
  48. static fz_font *load_noto_arabic(fz_context *ctx)
  49. {
  50. fz_font *font = load_noto(ctx, "Noto", "Naskh", "-Regular", 0);
  51. if (!font) font = load_noto(ctx, "Noto", "NaskhArabic", "-Regular", 0);
  52. if (!font) font = load_noto(ctx, "Droid", "Naskh", "-Regular", 0);
  53. if (!font) font = load_noto(ctx, "NotoSerif", "Arabic", "-Regular", 0);
  54. if (!font) font = load_noto(ctx, "NotoSans", "Arabic", "-Regular", 0);
  55. if (!font) font = load_noto(ctx, "DroidSans", "Arabic", "-Regular", 0);
  56. return font;
  57. }
  58. static fz_font *load_noto_try(fz_context *ctx, const char *stem)
  59. {
  60. fz_font *font = load_noto(ctx, "NotoSerif", stem, "-Regular", 0);
  61. if (!font) font = load_noto(ctx, "NotoSerif", stem, "-VF", 0);
  62. if (!font) font = load_noto(ctx, "NotoSans", stem, "-Regular", 0);
  63. if (!font) font = load_noto(ctx, "NotoSans", stem, "-VF", 0);
  64. if (!font) font = load_noto(ctx, "DroidSans", stem, "-Regular", 0);
  65. return font;
  66. }
  67. enum { JP, KR, SC, TC };
  68. fz_font *load_droid_fallback_font(fz_context *ctx, int script, int language, int serif, int bold, int italic)
  69. {
  70. const char *stem = NULL;
  71. switch (script)
  72. {
  73. case UCDN_SCRIPT_HANGUL: return load_noto_cjk(ctx, KR);
  74. case UCDN_SCRIPT_HIRAGANA: return load_noto_cjk(ctx, JP);
  75. case UCDN_SCRIPT_KATAKANA: return load_noto_cjk(ctx, JP);
  76. case UCDN_SCRIPT_BOPOMOFO: return load_noto_cjk(ctx, TC);
  77. case UCDN_SCRIPT_HAN:
  78. switch (language)
  79. {
  80. case FZ_LANG_ja: return load_noto_cjk(ctx, JP);
  81. case FZ_LANG_ko: return load_noto_cjk(ctx, KR);
  82. case FZ_LANG_zh_Hans: return load_noto_cjk(ctx, SC);
  83. default:
  84. case FZ_LANG_zh_Hant: return load_noto_cjk(ctx, TC);
  85. }
  86. case UCDN_SCRIPT_ARABIC:
  87. return load_noto_arabic(ctx);
  88. default:
  89. stem = fz_lookup_noto_stem_from_script(ctx, script, language);
  90. if (stem)
  91. return load_noto_try(ctx, stem);
  92. }
  93. return NULL;
  94. }
  95. fz_font *load_droid_cjk_font(fz_context *ctx, const char *name, int ros, int serif)
  96. {
  97. switch (ros)
  98. {
  99. case FZ_ADOBE_CNS: return load_noto_cjk(ctx, TC);
  100. case FZ_ADOBE_GB: return load_noto_cjk(ctx, SC);
  101. case FZ_ADOBE_JAPAN: return load_noto_cjk(ctx, JP);
  102. case FZ_ADOBE_KOREA: return load_noto_cjk(ctx, KR);
  103. }
  104. return NULL;
  105. }
  106. fz_font *load_droid_font(fz_context *ctx, const char *name, int bold, int italic, int needs_exact_metrics)
  107. {
  108. return NULL;
  109. }