fontutils.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * Copyright 2013 Mozilla Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import { bytesToString, stringToBytes } from "../../src/shared/util.js";
  17. function decodeFontData(base64) {
  18. const str = atob(base64);
  19. return stringToBytes(str);
  20. }
  21. function encodeFontData(data) {
  22. const str = bytesToString(data);
  23. return btoa(str);
  24. }
  25. async function ttx(data) {
  26. const response = await fetch("/ttx", {
  27. method: "POST",
  28. body: encodeFontData(data),
  29. });
  30. if (!response.ok) {
  31. throw new Error(response.statusText);
  32. }
  33. return response.text();
  34. }
  35. function verifyTtxOutput(output) {
  36. const m = /^<error>(.*?)<\/error>/.exec(output);
  37. if (m) {
  38. throw m[1];
  39. }
  40. }
  41. export { decodeFontData, encodeFontData, ttx, verifyTtxOutput };