Skip to content

Commit

Permalink
Functioning release
Browse files Browse the repository at this point in the history
Expanded McMapBook to do the font writing and word-wrapping. Now basically functional. Need to add hyphenation.
  • Loading branch information
Kroc Camen committed Oct 30, 2011
1 parent 2ca5887 commit f5f5257
Show file tree
Hide file tree
Showing 15 changed files with 314 additions and 24 deletions.
Empty file added .gitignore
Empty file.
70 changes: 70 additions & 0 deletions LICENCE.txt
@@ -0,0 +1,70 @@
McMaps © Copyright cc-by 3.0 Kroc Camen of Camen Design
=======================================================
The code and other resources of this project are licensed under a
Creative Commons 3.0 Attribution Unported licence (unless otherwise
stated), viewable here:

http://creativecommons.org/licenses/by/3.0/deed.en_GB

What does this mean? This means that you may do anything you want with
this code, as long as you give credit. How you decide to give credit is
up to you.

NOTE: The fonts included with the code are licenced under their own
terms, please see fonts/LICENCE.txt. Please be aware of this when
redistributing McMaps in any way.


Warranty:
=========
Or lack there-of. No warranty is provided. Once you download this code,
it's yours and your responsibility. The authors are not liable for any
damage, loss of time, money or security breaches. You are expected to
test the code yourself in the environment you are deploying it.


Licence Text:
=============
Creative Commons Attribution 3.0 Unported (CC BY 3.0)

You are free:

• To Share — to copy, distribute and transmit the work
• To Remix — to adapt the work

Under the following conditions:

• Attribution — You must attribute the work in the manner
specified by the author or licensor (but not in any way that
suggests that they endorse you or your use of the work).

With the understanding that:

• Waiver — Any of the above conditions can be waived if you get
permission from the copyright holder.

• Public Domain — Where the work or any of its elements is in the
public domain under applicable law, that status is in no way
affected by the license.

• Other Rights — In no way are any of the following rights
affected by the license:

+ Your fair dealing or fair use rights, or other
applicable copyright exceptions and limitations;

+ The author's moral rights;

+ Rights other persons may have either in the work itself
or in how the work is used, such as publicity or
privacy rights.


Notice — For any reuse or distribution, you must make clear to others
the license terms of this work. The best way to do this is provide a
copy of this file, or link to this file on the web.

This is a human-readable summary of the Legal Code (the full license),
which can be found at:

http://creativecommons.org/licenses/by/3.0/legalcode
29 changes: 29 additions & 0 deletions fonts/LICENCE.txt
@@ -0,0 +1,29 @@
Fonts:

./04b03/
"04b-03" - Copyright © 1998-2003 Yuji Yoshimoto - <dsg4.com/04>
'Freeware - You may use them as you like'

./alicia_marie/
"Alicia Marie" - Alicia Rios
'Free for personal use'

./cazoom.ttf
"CaZOOM" - Sara Batchelor
'Free for personal use'

./handy00.ttf
"Handy" - Cal Henderson - <iamcal.com/misc/fonts>
'Free'

./nayupixel.ttf
"Nayupixel" - Nathalie D. - <twitter.com/nnathaliee>
'Free for personal use'

./uni_05_x/
Uni 05 - Craig Kroeger - <miniml.com>
'free to use in commercial or personal work'

./XPAIDERP.TTF
Pixel Explosion 01 - Xpaider
? - assumed free
Binary file added fonts/XPAIDERP.TTF
Binary file not shown.
Binary file added fonts/cazoom.ttf
Binary file not shown.
Binary file added fonts/handy00.ttf
Binary file not shown.
Binary file added fonts/nayupixel.ttf
Binary file not shown.
Binary file added fonts/uni_05_x/uni05_53.ttf
Binary file not shown.
Binary file added fonts/uni_05_x/uni05_54.ttf
Binary file not shown.
Binary file added fonts/uni_05_x/uni05_63.ttf
Binary file not shown.
Binary file added fonts/uni_05_x/uni05_64.ttf
Binary file not shown.
54 changes: 33 additions & 21 deletions mcmap.php
@@ -1,28 +1,25 @@
<?php

/* a script to write text to a Minecraft map file.
/* a class to read and write Minecraft map item files
copyright © cc-by 2011 Kroc Camen <camendesign.com>
uses NBT Decoder / Encoder for PHP by Justin Martin */

require_once 'nbt/nbt.class.php';

//set font directory
putenv ('GDFONTPATH='.realpath ('./fonts'));

