dual-faced cards now print much more sanely
also eliminated some duplicated/unnecessary logic
This commit is contained in:
		
							parent
							
								
									78bad5b043
								
							
						
					
					
						commit
						5ad5b2edf3
					
				
							
								
								
									
										948
									
								
								output.pdf
								
								
								
								
							
							
						
						
									
										948
									
								
								output.pdf
								
								
								
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							|  | @ -1,4 +1,3 @@ | ||||||
| //TODO: print mana cost beside each name for dual-faced cards |  | ||||||
| //TODO: consider eliminating the TextCard struct | //TODO: consider eliminating the TextCard struct | ||||||
| //TODO: add some kind of "update" command to support pulling new oracle data | //TODO: add some kind of "update" command to support pulling new oracle data | ||||||
| //TODO: implement oracleFileName as a cli arg | //TODO: implement oracleFileName as a cli arg | ||||||
|  | @ -37,6 +36,7 @@ const Card = struct { | ||||||
|     toughness: ?[]const u8 = null,  //coerced to string |     toughness: ?[]const u8 = null,  //coerced to string | ||||||
|     card_faces: ?[]Card = null,     //array of cards |     card_faces: ?[]Card = null,     //array of cards | ||||||
|     loyalty: ?[]const u8 = null,    //coerced to string |     loyalty: ?[]const u8 = null,    //coerced to string | ||||||
|  |     isFace: bool = undefined,        //cheeky little property that I added | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| const TextCard = struct { | const TextCard = struct { | ||||||
|  | @ -105,23 +105,21 @@ pub fn main() !void { | ||||||
|     var jsonReader = json.reader(allocator, oracleFile.reader()); |     var jsonReader = json.reader(allocator, oracleFile.reader()); | ||||||
|     const parsedJson = try json.parseFromTokenSource([]Card, allocator, &jsonReader, .{ .ignore_unknown_fields = true }); |     const parsedJson = try json.parseFromTokenSource([]Card, allocator, &jsonReader, .{ .ignore_unknown_fields = true }); | ||||||
| 
 | 
 | ||||||
|     var cardNames = std.ArrayList([]const u8).init(allocator); |     var cardNames = std.BufSet.init(allocator); | ||||||
|     const listText = try cwd.readFileAlloc(allocator, listFileName, 1024 * 100); |     const listText = try cwd.readFileAlloc(allocator, listFileName, 1024 * 100); | ||||||
|     var listLines = std.mem.splitAny(u8, listText, "\n"); |     var listLines = std.mem.splitAny(u8, listText, "\n"); | ||||||
|     while (listLines.next()) |line| { |     while (listLines.next()) |line| { | ||||||
|         if (line.len < 5) break; |         if (line.len < 5) break; | ||||||
|         const cardName = line[indexOf(u8, line, " ").? + 1 .. indexOf(u8, line, "(").? - 1]; |         const cardName = line[indexOf(u8, line, " ").? + 1 .. indexOf(u8, line, "(").? - 1]; | ||||||
|         try cardNames.append(try allocator.dupe(u8, cardName)); |         try cardNames.insert(cardName); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     var cards = std.StringArrayHashMap(TextCard).init(allocator); |     var cards = std.StringArrayHashMap(TextCard).init(allocator); | ||||||
|     for (parsedJson.value) |cardObj| { |     for (parsedJson.value) |*cardObj| { | ||||||
|         for (cardNames.items, 0..) |cardName, i| { |             if (cardNames.contains(cardObj.name)) { | ||||||
|             if (std.mem.eql(u8, cardName, cardObj.name)) { |                 cardObj.isFace = false; | ||||||
|                 const printableCard = try card(cardObj, allocator, false); |                 const printableCard = try card(cardObj.*, allocator); | ||||||
|                 try cards.put(cardObj.name, printableCard); |                 try cards.put(cardObj.name, printableCard); | ||||||
|                 _ = cardNames.orderedRemove(i); |  | ||||||
|             } |  | ||||||
|             } |             } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | @ -148,12 +146,10 @@ pub fn main() !void { | ||||||
|     for (allPrinted.items, 1..) |text, rowNum| { |     for (allPrinted.items, 1..) |text, rowNum| { | ||||||
|         const pageRelative = rowNum % pageHeight; |         const pageRelative = rowNum % pageHeight; | ||||||
|         const pageOffset: f32 = 12 * @as(f32, @floatFromInt(pageRelative)); |         const pageOffset: f32 = 12 * @as(f32, @floatFromInt(pageRelative)); | ||||||
|         if (pageRelative != 0) { |         if (pageRelative == 0) { | ||||||
|             _ = c.pdf_add_text(pdf_doc, page, try std.mem.Allocator.dupeZ(allocator, u8, text), 8, 10, pdfHeight - pageOffset, c.PDF_BLACK); |  | ||||||
|         } else { |  | ||||||
|             page = c.pdf_append_page(pdf_doc); |             page = c.pdf_append_page(pdf_doc); | ||||||
|             _ = c.pdf_add_text(pdf_doc, page, try std.mem.Allocator.dupeZ(allocator, u8, text), 8, 10, pdfHeight - pageOffset, c.PDF_BLACK); |  | ||||||
|         } |         } | ||||||
|  |         _ = c.pdf_add_text(pdf_doc, page, try std.mem.Allocator.dupeZ(allocator, u8, text), 8, 10, pdfHeight - pageOffset, c.PDF_BLACK); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     _ = c.pdf_save(pdf_doc, "output.pdf"); |     _ = c.pdf_save(pdf_doc, "output.pdf"); | ||||||
|  | @ -163,12 +159,12 @@ pub fn main() !void { | ||||||
| fn card( | fn card( | ||||||
|     cardObj: Card, |     cardObj: Card, | ||||||
|     allocator: std.mem.Allocator, |     allocator: std.mem.Allocator, | ||||||
|     isFace: bool, |  | ||||||
| ) !TextCard { | ) !TextCard { | ||||||
|     var cardText = std.ArrayList([]const u8).init(allocator); |     var cardText = std.ArrayList([]const u8).init(allocator); | ||||||
| 
 | 
 | ||||||
|     var fullUnformattedText = std.ArrayList(u8).init(allocator); |     var fullUnformattedText = std.ArrayList(u8).init(allocator); | ||||||
|     if (!isFace) { | 
 | ||||||
|  |     if (cardObj.card_faces == null or cardObj.isFace) { | ||||||
|         try fullUnformattedText.appendSlice(try std.mem.concat(allocator, u8, &[_][]const u8{ |         try fullUnformattedText.appendSlice(try std.mem.concat(allocator, u8, &[_][]const u8{ | ||||||
|             cardObj.name, |             cardObj.name, | ||||||
|             if (cardObj.mana_cost.len > 0) " " else "", |             if (cardObj.mana_cost.len > 0) " " else "", | ||||||
|  | @ -176,23 +172,23 @@ fn card( | ||||||
|             " (", |             " (", | ||||||
|             cardObj.type_line, |             cardObj.type_line, | ||||||
|             ") >> ", |             ") >> ", | ||||||
|         })); |  | ||||||
|     } |  | ||||||
|     try fullUnformattedText.appendSlice(try std.mem.concat(allocator, u8, &[_][]const u8{ |  | ||||||
|             cardObj.oracle_text, |             cardObj.oracle_text, | ||||||
|             if (cardObj.power) |_| " (" else "", |             if (cardObj.power) |_| " (" else "", | ||||||
|             cardObj.power orelse "", |             cardObj.power orelse "", | ||||||
|             if (cardObj.power) |_| "/" else "", |             if (cardObj.power) |_| "/" else "", | ||||||
|             cardObj.toughness orelse "", |             cardObj.toughness orelse "", | ||||||
|             if (cardObj.power) |_| ") " else "", |             if (cardObj.power) |_| ") " else "", | ||||||
| 
 |  | ||||||
|             if(cardObj.loyalty) |_| "[" else "", |             if(cardObj.loyalty) |_| "[" else "", | ||||||
|             cardObj.loyalty orelse "", |             cardObj.loyalty orelse "", | ||||||
|             if(cardObj.loyalty) |_| "]" else "", |             if(cardObj.loyalty) |_| "]" else "", | ||||||
|         })); |         })); | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     if (cardObj.card_faces) |faces| { |     if (cardObj.card_faces) |faces| { | ||||||
|         for (faces, 0..) |face, idx| { |         for (faces, 0..) |*face, idx| { | ||||||
|             const faceText = (try card(face, allocator, true)).lines; |             face.isFace = true; | ||||||
|  |             const faceText = (try card(face.*, allocator)).lines; | ||||||
|             try fullUnformattedText.appendSlice(std.mem.trim(u8, try std.mem.join(allocator, " ", faceText), "\n")); |             try fullUnformattedText.appendSlice(std.mem.trim(u8, try std.mem.join(allocator, " ", faceText), "\n")); | ||||||
|             if (idx == 0) try fullUnformattedText.appendSlice("// "); |             if (idx == 0) try fullUnformattedText.appendSlice("// "); | ||||||
|         } |         } | ||||||
|  | @ -201,25 +197,20 @@ fn card( | ||||||
|     var line = std.ArrayList(u8).init(allocator); |     var line = std.ArrayList(u8).init(allocator); | ||||||
|     var wordIterator = std.mem.splitAny(u8, fullUnformattedText.items, "\n "); |     var wordIterator = std.mem.splitAny(u8, fullUnformattedText.items, "\n "); | ||||||
|     while (wordIterator.next()) |word| { |     while (wordIterator.next()) |word| { | ||||||
|         if (line.items.len + word.len + 1 < cardWidth) { |         if (line.items.len + word.len + 1 >= cardWidth) { | ||||||
|  |             try cardText.append(try line.toOwnedSlice()); | ||||||
|  |             line.clearAndFree(); | ||||||
|  |         } | ||||||
|         try line.appendSlice(word); |         try line.appendSlice(word); | ||||||
|         try line.append(' '); |         try line.append(' '); | ||||||
|         assert(line.items.len < 30); |         assert(line.items.len < 30); | ||||||
|     } else { |     } else { | ||||||
|         try cardText.append(try line.toOwnedSlice()); |         try cardText.append(try line.toOwnedSlice()); | ||||||
|             line.clearAndFree(); |  | ||||||
|             try line.appendSlice(word); |  | ||||||
|             try line.append(' '); |  | ||||||
|     } |     } | ||||||
|     } else { |     if(!cardObj.isFace and constantHeight) { | ||||||
|         try cardText.append(try line.toOwnedSlice()); |         assert(cardText.items.len <= cardHeight); | ||||||
|     } |         try cardText.appendNTimes(" " ** (cardWidth - 2), cardHeight - cardText.items.len); | ||||||
|     if(!isFace and constantHeight) { |     } else if(!cardObj.isFace) { | ||||||
|         const actualHeight: usize = cardText.items.len; |  | ||||||
|         assert(actualHeight <= cardHeight); |  | ||||||
|         const diff = cardHeight - actualHeight; |  | ||||||
|         try cardText.appendNTimes(" " ** (cardWidth - 2), diff); |  | ||||||
|     } else if(!isFace) { |  | ||||||
|         try cardText.append(" " ** (cardWidth - 2)); |         try cardText.append(" " ** (cardWidth - 2)); | ||||||
|     } |     } | ||||||
|     assert(cardText.items.len <= cardHeight); |     assert(cardText.items.len <= cardHeight); | ||||||
|  |  | ||||||
										
											Binary file not shown.
										
									
								
							
		Loading…
	
		Reference in New Issue