text.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright (C) 2014-2015 H3XL, Inc
  2. // This program is free software: you can redistribute it and/or modify
  3. // it under the terms of the GNU General Public License as published by
  4. // the Free Software Foundation, either version 3 of the License, or
  5. // (at your option) any later version.
  6. // This program is distributed in the hope that it will be useful,
  7. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. // GNU General Public License for more details.
  10. // You should have received a copy of the GNU General Public License
  11. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. // file overview
  13. // Code related to loading and rendering text for BlocksCAD
  14. 'use strict';
  15. // create Blockscad namespace
  16. var Blockscad = Blockscad || {};
  17. // Hold pre-loaded font object created by opentype.js
  18. Blockscad.fonts = {};
  19. // List and location of fonts to load into BlocksCAD
  20. Blockscad.fontList = ['/fonts/Roboto/Roboto-Bold.ttf',
  21. '/fonts/liberation/LiberationSerif-Bold.ttf',
  22. '/fonts/nimbus/nimbus-sans-l_bold.ttf',
  23. '/fonts/AverageMono/AverageMonoSimp.ttf',
  24. '/fonts/Open_Sans/OpenSans-ExtraBold.ttf',
  25. '/fonts/stardos-stencil/StardosStencil-Bold.ttf',
  26. '/fonts/Chewy/Chewy.ttf',
  27. '/fonts/bangers/Bangers.ttf'];
  28. // display names for fonts, used in font block (also used to key fonts object)
  29. Blockscad.fontName = ['Roboto',
  30. 'Liberation Serif',
  31. 'Nimbus Sans',
  32. 'Average Mono',
  33. 'Open Sans',
  34. 'Stardos Stencil',
  35. 'Chewy',
  36. 'Bangers'];
  37. // pre-load all fonts in Blockscad.fontList
  38. // calls the loadFont() function because of asynchronous font load
  39. Blockscad.loadFonts = function() {
  40. for (var i = 0; i < Blockscad.fontList.length; i++) {
  41. Blockscad.loadFont(i);
  42. }
  43. };
  44. Blockscad.loadFont = function(index) {
  45. opentype.load(Blockscad.fontList[index], function(err, font) {
  46. if (err) {
  47. console.log('Could not load font: ', font + ":" + err);
  48. } else {
  49. Blockscad.fonts[Blockscad.fontName[index]] = font;
  50. return font; // if I do this, can I use this in synchronous code?
  51. }
  52. });
  53. };
  54. Blockscad.loadFontThenRender = function(i,code) {
  55. try {
  56. var name = Blockscad.fontName[Blockscad.loadTheseFonts[i]];
  57. var url = Blockscad.fontList[Blockscad.loadTheseFonts[i]];
  58. var request = new XMLHttpRequest();
  59. request.open('get', url, true);
  60. request.responseType = 'arraybuffer';
  61. request.onload = function() {
  62. if (request.status !== 200) {
  63. throw new Error('failed to load font in loadRawFont:' + url + request.statusText);
  64. }
  65. Blockscad.fonts[Blockscad.fontName[Blockscad.loadTheseFonts[i]]] = request.response; // save the loaded fonts
  66. Blockscad.numloaded++;
  67. if (Blockscad.numloaded == Blockscad.loadTheseFonts.length) Blockscad.renderCode(code);
  68. };
  69. request.send();
  70. // opentype.load(Blockscad.fontList[Blockscad.loadTheseFonts[i]], function(err, font) {
  71. // if (err) {
  72. // console.log('Could not load font: ', font + ":" + err);
  73. // // I'm not going to be rendering because of a lack of font.
  74. // // need an error message in the render pane and set the render button active and "render"y.
  75. // $( '#error-message' ).html("Error: Failed to load font: " + name);
  76. // $( '#error-message' ).addClass("has-error");
  77. // $('#renderButton').html('Render');
  78. // $('#renderButton').prop('disabled', false);
  79. // } else {
  80. // Blockscad.fonts[Blockscad.fontName[Blockscad.loadTheseFonts[i]]] = font; // save the loaded fonts
  81. // Blockscad.numloaded++;
  82. // if (Blockscad.numloaded == Blockscad.loadTheseFonts.length) Blockscad.renderCode(code);
  83. // }
  84. // });
  85. }
  86. catch(err) {
  87. console.log("network error loading font in loadFontThenRender with: ", err);
  88. }
  89. };
  90. Blockscad.whichFonts = function(code) {
  91. var loadThisIndex = [];
  92. for (var i = 0; i < Blockscad.fontList.length; i++) {
  93. if (code.indexOf(Blockscad.fontName[i]) > -1)
  94. if (!Blockscad.fonts[Blockscad.fontName[i]])
  95. loadThisIndex.push(i);
  96. }
  97. return loadThisIndex;
  98. };