class McMap {
public $map; //the NBT class for reading/writing the file structure
public $image; //the GD image handle

private $palette = array (); //Notch’s palette for map items
private $nbt; //the NBT class for reading/writing the file structure

public $fonts = array (
//pts = GD points size (instead of px), h = line height
'alicia_marie' => array ('pts' => 6, 'h' => 8, 'ttf' => 'alicia_marie/alicia_marie')
);
public $image; //the GD image handle
public $palette = array (); //Notch’s palette for map items

function __construct () {
//instantiate a new NBT strucutre
$this->map = new NBT ();
$this->map->verbose = true;
$this->nbt = new NBT ();
//populate the default data for a Minecraft map item
$this->map->root[0] = array ('name' => '', 'type' => NBT::TAG_COMPOUND, 'value' => array (
$this->nbt->root[0] = array ('name' => '', 'type' => NBT::TAG_COMPOUND, 'value' => array (
array ('name' => 'data', 'type' => NBT::TAG_COMPOUND, 'value' => array (
//1:1 zoom
array ('name' => 'scale', 'type' => NBT::TAG_BYTE, 'value' => 0),
Expand Down Expand Up @@ -108,30 +105,45 @@ function __construct () {
) as $colour)
$this->palette[] = imagecolorallocate ($this->image, $colour['r'], $colour['g'], $colour['b'])
;

//set font directory
putenv ('GDFONTPATH='.realpath ('./fonts'));
//the ‘void’
imagecolortransparent ($this->image, $this->palette[0]);
}

public function writeText ($x, $y, $color_id, $font, $text) {
//the negative version of the colour index turns anti-aliasing off (crashes Minecraft otherwise)
public function writeText ($x, $y, $color_id, $ttf, $pts, $text) {
return imagettftext (
$this->image, $font['pts'], 0, $x, $y, -1*abs ($this->palette[$color_id]), $font['ttf'], $text
//the negative version of the colour index turns anti-aliasing off (crashes Minecraft otherwise)
$this->image, $pts, 0, $x, $y, -1 * $this->palette[$color_id], $ttf, $text
);
}

/* load: read in a map file and paint it onto the GD image for further manipulation
---------------------------------------------------------------------------------------------------------------*/
public function load ($file) {
$this->nbt->purge ();
$this->nbt->loadFile ($file);

//which element is the 'colors' array?
foreach ($this->nbt->root[0]['value'][0]['value'] as &$node) if ($node['name'] == 'colors') break;

for ($y=0; $y < 128; $y++) for ($x=0; $x < 128 ; $x++)
@imagesetpixel ($this->image, $x, $y, $this->palette[$node['value'][$x + ($y*128)]])
;
}

/* save: take the GD image, put it into the byte array, and save to disk
-------------------------------------------------------------------------------------------------------------- */
public function save ($file) {
//update the image data in the NBT:
for ($y=0; $y < 128; $y++) for ($x=0; $x < 128 ; $x++)
$this->map->root[0]['value'][0]['value'][6]['value'][$x + ($y*128)] = imagecolorat ($this->image, $x, $y)
$this->nbt->root[0]['value'][0]['value'][6]['value'][$x + ($y*128)] = imagecolorat ($this->image, $x, $y)
;
//save the NBT file to disk
return $this->map->writeFile ($file);
return $this->nbt->writeFile ($file);
}

function __destruct () {
//release handles, everything else should go away by itself
unset ($this->map);
unset ($this->nbt);
imagedestroy ($this->image);
}
}
Expand Down
142 changes: 140 additions & 2 deletions mcmapbook.php
@@ -1,12 +1,150 @@
<?php

//takes a lot of text and automatically generates sequential pages with word-wrapping
/* a class to draw lots of text into Minecraft map items
copyright © cc-by 2011 Kroc Camen <camendesign.com>
uses NBT Decoder / Encoder for PHP by Justin Martin */

class McMapBook {
public function generate ($beginning_map_id) {
public $fonts = array (
//(note: ['pts'] = GD points size (instead of px), ['h'] = line height)

//"04b-03" - Copyright © 1998-2003 Yuji Yoshimoto - <dsg4.com/04>
//'Freeware - You may use them as you like'
'04b-03' => array ('pts' => 6, 'h' => 8, 'ttf' => '04b03/04B_03__'),

//"Alicia Marie" - Alicia Rios
//'Free for personal use'
'Alicia Marie' => array ('pts' => 6, 'h' => 8, 'ttf' => 'alicia_marie/alicia_marie'),

//"CaZOOM" - Sara Batchelor
//'Free for personal use'
'CaZOOM' => array ('pts' => 7, 'h' => 8, 'ttf' => 'cazoom'),

//"Handy" - Cal Henderson - <iamcal.com/misc/fonts>
//'Free'
'Handy' => array ('pts' => 6, 'h' => 10, 'ttf' => 'handy00'),

//"Nayupixel" - Nathalie D. - <twitter.com/nnathaliee>
//'Free for personal use'
'Nayupixel' => array ('pts' => 6, 'h' => 8, 'ttf' => 'nayupixel'),

//Uni 05 - Craig Kroeger - <miniml.com>
//'free to use in commercial or personal work'
'Uni 05' => array ('pts' => 6, 'h' => 8, 'ttf' => 'uni_05_x/uni05_53'),

//Pixel Explosion 01 - Xpaider
//? - assumed free
'Pixel Explosion' => array ('pts' => 5, 'h' => 8, 'ttf' => 'XPAIDERP')
);

public $font;

public $padding = 8; //space to reserve from the writing to the edge of the page

public $path = ''; //the full path to the data folder to write the maps into
public $map_id = 0;

function __construct ($path, $map_id = 0) {
//set default font
$this->font = $this->fonts['Uni 05'];

//set the Minecraft world to modify, and what map number to start at
$this->path = $path;
$this->map_id = $map_id;
}

public function generate ($text) {
//width of a space
$space = $this->textWidth (' ');

$page = 1; //current page number
$x = 0; //current insertion point across the page, in pixels
$y = 2; //current line number (not px) down the page

$map = $this->newPage ($page);

//split into lines of text
$lines = explode ("\n", $text);
foreach ($lines as $line) {
//split into individual words
$words = explode (' ', $line);
while ($word = current ($words)) {
//replace tab chars with four spaces
$word = str_replace("\t", " ", $word);

//will this word fit on the end of the line? if not, word wrap
if ($x + $this->textWidth ($word) > 128 - ($this->padding * 2)) {
//carriage return
$y++; $x = 0;
echo "\n";

//is the page full?
if ($y * $this->font['h'] >= 128) {
//start a new page
$map = $this->nextPage ($map, &$page);
$x = 0; $y = 2;
}
}

//write the word
$this->writeText ($map, $this->padding + $x, $y * $this->font['h'], 55, $word);
echo "$word ";

//proceed to the next word
$x += $this->textWidth ($word) + $space; next ($words);
}

//line-break
next ($lines); $y++; $x = 0;
echo "\n";

//is the page full?
if (current ($lines) && $y * $this->font['h'] >= 128) {
$map = $this->nextPage ($map, $page);
$x = 0; $y = 2;
}
}

$this->savePage ($map, $page);
unset ($map);

//return the next available map ID so that additional books can be generated without overwriting
return $page++;
}

private function newPage ($page) {
$map = new McMap ();

echo "\nPage $page:\n";

//right align page number
$map->writeText (
128 - $this->padding - $this->textWidth ($page), $this->font['h'],
55, $this->font['ttf'], $this->font['pts'], $page
);

return $map;
}

private function savePage (&$map, $page) {
$id = $this->map_id + ($page - 1);
imagepng ($map->image, "map_$id.png", 9);
return $map->save ("/Users/kroc/Library/Application Support/minecraft/saves/Island/data/map_$id.dat");
}

private function nextPage (&$map, &$page) {
$this->savePage ($map, $page);
unset ($map);
return $this->newPage (++$page, $this->font);
}

public function textWidth ($text) {
$box = imagettfbbox ($this->font['pts'], 0, $this->font['ttf'], $text);
return $box[2] - $box[6];
}

public function writeText ($map, $x, $y, $colour, $text) {
return $map->writeText ($x, $y, $colour, $this->font['ttf'], $this->font['pts'], $text);
}
}

Expand Down
2 changes: 1 addition & 1 deletion nbt
Submodule nbt updated from a62595 to 4e85a9
41 changes: 41 additions & 0 deletions test.php.sh
@@ -0,0 +1,41 @@
#!/usr/bin/php
<?php

/* NOTE: This is a shell script. Please make it executable (`chmod +x test.php.sh`),
and run it from the terminal (`./test.php.sh`).
*/

include 'mcmap.php';
include 'mcmapbook.php';

$book = new McMapBook (
//the full path to the data folder of the Minecraft world, e.g. '~/Application Support/minecraft/saves/World1/data/'
'.',
//the map ID to begin writing at, i.e. 'map_0.dat'
0
);

//note: ensure the number of maps required already exist in your minecraft world, if you use McMaps to generate a map that
// hasn’t yet been crafted in the game, when you craft it, it will be overwritten. just run your McMaps script again

//you can use the next ID returned to start the next book
$next_id = $book->generate (<<<TXT
Leisure by W. H. Davies
What is this life if, full of care, we have no time to stand and stare?—
No time to stand beneath the boughs, and stare as long as sheep and cows:
No time to see, when woods we pass, where squirrels hide their nuts in grass:
No time to see, in broad daylight, streams full of stars, like skies at night:
No time to turn at Beauty's glance, and watch her feet, how they can dance:
No time to wait till her mouth can enrich that smile her eyes began?
A poor life this if, full of care, we have no time to stand and stare.
TXT
);

?>

0 comments on commit f5f5257

Please sign in to comment.