├── .gitignore ├── LICENSE ├── README.md ├── bin └── convert.js ├── convertall.sh ├── images ├── meme1.jpg └── preview.png ├── lib ├── bitmap.js ├── font.js ├── index.js └── text.js ├── package.json └── test ├── fib.js ├── fib.stl ├── hello.js └── hello.stl /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # Compiled binary addons (http://nodejs.org/api/addons.html) 20 | build/Release 21 | 22 | # Dependency directory 23 | # Deployed apps should consider commenting this line out: 24 | # see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git 25 | node_modules 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Code2STL 2 | ====== 3 | 4 | Lets be honest, programming has always been quite boring, staring at plain old 2D pages of code ..., now with `code2stl`, a **Revolution** is coming, **3D** code like you’ve never seen it before ! 5 | 6 | ![Image](https://raw.github.com/SamyPesse/code2stl/master/images/preview.png) 7 | 8 | 3D code is **scientifically proven** to **change** your perspective on code, fixing ugly portions of code such as callback hells, code duplication and many other horrors that have riddled the 2d world of the past. 9 | 10 | Fear no more, code is now beautiful thanks to our patent pending, world class 3d rendering engine ! 11 | 12 | 3D code is the salvation programmers have been waiting for, wait no more and join the future by using **code2stl**, and awesome **brogramming** !!! 13 | 14 | This is a highly profitable venture, many high profile companies are trying to own this market, it's not a coincidence if GitHub [launched their STL viewer](https://github.com/blog/1465-stl-file-viewing). 15 | 16 | ![Image](https://raw.github.com/SamyPesse/code2stl/master/images/meme1.jpg) 17 | 18 | ## Examples: 19 | 20 | * [HelloWorld](./test/hello.stl) 21 | * [Fibonacci](./test/fib.stl) 22 | 23 | ## How to use it? 24 | 25 | Install it via **NPM**: 26 | 27 | ``` 28 | $ npm install code2stl -g 29 | ``` 30 | 31 | And convert your source code using: 32 | 33 | ``` 34 | $ code2stl --input=myfile.js 35 | ``` 36 | -------------------------------------------------------------------------------- /bin/convert.js: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env node 2 | 3 | var path = require("path"); 4 | 5 | var program = require('commander'); 6 | var pkg = require('../package.json'); 7 | 8 | var generate = require("../lib"); 9 | 10 | program 11 | .version(pkg.version) 12 | .option('-i, --input [file]', 'Input file') 13 | .option('-o, --output [file]', 'Output file') 14 | .parse(process.argv); 15 | 16 | if (!program.input) { 17 | console.log("Need an input!"); 18 | process.exit(-1); 19 | } 20 | 21 | program.output = program.output || program.input.replace(path.extname(program.input), ".stl"); 22 | 23 | generate.file(program.input, program.output) 24 | .then(function() { 25 | console.log("Done!"); 26 | }, function(err) { 27 | console.log(err.stack || err); 28 | }); 29 | -------------------------------------------------------------------------------- /convertall.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | for f in ./test/*; do 4 | case "$f" in 5 | *.stl ) 6 | # it's a stl 7 | ;; 8 | *) 9 | echo "Processing $f file.."; 10 | ./bin/convert.js --input=$f 11 | ;; 12 | esac 13 | done -------------------------------------------------------------------------------- /images/meme1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/code2stl/55895b1bbf887b13dd305703aa3e92685126cde9/images/meme1.jpg -------------------------------------------------------------------------------- /images/preview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/code2stl/55895b1bbf887b13dd305703aa3e92685126cde9/images/preview.png -------------------------------------------------------------------------------- /lib/bitmap.js: -------------------------------------------------------------------------------- 1 | var bitmapBoxes = require("bitmap-to-boxes"); 2 | var _ = require("lodash"); 3 | 4 | var faceNormals = require('face-normals'); 5 | var ndPack = require("ndarray-pack"); 6 | 7 | var rectangleTo3D = function(rectangle, w, h, ew, eh, z) { 8 | var facets = []; 9 | 10 | // Extract positions 11 | var ex = rectangle[0][0]; 12 | var ey = rectangle[0][1]; 13 | 14 | var sx = rectangle[1][0]; 15 | var sy = rectangle[1][1]; 16 | 17 | // Normalize position: 18 | ex = (ex*(w+ew)); 19 | ey = -(ey*(h+eh)); 20 | 21 | sx = (sx*(w+ew) - ew); 22 | sy = -(sy*(h+eh) - eh); 23 | 24 | facets = [ 25 | // Face on (ex, ey) -> (sx, ey) 26 | [ 27 | [ex, ey, 0], 28 | [sx, ey, z], 29 | [ex, ey, z] 30 | ], 31 | [ 32 | [ex, ey, 0], 33 | [sx, ey, 0], 34 | [sx, ey, z] 35 | ], 36 | 37 | // Face (ex, ey) -> (ex, sy) 38 | [ 39 | [ex, ey, 0], 40 | [ex, sy, z], 41 | [ex, ey, z], 42 | ], 43 | [ 44 | [ex, ey, 0], 45 | [ex, sy, 0], 46 | [ex, sy, z] 47 | ], 48 | 49 | // Face (sx, ey) -> (sx, sy) 50 | [ 51 | [sx, ey, 0], 52 | [sx, ey, z], 53 | [sx, sy, z], 54 | ], 55 | [ 56 | [sx, ey, 0], 57 | [sx, sy, z], 58 | [sx, sy, 0], 59 | ], 60 | 61 | // Face (ex, sy) -> (sx, sy) 62 | [ 63 | [ex, sy, 0], 64 | [sx, sy, z], 65 | [ex, sy, z] 66 | ], 67 | [ 68 | [ex, sy, 0], 69 | [sx, sy, 0], 70 | [sx, sy, z] 71 | ], 72 | 73 | // Top face 74 | [ 75 | [ex, ey, z], 76 | [ex, sy, z], 77 | [sx, sy, z], 78 | 79 | ], 80 | [ 81 | [ex, ey, z], 82 | [sx, sy, z], 83 | [sx, ey, z], 84 | 85 | ], 86 | 87 | // Bottom face 88 | [ 89 | [ex, ey, 0], 90 | [sx, sy, 0], 91 | [ex, sy, 0], 92 | ], 93 | [ 94 | [ex, ey, 0], 95 | [sx, ey, 0], 96 | [sx, sy, 0], 97 | ] 98 | ] 99 | 100 | // Add normals 101 | facets = _.map(facets, function(verts) { 102 | var _normal = faceNormals(new Float32Array(_.flatten(verts))); 103 | _normal = _.map(_normal, _.identity).slice(0, 3); 104 | return { 105 | normal: _normal, 106 | verts: verts 107 | } 108 | }); 109 | 110 | return facets 111 | }; 112 | 113 | 114 | var bitMapToStl = function(image, options) { 115 | options = _.defaults(options || {}, { 116 | z: 10, 117 | w: 10, 118 | h: 10, 119 | 120 | ex: 0, 121 | ey: 0 122 | }); 123 | 124 | // Prepare image 125 | image = ndPack(image) 126 | 127 | // Convert to rectangles 128 | var boxes = bitmapBoxes(image); 129 | 130 | // Set in 3D 131 | boxes = _.map(boxes, function(rectangle) { 132 | return rectangleTo3D(rectangle, options.w, options.h, options.ex, options.ey, options.z); 133 | }); 134 | boxes = _.flatten(boxes); 135 | 136 | return boxes; 137 | }; 138 | 139 | // Merge bitmaps 140 | var mergeBitmaps = function(bitmaps, dx, dy) { 141 | if (!bitmaps.length) return null; 142 | 143 | var nBitmap = []; 144 | 145 | //console.log(bitmaps[0], dx, dy) 146 | var h = bitmaps[0].length; 147 | var w = bitmaps[0][0].length; 148 | 149 | 150 | var bigH = (h+dy)*Math.max(1, dy*bitmaps.length); 151 | var bigW = (w+dx)*Math.max(1, dx*bitmaps.length); 152 | 153 | nBitmap = new Array(bigH) 154 | for (var i =0; i (bitmap.length-5)) return content; 188 | return _.flatten([1, 0, 0, 0, content, 0, 0, 0, 1]); 189 | }); 190 | 191 | return bitmap; 192 | }; 193 | 194 | 195 | module.exports = { 196 | border: addBorder, 197 | toStl: bitMapToStl, 198 | merge: mergeBitmaps 199 | }; 200 | -------------------------------------------------------------------------------- /lib/font.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | w: 5, 3 | h: 8, 4 | content:[ 5 | [0x00 , 0x00 , 0x00 , 0x00 , 0x00], // 32 = 6 | [0x00 , 0x06 , 0x5F , 0x06 , 0x00], // 33 = ! 7 | [0x07 , 0x03 , 0x00 , 0x07 , 0x03], // 34 = " 8 | [0x24 , 0x7E , 0x24 , 0x7E , 0x24], // 35 = # 9 | [0x24 , 0x2B , 0x6A , 0x12 , 0x00], // 36 = $ 10 | [0x63 , 0x13 , 0x08 , 0x64 , 0x63], // 37 = % 11 | [0x36 , 0x49 , 0x56 , 0x20 , 0x50], // 38 = & 12 | [0x00 , 0x07 , 0x03 , 0x00 , 0x00], // 39 = ' 13 | [0x00 , 0x3E , 0x41 , 0x00 , 0x00], // 40 = ( 14 | [0x00 , 0x41 , 0x3E , 0x00 , 0x00], // 41 = ) 15 | [0x08 , 0x3E , 0x1C , 0x3E , 0x08], // 42 = * 16 | [0x08 , 0x08 , 0x3E , 0x08 , 0x08], // 43 = + 17 | [0x00 , 0xE0 , 0x60 , 0x00 , 0x00], // 44 = , 18 | [0x08 , 0x08 , 0x08 , 0x08 , 0x08], // 45 = - 19 | [0x00 , 0x60 , 0x60 , 0x00 , 0x00], // 46 = . 20 | [0x20 , 0x10 , 0x08 , 0x04 , 0x02], // 47 = / 21 | [0x3E , 0x51 , 0x49 , 0x45 , 0x3E], // 48 = 0 22 | [0x00 , 0x42 , 0x7F , 0x40 , 0x00], // 49 = 1 23 | [0x62 , 0x51 , 0x49 , 0x49 , 0x46], // 50 = 2 24 | [0x22 , 0x49 , 0x49 , 0x49 , 0x36], // 51 = 3 25 | [0x18 , 0x14 , 0x12 , 0x7F , 0x10], // 52 = 4 26 | [0x2F , 0x49 , 0x49 , 0x49 , 0x31], // 53 = 5 27 | [0x3C , 0x4A , 0x49 , 0x49 , 0x30], // 54 = 6 28 | [0x01 , 0x71 , 0x09 , 0x05 , 0x03], // 55 = 7 29 | [0x36 , 0x49 , 0x49 , 0x49 , 0x36], // 56 = 8 30 | [0x06 , 0x49 , 0x49 , 0x29 , 0x1E], // 57 = 9 31 | [0x00 , 0x6C , 0x6C , 0x00 , 0x00], // 58 = : 32 | [0x00 , 0xEC , 0x6C , 0x00 , 0x00], // 59 = ; 33 | [0x08 , 0x14 , 0x22 , 0x41 , 0x00], // 60 = < 34 | [0x24 , 0x24 , 0x24 , 0x24 , 0x24], // 61 = = 35 | [0x00 , 0x41 , 0x22 , 0x14 , 0x08], // 62 = > 36 | [0x02 , 0x01 , 0x59 , 0x09 , 0x06], // 63 = ? 37 | [0x3E , 0x41 , 0x5D , 0x55 , 0x1E], // 64 = @ 38 | [0x7E , 0x09 , 0x09 , 0x09 , 0x7E], // 65 = A 39 | [0x7F , 0x49 , 0x49 , 0x49 , 0x36], // 66 = B 40 | [0x3E , 0x41 , 0x41 , 0x41 , 0x22], // 67 = C 41 | [0x7F , 0x41 , 0x41 , 0x41 , 0x3E], // 68 = D 42 | [0x7F , 0x49 , 0x49 , 0x49 , 0x41], // 69 = E 43 | [0x7F , 0x09 , 0x09 , 0x09 , 0x01], // 70 = F 44 | [0x3E , 0x41 , 0x49 , 0x49 , 0x7A], // 71 = G 45 | [0x7F , 0x08 , 0x08 , 0x08 , 0x7F], // 72 = H 46 | [0x00 , 0x41 , 0x7F , 0x41 , 0x00], // 73 = I 47 | [0x30 , 0x40 , 0x40 , 0x40 , 0x3F], // 74 = J 48 | [0x7F , 0x08 , 0x14 , 0x22 , 0x41], // 75 = K 49 | [0x7F , 0x40 , 0x40 , 0x40 , 0x40], // 76 = L 50 | [0x7F , 0x02 , 0x04 , 0x02 , 0x7F], // 77 = M 51 | [0x7F , 0x02 , 0x04 , 0x08 , 0x7F], // 78 = N 52 | [0x3E , 0x41 , 0x41 , 0x41 , 0x3E], // 79 = O 53 | [0x7F , 0x09 , 0x09 , 0x09 , 0x06], // 80 = P 54 | [0x3E , 0x41 , 0x51 , 0x21 , 0x5E], // 81 = Q 55 | [0x7F , 0x09 , 0x09 , 0x19 , 0x66], // 82 = R 56 | [0x26 , 0x49 , 0x49 , 0x49 , 0x32], // 83 = S 57 | [0x01 , 0x01 , 0x7F , 0x01 , 0x01], // 84 = T 58 | [0x3F , 0x40 , 0x40 , 0x40 , 0x3F], // 85 = U 59 | [0x1F , 0x20 , 0x40 , 0x20 , 0x1F], // 86 = V 60 | [0x3F , 0x40 , 0x3C , 0x40 , 0x3F], // 87 = W 61 | [0x63 , 0x14 , 0x08 , 0x14 , 0x63], // 88 = X 62 | [0x07 , 0x08 , 0x70 , 0x08 , 0x07], // 89 = Y 63 | [0x71 , 0x49 , 0x45 , 0x43 , 0x00], // 90 = Z 64 | [0x00 , 0x7F , 0x41 , 0x41 , 0x00], // 91 = [ 65 | [0x02 , 0x04 , 0x08 , 0x10 , 0x20], // 92 = 66 | [0x00 , 0x41 , 0x41 , 0x7F , 0x00], // 93 = ] 67 | [0x04 , 0x02 , 0x01 , 0x02 , 0x04], // 94 = ^ 68 | [0x80 , 0x80 , 0x80 , 0x80 , 0x80], // 95 = _ 69 | [0x00 , 0x03 , 0x07 , 0x00 , 0x00], // 96 = ` 70 | [0x20 , 0x54 , 0x54 , 0x54 , 0x78], // 97 = a 71 | [0x7F , 0x44 , 0x44 , 0x44 , 0x38], // 98 = b 72 | [0x38 , 0x44 , 0x44 , 0x44 , 0x28], // 99 = c 73 | [0x38 , 0x44 , 0x44 , 0x44 , 0x7F], // 100 = d 74 | [0x38 , 0x54 , 0x54 , 0x54 , 0x18], // 101 = e 75 | [0x08 , 0x7E , 0x09 , 0x09 , 0x00], // 102 = f 76 | [0x18 , 0xA4 , 0xA4 , 0xA4 , 0x7C], // 103 = g 77 | [0x7F , 0x04 , 0x04 , 0x78 , 0x00], // 104 = h 78 | [0x00 , 0x00 , 0x7D , 0x00 , 0x00], // 105 = i 79 | [0x40 , 0x80 , 0x84 , 0x7D , 0x00], // 106 = j 80 | [0x7F , 0x10 , 0x28 , 0x44 , 0x00], // 107 = k 81 | [0x00 , 0x00 , 0x7F , 0x40 , 0x00], // 108 = l 82 | [0x7C , 0x04 , 0x18 , 0x04 , 0x78], // 109 = m 83 | [0x7C , 0x04 , 0x04 , 0x78 , 0x00], // 110 = n 84 | [0x38 , 0x44 , 0x44 , 0x44 , 0x38], // 111 = o 85 | [0xFC , 0x44 , 0x44 , 0x44 , 0x38], // 112 = p 86 | [0x38 , 0x44 , 0x44 , 0x44 , 0xFC], // 113 = q 87 | [0x44 , 0x78 , 0x44 , 0x04 , 0x08], // 114 = r 88 | [0x08 , 0x54 , 0x54 , 0x54 , 0x20], // 115 = s 89 | [0x04 , 0x3E , 0x44 , 0x24 , 0x00], // 116 = t 90 | [0x3C , 0x40 , 0x20 , 0x7C , 0x00], // 117 = u 91 | [0x1C , 0x20 , 0x40 , 0x20 , 0x1C], // 118 = v 92 | [0x3C , 0x60 , 0x30 , 0x60 , 0x3C], // 119 = w 93 | [0x6C , 0x10 , 0x10 , 0x6C , 0x00], // 120 = x 94 | [0x9C , 0xA0 , 0x60 , 0x3C , 0x00], // 121 = y 95 | [0x64 , 0x54 , 0x54 , 0x4C , 0x00], // 122 = z 96 | [0x08 , 0x3E , 0x41 , 0x41 , 0x00], // 123 = { 97 | [0x00 , 0x00 , 0x7F , 0x00 , 0x00], // 124 = | 98 | [0x00 , 0x41 , 0x41 , 0x3E , 0x08], // 125 = } 99 | [0x02 , 0x01 , 0x02 , 0x01 , 0x00] // 126 = ~ 100 | ] 101 | }; -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | var _ = require("lodash"); 2 | var fs = require("fs"); 3 | var path = require("path"); 4 | 5 | var Q = require("q"); 6 | var stl = require('stl'); 7 | 8 | var bitmap = require("./bitmap"); 9 | var textToBitmap = require("./text").textToBitmap; 10 | 11 | 12 | var generateFile = function(input, output) { 13 | console.log("Generating", output); 14 | return Q.nfcall(fs.readFile, input) 15 | 16 | // Generate lines number 17 | .then(function(sourceCode) { 18 | sourceCode = sourceCode.toString(); 19 | 20 | return _.chain(sourceCode.split("\n")) 21 | .map(function(line, i) { 22 | return i+" | "+line 23 | }) 24 | .join("\n") 25 | .value(); 26 | }) 27 | 28 | .then(function(sourceCode) { 29 | var image = textToBitmap(sourceCode); 30 | 31 | image = bitmap.border(image); 32 | var boxes = bitmap.toStl(image); 33 | 34 | return Q.nfcall(fs.writeFile, output, stl.fromObject({ 35 | description: "test", 36 | facets: boxes 37 | }, true)); 38 | }); 39 | }; 40 | 41 | module.exports = { 42 | file: generateFile 43 | }; -------------------------------------------------------------------------------- /lib/text.js: -------------------------------------------------------------------------------- 1 | var _ = require("lodash"); 2 | 3 | var font = require("./font"); 4 | var bitmapUtils = require("./bitmap"); 5 | 6 | // Convert a char to a bitmap 7 | var charToBitmap = function(c, z) { 8 | z = z || 1; 9 | 10 | var code = c.charCodeAt(0); 11 | var data = font.content[code - 32]; 12 | 13 | var bitmap = [], line = [], s = 0; 14 | 15 | for (var y = 0; y < font.h; y++) { 16 | line = []; 17 | for (var x = 0; x < font.w; x++) { 18 | s = 0; 19 | 20 | var b = data[x]; 21 | if((b & 0x01 << y) != 0) { 22 | s = z; 23 | } 24 | line.push(s); 25 | } 26 | bitmap.push(line); 27 | } 28 | 29 | return bitmap; 30 | }; 31 | 32 | // Text to bitmap 33 | var textToBitmap = function(text, z) { 34 | z = z || 1; 35 | 36 | var bitmap = []; 37 | var lines = text.split("\n"); 38 | return bitmapUtils.merge( 39 | _.chain(lines) 40 | .map(function(line) { 41 | return bitmapUtils.merge(_.map(line.replace("\n", ""), function(c) { 42 | return charToBitmap(c, z) 43 | }), 1, 0); 44 | }) 45 | .compact() 46 | .value() 47 | , 0, 1); 48 | }; 49 | 50 | 51 | module.exports = { 52 | charToBitmap: charToBitmap, 53 | textToBitmap: textToBitmap 54 | }; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "code2stl", 3 | "version": "0.0.1", 4 | "description": "Transform a source code file into a 3D STL", 5 | "author": "Samy Pesse ", 6 | "keywords": [ 7 | "3D", "stl" 8 | ], 9 | "homepage": "https://github.com/SamyPesse/code2stl", 10 | "main": "./lib/index.js", 11 | "dependencies": { 12 | "q": "1.0.1", 13 | "lodash": "2.4.1", 14 | "commander": "2.2.0", 15 | "stl": "0.1.2", 16 | "ndarray-pack": "1.1.1", 17 | "bitmap-to-boxes": "0.0.0", 18 | "face-normals": "0.0.0" 19 | }, 20 | "bin": { 21 | "code2stl": "./bin/convert.js" 22 | }, 23 | "scripts": { 24 | "test": "./convertall.sh" 25 | } 26 | } -------------------------------------------------------------------------------- /test/fib.js: -------------------------------------------------------------------------------- 1 | var Fib = function(n) { 2 | if (n <= 1) 3 | return n; 4 | return Fib(n-1) + Fib(n-2); 5 | } 6 | 7 | console.log(Fib(40)); 8 | -------------------------------------------------------------------------------- /test/fib.stl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/code2stl/55895b1bbf887b13dd305703aa3e92685126cde9/test/fib.stl -------------------------------------------------------------------------------- /test/hello.js: -------------------------------------------------------------------------------- 1 | var helloWorld = "Hello the world"; 2 | 3 | console.log(helloWorld); -------------------------------------------------------------------------------- /test/hello.stl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/code2stl/55895b1bbf887b13dd305703aa3e92685126cde9/test/hello.stl --------------------------------------------------------------------------------